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 ([email protected])
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
main(int argc,const char * argv[])180 int main(int argc, const char* argv[]) {
181 int ok = 0;
182 const char* in_file = NULL;
183 const char* out_file = NULL;
184
185 WebPDecoderConfig config;
186 WebPDecBuffer* const output_buffer = &config.output;
187 WebPBitstreamFeatures* const bitstream = &config.input;
188 WebPOutputFileFormat format = PNG;
189 uint8_t* external_buffer = NULL;
190 int use_external_memory = 0;
191 const uint8_t* data = NULL;
192
193 int incremental = 0;
194 int c;
195
196 INIT_WARGV(argc, argv);
197
198 if (!WebPInitDecoderConfig(&config)) {
199 fprintf(stderr, "Library version mismatch!\n");
200 FREE_WARGV_AND_RETURN(-1);
201 }
202
203 for (c = 1; c < argc; ++c) {
204 int parse_error = 0;
205 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
206 Help();
207 FREE_WARGV_AND_RETURN(0);
208 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
209 out_file = (const char*)GET_WARGV(argv, ++c);
210 } else if (!strcmp(argv[c], "-alpha")) {
211 format = ALPHA_PLANE_ONLY;
212 } else if (!strcmp(argv[c], "-nofancy")) {
213 config.options.no_fancy_upsampling = 1;
214 } else if (!strcmp(argv[c], "-nofilter")) {
215 config.options.bypass_filtering = 1;
216 } else if (!strcmp(argv[c], "-pam")) {
217 format = PAM;
218 } else if (!strcmp(argv[c], "-ppm")) {
219 format = PPM;
220 } else if (!strcmp(argv[c], "-bmp")) {
221 format = BMP;
222 } else if (!strcmp(argv[c], "-tiff")) {
223 format = TIFF;
224 } else if (!strcmp(argv[c], "-quiet")) {
225 quiet = 1;
226 } else if (!strcmp(argv[c], "-version")) {
227 const int version = WebPGetDecoderVersion();
228 printf("%d.%d.%d\n",
229 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
230 FREE_WARGV_AND_RETURN(0);
231 } else if (!strcmp(argv[c], "-pgm")) {
232 format = PGM;
233 } else if (!strcmp(argv[c], "-yuv")) {
234 format = RAW_YUV;
235 } else if (!strcmp(argv[c], "-pixel_format") && c < argc - 1) {
236 const char* const fmt = argv[++c];
237 if (!strcmp(fmt, "RGB")) format = RGB;
238 else if (!strcmp(fmt, "RGBA")) format = RGBA;
239 else if (!strcmp(fmt, "BGR")) format = BGR;
240 else if (!strcmp(fmt, "BGRA")) format = BGRA;
241 else if (!strcmp(fmt, "ARGB")) format = ARGB;
242 else if (!strcmp(fmt, "RGBA_4444")) format = RGBA_4444;
243 else if (!strcmp(fmt, "RGB_565")) format = RGB_565;
244 else if (!strcmp(fmt, "rgbA")) format = rgbA;
245 else if (!strcmp(fmt, "bgrA")) format = bgrA;
246 else if (!strcmp(fmt, "Argb")) format = Argb;
247 else if (!strcmp(fmt, "rgbA_4444")) format = rgbA_4444;
248 else if (!strcmp(fmt, "YUV")) format = YUV;
249 else if (!strcmp(fmt, "YUVA")) format = YUVA;
250 else {
251 fprintf(stderr, "Can't parse pixel_format %s\n", fmt);
252 parse_error = 1;
253 }
254 } else if (!strcmp(argv[c], "-external_memory") && c < argc - 1) {
255 use_external_memory = ExUtilGetInt(argv[++c], 0, &parse_error);
256 parse_error |= (use_external_memory > 2 || use_external_memory < 0);
257 if (parse_error) {
258 fprintf(stderr, "Can't parse 'external_memory' value %s\n", argv[c]);
259 }
260 } else if (!strcmp(argv[c], "-mt")) {
261 config.options.use_threads = 1;
262 } else if (!strcmp(argv[c], "-alpha_dither")) {
263 config.options.alpha_dithering_strength = 100;
264 } else if (!strcmp(argv[c], "-nodither")) {
265 config.options.dithering_strength = 0;
266 } else if (!strcmp(argv[c], "-dither") && c < argc - 1) {
267 config.options.dithering_strength =
268 ExUtilGetInt(argv[++c], 0, &parse_error);
269 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
270 config.options.use_cropping = 1;
271 config.options.crop_left = ExUtilGetInt(argv[++c], 0, &parse_error);
272 config.options.crop_top = ExUtilGetInt(argv[++c], 0, &parse_error);
273 config.options.crop_width = ExUtilGetInt(argv[++c], 0, &parse_error);
274 config.options.crop_height = ExUtilGetInt(argv[++c], 0, &parse_error);
275 } else if ((!strcmp(argv[c], "-scale") || !strcmp(argv[c], "-resize")) &&
276 c < argc - 2) { // '-scale' is left for compatibility
277 config.options.use_scaling = 1;
278 config.options.scaled_width = ExUtilGetInt(argv[++c], 0, &parse_error);
279 config.options.scaled_height = ExUtilGetInt(argv[++c], 0, &parse_error);
280 } else if (!strcmp(argv[c], "-flip")) {
281 config.options.flip = 1;
282 } else if (!strcmp(argv[c], "-v")) {
283 verbose = 1;
284 #ifndef WEBP_DLL
285 } else if (!strcmp(argv[c], "-noasm")) {
286 VP8GetCPUInfo = NULL;
287 #endif
288 } else if (!strcmp(argv[c], "-incremental")) {
289 incremental = 1;
290 } else if (!strcmp(argv[c], "--")) {
291 if (c < argc - 1) in_file = (const char*)GET_WARGV(argv, ++c);
292 break;
293 } else if (argv[c][0] == '-') {
294 fprintf(stderr, "Unknown option '%s'\n", argv[c]);
295 Help();
296 FREE_WARGV_AND_RETURN(-1);
297 } else {
298 in_file = (const char*)GET_WARGV(argv, c);
299 }
300
301 if (parse_error) {
302 Help();
303 FREE_WARGV_AND_RETURN(-1);
304 }
305 }
306
307 if (in_file == NULL) {
308 fprintf(stderr, "missing input file!!\n");
309 Help();
310 FREE_WARGV_AND_RETURN(-1);
311 }
312
313 if (quiet) verbose = 0;
314
315 {
316 VP8StatusCode status = VP8_STATUS_OK;
317 size_t data_size = 0;
318 if (!LoadWebP(in_file, &data, &data_size, bitstream)) {
319 FREE_WARGV_AND_RETURN(-1);
320 }
321
322 switch (format) {
323 case PNG:
324 #ifdef HAVE_WINCODEC_H
325 output_buffer->colorspace = bitstream->has_alpha ? MODE_BGRA : MODE_BGR;
326 #else
327 output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB;
328 #endif
329 break;
330 case PAM:
331 output_buffer->colorspace = MODE_RGBA;
332 break;
333 case PPM:
334 output_buffer->colorspace = MODE_RGB; // drops alpha for PPM
335 break;
336 case BMP:
337 output_buffer->colorspace = bitstream->has_alpha ? MODE_BGRA : MODE_BGR;
338 break;
339 case TIFF:
340 output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB;
341 break;
342 case PGM:
343 case RAW_YUV:
344 output_buffer->colorspace = bitstream->has_alpha ? MODE_YUVA : MODE_YUV;
345 break;
346 case ALPHA_PLANE_ONLY:
347 output_buffer->colorspace = MODE_YUVA;
348 break;
349 // forced modes:
350 case RGB: output_buffer->colorspace = MODE_RGB; break;
351 case RGBA: output_buffer->colorspace = MODE_RGBA; break;
352 case BGR: output_buffer->colorspace = MODE_BGR; break;
353 case BGRA: output_buffer->colorspace = MODE_BGRA; break;
354 case ARGB: output_buffer->colorspace = MODE_ARGB; break;
355 case RGBA_4444: output_buffer->colorspace = MODE_RGBA_4444; break;
356 case RGB_565: output_buffer->colorspace = MODE_RGB_565; break;
357 case rgbA: output_buffer->colorspace = MODE_rgbA; break;
358 case bgrA: output_buffer->colorspace = MODE_bgrA; break;
359 case Argb: output_buffer->colorspace = MODE_Argb; break;
360 case rgbA_4444: output_buffer->colorspace = MODE_rgbA_4444; break;
361 case YUV: output_buffer->colorspace = MODE_YUV; break;
362 case YUVA: output_buffer->colorspace = MODE_YUVA; break;
363 default: goto Exit;
364 }
365
366 if (use_external_memory > 0 && format >= RGB) {
367 external_buffer = AllocateExternalBuffer(&config, format,
368 use_external_memory);
369 if (external_buffer == NULL) goto Exit;
370 }
371
372 {
373 Stopwatch stop_watch;
374 if (verbose) StopwatchReset(&stop_watch);
375
376 if (incremental) {
377 status = DecodeWebPIncremental(data, data_size, &config);
378 } else {
379 status = DecodeWebP(data, data_size, &config);
380 }
381 if (verbose) {
382 const double decode_time = StopwatchReadAndReset(&stop_watch);
383 fprintf(stderr, "Time to decode picture: %.3fs\n", decode_time);
384 }
385 }
386
387 ok = (status == VP8_STATUS_OK);
388 if (!ok) {
389 PrintWebPError(in_file, status);
390 goto Exit;
391 }
392 }
393
394 if (out_file != NULL) {
395 if (!quiet) {
396 WFPRINTF(stderr, "Decoded %s.", (const W_CHAR*)in_file);
397 fprintf(stderr, " Dimensions: %d x %d %s. Format: %s. Now saving...\n",
398 output_buffer->width, output_buffer->height,
399 bitstream->has_alpha ? " (with alpha)" : "",
400 kFormatType[bitstream->format]);
401 }
402 ok = SaveOutput(output_buffer, format, out_file);
403 } else {
404 if (!quiet) {
405 WFPRINTF(stderr, "File %s can be decoded ", (const W_CHAR*)in_file);
406 fprintf(stderr, "(dimensions: %d x %d %s. Format: %s).\n",
407 output_buffer->width, output_buffer->height,
408 bitstream->has_alpha ? " (with alpha)" : "",
409 kFormatType[bitstream->format]);
410 fprintf(stderr, "Nothing written; "
411 "use -o flag to save the result as e.g. PNG.\n");
412 }
413 }
414 Exit:
415 WebPFreeDecBuffer(output_buffer);
416 WebPFree((void*)external_buffer);
417 WebPFree((void*)data);
418 FREE_WARGV_AND_RETURN(ok ? 0 : -1);
419 }
420
421 //------------------------------------------------------------------------------
422