]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libmagick.c
reverted invalid usage of GET*/PUT* macros
[libucw.git] / images / io-libmagick.c
index e4bca9c37e466bc69b34db369333aaf802e3f1c5..543f3f8effc5b5787c5b6383deb3c8fb88f68533 100644 (file)
 #include "images/images.h"
 #include "images/color.h"
 #include "images/io-main.h"
+
 #include <sys/types.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <magick/api.h>
+#include <pthread.h>
 
 #define MAX_FILE_SIZE (1 << 30)
 #define QUANTUM_SCALE (QuantumDepth - 8)
 #define QUANTUM_TO_BYTE(x) ((uns)(x) >> QUANTUM_SCALE)
 #define BYTE_TO_QUANTUM(x) ((uns)(x) << QUANTUM_SCALE)
-#define OPACITY_MAX ((1 << QuantumDepth) - 1)
+#define ALPHA_TO_BYTE(x) (255 - QUANTUM_TO_BYTE(x))
+#define BYTE_TO_ALPHA(x) (BYTE_TO_QUANTUM(255 - (x)))
+
+static pthread_mutex_t libmagick_mutex = PTHREAD_MUTEX_INITIALIZER;
+static uns libmagick_counter;
 
 struct magick_read_data {
   ExceptionInfo exception;
@@ -32,14 +38,32 @@ struct magick_read_data {
   Image *image;
 };
 
-static inline void
+int
+libmagick_init(struct image_io *io UNUSED)
+{
+  pthread_mutex_lock(&libmagick_mutex);
+  if (!libmagick_counter++)
+    InitializeMagick(NULL);
+  pthread_mutex_unlock(&libmagick_mutex);
+  return 1;
+}
+
+void
+libmagick_cleanup(struct image_io *io UNUSED)
+{
+  pthread_mutex_lock(&libmagick_mutex);
+  if (!--libmagick_counter)
+    DestroyMagick();
+  pthread_mutex_unlock(&libmagick_mutex);
+}
+
+static void
 libmagick_destroy_read_data(struct magick_read_data *rd)
 {
   if (rd->image)
     DestroyImage(rd->image);
   DestroyImageInfo(rd->info);
   DestroyExceptionInfo(&rd->exception);
-  DestroyMagick();
 }
 
 static void
@@ -48,8 +72,6 @@ libmagick_read_cancel(struct image_io *io)
   DBG("libmagick_read_cancel()");
 
   struct magick_read_data *rd = io->read_data;
-
-  DestroyImage(rd->image);
   libmagick_destroy_read_data(rd);
 }
 
@@ -70,10 +92,9 @@ libmagick_read_header(struct image_io *io)
   breadb(io->fastbuf, buf, buf_size);
 
   /* Allocate read structure */
-  struct magick_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
+  struct magick_read_data *rd = io->read_data = mp_alloc_zero(io->internal_pool, sizeof(*rd));
 
   /* Initialize GraphicsMagick */
-  InitializeMagick(NULL);
   GetExceptionInfo(&rd->exception);
   rd->info = CloneImageInfo(NULL);
   rd->info->subrange = 1;
@@ -98,12 +119,14 @@ libmagick_read_header(struct image_io *io)
   switch (rd->image->colorspace)
     {
       case GRAYColorspace:
-        io->flags = COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
+        io->flags = COLOR_SPACE_GRAYSCALE;
         break;
       default:
-        io->flags = COLOR_SPACE_RGB | IMAGE_ALPHA;
+        io->flags = COLOR_SPACE_RGB;
         break;
     }
+  if (rd->image->matte)
+    io->flags |= IMAGE_ALPHA;
   io->number_of_colors = rd->image->colors;
   if (rd->image->storage_class == PseudoClass && rd->image->compression != JPEGCompression)
     io->flags |= IMAGE_IO_HAS_PALETTE;
@@ -147,7 +170,7 @@ libmagick_read_data(struct image_io *io)
   struct image_io_read_data_internals rdi;
   uns read_flags = io->flags;
   if ((read_flags & IMAGE_IO_USE_BACKGROUND) && !(read_flags & IMAGE_ALPHA))
-    read_flags |= IMAGE_ALPHA;     
+    read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT;
   if (unlikely(!image_io_read_data_prepare(&rdi, io, rd->image->columns, rd->image->rows, read_flags)))
     {
       libmagick_destroy_read_data(rd);
@@ -187,7 +210,7 @@ libmagick_read_data(struct image_io *io)
 #       define IMAGE_WALK_COL_STEP 2
 #       define IMAGE_WALK_DO_STEP do{ \
          walk_pos[0] = libmagick_pixel_to_gray(src); \
-         walk_pos[1] = QUANTUM_TO_BYTE(src->opacity); \
+         walk_pos[1] = ALPHA_TO_BYTE(src->opacity); \
          src++; }while(0)
 #       include "images/image-walk.h"
        break;
@@ -216,7 +239,7 @@ libmagick_read_data(struct image_io *io)
          walk_pos[0] = QUANTUM_TO_BYTE(src->red); \
          walk_pos[1] = QUANTUM_TO_BYTE(src->green); \
          walk_pos[2] = QUANTUM_TO_BYTE(src->blue); \
-         walk_pos[3] = QUANTUM_TO_BYTE(src->opacity); \
+         walk_pos[3] = ALPHA_TO_BYTE(src->opacity); \
          src++; }while(0)
 #       include "images/image-walk.h"
        break;
@@ -241,7 +264,6 @@ libmagick_write(struct image_io *io)
   int result = 0;
   ExceptionInfo exception;
   ImageInfo *info;
-  InitializeMagick(NULL);
   GetExceptionInfo(&exception);
   info = CloneImageInfo(NULL);
 
@@ -304,7 +326,7 @@ libmagick_write(struct image_io *io)
          dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
          dest->green = BYTE_TO_QUANTUM(walk_pos[0]); \
          dest->blue = BYTE_TO_QUANTUM(walk_pos[0]); \
-         dest->opacity = OPACITY_MAX; \
+         dest->opacity = 0; \
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
@@ -319,7 +341,7 @@ libmagick_write(struct image_io *io)
          dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
          dest->green = BYTE_TO_QUANTUM(walk_pos[0]); \
          dest->blue = BYTE_TO_QUANTUM(walk_pos[0]); \
-         dest->opacity = BYTE_TO_QUANTUM(walk_pos[1]); \
+         dest->opacity = BYTE_TO_ALPHA(walk_pos[1]); \
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
@@ -334,7 +356,7 @@ libmagick_write(struct image_io *io)
          dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
          dest->green = BYTE_TO_QUANTUM(walk_pos[1]); \
          dest->blue = BYTE_TO_QUANTUM(walk_pos[2]); \
-         dest->opacity = OPACITY_MAX; \
+         dest->opacity = 0; \
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
@@ -349,7 +371,7 @@ libmagick_write(struct image_io *io)
          dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
          dest->green = BYTE_TO_QUANTUM(walk_pos[1]); \
          dest->blue = BYTE_TO_QUANTUM(walk_pos[2]); \
-         dest->opacity = BYTE_TO_QUANTUM(walk_pos[3]); \
+         dest->opacity = BYTE_TO_ALPHA(walk_pos[3]); \
          dest++; }while(0)
 #       include "images/image-walk.h"
        break;
@@ -390,6 +412,5 @@ err2:
 err:
   DestroyImageInfo(info);
   DestroyExceptionInfo(&exception);
-  DestroyMagick();
   return result;
 }