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