]> mj.ucw.cz Git - libucw.git/blob - images/image-tool.c
d9b20adc53ab35819b1f053962ee256aa5b41cf5
[libucw.git] / images / image-tool.c
1 /*
2  *      Simple image manupulation utility
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/getopt.h"
12 #include "lib/fastbuf.h"
13 #include "images/images.h"
14 #include <stdlib.h>
15 #include <fcntl.h>
16
17 static void NONRET
18 usage(void)
19 {
20   fputs("\
21 Usage: image-tool [options] infile [outfile]\n\
22 \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\
30 ", stderr);
31   exit(1);
32 }
33
34 static char *shortopts = "qf:F:s:b:c:Q:" CF_SHORT_OPTS;
35 static struct option longopts[] =
36 {
37   CF_LONG_OPTS
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' },
45   { NULL,               0, 0, 0 }
46 };
47                                                           
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;
53 static uns cols;
54 static uns rows;
55 static uns fit_to_box;
56 static uns channels_format;
57 static uns jpeg_quality;
58
59 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
60
61 int
62 main(int argc, char **argv)
63 {
64   log_init(argv[0]);
65   int opt;
66   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
67     switch (opt)
68       {
69         case 'q':
70           verbose = 0;
71           break;
72         case 'f':
73           if (!(input_format = image_extension_to_format(optarg)))
74             usage();
75           break;
76         case 'F':
77           if (!(output_format = image_extension_to_format(optarg)))
78             usage();
79           break;
80         case 's':
81           {
82             byte *r = strchr(optarg, 'x');
83             if (!r)
84               usage();
85             *r++ = 0;
86             if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
87               usage();
88             fit_to_box = 0;
89             break;
90           }
91         case 'b':
92           {
93             byte *r = strchr(optarg, 'x');
94             if (!r)
95               usage();
96             *r++ = 0;
97             if (!(cols = atoi(optarg)) || !(rows = atoi(r)))
98               usage();
99             fit_to_box = 1;
100             break;
101           }
102         case 'c':
103           if (!(channels_format = image_name_to_channels_format(optarg)))
104             usage();
105           break;
106         case 'Q':
107           if (!(jpeg_quality = atoi(optarg)))
108             usage();
109           break;
110         default:
111           usage();
112       }
113
114   if (argc != optind + 1 && argc != optind + 2)
115     usage();
116   input_file_name = argv[optind++];
117   if (argc > optind)
118     output_file_name = argv[optind];
119   
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;
123   struct image_io io;
124   image_thread_init(&it);
125   image_io_init(&it, &io);
126
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)
132     {
133       bclose(io.fastbuf);
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);
138     }
139   else
140     {
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));
143       if (cols)
144         if (fit_to_box)
145           {
146             image_dimensions_fit_to_box(&io.cols, &io.rows, MIN(cols, 0xffff), MIN(rows, 0xffff), 0);
147           }
148         else
149           {
150             io.cols = cols;
151             io.rows = rows;
152           }
153       if (channels_format)
154         io.flags = io.flags & ~IMAGE_PIXEL_FORMAT | channels_format;
155       if (jpeg_quality)
156         io.jpeg_quality = jpeg_quality;
157       TRY(image_io_read_data(&io, 0));
158       bclose(io.fastbuf);
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));
165       bclose(io.fastbuf);
166     }
167   
168   image_io_cleanup(&io);
169   image_thread_cleanup(&it);
170   MSG("Done.");
171   return 0;
172 }