]> mj.ucw.cz Git - libucw.git/commitdiff
simplified destroying of images
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Wed, 2 Aug 2006 08:20:40 +0000 (10:20 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Wed, 2 Aug 2006 08:20:40 +0000 (10:20 +0200)
images/images.h
images/io-main.c
images/io-main.h

index 485c3eb3a346b72526f593d1056923d25e6ea069..402ee28e90b688cd82441eff692a790f7b611692 100644 (file)
@@ -148,15 +148,15 @@ struct image_io {
   /* internals */
   struct image_thread *thread;
   struct mempool *internal_pool;
-  int image_destroy;
   void *read_data;
   void (*read_cancel)(struct image_io *io);
 };
 
 enum image_io_flags {
   IMAGE_IO_IMAGE_FLAGS = 0xffff,       /* [ HI  ] - mask of parameters to image new, read_header fills IMAGE_CHANNELS_FORMAT */
+  IMAGE_IO_NEED_DESTROY = 0x10000,     /* [   O ] - enables automatic call of image_destroy */
   IMAGE_IO_HAS_PALETTE = 0x10000,      /* [ H   ] - true for image with indexed colors */
-  IMAGE_IO_USE_BACKGROUND = 0x20000,   /* [  I  ] - merge transparent pixels with background_color */
+  IMAGE_IO_USE_BACKGROUND = 0x40000,   /* [  I  ] - merge transparent pixels with background_color */
 };
 
 void image_io_init(struct image_thread *it, struct image_io *io);
index 03a36cc266305e310295938a54ac6bfae55360f6..f0902d3b5ad3c1ca1ee1650239e8546456bd2164 100644 (file)
@@ -36,10 +36,10 @@ image_io_read_cancel(struct image_io *io)
 static inline void
 image_io_image_destroy(struct image_io *io)
 {
-  if (io->image_destroy)
+  if (io->image && (io->flags & IMAGE_IO_NEED_DESTROY))
     {
       image_destroy(io->image);
-      io->image_destroy = 0;
+      io->flags &= ~IMAGE_IO_NEED_DESTROY;
       io->image = NULL;
     }
 }
@@ -150,8 +150,8 @@ image_io_read_data(struct image_io *io, int ref)
   }
   if (result)
     {
-      if (ref)
-       io->image_destroy = 0;
+      if (!ref)
+       io->flags |= IMAGE_IO_NEED_DESTROY;
       return io->image;
     }
   else
@@ -242,16 +242,11 @@ struct image *
 image_io_read_data_prepare(struct image_io_read_data_internals *rdi, struct image_io *io, uns cols, uns rows, uns flags)
 {
   DBG("image_io_read_data_prepare()");
-  if (rdi->need_transformations = io->cols != cols || io->rows != rows || io->flags != flags)
-    {
-      rdi->need_destroy = 1;
-      return rdi->image = image_new(io->thread, cols, rows, flags & IMAGE_IO_IMAGE_FLAGS, NULL);
-    }
+  if (rdi->need_transformations = io->cols != cols || io->rows != rows ||
+      ((io->flags ^ flags) & (IMAGE_IO_IMAGE_FLAGS & ~IMAGE_NEED_DESTROY)))
+    return rdi->image = image_new(io->thread, cols, rows, flags & IMAGE_IO_IMAGE_FLAGS, NULL);
   else
-    {
-      rdi->need_destroy = !io->pool;
-      return rdi->image = image_new(io->thread, io->cols, io->rows, io->flags & IMAGE_IO_IMAGE_FLAGS, io->pool);
-    }
+    return rdi->image = image_new(io->thread, io->cols, io->rows, io->flags & IMAGE_IO_IMAGE_FLAGS, io->pool);
 }
 
 int
@@ -264,7 +259,7 @@ image_io_read_data_finish(struct image_io_read_data_internals *rdi, struct image
       if (io->cols != rdi->image->cols || io->rows != rdi->image->rows)
         {
          DBG("Scaling image");
-         rdi->need_destroy = rdi->need_transformations || !io->pool;
+         rdi->need_transformations = ((io->flags ^ rdi->image->flags) & (IMAGE_IO_IMAGE_FLAGS & ~IMAGE_NEED_DESTROY));
          struct image *img = image_new(io->thread, io->cols, io->rows, rdi->image->flags, rdi->need_transformations ? NULL : io->pool);
          if (unlikely(!img))
            {
@@ -274,8 +269,7 @@ image_io_read_data_finish(struct image_io_read_data_internals *rdi, struct image
           if (unlikely(!image_scale(io->thread, img, rdi->image)))
             {
               image_destroy(rdi->image);
-             if (rdi->need_destroy)
-               image_destroy(img);
+             image_destroy(img);
              return 0;
            }
          rdi->image = img;
@@ -285,7 +279,7 @@ image_io_read_data_finish(struct image_io_read_data_internals *rdi, struct image
       if ((io->flags ^ rdi->image->flags) & IMAGE_ALPHA)
         {
          DBG("Aplying background");
-         rdi->need_destroy = rdi->need_transformations || !io->pool;
+         rdi->need_transformations = 0;
          struct image *img = image_new(io->thread, io->cols, io->rows, io->flags, rdi->need_transformations ? NULL : io->pool);
          if (unlikely(!img))
            {
@@ -295,17 +289,17 @@ image_io_read_data_finish(struct image_io_read_data_internals *rdi, struct image
           if (unlikely(!image_apply_background(io->thread, img, rdi->image, &io->background_color)))
             {
               image_destroy(rdi->image);
-             if (rdi->need_destroy)
-               image_destroy(img);
+             image_destroy(img);
              return 0;
            }
          rdi->image = img;
        }
+
+      ASSERT(!rdi->need_transformations);
     }
 
   /* Success */
   io->image = rdi->image;
-  io->image_destroy = rdi->need_destroy;
   return 1;
 }
 
@@ -313,6 +307,6 @@ void
 image_io_read_data_break(struct image_io_read_data_internals *rdi, struct image_io *io UNUSED)
 {
   DBG("image_io_read_data_break()");
-  if (rdi->need_destroy)
+  if (rdi->image)
     image_destroy(rdi->image);
 }
index c7cfffcc966dac2000869191752e634b2a4a92bc..3088faf90a93ed8f19340b945708c0e498870f1b 100644 (file)
@@ -19,7 +19,6 @@ int libmagick_write(struct image_io *io);
 struct image_io_read_data_internals {
   struct image *image;
   int need_transformations;
-  int need_destroy;
 };
 
 struct image *image_io_read_data_prepare(struct image_io_read_data_internals *rdi, struct image_io *io, uns cols, uns rows, uns flags);