blob: c6f10970438951699fe0c0a606c20d3d3ddba976 [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
Pascal Massiminod61479f2012-01-20 07:20:56 -0800539static void PrintExtraInfo(const WebPPicture* const pic, int short_output,
540 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800541 const WebPAuxStats* const stats = pic->stats;
542 if (short_output) {
543 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700544 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800545 const int num_i4 = stats->block_count[0];
546 const int num_i16 = stats->block_count[1];
547 const int num_skip = stats->block_count[2];
548 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800549 fprintf(stderr, "File: %s\n", file_name);
550 fprintf(stderr, "Dimension: %d x %d%s\n",
551 pic->width, pic->height, (pic->a != NULL) ? " (with alpha)" : "");
552 fprintf(stderr, "Output: "
553 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800554 stats->coded_size,
555 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
556 if (total > 0) {
557 int totals[4] = { 0, 0, 0, 0 };
558 fprintf(stderr, "block count: intra4: %d\n"
559 " intra16: %d (-> %.2f%%)\n",
560 num_i4, num_i16, 100.f * num_i16 / total);
561 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
562 num_skip, 100.f * num_skip / total);
563 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
564 " mode-partition: %6d (%.1f%%)\n",
565 stats->header_bytes[0],
566 100.f * stats->header_bytes[0] / stats->coded_size,
567 stats->header_bytes[1],
568 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700569 if (stats->alpha_data_size) {
570 fprintf(stderr, " transparency: %6d\n",
571 stats->alpha_data_size);
572 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700573 if (stats->layer_data_size) {
574 fprintf(stderr, " enhancement: %6d\n",
575 stats->layer_data_size);
576 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800577 fprintf(stderr, " Residuals bytes "
578 "|segment 1|segment 2|segment 3"
579 "|segment 4| total\n");
580 fprintf(stderr, " intra4-coeffs: ");
581 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
582 fprintf(stderr, " intra16-coeffs: ");
583 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
584 fprintf(stderr, " chroma coeffs: ");
585 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
586 fprintf(stderr, " macroblocks: ");
587 PrintPercents(stats->segment_size, total);
588 fprintf(stderr, " quantizer: ");
589 PrintValues(stats->segment_quant);
590 fprintf(stderr, " filter level: ");
591 PrintValues(stats->segment_level);
592 fprintf(stderr, "------------------+---------");
593 fprintf(stderr, "+---------+---------+---------+-----------------\n");
594 fprintf(stderr, " segments total: ");
595 PrintByteCount(totals, stats->coded_size, NULL);
596 }
597 }
598 if (pic->extra_info) {
599 const int mb_w = (pic->width + 15) / 16;
600 const int mb_h = (pic->height + 15) / 16;
601 const int type = pic->extra_info_type;
602 int x, y;
603 for (y = 0; y < mb_h; ++y) {
604 for (x = 0; x < mb_w; ++x) {
605 const int c = pic->extra_info[x + y * mb_w];
606 if (type == 1) { // intra4/intra16
607 printf("%c", "+."[c]);
608 } else if (type == 2) { // segments
609 printf("%c", ".-*X"[c]);
610 } else if (type == 3) { // quantizers
611 printf("%.2d ", c);
612 } else if (type == 6 || type == 7) {
613 printf("%3d ", c);
614 } else {
615 printf("0x%.2x ", c);
616 }
617 }
618 printf("\n");
619 }
620 }
621}
622
James Zernc7e86ab2011-08-25 14:22:32 -0700623//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800624
625static int MyWriter(const uint8_t* data, size_t data_size,
626 const WebPPicture* const pic) {
627 FILE* const out = (FILE*)pic->custom_ptr;
628 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
629}
630
631// Dumps a picture as a PGM file using the IMC4 layout.
632static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
633 int y;
634 const int uv_width = (picture->width + 1) / 2;
635 const int uv_height = (picture->height + 1) / 2;
636 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700637 const int alpha_height = picture->a ? picture->height : 0;
638 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800639 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800640 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800641 fprintf(f, "P5\n%d %d\n255\n", stride, height);
642 for (y = 0; y < picture->height; ++y) {
643 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
644 return 0;
645 if (picture->width & 1) fputc(0, f); // pad
646 }
647 for (y = 0; y < uv_height; ++y) {
648 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
649 return 0;
650 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
651 return 0;
652 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700653 for (y = 0; y < alpha_height; ++y) {
654 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
655 return 0;
656 if (picture->width & 1) fputc(0, f); // pad
657 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800658 fclose(f);
659 return 1;
660}
661
James Zernc7e86ab2011-08-25 14:22:32 -0700662//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800663
Pascal Massimino30971c92011-12-01 02:24:50 -0800664static int ProgressReport(int percent, const WebPPicture* const picture) {
665 printf("[%s]: %3d %% \r",
666 (char*)picture->stats->user_data, percent);
667 fflush(stdout);
668 return 1; // all ok
669}
670
671//------------------------------------------------------------------------------
672
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700673static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674 printf("Usage:\n\n");
675 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
676 printf("where quality is between 0 (poor) to 100 (very good).\n");
677 printf("Typical value is around 80.\n\n");
678 printf("Try -longhelp for an exhaustive list of advanced options.\n");
679}
680
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700681static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800682 printf("Usage:\n");
683 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
684 printf("If input size (-s) for an image is not specified, "
685 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700686#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800687 printf("Windows builds can take as input any of the files handled by WIC\n");
688#endif
689 printf("options:\n");
690 printf(" -h / -help ............ short help\n");
691 printf(" -H / -longhelp ........ long help\n");
692 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530693 printf(" -alpha_q <int> ......... Transparency-compression quality "
694 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800695 printf(" -preset <string> ....... Preset setting, one of:\n");
696 printf(" default, photo, picture,\n");
697 printf(" drawing, icon, text\n");
698 printf(" -preset must come first, as it overwrites other parameters.");
699 printf("\n");
700 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
701 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700702 printf(" -size <int> ............ Target size (in bytes)\n");
703 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800704 printf("\n");
705 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
706 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
707 printf(" -f <int> ............... filter strength (0=off..100)\n");
708 printf(" -sharpness <int> ....... "
709 "filter sharpness (0:most .. 7:least sharp)\n");
710 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700711 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
712 printf(" "
713 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800714 printf(" -pass <int> ............ analysis pass number (1..10)\n");
715 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700716 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
717#ifdef WEBP_EXPERIMENTAL_FEATURES
718 printf(" -444 / -422 / -gray ..... Change colorspace\n");
719#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800720 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800721 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
722 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800723 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530724 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800725 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
726 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000727 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530728 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700729
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 printf("\n");
731 printf(" -short ................. condense printed message\n");
732 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700733 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700734#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700735 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700736#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800737 printf(" -v ..................... verbose, e.g. print encoding/decoding "
738 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800739 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800740 printf("\n");
741 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800742 printf(" -af .................... auto-adjust filter strength.\n");
743 printf(" -pre <int> ............. pre-processing filter\n");
744 printf("\n");
745}
746
James Zernc7e86ab2011-08-25 14:22:32 -0700747//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700748// Error messages
749
750static const char* const kErrorMessages[] = {
751 "OK",
752 "OUT_OF_MEMORY: Out of memory allocating objects",
753 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
754 "NULL_PARAMETER: NULL parameter passed to function",
755 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700756 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
757 "allowed is 16383 pixels.",
758 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
759 "To reduce the size of this partition, try using less segments "
760 "with the -segments option, and eventually reduce the number of "
761 "header bits using -partition_limit. More details are available "
762 "in the manual (`man cwebp`)",
763 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800764 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800765 "FILE_TOO_BIG: File would be too big to fit in 4G",
766 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700767};
768
James Zernc7e86ab2011-08-25 14:22:32 -0700769//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800770
771int main(int argc, const char *argv[]) {
772 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
773 FILE *out = NULL;
774 int c;
775 int short_output = 0;
776 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530777 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800778 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700779 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800780 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800781 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800782 int print_distortion = 0; // 1=PSNR, 2=SSIM
783 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784 WebPConfig config;
785 WebPAuxStats stats;
786 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700787
Pascal Massiminod61479f2012-01-20 07:20:56 -0800788 if (!WebPPictureInit(&picture) ||
789 !WebPPictureInit(&original_picture) ||
790 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791 fprintf(stderr, "Error! Version mismatch!\n");
792 goto Error;
793 }
794
795 if (argc == 1) {
796 HelpShort();
797 return 0;
798 }
799
800 for (c = 1; c < argc; ++c) {
801 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
802 HelpShort();
803 return 0;
804 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
805 HelpLong();
806 return 0;
807 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
808 out_file = argv[++c];
809 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
810 dump_file = argv[++c];
811 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800812 } else if (!strcmp(argv[c], "-print_ssim")) {
813 config.show_compressed = 1;
814 print_distortion = 2;
815 } else if (!strcmp(argv[c], "-print_psnr")) {
816 config.show_compressed = 1;
817 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800818 } else if (!strcmp(argv[c], "-short")) {
819 short_output++;
820 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700821 picture.width = strtol(argv[++c], NULL, 0);
822 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700824 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800825 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200826 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530827 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
828 config.alpha_quality = strtol(argv[++c], NULL, 0);
829 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
830 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000831 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
832 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530833 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800834 ++c;
835 if (!strcmp(argv[c], "none")) {
836 config.alpha_filtering = 0;
837 } else if (!strcmp(argv[c], "fast")) {
838 config.alpha_filtering = 1;
839 } else if (!strcmp(argv[c], "best")) {
840 config.alpha_filtering = 2;
841 } else {
842 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
843 goto Error;
844 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530845 } else if (!strcmp(argv[c], "-noalpha")) {
846 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000847#ifdef USE_LOSSLESS_ENCODER
848 } else if (!strcmp(argv[c], "-lossless")) {
849 config.lossless = 1;
850 picture.use_argb_input = 1;
851#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800852 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700853 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800854 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200855 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800856 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700857 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800858 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700859 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800860 } else if (!strcmp(argv[c], "-af")) {
861 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700862 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800863 config.filter_type = 1;
864 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700865 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800866 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700867 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800868 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700869 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800870 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700871 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700872 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
873 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800874 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700875 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700876#ifdef WEBP_EXPERIMENTAL_FEATURES
877 } else if (!strcmp(argv[c], "-444")) {
878 picture.colorspace = WEBP_YUV444;
879 } else if (!strcmp(argv[c], "-422")) {
880 picture.colorspace = WEBP_YUV422;
881 } else if (!strcmp(argv[c], "-gray")) {
882 picture.colorspace = WEBP_YUV400;
883#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800884 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
885 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700886 crop_x = strtol(argv[++c], NULL, 0);
887 crop_y = strtol(argv[++c], NULL, 0);
888 crop_w = strtol(argv[++c], NULL, 0);
889 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700890 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
891 resize_w = strtol(argv[++c], NULL, 0);
892 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700893#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700894 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000895 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700896#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700897 } else if (!strcmp(argv[c], "-version")) {
898 const int version = WebPGetEncoderVersion();
899 printf("%d.%d.%d\n",
900 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
901 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800902 } else if (!strcmp(argv[c], "-progress")) {
903 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800904 } else if (!strcmp(argv[c], "-quiet")) {
905 quiet = 1;
906 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
907 WebPPreset preset;
908 ++c;
909 if (!strcmp(argv[c], "default")) {
910 preset = WEBP_PRESET_DEFAULT;
911 } else if (!strcmp(argv[c], "photo")) {
912 preset = WEBP_PRESET_PHOTO;
913 } else if (!strcmp(argv[c], "picture")) {
914 preset = WEBP_PRESET_PICTURE;
915 } else if (!strcmp(argv[c], "drawing")) {
916 preset = WEBP_PRESET_DRAWING;
917 } else if (!strcmp(argv[c], "icon")) {
918 preset = WEBP_PRESET_ICON;
919 } else if (!strcmp(argv[c], "text")) {
920 preset = WEBP_PRESET_TEXT;
921 } else {
922 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
923 goto Error;
924 }
925 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700926 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800927 goto Error;
928 }
929 } else if (!strcmp(argv[c], "-v")) {
930 verbose = 1;
931 } else if (argv[c][0] == '-') {
932 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
933 HelpLong();
934 return -1;
935 } else {
936 in_file = argv[c];
937 }
938 }
939
940 if (!WebPValidateConfig(&config)) {
941 fprintf(stderr, "Error! Invalid configuration.\n");
942 goto Error;
943 }
944
Pascal Massimino0744e842011-02-25 12:03:27 -0800945 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700946 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800947 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700948 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700949 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800950 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700951 goto Error;
952 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800953 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
954
Pascal Massimino0744e842011-02-25 12:03:27 -0800955 if (verbose) {
956 const double time = StopwatchReadAndReset(&stop_watch);
957 fprintf(stderr, "Time to read input: %.3fs\n", time);
958 }
959
960 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800961 if (out_file) {
962 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800963 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800964 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
965 goto Error;
966 } else {
967 if (!short_output && !quiet) {
968 fprintf(stderr, "Saving file '%s'\n", out_file);
969 }
970 }
971 picture.writer = MyWriter;
972 picture.custom_ptr = (void*)out;
973 } else {
974 out = NULL;
975 if (!quiet && !short_output) {
976 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
977 fprintf(stderr, "be performed, but its results discarded.\n\n");
978 }
979 }
980 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -0800981 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800982
Pascal Massimino0744e842011-02-25 12:03:27 -0800983 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700984 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800985 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700986 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700987 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
988 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800989 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700990 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700991 if ((resize_w | resize_h) > 0) {
992 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
993 fprintf(stderr, "Error! Cannot resize picture\n");
994 goto Error;
995 }
996 }
997 if (picture.extra_info_type > 0) {
998 AllocExtraInfo(&picture);
999 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001000 if (print_distortion > 0) { // Save original picture for later comparison
1001 WebPPictureCopy(&picture, &original_picture);
1002 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001003 if (!WebPEncode(&config, &picture)) {
1004 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001005 fprintf(stderr, "Error code: %d (%s)\n",
1006 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001007 goto Error;
1008 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001009 if (verbose) {
1010 const double time = StopwatchReadAndReset(&stop_watch);
1011 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1012 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001013
1014 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001015 if (dump_file) {
1016 DumpPicture(&picture, dump_file);
1017 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001018
Pascal Massimino38369c02011-05-04 22:59:51 -07001019 if (!quiet) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001020 PrintExtraInfo(&picture, short_output, in_file);
1021 }
1022 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1023 float values[5];
1024 WebPPictureDistortion(&picture, &original_picture,
1025 (print_distortion == 1) ? 0 : 1, values);
1026 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1027 (print_distortion == 1) ? "PSNR" : "SSIM",
1028 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001029 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001030
1031 Error:
1032 free(picture.extra_info);
1033 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001034 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001035 if (out != NULL) {
1036 fclose(out);
1037 }
1038
1039 return 0;
1040}
1041
James Zernc7e86ab2011-08-25 14:22:32 -07001042//------------------------------------------------------------------------------