2 * Image Library -- libjpeg
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
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"
21 #include <sys/types.h>
27 struct jpeg_error_mgr pub;
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;
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;
49 libjpeg_read_error_exit(j_common_ptr cinfo)
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);
60 libjpeg_write_error_exit(j_common_ptr cinfo)
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);
71 libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
74 byte buf[JMSG_LENGTH_MAX];
75 cinfo->err->format_message(cinfo, buf);
76 DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf);
79 // Terminate on warning?
80 if (unlikely(msg_level == -1))
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);
92 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
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;
103 libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i)
105 bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
109 libjpeg_init_source(j_decompress_ptr cinfo)
111 DBG("libjpeg_init_source()");
112 libjpeg_fastbuf_read_prepare((struct libjpeg_read_internals *)cinfo);
116 libjpeg_term_source(j_decompress_ptr cinfo UNUSED)
118 DBG("libjpeg_term_source()");
119 //libjpeg_fastbuf_read_commit((struct libjpeg_read_internals *)cinfo);
123 libjpeg_fill_input_buffer(j_decompress_ptr cinfo)
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);
132 libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
134 DBG("libjpeg_skip_input_data(num_bytes=%d)", (int)num_bytes);
137 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
138 if ((unsigned long)num_bytes <= i->src.bytes_in_buffer)
140 i->src.next_input_byte += num_bytes;
141 i->src.bytes_in_buffer -= num_bytes;
145 num_bytes -= i->src.bytes_in_buffer;
146 libjpeg_fastbuf_read_commit(i);
147 if (!bskip(i->fastbuf, num_bytes))
149 IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file");
150 longjmp(i->err.setjmp_buf, 1);
152 libjpeg_fastbuf_read_prepare(i);
158 libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i)
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;
167 IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_WRITE_FAILED, "Unexpected end of stream");
168 longjmp(i->err.setjmp_buf, 1);
173 libjpeg_init_destination(j_compress_ptr cinfo)
175 DBG("libjpeg_init_destination()");
176 libjpeg_fastbuf_write_prepare((struct libjpeg_write_internals *)cinfo);
180 libjpeg_term_destination(j_compress_ptr cinfo)
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);
188 libjpeg_empty_output_buffer(j_compress_ptr cinfo)
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);
198 libjpeg_read_byte(struct libjpeg_read_internals *i)
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++;
209 libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
211 DBG("libjpeg_read_buf(len=%u)", len);
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;
226 static byte libjpeg_exif_header[6] = { 'E', 'x', 'i', 'f', 0, 0 };
229 libjpeg_app1_preprocessor(j_decompress_ptr cinfo)
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);
239 if (len < 7 /*|| io->exif_size*/)
241 libjpeg_skip_input_data(cinfo, len);
245 libjpeg_read_buf(i, header, 6);
246 if (memcmp(header, libjpeg_exif_header, 6))
248 libjpeg_skip_input_data(cinfo, len - 6);
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);
260 libjpeg_read_cancel(struct image_io *io)
262 DBG("libjpeg_read_cancel()");
263 struct libjpeg_read_internals *i = io->read_data;
264 jpeg_destroy_decompress(&i->cinfo);
268 libjpeg_read_header(struct image_io *io)
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;
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;
280 if (setjmp(i->err.setjmp_buf))
282 DBG("Libjpeg failed to read the image, longjump saved us");
283 jpeg_destroy_decompress(&i->cinfo);
286 jpeg_create_decompress(&i->cinfo);
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;
296 if (io->flags & IMAGE_IO_WANT_EXIF)
297 jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor);
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)
305 io->flags = COLOR_SPACE_GRAYSCALE;
308 io->flags = COLOR_SPACE_RGB;
311 io->flags = COLOR_SPACE_YCBCR;
314 io->flags = COLOR_SPACE_CMYK;
317 io->flags = COLOR_SPACE_YCCK;
320 if (unlikely(i->cinfo.num_components < 1 || i->cinfo.num_components > 4))
322 jpeg_destroy_decompress(&i->cinfo);
323 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Invalid color space.");
326 io->flags = COLOR_SPACE_UNKNOWN + i->cinfo.num_components;
329 if (unlikely(i->cinfo.num_components != (int)color_space_channels[io->flags]))
331 jpeg_destroy_decompress(&i->cinfo);
332 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Invalid number of color channels.");
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;
343 libjpeg_read_data(struct image_io *io)
345 DBG("libjpeg_read_data()");
347 struct libjpeg_read_internals *i = io->read_data;
348 uns read_flags = io->flags;
350 /* Select color space */
351 switch (read_flags & IMAGE_COLOR_SPACE)
353 case COLOR_SPACE_GRAYSCALE:
354 i->cinfo.out_color_space = JCS_GRAYSCALE;
356 case COLOR_SPACE_YCBCR:
357 i->cinfo.out_color_space = JCS_YCbCr;
359 case COLOR_SPACE_CMYK:
360 i->cinfo.out_color_space = JCS_CMYK;
362 case COLOR_SPACE_YCCK:
363 i->cinfo.out_color_space = JCS_YCCK;
366 switch (i->cinfo.jpeg_color_space)
369 read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_CMYK;
370 i->cinfo.out_color_space = JCS_YCbCr;
373 read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB;
374 i->cinfo.out_color_space = JCS_RGB;
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))
384 DBG("Scaling to 1/8");
385 i->cinfo.scale_num = 1;
386 i->cinfo.scale_denom = 8;
388 else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
390 DBG("Scaling to 1/4");
391 i->cinfo.scale_num = 1;
392 i->cinfo.scale_denom = 4;
394 else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
396 DBG("Scaling to 1/2");
397 i->cinfo.scale_num = 1;
398 i->cinfo.scale_denom = 2;
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)))
404 jpeg_destroy_decompress(&i->cinfo);
409 if (setjmp(i->err.setjmp_buf))
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);
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)
422 byte *pixels = img->pixels;
423 for (uns r = img->rows; r--; )
425 jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
426 pixels += img->row_size;
431 switch (img->pixel_size)
433 case 2: /* Grayscale -> Grayscale+Alpha */
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"
447 case 4: /* * -> *+Alpha or aligned * */
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"
467 ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
469 /* Destroy libjpeg object */
470 jpeg_finish_decompress(&i->cinfo);
471 jpeg_destroy_decompress(&i->cinfo);
473 /* Finish the image */
474 return image_io_read_data_finish(&rdi, io);
478 libjpeg_write(struct image_io *io)
480 DBG("libjpeg_write()");
481 struct libjpeg_write_internals i;
482 i.fastbuf = io->fastbuf;
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;
490 if (setjmp(i.err.setjmp_buf))
492 DBG("Libjpeg failed to write the image, longjump saved us");
493 jpeg_destroy_compress(&i.cinfo);
496 jpeg_create_compress(&i.cinfo);
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;
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)
510 case COLOR_SPACE_GRAYSCALE:
511 i.cinfo.in_color_space = JCS_GRAYSCALE;
513 case COLOR_SPACE_RGB:
514 i.cinfo.in_color_space = JCS_RGB;
516 case COLOR_SPACE_YCBCR:
517 i.cinfo.in_color_space = JCS_YCbCr;
519 case COLOR_SPACE_CMYK:
520 i.cinfo.in_color_space = JCS_CMYK;
522 case COLOR_SPACE_YCCK:
523 i.cinfo.in_color_space = JCS_YCCK;
526 jpeg_destroy_compress(&i.cinfo);
527 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
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);
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;
543 /* Compress the image */
544 jpeg_start_compress(&i.cinfo, TRUE);
548 jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
550 if ((int)img->pixel_size == i.cinfo.input_components)
552 byte *pixels = img->pixels;
553 for (uns r = img->rows; r--; )
555 jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
556 pixels += img->row_size;
561 switch (img->pixel_size)
563 case 2: /* Grayscale+Alpha -> Grayscale */
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"
577 case 4: /* *+Alpha or aligned * -> * */
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"
595 ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
596 jpeg_finish_compress(&i.cinfo);
597 jpeg_destroy_compress(&i.cinfo);