blob: 9d31bb1751abb1031129bda9384629f6f096946d [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08002//
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
James Zern04e84cf2011-11-04 15:20:08 -070057#if defined(__cplusplus) || defined(c_plusplus)
58extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070059#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070060
James Zern04e84cf2011-11-04 15:20:08 -070061extern void* VP8GetCPUInfo; // opaque forward declaration.
62
63#if defined(__cplusplus) || defined(c_plusplus)
64} // extern "C"
65#endif
66#endif // WEBP_DLL
67
James Zernc7e86ab2011-08-25 14:22:32 -070068//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080069
70static int verbose = 0;
71
72static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
73 const int uv_width = (pic->width + 1) / 2;
74 const int uv_height = (pic->height + 1) / 2;
75 int y;
76 int ok = 0;
77
78 if (!WebPPictureAlloc(pic)) return ok;
79
80 for (y = 0; y < pic->height; ++y) {
81 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
82 goto End;
83 }
84 }
85 for (y = 0; y < uv_height; ++y) {
86 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
87 goto End;
88 }
89 for (y = 0; y < uv_height; ++y) {
90 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
91 goto End;
92 }
93 ok = 1;
94
95 End:
96 return ok;
97}
98
James Zern31f9dc62011-06-06 17:56:50 -070099#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800100
101#define IFS(fn) \
102 do { \
103 if (SUCCEEDED(hr)) \
104 { \
105 hr = (fn); \
106 if (FAILED(hr) && verbose) \
107 printf(#fn " failed %08x\n", hr); \
108 } \
109 } while (0)
110
111#ifdef __cplusplus
112#define MAKE_REFGUID(x) (x)
113#else
114#define MAKE_REFGUID(x) &(x)
115#endif
116
117static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
118 HRESULT hr = S_OK;
119 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
120 if (FAILED(hr))
121 printf("Error opening input file %s (%08x)\n", filename, hr);
122 return hr;
123}
124
125static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700126 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800127 HRESULT hr = S_OK;
128 IWICBitmapFrameDecode* pFrame = NULL;
129 IWICFormatConverter* pConverter = NULL;
130 IWICImagingFactory* pFactory = NULL;
131 IWICBitmapDecoder* pDecoder = NULL;
132 IStream* pStream = NULL;
133 UINT frameCount = 0;
134 UINT width, height = 0;
135 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700136 WICPixelFormatGUID srcPixelFormat = { 0 };
137 GUID srcContainerFormat = { 0 };
138 const GUID* alphaContainers[] = {
139 &GUID_ContainerFormatBmp,
140 &GUID_ContainerFormatPng,
141 &GUID_ContainerFormatTiff
142 };
143 int has_alpha = 0;
144 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800145
146 IFS(CoInitialize(NULL));
147 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
148 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
149 (LPVOID*)&pFactory));
150 if (hr == REGDB_E_CLASSNOTREG) {
151 printf("Couldn't access Windows Imaging Component (are you running \n");
152 printf("Windows XP SP3 or newer?). Most formats not available.\n");
153 printf("Use -s for the available YUV input.\n");
154 }
155 // Prepare for image decoding.
156 IFS(OpenInputStream(filename, &pStream));
157 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
158 WICDecodeMetadataCacheOnDemand, &pDecoder));
159 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
160 if (SUCCEEDED(hr) && frameCount == 0) {
161 printf("No frame found in input file.\n");
162 hr = E_FAIL;
163 }
164 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700165 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
166 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
167
168 has_alpha = keep_alpha;
169 for (i = 0;
170 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
171 ++i) {
172 if (IsEqualGUID(&srcContainerFormat, alphaContainers[i])) {
173 has_alpha =
174 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppRGBA) ||
175 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppBGRA);
176 break;
177 }
178 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800179
180 // Prepare for pixel format conversion (if necessary).
181 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
182 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern3cfe0882011-06-21 18:29:30 -0700183 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA)
184 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB),
185 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800186 NULL, 0.0, WICBitmapPaletteTypeCustom));
187
188 // Decode.
189 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700190 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800191 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700192 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800193 if (rgb == NULL)
194 hr = E_OUTOFMEMORY;
195 }
James Zern3cfe0882011-06-21 18:29:30 -0700196 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
197 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800198
199 // WebP conversion.
200 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700201 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800202 pic->width = width;
203 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700204 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
205 : WebPPictureImportRGB(pic, rgb, stride);
206 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800207 hr = E_FAIL;
208 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000209 if (SUCCEEDED(hr)) {
210 if (has_alpha && keep_alpha == 2) {
211 WebPCleanupTransparentArea(pic);
212 }
213 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800214
215 // Cleanup.
216 if (pConverter != NULL) IUnknown_Release(pConverter);
217 if (pFrame != NULL) IUnknown_Release(pFrame);
218 if (pDecoder != NULL) IUnknown_Release(pDecoder);
219 if (pFactory != NULL) IUnknown_Release(pFactory);
220 if (pStream != NULL) IUnknown_Release(pStream);
221 free(rgb);
222 return hr;
223}
224
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700225static int ReadPicture(const char* const filename, WebPPicture* const pic,
226 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800227 int ok;
228 if (pic->width != 0 && pic->height != 0) {
229 // If image size is specified, infer it as YUV format.
230 FILE* in_file = fopen(filename, "rb");
231 if (in_file == NULL) {
232 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
233 return 0;
234 }
235 ok = ReadYUV(in_file, pic);
236 fclose(in_file);
237 } else {
238 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700239 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800240 }
241 if (!ok) {
242 fprintf(stderr, "Error! Could not process file %s\n", filename);
243 }
244 return ok;
245}
246
James Zern31f9dc62011-06-06 17:56:50 -0700247#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800248
249#ifdef WEBP_HAVE_JPEG
250struct my_error_mgr {
251 struct jpeg_error_mgr pub;
252 jmp_buf setjmp_buffer;
253};
254
255static void my_error_exit(j_common_ptr dinfo) {
256 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
257 (*dinfo->err->output_message) (dinfo);
258 longjmp(myerr->setjmp_buffer, 1);
259}
260
261static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
262 int ok = 0;
263 int stride, width, height;
264 uint8_t* rgb = NULL;
265 uint8_t* row_ptr = NULL;
266 struct jpeg_decompress_struct dinfo;
267 struct my_error_mgr jerr;
268 JSAMPARRAY buffer;
269
270 dinfo.err = jpeg_std_error(&jerr.pub);
271 jerr.pub.error_exit = my_error_exit;
272
James Zern04e84cf2011-11-04 15:20:08 -0700273 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800274 Error:
275 jpeg_destroy_decompress(&dinfo);
276 goto End;
277 }
278
279 jpeg_create_decompress(&dinfo);
280 jpeg_stdio_src(&dinfo, in_file);
281 jpeg_read_header(&dinfo, TRUE);
282
283 dinfo.out_color_space = JCS_RGB;
284 dinfo.dct_method = JDCT_IFAST;
285 dinfo.do_fancy_upsampling = TRUE;
286
287 jpeg_start_decompress(&dinfo);
288
289 if (dinfo.output_components != 3) {
290 goto Error;
291 }
292
293 width = dinfo.output_width;
294 height = dinfo.output_height;
295 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
296
297 rgb = (uint8_t*)malloc(stride * height);
298 if (rgb == NULL) {
299 goto End;
300 }
301 row_ptr = rgb;
302
303 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
304 JPOOL_IMAGE, stride, 1);
305 if (buffer == NULL) {
306 goto End;
307 }
308
309 while (dinfo.output_scanline < dinfo.output_height) {
310 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
311 goto End;
312 }
313 memcpy(row_ptr, buffer[0], stride);
314 row_ptr += stride;
315 }
316
James Zern04e84cf2011-11-04 15:20:08 -0700317 jpeg_finish_decompress(&dinfo);
318 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800319
320 // WebP conversion.
321 pic->width = width;
322 pic->height = height;
323 ok = WebPPictureImportRGB(pic, rgb, stride);
324
325 End:
326 if (rgb) {
327 free(rgb);
328 }
329 return ok;
330}
331
332#else
333static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400334 (void)in_file;
335 (void)pic;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800336 printf("JPEG support not compiled. Please install the libjpeg development "
337 "package before building.\n");
338 return 0;
339}
340#endif
341
342#ifdef WEBP_HAVE_PNG
343static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700344 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800345 longjmp(png_jmpbuf(png), 1);
346}
347
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700348static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800349 png_structp png;
350 png_infop info;
351 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700352 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800353 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700354 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800355 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700356 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800357 int stride;
358 uint8_t* rgb = NULL;
359
360 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
361 if (png == NULL) {
362 goto End;
363 }
364
365 png_set_error_fn(png, 0, error_function, NULL);
366 if (setjmp(png_jmpbuf(png))) {
367 Error:
368 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700369 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800370 goto End;
371 }
372
373 info = png_create_info_struct(png);
374 if (info == NULL) goto Error;
375
376 png_init_io(png, in_file);
377 png_read_info(png, info);
378 if (!png_get_IHDR(png, info,
379 &width, &height, &bit_depth, &color_type, &interlaced,
380 NULL, NULL)) goto Error;
381
382 png_set_strip_16(png);
383 png_set_packing(png);
384 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000385 if (color_type == PNG_COLOR_TYPE_GRAY ||
386 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800387 if (bit_depth < 8) {
388 png_set_expand_gray_1_2_4_to_8(png);
389 }
390 png_set_gray_to_rgb(png);
391 }
392 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
393 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700394 has_alpha = 1;
395 } else {
396 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800397 }
398
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700399 if (!keep_alpha) {
400 png_set_strip_alpha(png);
401 has_alpha = 0;
402 }
403
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800404 num_passes = png_set_interlace_handling(png);
405 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700406 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800407 rgb = (uint8_t*)malloc(stride * height);
408 if (rgb == NULL) goto Error;
409 for (p = 0; p < num_passes; ++p) {
410 for (y = 0; y < height; ++y) {
411 png_bytep row = rgb + y * stride;
412 png_read_rows(png, &row, NULL, 1);
413 }
414 }
415 png_read_end(png, info);
416 png_destroy_read_struct(&png, &info, NULL);
417
418 pic->width = width;
419 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700420 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
421 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800422 free(rgb);
423
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000424 if (ok && has_alpha && keep_alpha == 2) {
425 WebPCleanupTransparentArea(pic);
426 }
427
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800428 End:
429 return ok;
430}
431#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700432static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400433 (void)in_file;
434 (void)pic;
435 (void)keep_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800436 printf("PNG support not compiled. Please install the libpng development "
437 "package before building.\n");
438 return 0;
439}
440#endif
441
442typedef enum {
443 PNG = 0,
444 JPEG,
445 UNSUPPORTED,
446} InputFileFormat;
447
448static InputFileFormat GetImageType(FILE* in_file) {
449 InputFileFormat format = UNSUPPORTED;
450 unsigned int magic;
451 unsigned char buf[4];
452
453 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
454 (fseek(in_file, 0, SEEK_SET) != 0)) {
455 return format;
456 }
457
458 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
459 if (magic == 0x89504E47U) {
460 format = PNG;
461 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
462 format = JPEG;
463 }
464 return format;
465}
466
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700467static int ReadPicture(const char* const filename, WebPPicture* const pic,
468 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800469 int ok = 0;
470 FILE* in_file = fopen(filename, "rb");
471 if (in_file == NULL) {
472 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
473 return ok;
474 }
475
476 if (pic->width == 0 || pic->height == 0) {
477 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
478 const InputFileFormat format = GetImageType(in_file);
479 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700480 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800481 } else if (format == JPEG) {
482 ok = ReadJPEG(in_file, pic);
483 }
484 } else {
485 // If image size is specified, infer it as YUV format.
486 ok = ReadYUV(in_file, pic);
487 }
488 if (!ok) {
489 fprintf(stderr, "Error! Could not process file %s\n", filename);
490 }
491
492 fclose(in_file);
493 return ok;
494}
495
James Zern31f9dc62011-06-06 17:56:50 -0700496#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800497
498static void AllocExtraInfo(WebPPicture* const pic) {
499 const int mb_w = (pic->width + 15) / 16;
500 const int mb_h = (pic->height + 15) / 16;
501 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
502}
503
504static void PrintByteCount(const int bytes[4], int total_size,
505 int* const totals) {
506 int s;
507 int total = 0;
508 for (s = 0; s < 4; ++s) {
509 fprintf(stderr, "| %7d ", bytes[s]);
510 total += bytes[s];
511 if (totals) totals[s] += bytes[s];
512 }
James Zern04e84cf2011-11-04 15:20:08 -0700513 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800514}
515
516static void PrintPercents(const int counts[4], int total) {
517 int s;
518 for (s = 0; s < 4; ++s) {
519 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
520 }
James Zern04e84cf2011-11-04 15:20:08 -0700521 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800522}
523
524static void PrintValues(const int values[4]) {
525 int s;
526 for (s = 0; s < 4; ++s) {
527 fprintf(stderr, "| %7d ", values[s]);
528 }
James Zern04e84cf2011-11-04 15:20:08 -0700529 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800530}
531
Pascal Massiminod61479f2012-01-20 07:20:56 -0800532static void PrintExtraInfo(const WebPPicture* const pic, int short_output,
533 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800534 const WebPAuxStats* const stats = pic->stats;
535 if (short_output) {
536 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700537 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800538 const int num_i4 = stats->block_count[0];
539 const int num_i16 = stats->block_count[1];
540 const int num_skip = stats->block_count[2];
541 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800542 fprintf(stderr, "File: %s\n", file_name);
543 fprintf(stderr, "Dimension: %d x %d%s\n",
544 pic->width, pic->height, (pic->a != NULL) ? " (with alpha)" : "");
545 fprintf(stderr, "Output: "
546 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800547 stats->coded_size,
548 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
549 if (total > 0) {
550 int totals[4] = { 0, 0, 0, 0 };
551 fprintf(stderr, "block count: intra4: %d\n"
552 " intra16: %d (-> %.2f%%)\n",
553 num_i4, num_i16, 100.f * num_i16 / total);
554 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
555 num_skip, 100.f * num_skip / total);
556 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
557 " mode-partition: %6d (%.1f%%)\n",
558 stats->header_bytes[0],
559 100.f * stats->header_bytes[0] / stats->coded_size,
560 stats->header_bytes[1],
561 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700562 if (stats->alpha_data_size) {
563 fprintf(stderr, " transparency: %6d\n",
564 stats->alpha_data_size);
565 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700566 if (stats->layer_data_size) {
567 fprintf(stderr, " enhancement: %6d\n",
568 stats->layer_data_size);
569 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800570 fprintf(stderr, " Residuals bytes "
571 "|segment 1|segment 2|segment 3"
572 "|segment 4| total\n");
573 fprintf(stderr, " intra4-coeffs: ");
574 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
575 fprintf(stderr, " intra16-coeffs: ");
576 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
577 fprintf(stderr, " chroma coeffs: ");
578 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
579 fprintf(stderr, " macroblocks: ");
580 PrintPercents(stats->segment_size, total);
581 fprintf(stderr, " quantizer: ");
582 PrintValues(stats->segment_quant);
583 fprintf(stderr, " filter level: ");
584 PrintValues(stats->segment_level);
585 fprintf(stderr, "------------------+---------");
586 fprintf(stderr, "+---------+---------+---------+-----------------\n");
587 fprintf(stderr, " segments total: ");
588 PrintByteCount(totals, stats->coded_size, NULL);
589 }
590 }
591 if (pic->extra_info) {
592 const int mb_w = (pic->width + 15) / 16;
593 const int mb_h = (pic->height + 15) / 16;
594 const int type = pic->extra_info_type;
595 int x, y;
596 for (y = 0; y < mb_h; ++y) {
597 for (x = 0; x < mb_w; ++x) {
598 const int c = pic->extra_info[x + y * mb_w];
599 if (type == 1) { // intra4/intra16
600 printf("%c", "+."[c]);
601 } else if (type == 2) { // segments
602 printf("%c", ".-*X"[c]);
603 } else if (type == 3) { // quantizers
604 printf("%.2d ", c);
605 } else if (type == 6 || type == 7) {
606 printf("%3d ", c);
607 } else {
608 printf("0x%.2x ", c);
609 }
610 }
611 printf("\n");
612 }
613 }
614}
615
James Zernc7e86ab2011-08-25 14:22:32 -0700616//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800617
618static int MyWriter(const uint8_t* data, size_t data_size,
619 const WebPPicture* const pic) {
620 FILE* const out = (FILE*)pic->custom_ptr;
621 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
622}
623
624// Dumps a picture as a PGM file using the IMC4 layout.
625static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
626 int y;
627 const int uv_width = (picture->width + 1) / 2;
628 const int uv_height = (picture->height + 1) / 2;
629 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700630 const int alpha_height = picture->a ? picture->height : 0;
631 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800632 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800633 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800634 fprintf(f, "P5\n%d %d\n255\n", stride, height);
635 for (y = 0; y < picture->height; ++y) {
636 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
637 return 0;
638 if (picture->width & 1) fputc(0, f); // pad
639 }
640 for (y = 0; y < uv_height; ++y) {
641 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
642 return 0;
643 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
644 return 0;
645 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700646 for (y = 0; y < alpha_height; ++y) {
647 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
648 return 0;
649 if (picture->width & 1) fputc(0, f); // pad
650 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800651 fclose(f);
652 return 1;
653}
654
James Zernc7e86ab2011-08-25 14:22:32 -0700655//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800656
Pascal Massimino30971c92011-12-01 02:24:50 -0800657static int ProgressReport(int percent, const WebPPicture* const picture) {
658 printf("[%s]: %3d %% \r",
659 (char*)picture->stats->user_data, percent);
660 fflush(stdout);
661 return 1; // all ok
662}
663
664//------------------------------------------------------------------------------
665
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700666static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800667 printf("Usage:\n\n");
668 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
669 printf("where quality is between 0 (poor) to 100 (very good).\n");
670 printf("Typical value is around 80.\n\n");
671 printf("Try -longhelp for an exhaustive list of advanced options.\n");
672}
673
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700674static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800675 printf("Usage:\n");
676 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
677 printf("If input size (-s) for an image is not specified, "
678 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700679#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800680 printf("Windows builds can take as input any of the files handled by WIC\n");
681#endif
682 printf("options:\n");
683 printf(" -h / -help ............ short help\n");
684 printf(" -H / -longhelp ........ long help\n");
685 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530686 printf(" -alpha_q <int> ......... Transparency-compression quality "
687 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 printf(" -preset <string> ....... Preset setting, one of:\n");
689 printf(" default, photo, picture,\n");
690 printf(" drawing, icon, text\n");
691 printf(" -preset must come first, as it overwrites other parameters.");
692 printf("\n");
693 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
694 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700695 printf(" -size <int> ............ Target size (in bytes)\n");
696 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800697 printf("\n");
698 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
699 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
700 printf(" -f <int> ............... filter strength (0=off..100)\n");
701 printf(" -sharpness <int> ....... "
702 "filter sharpness (0:most .. 7:least sharp)\n");
703 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700704 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
705 printf(" "
706 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800707 printf(" -pass <int> ............ analysis pass number (1..10)\n");
708 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700709 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
710#ifdef WEBP_EXPERIMENTAL_FEATURES
711 printf(" -444 / -422 / -gray ..... Change colorspace\n");
712#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800714 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
715 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800716 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530717 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800718 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
719 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000720 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530721 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700722
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800723 printf("\n");
724 printf(" -short ................. condense printed message\n");
725 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700726 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700727#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700728 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700729#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 printf(" -v ..................... verbose, e.g. print encoding/decoding "
731 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800732 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800733 printf("\n");
734 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800735 printf(" -af .................... auto-adjust filter strength.\n");
736 printf(" -pre <int> ............. pre-processing filter\n");
737 printf("\n");
738}
739
James Zernc7e86ab2011-08-25 14:22:32 -0700740//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700741// Error messages
742
743static const char* const kErrorMessages[] = {
744 "OK",
745 "OUT_OF_MEMORY: Out of memory allocating objects",
746 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
747 "NULL_PARAMETER: NULL parameter passed to function",
748 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700749 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
750 "allowed is 16383 pixels.",
751 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
752 "To reduce the size of this partition, try using less segments "
753 "with the -segments option, and eventually reduce the number of "
754 "header bits using -partition_limit. More details are available "
755 "in the manual (`man cwebp`)",
756 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800757 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800758 "FILE_TOO_BIG: File would be too big to fit in 4G",
759 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700760};
761
James Zernc7e86ab2011-08-25 14:22:32 -0700762//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800763
764int main(int argc, const char *argv[]) {
765 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
766 FILE *out = NULL;
767 int c;
768 int short_output = 0;
769 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530770 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800771 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700772 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800773 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800774 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800775 int print_distortion = 0; // 1=PSNR, 2=SSIM
776 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777 WebPConfig config;
778 WebPAuxStats stats;
779 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700780
Pascal Massiminod61479f2012-01-20 07:20:56 -0800781 if (!WebPPictureInit(&picture) ||
782 !WebPPictureInit(&original_picture) ||
783 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784 fprintf(stderr, "Error! Version mismatch!\n");
785 goto Error;
786 }
787
788 if (argc == 1) {
789 HelpShort();
790 return 0;
791 }
792
793 for (c = 1; c < argc; ++c) {
794 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
795 HelpShort();
796 return 0;
797 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
798 HelpLong();
799 return 0;
800 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
801 out_file = argv[++c];
802 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
803 dump_file = argv[++c];
804 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800805 } else if (!strcmp(argv[c], "-print_ssim")) {
806 config.show_compressed = 1;
807 print_distortion = 2;
808 } else if (!strcmp(argv[c], "-print_psnr")) {
809 config.show_compressed = 1;
810 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800811 } else if (!strcmp(argv[c], "-short")) {
812 short_output++;
813 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700814 picture.width = strtol(argv[++c], NULL, 0);
815 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800816 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700817 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800818 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200819 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530820 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
821 config.alpha_quality = strtol(argv[++c], NULL, 0);
822 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
823 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000824 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
825 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530826 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800827 ++c;
828 if (!strcmp(argv[c], "none")) {
829 config.alpha_filtering = 0;
830 } else if (!strcmp(argv[c], "fast")) {
831 config.alpha_filtering = 1;
832 } else if (!strcmp(argv[c], "best")) {
833 config.alpha_filtering = 2;
834 } else {
835 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
836 goto Error;
837 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530838 } else if (!strcmp(argv[c], "-noalpha")) {
839 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800840 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700841 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800842 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200843 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800844 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700845 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800846 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700847 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800848 } else if (!strcmp(argv[c], "-af")) {
849 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700850 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800851 config.filter_type = 1;
852 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700853 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800854 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700855 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800856 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700857 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800858 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700859 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700860 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
861 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800862 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700863 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700864#ifdef WEBP_EXPERIMENTAL_FEATURES
865 } else if (!strcmp(argv[c], "-444")) {
866 picture.colorspace = WEBP_YUV444;
867 } else if (!strcmp(argv[c], "-422")) {
868 picture.colorspace = WEBP_YUV422;
869 } else if (!strcmp(argv[c], "-gray")) {
870 picture.colorspace = WEBP_YUV400;
871#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800872 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
873 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700874 crop_x = strtol(argv[++c], NULL, 0);
875 crop_y = strtol(argv[++c], NULL, 0);
876 crop_w = strtol(argv[++c], NULL, 0);
877 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700878 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
879 resize_w = strtol(argv[++c], NULL, 0);
880 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700881#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700882 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000883 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700884#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700885 } else if (!strcmp(argv[c], "-version")) {
886 const int version = WebPGetEncoderVersion();
887 printf("%d.%d.%d\n",
888 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
889 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800890 } else if (!strcmp(argv[c], "-progress")) {
891 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800892 } else if (!strcmp(argv[c], "-quiet")) {
893 quiet = 1;
894 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
895 WebPPreset preset;
896 ++c;
897 if (!strcmp(argv[c], "default")) {
898 preset = WEBP_PRESET_DEFAULT;
899 } else if (!strcmp(argv[c], "photo")) {
900 preset = WEBP_PRESET_PHOTO;
901 } else if (!strcmp(argv[c], "picture")) {
902 preset = WEBP_PRESET_PICTURE;
903 } else if (!strcmp(argv[c], "drawing")) {
904 preset = WEBP_PRESET_DRAWING;
905 } else if (!strcmp(argv[c], "icon")) {
906 preset = WEBP_PRESET_ICON;
907 } else if (!strcmp(argv[c], "text")) {
908 preset = WEBP_PRESET_TEXT;
909 } else {
910 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
911 goto Error;
912 }
913 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700914 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800915 goto Error;
916 }
917 } else if (!strcmp(argv[c], "-v")) {
918 verbose = 1;
919 } else if (argv[c][0] == '-') {
920 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
921 HelpLong();
922 return -1;
923 } else {
924 in_file = argv[c];
925 }
926 }
927
928 if (!WebPValidateConfig(&config)) {
929 fprintf(stderr, "Error! Invalid configuration.\n");
930 goto Error;
931 }
932
Pascal Massimino0744e842011-02-25 12:03:27 -0800933 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700934 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800935 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700936 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700937 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800938 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700939 goto Error;
940 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800941 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
942
Pascal Massimino0744e842011-02-25 12:03:27 -0800943 if (verbose) {
944 const double time = StopwatchReadAndReset(&stop_watch);
945 fprintf(stderr, "Time to read input: %.3fs\n", time);
946 }
947
948 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800949 if (out_file) {
950 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800951 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800952 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
953 goto Error;
954 } else {
955 if (!short_output && !quiet) {
956 fprintf(stderr, "Saving file '%s'\n", out_file);
957 }
958 }
959 picture.writer = MyWriter;
960 picture.custom_ptr = (void*)out;
961 } else {
962 out = NULL;
963 if (!quiet && !short_output) {
964 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
965 fprintf(stderr, "be performed, but its results discarded.\n\n");
966 }
967 }
968 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -0800969 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800970
Pascal Massimino0744e842011-02-25 12:03:27 -0800971 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700972 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800973 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700974 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700975 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
976 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800977 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700978 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700979 if ((resize_w | resize_h) > 0) {
980 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
981 fprintf(stderr, "Error! Cannot resize picture\n");
982 goto Error;
983 }
984 }
985 if (picture.extra_info_type > 0) {
986 AllocExtraInfo(&picture);
987 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800988 if (print_distortion > 0) { // Save original picture for later comparison
989 WebPPictureCopy(&picture, &original_picture);
990 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700991 if (!WebPEncode(&config, &picture)) {
992 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700993 fprintf(stderr, "Error code: %d (%s)\n",
994 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700995 goto Error;
996 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800997 if (verbose) {
998 const double time = StopwatchReadAndReset(&stop_watch);
999 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1000 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001001
1002 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001003 if (dump_file) {
1004 DumpPicture(&picture, dump_file);
1005 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001006
Pascal Massimino38369c02011-05-04 22:59:51 -07001007 if (!quiet) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001008 PrintExtraInfo(&picture, short_output, in_file);
1009 }
1010 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1011 float values[5];
1012 WebPPictureDistortion(&picture, &original_picture,
1013 (print_distortion == 1) ? 0 : 1, values);
1014 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1015 (print_distortion == 1) ? "PSNR" : "SSIM",
1016 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001017 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001018
1019 Error:
1020 free(picture.extra_info);
1021 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001022 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001023 if (out != NULL) {
1024 fclose(out);
1025 }
1026
1027 return 0;
1028}
1029
James Zernc7e86ab2011-08-25 14:22:32 -07001030//------------------------------------------------------------------------------