]> mj.ucw.cz Git - libucw.git/commitdiff
small bugfixes about transparency
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 31 Jul 2006 12:03:38 +0000 (14:03 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 31 Jul 2006 12:03:38 +0000 (14:03 +0200)
images/alpha.c
images/image-tool.c
images/images.h
images/io-libpng.c
images/io-libungif.c

index f3a85c354a39eff48c0307bf7b461a9a19040066..5c75e69f3cd1c4b7e313a5e4cac04884931c5664 100644 (file)
@@ -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];
index 7a778b29655caa06c8ae379646d9a56664480adb..6b9062c1eaadd14aa9b311d232635ab363dd7e0a 100644 (file)
 #include "lib/getopt.h"
 #include "lib/fastbuf.h"
 #include "images/images.h"
+#include "images/color.h"
 #include <stdlib.h>
 #include <fcntl.h>
+#include <errno.h>
+#include <stdio.h>
 
 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));
index d421dfc4790e8dc4403c7e5d024bd29130bebcf5..282489fd7a1ddb88378296fd06be4a4769311b19 100644 (file)
@@ -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);
index 1e1704f8cef4547fe1b28766c4c9e5c716838117..fe60fef60437ae765dee89a3821d38da5ca509ec 100644 (file)
@@ -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);
index a3bfc397eea43daf1c859e6dcbb676e765b300cc..ba2b250bc5f87e9c4bdf7b7cb034ac3e136af4df 100644 (file)
@@ -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);
     }