Debian Codegraph @

examples/img2webp.c

raw
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // generate an animated WebP out of a sequence of images
11 // (PNG, JPEG, ...)
12 //
13 // Example usage:
14 // img2webp -o out.webp -q 40 -mixed -duration 40 input??.png
15 //
16 // Author: [email protected] (Pascal Massimino)
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #ifdef HAVE_CONFIG_H
23 #include "webp/config.h"
24 #endif
25
26 #include "../examples/example_util.h"
27 #include "../imageio/image_dec.h"
28 #include "../imageio/imageio_util.h"
29 #include "./stopwatch.h"
30 #include "./unicode.h"
31 #include "sharpyuv/sharpyuv.h"
32 #include "webp/encode.h"
33 #include "webp/mux.h"
34
35 //------------------------------------------------------------------------------
36
37 static void Help(void) {
38 printf("Usage:\n\n");
39 printf(" img2webp [file_options] [[frame_options] frame_file]...");
40 printf(" [-o webp_file]\n\n");
41
42 printf("File-level options (only used at the start of compression):\n");
43 printf(" -min_size ............ minimize size\n");
44 printf(" -kmax <int> .......... maximum number of frame between key-frames\n"
45 " (0=only keyframes)\n");
46 printf(" -kmin <int> .......... minimum number of frame between key-frames\n"
47 " (0=disable key-frames altogether)\n");
48 printf(" -mixed ............... use mixed lossy/lossless automatic mode\n");
49 printf(" -near_lossless <int> . use near-lossless image preprocessing\n"
50 " (0..100=off), default=100\n");
51 printf(" -sharp_yuv ........... use sharper (and slower) RGB->YUV "
52 "conversion\n "
53 "(lossy only)\n");
54 printf(" -loop <int> .......... loop count (default: 0, = infinite loop)\n");
55 printf(" -v ................... verbose mode\n");
56 printf(" -h ................... this help\n");
57 printf(" -version ............. print version number and exit\n");
58 printf("\n");
59
60 printf("Per-frame options (only used for subsequent images input):\n");
61 printf(" -d <int> ............. frame duration in ms (default: 100)\n");
62 printf(" -lossless ............ use lossless mode (default)\n");
63 printf(" -lossy ............... use lossy mode\n");
64 printf(" -q <float> ........... quality\n");
65 printf(" -m <int> ............. compression method (0=fast, 6=slowest), "
66 "default=4\n");
67 printf(" -exact, -noexact ..... preserve or alter RGB values in transparent "
68 "area\n"
69 " (default: -noexact, may cause artifacts\n"
70 " with lossy animations)\n");
71
72 printf("\n");
73 printf("example: img2webp -loop 2 in0.png -lossy in1.jpg\n"
74 " -d 80 in2.tiff -o out.webp\n");
75 printf("\nNote: if a single file name is passed as the argument, the "
76 "arguments will be\n");
77 printf("tokenized from this file. The file name must not start with "
78 "the character '-'.\n");
79 printf("\nSupported input formats:\n %s\n",
80 WebPGetEnabledInputFileFormats());
81 }
82
83 //------------------------------------------------------------------------------
84
85 static int ReadImage(const char filename[], WebPPicture* const pic) {
86 const uint8_t* data = NULL;
87 size_t data_size = 0;
88 WebPImageReader reader;
89 int ok;
90 #ifdef HAVE_WINCODEC_H
91 // Try to decode the file using WIC falling back to the other readers for
92 // e.g., WebP.
93 ok = ReadPictureWithWIC(filename, pic, 1, NULL);
94 if (ok) return 1;
95 #endif
96 if (!ImgIoUtilReadFile(filename, &data, &data_size)) return 0;
97 reader = WebPGuessImageReader(data, data_size);
98 ok = reader(data, data_size, pic, 1, NULL);
99 WebPFree((void*)data);
100 return ok;
101 }
102
103 static int SetLoopCount(int loop_count, WebPData* const webp_data) {
104 int ok = 1;
105 WebPMuxError err;
106 uint32_t features;
107 WebPMuxAnimParams new_params;
108 WebPMux* const mux = WebPMuxCreate(webp_data, 1);
109 if (mux == NULL) return 0;
110
111 err = WebPMuxGetFeatures(mux, &features);
112 ok = (err == WEBP_MUX_OK);
113 if (!ok || !(features & ANIMATION_FLAG)) goto End;
114
115 err = WebPMuxGetAnimationParams(mux, &new_params);
116 ok = (err == WEBP_MUX_OK);
117 if (ok) {
118 new_params.loop_count = loop_count;
119 err = WebPMuxSetAnimationParams(mux, &new_params);
120 ok = (err == WEBP_MUX_OK);
121 }
122 if (ok) {
123 WebPDataClear(webp_data);
124 err = WebPMuxAssemble(mux, webp_data);
125 ok = (err == WEBP_MUX_OK);
126 }
127
128 End:
129 WebPMuxDelete(mux);
130 if (!ok) {
131 fprintf(stderr, "Error during loop-count setting\n");
132 }
133 return ok;
134 }
135
136 //------------------------------------------------------------------------------
137
138 // Returns EXIT_SUCCESS on success, EXIT_FAILURE on failure.
139 int main(int argc, const char* argv[]) {
140 const char* output = NULL;
141 WebPAnimEncoder* enc = NULL;
142 int verbose = 0;
143 int pic_num = 0;
144 int duration = 100;
145 int timestamp_ms = 0;
146 int loop_count = 0;
147 int width = 0, height = 0;
148 WebPAnimEncoderOptions anim_config;
149 WebPConfig config;
150 WebPPicture pic;
151 WebPData webp_data;
152 int c;
153 int have_input = 0;
154 int last_input_index = 0;
155 CommandLineArguments cmd_args;
156 int ok;
157
158 INIT_WARGV(argc, argv);
159
160 ok = ExUtilInitCommandLineArguments(argc - 1, argv + 1, &cmd_args);
161 if (!ok) FREE_WARGV_AND_RETURN(EXIT_FAILURE);
162
163 argc = cmd_args.argc_;
164 argv = cmd_args.argv_;
165
166 WebPDataInit(&webp_data);
167 if (!WebPAnimEncoderOptionsInit(&anim_config) ||
168 !WebPConfigInit(&config) ||
169 !WebPPictureInit(&pic)) {
170 fprintf(stderr, "Library version mismatch!\n");
171 ok = 0;
172 goto End;
173 }
174
175 // 1st pass of option parsing
176 for (c = 0; ok && c < argc; ++c) {
177 if (argv[c][0] == '-') {
178 int parse_error = 0;
179 if (!strcmp(argv[c], "-o") && c + 1 < argc) {
180 argv[c] = NULL;
181 output = (const char*)GET_WARGV_SHIFTED(argv, ++c);
182 } else if (!strcmp(argv[c], "-kmin") && c + 1 < argc) {
183 argv[c] = NULL;
184 anim_config.kmin = ExUtilGetInt(argv[++c], 0, &parse_error);
185 } else if (!strcmp(argv[c], "-kmax") && c + 1 < argc) {
186 argv[c] = NULL;
187 anim_config.kmax = ExUtilGetInt(argv[++c], 0, &parse_error);
188 } else if (!strcmp(argv[c], "-loop") && c + 1 < argc) {
189 argv[c] = NULL;
190 loop_count = ExUtilGetInt(argv[++c], 0, &parse_error);
191 if (loop_count < 0) {
192 fprintf(stderr, "Invalid non-positive loop-count (%d)\n", loop_count);
193 parse_error = 1;
194 }
195 } else if (!strcmp(argv[c], "-min_size")) {
196 anim_config.minimize_size = 1;
197 } else if (!strcmp(argv[c], "-mixed")) {
198 anim_config.allow_mixed = 1;
199 config.lossless = 0;
200 } else if (!strcmp(argv[c], "-near_lossless") && c + 1 < argc) {
201 argv[c] = NULL;
202 config.near_lossless = ExUtilGetInt(argv[++c], 0, &parse_error);
203 } else if (!strcmp(argv[c], "-sharp_yuv")) {
204 config.use_sharp_yuv = 1;
205 } else if (!strcmp(argv[c], "-v")) {
206 verbose = 1;
207 } else if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
208 Help();
209 FREE_WARGV_AND_RETURN(EXIT_SUCCESS);
210 } else if (!strcmp(argv[c], "-version")) {
211 const int enc_version = WebPGetEncoderVersion();
212 const int mux_version = WebPGetMuxVersion();
213 const int sharpyuv_version = SharpYuvGetVersion();
214 printf("WebP Encoder version: %d.%d.%d\nWebP Mux version: %d.%d.%d\n",
215 (enc_version >> 16) & 0xff, (enc_version >> 8) & 0xff,
216 enc_version & 0xff, (mux_version >> 16) & 0xff,
217 (mux_version >> 8) & 0xff, mux_version & 0xff);
218 printf("libsharpyuv: %d.%d.%d\n", (sharpyuv_version >> 24) & 0xff,
219 (sharpyuv_version >> 16) & 0xffff, sharpyuv_version & 0xff);
220 goto End;
221 } else {
222 continue;
223 }
224 ok = !parse_error;
225 if (!ok) goto End;
226 argv[c] = NULL; // mark option as 'parsed' during 1st pass
227 } else {
228 have_input |= 1;
229 }
230 }
231 if (!have_input) {
232 fprintf(stderr, "No input file(s) for generating animation!\n");
233 ok = 0;
234 Help();
235 goto End;
236 }
237
238 // image-reading pass
239 pic_num = 0;
240 config.lossless = 1;
241 for (c = 0; ok && c < argc; ++c) {
242 if (argv[c] == NULL) continue;
243 if (argv[c][0] == '-') { // parse local options
244 int parse_error = 0;
245 if (!strcmp(argv[c], "-lossy")) {
246 if (!anim_config.allow_mixed) config.lossless = 0;
247 } else if (!strcmp(argv[c], "-lossless")) {
248 if (!anim_config.allow_mixed) config.lossless = 1;
249 } else if (!strcmp(argv[c], "-q") && c + 1 < argc) {
250 config.quality = ExUtilGetFloat(argv[++c], &parse_error);
251 } else if (!strcmp(argv[c], "-m") && c + 1 < argc) {
252 config.method = ExUtilGetInt(argv[++c], 0, &parse_error);
253 } else if (!strcmp(argv[c], "-d") && c + 1 < argc) {
254 duration = ExUtilGetInt(argv[++c], 0, &parse_error);
255 if (duration <= 0) {
256 fprintf(stderr, "Invalid negative duration (%d)\n", duration);
257 parse_error = 1;
258 }
259 } else if (!strcmp(argv[c], "-exact")) {
260 config.exact = 1;
261 } else if (!strcmp(argv[c], "-noexact")) {
262 config.exact = 0;
263 } else {
264 parse_error = 1; // shouldn't be here.
265 fprintf(stderr, "Unknown option [%s]\n", argv[c]);
266 }
267 ok = !parse_error;
268 if (!ok) goto End;
269 continue;
270 }
271
272 if (ok) {
273 ok = WebPValidateConfig(&config);
274 if (!ok) {
275 fprintf(stderr, "Invalid configuration.\n");
276 goto End;
277 }
278 }
279
280 // read next input image
281 pic.use_argb = 1;
282 ok = ReadImage((const char*)GET_WARGV_SHIFTED(argv, c), &pic);
283 last_input_index = c;
284 if (!ok) goto End;
285
286 if (enc == NULL) {
287 width = pic.width;
288 height = pic.height;
289 enc = WebPAnimEncoderNew(width, height, &anim_config);
290 ok = (enc != NULL);
291 if (!ok) {
292 fprintf(stderr, "Could not create WebPAnimEncoder object.\n");
293 }
294 }
295
296 if (ok) {
297 ok = (width == pic.width && height == pic.height);
298 if (!ok) {
299 fprintf(stderr, "Frame #%d dimension mismatched! "
300 "Got %d x %d. Was expecting %d x %d.\n",
301 pic_num, pic.width, pic.height, width, height);
302 }
303 }
304
305 if (ok) {
306 ok = WebPAnimEncoderAdd(enc, &pic, timestamp_ms, &config);
307 if (!ok) {
308 fprintf(stderr, "Error while adding frame #%d\n", pic_num);
309 }
310 }
311 WebPPictureFree(&pic);
312 if (!ok) goto End;
313
314 if (verbose) {
315 WFPRINTF(stderr, "Added frame #%3d at time %4d (file: %s)\n",
316 pic_num, timestamp_ms, GET_WARGV_SHIFTED(argv, c));
317 }
318 timestamp_ms += duration;
319 ++pic_num;
320 }
321
322 for (c = last_input_index + 1; c < argc; ++c) {
323 if (argv[c] != NULL) {
324 fprintf(stderr, "Warning: unused option [%s]!"
325 " Frame options go before the input frame.\n", argv[c]);
326 }
327 }
328
329 // add a last fake frame to signal the last duration
330 ok = ok && WebPAnimEncoderAdd(enc, NULL, timestamp_ms, NULL);
331 ok = ok && WebPAnimEncoderAssemble(enc, &webp_data);
332 if (!ok) {
333 fprintf(stderr, "Error during final animation assembly.\n");
334 }
335
336 End:
337 // free resources
338 WebPAnimEncoderDelete(enc);
339
340 if (ok && loop_count > 0) { // Re-mux to add loop count.
341 ok = SetLoopCount(loop_count, &webp_data);
342 }
343
344 if (ok) {
345 if (output != NULL) {
346 ok = ImgIoUtilWriteFile(output, webp_data.bytes, webp_data.size);
347 if (ok) WFPRINTF(stderr, "output file: %s ", (const W_CHAR*)output);
348 } else {
349 fprintf(stderr, "[no output file specified] ");
350 }
351 }
352
353 if (ok) {
354 fprintf(stderr, "[%d frames, %u bytes].\n",
355 pic_num, (unsigned int)webp_data.size);
356 }
357 WebPDataClear(&webp_data);
358 ExUtilDeleteCommandLineArguments(&cmd_args);
359 FREE_WARGV_AND_RETURN(ok ? EXIT_SUCCESS : EXIT_FAILURE);
360 }