]> mj.ucw.cz Git - libucw.git/blob - images/image-tool.c
fixed meaning of "image-tool -e" switch
[libucw.git] / images / image-tool.c
1 /*
2  *      Simple image manupulation 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 (Gray, GrayAlpha, RGB, RGBAlpha)\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 #ifdef CONFIG_IMAGES_EXIF
38 "-e --exif                reads Exif data\n"
39 #endif
40 , stderr);
41   exit(1);
42 }
43
44 static char *shortopts = "qf:F:s:b:c:Q:g:G:ae";
45 static struct option longopts[] =
46 {
47   { "quiet",                    0, 0, 'q' },
48   { "input-format",             0, 0, 'f' },
49   { "output-format",            0, 0, 'F' },
50   { "size",                     0, 0, 's' },
51   { "fit-to-box",               0, 0, 'b' },
52   { "colorspace",               0, 0, 'c' },
53   { "jpeg-quality",             0, 0, 'Q' },
54   { "background",               0, 0, 'g' },
55   { "default-background",       0, 0, 'G' },
56   { "remove-alpha",             0, 0, 'a' },
57 #ifdef CONFIG_IMAGES_EXIF
58   { "exif",                     0, 0, 'e' },
59 #endif
60   { NULL,                       0, 0, 0 }
61 };
62
63 static uns verbose = 1;
64 static byte *input_file_name;
65 static enum image_format input_format;
66 static byte *output_file_name;
67 static enum image_format output_format;
68 static uns cols;
69 static uns rows;
70 static uns fit_to_box;
71 static uns channels_format;
72 static uns jpeg_quality;
73 static struct color background_color;
74 static struct color default_background_color;
75 static uns remove_alpha;
76 #ifdef CONFIG_IMAGES_EXIF
77 static uns exif;
78 #endif
79
80 static void
81 parse_color(struct color *color, byte *s)
82 {
83   if (strlen(s) != 6)
84     usage();
85   errno = 0;
86   char *end;
87   long int v = strtol(s, &end, 16);
88   if (errno || *end || v < 0)
89     usage();
90   color_make_rgb(color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
91 }
92
93 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
94
95 int
96 main(int argc, char **argv)
97 {
98   log_init(argv[0]);
99   int opt;
100   default_background_color = color_white;
101   while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0)
102     switch (opt)
103       {
104         case 'q':
105           verbose = 0;
106           break;
107         case 'f':
108           if (!(input_format = image_extension_to_format(optarg)))
109             usage();
110           break;
111         case 'F':
112           if (!(output_format = image_extension_to_format(optarg)))
113             usage();
114           break;
115         case 's':
116           {
117             byte *r = strchr(optarg, 'x');
118             if (!r)
119               usage();
120             *r++ = 0;
121             if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
122               usage();
123             fit_to_box = 0;
124             break;
125           }
126         case 'b':
127           {
128             byte *r = strchr(optarg, 'x');
129             if (!r)
130               usage();
131             *r++ = 0;
132             if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
133               usage();
134             fit_to_box = 1;
135             break;
136           }
137         case 'c':
138           if (!(channels_format = image_name_to_channels_format(optarg)))
139             usage();
140           break;
141         case 'Q':
142           if (!(jpeg_quality = atoi(optarg)))
143             usage();
144           break;
145         case 'g':
146           parse_color(&background_color, optarg);
147           break;
148         case 'G':
149           parse_color(&default_background_color, optarg);
150           break;
151         case 'a':
152           remove_alpha++;
153           break;
154 #ifdef CONFIG_IMAGES_EXIF
155         case 'e':
156           exif++;
157           break;
158 #endif
159         default:
160           usage();
161       }
162
163   if (argc != optind + 1 && argc != optind + 2)
164     usage();
165   input_file_name = argv[optind++];
166   if (argc > optind)
167     output_file_name = argv[optind];
168
169 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
170   MSG("Initializing image library");
171   struct image_thread it;
172   struct image_io io;
173   image_thread_init(&it);
174   if (!image_io_init(&it, &io))
175     die("Cannot initialize image I/O (%s)", it.err_msg);
176
177   MSG("Reading %s", input_file_name);
178   io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
179   io.format = input_format ? : image_file_name_to_format(input_file_name);
180 #ifdef CONFIG_IMAGES_EXIF
181   if (exif)
182     io.flags |= IMAGE_IO_WANT_EXIF;
183 #endif
184   TRY(image_io_read_header(&io));
185   if (!output_file_name)
186     {
187       bclose(io.fastbuf);
188       printf("Format:      %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
189       printf("Dimensions:  %dx%d\n", io.cols, io.rows);
190       printf("Colorspace:  %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
191       printf("NumColors:   %d\n", io.number_of_colors);
192       if (io.background_color.color_space)
193         {
194           byte rgb[3];
195           color_put_rgb(rgb, &io.background_color);
196           printf("Background:  %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]);
197         }
198 #ifdef CONFIG_IMAGES_EXIF
199       if (io.exif_size)
200         printf("ExifSize:    %u\n", io.exif_size);
201 #endif
202     }
203   else
204     {
205       MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
206           (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
207       if (cols)
208         if (fit_to_box)
209           {
210             image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
211           }
212         else
213           {
214             io.cols = cols;
215             io.rows = rows;
216           }
217       if (background_color.color_space)
218         io.background_color = background_color;
219       else if (!io.background_color.color_space)
220         io.background_color = default_background_color;
221       if (remove_alpha)
222         io.flags &= ~IMAGE_ALPHA;
223       if (channels_format)
224         io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
225       if (!(io.flags & IMAGE_ALPHA))
226         io.flags |= IMAGE_IO_USE_BACKGROUND;
227       if (jpeg_quality)
228         io.jpeg_quality = jpeg_quality;
229       TRY(image_io_read_data(&io, 0));
230       bclose(io.fastbuf);
231       MSG("Writing %s", output_file_name);
232       io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
233       io.format = output_format ? : image_file_name_to_format(output_file_name);
234       MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
235           image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
236       TRY(image_io_write(&io));
237       bclose(io.fastbuf);
238     }
239
240   image_io_cleanup(&io);
241   image_thread_cleanup(&it);
242   MSG("Done.");
243   return 0;
244 }