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/io-main.h"
18 #include <sys/types.h>
24 struct jpeg_error_mgr pub;
29 struct libjpeg_read_internals {
30 struct jpeg_decompress_struct cinfo;
31 struct jpeg_source_mgr src;
32 struct libjpeg_err err;
33 struct fastbuf *fastbuf;
37 struct libjpeg_write_internals {
38 struct jpeg_compress_struct cinfo;
39 struct jpeg_destination_mgr dest;
40 struct libjpeg_err err;
41 struct fastbuf *fastbuf;
46 libjpeg_read_error_exit(j_common_ptr cinfo)
48 DBG("libjpeg_error_exit()");
49 struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
50 byte buf[JMSG_LENGTH_MAX];
51 e->pub.format_message(cinfo, buf);
52 image_thread_err_dup(e->io->thread, IMAGE_ERR_READ_FAILED, buf);
53 longjmp(e->setjmp_buf, 1);
57 libjpeg_write_error_exit(j_common_ptr cinfo)
59 DBG("libjpeg_error_exit()");
60 struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
61 byte buf[JMSG_LENGTH_MAX];
62 e->pub.format_message(cinfo, buf);
63 image_thread_err_dup(e->io->thread, IMAGE_ERR_WRITE_FAILED, buf);
64 longjmp(e->setjmp_buf, 1);
68 libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
71 byte buf[JMSG_LENGTH_MAX];
72 cinfo->err->format_message(cinfo, buf);
73 DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf);
75 if (unlikely(msg_level == -1))
76 longjmp(((struct libjpeg_err *)(cinfo)->err)->setjmp_buf, 1);
80 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
83 uns len = bdirect_read_prepare(i->fastbuf, &start);
84 i->fastbuf_pos = start + len;
85 i->src.next_input_byte = start;
86 i->src.bytes_in_buffer = len;
91 libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i)
93 bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
97 libjpeg_init_source(j_decompress_ptr cinfo)
99 DBG("libjpeg_init_source()");
100 libjpeg_fastbuf_read_prepare((struct libjpeg_read_internals *)cinfo);
104 libjpeg_term_source(j_decompress_ptr cinfo UNUSED)
106 DBG("libjpeg_term_source()");
107 //libjpeg_fastbuf_read_commit((struct libjpeg_read_internals *)cinfo);
111 libjpeg_fill_input_buffer(j_decompress_ptr cinfo)
113 DBG("libjpeg_fill_input_buffer()");
114 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
115 libjpeg_fastbuf_read_commit(i);
116 return !!libjpeg_fastbuf_read_prepare(i);
120 libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
122 DBG("libjpeg_skip_input_data(num_bytes=%d)", (int)num_bytes);
125 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
126 if ((unsigned long)num_bytes <= i->src.bytes_in_buffer)
128 i->src.next_input_byte += num_bytes;
129 i->src.bytes_in_buffer -= num_bytes;
133 num_bytes -= i->src.bytes_in_buffer;
134 libjpeg_fastbuf_read_commit(i);
135 bskip(i->fastbuf, num_bytes);
136 libjpeg_fastbuf_read_prepare(i);
142 libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i)
145 uns len = bdirect_write_prepare(i->fastbuf, &start);
146 i->fastbuf_pos = start + len;
147 i->dest.next_output_byte = start;
148 i->dest.free_in_buffer = len;
151 image_thread_err(i->err.io->thread, IMAGE_ERR_WRITE_FAILED, "Unexpected end of stream");
152 longjmp(i->err.setjmp_buf, 1);
157 libjpeg_init_destination(j_compress_ptr cinfo)
159 DBG("libjpeg_init_destination()");
160 libjpeg_fastbuf_write_prepare((struct libjpeg_write_internals *)cinfo);
164 libjpeg_term_destination(j_compress_ptr cinfo)
166 DBG("libjpeg_term_destination()");
167 struct libjpeg_write_internals *i = (struct libjpeg_write_internals *)cinfo;
168 bdirect_write_commit(i->fastbuf, (byte *)i->dest.next_output_byte);
172 libjpeg_empty_output_buffer(j_compress_ptr cinfo)
174 DBG("libjpeg_empty_output_buffer()");
175 struct libjpeg_write_internals *i = (struct libjpeg_write_internals *)cinfo;
176 bdirect_write_commit(i->fastbuf, i->fastbuf_pos);
177 libjpeg_fastbuf_write_prepare(i);
181 #ifdef CONFIG_IMAGES_EXIF
184 libjpeg_read_byte(struct libjpeg_read_internals *i)
186 DBG("libjpeg_read_byte()");
187 if (!i->src.bytes_in_buffer)
188 if (!libjpeg_fill_input_buffer(&i->cinfo))
189 ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
190 i->src.bytes_in_buffer--;
191 return *i->src.next_input_byte++;
195 libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
197 DBG("libjpeg_read_buf(len=%u)", len);
200 if (!i->src.bytes_in_buffer)
201 if (!libjpeg_fill_input_buffer(&i->cinfo))
202 ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
203 uns buf_size = i->src.bytes_in_buffer;
204 uns read_size = MIN(buf_size, len);
205 memcpy(buf, i->src.next_input_byte, read_size);
206 i->src.bytes_in_buffer -= read_size;
207 i->src.next_input_byte += read_size;
212 static byte libjpeg_exif_header[6] = { 'E', 'x', 'i', 'f', 0, 0 };
215 libjpeg_app1_preprocessor(j_decompress_ptr cinfo)
217 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
218 struct image_io *io = i->err.io;
219 uns len = (libjpeg_read_byte(i) << 8) + libjpeg_read_byte(i);
220 DBG("Found APP1 marker, len=%u", len);
224 if (len < 7 /*|| io->exif_size*/)
226 libjpeg_skip_input_data(cinfo, len);
230 libjpeg_read_buf(i, header, 6);
231 if (memcmp(header, libjpeg_exif_header, 6))
233 libjpeg_skip_input_data(cinfo, len - 6);
237 io->exif_data = mp_alloc(io->internal_pool, len);
238 memcpy(io->exif_data, header, 6);
239 libjpeg_read_buf(i, io->exif_data + 6, len - 6);
240 DBG("Parsed EXIF of length %u", len);
247 libjpeg_read_cancel(struct image_io *io)
249 DBG("libjpeg_read_cancel()");
250 struct libjpeg_read_internals *i = io->read_data;
251 jpeg_destroy_decompress(&i->cinfo);
255 libjpeg_read_header(struct image_io *io)
257 DBG("libjpeg_read_header()");
258 struct libjpeg_read_internals *i = io->read_data = mp_alloc(io->internal_pool, sizeof(*i));
259 i->fastbuf = io->fastbuf;
261 /* Create libjpeg read structure */
262 DBG("Creating libjpeg read structure");
263 i->cinfo.err = jpeg_std_error(&i->err.pub);
264 i->err.pub.error_exit = libjpeg_read_error_exit;
265 i->err.pub.emit_message = libjpeg_emit_message;
267 if (setjmp(i->err.setjmp_buf))
269 DBG("Libjpeg failed to read the image, longjump saved us");
270 jpeg_destroy_decompress(&i->cinfo);
273 jpeg_create_decompress(&i->cinfo);
275 /* Initialize source manager */
276 i->cinfo.src = &i->src;
277 i->src.init_source = libjpeg_init_source;
278 i->src.fill_input_buffer = libjpeg_fill_input_buffer;
279 i->src.skip_input_data = libjpeg_skip_input_data;
280 i->src.resync_to_restart = jpeg_resync_to_restart;
281 i->src.term_source = libjpeg_term_source;
283 #ifdef CONFIG_IMAGES_EXIF
284 if (io->flags & IMAGE_IO_WANT_EXIF)
285 jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor);
288 /* Read JPEG header and setup decompression options */
289 DBG("Reading image header");
290 jpeg_read_header(&i->cinfo, TRUE);
291 switch (i->cinfo.jpeg_color_space)
294 io->flags = COLOR_SPACE_GRAYSCALE;
295 io->number_of_colors = 1 << 8;
298 io->flags = COLOR_SPACE_RGB;
299 io->number_of_colors = 1 << 24;
302 io->cols = i->cinfo.image_width;
303 io->rows = i->cinfo.image_height;
305 io->read_cancel = libjpeg_read_cancel;
310 libjpeg_read_data(struct image_io *io)
312 DBG("libjpeg_read_data()");
314 struct libjpeg_read_internals *i = io->read_data;
316 /* Select color space */
317 switch (io->flags & IMAGE_COLOR_SPACE)
319 case COLOR_SPACE_GRAYSCALE:
320 i->cinfo.out_color_space = JCS_GRAYSCALE;
322 case COLOR_SPACE_RGB:
323 i->cinfo.out_color_space = JCS_RGB;
326 jpeg_destroy_decompress(&i->cinfo);
327 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
331 /* Prepare the image */
332 struct image_io_read_data_internals rdi;
333 if (io->cols <= (i->cinfo.image_width >> 3) && io->rows <= (i->cinfo.image_height >> 3))
335 DBG("Scaling to 1/8");
336 i->cinfo.scale_num = 1;
337 i->cinfo.scale_denom = 8;
339 else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
341 DBG("Scaling to 1/4");
342 i->cinfo.scale_num = 1;
343 i->cinfo.scale_denom = 4;
345 else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
347 DBG("Scaling to 1/2");
348 i->cinfo.scale_num = 1;
349 i->cinfo.scale_denom = 2;
351 jpeg_calc_output_dimensions(&i->cinfo);
352 DBG("Output dimensions %ux%u", (uns)i->cinfo.output_width, (uns)i->cinfo.output_height);
353 if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, io->flags)))
355 jpeg_destroy_decompress(&i->cinfo);
360 if (setjmp(i->err.setjmp_buf))
362 DBG("Libjpeg failed to read the image, longjump saved us");
363 jpeg_destroy_decompress(&i->cinfo);
364 image_io_read_data_break(&rdi, io);
368 /* Decompress the image */
369 struct image *img = rdi.image;
370 jpeg_start_decompress(&i->cinfo);
371 switch (img->pixel_size)
373 /* grayscale or RGB */
377 byte *pixels = img->pixels;
378 for (uns r = img->rows; r--; )
380 jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
381 pixels += img->row_size;
385 /* grayscale with alpha */
388 byte buf[img->cols], *src;
389 # define IMAGE_WALK_PREFIX(x) walk_##x
390 # define IMAGE_WALK_INLINE
391 # define IMAGE_WALK_IMAGE img
392 # define IMAGE_WALK_UNROLL 4
393 # define IMAGE_WALK_COL_STEP 2
394 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
395 # define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
396 # include "images/image-walk.h"
399 /* RGBA or aligned RGB */
402 byte buf[img->cols * 3], *src;
403 # define IMAGE_WALK_PREFIX(x) walk_##x
404 # define IMAGE_WALK_INLINE
405 # define IMAGE_WALK_IMAGE img
406 # define IMAGE_WALK_UNROLL 4
407 # define IMAGE_WALK_COL_STEP 4
408 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
409 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0)
410 # include "images/image-walk.h"
416 ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
418 /* Destroy libjpeg object */
419 jpeg_finish_decompress(&i->cinfo);
420 jpeg_destroy_decompress(&i->cinfo);
422 /* Finish the image */
423 return image_io_read_data_finish(&rdi, io);
427 libjpeg_write(struct image_io *io)
429 DBG("libjpeg_write()");
430 struct libjpeg_write_internals i;
431 i.fastbuf = io->fastbuf;
433 /* Create libjpeg write structure */
434 DBG("Creating libjpeg write structure");
435 i.cinfo.err = jpeg_std_error(&i.err.pub);
436 i.err.pub.error_exit = libjpeg_write_error_exit;
437 i.err.pub.emit_message = libjpeg_emit_message;
439 if (setjmp(i.err.setjmp_buf))
441 DBG("Libjpeg failed to write the image, longjump saved us");
442 jpeg_destroy_compress(&i.cinfo);
445 jpeg_create_compress(&i.cinfo);
447 /* Initialize destination manager */
448 i.cinfo.dest = &i.dest;
449 i.dest.init_destination = libjpeg_init_destination;
450 i.dest.term_destination = libjpeg_term_destination;
451 i.dest.empty_output_buffer = libjpeg_empty_output_buffer;
453 /* Set output parameters */
454 struct image *img = io->image;
455 i.cinfo.image_width = img->cols;
456 i.cinfo.image_height = img->rows;
457 switch (img->flags & IMAGE_COLOR_SPACE)
459 case COLOR_SPACE_GRAYSCALE:
460 i.cinfo.input_components = 1;
461 i.cinfo.in_color_space = JCS_GRAYSCALE;
463 case COLOR_SPACE_RGB:
464 i.cinfo.input_components = 3;
465 i.cinfo.in_color_space = JCS_RGB;
468 jpeg_destroy_compress(&i.cinfo);
469 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
472 jpeg_set_defaults(&i.cinfo);
473 if (io->jpeg_quality)
474 jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
475 #ifdef CONFIG_IMAGES_EXIF
478 /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI,
479 * just as the JFIF specification requires the same for the JFIF APP0 marker!
480 * Therefore a JPEG file cannot legally be both Exif and JFIF. */
481 i.cinfo.write_JFIF_header = FALSE;
482 i.cinfo.write_Adobe_marker = FALSE;
486 /* Compress the image */
487 jpeg_start_compress(&i.cinfo, TRUE);
488 #ifdef CONFIG_IMAGES_EXIF
492 jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
495 switch (img->pixel_size)
497 /* grayscale or RGB */
501 byte *pixels = img->pixels;
502 for (uns r = img->rows; r--; )
504 jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
505 pixels += img->row_size;
509 /* grayscale with alpha (ignore alpha) */
512 byte buf[img->cols], *dest = buf;
513 # define IMAGE_WALK_PREFIX(x) walk_##x
514 # define IMAGE_WALK_INLINE
515 # define IMAGE_WALK_IMAGE img
516 # define IMAGE_WALK_UNROLL 4
517 # define IMAGE_WALK_COL_STEP 2
518 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
519 # define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
520 # include "images/image-walk.h"
523 /* RGBA (ignore alpha) or aligned RGB */
526 byte buf[img->cols * 3], *dest = buf;
527 # define IMAGE_WALK_PREFIX(x) walk_##x
528 # define IMAGE_WALK_INLINE
529 # define IMAGE_WALK_IMAGE img
530 # define IMAGE_WALK_UNROLL 4
531 # define IMAGE_WALK_COL_STEP 4
532 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
533 # define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0)
534 # include "images/image-walk.h"
540 ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
541 jpeg_finish_compress(&i.cinfo);
542 jpeg_destroy_compress(&i.cinfo);