From: Pavel Charvat Date: Wed, 2 Aug 2006 10:14:22 +0000 (+0200) Subject: cleaning image flags manipulation X-Git-Tag: holmes-import~614 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=0a023d6e079bbfc4112347b6943bad80685e7265;p=libucw.git cleaning image flags manipulation --- diff --git a/images/image.c b/images/image.c index 828313d5..e030a947 100644 --- a/images/image.c +++ b/images/image.c @@ -45,7 +45,7 @@ struct image * image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool) { DBG("image_new(cols=%u rows=%u flags=0x%x pool=%p)", cols, rows, flags, pool); - flags &= IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED; + flags &= IMAGE_NEW_FLAGS; if (unlikely(!image_dimensions_valid(cols, rows))) { image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows); @@ -121,7 +121,7 @@ image_clone(struct image_thread *it, struct image *src, uns flags, struct mempoo { DBG("image_clone(src=%p flags=0x%x pool=%p)", src, src->flags, pool); struct image *img; - flags &= ~IMAGE_CHANNELS_FORMAT; + flags &= IMAGE_NEW_FLAGS & ~IMAGE_CHANNELS_FORMAT; flags |= src->flags & IMAGE_CHANNELS_FORMAT; if (!(img = image_new(it, src->cols, src->rows, flags, pool))) return NULL; @@ -185,7 +185,7 @@ image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns img->rows = rows; img->row_size = row_size; img->image_size = rows * row_size; - img->flags = flags & (IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED); + img->flags = flags & (IMAGE_NEW_FLAGS | IMAGE_GAPS_PROTECTED); return 1; } @@ -199,7 +199,8 @@ image_init_subimage(struct image_thread *it UNUSED, struct image *img, struct im img->rows = rows; img->row_size = src->row_size; img->image_size = src->row_size * rows; - img->flags = src->flags & ~IMAGE_NEED_DESTROY; + img->flags = src->flags & IMAGE_NEW_FLAGS; + img->flags |= IMAGE_GAPS_PROTECTED; return 1; } diff --git a/images/images.h b/images/images.h index 12cada96..4ac9da6e 100644 --- a/images/images.h +++ b/images/images.h @@ -70,9 +70,12 @@ enum image_flag { IMAGE_PIXELS_ALIGNED = 0x10, /* align pixel size to the nearest power of two */ IMAGE_SSE_ALIGNED = 0x20, /* align scanlines to multiples of 16 bytes (both start and size) */ IMAGE_NEED_DESTROY = 0x40, /* image is allocated with xmalloc */ + IMAGE_GAPS_PROTECTED = 0x80, /* cannot write to gaps between rows */ IMAGE_CHANNELS_FORMAT = IMAGE_COLOR_SPACE | IMAGE_ALPHA, IMAGE_PIXEL_FORMAT = IMAGE_CHANNELS_FORMAT | IMAGE_PIXELS_ALIGNED, IMAGE_ALIGNED = IMAGE_PIXELS_ALIGNED | IMAGE_SSE_ALIGNED, + IMAGE_NEW_FLAGS = IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED, + IMAGE_INTERNAL_FLAGS = IMAGE_NEED_DESTROY | IMAGE_GAPS_PROTECTED, }; struct image { @@ -82,13 +85,13 @@ struct image { u32 rows; /* number of rows */ u32 pixel_size; /* size of pixel (1, 2, 3 or 4) */ u32 row_size; /* scanline size in bytes */ - u32 image_size; /* rows * rows_size */ + u32 image_size; /* rows * row_size */ u32 flags; /* enum image_flag */ }; struct image *image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool); struct image *image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool); -void image_destroy(struct image *img); /* only with NULL mempool */ +void image_destroy(struct image *img); void image_clear(struct image_thread *it, struct image *img); int image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags); int image_init_subimage(struct image_thread *it, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows); diff --git a/images/io-libmagick.c b/images/io-libmagick.c index e4bca9c3..bd503e79 100644 --- a/images/io-libmagick.c +++ b/images/io-libmagick.c @@ -147,7 +147,7 @@ libmagick_read_data(struct image_io *io) struct image_io_read_data_internals rdi; uns read_flags = io->flags; if ((read_flags & IMAGE_IO_USE_BACKGROUND) && !(read_flags & IMAGE_ALPHA)) - read_flags |= IMAGE_ALPHA; + read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; if (unlikely(!image_io_read_data_prepare(&rdi, io, rd->image->columns, rd->image->rows, read_flags))) { libmagick_destroy_read_data(rd); diff --git a/images/io-libpng.c b/images/io-libpng.c index a1705a37..f15955a6 100644 --- a/images/io-libpng.c +++ b/images/io-libpng.c @@ -229,7 +229,7 @@ libpng_read_data(struct image_io *io) if (io->flags & IMAGE_IO_USE_BACKGROUND) { png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); - read_flags |= IMAGE_ALPHA; + read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; } else if ((io->flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); @@ -251,7 +251,7 @@ libpng_read_data(struct image_io *io) if (!(io->flags & IMAGE_ALPHA)) { if (io->flags & IMAGE_IO_USE_BACKGROUND) - read_flags |= IMAGE_ALPHA; + read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; else png_set_strip_alpha(rd->png_ptr); } @@ -267,7 +267,7 @@ libpng_read_data(struct image_io *io) png_set_rgb_to_gray_fixed(rd->png_ptr, 1, 21267, 71514); if (!(io->flags & IMAGE_ALPHA)) if (io->flags & IMAGE_IO_USE_BACKGROUND) - read_flags |= IMAGE_ALPHA; + read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; else if ((io->flags & IMAGE_PIXEL_FORMAT) != (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) png_set_strip_alpha(rd->png_ptr); break; diff --git a/images/io-main.c b/images/io-main.c index f0902d3b..0ed20129 100644 --- a/images/io-main.c +++ b/images/io-main.c @@ -152,6 +152,8 @@ image_io_read_data(struct image_io *io, int ref) { if (!ref) io->flags |= IMAGE_IO_NEED_DESTROY; + else + io->flags &= ~IMAGE_IO_NEED_DESTROY; return io->image; } else @@ -243,7 +245,7 @@ image_io_read_data_prepare(struct image_io_read_data_internals *rdi, struct imag { DBG("image_io_read_data_prepare()"); if (rdi->need_transformations = io->cols != cols || io->rows != rows || - ((io->flags ^ flags) & (IMAGE_IO_IMAGE_FLAGS & ~IMAGE_NEED_DESTROY))) + ((io->flags ^ flags) & IMAGE_NEW_FLAGS)) return rdi->image = image_new(io->thread, cols, rows, flags & IMAGE_IO_IMAGE_FLAGS, NULL); else return rdi->image = image_new(io->thread, io->cols, io->rows, io->flags & IMAGE_IO_IMAGE_FLAGS, io->pool); @@ -259,7 +261,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_transformations = ((io->flags ^ rdi->image->flags) & (IMAGE_IO_IMAGE_FLAGS & ~IMAGE_NEED_DESTROY)); + rdi->need_transformations = ((io->flags ^ rdi->image->flags) & IMAGE_NEW_FLAGS); struct image *img = image_new(io->thread, io->cols, io->rows, rdi->image->flags, rdi->need_transformations ? NULL : io->pool); if (unlikely(!img)) { @@ -279,8 +281,9 @@ 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_transformations = 0; - 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; + rdi->need_transformations = (flags & io->flags) & IMAGE_NEW_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);