blob: 7e7e4b53e3caa915c27779a5abdd014ca76b1238 [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"
James Zern00b29e22012-05-15 13:48:11 -070045#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) {
James Zern1d38b252012-05-08 18:09:42 -0700179 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
180 MAKE_REFGUID(*alphaContainers[i]))) {
James Zern3cfe0882011-06-21 18:29:30 -0700181 has_alpha =
James Zern1d38b252012-05-08 18:09:42 -0700182 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
183 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
184 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
185 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
James Zern3cfe0882011-06-21 18:29:30 -0700186 break;
187 }
188 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800189
190 // Prepare for pixel format conversion (if necessary).
191 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
192 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700193 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
194 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700195 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800196 NULL, 0.0, WICBitmapPaletteTypeCustom));
197
198 // Decode.
199 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700200 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800201 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700202 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800203 if (rgb == NULL)
204 hr = E_OUTOFMEMORY;
205 }
James Zern3cfe0882011-06-21 18:29:30 -0700206 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
207 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800208
209 // WebP conversion.
210 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700211 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800212 pic->width = width;
213 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700214 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
215 : WebPPictureImportRGB(pic, rgb, stride);
216 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800217 hr = E_FAIL;
218 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000219 if (SUCCEEDED(hr)) {
220 if (has_alpha && keep_alpha == 2) {
221 WebPCleanupTransparentArea(pic);
222 }
223 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800224
225 // Cleanup.
226 if (pConverter != NULL) IUnknown_Release(pConverter);
227 if (pFrame != NULL) IUnknown_Release(pFrame);
228 if (pDecoder != NULL) IUnknown_Release(pDecoder);
229 if (pFactory != NULL) IUnknown_Release(pFactory);
230 if (pStream != NULL) IUnknown_Release(pStream);
231 free(rgb);
232 return hr;
233}
234
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700235static int ReadPicture(const char* const filename, WebPPicture* const pic,
236 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800237 int ok;
238 if (pic->width != 0 && pic->height != 0) {
239 // If image size is specified, infer it as YUV format.
240 FILE* in_file = fopen(filename, "rb");
241 if (in_file == NULL) {
242 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
243 return 0;
244 }
245 ok = ReadYUV(in_file, pic);
246 fclose(in_file);
247 } else {
248 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700249 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800250 }
251 if (!ok) {
252 fprintf(stderr, "Error! Could not process file %s\n", filename);
253 }
254 return ok;
255}
256
James Zern31f9dc62011-06-06 17:56:50 -0700257#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800258
259#ifdef WEBP_HAVE_JPEG
260struct my_error_mgr {
261 struct jpeg_error_mgr pub;
262 jmp_buf setjmp_buffer;
263};
264
265static void my_error_exit(j_common_ptr dinfo) {
266 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
267 (*dinfo->err->output_message) (dinfo);
268 longjmp(myerr->setjmp_buffer, 1);
269}
270
271static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
272 int ok = 0;
273 int stride, width, height;
274 uint8_t* rgb = NULL;
275 uint8_t* row_ptr = NULL;
276 struct jpeg_decompress_struct dinfo;
277 struct my_error_mgr jerr;
278 JSAMPARRAY buffer;
279
280 dinfo.err = jpeg_std_error(&jerr.pub);
281 jerr.pub.error_exit = my_error_exit;
282
James Zern04e84cf2011-11-04 15:20:08 -0700283 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800284 Error:
285 jpeg_destroy_decompress(&dinfo);
286 goto End;
287 }
288
289 jpeg_create_decompress(&dinfo);
290 jpeg_stdio_src(&dinfo, in_file);
291 jpeg_read_header(&dinfo, TRUE);
292
293 dinfo.out_color_space = JCS_RGB;
294 dinfo.dct_method = JDCT_IFAST;
295 dinfo.do_fancy_upsampling = TRUE;
296
297 jpeg_start_decompress(&dinfo);
298
299 if (dinfo.output_components != 3) {
300 goto Error;
301 }
302
303 width = dinfo.output_width;
304 height = dinfo.output_height;
305 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
306
307 rgb = (uint8_t*)malloc(stride * height);
308 if (rgb == NULL) {
309 goto End;
310 }
311 row_ptr = rgb;
312
313 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
314 JPOOL_IMAGE, stride, 1);
315 if (buffer == NULL) {
316 goto End;
317 }
318
319 while (dinfo.output_scanline < dinfo.output_height) {
320 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
321 goto End;
322 }
323 memcpy(row_ptr, buffer[0], stride);
324 row_ptr += stride;
325 }
326
James Zern04e84cf2011-11-04 15:20:08 -0700327 jpeg_finish_decompress(&dinfo);
328 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800329
330 // WebP conversion.
331 pic->width = width;
332 pic->height = height;
333 ok = WebPPictureImportRGB(pic, rgb, stride);
334
335 End:
336 if (rgb) {
337 free(rgb);
338 }
339 return ok;
340}
341
342#else
343static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400344 (void)in_file;
345 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800346 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
347 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800348 return 0;
349}
350#endif
351
352#ifdef WEBP_HAVE_PNG
353static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700354 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800355 longjmp(png_jmpbuf(png), 1);
356}
357
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700358static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800359 png_structp png;
360 png_infop info;
361 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700362 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800363 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700364 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800365 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700366 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800367 int stride;
368 uint8_t* rgb = NULL;
369
370 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
371 if (png == NULL) {
372 goto End;
373 }
374
375 png_set_error_fn(png, 0, error_function, NULL);
376 if (setjmp(png_jmpbuf(png))) {
377 Error:
378 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700379 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800380 goto End;
381 }
382
383 info = png_create_info_struct(png);
384 if (info == NULL) goto Error;
385
386 png_init_io(png, in_file);
387 png_read_info(png, info);
388 if (!png_get_IHDR(png, info,
389 &width, &height, &bit_depth, &color_type, &interlaced,
390 NULL, NULL)) goto Error;
391
392 png_set_strip_16(png);
393 png_set_packing(png);
394 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000395 if (color_type == PNG_COLOR_TYPE_GRAY ||
396 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800397 if (bit_depth < 8) {
398 png_set_expand_gray_1_2_4_to_8(png);
399 }
400 png_set_gray_to_rgb(png);
401 }
402 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
403 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700404 has_alpha = 1;
405 } else {
406 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800407 }
408
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700409 if (!keep_alpha) {
410 png_set_strip_alpha(png);
411 has_alpha = 0;
412 }
413
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800414 num_passes = png_set_interlace_handling(png);
415 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700416 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800417 rgb = (uint8_t*)malloc(stride * height);
418 if (rgb == NULL) goto Error;
419 for (p = 0; p < num_passes; ++p) {
420 for (y = 0; y < height; ++y) {
421 png_bytep row = rgb + y * stride;
422 png_read_rows(png, &row, NULL, 1);
423 }
424 }
425 png_read_end(png, info);
426 png_destroy_read_struct(&png, &info, NULL);
427
428 pic->width = width;
429 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700430 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
431 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800432 free(rgb);
433
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000434 if (ok && has_alpha && keep_alpha == 2) {
435 WebPCleanupTransparentArea(pic);
436 }
437
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800438 End:
439 return ok;
440}
441#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700442static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400443 (void)in_file;
444 (void)pic;
445 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800446 fprintf(stderr, "PNG support not compiled. Please install the libpng "
447 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800448 return 0;
449}
450#endif
451
452typedef enum {
453 PNG = 0,
454 JPEG,
James Zerna0b27362012-01-27 17:39:47 -0800455 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800456} InputFileFormat;
457
458static InputFileFormat GetImageType(FILE* in_file) {
459 InputFileFormat format = UNSUPPORTED;
460 unsigned int magic;
461 unsigned char buf[4];
462
463 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
464 (fseek(in_file, 0, SEEK_SET) != 0)) {
465 return format;
466 }
467
468 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
469 if (magic == 0x89504E47U) {
470 format = PNG;
471 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
472 format = JPEG;
473 }
474 return format;
475}
476
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700477static int ReadPicture(const char* const filename, WebPPicture* const pic,
478 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800479 int ok = 0;
480 FILE* in_file = fopen(filename, "rb");
481 if (in_file == NULL) {
482 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
483 return ok;
484 }
485
486 if (pic->width == 0 || pic->height == 0) {
487 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
488 const InputFileFormat format = GetImageType(in_file);
489 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700490 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800491 } else if (format == JPEG) {
492 ok = ReadJPEG(in_file, pic);
493 }
494 } else {
495 // If image size is specified, infer it as YUV format.
496 ok = ReadYUV(in_file, pic);
497 }
498 if (!ok) {
499 fprintf(stderr, "Error! Could not process file %s\n", filename);
500 }
501
502 fclose(in_file);
503 return ok;
504}
505
James Zern31f9dc62011-06-06 17:56:50 -0700506#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800507
508static void AllocExtraInfo(WebPPicture* const pic) {
509 const int mb_w = (pic->width + 15) / 16;
510 const int mb_h = (pic->height + 15) / 16;
511 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
512}
513
514static void PrintByteCount(const int bytes[4], int total_size,
515 int* const totals) {
516 int s;
517 int total = 0;
518 for (s = 0; s < 4; ++s) {
519 fprintf(stderr, "| %7d ", bytes[s]);
520 total += bytes[s];
521 if (totals) totals[s] += bytes[s];
522 }
James Zern04e84cf2011-11-04 15:20:08 -0700523 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800524}
525
526static void PrintPercents(const int counts[4], int total) {
527 int s;
528 for (s = 0; s < 4; ++s) {
529 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
530 }
James Zern04e84cf2011-11-04 15:20:08 -0700531 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800532}
533
534static void PrintValues(const int values[4]) {
535 int s;
536 for (s = 0; s < 4; ++s) {
537 fprintf(stderr, "| %7d ", values[s]);
538 }
James Zern04e84cf2011-11-04 15:20:08 -0700539 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800540}
541
Vikas Arorac4ccab62012-05-09 11:27:46 +0530542static void PrintExtraInfoLossless(const WebPPicture* const pic,
543 int short_output,
544 const char* const file_name) {
545 const WebPAuxStats* const stats = pic->stats;
546 if (short_output) {
547 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
548 } else {
549 fprintf(stderr, "File: %s\n", file_name);
550 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
551 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
552 }
553}
554
555static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
556 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800557 const WebPAuxStats* const stats = pic->stats;
558 if (short_output) {
559 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700560 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800561 const int num_i4 = stats->block_count[0];
562 const int num_i16 = stats->block_count[1];
563 const int num_skip = stats->block_count[2];
564 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800565 fprintf(stderr, "File: %s\n", file_name);
566 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700567 pic->width, pic->height,
568 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800569 fprintf(stderr, "Output: "
570 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800571 stats->coded_size,
572 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
573 if (total > 0) {
574 int totals[4] = { 0, 0, 0, 0 };
575 fprintf(stderr, "block count: intra4: %d\n"
576 " intra16: %d (-> %.2f%%)\n",
577 num_i4, num_i16, 100.f * num_i16 / total);
578 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
579 num_skip, 100.f * num_skip / total);
580 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
581 " mode-partition: %6d (%.1f%%)\n",
582 stats->header_bytes[0],
583 100.f * stats->header_bytes[0] / stats->coded_size,
584 stats->header_bytes[1],
585 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700586 if (stats->alpha_data_size) {
587 fprintf(stderr, " transparency: %6d\n",
588 stats->alpha_data_size);
589 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700590 if (stats->layer_data_size) {
591 fprintf(stderr, " enhancement: %6d\n",
592 stats->layer_data_size);
593 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800594 fprintf(stderr, " Residuals bytes "
595 "|segment 1|segment 2|segment 3"
596 "|segment 4| total\n");
597 fprintf(stderr, " intra4-coeffs: ");
598 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
599 fprintf(stderr, " intra16-coeffs: ");
600 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
601 fprintf(stderr, " chroma coeffs: ");
602 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
603 fprintf(stderr, " macroblocks: ");
604 PrintPercents(stats->segment_size, total);
605 fprintf(stderr, " quantizer: ");
606 PrintValues(stats->segment_quant);
607 fprintf(stderr, " filter level: ");
608 PrintValues(stats->segment_level);
609 fprintf(stderr, "------------------+---------");
610 fprintf(stderr, "+---------+---------+---------+-----------------\n");
611 fprintf(stderr, " segments total: ");
612 PrintByteCount(totals, stats->coded_size, NULL);
613 }
614 }
615 if (pic->extra_info) {
616 const int mb_w = (pic->width + 15) / 16;
617 const int mb_h = (pic->height + 15) / 16;
618 const int type = pic->extra_info_type;
619 int x, y;
620 for (y = 0; y < mb_h; ++y) {
621 for (x = 0; x < mb_w; ++x) {
622 const int c = pic->extra_info[x + y * mb_w];
623 if (type == 1) { // intra4/intra16
624 printf("%c", "+."[c]);
625 } else if (type == 2) { // segments
626 printf("%c", ".-*X"[c]);
627 } else if (type == 3) { // quantizers
628 printf("%.2d ", c);
629 } else if (type == 6 || type == 7) {
630 printf("%3d ", c);
631 } else {
632 printf("0x%.2x ", c);
633 }
634 }
635 printf("\n");
636 }
637 }
638}
639
James Zernc7e86ab2011-08-25 14:22:32 -0700640//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800641
642static int MyWriter(const uint8_t* data, size_t data_size,
643 const WebPPicture* const pic) {
644 FILE* const out = (FILE*)pic->custom_ptr;
645 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
646}
647
648// Dumps a picture as a PGM file using the IMC4 layout.
649static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
650 int y;
651 const int uv_width = (picture->width + 1) / 2;
652 const int uv_height = (picture->height + 1) / 2;
653 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700654 const int alpha_height =
655 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700656 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800657 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800658 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800659 fprintf(f, "P5\n%d %d\n255\n", stride, height);
660 for (y = 0; y < picture->height; ++y) {
661 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
662 return 0;
663 if (picture->width & 1) fputc(0, f); // pad
664 }
665 for (y = 0; y < uv_height; ++y) {
666 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
667 return 0;
668 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
669 return 0;
670 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700671 for (y = 0; y < alpha_height; ++y) {
672 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
673 return 0;
674 if (picture->width & 1) fputc(0, f); // pad
675 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800676 fclose(f);
677 return 1;
678}
679
James Zernc7e86ab2011-08-25 14:22:32 -0700680//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800681
Pascal Massimino30971c92011-12-01 02:24:50 -0800682static int ProgressReport(int percent, const WebPPicture* const picture) {
683 printf("[%s]: %3d %% \r",
684 (char*)picture->stats->user_data, percent);
685 fflush(stdout);
686 return 1; // all ok
687}
688
689//------------------------------------------------------------------------------
690
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700691static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800692 printf("Usage:\n\n");
693 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
694 printf("where quality is between 0 (poor) to 100 (very good).\n");
695 printf("Typical value is around 80.\n\n");
696 printf("Try -longhelp for an exhaustive list of advanced options.\n");
697}
698
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700699static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 printf("Usage:\n");
701 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
702 printf("If input size (-s) for an image is not specified, "
703 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700704#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800705 printf("Windows builds can take as input any of the files handled by WIC\n");
706#endif
707 printf("options:\n");
708 printf(" -h / -help ............ short help\n");
709 printf(" -H / -longhelp ........ long help\n");
710 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530711 printf(" -alpha_q <int> ......... Transparency-compression quality "
712 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 printf(" -preset <string> ....... Preset setting, one of:\n");
714 printf(" default, photo, picture,\n");
715 printf(" drawing, icon, text\n");
716 printf(" -preset must come first, as it overwrites other parameters.");
717 printf("\n");
718 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
719 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700720 printf(" -size <int> ............ Target size (in bytes)\n");
721 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800722 printf("\n");
723 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
724 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
725 printf(" -f <int> ............... filter strength (0=off..100)\n");
726 printf(" -sharpness <int> ....... "
727 "filter sharpness (0:most .. 7:least sharp)\n");
728 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700729 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
730 printf(" "
731 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800732 printf(" -pass <int> ............ analysis pass number (1..10)\n");
733 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700734 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
735#ifdef WEBP_EXPERIMENTAL_FEATURES
736 printf(" -444 / -422 / -gray ..... Change colorspace\n");
737#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800738 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800739 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
740 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800741 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530742 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800743 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
744 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000745 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530746 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530747 printf(" -lossless .............. Encode image losslessly.\n");
748 printf(" -hint <string> ......... Specify image characteristics hint.\n");
749 printf(" One of: photo or picture\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700750
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800751 printf("\n");
752 printf(" -short ................. condense printed message\n");
753 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700754 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700755#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700756 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700757#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800758 printf(" -v ..................... verbose, e.g. print encoding/decoding "
759 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800760 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800761 printf("\n");
762 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800763 printf(" -af .................... auto-adjust filter strength.\n");
764 printf(" -pre <int> ............. pre-processing filter\n");
765 printf("\n");
766}
767
James Zernc7e86ab2011-08-25 14:22:32 -0700768//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700769// Error messages
770
771static const char* const kErrorMessages[] = {
772 "OK",
773 "OUT_OF_MEMORY: Out of memory allocating objects",
774 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
775 "NULL_PARAMETER: NULL parameter passed to function",
776 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700777 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
778 "allowed is 16383 pixels.",
779 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
780 "To reduce the size of this partition, try using less segments "
781 "with the -segments option, and eventually reduce the number of "
782 "header bits using -partition_limit. More details are available "
783 "in the manual (`man cwebp`)",
784 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800785 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800786 "FILE_TOO_BIG: File would be too big to fit in 4G",
787 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700788};
789
James Zernc7e86ab2011-08-25 14:22:32 -0700790//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791
792int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700793 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800794 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
795 FILE *out = NULL;
796 int c;
797 int short_output = 0;
798 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530799 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800800 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700801 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800802 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800803 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800804 int print_distortion = 0; // 1=PSNR, 2=SSIM
805 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800806 WebPConfig config;
807 WebPAuxStats stats;
808 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700809
Pascal Massiminod61479f2012-01-20 07:20:56 -0800810 if (!WebPPictureInit(&picture) ||
811 !WebPPictureInit(&original_picture) ||
812 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800813 fprintf(stderr, "Error! Version mismatch!\n");
814 goto Error;
815 }
816
817 if (argc == 1) {
818 HelpShort();
819 return 0;
820 }
821
822 for (c = 1; c < argc; ++c) {
823 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
824 HelpShort();
825 return 0;
826 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
827 HelpLong();
828 return 0;
829 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
830 out_file = argv[++c];
831 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
832 dump_file = argv[++c];
833 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800834 } else if (!strcmp(argv[c], "-print_ssim")) {
835 config.show_compressed = 1;
836 print_distortion = 2;
837 } else if (!strcmp(argv[c], "-print_psnr")) {
838 config.show_compressed = 1;
839 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800840 } else if (!strcmp(argv[c], "-short")) {
841 short_output++;
842 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700843 picture.width = strtol(argv[++c], NULL, 0);
844 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800845 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700846 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800847 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200848 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530849 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
850 config.alpha_quality = strtol(argv[++c], NULL, 0);
851 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
852 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000853 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
854 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530855 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800856 ++c;
857 if (!strcmp(argv[c], "none")) {
858 config.alpha_filtering = 0;
859 } else if (!strcmp(argv[c], "fast")) {
860 config.alpha_filtering = 1;
861 } else if (!strcmp(argv[c], "best")) {
862 config.alpha_filtering = 2;
863 } else {
864 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
865 goto Error;
866 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530867 } else if (!strcmp(argv[c], "-noalpha")) {
868 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000869 } else if (!strcmp(argv[c], "-lossless")) {
870 config.lossless = 1;
871 picture.use_argb_input = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530872 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
873 ++c;
874 if (!strcmp(argv[c], "photo")) {
875 config.image_hint = WEBP_HINT_PHOTO;
876 } else if (!strcmp(argv[c], "picture")) {
877 config.image_hint = WEBP_HINT_PICTURE;
878 } else {
879 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
880 goto Error;
881 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800882 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700883 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800884 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200885 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800886 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700887 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800888 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700889 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800890 } else if (!strcmp(argv[c], "-af")) {
891 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700892 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800893 config.filter_type = 1;
894 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700895 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800896 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700897 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800898 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700899 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800900 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700901 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700902 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
903 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800904 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700905 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700906#ifdef WEBP_EXPERIMENTAL_FEATURES
907 } else if (!strcmp(argv[c], "-444")) {
908 picture.colorspace = WEBP_YUV444;
909 } else if (!strcmp(argv[c], "-422")) {
910 picture.colorspace = WEBP_YUV422;
911 } else if (!strcmp(argv[c], "-gray")) {
912 picture.colorspace = WEBP_YUV400;
913#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800914 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
915 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700916 crop_x = strtol(argv[++c], NULL, 0);
917 crop_y = strtol(argv[++c], NULL, 0);
918 crop_w = strtol(argv[++c], NULL, 0);
919 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700920 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
921 resize_w = strtol(argv[++c], NULL, 0);
922 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700923#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700924 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000925 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700926#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700927 } else if (!strcmp(argv[c], "-version")) {
928 const int version = WebPGetEncoderVersion();
929 printf("%d.%d.%d\n",
930 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
931 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800932 } else if (!strcmp(argv[c], "-progress")) {
933 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800934 } else if (!strcmp(argv[c], "-quiet")) {
935 quiet = 1;
936 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
937 WebPPreset preset;
938 ++c;
939 if (!strcmp(argv[c], "default")) {
940 preset = WEBP_PRESET_DEFAULT;
941 } else if (!strcmp(argv[c], "photo")) {
942 preset = WEBP_PRESET_PHOTO;
943 } else if (!strcmp(argv[c], "picture")) {
944 preset = WEBP_PRESET_PICTURE;
945 } else if (!strcmp(argv[c], "drawing")) {
946 preset = WEBP_PRESET_DRAWING;
947 } else if (!strcmp(argv[c], "icon")) {
948 preset = WEBP_PRESET_ICON;
949 } else if (!strcmp(argv[c], "text")) {
950 preset = WEBP_PRESET_TEXT;
951 } else {
952 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
953 goto Error;
954 }
955 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700956 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800957 goto Error;
958 }
959 } else if (!strcmp(argv[c], "-v")) {
960 verbose = 1;
961 } else if (argv[c][0] == '-') {
962 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
963 HelpLong();
964 return -1;
965 } else {
966 in_file = argv[c];
967 }
968 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700969 if (in_file == NULL) {
970 fprintf(stderr, "No input file specified!\n");
971 HelpShort();
972 goto Error;
973 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800974
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530975 // Check for unsupported command line options for lossless mode and log
976 // warning for such options.
977 if (config.lossless == 1) {
978 if (config.target_size > 0 || config.target_PSNR > 0) {
979 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
980 " for lossless encoding. Ignoring such option(s)!\n");
981 }
982 if (config.partition_limit > 0) {
983 fprintf(stderr, "Partition limit option is not required for lossless"
984 " encoding. Ignoring this option!\n");
985 }
986 if (show_progress) {
987 fprintf(stderr, "Progress reporting option is not supported for lossless"
988 " encoding. Ignoring this option!\n");
989 }
990 }
991
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800992 if (!WebPValidateConfig(&config)) {
993 fprintf(stderr, "Error! Invalid configuration.\n");
994 goto Error;
995 }
996
Pascal Massimino0744e842011-02-25 12:03:27 -0800997 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700998 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800999 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001000 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -07001001 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001002 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001003 goto Error;
1004 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001005 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1006
Pascal Massimino0744e842011-02-25 12:03:27 -08001007 if (verbose) {
1008 const double time = StopwatchReadAndReset(&stop_watch);
1009 fprintf(stderr, "Time to read input: %.3fs\n", time);
1010 }
1011
1012 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001013 if (out_file) {
1014 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001015 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001016 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1017 goto Error;
1018 } else {
1019 if (!short_output && !quiet) {
1020 fprintf(stderr, "Saving file '%s'\n", out_file);
1021 }
1022 }
1023 picture.writer = MyWriter;
1024 picture.custom_ptr = (void*)out;
1025 } else {
1026 out = NULL;
1027 if (!quiet && !short_output) {
1028 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1029 fprintf(stderr, "be performed, but its results discarded.\n\n");
1030 }
1031 }
1032 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -08001033 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001034
Pascal Massimino0744e842011-02-25 12:03:27 -08001035 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001036 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001037 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001038 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001039 if (crop != 0) {
1040 // We use self-cropping using a view.
1041 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1042 fprintf(stderr, "Error! Cannot crop picture\n");
1043 goto Error;
1044 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001045 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001046 if ((resize_w | resize_h) > 0) {
1047 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1048 fprintf(stderr, "Error! Cannot resize picture\n");
1049 goto Error;
1050 }
1051 }
1052 if (picture.extra_info_type > 0) {
1053 AllocExtraInfo(&picture);
1054 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001055 if (print_distortion > 0) { // Save original picture for later comparison
1056 WebPPictureCopy(&picture, &original_picture);
1057 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001058 if (!WebPEncode(&config, &picture)) {
1059 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001060 fprintf(stderr, "Error code: %d (%s)\n",
1061 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001062 goto Error;
1063 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001064 if (verbose) {
1065 const double time = StopwatchReadAndReset(&stop_watch);
1066 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1067 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001068
1069 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001070 if (dump_file) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001071 if (picture.use_argb_input) {
1072 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1073 } else if (!DumpPicture(&picture, dump_file)) {
1074 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1075 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001076 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001077
Pascal Massimino38369c02011-05-04 22:59:51 -07001078 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301079 if (config.lossless) {
1080 PrintExtraInfoLossless(&picture, short_output, in_file);
1081 } else {
1082 PrintExtraInfoLossy(&picture, short_output, in_file);
1083 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001084 }
1085 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1086 float values[5];
1087 WebPPictureDistortion(&picture, &original_picture,
1088 (print_distortion == 1) ? 0 : 1, values);
1089 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1090 (print_distortion == 1) ? "PSNR" : "SSIM",
1091 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001092 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001093 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001094
1095 Error:
1096 free(picture.extra_info);
1097 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001098 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001099 if (out != NULL) {
1100 fclose(out);
1101 }
1102
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001103 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001104}
1105
James Zernc7e86ab2011-08-25 14:22:32 -07001106//------------------------------------------------------------------------------