From 2e2bb3e8e00e6dbf043a9e531eacb02f61b4efc4 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Mon, 31 Jul 2006 14:03:38 +0200 Subject: [PATCH] small bugfixes about transparency --- images/alpha.c | 10 ++++++++-- images/image-tool.c | 24 +++++++++++++++++++++++- images/images.h | 3 +-- images/io-libpng.c | 13 ++++++------- images/io-libungif.c | 2 +- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/images/alpha.c b/images/alpha.c index f3a85c35..5c75e69f 100644 --- a/images/alpha.c +++ b/images/alpha.c @@ -29,7 +29,10 @@ image_apply_background(struct image_thread *thread UNUSED, struct image *dest, s { ASSERT(dest->pixel_size == 1); byte bg; - color_put_grayscale(&bg, background); + if (background->color_space) + color_put_grayscale(&bg, background); + else + bg = 0; uns a = 255 * bg, b = bg; # define IMAGE_WALK_PREFIX(x) walk_##x # define IMAGE_WALK_INLINE @@ -48,7 +51,10 @@ image_apply_background(struct image_thread *thread UNUSED, struct image *dest, s { ASSERT((src->flags & IMAGE_ALPHA) && dest->pixel_size >= 3 && !(dest->flags & IMAGE_ALPHA)); byte bg[3]; - color_put_rgb(bg, background); + if (background->color_space) + color_put_rgb(bg, background); + else + bg[0] = bg[1] = bg[2] = 0; uns a0 = 255 * bg[0], b0 = bg[0]; uns a1 = 255 * bg[1], b1 = bg[1]; uns a2 = 255 * bg[2], b2 = bg[2]; diff --git a/images/image-tool.c b/images/image-tool.c index 7a778b29..6b9062c1 100644 --- a/images/image-tool.c +++ b/images/image-tool.c @@ -11,8 +11,11 @@ #include "lib/getopt.h" #include "lib/fastbuf.h" #include "images/images.h" +#include "images/color.h" #include #include +#include +#include static void NONRET usage(void) @@ -27,11 +30,12 @@ Usage: image-tool [options] infile [outfile]\n\ -b --fit-to-box scale to fit the box (100x200)\n\ -c --colorspace force output colorspace (Gray, GrayAlpha, RGB, RGBAlpha)\n\ -Q --jpeg-quality JPEG quality (1..100)\n\ +-g --background background color (hexadecimal RRGGBB)\n\ ", stderr); exit(1); } -static char *shortopts = "qf:F:s:b:c:Q:" CF_SHORT_OPTS; +static char *shortopts = "qf:F:s:b:c:Q:g:" CF_SHORT_OPTS; static struct option longopts[] = { CF_LONG_OPTS @@ -42,6 +46,7 @@ static struct option longopts[] = { "fit-to-box", 0, 0, 'b' }, { "colorspace", 0, 0, 'c' }, { "jpeg-quality", 0, 0, 'Q' }, + { "background", 0, 0, 'g' }, { NULL, 0, 0, 0 } }; @@ -55,6 +60,7 @@ static uns rows; static uns fit_to_box; static uns channels_format; static uns jpeg_quality; +static struct color background_color; #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0) @@ -107,6 +113,18 @@ main(int argc, char **argv) if (!(jpeg_quality = atoi(optarg))) usage(); break; + case 'g': + { + if (strlen(optarg) != 6) + usage(); + errno = 0; + char *end; + long int v = strtol(optarg, &end, 16); + if (errno || *end || v < 0) + usage(); + color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255); + } + break; default: usage(); } @@ -150,8 +168,12 @@ main(int argc, char **argv) io.cols = cols; io.rows = rows; } + if (background_color.color_space) + io.background_color = background_color; if (channels_format) io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format; + if (!(io.flags & IMAGE_ALPHA)) + io.flags |= IMAGE_IO_USE_BACKGROUND; if (jpeg_quality) io.jpeg_quality = jpeg_quality; TRY(image_io_read_data(&io, 0)); diff --git a/images/images.h b/images/images.h index d421dfc4..282489fd 100644 --- a/images/images.h +++ b/images/images.h @@ -147,8 +147,7 @@ struct image_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_HAS_PALETTE = 0x10000, /* [ H ] - true for image with indexed colors */ - IMAGE_IO_HAS_BACKGROUND = 0x20000, /* [ H ] - picture contains background info */ - IMAGE_IO_USE_BACKGROUND = 0x40000, /* [ I ] - merge transparent pixels with background_color */ + IMAGE_IO_USE_BACKGROUND = 0x20000, /* [ I ] - merge transparent pixels with background_color */ }; void image_io_init(struct image_thread *it, struct image_io *io); diff --git a/images/io-libpng.c b/images/io-libpng.c index 1e1704f8..fe60fef6 100644 --- a/images/io-libpng.c +++ b/images/io-libpng.c @@ -217,6 +217,7 @@ libpng_read_data(struct image_io *io) switch (rd->color_type) { case PNG_COLOR_TYPE_PALETTE: + /* FIXME: add support for palette with colors */ if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE) { png_set_palette_to_rgb(rd->png_ptr); @@ -255,13 +256,11 @@ libpng_read_data(struct image_io *io) case PNG_COLOR_TYPE_RGB_ALPHA: if ((io->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)) - { - if (io->flags & IMAGE_IO_USE_BACKGROUND) - read_flags |= IMAGE_ALPHA; - else - png_set_strip_alpha(rd->png_ptr); - } + if (!(io->flags & IMAGE_ALPHA)) + if (io->flags & IMAGE_IO_USE_BACKGROUND) + read_flags |= IMAGE_ALPHA; + else if ((io->flags & IMAGE_PIXEL_FORMAT) != (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED)) + png_set_strip_alpha(rd->png_ptr); break; default: ASSERT(0); diff --git a/images/io-libungif.c b/images/io-libungif.c index a3bfc397..ba2b250b 100644 --- a/images/io-libungif.c +++ b/images/io-libungif.c @@ -90,7 +90,7 @@ libungif_read_header(struct image_io *io) #if 0 if (gif->SColorMap && !image->ImageDesc.ColorMap && (uns)gif->SBackGroundColor < (uns)color_map->ColorCount) { - io->flags |= IMAGE_ALPHA | IMAGE_IO_HAS_BACKGROUND; + io->flags |= IMAGE_ALPHA; GifColorType *background = color_map->Colors + gif->SBackGroundColor; color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue); } -- 2.39.2