]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libjpeg.c
ucw docs: Array sorter
[libucw.git] / images / io-libjpeg.c
index f4a72e29a70228afb7d98681e246347d531605b2..16f10b7a46ef3f39853089d948e0b9646bbb9bd9 100644 (file)
@@ -7,11 +7,11 @@
  *     of the GNU Lesser General Public License.
  */
 
  *     of the GNU Lesser General Public License.
  */
 
-#define LOCAL_DEBUG
+#undef LOCAL_DEBUG
 
 
-#include "lib/lib.h"
-#include "lib/mempool.h"
-#include "lib/fastbuf.h"
+#include "ucw/lib.h"
+#include "ucw/mempool.h"
+#include "ucw/fastbuf.h"
 #include "images/images.h"
 #include "images/error.h"
 #include "images/color.h"
 #include "images/images.h"
 #include "images/error.h"
 #include "images/color.h"
@@ -52,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);
   struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
   byte buf[JMSG_LENGTH_MAX];
   e->pub.format_message(cinfo, buf);
-  IMAGE_ERROR(e->io->context, IMAGE_ERROR_READ_FAILED, "%s", buf);
+  IMAGE_ERROR(e->io->context, IMAGE_ERROR_READ_FAILED, "libjpeg: %s", buf);
   longjmp(e->setjmp_buf, 1);
 }
 
   longjmp(e->setjmp_buf, 1);
 }
 
@@ -63,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);
   struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
   byte buf[JMSG_LENGTH_MAX];
   e->pub.format_message(cinfo, buf);
-  IMAGE_ERROR(e->io->context, IMAGE_ERROR_WRITE_FAILED, "%s", buf);
+  IMAGE_ERROR(e->io->context, IMAGE_ERROR_WRITE_FAILED, "libjpeg: %s", buf);
   longjmp(e->setjmp_buf, 1);
 }
 
   longjmp(e->setjmp_buf, 1);
 }
 
@@ -75,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
   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))
   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)
 {
 }
 
 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);
   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;
   i->fastbuf_pos = start + len;
   i->src.next_input_byte = start;
   i->src.bytes_in_buffer = len;
@@ -93,6 +110,7 @@ libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
 static inline void
 libjpeg_fastbuf_read_commit(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);
 }
 
   bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
 }
 
@@ -116,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);
   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
 }
 
 static void
@@ -336,16 +355,14 @@ libjpeg_read_data(struct image_io *io)
   DBG("libjpeg_read_data()");
 
   struct libjpeg_read_internals *i = io->read_data;
   DBG("libjpeg_read_data()");
 
   struct libjpeg_read_internals *i = io->read_data;
+  uns read_flags = io->flags;
 
   /* Select color space */
 
   /* Select color space */
-  switch (io->flags & IMAGE_COLOR_SPACE)
+  switch (read_flags & IMAGE_COLOR_SPACE)
     {
       case COLOR_SPACE_GRAYSCALE:
        i->cinfo.out_color_space = JCS_GRAYSCALE;
        break;
     {
       case COLOR_SPACE_GRAYSCALE:
        i->cinfo.out_color_space = JCS_GRAYSCALE;
        break;
-      case COLOR_SPACE_RGB:
-       i->cinfo.out_color_space = JCS_RGB;
-       break;
       case COLOR_SPACE_YCBCR:
        i->cinfo.out_color_space = JCS_YCbCr;
        break;
       case COLOR_SPACE_YCBCR:
        i->cinfo.out_color_space = JCS_YCbCr;
        break;
@@ -356,9 +373,22 @@ libjpeg_read_data(struct image_io *io)
        i->cinfo.out_color_space = JCS_YCCK;
        break;
       default:
        i->cinfo.out_color_space = JCS_YCCK;
        break;
       default:
-       jpeg_destroy_decompress(&i->cinfo);
-       IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
-       return 0;
+       switch (i->cinfo.jpeg_color_space)
+         {
+           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:
+             read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; 
+             i->cinfo.out_color_space = JCS_RGB;
+             break;
+         }
+       break;
     }
 
   /* Prepare the image  */
     }
 
   /* Prepare the image  */
@@ -383,7 +413,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);
     }
   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;
     {
       jpeg_destroy_decompress(&i->cinfo);
       return 0;
@@ -401,7 +431,7 @@ libjpeg_read_data(struct image_io *io)
   /* Decompress the image */
   struct image *img = rdi.image;
   jpeg_start_decompress(&i->cinfo);
   /* Decompress the image */
   struct image *img = rdi.image;
   jpeg_start_decompress(&i->cinfo);
-  if ((int)img->pixel_size == i->cinfo.num_components)
+  if ((int)img->pixel_size == i->cinfo.output_components)
     {
       byte *pixels = img->pixels;
       for (uns r = img->rows; r--; )
     {
       byte *pixels = img->pixels;
       for (uns r = img->rows; r--; )
@@ -416,7 +446,7 @@ libjpeg_read_data(struct image_io *io)
         {
          case 2: /* Grayscale -> Grayscale+Alpha */
            {
         {
          case 2: /* Grayscale -> Grayscale+Alpha */
            {
-             ASSERT(i->cinfo.num_components == 1);
+             ASSERT(i->cinfo.output_components == 1);
              byte buf[img->cols], *src;
 #            define IMAGE_WALK_PREFIX(x) walk_##x
 #             define IMAGE_WALK_INLINE
              byte buf[img->cols], *src;
 #            define IMAGE_WALK_PREFIX(x) walk_##x
 #             define IMAGE_WALK_INLINE
@@ -430,7 +460,7 @@ libjpeg_read_data(struct image_io *io)
            break;
          case 4: /* * -> *+Alpha or aligned * */
            {
            break;
          case 4: /* * -> *+Alpha or aligned * */
            {
-             ASSERT(i->cinfo.num_components == 3);
+             ASSERT(i->cinfo.output_components == 3);
              byte buf[img->cols * 3], *src;
 #            define IMAGE_WALK_PREFIX(x) walk_##x
 #             define IMAGE_WALK_INLINE
              byte buf[img->cols * 3], *src;
 #            define IMAGE_WALK_PREFIX(x) walk_##x
 #             define IMAGE_WALK_INLINE
@@ -513,6 +543,7 @@ libjpeg_write(struct image_io *io)
     }
   i.cinfo.input_components = color_space_channels[img->flags & IMAGE_COLOR_SPACE];
   jpeg_set_defaults(&i.cinfo);
     }
   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);
   if (io->exif_size)
   if (io->jpeg_quality)
     jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
   if (io->exif_size)