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