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