]> mj.ucw.cz Git - libucw.git/blob - images/io-libjpeg.c
tableprinter: update of user-defined column types
[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 <ucw/lib.h>
13 #include <ucw/mempool.h>
14 #include <ucw/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 uint
92 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
93 {
94   DBG("libjpeg_fb_read_prepare()");
95   byte *start;
96   uint 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   uint 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 uint
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, uint 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       uint buf_size = i->src.bytes_in_buffer;
228       uint 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   uint 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   uint read_flags = io->flags;
359
360   /* Select color space */
361   switch (i->cinfo.jpeg_color_space)
362     {
363       case JCS_GRAYSCALE:
364         read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCBCR; 
365         i->cinfo.out_color_space = JCS_YCbCr;
366         break;
367       case JCS_YCbCr:
368         read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCBCR; 
369         i->cinfo.out_color_space = JCS_YCbCr;
370         break;
371       case JCS_CMYK:
372         read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_CMYK; 
373         i->cinfo.out_color_space = JCS_CMYK;
374         break;
375       case JCS_YCCK:
376         read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCCK; 
377         i->cinfo.out_color_space = JCS_YCCK;
378         break;
379       default:
380         read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; 
381         i->cinfo.out_color_space = JCS_RGB;
382         break;
383   }
384
385   /* Prepare the image  */
386   struct image_io_read_data_internals rdi;
387   if (io->cols <= (i->cinfo.image_width >> 3) && io->rows <= (i->cinfo.image_height >> 3))
388     {
389       DBG("Scaling to 1/8");
390       i->cinfo.scale_num = 1;
391       i->cinfo.scale_denom = 8;
392     }
393   else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
394     {
395       DBG("Scaling to 1/4");
396       i->cinfo.scale_num = 1;
397       i->cinfo.scale_denom = 4;
398     }
399   else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
400     {
401       DBG("Scaling to 1/2");
402       i->cinfo.scale_num = 1;
403       i->cinfo.scale_denom = 2;
404     }
405   jpeg_calc_output_dimensions(&i->cinfo);
406   DBG("Output dimensions %ux%u", (uint)i->cinfo.output_width, (uint)i->cinfo.output_height);
407   if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, read_flags)))
408     {
409       jpeg_destroy_decompress(&i->cinfo);
410       return 0;
411     }
412
413   /* Setup fallback */
414   if (setjmp(i->err.setjmp_buf))
415     {
416       DBG("Libjpeg failed to read the image, longjump saved us");
417       jpeg_destroy_decompress(&i->cinfo);
418       image_io_read_data_break(&rdi, io);
419       return 0;
420     }
421
422   /* Decompress the image */
423   struct image *img = rdi.image;
424   jpeg_start_decompress(&i->cinfo);
425   if ((int)img->pixel_size == i->cinfo.output_components)
426     {
427       byte *pixels = img->pixels;
428       for (uint r = img->rows; r--; )
429         {
430           jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
431           pixels += img->row_size;
432         }
433     }
434   else
435     {
436       switch (img->pixel_size)
437         {
438           case 2: /* Grayscale -> Grayscale+Alpha */
439             {
440               ASSERT(i->cinfo.output_components == 1);
441               byte buf[img->cols], *src;
442 #             define IMAGE_WALK_PREFIX(x) walk_##x
443 #             define IMAGE_WALK_INLINE
444 #             define IMAGE_WALK_IMAGE img
445 #             define IMAGE_WALK_UNROLL 4
446 #             define IMAGE_WALK_COL_STEP 2
447 #             define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
448 #             define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
449 #             include <images/image-walk.h>
450             }
451             break;
452           case 4: /* * -> *+Alpha or aligned * */
453             {
454               ASSERT(i->cinfo.output_components == 3);
455               byte buf[img->cols * 3], *src;
456 #             define IMAGE_WALK_PREFIX(x) walk_##x
457 #             define IMAGE_WALK_INLINE
458 #             define IMAGE_WALK_IMAGE img
459 #             define IMAGE_WALK_UNROLL 4
460 #             define IMAGE_WALK_COL_STEP 4
461 #             define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
462 #             define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0)
463 #             include <images/image-walk.h>
464             }
465             break;
466           default:
467             ASSERT(0);
468         }
469
470     }
471
472   ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
473
474   /* Destroy libjpeg object */
475   jpeg_finish_decompress(&i->cinfo);
476   jpeg_destroy_decompress(&i->cinfo);
477
478   /* Finish the image */
479   return image_io_read_data_finish(&rdi, io);
480 }
481
482 int
483 libjpeg_write(struct image_io *io)
484 {
485   DBG("libjpeg_write()");
486   struct libjpeg_write_internals i;
487   i.fastbuf = io->fastbuf;
488
489   /* Create libjpeg write structure */
490   DBG("Creating libjpeg write structure");
491   i.cinfo.err = jpeg_std_error(&i.err.pub);
492   i.err.pub.error_exit = libjpeg_write_error_exit;
493   i.err.pub.emit_message = libjpeg_emit_message;
494   i.err.io = io;
495   if (setjmp(i.err.setjmp_buf))
496     {
497       DBG("Libjpeg failed to write the image, longjump saved us");
498       jpeg_destroy_compress(&i.cinfo);
499       return 0;
500     }
501   jpeg_create_compress(&i.cinfo);
502
503   /* Initialize destination manager */
504   i.cinfo.dest = &i.dest;
505   i.dest.init_destination = libjpeg_init_destination;
506   i.dest.term_destination = libjpeg_term_destination;
507   i.dest.empty_output_buffer = libjpeg_empty_output_buffer;
508
509   /* Set output parameters */
510   struct image *img = io->image;
511   i.cinfo.image_width = img->cols;
512   i.cinfo.image_height = img->rows;
513   switch (img->flags & IMAGE_COLOR_SPACE)
514     {
515       case COLOR_SPACE_GRAYSCALE:
516         i.cinfo.in_color_space = JCS_GRAYSCALE;
517         break;
518       case COLOR_SPACE_RGB:
519         i.cinfo.in_color_space = JCS_RGB;
520         break;
521       case COLOR_SPACE_YCBCR:
522         i.cinfo.in_color_space = JCS_YCbCr;
523         break;
524       case COLOR_SPACE_CMYK:
525         i.cinfo.in_color_space = JCS_CMYK;
526         break;
527       case COLOR_SPACE_YCCK:
528         i.cinfo.in_color_space = JCS_YCCK;
529         break;
530       default:
531         jpeg_destroy_compress(&i.cinfo);
532         IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
533         return 0;
534     }
535   i.cinfo.input_components = color_space_channels[img->flags & IMAGE_COLOR_SPACE];
536   jpeg_set_defaults(&i.cinfo);
537   jpeg_set_colorspace(&i.cinfo, i.cinfo.in_color_space);
538   if (io->jpeg_quality)
539     jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
540   if (io->exif_size)
541     {
542       /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI,
543        * just as the JFIF specification requires the same for the JFIF APP0 marker!
544        * Therefore a JPEG file cannot legally be both Exif and JFIF.  */
545       i.cinfo.write_JFIF_header = FALSE;
546       i.cinfo.write_Adobe_marker = FALSE;
547     }
548
549   /* Compress the image */
550   jpeg_start_compress(&i.cinfo, TRUE);
551   if (io->exif_size)
552     {
553       DBG("Writing EXIF");
554       jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
555     }
556   if ((int)img->pixel_size == i.cinfo.input_components)
557     {
558       byte *pixels = img->pixels;
559       for (uint r = img->rows; r--; )
560         {
561           jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
562           pixels += img->row_size;
563         }
564     }
565   else
566     {
567       switch (img->pixel_size)
568         {
569           case 2: /* Grayscale+Alpha -> Grayscale */
570             {
571               ASSERT(i.cinfo.input_components == 1);
572               byte buf[img->cols], *dest = buf;
573 #             define IMAGE_WALK_PREFIX(x) walk_##x
574 #             define IMAGE_WALK_INLINE
575 #             define IMAGE_WALK_IMAGE img
576 #             define IMAGE_WALK_UNROLL 4
577 #             define IMAGE_WALK_COL_STEP 2
578 #             define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
579 #             define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
580 #             include <images/image-walk.h>
581               break;
582             }
583           case 4: /* *+Alpha or aligned * -> * */
584             {
585               ASSERT(i.cinfo.input_components == 3);
586               byte buf[img->cols * 3], *dest = buf;
587 #             define IMAGE_WALK_PREFIX(x) walk_##x
588 #             define IMAGE_WALK_INLINE
589 #             define IMAGE_WALK_IMAGE img
590 #             define IMAGE_WALK_UNROLL 4
591 #             define IMAGE_WALK_COL_STEP 4
592 #             define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
593 #             define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0)
594 #             include <images/image-walk.h>
595               break;
596             }
597           default:
598             ASSERT(0);
599         }
600     }
601   ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
602   jpeg_finish_compress(&i.cinfo);
603   jpeg_destroy_compress(&i.cinfo);
604   return 1;
605 }