blob: 67bdf9f85c1e6c6339caf80a0940737287fecd9f [file] [log] [blame]
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001// Copyright 2011 Google Inc.
2//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// simple command line calling the WebPEncode function.
9// Encodes a raw .YUV into WebP bitstream
10//
11// Author: Skal (pascal.massimino@gmail.com)
12
13#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070014#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080015#include <string.h>
16
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#ifdef WEBP_HAVE_PNG
22#include <png.h>
23#endif
24
25#ifdef WEBP_HAVE_JPEG
26#include <setjmp.h> // note: this must be included *after* png.h
27#include <jpeglib.h>
28#endif
29
James Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041
42#ifndef GUID_WICPixelFormat24bppRGB
43// From Microsoft SDK 7.0a
44DEFINE_GUID(GUID_WICPixelFormat24bppRGB,
45 0x6fddc324, 0x4e03, 0x4bfe, 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046#endif
James Zern3cfe0882011-06-21 18:29:30 -070047#ifndef GUID_WICPixelFormat32bppRGBA
48DEFINE_GUID(GUID_WICPixelFormat32bppRGBA,
49 0xf5c7ad2d, 0x6a8d, 0x43dd, 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
50#endif
James Zern31f9dc62011-06-06 17:56:50 -070051#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080052
53
54#include "webp/encode.h"
55#include "stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070056#ifndef WEBP_DLL
Pascal Massiminoa11009d2011-06-10 15:10:18 -070057extern void* VP8EncGetCPUInfo; // opaque forward declaration.
James Zernb4d0ef82011-07-15 14:53:03 -070058#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070059
James Zernc7e86ab2011-08-25 14:22:32 -070060//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080061
62static int verbose = 0;
63
64static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
65 const int uv_width = (pic->width + 1) / 2;
66 const int uv_height = (pic->height + 1) / 2;
67 int y;
68 int ok = 0;
69
70 if (!WebPPictureAlloc(pic)) return ok;
71
72 for (y = 0; y < pic->height; ++y) {
73 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
74 goto End;
75 }
76 }
77 for (y = 0; y < uv_height; ++y) {
78 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
79 goto End;
80 }
81 for (y = 0; y < uv_height; ++y) {
82 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
83 goto End;
84 }
85 ok = 1;
86
87 End:
88 return ok;
89}
90
James Zern31f9dc62011-06-06 17:56:50 -070091#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080092
93#define IFS(fn) \
94 do { \
95 if (SUCCEEDED(hr)) \
96 { \
97 hr = (fn); \
98 if (FAILED(hr) && verbose) \
99 printf(#fn " failed %08x\n", hr); \
100 } \
101 } while (0)
102
103#ifdef __cplusplus
104#define MAKE_REFGUID(x) (x)
105#else
106#define MAKE_REFGUID(x) &(x)
107#endif
108
109static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
110 HRESULT hr = S_OK;
111 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
112 if (FAILED(hr))
113 printf("Error opening input file %s (%08x)\n", filename, hr);
114 return hr;
115}
116
117static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700118 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800119 HRESULT hr = S_OK;
120 IWICBitmapFrameDecode* pFrame = NULL;
121 IWICFormatConverter* pConverter = NULL;
122 IWICImagingFactory* pFactory = NULL;
123 IWICBitmapDecoder* pDecoder = NULL;
124 IStream* pStream = NULL;
125 UINT frameCount = 0;
126 UINT width, height = 0;
127 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700128 WICPixelFormatGUID srcPixelFormat = { 0 };
129 GUID srcContainerFormat = { 0 };
130 const GUID* alphaContainers[] = {
131 &GUID_ContainerFormatBmp,
132 &GUID_ContainerFormatPng,
133 &GUID_ContainerFormatTiff
134 };
135 int has_alpha = 0;
136 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800137
138 IFS(CoInitialize(NULL));
139 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
140 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
141 (LPVOID*)&pFactory));
142 if (hr == REGDB_E_CLASSNOTREG) {
143 printf("Couldn't access Windows Imaging Component (are you running \n");
144 printf("Windows XP SP3 or newer?). Most formats not available.\n");
145 printf("Use -s for the available YUV input.\n");
146 }
147 // Prepare for image decoding.
148 IFS(OpenInputStream(filename, &pStream));
149 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
150 WICDecodeMetadataCacheOnDemand, &pDecoder));
151 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
152 if (SUCCEEDED(hr) && frameCount == 0) {
153 printf("No frame found in input file.\n");
154 hr = E_FAIL;
155 }
156 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700157 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
158 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
159
160 has_alpha = keep_alpha;
161 for (i = 0;
162 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
163 ++i) {
164 if (IsEqualGUID(&srcContainerFormat, alphaContainers[i])) {
165 has_alpha =
166 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppRGBA) ||
167 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppBGRA);
168 break;
169 }
170 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800171
172 // Prepare for pixel format conversion (if necessary).
173 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
174 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern3cfe0882011-06-21 18:29:30 -0700175 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA)
176 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB),
177 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800178 NULL, 0.0, WICBitmapPaletteTypeCustom));
179
180 // Decode.
181 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700182 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800183 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700184 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800185 if (rgb == NULL)
186 hr = E_OUTOFMEMORY;
187 }
James Zern3cfe0882011-06-21 18:29:30 -0700188 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
189 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800190
191 // WebP conversion.
192 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700193 int ok;
194#ifdef WEBP_EXPERIMENTAL_FEATURES
195 if (has_alpha) {
196 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
197 }
198#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800199 pic->width = width;
200 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700201 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
202 : WebPPictureImportRGB(pic, rgb, stride);
203 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800204 hr = E_FAIL;
205 }
206
207 // Cleanup.
208 if (pConverter != NULL) IUnknown_Release(pConverter);
209 if (pFrame != NULL) IUnknown_Release(pFrame);
210 if (pDecoder != NULL) IUnknown_Release(pDecoder);
211 if (pFactory != NULL) IUnknown_Release(pFactory);
212 if (pStream != NULL) IUnknown_Release(pStream);
213 free(rgb);
214 return hr;
215}
216
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700217static int ReadPicture(const char* const filename, WebPPicture* const pic,
218 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800219 int ok;
220 if (pic->width != 0 && pic->height != 0) {
221 // If image size is specified, infer it as YUV format.
222 FILE* in_file = fopen(filename, "rb");
223 if (in_file == NULL) {
224 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
225 return 0;
226 }
227 ok = ReadYUV(in_file, pic);
228 fclose(in_file);
229 } else {
230 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700231 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800232 }
233 if (!ok) {
234 fprintf(stderr, "Error! Could not process file %s\n", filename);
235 }
236 return ok;
237}
238
James Zern31f9dc62011-06-06 17:56:50 -0700239#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800240
241#ifdef WEBP_HAVE_JPEG
242struct my_error_mgr {
243 struct jpeg_error_mgr pub;
244 jmp_buf setjmp_buffer;
245};
246
247static void my_error_exit(j_common_ptr dinfo) {
248 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
249 (*dinfo->err->output_message) (dinfo);
250 longjmp(myerr->setjmp_buffer, 1);
251}
252
253static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
254 int ok = 0;
255 int stride, width, height;
256 uint8_t* rgb = NULL;
257 uint8_t* row_ptr = NULL;
258 struct jpeg_decompress_struct dinfo;
259 struct my_error_mgr jerr;
260 JSAMPARRAY buffer;
261
262 dinfo.err = jpeg_std_error(&jerr.pub);
263 jerr.pub.error_exit = my_error_exit;
264
265 if (setjmp (jerr.setjmp_buffer)) {
266 Error:
267 jpeg_destroy_decompress(&dinfo);
268 goto End;
269 }
270
271 jpeg_create_decompress(&dinfo);
272 jpeg_stdio_src(&dinfo, in_file);
273 jpeg_read_header(&dinfo, TRUE);
274
275 dinfo.out_color_space = JCS_RGB;
276 dinfo.dct_method = JDCT_IFAST;
277 dinfo.do_fancy_upsampling = TRUE;
278
279 jpeg_start_decompress(&dinfo);
280
281 if (dinfo.output_components != 3) {
282 goto Error;
283 }
284
285 width = dinfo.output_width;
286 height = dinfo.output_height;
287 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
288
289 rgb = (uint8_t*)malloc(stride * height);
290 if (rgb == NULL) {
291 goto End;
292 }
293 row_ptr = rgb;
294
295 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
296 JPOOL_IMAGE, stride, 1);
297 if (buffer == NULL) {
298 goto End;
299 }
300
301 while (dinfo.output_scanline < dinfo.output_height) {
302 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
303 goto End;
304 }
305 memcpy(row_ptr, buffer[0], stride);
306 row_ptr += stride;
307 }
308
309 jpeg_finish_decompress (&dinfo);
310 jpeg_destroy_decompress (&dinfo);
311
312 // WebP conversion.
313 pic->width = width;
314 pic->height = height;
315 ok = WebPPictureImportRGB(pic, rgb, stride);
316
317 End:
318 if (rgb) {
319 free(rgb);
320 }
321 return ok;
322}
323
324#else
325static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400326 (void)in_file;
327 (void)pic;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800328 printf("JPEG support not compiled. Please install the libjpeg development "
329 "package before building.\n");
330 return 0;
331}
332#endif
333
334#ifdef WEBP_HAVE_PNG
335static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700336 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800337 longjmp(png_jmpbuf(png), 1);
338}
339
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700340static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800341 png_structp png;
342 png_infop info;
343 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700344 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800345 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700346 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800347 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700348 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800349 int stride;
350 uint8_t* rgb = NULL;
351
352 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
353 if (png == NULL) {
354 goto End;
355 }
356
357 png_set_error_fn(png, 0, error_function, NULL);
358 if (setjmp(png_jmpbuf(png))) {
359 Error:
360 png_destroy_read_struct(&png, NULL, NULL);
361 if (rgb) free(rgb);
362 goto End;
363 }
364
365 info = png_create_info_struct(png);
366 if (info == NULL) goto Error;
367
368 png_init_io(png, in_file);
369 png_read_info(png, info);
370 if (!png_get_IHDR(png, info,
371 &width, &height, &bit_depth, &color_type, &interlaced,
372 NULL, NULL)) goto Error;
373
374 png_set_strip_16(png);
375 png_set_packing(png);
376 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
377 if (color_type == PNG_COLOR_TYPE_GRAY) {
378 if (bit_depth < 8) {
379 png_set_expand_gray_1_2_4_to_8(png);
380 }
381 png_set_gray_to_rgb(png);
382 }
383 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
384 png_set_tRNS_to_alpha(png);
385 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700386 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800387
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700388 if (!keep_alpha) {
389 png_set_strip_alpha(png);
390 has_alpha = 0;
391 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700392#ifdef WEBP_EXPERIMENTAL_FEATURES
393 if (has_alpha) {
394 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
395 }
396#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700397
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800398 num_passes = png_set_interlace_handling(png);
399 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700400 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800401 rgb = (uint8_t*)malloc(stride * height);
402 if (rgb == NULL) goto Error;
403 for (p = 0; p < num_passes; ++p) {
404 for (y = 0; y < height; ++y) {
405 png_bytep row = rgb + y * stride;
406 png_read_rows(png, &row, NULL, 1);
407 }
408 }
409 png_read_end(png, info);
410 png_destroy_read_struct(&png, &info, NULL);
411
412 pic->width = width;
413 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700414 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
415 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800416 free(rgb);
417
418 End:
419 return ok;
420}
421#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700422static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400423 (void)in_file;
424 (void)pic;
425 (void)keep_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800426 printf("PNG support not compiled. Please install the libpng development "
427 "package before building.\n");
428 return 0;
429}
430#endif
431
432typedef enum {
433 PNG = 0,
434 JPEG,
435 UNSUPPORTED,
436} InputFileFormat;
437
438static InputFileFormat GetImageType(FILE* in_file) {
439 InputFileFormat format = UNSUPPORTED;
440 unsigned int magic;
441 unsigned char buf[4];
442
443 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
444 (fseek(in_file, 0, SEEK_SET) != 0)) {
445 return format;
446 }
447
448 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
449 if (magic == 0x89504E47U) {
450 format = PNG;
451 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
452 format = JPEG;
453 }
454 return format;
455}
456
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700457static int ReadPicture(const char* const filename, WebPPicture* const pic,
458 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800459 int ok = 0;
460 FILE* in_file = fopen(filename, "rb");
461 if (in_file == NULL) {
462 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
463 return ok;
464 }
465
466 if (pic->width == 0 || pic->height == 0) {
467 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
468 const InputFileFormat format = GetImageType(in_file);
469 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700470 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800471 } else if (format == JPEG) {
472 ok = ReadJPEG(in_file, pic);
473 }
474 } else {
475 // If image size is specified, infer it as YUV format.
476 ok = ReadYUV(in_file, pic);
477 }
478 if (!ok) {
479 fprintf(stderr, "Error! Could not process file %s\n", filename);
480 }
481
482 fclose(in_file);
483 return ok;
484}
485
James Zern31f9dc62011-06-06 17:56:50 -0700486#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800487
488static void AllocExtraInfo(WebPPicture* const pic) {
489 const int mb_w = (pic->width + 15) / 16;
490 const int mb_h = (pic->height + 15) / 16;
491 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
492}
493
494static void PrintByteCount(const int bytes[4], int total_size,
495 int* const totals) {
496 int s;
497 int total = 0;
498 for (s = 0; s < 4; ++s) {
499 fprintf(stderr, "| %7d ", bytes[s]);
500 total += bytes[s];
501 if (totals) totals[s] += bytes[s];
502 }
503 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
504}
505
506static void PrintPercents(const int counts[4], int total) {
507 int s;
508 for (s = 0; s < 4; ++s) {
509 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
510 }
511 fprintf(stderr,"| %7d\n", total);
512}
513
514static void PrintValues(const int values[4]) {
515 int s;
516 for (s = 0; s < 4; ++s) {
517 fprintf(stderr, "| %7d ", values[s]);
518 }
519 fprintf(stderr,"|\n");
520}
521
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700522static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800523 const WebPAuxStats* const stats = pic->stats;
524 if (short_output) {
525 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
526 } else{
527 const int num_i4 = stats->block_count[0];
528 const int num_i16 = stats->block_count[1];
529 const int num_skip = stats->block_count[2];
530 const int total = num_i4 + num_i16;
531 fprintf(stderr,
532 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
533 stats->coded_size,
534 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
535 if (total > 0) {
536 int totals[4] = { 0, 0, 0, 0 };
537 fprintf(stderr, "block count: intra4: %d\n"
538 " intra16: %d (-> %.2f%%)\n",
539 num_i4, num_i16, 100.f * num_i16 / total);
540 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
541 num_skip, 100.f * num_skip / total);
542 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
543 " mode-partition: %6d (%.1f%%)\n",
544 stats->header_bytes[0],
545 100.f * stats->header_bytes[0] / stats->coded_size,
546 stats->header_bytes[1],
547 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700548 if (stats->alpha_data_size) {
549 fprintf(stderr, " transparency: %6d\n",
550 stats->alpha_data_size);
551 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700552 if (stats->layer_data_size) {
553 fprintf(stderr, " enhancement: %6d\n",
554 stats->layer_data_size);
555 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800556 fprintf(stderr, " Residuals bytes "
557 "|segment 1|segment 2|segment 3"
558 "|segment 4| total\n");
559 fprintf(stderr, " intra4-coeffs: ");
560 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
561 fprintf(stderr, " intra16-coeffs: ");
562 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
563 fprintf(stderr, " chroma coeffs: ");
564 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
565 fprintf(stderr, " macroblocks: ");
566 PrintPercents(stats->segment_size, total);
567 fprintf(stderr, " quantizer: ");
568 PrintValues(stats->segment_quant);
569 fprintf(stderr, " filter level: ");
570 PrintValues(stats->segment_level);
571 fprintf(stderr, "------------------+---------");
572 fprintf(stderr, "+---------+---------+---------+-----------------\n");
573 fprintf(stderr, " segments total: ");
574 PrintByteCount(totals, stats->coded_size, NULL);
575 }
576 }
577 if (pic->extra_info) {
578 const int mb_w = (pic->width + 15) / 16;
579 const int mb_h = (pic->height + 15) / 16;
580 const int type = pic->extra_info_type;
581 int x, y;
582 for (y = 0; y < mb_h; ++y) {
583 for (x = 0; x < mb_w; ++x) {
584 const int c = pic->extra_info[x + y * mb_w];
585 if (type == 1) { // intra4/intra16
586 printf("%c", "+."[c]);
587 } else if (type == 2) { // segments
588 printf("%c", ".-*X"[c]);
589 } else if (type == 3) { // quantizers
590 printf("%.2d ", c);
591 } else if (type == 6 || type == 7) {
592 printf("%3d ", c);
593 } else {
594 printf("0x%.2x ", c);
595 }
596 }
597 printf("\n");
598 }
599 }
600}
601
James Zernc7e86ab2011-08-25 14:22:32 -0700602//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800603
604static int MyWriter(const uint8_t* data, size_t data_size,
605 const WebPPicture* const pic) {
606 FILE* const out = (FILE*)pic->custom_ptr;
607 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
608}
609
610// Dumps a picture as a PGM file using the IMC4 layout.
611static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
612 int y;
613 const int uv_width = (picture->width + 1) / 2;
614 const int uv_height = (picture->height + 1) / 2;
615 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700616 const int alpha_height = picture->a ? picture->height : 0;
617 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800618 FILE* const f = fopen(PGM_name, "wb");
619 if (!f) return 0;
620 fprintf(f, "P5\n%d %d\n255\n", stride, height);
621 for (y = 0; y < picture->height; ++y) {
622 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
623 return 0;
624 if (picture->width & 1) fputc(0, f); // pad
625 }
626 for (y = 0; y < uv_height; ++y) {
627 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
628 return 0;
629 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
630 return 0;
631 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700632 for (y = 0; y < alpha_height; ++y) {
633 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
634 return 0;
635 if (picture->width & 1) fputc(0, f); // pad
636 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800637 fclose(f);
638 return 1;
639}
640
James Zernc7e86ab2011-08-25 14:22:32 -0700641//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800642
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700643static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 printf("Usage:\n\n");
645 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
646 printf("where quality is between 0 (poor) to 100 (very good).\n");
647 printf("Typical value is around 80.\n\n");
648 printf("Try -longhelp for an exhaustive list of advanced options.\n");
649}
650
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700651static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800652 printf("Usage:\n");
653 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
654 printf("If input size (-s) for an image is not specified, "
655 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700656#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800657 printf("Windows builds can take as input any of the files handled by WIC\n");
658#endif
659 printf("options:\n");
660 printf(" -h / -help ............ short help\n");
661 printf(" -H / -longhelp ........ long help\n");
662 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
663 printf(" -preset <string> ....... Preset setting, one of:\n");
664 printf(" default, photo, picture,\n");
665 printf(" drawing, icon, text\n");
666 printf(" -preset must come first, as it overwrites other parameters.");
667 printf("\n");
668 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
669 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700670 printf(" -size <int> ............ Target size (in bytes)\n");
671 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800672 printf("\n");
673 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
674 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
675 printf(" -f <int> ............... filter strength (0=off..100)\n");
676 printf(" -sharpness <int> ....... "
677 "filter sharpness (0:most .. 7:least sharp)\n");
678 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700679 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
680 printf(" "
681 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700682 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
683 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800684 printf(" -pass <int> ............ analysis pass number (1..10)\n");
685 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700686 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
687#ifdef WEBP_EXPERIMENTAL_FEATURES
688 printf(" -444 / -422 / -gray ..... Change colorspace\n");
689#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800690 printf(" -map <int> ............. print map of extra info.\n");
691 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700692
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800693 printf("\n");
694 printf(" -short ................. condense printed message\n");
695 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700696 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700697#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700698 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700699#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 printf(" -v ..................... verbose, e.g. print encoding/decoding "
701 "times\n");
702 printf("\n");
703 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800704 printf(" -af .................... auto-adjust filter strength.\n");
705 printf(" -pre <int> ............. pre-processing filter\n");
706 printf("\n");
707}
708
James Zernc7e86ab2011-08-25 14:22:32 -0700709//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700710// Error messages
711
712static const char* const kErrorMessages[] = {
713 "OK",
714 "OUT_OF_MEMORY: Out of memory allocating objects",
715 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
716 "NULL_PARAMETER: NULL parameter passed to function",
717 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700718 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
719 "allowed is 16383 pixels.",
720 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
721 "To reduce the size of this partition, try using less segments "
722 "with the -segments option, and eventually reduce the number of "
723 "header bits using -partition_limit. More details are available "
724 "in the manual (`man cwebp`)",
725 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
726 "BAD_WRITE: Picture writer returned an I/O error"
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200727 "FILE_TOO_BIG: File would be too big to fit in 4G"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700728};
729
James Zernc7e86ab2011-08-25 14:22:32 -0700730//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800731
732int main(int argc, const char *argv[]) {
733 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
734 FILE *out = NULL;
735 int c;
736 int short_output = 0;
737 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700738 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800739 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700740 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800741 WebPPicture picture;
742 WebPConfig config;
743 WebPAuxStats stats;
744 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700745
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700746#ifdef WEBP_EXPERIMENTAL_FEATURES
747 keep_alpha = 1;
748#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700749
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800750 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
751 fprintf(stderr, "Error! Version mismatch!\n");
752 goto Error;
753 }
754
755 if (argc == 1) {
756 HelpShort();
757 return 0;
758 }
759
760 for (c = 1; c < argc; ++c) {
761 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
762 HelpShort();
763 return 0;
764 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
765 HelpLong();
766 return 0;
767 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
768 out_file = argv[++c];
769 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
770 dump_file = argv[++c];
771 config.show_compressed = 1;
772 } else if (!strcmp(argv[c], "-short")) {
773 short_output++;
774 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700775 picture.width = strtol(argv[++c], NULL, 0);
776 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700778 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800779 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200780 config.quality = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800781 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700782 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800783 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200784 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800785 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700786 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700788 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 } else if (!strcmp(argv[c], "-af")) {
790 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700791 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800792 config.filter_type = 1;
793 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700794 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800795 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700796 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800797 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700798 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700800 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700801 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
802 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700803 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
804 config.alpha_compression = strtol(argv[++c], NULL, 0);
805 } else if (!strcmp(argv[c], "-noalpha")) {
806 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800807 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700808 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700809#ifdef WEBP_EXPERIMENTAL_FEATURES
810 } else if (!strcmp(argv[c], "-444")) {
811 picture.colorspace = WEBP_YUV444;
812 } else if (!strcmp(argv[c], "-422")) {
813 picture.colorspace = WEBP_YUV422;
814 } else if (!strcmp(argv[c], "-gray")) {
815 picture.colorspace = WEBP_YUV400;
816#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800817 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
818 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700819 crop_x = strtol(argv[++c], NULL, 0);
820 crop_y = strtol(argv[++c], NULL, 0);
821 crop_w = strtol(argv[++c], NULL, 0);
822 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700823 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
824 resize_w = strtol(argv[++c], NULL, 0);
825 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700826#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700827 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoa11009d2011-06-10 15:10:18 -0700828 VP8EncGetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700829#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700830 } else if (!strcmp(argv[c], "-version")) {
831 const int version = WebPGetEncoderVersion();
832 printf("%d.%d.%d\n",
833 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
834 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 } else if (!strcmp(argv[c], "-quiet")) {
836 quiet = 1;
837 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
838 WebPPreset preset;
839 ++c;
840 if (!strcmp(argv[c], "default")) {
841 preset = WEBP_PRESET_DEFAULT;
842 } else if (!strcmp(argv[c], "photo")) {
843 preset = WEBP_PRESET_PHOTO;
844 } else if (!strcmp(argv[c], "picture")) {
845 preset = WEBP_PRESET_PICTURE;
846 } else if (!strcmp(argv[c], "drawing")) {
847 preset = WEBP_PRESET_DRAWING;
848 } else if (!strcmp(argv[c], "icon")) {
849 preset = WEBP_PRESET_ICON;
850 } else if (!strcmp(argv[c], "text")) {
851 preset = WEBP_PRESET_TEXT;
852 } else {
853 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
854 goto Error;
855 }
856 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700857 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800858 goto Error;
859 }
860 } else if (!strcmp(argv[c], "-v")) {
861 verbose = 1;
862 } else if (argv[c][0] == '-') {
863 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
864 HelpLong();
865 return -1;
866 } else {
867 in_file = argv[c];
868 }
869 }
870
871 if (!WebPValidateConfig(&config)) {
872 fprintf(stderr, "Error! Invalid configuration.\n");
873 goto Error;
874 }
875
Pascal Massimino0744e842011-02-25 12:03:27 -0800876 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700877 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800878 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700879 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700880 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700881 fprintf(stderr, "Error! Cannot read input picture\n");
882 goto Error;
883 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800884 if (verbose) {
885 const double time = StopwatchReadAndReset(&stop_watch);
886 fprintf(stderr, "Time to read input: %.3fs\n", time);
887 }
888
889 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800890 if (out_file) {
891 out = fopen(out_file, "wb");
892 if (!out) {
893 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
894 goto Error;
895 } else {
896 if (!short_output && !quiet) {
897 fprintf(stderr, "Saving file '%s'\n", out_file);
898 }
899 }
900 picture.writer = MyWriter;
901 picture.custom_ptr = (void*)out;
902 } else {
903 out = NULL;
904 if (!quiet && !short_output) {
905 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
906 fprintf(stderr, "be performed, but its results discarded.\n\n");
907 }
908 }
909 picture.stats = &stats;
910
Pascal Massimino0744e842011-02-25 12:03:27 -0800911 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700912 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800913 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700914 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700915 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
916 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800917 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700918 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700919 if ((resize_w | resize_h) > 0) {
920 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
921 fprintf(stderr, "Error! Cannot resize picture\n");
922 goto Error;
923 }
924 }
925 if (picture.extra_info_type > 0) {
926 AllocExtraInfo(&picture);
927 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700928 if (!WebPEncode(&config, &picture)) {
929 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700930 fprintf(stderr, "Error code: %d (%s)\n",
931 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700932 goto Error;
933 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800934 if (verbose) {
935 const double time = StopwatchReadAndReset(&stop_watch);
936 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
937 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800938
939 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700940 if (dump_file) {
941 DumpPicture(&picture, dump_file);
942 }
943 if (!quiet) {
944 PrintExtraInfo(&picture, short_output);
945 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800946
947 Error:
948 free(picture.extra_info);
949 WebPPictureFree(&picture);
950 if (out != NULL) {
951 fclose(out);
952 }
953
954 return 0;
955}
956
James Zernc7e86ab2011-08-25 14:22:32 -0700957//------------------------------------------------------------------------------