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"
21 Usage: image-tool [options] infile [outfile]\n\
23 -q --quiet no progress messages\n\
24 -f --input-format input image format (jpeg, gif, png)\n\
25 -F --output-format output image format\n\
26 -s --size force output dimensions (100x200)\n\
27 -b --fit-box scale to fit the box (100x200)\n\
28 -c --colorspace force output colorspace (gray, grayalpha, rgb, rgbalpha)\n\
33 static char *shortopts = "qf:F:s:b:c:" CF_SHORT_OPTS;
34 static struct option longopts[] =
37 { "quiet", 0, 0, 'q' },
38 { "input-format", 0, 0, 'f' },
39 { "output-format", 0, 0, 'F' },
40 { "size", 0, 0, 's' },
41 { "fit-box", 0, 0, 'b' },
42 { "colorspace", 0, 0, 'c' },
46 static uns verbose = 1;
47 static byte *input_file_name;
48 static enum image_format input_format;
49 static byte *output_file_name;
50 static enum image_format output_format;
54 static uns channels_format;
56 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
59 main(int argc, char **argv)
63 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
70 if (!(input_format = image_extension_to_format(optarg)))
74 if (!(output_format = image_extension_to_format(optarg)))
79 byte *r = strchr(optarg, 'x');
83 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
90 byte *r = strchr(optarg, 'x');
94 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
100 if (!(channels_format = image_name_to_channels_format(optarg)))
107 if (argc != optind + 1 && argc != optind + 2)
109 input_file_name = argv[optind++];
111 output_file_name = argv[optind];
113 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
114 MSG("Initializing image library");
115 struct image_thread it;
117 image_thread_init(&it);
118 image_io_init(&it, &io);
120 MSG("Reading %s", input_file_name);
121 io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
122 io.format = input_format ? : image_file_name_to_format(input_file_name);
123 TRY(image_io_read_header(&io));
124 if (!output_file_name)
127 printf("Format: %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
128 printf("Dimensions: %dx%d\n", io.cols, io.rows);
129 printf("Colorspace: %s\n", image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
133 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
134 image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
138 image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
146 io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
147 TRY(image_io_read_data(&io, 0));
149 MSG("Writing %s", output_file_name);
150 io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
151 io.format = output_format ? : image_file_name_to_format(output_file_name);
152 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
153 image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
154 TRY(image_io_write(&io));
158 image_io_cleanup(&io);
159 image_thread_cleanup(&it);