]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libjpeg.c
Configure: libgif seems to be available on all recent system, libungif doesn't
[libucw.git] / images / io-libjpeg.c
index 70efc91410ff5ea639732fb45f3d94d47c54c8a1..cc2f2060fd6cc774a4dbae049b4c640f6f102c56 100644 (file)
@@ -9,13 +9,13 @@
 
 #undef LOCAL_DEBUG
 
-#include "lib/lib.h"
-#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 <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/io-main.h>
 
 #include <stdio.h>
 #include <sys/types.h>
@@ -88,12 +88,12 @@ libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
 #endif
 }
 
-static inline uns
+static inline uint
 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
 {
   DBG("libjpeg_fb_read_prepare()");
   byte *start;
-  uns len = bdirect_read_prepare(i->fastbuf, &start);
+  uint len = bdirect_read_prepare(i->fastbuf, &start);
   DBG("readed %u bytes at %p", len, start);
   if (!len)
     {
@@ -168,7 +168,7 @@ static inline void
 libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i)
 {
   byte *start;
-  uns len = bdirect_write_prepare(i->fastbuf, &start);
+  uint len = bdirect_write_prepare(i->fastbuf, &start);
   i->fastbuf_pos = start + len;
   i->dest.next_output_byte = start;
   i->dest.free_in_buffer = len;
@@ -204,7 +204,7 @@ libjpeg_empty_output_buffer(j_compress_ptr cinfo)
   return TRUE;
 }
 
-static inline uns
+static inline uint
 libjpeg_read_byte(struct libjpeg_read_internals *i)
 {
   DBG("libjpeg_read_byte()");
@@ -216,7 +216,7 @@ libjpeg_read_byte(struct libjpeg_read_internals *i)
 }
 
 static inline void
-libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
+libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uint len)
 {
   DBG("libjpeg_read_buf(len=%u)", len);
   while (len)
@@ -224,8 +224,8 @@ libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns 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);
+      uint buf_size = i->src.bytes_in_buffer;
+      uint 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;
@@ -240,7 +240,7 @@ 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;
+  uint len = libjpeg_read_byte(i) << 8;
   len += libjpeg_read_byte(i);
   DBG("Found APP1 marker, len=%u", len);
   if (len < 2)
@@ -355,37 +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;
+  uint read_flags = io->flags;
 
   /* Select color space */
-  switch (read_flags & IMAGE_COLOR_SPACE)
+  switch (i->cinfo.jpeg_color_space)
     {
-      case COLOR_SPACE_GRAYSCALE:
-       i->cinfo.out_color_space = JCS_GRAYSCALE;
-       break;
-      case COLOR_SPACE_YCBCR:
+      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_CMYK:
+      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 COLOR_SPACE_YCCK:
+      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:
-       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_YCbCr;
-             break;
-           default:
-             read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; 
-             i->cinfo.out_color_space = JCS_RGB;
-             break;
-         }
+       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;
@@ -408,7 +403,7 @@ libjpeg_read_data(struct image_io *io)
       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);
+  DBG("Output dimensions %ux%u", (uint)i->cinfo.output_width, (uint)i->cinfo.output_height);
   if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, read_flags)))
     {
       jpeg_destroy_decompress(&i->cinfo);
@@ -430,7 +425,7 @@ libjpeg_read_data(struct image_io *io)
   if ((int)img->pixel_size == i->cinfo.output_components)
     {
       byte *pixels = img->pixels;
-      for (uns r = img->rows; r--; )
+      for (uint r = img->rows; r--; )
         {
           jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
           pixels += img->row_size;
@@ -451,7 +446,7 @@ libjpeg_read_data(struct image_io *io)
 #             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"
+#             include <images/image-walk.h>
            }
            break;
          case 4: /* * -> *+Alpha or aligned * */
@@ -465,7 +460,7 @@ libjpeg_read_data(struct image_io *io)
 #             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"
+#             include <images/image-walk.h>
            }
            break;
          default:
@@ -539,6 +534,7 @@ libjpeg_write(struct image_io *io)
     }
   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)
@@ -560,7 +556,7 @@ libjpeg_write(struct image_io *io)
   if ((int)img->pixel_size == i.cinfo.input_components)
     {
       byte *pixels = img->pixels;
-      for (uns r = img->rows; r--; )
+      for (uint r = img->rows; r--; )
         {
           jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
           pixels += img->row_size;
@@ -581,7 +577,7 @@ libjpeg_write(struct image_io *io)
 #             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"
+#             include <images/image-walk.h>
              break;
            }
          case 4: /* *+Alpha or aligned * -> * */
@@ -595,7 +591,7 @@ libjpeg_write(struct image_io *io)
 #             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"
+#             include <images/image-walk.h>
              break;
            }
          default: