]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libmagick.c
ucw docs: init and commit hooks described in-place
[libucw.git] / images / io-libmagick.c
index db5d73987c56bfc39bfcb30f3d04e5bca377e6cc..fd9069f349d210297e53853dd9adc034f7a4cb77 100644 (file)
@@ -9,20 +9,29 @@
 
 #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/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;
@@ -30,14 +39,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
@@ -46,8 +73,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);
 }
 
@@ -57,10 +82,10 @@ libmagick_read_header(struct image_io *io)
   DBG("libmagick_read_header()");
 
   /* Read entire stream */
-  sh_off_t file_size = bfilesize(io->fastbuf) - btell(io->fastbuf);
+  ucw_off_t file_size = bfilesize(io->fastbuf) - btell(io->fastbuf);
   if (unlikely(file_size > MAX_FILE_SIZE))
     {
-      image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too long stream.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too long stream.");
       return 0;
     }
   uns buf_size = file_size;
@@ -68,10 +93,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;
@@ -81,12 +105,12 @@ libmagick_read_header(struct image_io *io)
   xfree(buf);
   if (unlikely(!rd->image))
     {
-      image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "GraphicsMagick failed to read the image.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "GraphicsMagick failed to read the image.");
       goto err;
     }
-  if (unlikely(rd->image->columns > IMAGE_MAX_SIZE || rd->image->rows > IMAGE_MAX_SIZE))
+  if (unlikely(rd->image->columns > image_max_dim || rd->image->rows > image_max_dim))
     {
-      image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Image too large.");
       goto err;
     }
 
@@ -96,15 +120,17 @@ 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->has_palette = 1;
+    io->flags |= IMAGE_IO_HAS_PALETTE;
 
   io->read_cancel = libmagick_read_cancel;
   return 1;
@@ -117,7 +143,7 @@ err:
 static inline byte
 libmagick_pixel_to_gray(PixelPacket *pixel)
 {
-  return ((uns)pixel->red * 19660 + (uns)pixel->green * 38666 + (uns)pixel->blue * 7210) >> (16 + QUANTUM_SCALE);
+  return rgb_to_gray_func(pixel->red, pixel->green, pixel->blue) >> QUANTUM_SCALE;
 }
 
 int
@@ -141,30 +167,37 @@ libmagick_read_data(struct image_io *io)
        break;
     }
 
-  /* Allocate image for conversion */
-  int need_scale = io->cols != rd->image->columns || io->rows != rd->image->rows;
-  int need_destroy = need_scale || !io->pool;
-  struct image *img = need_scale ?
-    image_new(io->thread, rd->image->columns, rd->image->rows, io->flags & IMAGE_CHANNELS_FORMAT, NULL) :
-    image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
-  if (unlikely(!img))
-    goto err;
+  /* Prepare the image */
+  struct image_io_read_data_internals rdi;
+  uns read_flags = io->flags;
+  uns cs = read_flags & IMAGE_COLOR_SPACE;
+  if (cs != COLOR_SPACE_GRAYSCALE && cs != COLOR_SPACE_RGB)
+    read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_PIXEL_FORMAT) | COLOR_SPACE_RGB;
+  if ((read_flags & IMAGE_IO_USE_BACKGROUND) && !(read_flags & IMAGE_ALPHA))
+    read_flags = (read_flags & IMAGE_CHANNELS_FORMAT) | IMAGE_ALPHA;
+  if (unlikely(!image_io_read_data_prepare(&rdi, io, rd->image->columns, rd->image->rows, read_flags)))
+    {
+      libmagick_destroy_read_data(rd);
+      return 0;
+    }
 
   /* Acquire pixels */
   PixelPacket *src = (PixelPacket *)AcquireImagePixels(rd->image, 0, 0, rd->image->columns, rd->image->rows, &rd->exception);
   if (unlikely(!src))
     {
-      image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot acquire image pixels.");
-      goto err;
+      IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Cannot acquire image pixels.");
+      libmagick_destroy_read_data(rd);
+      image_io_read_data_break(&rdi, io);
+      return 0;
     }
 
   /* Convert pixels */
-  switch (img->pixel_size)
+  switch (rdi.image->pixel_size)
     {
       case 1:
 #      define IMAGE_WALK_PREFIX(x) walk_##x
 #       define IMAGE_WALK_INLINE
-#      define IMAGE_WALK_IMAGE img
+#      define IMAGE_WALK_IMAGE (rdi.image)
 #       define IMAGE_WALK_UNROLL 4
 #       define IMAGE_WALK_COL_STEP 1
 #       define IMAGE_WALK_DO_STEP do{ \
@@ -176,12 +209,12 @@ libmagick_read_data(struct image_io *io)
       case 2:
 #      define IMAGE_WALK_PREFIX(x) walk_##x
 #       define IMAGE_WALK_INLINE
-#      define IMAGE_WALK_IMAGE img
+#      define IMAGE_WALK_IMAGE (rdi.image)
 #       define IMAGE_WALK_UNROLL 4
 #       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;
@@ -189,7 +222,7 @@ libmagick_read_data(struct image_io *io)
       case 3:
 #      define IMAGE_WALK_PREFIX(x) walk_##x
 #       define IMAGE_WALK_INLINE
-#      define IMAGE_WALK_IMAGE img
+#      define IMAGE_WALK_IMAGE (rdi.image)
 #       define IMAGE_WALK_UNROLL 4
 #       define IMAGE_WALK_COL_STEP 3
 #       define IMAGE_WALK_DO_STEP do{ \
@@ -203,14 +236,14 @@ libmagick_read_data(struct image_io *io)
       case 4:
 #      define IMAGE_WALK_PREFIX(x) walk_##x
 #       define IMAGE_WALK_INLINE
-#      define IMAGE_WALK_IMAGE img
+#      define IMAGE_WALK_IMAGE (rdi.image)
 #       define IMAGE_WALK_UNROLL 4
 #       define IMAGE_WALK_COL_STEP 4
 #       define IMAGE_WALK_DO_STEP do{ \
          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;
@@ -222,32 +255,8 @@ libmagick_read_data(struct image_io *io)
   /* Free GraphicsMagick structures */
   libmagick_destroy_read_data(rd);
 
-  /* Scale image */
-  if (need_scale)
-    {
-      struct image *img2 = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
-      if (unlikely(!img2))
-        goto err2;
-      int result = image_scale(io->thread, img2, img);
-      image_destroy(img);
-      img = img2;
-      need_destroy = !io->pool;
-      if (unlikely(!result))
-       goto err2;
-    }
-
-  /* Success */
-  io->image = img;
-  io->image_destroy = need_destroy;
-  return 1;
-
-  /* Free structures */
-err:
-  libmagick_destroy_read_data(rd);
-err2:
-  if (need_destroy)
-    image_destroy(img);
-  return 0;
+  /* Finish the image */
+  return image_io_read_data_finish(&rdi, io);
 }
 
 int
@@ -259,7 +268,6 @@ libmagick_write(struct image_io *io)
   int result = 0;
   ExceptionInfo exception;
   ImageInfo *info;
-  InitializeMagick(NULL);
   GetExceptionInfo(&exception);
   info = CloneImageInfo(NULL);
 
@@ -274,7 +282,8 @@ libmagick_write(struct image_io *io)
         info->colorspace = RGBColorspace;
         break;
       default:
-        ASSERT(0);
+        IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Unsupported color space.");
+        goto err;
     }
   switch (io->format)
     {
@@ -295,7 +304,7 @@ libmagick_write(struct image_io *io)
   Image *image = AllocateImage(info);
   if (unlikely(!image))
     {
-      image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
       goto err;
     }
   image->columns = img->cols;
@@ -305,7 +314,7 @@ libmagick_write(struct image_io *io)
   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.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Cannot get GraphicsMagick pixels.");
       goto err2;
     }
 
@@ -322,7 +331,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;
@@ -337,7 +346,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;
@@ -352,7 +361,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;
@@ -367,7 +376,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;
@@ -379,7 +388,7 @@ libmagick_write(struct image_io *io)
   /* Store pixels */
   if (unlikely(!SyncImagePixels(image)))
     {
-      image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot sync GraphicsMagick pixels.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Cannot sync GraphicsMagick pixels.");
       goto err2;
     }
 
@@ -388,12 +397,12 @@ libmagick_write(struct image_io *io)
   void *buf = ImageToBlob(info, image, &buf_len, &exception);
   if (unlikely(!buf))
     {
-      image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to compress the image.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "GraphicsMagick failed to compress the image.");
       goto err2;
     }
   if (unlikely(buf_len > MAX_FILE_SIZE))
     {
-      image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Image too large.");
+      IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Image too large.");
       goto err2;
     }
 
@@ -408,6 +417,5 @@ err2:
 err:
   DestroyImageInfo(info);
   DestroyExceptionInfo(&exception);
-  DestroyMagick();
   return result;
 }