2 * Simple image manupulation utility
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
11 #include "lib/fastbuf.h"
12 #include "images/images.h"
13 #include "images/color.h"
25 Usage: image-tool [options] infile [outfile]\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"
44 static char *shortopts = "qf:F:s:b:c:Q:g:G:ae";
45 static struct option longopts[] =
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' },
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;
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
81 parse_color(struct color *color, byte *s)
87 long int v = strtol(s, &end, 16);
88 if (errno || *end || v < 0)
90 color_make_rgb(color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
93 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
96 main(int argc, char **argv)
100 default_background_color = color_white;
101 while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0)
108 if (!(input_format = image_extension_to_format(optarg)))
112 if (!(output_format = image_extension_to_format(optarg)))
117 byte *r = strchr(optarg, 'x');
121 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
128 byte *r = strchr(optarg, 'x');
132 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
138 if (!(channels_format = image_name_to_channels_format(optarg)))
142 if (!(jpeg_quality = atoi(optarg)))
146 parse_color(&background_color, optarg);
149 parse_color(&default_background_color, optarg);
154 #ifdef CONFIG_IMAGES_EXIF
163 if (argc != optind + 1 && argc != optind + 2)
165 input_file_name = argv[optind++];
167 output_file_name = argv[optind];
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;
173 image_thread_init(&it);
174 if (!image_io_init(&it, &io))
175 die("Cannot initialize image I/O (%s)", it.err_msg);
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
182 io.flags |= IMAGE_IO_WANT_EXIF;
184 TRY(image_io_read_header(&io));
185 if (!output_file_name)
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)
195 color_put_rgb(rgb, &io.background_color);
196 printf("Background: %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]);
198 #ifdef CONFIG_IMAGES_EXIF
200 printf("ExifSize: %u\n", io.exif_size);
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));
210 image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
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;
222 io.flags &= ~IMAGE_ALPHA;
224 io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
225 if (!(io.flags & IMAGE_ALPHA))
226 io.flags |= IMAGE_IO_USE_BACKGROUND;
228 io.jpeg_quality = jpeg_quality;
229 TRY(image_io_read_data(&io, 0));
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));
240 image_io_cleanup(&io);
241 image_thread_cleanup(&it);