#include "images/io-main.h"
#include <string.h>
-void
+int
image_io_init(struct image_thread *it, struct image_io *io)
{
DBG("image_io_init()");
bzero(io, sizeof(*io));
io->thread = it;
+#ifdef CONFIG_IMAGES_LIBJPEG
+ if (!libjpeg_init(io))
+ goto libjpeg_failed;
+#endif
+#ifdef CONFIG_IMAGES_LIBPNG
+ if (!libpng_init(io))
+ goto libpng_failed;
+#endif
+#ifdef CONFIG_IMAGES_LIBUNGIF
+ if (!libungif_init(io))
+ goto libungif_failed;
+#endif
+#ifdef CONFIG_IMAGES_LIBMAGICK
+ if (!libmagick_init(io))
+ goto libmagick_failed;
+#endif
io->internal_pool = mp_new(1024);
+ return 1;
+#ifdef CONFIG_IMAGES_LIBMAGICK
+ libmagick_cleanup(io);
+libmagick_failed:
+#endif
+#ifdef CONFIG_IMAGES_LIBUNGIF
+ libungif_cleanup(io);
+libungif_failed:
+#endif
+#ifdef CONFIG_IMAGES_LIBPNG
+ libpng_cleanup(io);
+libpng_failed:
+#endif
+#ifdef CONFIG_IMAGES_LIBJPEG
+ libjpeg_cleanup(io);
+libjpeg_failed:
+#endif
+ return 0;
}
static inline void
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;
}
}
DBG("image_io_cleanup()");
image_io_read_cancel(io);
image_io_image_destroy(io);
+#ifdef CONFIG_IMAGES_LIBMAGICK
+ libmagick_cleanup(io);
+#endif
+#ifdef CONFIG_IMAGES_LIBUNGIF
+ libungif_cleanup(io);
+#endif
+#ifdef CONFIG_IMAGES_LIBPNG
+ libpng_cleanup(io);
+#endif
+#ifdef CONFIG_IMAGES_LIBJPEG
+ libjpeg_cleanup(io);
+#endif
mp_delete(io->internal_pool);
}
image_io_read_cancel(io);
image_io_image_destroy(io);
struct mempool *pool = io->internal_pool;
+ struct image_thread *thread = io->thread;
mp_flush(pool);
bzero(io, sizeof(*io));
io->internal_pool = pool;
+ io->thread = thread;
}
int
break;
case IMAGE_FORMAT_UNDEFINED:
- // FIXME: auto-detect
+#if defined (CONFIG_IMAGES_LIBMAGICK)
+ return libmagick_read_header(io);
+#endif
break;
default:
#endif
break;
+ case IMAGE_FORMAT_UNDEFINED:
+#if defined(CONFIG_IMAGES_LIBMAGICK)
+ result = libmagick_read_data(io);
+#else
+ ASSERT(0);
+#endif
+ break;
+
default:
ASSERT(0);
}
if (result)
{
- if (ref)
- io->image_destroy = 0;
+ if (!ref)
+ io->flags |= IMAGE_IO_NEED_DESTROY;
+ else
+ io->flags &= ~IMAGE_IO_NEED_DESTROY;
return io->image;
}
else
default:
break;
}
- image_thread_err(io->thread, IMAGE_ERR_INVALID_FILE_FORMAT, "Image format not supported.");
+ image_thread_err(io->thread, IMAGE_ERR_INVALID_FILE_FORMAT, "Output format not supported.");
return 0;
}
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_NEW_FLAGS))
+ 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;
- struct image *img = image_new(io->thread, io->cols, io->rows, rdi->image->flags, rdi->need_transformations ? NULL : io->pool);
+ uns flags = rdi->image->flags;
+ if (!(rdi->need_transformations = ((io->flags ^ rdi->image->flags) & (IMAGE_NEW_FLAGS & ~IMAGE_PIXELS_ALIGNED))))
+ flags = io->flags;
+ struct image *img = image_new(io->thread, io->cols, io->rows, flags, rdi->need_transformations ? NULL : io->pool);
if (unlikely(!img))
{
image_destroy(rdi->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;
}
+ image_destroy(rdi->image);
rdi->image = img;
}
if ((io->flags ^ rdi->image->flags) & IMAGE_ALPHA)
{
DBG("Aplying background");
- rdi->need_destroy = rdi->need_transformations || !io->pool;
- struct image *img = image_new(io->thread, io->cols, io->rows, io->flags, rdi->need_transformations ? NULL : io->pool);
+ uns flags = rdi->image->flags & ~IMAGE_ALPHA;
+ if (!(rdi->need_transformations = (flags ^ io->flags) & (IMAGE_NEW_FLAGS & ~IMAGE_PIXELS_ALIGNED)))
+ flags = io->flags;
+ struct image *img = image_new(io->thread, io->cols, io->rows, flags, rdi->need_transformations ? NULL : io->pool);
if (unlikely(!img))
{
image_destroy(rdi->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;
}
+ image_destroy(rdi->image);
rdi->image = img;
}
+
+ // FIXME: support for various color spaces
+
+ 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);
}