]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libjpeg.c
* removed CONFIG_IMAGES_EXIF switch (always enabled)
[libucw.git] / images / io-libjpeg.c
index 1b4679709e17815d6cdcace73e8ff980c9a97407..52edd03bf79603155fbd824a8d4d85e8bed6b8c4 100644 (file)
 #include "lib/mempool.h"
 #include "lib/fastbuf.h"
 #include "images/images.h"
+#include "images/error.h"
+#include "images/color.h"
+#include "images/io-main.h"
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <jpeglib.h>
+#include <jerror.h>
 #include <setjmp.h>
 
 struct libjpeg_err {
@@ -48,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(e->io->thread, IMAGE_ERR_READ_FAILED, buf);
+  IMAGE_ERROR(e->io->context, IMAGE_ERROR_READ_FAILED, "%s", buf);
   longjmp(e->setjmp_buf, 1);
 }
 
@@ -59,18 +63,18 @@ 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(e->io->thread, IMAGE_ERR_WRITE_FAILED,  buf);
+  IMAGE_ERROR(e->io->context, IMAGE_ERROR_WRITE_FAILED, "%s", buf);
   longjmp(e->setjmp_buf, 1);
 }
 
 static void
 libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
 {
-#ifdef LOCAL_DEBUG  
+#ifdef LOCAL_DEBUG
   byte buf[JMSG_LENGTH_MAX];
   cinfo->err->format_message(cinfo, buf);
   DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf);
-#endif  
+#endif
   if (unlikely(msg_level == -1))
     longjmp(((struct libjpeg_err *)(cinfo)->err)->setjmp_buf, 1);
 }
@@ -131,7 +135,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);
        }
     }
@@ -147,7 +155,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);
     }
 }
@@ -177,6 +185,67 @@ libjpeg_empty_output_buffer(j_compress_ptr cinfo)
   return TRUE;
 }
 
+static inline uns
+libjpeg_read_byte(struct libjpeg_read_internals *i)
+{
+  DBG("libjpeg_read_byte()");
+  if (!i->src.bytes_in_buffer)
+    if (!libjpeg_fill_input_buffer(&i->cinfo))
+      ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
+  i->src.bytes_in_buffer--;
+  return *i->src.next_input_byte++;
+}
+
+static inline void
+libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
+{
+  DBG("libjpeg_read_buf(len=%u)", len);
+  while (len)
+    {
+      if (!i->src.bytes_in_buffer)
+       if (!libjpeg_fill_input_buffer(&i->cinfo))
+         ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
+      uns buf_size = i->src.bytes_in_buffer;
+      uns read_size = MIN(buf_size, len);
+      memcpy(buf, i->src.next_input_byte, read_size);
+      i->src.bytes_in_buffer -= read_size;
+      i->src.next_input_byte += read_size;
+      len -= read_size;
+    }
+}
+
+static byte libjpeg_exif_header[6] = { 'E', 'x', 'i', 'f', 0, 0 };
+
+static boolean
+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);
+  DBG("Found APP1 marker, len=%u", len);
+  if (len < 2)
+    return TRUE;
+  len -= 2;
+  if (len < 7 /*|| io->exif_size*/)
+    {
+      libjpeg_skip_input_data(cinfo, len);
+      return TRUE;
+    }
+  byte header[6];
+  libjpeg_read_buf(i, header, 6);
+  if (memcmp(header, libjpeg_exif_header, 6))
+    {
+      libjpeg_skip_input_data(cinfo, len - 6);
+      return TRUE;
+    }
+  io->exif_size = len;
+  io->exif_data = mp_alloc(io->internal_pool, len);
+  memcpy(io->exif_data, header, 6);
+  libjpeg_read_buf(i, io->exif_data + 6, len - 6);
+  DBG("Parsed EXIF of length %u", len);
+  return TRUE;
+}
+
 static void
 libjpeg_read_cancel(struct image_io *io)
 {
@@ -214,23 +283,25 @@ libjpeg_read_header(struct image_io *io)
   i->src.resync_to_restart = jpeg_resync_to_restart;
   i->src.term_source = libjpeg_term_source;
 
+  if (io->flags & IMAGE_IO_WANT_EXIF)
+    jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor);
+
   /* Read JPEG header and setup decompression options */
   DBG("Reading image header");
   jpeg_read_header(&i->cinfo, TRUE);
-  if (!(io->flags & IMAGE_COLOR_SPACE))
-    switch (i->cinfo.jpeg_color_space)
-      {
-        case JCS_GRAYSCALE:
-         io->flags |= COLOR_SPACE_GRAYSCALE;
-         break;
-        default:
-         io->flags |= COLOR_SPACE_RGB;
-         break;
-      }
-  if (!io->cols)
-    io->cols = i->cinfo.image_width;
-  if (!io->rows)
-    io->rows = i->cinfo.image_height;
+  switch (i->cinfo.jpeg_color_space)
+    {
+      case JCS_GRAYSCALE:
+        io->flags = COLOR_SPACE_GRAYSCALE;
+       io->number_of_colors = 1 << 8;
+        break;
+      default:
+        io->flags = COLOR_SPACE_RGB;
+       io->number_of_colors = 1 << 24;
+        break;
+    }
+  io->cols = i->cinfo.image_width;
+  io->rows = i->cinfo.image_height;
 
   io->read_cancel = libjpeg_read_cancel;
   return 1;
@@ -242,8 +313,8 @@ libjpeg_read_data(struct image_io *io)
   DBG("libjpeg_read_data()");
 
   struct libjpeg_read_internals *i = io->read_data;
-  /* Select color space */ 
+
+  /* Select color space */
   switch (io->flags & IMAGE_COLOR_SPACE)
     {
       case COLOR_SPACE_GRAYSCALE:
@@ -254,32 +325,49 @@ libjpeg_read_data(struct image_io *io)
        break;
       default:
        jpeg_destroy_decompress(&i->cinfo);
-       image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
+       IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
        return 0;
     }
 
-  /* Allocate the image... FIXME: use libjpeg feature to speed up downscale */
-  volatile int need_scale = io->cols != i->cinfo.image_width || io->rows != i->cinfo.image_height;
-  struct image * volatile img = need_scale ?
-    image_new(io->thread, i->cinfo.image_width, i->cinfo.image_height, io->flags & IMAGE_PIXEL_FORMAT, NULL) :
-    image_new(io->thread, i->cinfo.image_width, i->cinfo.image_height, io->flags, io->pool);
-  if (!img)
+  /* Prepare the image  */
+  struct image_io_read_data_internals rdi;
+  if (io->cols <= (i->cinfo.image_width >> 3) && io->rows <= (i->cinfo.image_height >> 3))
+    {
+      DBG("Scaling to 1/8");
+      i->cinfo.scale_num = 1;
+      i->cinfo.scale_denom = 8;
+    }
+  else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
+    {
+      DBG("Scaling to 1/4");
+      i->cinfo.scale_num = 1;
+      i->cinfo.scale_denom = 4;
+    }
+  else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
     {
-      image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
+      DBG("Scaling to 1/2");
+      i->cinfo.scale_num = 1;
+      i->cinfo.scale_denom = 2;
+    }
+  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)))
+    {
+      jpeg_destroy_decompress(&i->cinfo);
       return 0;
     }
-  
+
   /* Setup fallback */
   if (setjmp(i->err.setjmp_buf))
     {
       DBG("Libjpeg failed to read the image, longjump saved us");
       jpeg_destroy_decompress(&i->cinfo);
-      if (need_scale || !io->pool)
-       image_destroy(io->thread, img);
+      image_io_read_data_break(&rdi, io);
       return 0;
     }
 
   /* Decompress the image */
+  struct image *img = rdi.image;
   jpeg_start_decompress(&i->cinfo);
   switch (img->pixel_size)
     {
@@ -295,15 +383,17 @@ libjpeg_read_data(struct image_io *io)
             }
        }
        break;
-      /* garscale with alpha */
+      /* 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{ pos[0] = *src++; pos[1] = 255; }while(0)
+#         define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
 #         include "images/image-walk.h"
        }
        break;
@@ -311,11 +401,13 @@ libjpeg_read_data(struct image_io *io)
       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 *)pos = *(u32 *)src; pos[3] = 255; src += 3; }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"
        }
        break;
@@ -323,37 +415,13 @@ libjpeg_read_data(struct image_io *io)
        ASSERT(0);
     }
   ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
-  
+
   /* Destroy libjpeg object */
   jpeg_finish_decompress(&i->cinfo);
   jpeg_destroy_decompress(&i->cinfo);
 
-  /* Scale result if necessary */
-  if (need_scale)
-    {
-      DBG("Scaling image");
-      struct image *dest = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
-      if (!dest)
-        {
-         image_destroy(io->thread, img);
-         return 0;
-       }
-      if (!(image_scale(io->thread, dest, img)))
-        {
-         image_destroy(io->thread, img);
-         if (!io->pool)
-           image_destroy(io->thread, dest);
-         return 0;
-       }
-      image_destroy(io->thread, img);
-      io->image = dest;
-    }
-  else
-    io->image = img;
-  io->image_destroy = !io->pool;
-
-  DBG("Image readed");
-  return 1;
+  /* Finish the image */
+  return image_io_read_data_finish(&rdi, io);
 }
 
 int
@@ -376,7 +444,7 @@ libjpeg_write(struct image_io *io)
       return 0;
     }
   jpeg_create_compress(&i.cinfo);
-  
+
   /* Initialize destination manager */
   i.cinfo.dest = &i.dest;
   i.dest.init_destination = libjpeg_init_destination;
@@ -399,13 +467,28 @@ libjpeg_write(struct image_io *io)
        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;
     }
   jpeg_set_defaults(&i.cinfo);
+  if (io->jpeg_quality)
+    jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
+  if (io->exif_size)
+    {
+      /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI,
+       * just as the JFIF specification requires the same for the JFIF APP0 marker!
+       * Therefore a JPEG file cannot legally be both Exif and JFIF.  */
+      i.cinfo.write_JFIF_header = FALSE;
+      i.cinfo.write_Adobe_marker = FALSE;
+    }
 
   /* Compress the image */
   jpeg_start_compress(&i.cinfo, TRUE);
+  if (io->exif_size)
+    {
+      DBG("Writing EXIF");
+      jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
+    }
   switch (img->pixel_size)
     {
       /* grayscale or RGB */
@@ -424,11 +507,13 @@ libjpeg_write(struct image_io *io)
       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++ = pos[0]; }while(0)
+#         define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
 #         include "images/image-walk.h"
        }
        break;
@@ -436,11 +521,13 @@ libjpeg_write(struct image_io *io)
       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++ = pos[0]; *dest++ = pos[1]; *dest++ = pos[2]; }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"
        }
        break;