X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=images%2Fio-libpng.c;h=e39c8b7efd112128daadadae5cc2d31397705462;hb=1bc3bb66e47ec02003658fb3040aef0ffd7b7540;hp=76b2d2478fc290401842b7dbac350ac4cd3d201b;hpb=487f19b958da5875f0c05e53d5c229874662735e;p=libucw.git diff --git a/images/io-libpng.c b/images/io-libpng.c index 76b2d247..e39c8b7e 100644 --- a/images/io-libpng.c +++ b/images/io-libpng.c @@ -9,13 +9,13 @@ #undef LOCAL_DEBUG -#include "lib/lib.h" -#include "lib/mempool.h" -#include "lib/fastbuf.h" -#include "images/images.h" -#include "images/error.h" -#include "images/color.h" -#include "images/io-main.h" +#include +#include +#include +#include +#include +#include +#include #include #include @@ -33,7 +33,7 @@ struct libpng_read_data { static png_voidp libpng_malloc(png_structp png_ptr, png_size_t size) { - DBG("libpng_malloc(size=%u)", (uns)size); + DBG("libpng_malloc(size=%u)", (uint)size); return mp_alloc(png_get_mem_ptr(png_ptr), size); } @@ -68,7 +68,7 @@ libpng_warning(png_structp png_ptr UNUSED, png_const_charp msg UNUSED) static void libpng_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) { - DBG("libpng_read_fn(len=%u)", (uns)length); + DBG("libpng_read_fn(len=%u)", (uint)length); if (unlikely(bread((struct fastbuf *)png_get_io_ptr(png_ptr), (byte *)data, length) < length)) png_error(png_ptr, "Incomplete data"); } @@ -76,7 +76,7 @@ libpng_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) static void libpng_write_fn(png_structp png_ptr, png_bytep data, png_size_t length) { - DBG("libpng_write_fn(len=%u)", (uns)length); + DBG("libpng_write_fn(len=%u)", (uint)length); bwrite((struct fastbuf *)png_get_io_ptr(png_ptr), (byte *)data, length); } @@ -188,18 +188,6 @@ libpng_read_data(struct image_io *io) struct libpng_read_data *rd = io->read_data; - /* Test supported pixel formats */ - switch (io->flags & IMAGE_COLOR_SPACE) - { - case COLOR_SPACE_GRAYSCALE: - case COLOR_SPACE_RGB: - break; - default: - png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr); - IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Unsupported color space."); - return 0; - } - struct image_io_read_data_internals rdi; rdi.image = NULL; @@ -212,29 +200,32 @@ libpng_read_data(struct image_io *io) return 0; } - uns read_flags = io->flags; - + uint read_flags = io->flags; + /* Apply transformations */ if (rd->bit_depth == 16) png_set_strip_16(rd->png_ptr); switch (rd->color_type) { case PNG_COLOR_TYPE_PALETTE: - if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) + if ((read_flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) { png_set_palette_to_rgb(rd->png_ptr); png_set_rgb_to_gray_fixed(rd->png_ptr, 1, 21267, 71514); } else - png_set_palette_to_rgb(rd->png_ptr); - if (!(io->flags & IMAGE_ALPHA)) + { + png_set_palette_to_rgb(rd->png_ptr); + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; + } + if (!(read_flags & IMAGE_ALPHA)) { if (io->flags & IMAGE_IO_USE_BACKGROUND) { png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); - read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; + read_flags = (read_flags & IMAGE_CHANNELS_FORMAT) | IMAGE_ALPHA; } - else if ((io->flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) + else if ((read_flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); else png_set_strip_alpha(rd->png_ptr); @@ -243,35 +234,45 @@ libpng_read_data(struct image_io *io) png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); break; case PNG_COLOR_TYPE_GRAY: - if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_RGB) - png_set_gray_to_rgb(rd->png_ptr); - if (io->flags & IMAGE_ALPHA) + if ((read_flags & IMAGE_COLOR_SPACE) != COLOR_SPACE_GRAYSCALE) + { + png_set_gray_to_rgb(rd->png_ptr); + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; + } + if (read_flags & IMAGE_ALPHA) png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); break; case PNG_COLOR_TYPE_GRAY_ALPHA: - if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_RGB) - png_set_gray_to_rgb(rd->png_ptr); - if (!(io->flags & IMAGE_ALPHA)) + if ((read_flags & IMAGE_COLOR_SPACE) != COLOR_SPACE_GRAYSCALE) + { + png_set_gray_to_rgb(rd->png_ptr); + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; + } + if (!(read_flags & IMAGE_ALPHA)) { if (io->flags & IMAGE_IO_USE_BACKGROUND) - read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; + read_flags = (read_flags & IMAGE_CHANNELS_FORMAT) | IMAGE_ALPHA; else png_set_strip_alpha(rd->png_ptr); - } + } break; case PNG_COLOR_TYPE_RGB: - if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) + if ((read_flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) png_set_rgb_to_gray_fixed(rd->png_ptr, 1, 21267, 71514); - if ((io->flags & IMAGE_ALPHA) || (io->flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) + else + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; + if ((read_flags & IMAGE_ALPHA) || (read_flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER); break; case PNG_COLOR_TYPE_RGB_ALPHA: - if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) + if ((read_flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) png_set_rgb_to_gray_fixed(rd->png_ptr, 1, 21267, 71514); - if (!(io->flags & IMAGE_ALPHA)) + else + read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; + if (!(read_flags & IMAGE_ALPHA)) if (io->flags & IMAGE_IO_USE_BACKGROUND) - read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT; - else if ((io->flags & IMAGE_PIXEL_FORMAT) != (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) + read_flags = (read_flags & IMAGE_CHANNELS_FORMAT) | IMAGE_ALPHA; + else if ((read_flags & IMAGE_PIXEL_FORMAT) != (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) png_set_strip_alpha(rd->png_ptr); break; default: @@ -290,7 +291,7 @@ libpng_read_data(struct image_io *io) struct image *img = rdi.image; byte *pixels = img->pixels; png_bytep rows[img->rows]; - for (uns r = 0; r < img->rows; r++, pixels += img->row_size) + for (uint r = 0; r < img->rows; r++, pixels += img->row_size) rows[r] = (png_bytep)pixels; png_read_image(rd->png_ptr, rows); png_read_end(rd->png_ptr, rd->end_ptr); @@ -361,14 +362,16 @@ libpng_write(struct image_io *io) png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); break; default: - ASSERT(0); + IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Libpng does not support this pixel format (0x%x)", img->flags & IMAGE_PIXEL_FORMAT); + png_destroy_write_struct(&png_ptr, &info_ptr); + return 0; } png_write_info(png_ptr, info_ptr); /* Write pixels */ byte *pixels = img->pixels; png_bytep rows[img->rows]; - for (uns r = 0; r < img->rows; r++, pixels += img->row_size) + for (uint r = 0; r < img->rows; r++, pixels += img->row_size) rows[r] = (png_bytep)pixels; png_write_image(png_ptr, rows); png_write_end(png_ptr, info_ptr);