X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=images%2Fio-libjpeg.c;h=74af24acccc1d95fceba24b91554ac35be29b1ec;hb=0ca8c151f5afe1680e98f3bfbe9d5c752d8a2924;hp=10c49d7d91d1b256eb7683de3cac9d5cb7f47600;hpb=ab279cc435833a28fa9b7e2bb025b9b580bfeeb3;p=libucw.git diff --git a/images/io-libjpeg.c b/images/io-libjpeg.c index 10c49d7d..74af24ac 100644 --- a/images/io-libjpeg.c +++ b/images/io-libjpeg.c @@ -9,11 +9,14 @@ #undef LOCAL_DEBUG -#include "lib/lib.h" -#include "lib/mempool.h" -#include "lib/fastbuf.h" -#include "images/images.h" -#include "images/io-main.h" +#include +#include +#include +#include +#include +#include +#include + #include #include #include @@ -49,7 +52,7 @@ libjpeg_read_error_exit(j_common_ptr cinfo) struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err; byte buf[JMSG_LENGTH_MAX]; e->pub.format_message(cinfo, buf); - image_thread_err_dup(e->io->thread, IMAGE_ERR_READ_FAILED, buf); + IMAGE_ERROR(e->io->context, IMAGE_ERROR_READ_FAILED, "libjpeg: %s", buf); longjmp(e->setjmp_buf, 1); } @@ -60,7 +63,7 @@ libjpeg_write_error_exit(j_common_ptr cinfo) struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err; byte buf[JMSG_LENGTH_MAX]; e->pub.format_message(cinfo, buf); - image_thread_err_dup(e->io->thread, IMAGE_ERR_WRITE_FAILED, buf); + IMAGE_ERROR(e->io->context, IMAGE_ERROR_WRITE_FAILED, "libjpeg: %s", buf); longjmp(e->setjmp_buf, 1); } @@ -72,15 +75,32 @@ libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED) cinfo->err->format_message(cinfo, buf); DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf); #endif +#if 0 + // Terminate on warning? if (unlikely(msg_level == -1)) - longjmp(((struct libjpeg_err *)(cinfo)->err)->setjmp_buf, 1); + { + struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err; + byte buf[JMSG_LENGTH_MAX]; + cinfo->err->format_message(cinfo, buf); + IMAGE_ERROR(e->io->context, 0, "libjpeg: %s", buf); + longjmp(e->setjmp_buf, 1); + } +#endif } static inline uns libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i) { + DBG("libjpeg_fb_read_prepare()"); byte *start; uns len = bdirect_read_prepare(i->fastbuf, &start); + DBG("readed %u bytes at %p", len, start); + if (!len) + { + // XXX: maybe only generate a warning and return EOI markers to recover from such errors (also in skip_input_data) + IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file"); + longjmp(i->err.setjmp_buf, 1); + } i->fastbuf_pos = start + len; i->src.next_input_byte = start; i->src.bytes_in_buffer = len; @@ -90,6 +110,7 @@ libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i) static inline void libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i) { + DBG("libjpeg_fb_read_commit()"); bdirect_read_commit(i->fastbuf, i->fastbuf_pos); } @@ -113,7 +134,8 @@ libjpeg_fill_input_buffer(j_decompress_ptr cinfo) DBG("libjpeg_fill_input_buffer()"); struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo; libjpeg_fastbuf_read_commit(i); - return !!libjpeg_fastbuf_read_prepare(i); + libjpeg_fastbuf_read_prepare(i); + return 1; } static void @@ -132,7 +154,11 @@ libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { num_bytes -= i->src.bytes_in_buffer; libjpeg_fastbuf_read_commit(i); - bskip(i->fastbuf, num_bytes); + if (!bskip(i->fastbuf, num_bytes)) + { + IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file"); + longjmp(i->err.setjmp_buf, 1); + } libjpeg_fastbuf_read_prepare(i); } } @@ -148,7 +174,7 @@ libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i) i->dest.free_in_buffer = len; if (!len) { - image_thread_err(i->err.io->thread, IMAGE_ERR_WRITE_FAILED, "Unexpected end of stream"); + IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_WRITE_FAILED, "Unexpected end of stream"); longjmp(i->err.setjmp_buf, 1); } } @@ -178,8 +204,6 @@ libjpeg_empty_output_buffer(j_compress_ptr cinfo) return TRUE; } -#ifdef CONFIG_IMAGES_EXIF - static inline uns libjpeg_read_byte(struct libjpeg_read_internals *i) { @@ -216,7 +240,8 @@ libjpeg_app1_preprocessor(j_decompress_ptr cinfo) { struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo; struct image_io *io = i->err.io; - uns len = (libjpeg_read_byte(i) << 8) + libjpeg_read_byte(i); + uns len = libjpeg_read_byte(i) << 8; + len += libjpeg_read_byte(i); DBG("Found APP1 marker, len=%u", len); if (len < 2) return TRUE; @@ -241,8 +266,6 @@ libjpeg_app1_preprocessor(j_decompress_ptr cinfo) return TRUE; } -#endif - static void libjpeg_read_cancel(struct image_io *io) { @@ -280,10 +303,8 @@ libjpeg_read_header(struct image_io *io) i->src.resync_to_restart = jpeg_resync_to_restart; i->src.term_source = libjpeg_term_source; -#ifdef CONFIG_IMAGES_EXIF if (io->flags & IMAGE_IO_WANT_EXIF) jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor); -#endif /* Read JPEG header and setup decompression options */ DBG("Reading image header"); @@ -292,16 +313,38 @@ libjpeg_read_header(struct image_io *io) { case JCS_GRAYSCALE: io->flags = COLOR_SPACE_GRAYSCALE; - io->number_of_colors = 1 << 8; break; + case JCS_RGB: + io->flags = COLOR_SPACE_RGB; + break; + case JCS_YCbCr: + io->flags = COLOR_SPACE_YCBCR; + break; + case JCS_CMYK: + io->flags = COLOR_SPACE_CMYK; + break; + case JCS_YCCK: + io->flags = COLOR_SPACE_YCCK; + break; default: - io->flags = COLOR_SPACE_RGB; - io->number_of_colors = 1 << 24; - break; + if (unlikely(i->cinfo.num_components < 1 || i->cinfo.num_components > 4)) + { + jpeg_destroy_decompress(&i->cinfo); + IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Invalid color space."); + return 0; + } + io->flags = COLOR_SPACE_UNKNOWN + i->cinfo.num_components; + break; + } + if (unlikely(i->cinfo.num_components != (int)color_space_channels[io->flags])) + { + jpeg_destroy_decompress(&i->cinfo); + IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Invalid number of color channels."); + return 0; } io->cols = i->cinfo.image_width; io->rows = i->cinfo.image_height; - + io->number_of_colors = (i->cinfo.num_components < 4) ? (1U << (i->cinfo.num_components * 8)) : 0xffffffff; io->read_cancel = libjpeg_read_cancel; return 1; } @@ -312,21 +355,32 @@ libjpeg_read_data(struct image_io *io) DBG("libjpeg_read_data()"); struct libjpeg_read_internals *i = io->read_data; + uns read_flags = io->flags; /* Select color space */ - switch (io->flags & IMAGE_COLOR_SPACE) + switch (i->cinfo.jpeg_color_space) { - case COLOR_SPACE_GRAYSCALE: - i->cinfo.out_color_space = JCS_GRAYSCALE; + case JCS_GRAYSCALE: + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCBCR; + i->cinfo.out_color_space = JCS_YCbCr; + break; + case JCS_YCbCr: + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCBCR; + i->cinfo.out_color_space = JCS_YCbCr; break; - case COLOR_SPACE_RGB: - i->cinfo.out_color_space = JCS_RGB; + case JCS_CMYK: + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_CMYK; + i->cinfo.out_color_space = JCS_CMYK; + break; + case JCS_YCCK: + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCCK; + i->cinfo.out_color_space = JCS_YCCK; break; default: - jpeg_destroy_decompress(&i->cinfo); - image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space."); - return 0; - } + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; + i->cinfo.out_color_space = JCS_RGB; + break; + } /* Prepare the image */ struct image_io_read_data_internals rdi; @@ -350,7 +404,7 @@ libjpeg_read_data(struct image_io *io) } jpeg_calc_output_dimensions(&i->cinfo); DBG("Output dimensions %ux%u", (uns)i->cinfo.output_width, (uns)i->cinfo.output_height); - if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, io->flags))) + if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, read_flags))) { jpeg_destroy_decompress(&i->cinfo); return 0; @@ -368,51 +422,53 @@ libjpeg_read_data(struct image_io *io) /* Decompress the image */ struct image *img = rdi.image; jpeg_start_decompress(&i->cinfo); - switch (img->pixel_size) + if ((int)img->pixel_size == i->cinfo.output_components) { - /* grayscale or RGB */ - case 1: - case 3: - { - byte *pixels = img->pixels; - for (uns r = img->rows; r--; ) - { - jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1); - pixels += img->row_size; - } - } - break; - /* grayscale with alpha */ - case 2: - { - byte buf[img->cols], *src; -# define IMAGE_WALK_PREFIX(x) walk_##x -# define IMAGE_WALK_INLINE -# define IMAGE_WALK_IMAGE img -# define IMAGE_WALK_UNROLL 4 -# define IMAGE_WALK_COL_STEP 2 -# define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0) -# define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0) -# include "images/image-walk.h" - } - break; - /* RGBA or aligned RGB */ - case 4: - { - byte buf[img->cols * 3], *src; -# define IMAGE_WALK_PREFIX(x) walk_##x -# define IMAGE_WALK_INLINE -# define IMAGE_WALK_IMAGE img -# define IMAGE_WALK_UNROLL 4 -# define IMAGE_WALK_COL_STEP 4 -# define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0) -# define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0) -# include "images/image-walk.h" + byte *pixels = img->pixels; + for (uns r = img->rows; r--; ) + { + jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1); + pixels += img->row_size; + } + } + else + { + switch (img->pixel_size) + { + case 2: /* Grayscale -> Grayscale+Alpha */ + { + ASSERT(i->cinfo.output_components == 1); + byte buf[img->cols], *src; +# define IMAGE_WALK_PREFIX(x) walk_##x +# define IMAGE_WALK_INLINE +# define IMAGE_WALK_IMAGE img +# define IMAGE_WALK_UNROLL 4 +# define IMAGE_WALK_COL_STEP 2 +# define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0) +# define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0) +# include + } + break; + case 4: /* * -> *+Alpha or aligned * */ + { + ASSERT(i->cinfo.output_components == 3); + byte buf[img->cols * 3], *src; +# define IMAGE_WALK_PREFIX(x) walk_##x +# define IMAGE_WALK_INLINE +# define IMAGE_WALK_IMAGE img +# define IMAGE_WALK_UNROLL 4 +# define IMAGE_WALK_COL_STEP 4 +# define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0) +# define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0) +# include + } + break; + default: + ASSERT(0); } - break; - default: - ASSERT(0); + } + ASSERT(i->cinfo.output_scanline == i->cinfo.output_height); /* Destroy libjpeg object */ @@ -457,22 +513,30 @@ libjpeg_write(struct image_io *io) switch (img->flags & IMAGE_COLOR_SPACE) { case COLOR_SPACE_GRAYSCALE: - i.cinfo.input_components = 1; i.cinfo.in_color_space = JCS_GRAYSCALE; break; case COLOR_SPACE_RGB: - i.cinfo.input_components = 3; i.cinfo.in_color_space = JCS_RGB; break; + case COLOR_SPACE_YCBCR: + i.cinfo.in_color_space = JCS_YCbCr; + break; + case COLOR_SPACE_CMYK: + i.cinfo.in_color_space = JCS_CMYK; + break; + case COLOR_SPACE_YCCK: + i.cinfo.in_color_space = JCS_YCCK; + break; default: jpeg_destroy_compress(&i.cinfo); - image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported pixel format."); + IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported pixel format."); return 0; } + i.cinfo.input_components = color_space_channels[img->flags & IMAGE_COLOR_SPACE]; jpeg_set_defaults(&i.cinfo); + jpeg_set_colorspace(&i.cinfo, i.cinfo.in_color_space); if (io->jpeg_quality) jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1); -#ifdef CONFIG_IMAGES_EXIF if (io->exif_size) { /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI, @@ -481,61 +545,58 @@ libjpeg_write(struct image_io *io) i.cinfo.write_JFIF_header = FALSE; i.cinfo.write_Adobe_marker = FALSE; } -#endif /* Compress the image */ jpeg_start_compress(&i.cinfo, TRUE); -#ifdef CONFIG_IMAGES_EXIF if (io->exif_size) { DBG("Writing EXIF"); jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size); } -#endif - switch (img->pixel_size) + if ((int)img->pixel_size == i.cinfo.input_components) + { + byte *pixels = img->pixels; + for (uns r = img->rows; r--; ) + { + jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1); + pixels += img->row_size; + } + } + else { - /* grayscale or RGB */ - case 1: - case 3: - { - byte *pixels = img->pixels; - for (uns r = img->rows; r--; ) + switch (img->pixel_size) + { + case 2: /* Grayscale+Alpha -> Grayscale */ { - jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1); - pixels += img->row_size; - } - } - break; - /* grayscale with alpha (ignore alpha) */ - case 2: - { - byte buf[img->cols], *dest = buf; -# define IMAGE_WALK_PREFIX(x) walk_##x -# define IMAGE_WALK_INLINE -# define IMAGE_WALK_IMAGE img -# define IMAGE_WALK_UNROLL 4 -# define IMAGE_WALK_COL_STEP 2 -# define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0) -# define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0) -# include "images/image-walk.h" - } - break; - /* RGBA (ignore alpha) or aligned RGB */ - case 4: - { - byte buf[img->cols * 3], *dest = buf; -# define IMAGE_WALK_PREFIX(x) walk_##x -# define IMAGE_WALK_INLINE -# define IMAGE_WALK_IMAGE img -# define IMAGE_WALK_UNROLL 4 -# define IMAGE_WALK_COL_STEP 4 -# define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0) -# define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0) -# include "images/image-walk.h" + ASSERT(i.cinfo.input_components == 1); + byte buf[img->cols], *dest = buf; +# define IMAGE_WALK_PREFIX(x) walk_##x +# define IMAGE_WALK_INLINE +# define IMAGE_WALK_IMAGE img +# define IMAGE_WALK_UNROLL 4 +# define IMAGE_WALK_COL_STEP 2 +# define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0) +# define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0) +# include + break; + } + case 4: /* *+Alpha or aligned * -> * */ + { + ASSERT(i.cinfo.input_components == 3); + byte buf[img->cols * 3], *dest = buf; +# define IMAGE_WALK_PREFIX(x) walk_##x +# define IMAGE_WALK_INLINE +# define IMAGE_WALK_IMAGE img +# define IMAGE_WALK_UNROLL 4 +# define IMAGE_WALK_COL_STEP 4 +# define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0) +# define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0) +# include + break; + } + default: + ASSERT(0); } - break; - default: - ASSERT(0); } ASSERT(i.cinfo.next_scanline == i.cinfo.image_height); jpeg_finish_compress(&i.cinfo);