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-to-box scale to fit the box (100x200)\n\
28 -c --colorspace force output colorspace (Gray, GrayAlpha, RGB, RGBAlpha)\n\
29 -Q --jpeg-quality JPEG quality (1..100)\n\
34 static char *shortopts = "qf:F:s:b:c:Q:" CF_SHORT_OPTS;
35 static struct option longopts[] =
38 { "quiet", 0, 0, 'q' },
39 { "input-format", 0, 0, 'f' },
40 { "output-format", 0, 0, 'F' },
41 { "size", 0, 0, 's' },
42 { "fit-to-box", 0, 0, 'b' },
43 { "colorspace", 0, 0, 'c' },
44 { "jpeg-quality", 0, 0, 'Q' },
48 static uns verbose = 1;
49 static byte *input_file_name;
50 static enum image_format input_format;
51 static byte *output_file_name;
52 static enum image_format output_format;
55 static uns fit_to_box;
56 static uns channels_format;
57 static uns jpeg_quality;
59 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
62 main(int argc, char **argv)
66 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
73 if (!(input_format = image_extension_to_format(optarg)))
77 if (!(output_format = image_extension_to_format(optarg)))
82 byte *r = strchr(optarg, 'x');
86 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
93 byte *r = strchr(optarg, 'x');
97 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
103 if (!(channels_format = image_name_to_channels_format(optarg)))
107 if (!(jpeg_quality = atoi(optarg)))
114 if (argc != optind + 1 && argc != optind + 2)
116 input_file_name = argv[optind++];
118 output_file_name = argv[optind];
120 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
121 MSG("Initializing image library");
122 struct image_thread it;
124 image_thread_init(&it);
125 image_io_init(&it, &io);
127 MSG("Reading %s", input_file_name);
128 io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
129 io.format = input_format ? : image_file_name_to_format(input_file_name);
130 TRY(image_io_read_header(&io));
131 if (!output_file_name)
134 printf("Format: %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
135 printf("Dimensions: %dx%d\n", io.cols, io.rows);
136 printf("Colorspace: %s\n", io.has_palette ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
137 printf("NumColors: %d\n", io.number_of_colors);
141 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
142 io.has_palette ? (byte *)"Palette" : image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
146 image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
154 io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
156 io.jpeg_quality = jpeg_quality;
157 TRY(image_io_read_data(&io, 0));
159 MSG("Writing %s", output_file_name);
160 io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
161 io.format = output_format ? : image_file_name_to_format(output_file_name);
162 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
163 image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT));
164 TRY(image_io_write(&io));
168 image_io_cleanup(&io);
169 image_thread_cleanup(&it);