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