blob: d2484f1cd32d7d2d46cdfd5d3f4624aed166c08f [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 Massiminoe06ac082011-09-02 21:30:08 +000057extern void* VP8GetCPUInfo; // 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);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700361 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800362 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);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700385 has_alpha = 1;
386 } else {
387 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800388 }
389
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700390 if (!keep_alpha) {
391 png_set_strip_alpha(png);
392 has_alpha = 0;
393 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700394#ifdef WEBP_EXPERIMENTAL_FEATURES
395 if (has_alpha) {
396 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
397 }
398#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700399
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800400 num_passes = png_set_interlace_handling(png);
401 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700402 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800403 rgb = (uint8_t*)malloc(stride * height);
404 if (rgb == NULL) goto Error;
405 for (p = 0; p < num_passes; ++p) {
406 for (y = 0; y < height; ++y) {
407 png_bytep row = rgb + y * stride;
408 png_read_rows(png, &row, NULL, 1);
409 }
410 }
411 png_read_end(png, info);
412 png_destroy_read_struct(&png, &info, NULL);
413
414 pic->width = width;
415 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700416 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
417 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800418 free(rgb);
419
420 End:
421 return ok;
422}
423#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700424static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400425 (void)in_file;
426 (void)pic;
427 (void)keep_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800428 printf("PNG support not compiled. Please install the libpng development "
429 "package before building.\n");
430 return 0;
431}
432#endif
433
434typedef enum {
435 PNG = 0,
436 JPEG,
437 UNSUPPORTED,
438} InputFileFormat;
439
440static InputFileFormat GetImageType(FILE* in_file) {
441 InputFileFormat format = UNSUPPORTED;
442 unsigned int magic;
443 unsigned char buf[4];
444
445 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
446 (fseek(in_file, 0, SEEK_SET) != 0)) {
447 return format;
448 }
449
450 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
451 if (magic == 0x89504E47U) {
452 format = PNG;
453 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
454 format = JPEG;
455 }
456 return format;
457}
458
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700459static int ReadPicture(const char* const filename, WebPPicture* const pic,
460 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800461 int ok = 0;
462 FILE* in_file = fopen(filename, "rb");
463 if (in_file == NULL) {
464 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
465 return ok;
466 }
467
468 if (pic->width == 0 || pic->height == 0) {
469 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
470 const InputFileFormat format = GetImageType(in_file);
471 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700472 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800473 } else if (format == JPEG) {
474 ok = ReadJPEG(in_file, pic);
475 }
476 } else {
477 // If image size is specified, infer it as YUV format.
478 ok = ReadYUV(in_file, pic);
479 }
480 if (!ok) {
481 fprintf(stderr, "Error! Could not process file %s\n", filename);
482 }
483
484 fclose(in_file);
485 return ok;
486}
487
James Zern31f9dc62011-06-06 17:56:50 -0700488#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800489
490static void AllocExtraInfo(WebPPicture* const pic) {
491 const int mb_w = (pic->width + 15) / 16;
492 const int mb_h = (pic->height + 15) / 16;
493 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
494}
495
496static void PrintByteCount(const int bytes[4], int total_size,
497 int* const totals) {
498 int s;
499 int total = 0;
500 for (s = 0; s < 4; ++s) {
501 fprintf(stderr, "| %7d ", bytes[s]);
502 total += bytes[s];
503 if (totals) totals[s] += bytes[s];
504 }
505 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
506}
507
508static void PrintPercents(const int counts[4], int total) {
509 int s;
510 for (s = 0; s < 4; ++s) {
511 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
512 }
513 fprintf(stderr,"| %7d\n", total);
514}
515
516static void PrintValues(const int values[4]) {
517 int s;
518 for (s = 0; s < 4; ++s) {
519 fprintf(stderr, "| %7d ", values[s]);
520 }
521 fprintf(stderr,"|\n");
522}
523
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700524static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800525 const WebPAuxStats* const stats = pic->stats;
526 if (short_output) {
527 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
528 } else{
529 const int num_i4 = stats->block_count[0];
530 const int num_i16 = stats->block_count[1];
531 const int num_skip = stats->block_count[2];
532 const int total = num_i4 + num_i16;
533 fprintf(stderr,
534 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
535 stats->coded_size,
536 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
537 if (total > 0) {
538 int totals[4] = { 0, 0, 0, 0 };
539 fprintf(stderr, "block count: intra4: %d\n"
540 " intra16: %d (-> %.2f%%)\n",
541 num_i4, num_i16, 100.f * num_i16 / total);
542 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
543 num_skip, 100.f * num_skip / total);
544 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
545 " mode-partition: %6d (%.1f%%)\n",
546 stats->header_bytes[0],
547 100.f * stats->header_bytes[0] / stats->coded_size,
548 stats->header_bytes[1],
549 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700550 if (stats->alpha_data_size) {
551 fprintf(stderr, " transparency: %6d\n",
552 stats->alpha_data_size);
553 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700554 if (stats->layer_data_size) {
555 fprintf(stderr, " enhancement: %6d\n",
556 stats->layer_data_size);
557 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800558 fprintf(stderr, " Residuals bytes "
559 "|segment 1|segment 2|segment 3"
560 "|segment 4| total\n");
561 fprintf(stderr, " intra4-coeffs: ");
562 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
563 fprintf(stderr, " intra16-coeffs: ");
564 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
565 fprintf(stderr, " chroma coeffs: ");
566 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
567 fprintf(stderr, " macroblocks: ");
568 PrintPercents(stats->segment_size, total);
569 fprintf(stderr, " quantizer: ");
570 PrintValues(stats->segment_quant);
571 fprintf(stderr, " filter level: ");
572 PrintValues(stats->segment_level);
573 fprintf(stderr, "------------------+---------");
574 fprintf(stderr, "+---------+---------+---------+-----------------\n");
575 fprintf(stderr, " segments total: ");
576 PrintByteCount(totals, stats->coded_size, NULL);
577 }
578 }
579 if (pic->extra_info) {
580 const int mb_w = (pic->width + 15) / 16;
581 const int mb_h = (pic->height + 15) / 16;
582 const int type = pic->extra_info_type;
583 int x, y;
584 for (y = 0; y < mb_h; ++y) {
585 for (x = 0; x < mb_w; ++x) {
586 const int c = pic->extra_info[x + y * mb_w];
587 if (type == 1) { // intra4/intra16
588 printf("%c", "+."[c]);
589 } else if (type == 2) { // segments
590 printf("%c", ".-*X"[c]);
591 } else if (type == 3) { // quantizers
592 printf("%.2d ", c);
593 } else if (type == 6 || type == 7) {
594 printf("%3d ", c);
595 } else {
596 printf("0x%.2x ", c);
597 }
598 }
599 printf("\n");
600 }
601 }
602}
603
James Zernc7e86ab2011-08-25 14:22:32 -0700604//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800605
606static int MyWriter(const uint8_t* data, size_t data_size,
607 const WebPPicture* const pic) {
608 FILE* const out = (FILE*)pic->custom_ptr;
609 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
610}
611
612// Dumps a picture as a PGM file using the IMC4 layout.
613static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
614 int y;
615 const int uv_width = (picture->width + 1) / 2;
616 const int uv_height = (picture->height + 1) / 2;
617 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700618 const int alpha_height = picture->a ? picture->height : 0;
619 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800620 FILE* const f = fopen(PGM_name, "wb");
621 if (!f) return 0;
622 fprintf(f, "P5\n%d %d\n255\n", stride, height);
623 for (y = 0; y < picture->height; ++y) {
624 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
625 return 0;
626 if (picture->width & 1) fputc(0, f); // pad
627 }
628 for (y = 0; y < uv_height; ++y) {
629 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
630 return 0;
631 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
632 return 0;
633 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700634 for (y = 0; y < alpha_height; ++y) {
635 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
636 return 0;
637 if (picture->width & 1) fputc(0, f); // pad
638 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800639 fclose(f);
640 return 1;
641}
642
James Zernc7e86ab2011-08-25 14:22:32 -0700643//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700645static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800646 printf("Usage:\n\n");
647 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
648 printf("where quality is between 0 (poor) to 100 (very good).\n");
649 printf("Typical value is around 80.\n\n");
650 printf("Try -longhelp for an exhaustive list of advanced options.\n");
651}
652
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700653static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800654 printf("Usage:\n");
655 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
656 printf("If input size (-s) for an image is not specified, "
657 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700658#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800659 printf("Windows builds can take as input any of the files handled by WIC\n");
660#endif
661 printf("options:\n");
662 printf(" -h / -help ............ short help\n");
663 printf(" -H / -longhelp ........ long help\n");
664 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
665 printf(" -preset <string> ....... Preset setting, one of:\n");
666 printf(" default, photo, picture,\n");
667 printf(" drawing, icon, text\n");
668 printf(" -preset must come first, as it overwrites other parameters.");
669 printf("\n");
670 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
671 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700672 printf(" -size <int> ............ Target size (in bytes)\n");
673 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674 printf("\n");
675 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
676 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
677 printf(" -f <int> ............... filter strength (0=off..100)\n");
678 printf(" -sharpness <int> ....... "
679 "filter sharpness (0:most .. 7:least sharp)\n");
680 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700681 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
682 printf(" "
683 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminoa5b78c82011-08-29 21:49:18 -0700684#ifdef WEBP_EXPERIMENTAL_FEATURES
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700685 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
686 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminoa5b78c82011-08-29 21:49:18 -0700687#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 printf(" -pass <int> ............ analysis pass number (1..10)\n");
689 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700690 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
691#ifdef WEBP_EXPERIMENTAL_FEATURES
692 printf(" -444 / -422 / -gray ..... Change colorspace\n");
693#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800694 printf(" -map <int> ............. print map of extra info.\n");
695 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700696
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800697 printf("\n");
698 printf(" -short ................. condense printed message\n");
699 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700700 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700701#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700702 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700703#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800704 printf(" -v ..................... verbose, e.g. print encoding/decoding "
705 "times\n");
706 printf("\n");
707 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800708 printf(" -af .................... auto-adjust filter strength.\n");
709 printf(" -pre <int> ............. pre-processing filter\n");
710 printf("\n");
711}
712
James Zernc7e86ab2011-08-25 14:22:32 -0700713//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700714// Error messages
715
716static const char* const kErrorMessages[] = {
717 "OK",
718 "OUT_OF_MEMORY: Out of memory allocating objects",
719 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
720 "NULL_PARAMETER: NULL parameter passed to function",
721 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700722 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
723 "allowed is 16383 pixels.",
724 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
725 "To reduce the size of this partition, try using less segments "
726 "with the -segments option, and eventually reduce the number of "
727 "header bits using -partition_limit. More details are available "
728 "in the manual (`man cwebp`)",
729 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
730 "BAD_WRITE: Picture writer returned an I/O error"
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200731 "FILE_TOO_BIG: File would be too big to fit in 4G"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700732};
733
James Zernc7e86ab2011-08-25 14:22:32 -0700734//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800735
736int main(int argc, const char *argv[]) {
737 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
738 FILE *out = NULL;
739 int c;
740 int short_output = 0;
741 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700742 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800743 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700744 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800745 WebPPicture picture;
746 WebPConfig config;
747 WebPAuxStats stats;
748 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700749
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700750#ifdef WEBP_EXPERIMENTAL_FEATURES
751 keep_alpha = 1;
752#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700753
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800754 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
755 fprintf(stderr, "Error! Version mismatch!\n");
756 goto Error;
757 }
758
759 if (argc == 1) {
760 HelpShort();
761 return 0;
762 }
763
764 for (c = 1; c < argc; ++c) {
765 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
766 HelpShort();
767 return 0;
768 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
769 HelpLong();
770 return 0;
771 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
772 out_file = argv[++c];
773 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
774 dump_file = argv[++c];
775 config.show_compressed = 1;
776 } else if (!strcmp(argv[c], "-short")) {
777 short_output++;
778 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700779 picture.width = strtol(argv[++c], NULL, 0);
780 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800781 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700782 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800783 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200784 config.quality = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800785 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700786 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200788 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700790 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700792 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800793 } else if (!strcmp(argv[c], "-af")) {
794 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700795 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800796 config.filter_type = 1;
797 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700798 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700800 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800801 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700802 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800803 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700804 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700805 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
806 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminoa5b78c82011-08-29 21:49:18 -0700807#ifdef WEBP_EXPERIMENTAL_FEATURES
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700808 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
809 config.alpha_compression = strtol(argv[++c], NULL, 0);
810 } else if (!strcmp(argv[c], "-noalpha")) {
811 keep_alpha = 0;
Pascal Massiminoa5b78c82011-08-29 21:49:18 -0700812#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800813 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700814 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700815#ifdef WEBP_EXPERIMENTAL_FEATURES
816 } else if (!strcmp(argv[c], "-444")) {
817 picture.colorspace = WEBP_YUV444;
818 } else if (!strcmp(argv[c], "-422")) {
819 picture.colorspace = WEBP_YUV422;
820 } else if (!strcmp(argv[c], "-gray")) {
821 picture.colorspace = WEBP_YUV400;
822#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
824 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700825 crop_x = strtol(argv[++c], NULL, 0);
826 crop_y = strtol(argv[++c], NULL, 0);
827 crop_w = strtol(argv[++c], NULL, 0);
828 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700829 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
830 resize_w = strtol(argv[++c], NULL, 0);
831 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700832#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700833 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000834 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700835#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700836 } else if (!strcmp(argv[c], "-version")) {
837 const int version = WebPGetEncoderVersion();
838 printf("%d.%d.%d\n",
839 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
840 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800841 } else if (!strcmp(argv[c], "-quiet")) {
842 quiet = 1;
843 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
844 WebPPreset preset;
845 ++c;
846 if (!strcmp(argv[c], "default")) {
847 preset = WEBP_PRESET_DEFAULT;
848 } else if (!strcmp(argv[c], "photo")) {
849 preset = WEBP_PRESET_PHOTO;
850 } else if (!strcmp(argv[c], "picture")) {
851 preset = WEBP_PRESET_PICTURE;
852 } else if (!strcmp(argv[c], "drawing")) {
853 preset = WEBP_PRESET_DRAWING;
854 } else if (!strcmp(argv[c], "icon")) {
855 preset = WEBP_PRESET_ICON;
856 } else if (!strcmp(argv[c], "text")) {
857 preset = WEBP_PRESET_TEXT;
858 } else {
859 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
860 goto Error;
861 }
862 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700863 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800864 goto Error;
865 }
866 } else if (!strcmp(argv[c], "-v")) {
867 verbose = 1;
868 } else if (argv[c][0] == '-') {
869 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
870 HelpLong();
871 return -1;
872 } else {
873 in_file = argv[c];
874 }
875 }
876
877 if (!WebPValidateConfig(&config)) {
878 fprintf(stderr, "Error! Invalid configuration.\n");
879 goto Error;
880 }
881
Pascal Massimino0744e842011-02-25 12:03:27 -0800882 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700883 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800884 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700885 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700886 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700887 fprintf(stderr, "Error! Cannot read input picture\n");
888 goto Error;
889 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800890 if (verbose) {
891 const double time = StopwatchReadAndReset(&stop_watch);
892 fprintf(stderr, "Time to read input: %.3fs\n", time);
893 }
894
895 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800896 if (out_file) {
897 out = fopen(out_file, "wb");
898 if (!out) {
899 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
900 goto Error;
901 } else {
902 if (!short_output && !quiet) {
903 fprintf(stderr, "Saving file '%s'\n", out_file);
904 }
905 }
906 picture.writer = MyWriter;
907 picture.custom_ptr = (void*)out;
908 } else {
909 out = NULL;
910 if (!quiet && !short_output) {
911 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
912 fprintf(stderr, "be performed, but its results discarded.\n\n");
913 }
914 }
915 picture.stats = &stats;
916
Pascal Massimino0744e842011-02-25 12:03:27 -0800917 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700918 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800919 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700920 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700921 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
922 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800923 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700924 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700925 if ((resize_w | resize_h) > 0) {
926 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
927 fprintf(stderr, "Error! Cannot resize picture\n");
928 goto Error;
929 }
930 }
931 if (picture.extra_info_type > 0) {
932 AllocExtraInfo(&picture);
933 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700934 if (!WebPEncode(&config, &picture)) {
935 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700936 fprintf(stderr, "Error code: %d (%s)\n",
937 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700938 goto Error;
939 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800940 if (verbose) {
941 const double time = StopwatchReadAndReset(&stop_watch);
942 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
943 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800944
945 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700946 if (dump_file) {
947 DumpPicture(&picture, dump_file);
948 }
949 if (!quiet) {
950 PrintExtraInfo(&picture, short_output);
951 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800952
953 Error:
954 free(picture.extra_info);
955 WebPPictureFree(&picture);
956 if (out != NULL) {
957 fclose(out);
958 }
959
960 return 0;
961}
962
James Zernc7e86ab2011-08-25 14:22:32 -0700963//------------------------------------------------------------------------------