X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=images%2Fimage-tool.c;h=3f24d0294ddfff4a3121ee03f3fe6b64c7e40894;hb=be141e60a70c2a080263070cb3458d24821ff908;hp=94fdd44dc0f987fe918adc6ec9e9a06a77cdac82;hpb=966305189fb3c543170117da57f110a8e9851ac2;p=libucw.git diff --git a/images/image-tool.c b/images/image-tool.c index 94fdd44d..3f24d029 100644 --- a/images/image-tool.c +++ b/images/image-tool.c @@ -1,5 +1,5 @@ /* - * Simple image manupulation utility + * Image Library -- Simple image manipulation utility * * (c) 2006 Pavel Charvat * @@ -7,12 +7,16 @@ * of the GNU General Public License. */ -#include "lib/lib.h" -#include "lib/getopt.h" -#include "lib/fastbuf.h" -#include "images/images.h" +#include +#include +#include +#include + +#include #include #include +#include +#include static void NONRET usage(void) @@ -20,29 +24,38 @@ usage(void) fputs("\ Usage: image-tool [options] infile [outfile]\n\ \n\ --q --quiet no progress messages\n\ --f --input-format input image format (jpeg, gif, png)\n\ --F --output-format output image format\n\ --s --size force output dimensions (100x200)\n\ --b --fit-box scale to fit the box (100x200)\n\ --c --colorspace force output colorspace (gray, grayalpha, rgb, rgbalpha)\n\ -", stderr); +-q --quiet no progress messages\n\ +-f --input-format input image format (jpeg, gif, png)\n\ +-F --output-format output image format\n\ +-s --size force output dimensions (100x200)\n\ +-b --fit-to-box scale to fit the box (100x200)\n\ +-c --colorspace force output colorspace (Grayscale, Grayscale+Alpha, RGB, RGB+Alpha, ...)\n\ +-Q --jpeg-quality JPEG quality (1..100)\n\ +-g --background background color (hexadecimal RRGGBB)\n\ +-G --default-background background applied only if the image contains no background info (RRGGBB, default=FFFFFF)\n\ +-a --remove-alpha remove alpha channel\n\ +-e --exif reads Exif data\n" +, stderr); exit(1); } -static char *shortopts = "qf:F:s:b:c:" CF_SHORT_OPTS; +static char *shortopts = "qf:F:s:b:c:Q:g:G:ae"; static struct option longopts[] = { - CF_LONG_OPTS - { "quiet", 0, 0, 'q' }, - { "input-format", 0, 0, 'f' }, - { "output-format", 0, 0, 'F' }, - { "size", 0, 0, 's' }, - { "fit-box", 0, 0, 'b' }, - { "colorspace", 0, 0, 'c' }, - { NULL, 0, 0, 0 } + { "quiet", 0, 0, 'q' }, + { "input-format", 0, 0, 'f' }, + { "output-format", 0, 0, 'F' }, + { "size", 0, 0, 's' }, + { "fit-to-box", 0, 0, 'b' }, + { "colorspace", 0, 0, 'c' }, + { "jpeg-quality", 0, 0, 'Q' }, + { "background", 0, 0, 'g' }, + { "default-background", 0, 0, 'G' }, + { "remove-alpha", 0, 0, 'a' }, + { "exif", 0, 0, 'e' }, + { NULL, 0, 0, 0 } }; - + static uns verbose = 1; static byte *input_file_name; static enum image_format input_format; @@ -50,17 +63,36 @@ static byte *output_file_name; static enum image_format output_format; static uns cols; static uns rows; -static uns fit_box; +static uns fit_to_box; static uns channels_format; +static uns jpeg_quality; +static struct color background_color; +static struct color default_background_color; +static uns remove_alpha; +static uns exif; + +static void +parse_color(struct color *color, byte *s) +{ + if (strlen(s) != 6) + usage(); + errno = 0; + char *end; + long int v = strtol(s, &end, 16); + if (errno || *end || v < 0) + usage(); + color_make_rgb(color, (v >> 16) & 255, (v >> 8) & 255, v & 255); +} -#define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0) +#define MSG(x...) do{ if (verbose) msg(L_INFO, ##x); }while(0) int main(int argc, char **argv) { log_init(argv[0]); int opt; - while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0) + default_background_color = color_white; + while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0) switch (opt) { case 'q': @@ -82,7 +114,7 @@ main(int argc, char **argv) *r++ = 0; if (!(cols = atoi(optarg)) || !(rows = atoi(r))) usage(); - fit_box = 0; + fit_to_box = 0; break; } case 'b': @@ -93,13 +125,29 @@ main(int argc, char **argv) *r++ = 0; if (!(cols = atoi(optarg)) || !(rows = atoi(r))) usage(); - fit_box = 1; + fit_to_box = 1; break; } case 'c': if (!(channels_format = image_name_to_channels_format(optarg))) usage(); break; + case 'Q': + if (!(jpeg_quality = atoi(optarg))) + usage(); + break; + case 'g': + parse_color(&background_color, optarg); + break; + case 'G': + parse_color(&default_background_color, optarg); + break; + case 'a': + remove_alpha++; + break; + case 'e': + exif++; + break; default: usage(); } @@ -109,31 +157,45 @@ main(int argc, char **argv) input_file_name = argv[optind++]; if (argc > optind) output_file_name = argv[optind]; - -#define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0) + +#define TRY(x) do{ if (!(x)) exit(1); }while(0) MSG("Initializing image library"); - struct image_thread it; + struct image_context ctx; struct image_io io; - image_thread_init(&it); - image_io_init(&it, &io); + image_context_init(&ctx); + ctx.tracing_level = ~0U; + if (!image_io_init(&ctx, &io)) + die("Cannot initialize image I/O"); MSG("Reading %s", input_file_name); + byte cs_buf[IMAGE_CHANNELS_FORMAT_MAX_SIZE]; io.fastbuf = bopen(input_file_name, O_RDONLY, 1 << 18); io.format = input_format ? : image_file_name_to_format(input_file_name); + if (exif) + io.flags |= IMAGE_IO_WANT_EXIF; TRY(image_io_read_header(&io)); if (!output_file_name) { bclose(io.fastbuf); printf("Format: %s\n", image_format_to_extension(io.format) ? : (byte *)"?"); printf("Dimensions: %dx%d\n", io.cols, io.rows); - printf("Colorspace: %s\n", image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT)); + printf("Colorspace: %s\n", (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags, cs_buf)); + printf("NumColors: %u\n", io.number_of_colors); + if (io.background_color.color_space) + { + byte rgb[3]; + TRY(color_put(&ctx, &io.background_color, rgb, COLOR_SPACE_RGB)); + printf("Background: %02x%02x%02x\n", rgb[0], rgb[1], rgb[2]); + } + if (io.exif_size) + printf("ExifSize: %u\n", io.exif_size); } else { MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows, - image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT)); + (io.flags & IMAGE_IO_HAS_PALETTE) ? (byte *)"Palette" : image_channels_format_to_name(io.flags, cs_buf)); if (cols) - if (fit_box) + if (fit_to_box) { image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0); } @@ -142,21 +204,40 @@ main(int argc, char **argv) io.cols = cols; io.rows = rows; } + if (background_color.color_space) + io.background_color = background_color; + else if (!io.background_color.color_space) + io.background_color = default_background_color; + if (remove_alpha) + io.flags &= ~IMAGE_ALPHA; if (channels_format) io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format; + if (!(io.flags & IMAGE_ALPHA)) + io.flags |= IMAGE_IO_USE_BACKGROUND; + if (jpeg_quality) + io.jpeg_quality = jpeg_quality; + uns output_fmt = output_format ? : image_file_name_to_format(output_file_name); + uns output_cs = io.flags & IMAGE_COLOR_SPACE; + if (output_fmt != IMAGE_FORMAT_JPEG && + output_cs != COLOR_SPACE_GRAYSCALE && + output_cs != COLOR_SPACE_RGB) + { + MSG("Forcing RGB color space"); + io.flags = (io.flags & ~IMAGE_COLOR_SPACE) | COLOR_SPACE_RGB; + } TRY(image_io_read_data(&io, 0)); bclose(io.fastbuf); MSG("Writing %s", output_file_name); io.fastbuf = bopen(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 1 << 18); io.format = output_format ? : image_file_name_to_format(output_file_name); MSG("%s %dx%d %s", image_format_to_extension(io.format) ? : (byte *)"?", io.cols, io.rows, - image_channels_format_to_name(io.flags & IMAGE_CHANNELS_FORMAT)); + image_channels_format_to_name(io.flags, cs_buf)); TRY(image_io_write(&io)); bclose(io.fastbuf); } - + image_io_cleanup(&io); - image_thread_cleanup(&it); + image_context_cleanup(&ctx); MSG("Done."); return 0; }