1 // Copyright 2010 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 // Command-line tool for decoding a WebP image.
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13
14 #include <assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #ifdef HAVE_CONFIG_H
20 #include "webp/config.h"
21 #endif
22
23 #include "../examples/example_util.h"
24 #include "../imageio/image_enc.h"
25 #include "../imageio/webpdec.h"
26 #include "./stopwatch.h"
27 #include "./unicode.h"
28
29 static int verbose = 0;
30 static int quiet = 0;
31 #ifndef WEBP_DLL
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 extern void* VP8GetCPUInfo; // opaque forward declaration.
37
38 #ifdef __cplusplus
39 } // extern "C"
40 #endif
41 #endif // WEBP_DLL
42
43
SaveOutput(const WebPDecBuffer * const buffer,WebPOutputFileFormat format,const char * const out_file)44 static int SaveOutput(const WebPDecBuffer* const buffer,
45 WebPOutputFileFormat format, const char* const out_file) {
46 const int use_stdout = (out_file != NULL) && !WSTRCMP(out_file, "-");
47 int ok = 1;
48 Stopwatch stop_watch;
49
50 if (verbose) {
51 StopwatchReset(&stop_watch);
52 }
53 ok = WebPSaveImage(buffer, format, out_file);
54
55 if (ok) {
56 if (!quiet) {
57 if (use_stdout) {
58 fprintf(stderr, "Saved to stdout\n");
59 } else {
60 WFPRINTF(stderr, "Saved file %s\n", (const W_CHAR*)out_file);
61 }
62 }
63 if (verbose) {
64 const double write_time = StopwatchReadAndReset(&stop_watch);
65 fprintf(stderr, "Time to write output: %.3fs\n", write_time);
66 }
67 } else {
68 if (use_stdout) {
69 fprintf(stderr, "Error writing to stdout !!\n");
70 } else {
71 WFPRINTF(stderr, "Error writing file %s !!\n", (const W_CHAR*)out_file);
72 }
73 }
74 return ok;
75 }
76
Help(void)77 static void Help(void) {
78 printf("Usage: dwebp in_file [options] [-o out_file]\n\n"
79 "Decodes the WebP image file to PNG format [Default].\n"
80 "Note: Animated WebP files are not supported.\n\n"
81 "Use following options to convert into alternate image formats:\n"
82 " -pam ......... save the raw RGBA samples as a color PAM\n"
83 " -ppm ......... save the raw RGB samples as a color PPM\n"
84 " -bmp ......... save as uncompressed BMP format\n"
85 " -tiff ........ save as uncompressed TIFF format\n"
86 " -pgm ......... save the raw YUV samples as a grayscale PGM\n"
87 " file with IMC4 layout\n"
88 " -yuv ......... save the raw YUV samples in flat layout\n"
89 "\n"
90 " Other options are:\n"
91 " -version ..... print version number and exit\n"
92 " -nofancy ..... don't use the fancy YUV420 upscaler\n"
93 " -nofilter .... disable in-loop filtering\n"
94 " -nodither .... disable dithering\n"
95 " -dither <d> .. dithering strength (in 0..100)\n"
96 " -alpha_dither use alpha-plane dithering if needed\n"
97 " -mt .......... use multi-threading\n"
98 " -crop <x> <y> <w> <h> ... crop output with the given rectangle\n"
99 " -resize <w> <h> ......... resize output (*after* any cropping)\n"
100 " -flip ........ flip the output vertically\n"
101 " -alpha ....... only save the alpha plane\n"
102 " -incremental . use incremental decoding (useful for tests)\n"
103 " -h ........... this help message\n"
104 " -v ........... verbose (e.g. print encoding/decoding times)\n"
105 " -quiet ....... quiet mode, don't print anything\n"
106 #ifndef WEBP_DLL
107 " -noasm ....... disable all assembly optimizations\n"
108 #endif
109 );
110 }
111
112 static const char* const kFormatType[] = {
113 "unspecified", "lossy", "lossless"
114 };
115
AllocateExternalBuffer(WebPDecoderConfig * config,WebPOutputFileFormat format,int use_external_memory)116 static uint8_t* AllocateExternalBuffer(WebPDecoderConfig* config,
117 WebPOutputFileFormat format,
118 int use_external_memory) {
119 uint8_t* external_buffer = NULL;
120 WebPDecBuffer* const output_buffer = &config->output;
121 int w = config->input.width;
122 int h = config->input.height;
123 if (config->options.use_scaling) {
124 w = config->options.scaled_width;
125 h = config->options.scaled_height;
126 } else if (config->options.use_cropping) {
127 w = config->options.crop_width;
128 h = config->options.crop_height;
129 }
130 if (format >= RGB && format <= rgbA_4444) {
131 const int bpp = (format == RGB || format == BGR) ? 3
132 : (format == RGBA_4444 || format == rgbA_4444 ||
133 format == RGB_565) ? 2
134 : 4;
135 uint32_t stride = bpp * w + 7; // <- just for exercising
136 external_buffer = (uint8_t*)WebPMalloc(stride * h);
137 if (external_buffer == NULL) return NULL;
138 output_buffer->u.RGBA.stride = stride;
139 output_buffer->u.RGBA.size = stride * h;
140 output_buffer->u.RGBA.rgba = external_buffer;
141 } else { // YUV and YUVA
142 const int has_alpha = WebPIsAlphaMode(output_buffer->colorspace);
143 uint8_t* tmp;
144 uint32_t stride = w + 3;
145 uint32_t uv_stride = (w + 1) / 2 + 13;
146 uint32_t total_size = stride * h * (has_alpha ? 2 : 1)
147 + 2 * uv_stride * (h + 1) / 2;
148 assert(format >= YUV && format <= YUVA);
149 external_buffer = (uint8_t*)WebPMalloc(total_size);
150 if (external_buffer == NULL) return NULL;
151 tmp = external_buffer;
152 output_buffer->u.YUVA.y = tmp;
153 output_buffer->u.YUVA.y_stride = stride;
154 output_buffer->u.YUVA.y_size = stride * h;
155 tmp += output_buffer->u.YUVA.y_size;
156 if (has_alpha) {
157 output_buffer->u.YUVA.a = tmp;
158 output_buffer->u.YUVA.a_stride = stride;
159 output_buffer->u.YUVA.a_size = stride * h;
160 tmp += output_buffer->u.YUVA.a_size;
161 } else {
162 output_buffer->u.YUVA.a = NULL;
163 output_buffer->u.YUVA.a_stride = 0;
164 }
165 output_buffer->u.YUVA.u = tmp;
166 output_buffer->u.YUVA.u_stride = uv_stride;
167 output_buffer->u.YUVA.u_size = uv_stride * (h + 1) / 2;
168 tmp += output_buffer->u.YUVA.u_size;
169
170 output_buffer->u.YUVA.v = tmp;
171 output_buffer->u.YUVA.v_stride = uv_stride;
172 output_buffer->u.YUVA.v_size = uv_stride * (h + 1) / 2;
173 tmp += output_buffer->u.YUVA.v_size;
174 assert(tmp <= external_buffer + total_size);
175 }
176 output_buffer->is_external_memory = use_external_memory;
177 return external_buffer;
178 }
179
180 // Returns EXIT_SUCCESS on success, EXIT_FAILURE on failure.
main(int argc,const char * argv[])181 int main(int argc, const char* argv[]) {
182 int ok = 0;
183 const char* in_file = NULL;
184 const char* out_file = NULL;
185
186 WebPDecoderConfig config;
187 WebPDecBuffer* const output_buffer = &config.output;
188 WebPBitstreamFeatures* const bitstream = &config.input;
189 WebPOutputFileFormat format = PNG;
190 uint8_t* external_buffer = NULL;
191 int use_external_memory = 0;
192 const uint8_t* data = NULL;
193
194 int incremental = 0;
195 int c;
196
197 INIT_WARGV(argc, argv);
198
199 if (!WebPInitDecoderConfig(&config)) {
200 fprintf(stderr, "Library version mismatch!\n");
201 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
202 }
203
204 for (c = 1; c < argc; ++c) {
205 int parse_error = 0;
206 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
207 Help();
208 FREE_WARGV_AND_RETURN(EXIT_SUCCESS);
209 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
210 out_file = (const char*)GET_WARGV(argv, ++c);
211 } else if (!strcmp(argv[c], "-alpha")) {
212 format = ALPHA_PLANE_ONLY;
213 } else if (!strcmp(argv[c], "-nofancy")) {
214 config.options.no_fancy_upsampling = 1;
215 } else if (!strcmp(argv[c], "-nofilter")) {
216 config.options.bypass_filtering = 1;
217 } else if (!strcmp(argv[c], "-pam")) {
218 format = PAM;
219 } else if (!strcmp(argv[c], "-ppm")) {
220 format = PPM;
221 } else if (!strcmp(argv[c], "-bmp")) {
222 format = BMP;
223 } else if (!strcmp(argv[c], "-tiff")) {
224 format = TIFF;
225 } else if (!strcmp(argv[c], "-quiet")) {
226 quiet = 1;
227 } else if (!strcmp(argv[c], "-version")) {
228 const int version = WebPGetDecoderVersion();
229 printf("%d.%d.%d\n",
230 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
231 FREE_WARGV_AND_RETURN(EXIT_SUCCESS);
232 } else if (!strcmp(argv[c], "-pgm")) {
233 format = PGM;
234 } else if (!strcmp(argv[c], "-yuv")) {
235 format = RAW_YUV;
236 } else if (!strcmp(argv[c], "-pixel_format") && c < argc - 1) {
237 const char* const fmt = argv[++c];
238 if (!strcmp(fmt, "RGB")) format = RGB;
239 else if (!strcmp(fmt, "RGBA")) format = RGBA;
240 else if (!strcmp(fmt, "BGR")) format = BGR;
241 else if (!strcmp(fmt, "BGRA")) format = BGRA;
242 else if (!strcmp(fmt, "ARGB")) format = ARGB;
243 else if (!strcmp(fmt, "RGBA_4444")) format = RGBA_4444;
244 else if (!strcmp(fmt, "RGB_565")) format = RGB_565;
245 else if (!strcmp(fmt, "rgbA")) format = rgbA;
246 else if (!strcmp(fmt, "bgrA")) format = bgrA;
247 else if (!strcmp(fmt, "Argb")) format = Argb;
248 else if (!strcmp(fmt, "rgbA_4444")) format = rgbA_4444;
249 else if (!strcmp(fmt, "YUV")) format = YUV;
250 else if (!strcmp(fmt, "YUVA")) format = YUVA;
251 else {
252 fprintf(stderr, "Can't parse pixel_format %s\n", fmt);
253 parse_error = 1;
254 }
255 } else if (!strcmp(argv[c], "-external_memory") && c < argc - 1) {
256 use_external_memory = ExUtilGetInt(argv[++c], 0, &parse_error);
257 parse_error |= (use_external_memory > 2 || use_external_memory < 0);
258 if (parse_error) {
259 fprintf(stderr, "Can't parse 'external_memory' value %s\n", argv[c]);
260 }
261 } else if (!strcmp(argv[c], "-mt")) {
262 config.options.use_threads = 1;
263 } else if (!strcmp(argv[c], "-alpha_dither")) {
264 config.options.alpha_dithering_strength = 100;
265 } else if (!strcmp(argv[c], "-nodither")) {
266 config.options.dithering_strength = 0;
267 } else if (!strcmp(argv[c], "-dither") && c < argc - 1) {
268 config.options.dithering_strength =
269 ExUtilGetInt(argv[++c], 0, &parse_error);
270 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
271 config.options.use_cropping = 1;
272 config.options.crop_left = ExUtilGetInt(argv[++c], 0, &parse_error);
273 config.options.crop_top = ExUtilGetInt(argv[++c], 0, &parse_error);
274 config.options.crop_width = ExUtilGetInt(argv[++c], 0, &parse_error);
275 config.options.crop_height = ExUtilGetInt(argv[++c], 0, &parse_error);
276 } else if ((!strcmp(argv[c], "-scale") || !strcmp(argv[c], "-resize")) &&
277 c < argc - 2) { // '-scale' is left for compatibility
278 config.options.use_scaling = 1;
279 config.options.scaled_width = ExUtilGetInt(argv[++c], 0, &parse_error);
280 config.options.scaled_height = ExUtilGetInt(argv[++c], 0, &parse_error);
281 } else if (!strcmp(argv[c], "-flip")) {
282 config.options.flip = 1;
283 } else if (!strcmp(argv[c], "-v")) {
284 verbose = 1;
285 #ifndef WEBP_DLL
286 } else if (!strcmp(argv[c], "-noasm")) {
287 VP8GetCPUInfo = NULL;
288 #endif
289 } else if (!strcmp(argv[c], "-incremental")) {
290 incremental = 1;
291 } else if (!strcmp(argv[c], "--")) {
292 if (c < argc - 1) in_file = (const char*)GET_WARGV(argv, ++c);
293 break;
294 } else if (argv[c][0] == '-') {
295 fprintf(stderr, "Unknown option '%s'\n", argv[c]);
296 Help();
297 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
298 } else {
299 in_file = (const char*)GET_WARGV(argv, c);
300 }
301
302 if (parse_error) {
303 Help();
304 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
305 }
306 }
307
308 if (in_file == NULL) {
309 fprintf(stderr, "missing input file!!\n");
310 Help();
311 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
312 }
313
314 if (quiet) verbose = 0;
315
316 {
317 VP8StatusCode status = VP8_STATUS_OK;
318 size_t data_size = 0;
319 if (!LoadWebP(in_file, &data, &data_size, bitstream)) {
320 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
321 }
322
323 switch (format) {
324 case PNG:
325 #ifdef HAVE_WINCODEC_H
326 output_buffer->colorspace = bitstream->has_alpha ? MODE_BGRA : MODE_BGR;
327 #else
328 output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB;
329 #endif
330 break;
331 case PAM:
332 output_buffer->colorspace = MODE_RGBA;
333 break;
334 case PPM:
335 output_buffer->colorspace = MODE_RGB; // drops alpha for PPM
336 break;
337 case BMP:
338 output_buffer->colorspace = bitstream->has_alpha ? MODE_BGRA : MODE_BGR;
339 break;
340 case TIFF:
341 output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB;
342 break;
343 case PGM:
344 case RAW_YUV:
345 output_buffer->colorspace = bitstream->has_alpha ? MODE_YUVA : MODE_YUV;
346 break;
347 case ALPHA_PLANE_ONLY:
348 output_buffer->colorspace = MODE_YUVA;
349 break;
350 // forced modes:
351 case RGB: output_buffer->colorspace = MODE_RGB; break;
352 case RGBA: output_buffer->colorspace = MODE_RGBA; break;
353 case BGR: output_buffer->colorspace = MODE_BGR; break;
354 case BGRA: output_buffer->colorspace = MODE_BGRA; break;
355 case ARGB: output_buffer->colorspace = MODE_ARGB; break;
356 case RGBA_4444: output_buffer->colorspace = MODE_RGBA_4444; break;
357 case RGB_565: output_buffer->colorspace = MODE_RGB_565; break;
358 case rgbA: output_buffer->colorspace = MODE_rgbA; break;
359 case bgrA: output_buffer->colorspace = MODE_bgrA; break;
360 case Argb: output_buffer->colorspace = MODE_Argb; break;
361 case rgbA_4444: output_buffer->colorspace = MODE_rgbA_4444; break;
362 case YUV: output_buffer->colorspace = MODE_YUV; break;
363 case YUVA: output_buffer->colorspace = MODE_YUVA; break;
364 default: goto Exit;
365 }
366
367 if (use_external_memory > 0 && format >= RGB) {
368 external_buffer = AllocateExternalBuffer(&config, format,
369 use_external_memory);
370 if (external_buffer == NULL) goto Exit;
371 }
372
373 {
374 Stopwatch stop_watch;
375 if (verbose) StopwatchReset(&stop_watch);
376
377 if (incremental) {
378 status = DecodeWebPIncremental(data, data_size, &config);
379 } else {
380 status = DecodeWebP(data, data_size, &config);
381 }
382 if (verbose) {
383 const double decode_time = StopwatchReadAndReset(&stop_watch);
384 fprintf(stderr, "Time to decode picture: %.3fs\n", decode_time);
385 }
386 }
387
388 ok = (status == VP8_STATUS_OK);
389 if (!ok) {
390 PrintWebPError(in_file, status);
391 goto Exit;
392 }
393 }
394
395 if (out_file != NULL) {
396 if (!quiet) {
397 WFPRINTF(stderr, "Decoded %s.", (const W_CHAR*)in_file);
398 fprintf(stderr, " Dimensions: %d x %d %s. Format: %s. Now saving...\n",
399 output_buffer->width, output_buffer->height,
400 bitstream->has_alpha ? " (with alpha)" : "",
401 kFormatType[bitstream->format]);
402 }
403 ok = SaveOutput(output_buffer, format, out_file);
404 } else {
405 if (!quiet) {
406 WFPRINTF(stderr, "File %s can be decoded ", (const W_CHAR*)in_file);
407 fprintf(stderr, "(dimensions: %d x %d %s. Format: %s).\n",
408 output_buffer->width, output_buffer->height,
409 bitstream->has_alpha ? " (with alpha)" : "",
410 kFormatType[bitstream->format]);
411 fprintf(stderr, "Nothing written; "
412 "use -o flag to save the result as e.g. PNG.\n");
413 }
414 }
415 Exit:
416 WebPFreeDecBuffer(output_buffer);
417 WebPFree((void*)external_buffer);
418 WebPFree((void*)data);
419 FREE_WARGV_AND_RETURN(ok ? EXIT_SUCCESS : EXIT_FAILURE);
420 }
421
422 //------------------------------------------------------------------------------
423