]> mj.ucw.cz Git - libucw.git/blob - images/image-tool.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git#v3.10
[libucw.git] / images / image-tool.c
1 /*
2  *      Image Library -- Simple image manipulation utility
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "images/images.h"
13 #include "images/color.h"
14
15 #include <getopt.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <stdio.h>
20
21 static void NONRET
22 usage(void)
23 {
24   fputs("\
25 Usage: image-tool [options] infile [outfile]\n\
26 \n\
27 -q --quiet               no progress messages\n\
28 -f --input-format        input image format (jpeg, gif, png)\n\
29 -F --output-format       output image format\n\
30 -s --size                force output dimensions (100x200)\n\
31 -b --fit-to-box          scale to fit the box (100x200)\n\
32 -c --colorspace          force output colorspace (Grayscale, Grayscale+Alpha, RGB, RGB+Alpha, ...)\n\
33 -Q --jpeg-quality        JPEG quality (1..100)\n\
34 -g --background          background color (hexadecimal RRGGBB)\n\
35 -G --default-background  background applied only if the image contains no background info (RRGGBB, default=FFFFFF)\n\
36 -a --remove-alpha        remove alpha channel\n\
37 -e --exif                reads Exif data\n"
38 , stderr);
39   exit(1);
40 }
41
42 static char *shortopts = "qf:F:s:b:c:Q:g:G:ae";
43 static struct option longopts[] =
44 {
45   { "quiet",                    0, 0, 'q' },
46   { "input-format",             0, 0, 'f' },
47   { "output-format",            0, 0, 'F' },
48   { "size",                     0, 0, 's' },
49   { "fit-to-box",               0, 0, 'b' },
50   { "colorspace",               0, 0, 'c' },
51   { "jpeg-quality",             0, 0, 'Q' },
52   { "background",               0, 0, 'g' },
53   { "default-background",       0, 0, 'G' },
54   { "remove-alpha",             0, 0, 'a' },
55   { "exif",                     0, 0, 'e' },
56   { NULL,                       0, 0, 0 }
57 };
58
59 static uns verbose = 1;
60 static byte *input_file_name;
61 static enum image_format input_format;
62 static byte *output_file_name;
63 static enum image_format output_format;
64 static uns cols;
65 static uns rows;
66 static uns fit_to_box;
67 static uns channels_format;
68 static uns jpeg_quality;
69 static struct color background_color;
70 static struct color default_background_color;
71 static uns remove_alpha;
72 static uns exif;
73
74 static void
75 parse_color(struct color *color, byte *s)
76 {
77   if (strlen(s) != 6)
78     usage();
79   errno = 0;
80   char *end;
81   long int v = strtol(s, &end, 16);
82   if (errno || *end || v < 0)
83     usage();
84   color_make_rgb(color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
85 }
86
87 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
88
89 int
90 main(int argc, char **argv)
91 {
92   log_init(argv[0]);
93   int opt;
94   default_background_color = color_white;
95   while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0)
96     switch (opt)
97       {
98         case 'q':
99           verbose = 0;
100           break;
101         case 'f':
102           if (!(input_format = image_extension_to_format(optarg)))
103             usage();
104           break;
105         case 'F':
106           if (!(output_format = image_extension_to_format(optarg)))
107             usage();
108           break;
109         case 's':
110           {
111             byte *r = strchr(optarg, 'x');
112             if (!r)
113               usage();
114             *r++ = 0;
115             if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
116               usage();
117             fit_to_box = 0;
118             break;
119           }
120         case 'b':
121           {
122             byte *r = strchr(optarg, 'x');
123             if (!r)
124               usage();
125             *r++ = 0;
126             if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
127               usage();
128             fit_to_box = 1;
129             break;
130           }
131         case 'c':
132           if (!(channels_format = image_name_to_channels_format(optarg)))
133             usage();
134           break;
135         case 'Q':
136           if (!(jpeg_quality = atoi(optarg)))
137             usage();
138           break;
139         case 'g':
140           parse_color(&background_color, optarg);
141           break;
142         case 'G':
143           parse_color(&default_background_color, optarg);
144           break;
145         case 'a':
146           remove_alpha++;
147           break;
148         case 'e':
149           exif++;
150           break;
151         default:
152           usage();
153       }
154
155   if (argc != optind + 1 && argc != optind + 2)
156     usage();
157   input_file_name = argv[optind++];
158   if (argc > optind)
159     output_file_name = argv[optind];
160
161 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
162   MSG("Initializing image library");
163   struct image_context ctx;
164   struct image_io io;
165   image_context_init(&ctx);
166   ctx.tracing_level = ~0U;
167   if (!image_io_init(&ctx, &io))
168     die("Cannot initialize image I/O");
169
170   MSG("Reading %s", input_file_name);
171   byte cs_buf[IMAGE_CHANNELS_FORMAT_MAX_SIZE];
172   io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
173   io.format = input_format ? : image_file_name_to_format(input_file_name);
174   if (exif)
175     io.flags |= IMAGE_IO_WANT_EXIF;
176   TRY(image_io_read_header(&io));
177   if (!output_file_name)
178     {
179       bclose(io.fastbuf);
180       printf("Format:      %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
181       printf("Dimensions:  %dx%d\n", io.cols, io.rows);
182       printf("Colorspace:  %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags, cs_buf));
183       printf("NumColors:   %u\n", io.number_of_colors);
184       if (io.background_color.color_space)
185         {
186           byte rgb[3];
187           TRY(color_put(&ctx, &io.background_color, rgb, COLOR_SPACE_RGB));
188           printf("Background:  %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]);
189         }
190       if (io.exif_size)
191         printf("ExifSize:    %u\n", io.exif_size);
192     }
193   else
194     {
195       MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
196           (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags, cs_buf));
197       if (cols)
198         if (fit_to_box)
199           {
200             image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
201           }
202         else
203           {
204             io.cols = cols;
205             io.rows = rows;
206           }
207       if (background_color.color_space)
208         io.background_color = background_color;
209       else if (!io.background_color.color_space)
210         io.background_color = default_background_color;
211       if (remove_alpha)
212         io.flags &= ~IMAGE_ALPHA;
213       if (channels_format)
214         io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
215       if (!(io.flags & IMAGE_ALPHA))
216         io.flags |= IMAGE_IO_USE_BACKGROUND;
217       if (jpeg_quality)
218         io.jpeg_quality = jpeg_quality;
219       uns output_fmt = output_format ? : image_file_name_to_format(output_file_name);
220       uns output_cs = io.flags & IMAGE_COLOR_SPACE;
221       if (output_fmt != IMAGE_FORMAT_JPEG &&
222           output_cs != COLOR_SPACE_GRAYSCALE &&
223           output_cs != COLOR_SPACE_RGB)
224         {
225           MSG("Forcing RGB color space");
226           io.flags = (io.flags & ~IMAGE_COLOR_SPACE) | COLOR_SPACE_RGB;
227         }
228       TRY(image_io_read_data(&io, 0));
229       bclose(io.fastbuf);
230       MSG("Writing %s", output_file_name);
231       io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
232       io.format = output_format ? : image_file_name_to_format(output_file_name);
233       MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
234           image_channels_format_to_name(io.flags, cs_buf));
235       TRY(image_io_write(&io));
236       bclose(io.fastbuf);
237     }
238
239   image_io_cleanup(&io);
240   image_context_cleanup(&ctx);
241   MSG("Done.");
242   return 0;
243 }