]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libmagick.c
added support for libungif
[libucw.git] / images / io-libmagick.c
index 1bbdfc0c4fa9732a8a86231b1e57df50139a38e8..ee0480d1b5371c7606eb051088d5b67468d0dce4 100644 (file)
@@ -7,7 +7,7 @@
  *     of the GNU Lesser General Public License.
  */
 
-#define LOCAL_DEBUG
+#undef LOCAL_DEBUG
 
 #include "lib/lib.h"
 #include "lib/mempool.h"
@@ -65,7 +65,7 @@ libmagick_read_header(struct image_io *io)
     }
   uns buf_size = file_size;
   byte *buf = xmalloc(buf_size);
-  bread(io->fastbuf, buf, buf_size);
+  breadb(io->fastbuf, buf, buf_size);
 
   /* Allocate read structure */
   struct magick_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
@@ -91,22 +91,20 @@ libmagick_read_header(struct image_io *io)
     }
 
   /* Fill image parameters */
-  if (!io->cols)
-    io->cols = rd->image->columns;
-  if (!io->rows)
-    io->rows = rd->image->rows;
-  if (!(io->flags & IMAGE_CHANNELS_FORMAT))
+  io->cols = rd->image->columns;
+  io->rows = rd->image->rows;
+  switch (rd->image->colorspace)
     {
-      switch (rd->image->colorspace)
-        {
-         case GRAYColorspace:
-           io->flags |= COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
-           break;
-         default:
-           io->flags |= COLOR_SPACE_RGB | IMAGE_ALPHA;
-           break;
-       }
+      case GRAYColorspace:
+        io->flags |= COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
+        break;
+      default:
+        io->flags |= COLOR_SPACE_RGB | IMAGE_ALPHA;
+        break;
     }
+  io->number_of_colors = rd->image->colors;
+  if (rd->image->storage_class == PseudoClass && rd->image->compression != JPEGCompression)
+    io->has_palette = 1;
 
   io->read_cancel = libmagick_read_cancel;
   return 1;
@@ -223,7 +221,7 @@ libmagick_read_data(struct image_io *io)
       if (unlikely(!img2))
         goto err2;
       int result = image_scale(io->thread, img2, img);
-      image_destroy(io->thread, img);
+      image_destroy(img);
       img = img2;
       need_destroy = !io->pool;
       if (unlikely(!result))
@@ -240,7 +238,7 @@ err:
   libmagick_destroy_read_data(rd);
 err2:
   if (need_destroy)
-    image_destroy(io->thread, img);
+    image_destroy(img);
   return 0;
 }
 
@@ -258,7 +256,8 @@ libmagick_write(struct image_io *io)
   info = CloneImageInfo(NULL);
 
   /* Setup image parameters and allocate the image*/
-  switch (io->flags & IMAGE_COLOR_SPACE)
+  struct image *img = io->image;
+  switch (img->flags & IMAGE_COLOR_SPACE)
     {
       case COLOR_SPACE_GRAYSCALE:
        info->colorspace = GRAYColorspace;
@@ -273,6 +272,8 @@ libmagick_write(struct image_io *io)
     {
       case IMAGE_FORMAT_JPEG:
        strcpy(info->magick, "JPEG");
+       if (io->jpeg_quality)
+         info->quality = MIN(io->jpeg_quality, 100);
        break;
       case IMAGE_FORMAT_PNG:
        strcpy(info->magick, "PNG");
@@ -289,11 +290,11 @@ libmagick_write(struct image_io *io)
       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
       goto err;
     }
-  image->columns = io->cols;
-  image->rows = io->rows;
+  image->columns = img->cols;
+  image->rows = img->rows;
 
   /* Get pixels */
-  PixelPacket *pixels = SetImagePixels(image, 0, 0, io->cols, io->rows), *dest = pixels;
+  PixelPacket *pixels = SetImagePixels(image, 0, 0, img->cols, img->rows), *dest = pixels;
   if (unlikely(!pixels))
     {
       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot get GraphicsMagick pixels.");
@@ -301,7 +302,6 @@ libmagick_write(struct image_io *io)
     }
 
   /* Convert pixels */
-  struct image *img = io->image;
   switch (img->pixel_size)
     {
       case 1:
@@ -316,6 +316,7 @@ libmagick_write(struct image_io *io)
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
+
       case 2:
 #       define IMAGE_WALK_INLINE
 #       define IMAGE_WALK_UNROLL 4
@@ -328,6 +329,7 @@ libmagick_write(struct image_io *io)
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
+
       case 3:
 #       define IMAGE_WALK_INLINE
 #       define IMAGE_WALK_UNROLL 4
@@ -340,6 +342,7 @@ libmagick_write(struct image_io *io)
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
+
       case 4:
 #       define IMAGE_WALK_INLINE
 #       define IMAGE_WALK_UNROLL 4
@@ -352,6 +355,9 @@ libmagick_write(struct image_io *io)
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
+
+      default:
+       ASSERT(0);
     }
 
   /* Store pixels */