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/getopt.h"
12 #include "lib/fastbuf.h"
13 #include "images/images.h"
14 #include "images/color.h"
24 Usage: image-tool [options] infile [outfile]\n\
26 -q --quiet no progress messages\n\
27 -f --input-format input image format (jpeg, gif, png)\n\
28 -F --output-format output image format\n\
29 -s --size force output dimensions (100x200)\n\
30 -b --fit-to-box scale to fit the box (100x200)\n\
31 -c --colorspace force output colorspace (Gray, GrayAlpha, RGB, RGBAlpha)\n\
32 -Q --jpeg-quality JPEG quality (1..100)\n\
33 -g --background background color (hexadecimal RRGGBB)\n\
38 static char *shortopts = "qf:F:s:b:c:Q:g:" CF_SHORT_OPTS;
39 static struct option longopts[] =
42 { "quiet", 0, 0, 'q' },
43 { "input-format", 0, 0, 'f' },
44 { "output-format", 0, 0, 'F' },
45 { "size", 0, 0, 's' },
46 { "fit-to-box", 0, 0, 'b' },
47 { "colorspace", 0, 0, 'c' },
48 { "jpeg-quality", 0, 0, 'Q' },
49 { "background", 0, 0, 'g' },
53 static uns verbose = 1;
54 static byte *input_file_name;
55 static enum image_format input_format;
56 static byte *output_file_name;
57 static enum image_format output_format;
60 static uns fit_to_box;
61 static uns channels_format;
62 static uns jpeg_quality;
63 static struct color background_color;
65 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
68 main(int argc, char **argv)
72 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
79 if (!(input_format = image_extension_to_format(optarg)))
83 if (!(output_format = image_extension_to_format(optarg)))
88 byte *r = strchr(optarg, 'x');
92 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
99 byte *r = strchr(optarg, 'x');
103 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
109 if (!(channels_format = image_name_to_channels_format(optarg)))
113 if (!(jpeg_quality = atoi(optarg)))
118 if (strlen(optarg) != 6)
122 long int v = strtol(optarg, &end, 16);
123 if (errno || *end || v < 0)
125 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
132 if (argc != optind + 1 && argc != optind + 2)
134 input_file_name = argv[optind++];
136 output_file_name = argv[optind];
138 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
139 MSG("Initializing image library");
140 struct image_thread it;
142 image_thread_init(&it);
143 image_io_init(&it, &io);
145 MSG("Reading %s", input_file_name);
146 io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
147 io.format = input_format ? : image_file_name_to_format(input_file_name);
148 TRY(image_io_read_header(&io));
149 if (!output_file_name)
152 printf("Format: %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
153 printf("Dimensions: %dx%d\n", io.cols, io.rows);
154 printf("Colorspace: %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
155 printf("NumColors: %d\n", io.number_of_colors);
159 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
160 (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
164 image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
171 if (background_color.color_space)
172 io.background_color = background_color;
174 io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
175 if (!(io.flags & IMAGE_ALPHA))
176 io.flags |= IMAGE_IO_USE_BACKGROUND;
178 io.jpeg_quality = jpeg_quality;
179 TRY(image_io_read_data(&io, 0));
181 MSG("Writing %s", output_file_name);
182 io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
183 io.format = output_format ? : image_file_name_to_format(output_file_name);
184 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
185 image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
186 TRY(image_io_write(&io));
190 image_io_cleanup(&io);
191 image_thread_cleanup(&it);