]> mj.ucw.cz Git - libucw.git/blobdiff - images/image-tool.c
support for transparent GIFs, finally
[libucw.git] / images / image-tool.c
index 94fdd44dc0f987fe918adc6ec9e9a06a77cdac82..b52d59fda6cb4809887c2a237a39b5ffec56ca64 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)
@@ -24,13 +27,15 @@ Usage: image-tool [options] infile [outfile]\n\
 -f --input-format   input image format (jpeg, gif, png)\n\
 -F --output-format  output image format\n\
 -s --size           force output dimensions (100x200)\n\
--b --fit-box        scale to fit the box (100x200)\n\
--c --colorspace     force output colorspace (gray, grayalpha, rgb, rgbalpha)\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:" CF_SHORT_OPTS;
+static char *shortopts = "qf:F:s:b:c:Q:g:" CF_SHORT_OPTS;
 static struct option longopts[] =
 {
   CF_LONG_OPTS
@@ -38,8 +43,10 @@ static struct option longopts[] =
   { "input-format",    0, 0, 'f' },
   { "output-format",   0, 0, 'F' },
   { "size",            0, 0, 's' },
-  { "fit-box",         0, 0, 'b' },
+  { "fit-to-box",      0, 0, 'b' },
   { "colorspace",      0, 0, 'c' },
+  { "jpeg-quality",    0, 0, 'Q' },
+  { "background",      0, 0, 'g' },
   { NULL,              0, 0, 0 }
 };
                                                          
@@ -50,8 +57,10 @@ static byte *output_file_name;
 static enum image_format output_format;
 static uns cols;
 static uns rows;
-static uns fit_box;
+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)
 
@@ -82,7 +91,7 @@ main(int argc, char **argv)
            *r++ = 0;
            if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
              usage();
-           fit_box = 0;
+           fit_to_box = 0;
            break;
          }
        case 'b':
@@ -93,13 +102,29 @@ main(int argc, char **argv)
            *r++ = 0;
            if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
              usage();
-           fit_box = 1;
+           fit_to_box = 1;
            break;
          }
        case 'c':
          if (!(channels_format = image_name_to_channels_format(optarg)))
            usage();
          break;
+       case 'Q':
+         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();
       }
@@ -126,14 +151,21 @@ main(int argc, char **argv)
       bclose(io.fastbuf);
       printf("Format:      %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
       printf("Dimensions:  %dx%d\n", io.cols, io.rows);
-      printf("Colorspace:  %s\n", image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
+      printf("Colorspace:  %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
+      printf("NumColors:   %d\n", io.number_of_colors);
+      if (io.background_color.color_space)
+        {
+         byte rgb[3];
+         color_put_rgb(rgb, &io.background_color);
+          printf("Background:  %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]);
+       }
     }
   else
     {
       MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
-         image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
+         (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
       if (cols)
-        if (fit_box)
+        if (fit_to_box)
          {
             image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
          }
@@ -142,8 +174,14 @@ 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));
       bclose(io.fastbuf);
       MSG("Writing %s", output_file_name);