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\
41 static char *shortopts = "qf:F:s:b:c:Q:g:G:a";
42 static struct option longopts[] =
44 { "quiet", 0, 0, 'q' },
45 { "input-format", 0, 0, 'f' },
46 { "output-format", 0, 0, 'F' },
47 { "size", 0, 0, 's' },
48 { "fit-to-box", 0, 0, 'b' },
49 { "colorspace", 0, 0, 'c' },
50 { "jpeg-quality", 0, 0, 'Q' },
51 { "background", 0, 0, 'g' },
52 { "default-background", 0, 0, 'G' },
53 { "remove-alpha", 0, 0, 'a' },
57 static uns verbose = 1;
58 static byte *input_file_name;
59 static enum image_format input_format;
60 static byte *output_file_name;
61 static enum image_format output_format;
64 static uns fit_to_box;
65 static uns channels_format;
66 static uns jpeg_quality;
67 static struct color background_color;
68 static struct color default_background_color;
69 static uns remove_alpha;
72 parse_color(struct color *color, byte *s)
78 long int v = strtol(s, &end, 16);
79 if (errno || *end || v < 0)
81 color_make_rgb(color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
84 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
87 main(int argc, char **argv)
91 default_background_color = color_white;
92 while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0)
99 if (!(input_format = image_extension_to_format(optarg)))
103 if (!(output_format = image_extension_to_format(optarg)))
108 byte *r = strchr(optarg, 'x');
112 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
119 byte *r = strchr(optarg, 'x');
123 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
129 if (!(channels_format = image_name_to_channels_format(optarg)))
133 if (!(jpeg_quality = atoi(optarg)))
137 parse_color(&background_color, optarg);
140 parse_color(&default_background_color, optarg);
149 if (argc != optind + 1 && argc != optind + 2)
151 input_file_name = argv[optind++];
153 output_file_name = argv[optind];
155 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
156 MSG("Initializing image library");
157 struct image_thread it;
159 image_thread_init(&it);
160 if (!image_io_init(&it, &io))
161 die("Cannot initialize image I/O (%s)", it.err_msg);
163 MSG("Reading %s", input_file_name);
164 io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
165 io.format = input_format ? : image_file_name_to_format(input_file_name);
166 TRY(image_io_read_header(&io));
167 if (!output_file_name)
170 printf("Format: %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
171 printf("Dimensions: %dx%d\n", io.cols, io.rows);
172 printf("Colorspace: %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
173 printf("NumColors: %d\n", io.number_of_colors);
174 if (io.background_color.color_space)
177 color_put_rgb(rgb, &io.background_color);
178 printf("Background: %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]);
183 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
184 (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
188 image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
195 if (background_color.color_space)
196 io.background_color = background_color;
197 else if (!io.background_color.color_space)
198 io.background_color = default_background_color;
200 io.flags &= ~IMAGE_ALPHA;
202 io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
203 if (!(io.flags & IMAGE_ALPHA))
204 io.flags |= IMAGE_IO_USE_BACKGROUND;
206 io.jpeg_quality = jpeg_quality;
207 TRY(image_io_read_data(&io, 0));
209 MSG("Writing %s", output_file_name);
210 io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
211 io.format = output_format ? : image_file_name_to_format(output_file_name);
212 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
213 image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
214 TRY(image_io_write(&io));
218 image_io_cleanup(&io);
219 image_thread_cleanup(&it);