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