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, "%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, "%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);
78 if (unlikely(msg_level == -1))
79 longjmp(((struct libjpeg_err *)(cinfo)->err)->setjmp_buf, 1);
83 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
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;
94 libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i)
96 bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
100 libjpeg_init_source(j_decompress_ptr cinfo)
102 DBG("libjpeg_init_source()");
103 libjpeg_fastbuf_read_prepare((struct libjpeg_read_internals *)cinfo);
107 libjpeg_term_source(j_decompress_ptr cinfo UNUSED)
109 DBG("libjpeg_term_source()");
110 //libjpeg_fastbuf_read_commit((struct libjpeg_read_internals *)cinfo);
114 libjpeg_fill_input_buffer(j_decompress_ptr cinfo)
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);
123 libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
125 DBG("libjpeg_skip_input_data(num_bytes=%d)", (int)num_bytes);
128 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
129 if ((unsigned long)num_bytes <= i->src.bytes_in_buffer)
131 i->src.next_input_byte += num_bytes;
132 i->src.bytes_in_buffer -= num_bytes;
136 num_bytes -= i->src.bytes_in_buffer;
137 libjpeg_fastbuf_read_commit(i);
138 if (!bskip(i->fastbuf, num_bytes))
140 IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file");
141 longjmp(i->err.setjmp_buf, 1);
143 libjpeg_fastbuf_read_prepare(i);
149 libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i)
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;
158 IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_WRITE_FAILED, "Unexpected end of stream");
159 longjmp(i->err.setjmp_buf, 1);
164 libjpeg_init_destination(j_compress_ptr cinfo)
166 DBG("libjpeg_init_destination()");
167 libjpeg_fastbuf_write_prepare((struct libjpeg_write_internals *)cinfo);
171 libjpeg_term_destination(j_compress_ptr cinfo)
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);
179 libjpeg_empty_output_buffer(j_compress_ptr cinfo)
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);
188 #ifdef CONFIG_IMAGES_EXIF
191 libjpeg_read_byte(struct libjpeg_read_internals *i)
193 DBG("libjpeg_read_byte()");
194 if (!i->src.bytes_in_buffer)
195 if (!libjpeg_fill_input_buffer(&i->cinfo))
196 ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
197 i->src.bytes_in_buffer--;
198 return *i->src.next_input_byte++;
202 libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
204 DBG("libjpeg_read_buf(len=%u)", len);
207 if (!i->src.bytes_in_buffer)
208 if (!libjpeg_fill_input_buffer(&i->cinfo))
209 ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
210 uns buf_size = i->src.bytes_in_buffer;
211 uns read_size = MIN(buf_size, len);
212 memcpy(buf, i->src.next_input_byte, read_size);
213 i->src.bytes_in_buffer -= read_size;
214 i->src.next_input_byte += read_size;
219 static byte libjpeg_exif_header[6] = { 'E', 'x', 'i', 'f', 0, 0 };
222 libjpeg_app1_preprocessor(j_decompress_ptr cinfo)
224 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
225 struct image_io *io = i->err.io;
226 uns len = (libjpeg_read_byte(i) << 8) + libjpeg_read_byte(i);
227 DBG("Found APP1 marker, len=%u", len);
231 if (len < 7 /*|| io->exif_size*/)
233 libjpeg_skip_input_data(cinfo, len);
237 libjpeg_read_buf(i, header, 6);
238 if (memcmp(header, libjpeg_exif_header, 6))
240 libjpeg_skip_input_data(cinfo, len - 6);
244 io->exif_data = mp_alloc(io->internal_pool, len);
245 memcpy(io->exif_data, header, 6);
246 libjpeg_read_buf(i, io->exif_data + 6, len - 6);
247 DBG("Parsed EXIF of length %u", len);
254 libjpeg_read_cancel(struct image_io *io)
256 DBG("libjpeg_read_cancel()");
257 struct libjpeg_read_internals *i = io->read_data;
258 jpeg_destroy_decompress(&i->cinfo);
262 libjpeg_read_header(struct image_io *io)
264 DBG("libjpeg_read_header()");
265 struct libjpeg_read_internals *i = io->read_data = mp_alloc(io->internal_pool, sizeof(*i));
266 i->fastbuf = io->fastbuf;
268 /* Create libjpeg read structure */
269 DBG("Creating libjpeg read structure");
270 i->cinfo.err = jpeg_std_error(&i->err.pub);
271 i->err.pub.error_exit = libjpeg_read_error_exit;
272 i->err.pub.emit_message = libjpeg_emit_message;
274 if (setjmp(i->err.setjmp_buf))
276 DBG("Libjpeg failed to read the image, longjump saved us");
277 jpeg_destroy_decompress(&i->cinfo);
280 jpeg_create_decompress(&i->cinfo);
282 /* Initialize source manager */
283 i->cinfo.src = &i->src;
284 i->src.init_source = libjpeg_init_source;
285 i->src.fill_input_buffer = libjpeg_fill_input_buffer;
286 i->src.skip_input_data = libjpeg_skip_input_data;
287 i->src.resync_to_restart = jpeg_resync_to_restart;
288 i->src.term_source = libjpeg_term_source;
290 #ifdef CONFIG_IMAGES_EXIF
291 if (io->flags & IMAGE_IO_WANT_EXIF)
292 jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor);
295 /* Read JPEG header and setup decompression options */
296 DBG("Reading image header");
297 jpeg_read_header(&i->cinfo, TRUE);
298 switch (i->cinfo.jpeg_color_space)
301 io->flags = COLOR_SPACE_GRAYSCALE;
302 io->number_of_colors = 1 << 8;
305 io->flags = COLOR_SPACE_RGB;
306 io->number_of_colors = 1 << 24;
309 io->cols = i->cinfo.image_width;
310 io->rows = i->cinfo.image_height;
312 io->read_cancel = libjpeg_read_cancel;
317 libjpeg_read_data(struct image_io *io)
319 DBG("libjpeg_read_data()");
321 struct libjpeg_read_internals *i = io->read_data;
323 /* Select color space */
324 switch (io->flags & IMAGE_COLOR_SPACE)
326 case COLOR_SPACE_GRAYSCALE:
327 i->cinfo.out_color_space = JCS_GRAYSCALE;
329 case COLOR_SPACE_RGB:
330 i->cinfo.out_color_space = JCS_RGB;
333 jpeg_destroy_decompress(&i->cinfo);
334 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
338 /* Prepare the image */
339 struct image_io_read_data_internals rdi;
340 if (io->cols <= (i->cinfo.image_width >> 3) && io->rows <= (i->cinfo.image_height >> 3))
342 DBG("Scaling to 1/8");
343 i->cinfo.scale_num = 1;
344 i->cinfo.scale_denom = 8;
346 else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
348 DBG("Scaling to 1/4");
349 i->cinfo.scale_num = 1;
350 i->cinfo.scale_denom = 4;
352 else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
354 DBG("Scaling to 1/2");
355 i->cinfo.scale_num = 1;
356 i->cinfo.scale_denom = 2;
358 jpeg_calc_output_dimensions(&i->cinfo);
359 DBG("Output dimensions %ux%u", (uns)i->cinfo.output_width, (uns)i->cinfo.output_height);
360 if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, io->flags)))
362 jpeg_destroy_decompress(&i->cinfo);
367 if (setjmp(i->err.setjmp_buf))
369 DBG("Libjpeg failed to read the image, longjump saved us");
370 jpeg_destroy_decompress(&i->cinfo);
371 image_io_read_data_break(&rdi, io);
375 /* Decompress the image */
376 struct image *img = rdi.image;
377 jpeg_start_decompress(&i->cinfo);
378 switch (img->pixel_size)
380 /* grayscale or RGB */
384 byte *pixels = img->pixels;
385 for (uns r = img->rows; r--; )
387 jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
388 pixels += img->row_size;
392 /* grayscale with alpha */
395 byte buf[img->cols], *src;
396 # define IMAGE_WALK_PREFIX(x) walk_##x
397 # define IMAGE_WALK_INLINE
398 # define IMAGE_WALK_IMAGE img
399 # define IMAGE_WALK_UNROLL 4
400 # define IMAGE_WALK_COL_STEP 2
401 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
402 # define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
403 # include "images/image-walk.h"
406 /* RGBA or aligned RGB */
409 byte buf[img->cols * 3], *src;
410 # define IMAGE_WALK_PREFIX(x) walk_##x
411 # define IMAGE_WALK_INLINE
412 # define IMAGE_WALK_IMAGE img
413 # define IMAGE_WALK_UNROLL 4
414 # define IMAGE_WALK_COL_STEP 4
415 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
416 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0)
417 # include "images/image-walk.h"
423 ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
425 /* Destroy libjpeg object */
426 jpeg_finish_decompress(&i->cinfo);
427 jpeg_destroy_decompress(&i->cinfo);
429 /* Finish the image */
430 return image_io_read_data_finish(&rdi, io);
434 libjpeg_write(struct image_io *io)
436 DBG("libjpeg_write()");
437 struct libjpeg_write_internals i;
438 i.fastbuf = io->fastbuf;
440 /* Create libjpeg write structure */
441 DBG("Creating libjpeg write structure");
442 i.cinfo.err = jpeg_std_error(&i.err.pub);
443 i.err.pub.error_exit = libjpeg_write_error_exit;
444 i.err.pub.emit_message = libjpeg_emit_message;
446 if (setjmp(i.err.setjmp_buf))
448 DBG("Libjpeg failed to write the image, longjump saved us");
449 jpeg_destroy_compress(&i.cinfo);
452 jpeg_create_compress(&i.cinfo);
454 /* Initialize destination manager */
455 i.cinfo.dest = &i.dest;
456 i.dest.init_destination = libjpeg_init_destination;
457 i.dest.term_destination = libjpeg_term_destination;
458 i.dest.empty_output_buffer = libjpeg_empty_output_buffer;
460 /* Set output parameters */
461 struct image *img = io->image;
462 i.cinfo.image_width = img->cols;
463 i.cinfo.image_height = img->rows;
464 switch (img->flags & IMAGE_COLOR_SPACE)
466 case COLOR_SPACE_GRAYSCALE:
467 i.cinfo.input_components = 1;
468 i.cinfo.in_color_space = JCS_GRAYSCALE;
470 case COLOR_SPACE_RGB:
471 i.cinfo.input_components = 3;
472 i.cinfo.in_color_space = JCS_RGB;
475 jpeg_destroy_compress(&i.cinfo);
476 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
479 jpeg_set_defaults(&i.cinfo);
480 if (io->jpeg_quality)
481 jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
482 #ifdef CONFIG_IMAGES_EXIF
485 /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI,
486 * just as the JFIF specification requires the same for the JFIF APP0 marker!
487 * Therefore a JPEG file cannot legally be both Exif and JFIF. */
488 i.cinfo.write_JFIF_header = FALSE;
489 i.cinfo.write_Adobe_marker = FALSE;
493 /* Compress the image */
494 jpeg_start_compress(&i.cinfo, TRUE);
495 #ifdef CONFIG_IMAGES_EXIF
499 jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
502 switch (img->pixel_size)
504 /* grayscale or RGB */
508 byte *pixels = img->pixels;
509 for (uns r = img->rows; r--; )
511 jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
512 pixels += img->row_size;
516 /* grayscale with alpha (ignore alpha) */
519 byte buf[img->cols], *dest = buf;
520 # define IMAGE_WALK_PREFIX(x) walk_##x
521 # define IMAGE_WALK_INLINE
522 # define IMAGE_WALK_IMAGE img
523 # define IMAGE_WALK_UNROLL 4
524 # define IMAGE_WALK_COL_STEP 2
525 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
526 # define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
527 # include "images/image-walk.h"
530 /* RGBA (ignore alpha) or aligned RGB */
533 byte buf[img->cols * 3], *dest = buf;
534 # define IMAGE_WALK_PREFIX(x) walk_##x
535 # define IMAGE_WALK_INLINE
536 # define IMAGE_WALK_IMAGE img
537 # define IMAGE_WALK_UNROLL 4
538 # define IMAGE_WALK_COL_STEP 4
539 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
540 # define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0)
541 # include "images/image-walk.h"
547 ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
548 jpeg_finish_compress(&i.cinfo);
549 jpeg_destroy_compress(&i.cinfo);