]> mj.ucw.cz Git - libucw.git/blob - images/io-libjpeg.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git#v3.10
[libucw.git] / images / io-libjpeg.c
1 /*
2  *      Image Library -- libjpeg
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/error.h"
17 #include "images/color.h"
18 #include "images/io-main.h"
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <jpeglib.h>
23 #include <jerror.h>
24 #include <setjmp.h>
25
26 struct libjpeg_err {
27   struct jpeg_error_mgr pub;
28   jmp_buf setjmp_buf;
29   struct image_io *io;
30 };
31
32 struct libjpeg_read_internals {
33   struct jpeg_decompress_struct cinfo;
34   struct jpeg_source_mgr src;
35   struct libjpeg_err err;
36   struct fastbuf *fastbuf;
37   byte *fastbuf_pos;
38 };
39
40 struct libjpeg_write_internals {
41   struct jpeg_compress_struct cinfo;
42   struct jpeg_destination_mgr dest;
43   struct libjpeg_err err;
44   struct fastbuf *fastbuf;
45   byte *fastbuf_pos;
46 };
47
48 static void NONRET
49 libjpeg_read_error_exit(j_common_ptr cinfo)
50 {
51   DBG("libjpeg_error_exit()");
52   struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
53   byte buf[JMSG_LENGTH_MAX];
54   e->pub.format_message(cinfo, buf);
55   IMAGE_ERROR(e->io->context, IMAGE_ERROR_READ_FAILED, "libjpeg: %s", buf);
56   longjmp(e->setjmp_buf, 1);
57 }
58
59 static void NONRET
60 libjpeg_write_error_exit(j_common_ptr cinfo)
61 {
62   DBG("libjpeg_error_exit()");
63   struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
64   byte buf[JMSG_LENGTH_MAX];
65   e->pub.format_message(cinfo, buf);
66   IMAGE_ERROR(e->io->context, IMAGE_ERROR_WRITE_FAILED, "libjpeg: %s", buf);
67   longjmp(e->setjmp_buf, 1);
68 }
69
70 static void
71 libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
72 {
73 #ifdef LOCAL_DEBUG
74   byte buf[JMSG_LENGTH_MAX];
75   cinfo->err->format_message(cinfo, buf);
76   DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf);
77 #endif
78 #if 0
79   // Terminate on warning?
80   if (unlikely(msg_level == -1))
81     {
82       struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
83       byte buf[JMSG_LENGTH_MAX];
84       cinfo->err->format_message(cinfo, buf);
85       IMAGE_ERROR(e->io->context, 0, "libjpeg: %s", buf);
86       longjmp(e->setjmp_buf, 1);
87     }
88 #endif
89 }
90
91 static inline uns
92 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
93 {
94   DBG("libjpeg_fb_read_prepare()");
95   byte *start;
96   uns len = bdirect_read_prepare(i->fastbuf, &start);
97   DBG("readed %u bytes at %p", len, start);
98   if (!len)
99     {
100       // XXX: maybe only generate a warning and return EOI markers to recover from such errors (also in skip_input_data)
101       IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file");
102       longjmp(i->err.setjmp_buf, 1);
103     }
104   i->fastbuf_pos = start + len;
105   i->src.next_input_byte = start;
106   i->src.bytes_in_buffer = len;
107   return len;
108 }
109
110 static inline void
111 libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i)
112 {
113   DBG("libjpeg_fb_read_commit()");
114   bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
115 }
116
117 static void
118 libjpeg_init_source(j_decompress_ptr cinfo)
119 {
120   DBG("libjpeg_init_source()");
121   libjpeg_fastbuf_read_prepare((struct libjpeg_read_internals *)cinfo);
122 }
123
124 static void
125 libjpeg_term_source(j_decompress_ptr cinfo UNUSED)
126 {
127   DBG("libjpeg_term_source()");
128   //libjpeg_fastbuf_read_commit((struct libjpeg_read_internals *)cinfo);
129 }
130
131 static boolean
132 libjpeg_fill_input_buffer(j_decompress_ptr cinfo)
133 {
134   DBG("libjpeg_fill_input_buffer()");
135   struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
136   libjpeg_fastbuf_read_commit(i);
137   libjpeg_fastbuf_read_prepare(i);
138   return 1;
139 }
140
141 static void
142 libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
143 {
144   DBG("libjpeg_skip_input_data(num_bytes=%d)", (int)num_bytes);
145   if (num_bytes > 0)
146     {
147       struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
148       if ((unsigned long)num_bytes <= i->src.bytes_in_buffer)
149         {
150           i->src.next_input_byte += num_bytes;
151           i->src.bytes_in_buffer -= num_bytes;
152         }
153       else
154         {
155           num_bytes -= i->src.bytes_in_buffer;
156           libjpeg_fastbuf_read_commit(i);
157           if (!bskip(i->fastbuf, num_bytes))
158             {
159               IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file");
160               longjmp(i->err.setjmp_buf, 1);
161             }
162           libjpeg_fastbuf_read_prepare(i);
163         }
164     }
165 }
166
167 static inline void
168 libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i)
169 {
170   byte *start;
171   uns len = bdirect_write_prepare(i->fastbuf, &start);
172   i->fastbuf_pos = start + len;
173   i->dest.next_output_byte = start;
174   i->dest.free_in_buffer = len;
175   if (!len)
176     {
177       IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_WRITE_FAILED, "Unexpected end of stream");
178       longjmp(i->err.setjmp_buf, 1);
179     }
180 }
181
182 static void
183 libjpeg_init_destination(j_compress_ptr cinfo)
184 {
185   DBG("libjpeg_init_destination()");
186   libjpeg_fastbuf_write_prepare((struct libjpeg_write_internals *)cinfo);
187 }
188
189 static void
190 libjpeg_term_destination(j_compress_ptr cinfo)
191 {
192   DBG("libjpeg_term_destination()");
193   struct libjpeg_write_internals *i = (struct libjpeg_write_internals *)cinfo;
194   bdirect_write_commit(i->fastbuf, (byte *)i->dest.next_output_byte);
195 }
196
197 static boolean
198 libjpeg_empty_output_buffer(j_compress_ptr cinfo)
199 {
200   DBG("libjpeg_empty_output_buffer()");
201   struct libjpeg_write_internals *i = (struct libjpeg_write_internals *)cinfo;
202   bdirect_write_commit(i->fastbuf, i->fastbuf_pos);
203   libjpeg_fastbuf_write_prepare(i);
204   return TRUE;
205 }
206
207 static inline uns
208 libjpeg_read_byte(struct libjpeg_read_internals *i)
209 {
210   DBG("libjpeg_read_byte()");
211   if (!i->src.bytes_in_buffer)
212     if (!libjpeg_fill_input_buffer(&i->cinfo))
213       ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
214   i->src.bytes_in_buffer--;
215   return *i->src.next_input_byte++;
216 }
217
218 static inline void
219 libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
220 {
221   DBG("libjpeg_read_buf(len=%u)", len);
222   while (len)
223     {
224       if (!i->src.bytes_in_buffer)
225         if (!libjpeg_fill_input_buffer(&i->cinfo))
226           ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
227       uns buf_size = i->src.bytes_in_buffer;
228       uns read_size = MIN(buf_size, len);
229       memcpy(buf, i->src.next_input_byte, read_size);
230       i->src.bytes_in_buffer -= read_size;
231       i->src.next_input_byte += read_size;
232       len -= read_size;
233     }
234 }
235
236 static byte libjpeg_exif_header[6] = { 'E', 'x', 'i', 'f', 0, 0 };
237
238 static boolean
239 libjpeg_app1_preprocessor(j_decompress_ptr cinfo)
240 {
241   struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
242   struct image_io *io = i->err.io;
243   uns len = libjpeg_read_byte(i) << 8;
244   len += libjpeg_read_byte(i);
245   DBG("Found APP1 marker, len=%u", len);
246   if (len < 2)
247     return TRUE;
248   len -= 2;
249   if (len < 7 /*|| io->exif_size*/)
250     {
251       libjpeg_skip_input_data(cinfo, len);
252       return TRUE;
253     }
254   byte header[6];
255   libjpeg_read_buf(i, header, 6);
256   if (memcmp(header, libjpeg_exif_header, 6))
257     {
258       libjpeg_skip_input_data(cinfo, len - 6);
259       return TRUE;
260     }
261   io->exif_size = len;
262   io->exif_data = mp_alloc(io->internal_pool, len);
263   memcpy(io->exif_data, header, 6);
264   libjpeg_read_buf(i, io->exif_data + 6, len - 6);
265   DBG("Parsed EXIF of length %u", len);
266   return TRUE;
267 }
268
269 static void
270 libjpeg_read_cancel(struct image_io *io)
271 {
272   DBG("libjpeg_read_cancel()");
273   struct libjpeg_read_internals *i = io->read_data;
274   jpeg_destroy_decompress(&i->cinfo);
275 }
276
277 int
278 libjpeg_read_header(struct image_io *io)
279 {
280   DBG("libjpeg_read_header()");
281   struct libjpeg_read_internals *i = io->read_data = mp_alloc(io->internal_pool, sizeof(*i));
282   i->fastbuf = io->fastbuf;
283
284   /* Create libjpeg read structure */
285   DBG("Creating libjpeg read structure");
286   i->cinfo.err = jpeg_std_error(&i->err.pub);
287   i->err.pub.error_exit = libjpeg_read_error_exit;
288   i->err.pub.emit_message = libjpeg_emit_message;
289   i->err.io = io;
290   if (setjmp(i->err.setjmp_buf))
291     {
292       DBG("Libjpeg failed to read the image, longjump saved us");
293       jpeg_destroy_decompress(&i->cinfo);
294       return 0;
295     }
296   jpeg_create_decompress(&i->cinfo);
297
298   /* Initialize source manager */
299   i->cinfo.src = &i->src;
300   i->src.init_source = libjpeg_init_source;
301   i->src.fill_input_buffer = libjpeg_fill_input_buffer;
302   i->src.skip_input_data = libjpeg_skip_input_data;
303   i->src.resync_to_restart = jpeg_resync_to_restart;
304   i->src.term_source = libjpeg_term_source;
305
306   if (io->flags & IMAGE_IO_WANT_EXIF)
307     jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor);
308
309   /* Read JPEG header and setup decompression options */
310   DBG("Reading image header");
311   jpeg_read_header(&i->cinfo, TRUE);
312   switch (i->cinfo.jpeg_color_space)
313     {
314       case JCS_GRAYSCALE:
315         io->flags = COLOR_SPACE_GRAYSCALE;
316         break;
317       case JCS_RGB:
318         io->flags = COLOR_SPACE_RGB;
319         break;
320       case JCS_YCbCr:
321         io->flags = COLOR_SPACE_YCBCR;
322         break;
323       case JCS_CMYK:
324         io->flags = COLOR_SPACE_CMYK;
325         break;
326       case JCS_YCCK:
327         io->flags = COLOR_SPACE_YCCK;
328         break;
329       default:
330         if (unlikely(i->cinfo.num_components < 1 || i->cinfo.num_components > 4))
331           {
332             jpeg_destroy_decompress(&i->cinfo);
333             IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Invalid color space.");
334             return 0;
335           }
336         io->flags = COLOR_SPACE_UNKNOWN + i->cinfo.num_components;
337         break;
338     }
339   if (unlikely(i->cinfo.num_components != (int)color_space_channels[io->flags]))
340     {
341       jpeg_destroy_decompress(&i->cinfo);
342       IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Invalid number of color channels.");
343       return 0;
344     }
345   io->cols = i->cinfo.image_width;
346   io->rows = i->cinfo.image_height;
347   io->number_of_colors = (i->cinfo.num_components < 4) ? (1U << (i->cinfo.num_components * 8)) : 0xffffffff;
348   io->read_cancel = libjpeg_read_cancel;
349   return 1;
350 }
351
352 int
353 libjpeg_read_data(struct image_io *io)
354 {
355   DBG("libjpeg_read_data()");
356
357   struct libjpeg_read_internals *i = io->read_data;
358   uns read_flags = io->flags;
359
360   /* Select color space */
361   switch (read_flags & IMAGE_COLOR_SPACE)
362     {
363       case COLOR_SPACE_GRAYSCALE:
364         i->cinfo.out_color_space = JCS_GRAYSCALE;
365         break;
366       case COLOR_SPACE_YCBCR:
367         i->cinfo.out_color_space = JCS_YCbCr;
368         break;
369       case COLOR_SPACE_CMYK:
370         i->cinfo.out_color_space = JCS_CMYK;
371         break;
372       case COLOR_SPACE_YCCK:
373         i->cinfo.out_color_space = JCS_YCCK;
374         break;
375       default:
376         switch (i->cinfo.jpeg_color_space)
377           {
378             case JCS_CMYK:
379               read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_CMYK; 
380               i->cinfo.out_color_space = JCS_CMYK;
381               break;
382             case JCS_YCCK:
383               read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCCK; 
384               i->cinfo.out_color_space = JCS_YCCK;
385               break;
386             default:
387               read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; 
388               i->cinfo.out_color_space = JCS_RGB;
389               break;
390           }
391         break;
392     }
393
394   /* Prepare the image  */
395   struct image_io_read_data_internals rdi;
396   if (io->cols <= (i->cinfo.image_width >> 3) && io->rows <= (i->cinfo.image_height >> 3))
397     {
398       DBG("Scaling to 1/8");
399       i->cinfo.scale_num = 1;
400       i->cinfo.scale_denom = 8;
401     }
402   else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
403     {
404       DBG("Scaling to 1/4");
405       i->cinfo.scale_num = 1;
406       i->cinfo.scale_denom = 4;
407     }
408   else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
409     {
410       DBG("Scaling to 1/2");
411       i->cinfo.scale_num = 1;
412       i->cinfo.scale_denom = 2;
413     }
414   jpeg_calc_output_dimensions(&i->cinfo);
415   DBG("Output dimensions %ux%u", (uns)i->cinfo.output_width, (uns)i->cinfo.output_height);
416   if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, read_flags)))
417     {
418       jpeg_destroy_decompress(&i->cinfo);
419       return 0;
420     }
421
422   /* Setup fallback */
423   if (setjmp(i->err.setjmp_buf))
424     {
425       DBG("Libjpeg failed to read the image, longjump saved us");
426       jpeg_destroy_decompress(&i->cinfo);
427       image_io_read_data_break(&rdi, io);
428       return 0;
429     }
430
431   /* Decompress the image */
432   struct image *img = rdi.image;
433   jpeg_start_decompress(&i->cinfo);
434   if ((int)img->pixel_size == i->cinfo.output_components)
435     {
436       byte *pixels = img->pixels;
437       for (uns r = img->rows; r--; )
438         {
439           jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
440           pixels += img->row_size;
441         }
442     }
443   else
444     {
445       switch (img->pixel_size)
446         {
447           case 2: /* Grayscale -> Grayscale+Alpha */
448             {
449               ASSERT(i->cinfo.output_components == 1);
450               byte buf[img->cols], *src;
451 #             define IMAGE_WALK_PREFIX(x) walk_##x
452 #             define IMAGE_WALK_INLINE
453 #             define IMAGE_WALK_IMAGE img
454 #             define IMAGE_WALK_UNROLL 4
455 #             define IMAGE_WALK_COL_STEP 2
456 #             define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
457 #             define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
458 #             include "images/image-walk.h"
459             }
460             break;
461           case 4: /* * -> *+Alpha or aligned * */
462             {
463               ASSERT(i->cinfo.output_components == 3);
464               byte buf[img->cols * 3], *src;
465 #             define IMAGE_WALK_PREFIX(x) walk_##x
466 #             define IMAGE_WALK_INLINE
467 #             define IMAGE_WALK_IMAGE img
468 #             define IMAGE_WALK_UNROLL 4
469 #             define IMAGE_WALK_COL_STEP 4
470 #             define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
471 #             define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0)
472 #             include "images/image-walk.h"
473             }
474             break;
475           default:
476             ASSERT(0);
477         }
478
479     }
480
481   ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
482
483   /* Destroy libjpeg object */
484   jpeg_finish_decompress(&i->cinfo);
485   jpeg_destroy_decompress(&i->cinfo);
486
487   /* Finish the image */
488   return image_io_read_data_finish(&rdi, io);
489 }
490
491 int
492 libjpeg_write(struct image_io *io)
493 {
494   DBG("libjpeg_write()");
495   struct libjpeg_write_internals i;
496   i.fastbuf = io->fastbuf;
497
498   /* Create libjpeg write structure */
499   DBG("Creating libjpeg write structure");
500   i.cinfo.err = jpeg_std_error(&i.err.pub);
501   i.err.pub.error_exit = libjpeg_write_error_exit;
502   i.err.pub.emit_message = libjpeg_emit_message;
503   i.err.io = io;
504   if (setjmp(i.err.setjmp_buf))
505     {
506       DBG("Libjpeg failed to write the image, longjump saved us");
507       jpeg_destroy_compress(&i.cinfo);
508       return 0;
509     }
510   jpeg_create_compress(&i.cinfo);
511
512   /* Initialize destination manager */
513   i.cinfo.dest = &i.dest;
514   i.dest.init_destination = libjpeg_init_destination;
515   i.dest.term_destination = libjpeg_term_destination;
516   i.dest.empty_output_buffer = libjpeg_empty_output_buffer;
517
518   /* Set output parameters */
519   struct image *img = io->image;
520   i.cinfo.image_width = img->cols;
521   i.cinfo.image_height = img->rows;
522   switch (img->flags & IMAGE_COLOR_SPACE)
523     {
524       case COLOR_SPACE_GRAYSCALE:
525         i.cinfo.in_color_space = JCS_GRAYSCALE;
526         break;
527       case COLOR_SPACE_RGB:
528         i.cinfo.in_color_space = JCS_RGB;
529         break;
530       case COLOR_SPACE_YCBCR:
531         i.cinfo.in_color_space = JCS_YCbCr;
532         break;
533       case COLOR_SPACE_CMYK:
534         i.cinfo.in_color_space = JCS_CMYK;
535         break;
536       case COLOR_SPACE_YCCK:
537         i.cinfo.in_color_space = JCS_YCCK;
538         break;
539       default:
540         jpeg_destroy_compress(&i.cinfo);
541         IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
542         return 0;
543     }
544   i.cinfo.input_components = color_space_channels[img->flags & IMAGE_COLOR_SPACE];
545   jpeg_set_defaults(&i.cinfo);
546   jpeg_set_colorspace(&i.cinfo, i.cinfo.in_color_space);
547   if (io->jpeg_quality)
548     jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
549   if (io->exif_size)
550     {
551       /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI,
552        * just as the JFIF specification requires the same for the JFIF APP0 marker!
553        * Therefore a JPEG file cannot legally be both Exif and JFIF.  */
554       i.cinfo.write_JFIF_header = FALSE;
555       i.cinfo.write_Adobe_marker = FALSE;
556     }
557
558   /* Compress the image */
559   jpeg_start_compress(&i.cinfo, TRUE);
560   if (io->exif_size)
561     {
562       DBG("Writing EXIF");
563       jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
564     }
565   if ((int)img->pixel_size == i.cinfo.input_components)
566     {
567       byte *pixels = img->pixels;
568       for (uns r = img->rows; r--; )
569         {
570           jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
571           pixels += img->row_size;
572         }
573     }
574   else
575     {
576       switch (img->pixel_size)
577         {
578           case 2: /* Grayscale+Alpha -> Grayscale */
579             {
580               ASSERT(i.cinfo.input_components == 1);
581               byte buf[img->cols], *dest = buf;
582 #             define IMAGE_WALK_PREFIX(x) walk_##x
583 #             define IMAGE_WALK_INLINE
584 #             define IMAGE_WALK_IMAGE img
585 #             define IMAGE_WALK_UNROLL 4
586 #             define IMAGE_WALK_COL_STEP 2
587 #             define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
588 #             define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
589 #             include "images/image-walk.h"
590               break;
591             }
592           case 4: /* *+Alpha or aligned * -> * */
593             {
594               ASSERT(i.cinfo.input_components == 3);
595               byte buf[img->cols * 3], *dest = buf;
596 #             define IMAGE_WALK_PREFIX(x) walk_##x
597 #             define IMAGE_WALK_INLINE
598 #             define IMAGE_WALK_IMAGE img
599 #             define IMAGE_WALK_UNROLL 4
600 #             define IMAGE_WALK_COL_STEP 4
601 #             define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
602 #             define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0)
603 #             include "images/image-walk.h"
604               break;
605             }
606           default:
607             ASSERT(0);
608         }
609     }
610   ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
611   jpeg_finish_compress(&i.cinfo);
612   jpeg_destroy_compress(&i.cinfo);
613   return 1;
614 }