/* 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);
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;
}
}
}
if (result)
{
- if (ref)
- io->image_destroy = 0;
+ if (!ref)
+ io->flags |= IMAGE_IO_NEED_DESTROY;
return io->image;
}
else
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
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))
{
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;
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))
{
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;
}
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);
}