From b89a8831b2a64f9995eb7ade9d0a9e06da784950 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Wed, 2 Aug 2006 10:20:40 +0200 Subject: [PATCH] simplified destroying of images --- images/images.h | 4 ++-- images/io-main.c | 36 +++++++++++++++--------------------- images/io-main.h | 1 - 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/images/images.h b/images/images.h index 485c3eb3..402ee28e 100644 --- a/images/images.h +++ b/images/images.h @@ -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); diff --git a/images/io-main.c b/images/io-main.c index 03a36cc2..f0902d3b 100644 --- a/images/io-main.c +++ b/images/io-main.c @@ -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); } diff --git a/images/io-main.h b/images/io-main.h index c7cfffcc..3088faf9 100644 --- a/images/io-main.h +++ b/images/io-main.h @@ -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); -- 2.39.5