2 * Image Library -- Simple image manipulation 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 <ucw/fastbuf.h>
12 #include <images/images.h>
13 #include <images/color.h>
25 Usage: ucw-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 (Grayscale, Grayscale+Alpha, RGB, RGB+Alpha, ...)\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 -e --exif reads Exif data\n"
42 static char *shortopts = "qf:F:s:b:c:Q:g:G:ae";
43 static struct option longopts[] =
45 { "quiet", 0, 0, 'q' },
46 { "input-format", 0, 0, 'f' },
47 { "output-format", 0, 0, 'F' },
48 { "size", 0, 0, 's' },
49 { "fit-to-box", 0, 0, 'b' },
50 { "colorspace", 0, 0, 'c' },
51 { "jpeg-quality", 0, 0, 'Q' },
52 { "background", 0, 0, 'g' },
53 { "default-background", 0, 0, 'G' },
54 { "remove-alpha", 0, 0, 'a' },
55 { "exif", 0, 0, 'e' },
59 static uint verbose = 1;
60 static byte *input_file_name;
61 static enum image_format input_format;
62 static byte *output_file_name;
63 static enum image_format output_format;
66 static uint fit_to_box;
67 static uint channels_format;
68 static uint jpeg_quality;
69 static struct color background_color;
70 static struct color default_background_color;
71 static uint remove_alpha;
75 parse_color(struct color *color, byte *s)
81 long int v = strtol(s, &end, 16);
82 if (errno || *end || v < 0)
84 color_make_rgb(color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
87 #define MSG(x...) do{ if (verbose) msg(L_INFO, ##x); }while(0)
90 main(int argc, char **argv)
94 default_background_color = color_white;
95 while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0)
102 if (!(input_format = image_extension_to_format(optarg)))
106 if (!(output_format = image_extension_to_format(optarg)))
111 byte *r = strchr(optarg, 'x');
115 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
122 byte *r = strchr(optarg, 'x');
126 if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
132 if (!(channels_format = image_name_to_channels_format(optarg)))
136 if (!(jpeg_quality = atoi(optarg)))
140 parse_color(&background_color, optarg);
143 parse_color(&default_background_color, optarg);
155 if (argc != optind + 1 && argc != optind + 2)
157 input_file_name = argv[optind++];
159 output_file_name = argv[optind];
161 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
162 MSG("Initializing image library");
163 struct image_context ctx;
165 image_context_init(&ctx);
166 ctx.tracing_level = ~0U;
167 if (!image_io_init(&ctx, &io))
168 die("Cannot initialize image I/O");
170 MSG("Reading %s", input_file_name);
171 byte cs_buf[IMAGE_CHANNELS_FORMAT_MAX_SIZE];
172 io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18);
173 io.format = input_format ? : image_file_name_to_format(input_file_name);
175 io.flags |= IMAGE_IO_WANT_EXIF;
176 TRY(image_io_read_header(&io));
177 if (!output_file_name)
180 printf("Format: %s\n", image_format_to_extension(io.format) ? : (byte *)"?");
181 printf("Dimensions: %dx%d\n", io.cols, io.rows);
182 printf("Colorspace: %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags, cs_buf));
183 printf("NumColors: %u\n", io.number_of_colors);
184 if (io.background_color.color_space)
187 TRY(color_put(&ctx, &io.background_color, rgb, COLOR_SPACE_RGB));
188 printf("Background: %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]);
191 printf("ExifSize: %u\n", io.exif_size);
195 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
196 (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags, cs_buf));
200 image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
207 if (background_color.color_space)
208 io.background_color = background_color;
209 else if (!io.background_color.color_space)
210 io.background_color = default_background_color;
212 io.flags &= ~IMAGE_ALPHA;
214 io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
215 if (!(io.flags & IMAGE_ALPHA))
216 io.flags |= IMAGE_IO_USE_BACKGROUND;
218 io.jpeg_quality = jpeg_quality;
219 uint output_fmt = output_format ? : image_file_name_to_format(output_file_name);
220 uint output_cs = io.flags & IMAGE_COLOR_SPACE;
221 if (output_fmt != IMAGE_FORMAT_JPEG &&
222 output_cs != COLOR_SPACE_GRAYSCALE &&
223 output_cs != COLOR_SPACE_RGB)
225 MSG("Forcing RGB color space");
226 io.flags = (io.flags & ~IMAGE_COLOR_SPACE) | COLOR_SPACE_RGB;
228 TRY(image_io_read_data(&io, 0));
230 MSG("Writing %s", output_file_name);
231 io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18);
232 io.format = output_format ? : image_file_name_to_format(output_file_name);
233 MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows,
234 image_channels_format_to_name(io.flags, cs_buf));
235 TRY(image_io_write(&io));
239 image_io_cleanup(&io);
240 image_context_cleanup(&ctx);