blob: 9b4452d208080454d28ae975f0928567cf7fa4e0 [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#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080042
43
44#include "webp/encode.h"
45#include "stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070046#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070047#if defined(__cplusplus) || defined(c_plusplus)
48extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070049#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070050
James Zern04e84cf2011-11-04 15:20:08 -070051extern void* VP8GetCPUInfo; // opaque forward declaration.
52
53#if defined(__cplusplus) || defined(c_plusplus)
54} // extern "C"
55#endif
56#endif // WEBP_DLL
57
James Zernc7e86ab2011-08-25 14:22:32 -070058//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080059
60static int verbose = 0;
61
62static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
63 const int uv_width = (pic->width + 1) / 2;
64 const int uv_height = (pic->height + 1) / 2;
65 int y;
66 int ok = 0;
67
68 if (!WebPPictureAlloc(pic)) return ok;
69
70 for (y = 0; y < pic->height; ++y) {
71 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
72 goto End;
73 }
74 }
75 for (y = 0; y < uv_height; ++y) {
76 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
77 goto End;
78 }
79 for (y = 0; y < uv_height; ++y) {
80 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
81 goto End;
82 }
83 ok = 1;
84
85 End:
86 return ok;
87}
88
James Zern31f9dc62011-06-06 17:56:50 -070089#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080090
91#define IFS(fn) \
92 do { \
93 if (SUCCEEDED(hr)) \
94 { \
95 hr = (fn); \
96 if (FAILED(hr) && verbose) \
James Zern974aaff2012-01-24 12:46:46 -080097 fprintf(stderr, #fn " failed %08x\n", hr); \
Pascal Massiminof61d14a2011-02-18 23:33:46 -080098 } \
99 } while (0)
100
James Zern902d3e32012-05-08 17:49:39 -0700101// modified version of DEFINE_GUID from guiddef.h.
102#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
103 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
104
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800105#ifdef __cplusplus
106#define MAKE_REFGUID(x) (x)
107#else
108#define MAKE_REFGUID(x) &(x)
109#endif
110
111static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
112 HRESULT hr = S_OK;
113 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
114 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800115 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800116 return hr;
117}
118
119static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700120 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800121 HRESULT hr = S_OK;
122 IWICBitmapFrameDecode* pFrame = NULL;
123 IWICFormatConverter* pConverter = NULL;
124 IWICImagingFactory* pFactory = NULL;
125 IWICBitmapDecoder* pDecoder = NULL;
126 IStream* pStream = NULL;
127 UINT frameCount = 0;
128 UINT width, height = 0;
129 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700130 WICPixelFormatGUID srcPixelFormat = { 0 };
131 GUID srcContainerFormat = { 0 };
132 const GUID* alphaContainers[] = {
133 &GUID_ContainerFormatBmp,
134 &GUID_ContainerFormatPng,
135 &GUID_ContainerFormatTiff
136 };
137 int has_alpha = 0;
138 int i, stride;
James Zern902d3e32012-05-08 17:49:39 -0700139 // From Microsoft SDK 7.0a
140 // Create local copies for compatibility when building against earlier
141 // versions of the SDK.
142 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
143 0x6fddc324, 0x4e03, 0x4bfe,
144 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
145 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
146 0xf5c7ad2d, 0x6a8d, 0x43dd,
147 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
148 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
149 0x6fddc324, 0x4e03, 0x4bfe,
150 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800151
152 IFS(CoInitialize(NULL));
153 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
154 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
155 (LPVOID*)&pFactory));
156 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800157 fprintf(stderr,
158 "Couldn't access Windows Imaging Component (are you running "
159 "Windows XP SP3 or newer?). Most formats not available. "
160 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800161 }
162 // Prepare for image decoding.
163 IFS(OpenInputStream(filename, &pStream));
164 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
165 WICDecodeMetadataCacheOnDemand, &pDecoder));
166 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
167 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800168 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800169 hr = E_FAIL;
170 }
171 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700172 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
173 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
174
175 has_alpha = keep_alpha;
176 for (i = 0;
177 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
178 ++i) {
179 if (IsEqualGUID(&srcContainerFormat, alphaContainers[i])) {
180 has_alpha =
James Zern902d3e32012-05-08 17:49:39 -0700181 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppRGBA_) ||
182 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppBGRA_);
James Zern3cfe0882011-06-21 18:29:30 -0700183 break;
184 }
185 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800186
187 // Prepare for pixel format conversion (if necessary).
188 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
189 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700190 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
191 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700192 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800193 NULL, 0.0, WICBitmapPaletteTypeCustom));
194
195 // Decode.
196 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700197 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800198 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700199 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800200 if (rgb == NULL)
201 hr = E_OUTOFMEMORY;
202 }
James Zern3cfe0882011-06-21 18:29:30 -0700203 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
204 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800205
206 // WebP conversion.
207 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700208 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800209 pic->width = width;
210 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700211 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
212 : WebPPictureImportRGB(pic, rgb, stride);
213 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800214 hr = E_FAIL;
215 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000216 if (SUCCEEDED(hr)) {
217 if (has_alpha && keep_alpha == 2) {
218 WebPCleanupTransparentArea(pic);
219 }
220 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800221
222 // Cleanup.
223 if (pConverter != NULL) IUnknown_Release(pConverter);
224 if (pFrame != NULL) IUnknown_Release(pFrame);
225 if (pDecoder != NULL) IUnknown_Release(pDecoder);
226 if (pFactory != NULL) IUnknown_Release(pFactory);
227 if (pStream != NULL) IUnknown_Release(pStream);
228 free(rgb);
229 return hr;
230}
231
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700232static int ReadPicture(const char* const filename, WebPPicture* const pic,
233 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800234 int ok;
235 if (pic->width != 0 && pic->height != 0) {
236 // If image size is specified, infer it as YUV format.
237 FILE* in_file = fopen(filename, "rb");
238 if (in_file == NULL) {
239 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
240 return 0;
241 }
242 ok = ReadYUV(in_file, pic);
243 fclose(in_file);
244 } else {
245 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700246 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800247 }
248 if (!ok) {
249 fprintf(stderr, "Error! Could not process file %s\n", filename);
250 }
251 return ok;
252}
253
James Zern31f9dc62011-06-06 17:56:50 -0700254#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800255
256#ifdef WEBP_HAVE_JPEG
257struct my_error_mgr {
258 struct jpeg_error_mgr pub;
259 jmp_buf setjmp_buffer;
260};
261
262static void my_error_exit(j_common_ptr dinfo) {
263 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
264 (*dinfo->err->output_message) (dinfo);
265 longjmp(myerr->setjmp_buffer, 1);
266}
267
268static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
269 int ok = 0;
270 int stride, width, height;
271 uint8_t* rgb = NULL;
272 uint8_t* row_ptr = NULL;
273 struct jpeg_decompress_struct dinfo;
274 struct my_error_mgr jerr;
275 JSAMPARRAY buffer;
276
277 dinfo.err = jpeg_std_error(&jerr.pub);
278 jerr.pub.error_exit = my_error_exit;
279
James Zern04e84cf2011-11-04 15:20:08 -0700280 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800281 Error:
282 jpeg_destroy_decompress(&dinfo);
283 goto End;
284 }
285
286 jpeg_create_decompress(&dinfo);
287 jpeg_stdio_src(&dinfo, in_file);
288 jpeg_read_header(&dinfo, TRUE);
289
290 dinfo.out_color_space = JCS_RGB;
291 dinfo.dct_method = JDCT_IFAST;
292 dinfo.do_fancy_upsampling = TRUE;
293
294 jpeg_start_decompress(&dinfo);
295
296 if (dinfo.output_components != 3) {
297 goto Error;
298 }
299
300 width = dinfo.output_width;
301 height = dinfo.output_height;
302 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
303
304 rgb = (uint8_t*)malloc(stride * height);
305 if (rgb == NULL) {
306 goto End;
307 }
308 row_ptr = rgb;
309
310 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
311 JPOOL_IMAGE, stride, 1);
312 if (buffer == NULL) {
313 goto End;
314 }
315
316 while (dinfo.output_scanline < dinfo.output_height) {
317 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
318 goto End;
319 }
320 memcpy(row_ptr, buffer[0], stride);
321 row_ptr += stride;
322 }
323
James Zern04e84cf2011-11-04 15:20:08 -0700324 jpeg_finish_decompress(&dinfo);
325 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800326
327 // WebP conversion.
328 pic->width = width;
329 pic->height = height;
330 ok = WebPPictureImportRGB(pic, rgb, stride);
331
332 End:
333 if (rgb) {
334 free(rgb);
335 }
336 return ok;
337}
338
339#else
340static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400341 (void)in_file;
342 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800343 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
344 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800345 return 0;
346}
347#endif
348
349#ifdef WEBP_HAVE_PNG
350static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700351 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800352 longjmp(png_jmpbuf(png), 1);
353}
354
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700355static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800356 png_structp png;
357 png_infop info;
358 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700359 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800360 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700361 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800362 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700363 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800364 int stride;
365 uint8_t* rgb = NULL;
366
367 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
368 if (png == NULL) {
369 goto End;
370 }
371
372 png_set_error_fn(png, 0, error_function, NULL);
373 if (setjmp(png_jmpbuf(png))) {
374 Error:
375 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700376 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800377 goto End;
378 }
379
380 info = png_create_info_struct(png);
381 if (info == NULL) goto Error;
382
383 png_init_io(png, in_file);
384 png_read_info(png, info);
385 if (!png_get_IHDR(png, info,
386 &width, &height, &bit_depth, &color_type, &interlaced,
387 NULL, NULL)) goto Error;
388
389 png_set_strip_16(png);
390 png_set_packing(png);
391 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000392 if (color_type == PNG_COLOR_TYPE_GRAY ||
393 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800394 if (bit_depth < 8) {
395 png_set_expand_gray_1_2_4_to_8(png);
396 }
397 png_set_gray_to_rgb(png);
398 }
399 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
400 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700401 has_alpha = 1;
402 } else {
403 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800404 }
405
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700406 if (!keep_alpha) {
407 png_set_strip_alpha(png);
408 has_alpha = 0;
409 }
410
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800411 num_passes = png_set_interlace_handling(png);
412 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700413 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800414 rgb = (uint8_t*)malloc(stride * height);
415 if (rgb == NULL) goto Error;
416 for (p = 0; p < num_passes; ++p) {
417 for (y = 0; y < height; ++y) {
418 png_bytep row = rgb + y * stride;
419 png_read_rows(png, &row, NULL, 1);
420 }
421 }
422 png_read_end(png, info);
423 png_destroy_read_struct(&png, &info, NULL);
424
425 pic->width = width;
426 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700427 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
428 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800429 free(rgb);
430
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000431 if (ok && has_alpha && keep_alpha == 2) {
432 WebPCleanupTransparentArea(pic);
433 }
434
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800435 End:
436 return ok;
437}
438#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700439static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400440 (void)in_file;
441 (void)pic;
442 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800443 fprintf(stderr, "PNG support not compiled. Please install the libpng "
444 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800445 return 0;
446}
447#endif
448
449typedef enum {
450 PNG = 0,
451 JPEG,
James Zerna0b27362012-01-27 17:39:47 -0800452 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800453} InputFileFormat;
454
455static InputFileFormat GetImageType(FILE* in_file) {
456 InputFileFormat format = UNSUPPORTED;
457 unsigned int magic;
458 unsigned char buf[4];
459
460 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
461 (fseek(in_file, 0, SEEK_SET) != 0)) {
462 return format;
463 }
464
465 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
466 if (magic == 0x89504E47U) {
467 format = PNG;
468 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
469 format = JPEG;
470 }
471 return format;
472}
473
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700474static int ReadPicture(const char* const filename, WebPPicture* const pic,
475 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800476 int ok = 0;
477 FILE* in_file = fopen(filename, "rb");
478 if (in_file == NULL) {
479 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
480 return ok;
481 }
482
483 if (pic->width == 0 || pic->height == 0) {
484 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
485 const InputFileFormat format = GetImageType(in_file);
486 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700487 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800488 } else if (format == JPEG) {
489 ok = ReadJPEG(in_file, pic);
490 }
491 } else {
492 // If image size is specified, infer it as YUV format.
493 ok = ReadYUV(in_file, pic);
494 }
495 if (!ok) {
496 fprintf(stderr, "Error! Could not process file %s\n", filename);
497 }
498
499 fclose(in_file);
500 return ok;
501}
502
James Zern31f9dc62011-06-06 17:56:50 -0700503#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800504
505static void AllocExtraInfo(WebPPicture* const pic) {
506 const int mb_w = (pic->width + 15) / 16;
507 const int mb_h = (pic->height + 15) / 16;
508 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
509}
510
511static void PrintByteCount(const int bytes[4], int total_size,
512 int* const totals) {
513 int s;
514 int total = 0;
515 for (s = 0; s < 4; ++s) {
516 fprintf(stderr, "| %7d ", bytes[s]);
517 total += bytes[s];
518 if (totals) totals[s] += bytes[s];
519 }
James Zern04e84cf2011-11-04 15:20:08 -0700520 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800521}
522
523static void PrintPercents(const int counts[4], int total) {
524 int s;
525 for (s = 0; s < 4; ++s) {
526 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
527 }
James Zern04e84cf2011-11-04 15:20:08 -0700528 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800529}
530
531static void PrintValues(const int values[4]) {
532 int s;
533 for (s = 0; s < 4; ++s) {
534 fprintf(stderr, "| %7d ", values[s]);
535 }
James Zern04e84cf2011-11-04 15:20:08 -0700536 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800537}
538
Vikas Arorac4ccab62012-05-09 11:27:46 +0530539static void PrintExtraInfoLossless(const WebPPicture* const pic,
540 int short_output,
541 const char* const file_name) {
542 const WebPAuxStats* const stats = pic->stats;
543 if (short_output) {
544 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
545 } else {
546 fprintf(stderr, "File: %s\n", file_name);
547 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
548 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
549 }
550}
551
552static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
553 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800554 const WebPAuxStats* const stats = pic->stats;
555 if (short_output) {
556 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700557 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800558 const int num_i4 = stats->block_count[0];
559 const int num_i16 = stats->block_count[1];
560 const int num_skip = stats->block_count[2];
561 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800562 fprintf(stderr, "File: %s\n", file_name);
563 fprintf(stderr, "Dimension: %d x %d%s\n",
564 pic->width, pic->height, (pic->a != NULL) ? " (with alpha)" : "");
565 fprintf(stderr, "Output: "
566 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800567 stats->coded_size,
568 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
569 if (total > 0) {
570 int totals[4] = { 0, 0, 0, 0 };
571 fprintf(stderr, "block count: intra4: %d\n"
572 " intra16: %d (-> %.2f%%)\n",
573 num_i4, num_i16, 100.f * num_i16 / total);
574 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
575 num_skip, 100.f * num_skip / total);
576 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
577 " mode-partition: %6d (%.1f%%)\n",
578 stats->header_bytes[0],
579 100.f * stats->header_bytes[0] / stats->coded_size,
580 stats->header_bytes[1],
581 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700582 if (stats->alpha_data_size) {
583 fprintf(stderr, " transparency: %6d\n",
584 stats->alpha_data_size);
585 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700586 if (stats->layer_data_size) {
587 fprintf(stderr, " enhancement: %6d\n",
588 stats->layer_data_size);
589 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800590 fprintf(stderr, " Residuals bytes "
591 "|segment 1|segment 2|segment 3"
592 "|segment 4| total\n");
593 fprintf(stderr, " intra4-coeffs: ");
594 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
595 fprintf(stderr, " intra16-coeffs: ");
596 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
597 fprintf(stderr, " chroma coeffs: ");
598 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
599 fprintf(stderr, " macroblocks: ");
600 PrintPercents(stats->segment_size, total);
601 fprintf(stderr, " quantizer: ");
602 PrintValues(stats->segment_quant);
603 fprintf(stderr, " filter level: ");
604 PrintValues(stats->segment_level);
605 fprintf(stderr, "------------------+---------");
606 fprintf(stderr, "+---------+---------+---------+-----------------\n");
607 fprintf(stderr, " segments total: ");
608 PrintByteCount(totals, stats->coded_size, NULL);
609 }
610 }
611 if (pic->extra_info) {
612 const int mb_w = (pic->width + 15) / 16;
613 const int mb_h = (pic->height + 15) / 16;
614 const int type = pic->extra_info_type;
615 int x, y;
616 for (y = 0; y < mb_h; ++y) {
617 for (x = 0; x < mb_w; ++x) {
618 const int c = pic->extra_info[x + y * mb_w];
619 if (type == 1) { // intra4/intra16
620 printf("%c", "+."[c]);
621 } else if (type == 2) { // segments
622 printf("%c", ".-*X"[c]);
623 } else if (type == 3) { // quantizers
624 printf("%.2d ", c);
625 } else if (type == 6 || type == 7) {
626 printf("%3d ", c);
627 } else {
628 printf("0x%.2x ", c);
629 }
630 }
631 printf("\n");
632 }
633 }
634}
635
James Zernc7e86ab2011-08-25 14:22:32 -0700636//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800637
638static int MyWriter(const uint8_t* data, size_t data_size,
639 const WebPPicture* const pic) {
640 FILE* const out = (FILE*)pic->custom_ptr;
641 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
642}
643
644// Dumps a picture as a PGM file using the IMC4 layout.
645static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
646 int y;
647 const int uv_width = (picture->width + 1) / 2;
648 const int uv_height = (picture->height + 1) / 2;
649 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700650 const int alpha_height = picture->a ? picture->height : 0;
651 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800652 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800653 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800654 fprintf(f, "P5\n%d %d\n255\n", stride, height);
655 for (y = 0; y < picture->height; ++y) {
656 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
657 return 0;
658 if (picture->width & 1) fputc(0, f); // pad
659 }
660 for (y = 0; y < uv_height; ++y) {
661 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
662 return 0;
663 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
664 return 0;
665 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700666 for (y = 0; y < alpha_height; ++y) {
667 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
668 return 0;
669 if (picture->width & 1) fputc(0, f); // pad
670 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800671 fclose(f);
672 return 1;
673}
674
James Zernc7e86ab2011-08-25 14:22:32 -0700675//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800676
Pascal Massimino30971c92011-12-01 02:24:50 -0800677static int ProgressReport(int percent, const WebPPicture* const picture) {
678 printf("[%s]: %3d %% \r",
679 (char*)picture->stats->user_data, percent);
680 fflush(stdout);
681 return 1; // all ok
682}
683
684//------------------------------------------------------------------------------
685
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700686static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800687 printf("Usage:\n\n");
688 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
689 printf("where quality is between 0 (poor) to 100 (very good).\n");
690 printf("Typical value is around 80.\n\n");
691 printf("Try -longhelp for an exhaustive list of advanced options.\n");
692}
693
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700694static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800695 printf("Usage:\n");
696 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
697 printf("If input size (-s) for an image is not specified, "
698 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700699#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 printf("Windows builds can take as input any of the files handled by WIC\n");
701#endif
702 printf("options:\n");
703 printf(" -h / -help ............ short help\n");
704 printf(" -H / -longhelp ........ long help\n");
705 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530706 printf(" -alpha_q <int> ......... Transparency-compression quality "
707 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800708 printf(" -preset <string> ....... Preset setting, one of:\n");
709 printf(" default, photo, picture,\n");
710 printf(" drawing, icon, text\n");
711 printf(" -preset must come first, as it overwrites other parameters.");
712 printf("\n");
713 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
714 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700715 printf(" -size <int> ............ Target size (in bytes)\n");
716 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800717 printf("\n");
718 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
719 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
720 printf(" -f <int> ............... filter strength (0=off..100)\n");
721 printf(" -sharpness <int> ....... "
722 "filter sharpness (0:most .. 7:least sharp)\n");
723 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700724 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
725 printf(" "
726 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800727 printf(" -pass <int> ............ analysis pass number (1..10)\n");
728 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700729 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
730#ifdef WEBP_EXPERIMENTAL_FEATURES
731 printf(" -444 / -422 / -gray ..... Change colorspace\n");
732#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800733 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800734 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
735 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800736 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530737 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800738 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
739 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000740 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530741 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700742
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800743 printf("\n");
744 printf(" -short ................. condense printed message\n");
745 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700746 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700747#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700748 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700749#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800750 printf(" -v ..................... verbose, e.g. print encoding/decoding "
751 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800752 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753 printf("\n");
754 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800755 printf(" -af .................... auto-adjust filter strength.\n");
756 printf(" -pre <int> ............. pre-processing filter\n");
757 printf("\n");
758}
759
James Zernc7e86ab2011-08-25 14:22:32 -0700760//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700761// Error messages
762
763static const char* const kErrorMessages[] = {
764 "OK",
765 "OUT_OF_MEMORY: Out of memory allocating objects",
766 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
767 "NULL_PARAMETER: NULL parameter passed to function",
768 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700769 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
770 "allowed is 16383 pixels.",
771 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
772 "To reduce the size of this partition, try using less segments "
773 "with the -segments option, and eventually reduce the number of "
774 "header bits using -partition_limit. More details are available "
775 "in the manual (`man cwebp`)",
776 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800777 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800778 "FILE_TOO_BIG: File would be too big to fit in 4G",
779 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700780};
781
James Zernc7e86ab2011-08-25 14:22:32 -0700782//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800783
784int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700785 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800786 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
787 FILE *out = NULL;
788 int c;
789 int short_output = 0;
790 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530791 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800792 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700793 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800794 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800795 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800796 int print_distortion = 0; // 1=PSNR, 2=SSIM
797 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800798 WebPConfig config;
799 WebPAuxStats stats;
800 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700801
Pascal Massiminod61479f2012-01-20 07:20:56 -0800802 if (!WebPPictureInit(&picture) ||
803 !WebPPictureInit(&original_picture) ||
804 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800805 fprintf(stderr, "Error! Version mismatch!\n");
806 goto Error;
807 }
808
809 if (argc == 1) {
810 HelpShort();
811 return 0;
812 }
813
814 for (c = 1; c < argc; ++c) {
815 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
816 HelpShort();
817 return 0;
818 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
819 HelpLong();
820 return 0;
821 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
822 out_file = argv[++c];
823 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
824 dump_file = argv[++c];
825 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800826 } else if (!strcmp(argv[c], "-print_ssim")) {
827 config.show_compressed = 1;
828 print_distortion = 2;
829 } else if (!strcmp(argv[c], "-print_psnr")) {
830 config.show_compressed = 1;
831 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800832 } else if (!strcmp(argv[c], "-short")) {
833 short_output++;
834 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700835 picture.width = strtol(argv[++c], NULL, 0);
836 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800837 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700838 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800839 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200840 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530841 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
842 config.alpha_quality = strtol(argv[++c], NULL, 0);
843 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
844 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000845 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
846 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530847 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800848 ++c;
849 if (!strcmp(argv[c], "none")) {
850 config.alpha_filtering = 0;
851 } else if (!strcmp(argv[c], "fast")) {
852 config.alpha_filtering = 1;
853 } else if (!strcmp(argv[c], "best")) {
854 config.alpha_filtering = 2;
855 } else {
856 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
857 goto Error;
858 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530859 } else if (!strcmp(argv[c], "-noalpha")) {
860 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000861#ifdef USE_LOSSLESS_ENCODER
862 } else if (!strcmp(argv[c], "-lossless")) {
863 config.lossless = 1;
864 picture.use_argb_input = 1;
865#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800866 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700867 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800868 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200869 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800870 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700871 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800872 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700873 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800874 } else if (!strcmp(argv[c], "-af")) {
875 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700876 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800877 config.filter_type = 1;
878 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700879 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800880 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700881 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800882 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700883 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800884 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700885 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700886 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
887 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800888 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700889 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700890#ifdef WEBP_EXPERIMENTAL_FEATURES
891 } else if (!strcmp(argv[c], "-444")) {
892 picture.colorspace = WEBP_YUV444;
893 } else if (!strcmp(argv[c], "-422")) {
894 picture.colorspace = WEBP_YUV422;
895 } else if (!strcmp(argv[c], "-gray")) {
896 picture.colorspace = WEBP_YUV400;
897#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800898 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
899 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700900 crop_x = strtol(argv[++c], NULL, 0);
901 crop_y = strtol(argv[++c], NULL, 0);
902 crop_w = strtol(argv[++c], NULL, 0);
903 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700904 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
905 resize_w = strtol(argv[++c], NULL, 0);
906 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700907#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700908 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000909 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700910#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700911 } else if (!strcmp(argv[c], "-version")) {
912 const int version = WebPGetEncoderVersion();
913 printf("%d.%d.%d\n",
914 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
915 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800916 } else if (!strcmp(argv[c], "-progress")) {
917 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800918 } else if (!strcmp(argv[c], "-quiet")) {
919 quiet = 1;
920 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
921 WebPPreset preset;
922 ++c;
923 if (!strcmp(argv[c], "default")) {
924 preset = WEBP_PRESET_DEFAULT;
925 } else if (!strcmp(argv[c], "photo")) {
926 preset = WEBP_PRESET_PHOTO;
927 } else if (!strcmp(argv[c], "picture")) {
928 preset = WEBP_PRESET_PICTURE;
929 } else if (!strcmp(argv[c], "drawing")) {
930 preset = WEBP_PRESET_DRAWING;
931 } else if (!strcmp(argv[c], "icon")) {
932 preset = WEBP_PRESET_ICON;
933 } else if (!strcmp(argv[c], "text")) {
934 preset = WEBP_PRESET_TEXT;
935 } else {
936 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
937 goto Error;
938 }
939 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700940 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800941 goto Error;
942 }
943 } else if (!strcmp(argv[c], "-v")) {
944 verbose = 1;
945 } else if (argv[c][0] == '-') {
946 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
947 HelpLong();
948 return -1;
949 } else {
950 in_file = argv[c];
951 }
952 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700953 if (in_file == NULL) {
954 fprintf(stderr, "No input file specified!\n");
955 HelpShort();
956 goto Error;
957 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800958
959 if (!WebPValidateConfig(&config)) {
960 fprintf(stderr, "Error! Invalid configuration.\n");
961 goto Error;
962 }
963
Pascal Massimino0744e842011-02-25 12:03:27 -0800964 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700965 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800966 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700967 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700968 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800969 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700970 goto Error;
971 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800972 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
973
Pascal Massimino0744e842011-02-25 12:03:27 -0800974 if (verbose) {
975 const double time = StopwatchReadAndReset(&stop_watch);
976 fprintf(stderr, "Time to read input: %.3fs\n", time);
977 }
978
979 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800980 if (out_file) {
981 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800982 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800983 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
984 goto Error;
985 } else {
986 if (!short_output && !quiet) {
987 fprintf(stderr, "Saving file '%s'\n", out_file);
988 }
989 }
990 picture.writer = MyWriter;
991 picture.custom_ptr = (void*)out;
992 } else {
993 out = NULL;
994 if (!quiet && !short_output) {
995 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
996 fprintf(stderr, "be performed, but its results discarded.\n\n");
997 }
998 }
999 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -08001000 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001001
Pascal Massimino0744e842011-02-25 12:03:27 -08001002 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001003 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001004 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001005 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001006 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
1007 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001008 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -07001009 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001010 if ((resize_w | resize_h) > 0) {
1011 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1012 fprintf(stderr, "Error! Cannot resize picture\n");
1013 goto Error;
1014 }
1015 }
1016 if (picture.extra_info_type > 0) {
1017 AllocExtraInfo(&picture);
1018 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001019 if (print_distortion > 0) { // Save original picture for later comparison
1020 WebPPictureCopy(&picture, &original_picture);
1021 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001022 if (!WebPEncode(&config, &picture)) {
1023 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001024 fprintf(stderr, "Error code: %d (%s)\n",
1025 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001026 goto Error;
1027 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001028 if (verbose) {
1029 const double time = StopwatchReadAndReset(&stop_watch);
1030 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1031 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001032
1033 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001034 if (dump_file) {
1035 DumpPicture(&picture, dump_file);
1036 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001037
Pascal Massimino38369c02011-05-04 22:59:51 -07001038 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301039 if (config.lossless) {
1040 PrintExtraInfoLossless(&picture, short_output, in_file);
1041 } else {
1042 PrintExtraInfoLossy(&picture, short_output, in_file);
1043 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001044 }
1045 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1046 float values[5];
1047 WebPPictureDistortion(&picture, &original_picture,
1048 (print_distortion == 1) ? 0 : 1, values);
1049 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1050 (print_distortion == 1) ? "PSNR" : "SSIM",
1051 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001052 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001053 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001054
1055 Error:
1056 free(picture.extra_info);
1057 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001058 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001059 if (out != NULL) {
1060 fclose(out);
1061 }
1062
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001063 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001064}
1065
James Zernc7e86ab2011-08-25 14:22:32 -07001066//------------------------------------------------------------------------------