blob: 7c1fb1a8f2d4bb057f9eb1cab499d740635fb968 [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08002//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// simple command line calling the WebPEncode function.
9// Encodes a raw .YUV into WebP bitstream
10//
11// Author: Skal (pascal.massimino@gmail.com)
12
13#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070014#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080015#include <string.h>
16
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#ifdef WEBP_HAVE_PNG
22#include <png.h>
23#endif
24
25#ifdef WEBP_HAVE_JPEG
26#include <setjmp.h> // note: this must be included *after* png.h
27#include <jpeglib.h>
28#endif
29
James Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041
42#ifndef GUID_WICPixelFormat24bppRGB
43// From Microsoft SDK 7.0a
44DEFINE_GUID(GUID_WICPixelFormat24bppRGB,
45 0x6fddc324, 0x4e03, 0x4bfe, 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046#endif
James Zern3cfe0882011-06-21 18:29:30 -070047#ifndef GUID_WICPixelFormat32bppRGBA
48DEFINE_GUID(GUID_WICPixelFormat32bppRGBA,
49 0xf5c7ad2d, 0x6a8d, 0x43dd, 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
50#endif
James Zern31f9dc62011-06-06 17:56:50 -070051#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080052
53
54#include "webp/encode.h"
55#include "stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070056#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070057#if defined(__cplusplus) || defined(c_plusplus)
58extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070059#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070060
James Zern04e84cf2011-11-04 15:20:08 -070061extern void* VP8GetCPUInfo; // opaque forward declaration.
62
63#if defined(__cplusplus) || defined(c_plusplus)
64} // extern "C"
65#endif
66#endif // WEBP_DLL
67
James Zernc7e86ab2011-08-25 14:22:32 -070068//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080069
70static int verbose = 0;
71
72static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
73 const int uv_width = (pic->width + 1) / 2;
74 const int uv_height = (pic->height + 1) / 2;
75 int y;
76 int ok = 0;
77
78 if (!WebPPictureAlloc(pic)) return ok;
79
80 for (y = 0; y < pic->height; ++y) {
81 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
82 goto End;
83 }
84 }
85 for (y = 0; y < uv_height; ++y) {
86 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
87 goto End;
88 }
89 for (y = 0; y < uv_height; ++y) {
90 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
91 goto End;
92 }
93 ok = 1;
94
95 End:
96 return ok;
97}
98
James Zern31f9dc62011-06-06 17:56:50 -070099#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800100
101#define IFS(fn) \
102 do { \
103 if (SUCCEEDED(hr)) \
104 { \
105 hr = (fn); \
106 if (FAILED(hr) && verbose) \
James Zern974aaff2012-01-24 12:46:46 -0800107 fprintf(stderr, #fn " failed %08x\n", hr); \
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800108 } \
109 } while (0)
110
111#ifdef __cplusplus
112#define MAKE_REFGUID(x) (x)
113#else
114#define MAKE_REFGUID(x) &(x)
115#endif
116
117static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
118 HRESULT hr = S_OK;
119 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
120 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800121 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800122 return hr;
123}
124
125static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700126 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800127 HRESULT hr = S_OK;
128 IWICBitmapFrameDecode* pFrame = NULL;
129 IWICFormatConverter* pConverter = NULL;
130 IWICImagingFactory* pFactory = NULL;
131 IWICBitmapDecoder* pDecoder = NULL;
132 IStream* pStream = NULL;
133 UINT frameCount = 0;
134 UINT width, height = 0;
135 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700136 WICPixelFormatGUID srcPixelFormat = { 0 };
137 GUID srcContainerFormat = { 0 };
138 const GUID* alphaContainers[] = {
139 &GUID_ContainerFormatBmp,
140 &GUID_ContainerFormatPng,
141 &GUID_ContainerFormatTiff
142 };
143 int has_alpha = 0;
144 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800145
146 IFS(CoInitialize(NULL));
147 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
148 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
149 (LPVOID*)&pFactory));
150 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800151 fprintf(stderr,
152 "Couldn't access Windows Imaging Component (are you running "
153 "Windows XP SP3 or newer?). Most formats not available. "
154 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800155 }
156 // Prepare for image decoding.
157 IFS(OpenInputStream(filename, &pStream));
158 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
159 WICDecodeMetadataCacheOnDemand, &pDecoder));
160 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
161 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800162 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800163 hr = E_FAIL;
164 }
165 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700166 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
167 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
168
169 has_alpha = keep_alpha;
170 for (i = 0;
171 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
172 ++i) {
173 if (IsEqualGUID(&srcContainerFormat, alphaContainers[i])) {
174 has_alpha =
175 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppRGBA) ||
176 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppBGRA);
177 break;
178 }
179 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800180
181 // Prepare for pixel format conversion (if necessary).
182 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
183 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern3cfe0882011-06-21 18:29:30 -0700184 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA)
185 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB),
186 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800187 NULL, 0.0, WICBitmapPaletteTypeCustom));
188
189 // Decode.
190 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700191 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800192 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700193 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800194 if (rgb == NULL)
195 hr = E_OUTOFMEMORY;
196 }
James Zern3cfe0882011-06-21 18:29:30 -0700197 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
198 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800199
200 // WebP conversion.
201 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700202 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800203 pic->width = width;
204 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700205 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
206 : WebPPictureImportRGB(pic, rgb, stride);
207 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800208 hr = E_FAIL;
209 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000210 if (SUCCEEDED(hr)) {
211 if (has_alpha && keep_alpha == 2) {
212 WebPCleanupTransparentArea(pic);
213 }
214 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800215
216 // Cleanup.
217 if (pConverter != NULL) IUnknown_Release(pConverter);
218 if (pFrame != NULL) IUnknown_Release(pFrame);
219 if (pDecoder != NULL) IUnknown_Release(pDecoder);
220 if (pFactory != NULL) IUnknown_Release(pFactory);
221 if (pStream != NULL) IUnknown_Release(pStream);
222 free(rgb);
223 return hr;
224}
225
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700226static int ReadPicture(const char* const filename, WebPPicture* const pic,
227 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800228 int ok;
229 if (pic->width != 0 && pic->height != 0) {
230 // If image size is specified, infer it as YUV format.
231 FILE* in_file = fopen(filename, "rb");
232 if (in_file == NULL) {
233 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
234 return 0;
235 }
236 ok = ReadYUV(in_file, pic);
237 fclose(in_file);
238 } else {
239 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700240 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800241 }
242 if (!ok) {
243 fprintf(stderr, "Error! Could not process file %s\n", filename);
244 }
245 return ok;
246}
247
James Zern31f9dc62011-06-06 17:56:50 -0700248#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800249
250#ifdef WEBP_HAVE_JPEG
251struct my_error_mgr {
252 struct jpeg_error_mgr pub;
253 jmp_buf setjmp_buffer;
254};
255
256static void my_error_exit(j_common_ptr dinfo) {
257 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
258 (*dinfo->err->output_message) (dinfo);
259 longjmp(myerr->setjmp_buffer, 1);
260}
261
262static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
263 int ok = 0;
264 int stride, width, height;
265 uint8_t* rgb = NULL;
266 uint8_t* row_ptr = NULL;
267 struct jpeg_decompress_struct dinfo;
268 struct my_error_mgr jerr;
269 JSAMPARRAY buffer;
270
271 dinfo.err = jpeg_std_error(&jerr.pub);
272 jerr.pub.error_exit = my_error_exit;
273
James Zern04e84cf2011-11-04 15:20:08 -0700274 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800275 Error:
276 jpeg_destroy_decompress(&dinfo);
277 goto End;
278 }
279
280 jpeg_create_decompress(&dinfo);
281 jpeg_stdio_src(&dinfo, in_file);
282 jpeg_read_header(&dinfo, TRUE);
283
284 dinfo.out_color_space = JCS_RGB;
285 dinfo.dct_method = JDCT_IFAST;
286 dinfo.do_fancy_upsampling = TRUE;
287
288 jpeg_start_decompress(&dinfo);
289
290 if (dinfo.output_components != 3) {
291 goto Error;
292 }
293
294 width = dinfo.output_width;
295 height = dinfo.output_height;
296 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
297
298 rgb = (uint8_t*)malloc(stride * height);
299 if (rgb == NULL) {
300 goto End;
301 }
302 row_ptr = rgb;
303
304 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
305 JPOOL_IMAGE, stride, 1);
306 if (buffer == NULL) {
307 goto End;
308 }
309
310 while (dinfo.output_scanline < dinfo.output_height) {
311 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
312 goto End;
313 }
314 memcpy(row_ptr, buffer[0], stride);
315 row_ptr += stride;
316 }
317
James Zern04e84cf2011-11-04 15:20:08 -0700318 jpeg_finish_decompress(&dinfo);
319 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800320
321 // WebP conversion.
322 pic->width = width;
323 pic->height = height;
324 ok = WebPPictureImportRGB(pic, rgb, stride);
325
326 End:
327 if (rgb) {
328 free(rgb);
329 }
330 return ok;
331}
332
333#else
334static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400335 (void)in_file;
336 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800337 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
338 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800339 return 0;
340}
341#endif
342
343#ifdef WEBP_HAVE_PNG
344static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700345 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800346 longjmp(png_jmpbuf(png), 1);
347}
348
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700349static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800350 png_structp png;
351 png_infop info;
352 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700353 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800354 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700355 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800356 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700357 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800358 int stride;
359 uint8_t* rgb = NULL;
360
361 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
362 if (png == NULL) {
363 goto End;
364 }
365
366 png_set_error_fn(png, 0, error_function, NULL);
367 if (setjmp(png_jmpbuf(png))) {
368 Error:
369 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700370 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800371 goto End;
372 }
373
374 info = png_create_info_struct(png);
375 if (info == NULL) goto Error;
376
377 png_init_io(png, in_file);
378 png_read_info(png, info);
379 if (!png_get_IHDR(png, info,
380 &width, &height, &bit_depth, &color_type, &interlaced,
381 NULL, NULL)) goto Error;
382
383 png_set_strip_16(png);
384 png_set_packing(png);
385 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000386 if (color_type == PNG_COLOR_TYPE_GRAY ||
387 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800388 if (bit_depth < 8) {
389 png_set_expand_gray_1_2_4_to_8(png);
390 }
391 png_set_gray_to_rgb(png);
392 }
393 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
394 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700395 has_alpha = 1;
396 } else {
397 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800398 }
399
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700400 if (!keep_alpha) {
401 png_set_strip_alpha(png);
402 has_alpha = 0;
403 }
404
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800405 num_passes = png_set_interlace_handling(png);
406 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700407 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800408 rgb = (uint8_t*)malloc(stride * height);
409 if (rgb == NULL) goto Error;
410 for (p = 0; p < num_passes; ++p) {
411 for (y = 0; y < height; ++y) {
412 png_bytep row = rgb + y * stride;
413 png_read_rows(png, &row, NULL, 1);
414 }
415 }
416 png_read_end(png, info);
417 png_destroy_read_struct(&png, &info, NULL);
418
419 pic->width = width;
420 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700421 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
422 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800423 free(rgb);
424
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000425 if (ok && has_alpha && keep_alpha == 2) {
426 WebPCleanupTransparentArea(pic);
427 }
428
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800429 End:
430 return ok;
431}
432#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700433static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400434 (void)in_file;
435 (void)pic;
436 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800437 fprintf(stderr, "PNG support not compiled. Please install the libpng "
438 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800439 return 0;
440}
441#endif
442
443typedef enum {
444 PNG = 0,
445 JPEG,
James Zerna0b27362012-01-27 17:39:47 -0800446 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800447} InputFileFormat;
448
449static InputFileFormat GetImageType(FILE* in_file) {
450 InputFileFormat format = UNSUPPORTED;
451 unsigned int magic;
452 unsigned char buf[4];
453
454 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
455 (fseek(in_file, 0, SEEK_SET) != 0)) {
456 return format;
457 }
458
459 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
460 if (magic == 0x89504E47U) {
461 format = PNG;
462 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
463 format = JPEG;
464 }
465 return format;
466}
467
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700468static int ReadPicture(const char* const filename, WebPPicture* const pic,
469 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800470 int ok = 0;
471 FILE* in_file = fopen(filename, "rb");
472 if (in_file == NULL) {
473 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
474 return ok;
475 }
476
477 if (pic->width == 0 || pic->height == 0) {
478 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
479 const InputFileFormat format = GetImageType(in_file);
480 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700481 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800482 } else if (format == JPEG) {
483 ok = ReadJPEG(in_file, pic);
484 }
485 } else {
486 // If image size is specified, infer it as YUV format.
487 ok = ReadYUV(in_file, pic);
488 }
489 if (!ok) {
490 fprintf(stderr, "Error! Could not process file %s\n", filename);
491 }
492
493 fclose(in_file);
494 return ok;
495}
496
James Zern31f9dc62011-06-06 17:56:50 -0700497#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800498
499static void AllocExtraInfo(WebPPicture* const pic) {
500 const int mb_w = (pic->width + 15) / 16;
501 const int mb_h = (pic->height + 15) / 16;
502 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
503}
504
505static void PrintByteCount(const int bytes[4], int total_size,
506 int* const totals) {
507 int s;
508 int total = 0;
509 for (s = 0; s < 4; ++s) {
510 fprintf(stderr, "| %7d ", bytes[s]);
511 total += bytes[s];
512 if (totals) totals[s] += bytes[s];
513 }
James Zern04e84cf2011-11-04 15:20:08 -0700514 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800515}
516
517static void PrintPercents(const int counts[4], int total) {
518 int s;
519 for (s = 0; s < 4; ++s) {
520 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
521 }
James Zern04e84cf2011-11-04 15:20:08 -0700522 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800523}
524
525static void PrintValues(const int values[4]) {
526 int s;
527 for (s = 0; s < 4; ++s) {
528 fprintf(stderr, "| %7d ", values[s]);
529 }
James Zern04e84cf2011-11-04 15:20:08 -0700530 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800531}
532
Vikas Arorac4ccab62012-05-09 11:27:46 +0530533static void PrintExtraInfoLossless(const WebPPicture* const pic,
534 int short_output,
535 const char* const file_name) {
536 const WebPAuxStats* const stats = pic->stats;
537 if (short_output) {
538 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
539 } else {
540 fprintf(stderr, "File: %s\n", file_name);
541 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
542 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
543 }
544}
545
546static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
547 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800548 const WebPAuxStats* const stats = pic->stats;
549 if (short_output) {
550 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700551 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800552 const int num_i4 = stats->block_count[0];
553 const int num_i16 = stats->block_count[1];
554 const int num_skip = stats->block_count[2];
555 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800556 fprintf(stderr, "File: %s\n", file_name);
557 fprintf(stderr, "Dimension: %d x %d%s\n",
558 pic->width, pic->height, (pic->a != NULL) ? " (with alpha)" : "");
559 fprintf(stderr, "Output: "
560 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800561 stats->coded_size,
562 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
563 if (total > 0) {
564 int totals[4] = { 0, 0, 0, 0 };
565 fprintf(stderr, "block count: intra4: %d\n"
566 " intra16: %d (-> %.2f%%)\n",
567 num_i4, num_i16, 100.f * num_i16 / total);
568 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
569 num_skip, 100.f * num_skip / total);
570 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
571 " mode-partition: %6d (%.1f%%)\n",
572 stats->header_bytes[0],
573 100.f * stats->header_bytes[0] / stats->coded_size,
574 stats->header_bytes[1],
575 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700576 if (stats->alpha_data_size) {
577 fprintf(stderr, " transparency: %6d\n",
578 stats->alpha_data_size);
579 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700580 if (stats->layer_data_size) {
581 fprintf(stderr, " enhancement: %6d\n",
582 stats->layer_data_size);
583 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800584 fprintf(stderr, " Residuals bytes "
585 "|segment 1|segment 2|segment 3"
586 "|segment 4| total\n");
587 fprintf(stderr, " intra4-coeffs: ");
588 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
589 fprintf(stderr, " intra16-coeffs: ");
590 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
591 fprintf(stderr, " chroma coeffs: ");
592 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
593 fprintf(stderr, " macroblocks: ");
594 PrintPercents(stats->segment_size, total);
595 fprintf(stderr, " quantizer: ");
596 PrintValues(stats->segment_quant);
597 fprintf(stderr, " filter level: ");
598 PrintValues(stats->segment_level);
599 fprintf(stderr, "------------------+---------");
600 fprintf(stderr, "+---------+---------+---------+-----------------\n");
601 fprintf(stderr, " segments total: ");
602 PrintByteCount(totals, stats->coded_size, NULL);
603 }
604 }
605 if (pic->extra_info) {
606 const int mb_w = (pic->width + 15) / 16;
607 const int mb_h = (pic->height + 15) / 16;
608 const int type = pic->extra_info_type;
609 int x, y;
610 for (y = 0; y < mb_h; ++y) {
611 for (x = 0; x < mb_w; ++x) {
612 const int c = pic->extra_info[x + y * mb_w];
613 if (type == 1) { // intra4/intra16
614 printf("%c", "+."[c]);
615 } else if (type == 2) { // segments
616 printf("%c", ".-*X"[c]);
617 } else if (type == 3) { // quantizers
618 printf("%.2d ", c);
619 } else if (type == 6 || type == 7) {
620 printf("%3d ", c);
621 } else {
622 printf("0x%.2x ", c);
623 }
624 }
625 printf("\n");
626 }
627 }
628}
629
James Zernc7e86ab2011-08-25 14:22:32 -0700630//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800631
632static int MyWriter(const uint8_t* data, size_t data_size,
633 const WebPPicture* const pic) {
634 FILE* const out = (FILE*)pic->custom_ptr;
635 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
636}
637
638// Dumps a picture as a PGM file using the IMC4 layout.
639static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
640 int y;
641 const int uv_width = (picture->width + 1) / 2;
642 const int uv_height = (picture->height + 1) / 2;
643 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700644 const int alpha_height = picture->a ? picture->height : 0;
645 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800646 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800647 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800648 fprintf(f, "P5\n%d %d\n255\n", stride, height);
649 for (y = 0; y < picture->height; ++y) {
650 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
651 return 0;
652 if (picture->width & 1) fputc(0, f); // pad
653 }
654 for (y = 0; y < uv_height; ++y) {
655 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
656 return 0;
657 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
658 return 0;
659 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700660 for (y = 0; y < alpha_height; ++y) {
661 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
662 return 0;
663 if (picture->width & 1) fputc(0, f); // pad
664 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800665 fclose(f);
666 return 1;
667}
668
James Zernc7e86ab2011-08-25 14:22:32 -0700669//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800670
Pascal Massimino30971c92011-12-01 02:24:50 -0800671static int ProgressReport(int percent, const WebPPicture* const picture) {
672 printf("[%s]: %3d %% \r",
673 (char*)picture->stats->user_data, percent);
674 fflush(stdout);
675 return 1; // all ok
676}
677
678//------------------------------------------------------------------------------
679
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700680static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800681 printf("Usage:\n\n");
682 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
683 printf("where quality is between 0 (poor) to 100 (very good).\n");
684 printf("Typical value is around 80.\n\n");
685 printf("Try -longhelp for an exhaustive list of advanced options.\n");
686}
687
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700688static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800689 printf("Usage:\n");
690 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
691 printf("If input size (-s) for an image is not specified, "
692 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700693#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800694 printf("Windows builds can take as input any of the files handled by WIC\n");
695#endif
696 printf("options:\n");
697 printf(" -h / -help ............ short help\n");
698 printf(" -H / -longhelp ........ long help\n");
699 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530700 printf(" -alpha_q <int> ......... Transparency-compression quality "
701 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800702 printf(" -preset <string> ....... Preset setting, one of:\n");
703 printf(" default, photo, picture,\n");
704 printf(" drawing, icon, text\n");
705 printf(" -preset must come first, as it overwrites other parameters.");
706 printf("\n");
707 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
708 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700709 printf(" -size <int> ............ Target size (in bytes)\n");
710 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 printf("\n");
712 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
713 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
714 printf(" -f <int> ............... filter strength (0=off..100)\n");
715 printf(" -sharpness <int> ....... "
716 "filter sharpness (0:most .. 7:least sharp)\n");
717 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700718 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
719 printf(" "
720 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800721 printf(" -pass <int> ............ analysis pass number (1..10)\n");
722 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700723 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
724#ifdef WEBP_EXPERIMENTAL_FEATURES
725 printf(" -444 / -422 / -gray ..... Change colorspace\n");
726#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800727 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800728 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
729 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530731 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800732 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
733 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000734 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530735 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700736
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800737 printf("\n");
738 printf(" -short ................. condense printed message\n");
739 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700740 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700741#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700742 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700743#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800744 printf(" -v ..................... verbose, e.g. print encoding/decoding "
745 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800746 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800747 printf("\n");
748 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800749 printf(" -af .................... auto-adjust filter strength.\n");
750 printf(" -pre <int> ............. pre-processing filter\n");
751 printf("\n");
752}
753
James Zernc7e86ab2011-08-25 14:22:32 -0700754//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700755// Error messages
756
757static const char* const kErrorMessages[] = {
758 "OK",
759 "OUT_OF_MEMORY: Out of memory allocating objects",
760 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
761 "NULL_PARAMETER: NULL parameter passed to function",
762 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700763 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
764 "allowed is 16383 pixels.",
765 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
766 "To reduce the size of this partition, try using less segments "
767 "with the -segments option, and eventually reduce the number of "
768 "header bits using -partition_limit. More details are available "
769 "in the manual (`man cwebp`)",
770 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800771 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800772 "FILE_TOO_BIG: File would be too big to fit in 4G",
773 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700774};
775
James Zernc7e86ab2011-08-25 14:22:32 -0700776//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777
778int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700779 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800780 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
781 FILE *out = NULL;
782 int c;
783 int short_output = 0;
784 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530785 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800786 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700787 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800788 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800790 int print_distortion = 0; // 1=PSNR, 2=SSIM
791 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800792 WebPConfig config;
793 WebPAuxStats stats;
794 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700795
Pascal Massiminod61479f2012-01-20 07:20:56 -0800796 if (!WebPPictureInit(&picture) ||
797 !WebPPictureInit(&original_picture) ||
798 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799 fprintf(stderr, "Error! Version mismatch!\n");
800 goto Error;
801 }
802
803 if (argc == 1) {
804 HelpShort();
805 return 0;
806 }
807
808 for (c = 1; c < argc; ++c) {
809 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
810 HelpShort();
811 return 0;
812 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
813 HelpLong();
814 return 0;
815 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
816 out_file = argv[++c];
817 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
818 dump_file = argv[++c];
819 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800820 } else if (!strcmp(argv[c], "-print_ssim")) {
821 config.show_compressed = 1;
822 print_distortion = 2;
823 } else if (!strcmp(argv[c], "-print_psnr")) {
824 config.show_compressed = 1;
825 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800826 } else if (!strcmp(argv[c], "-short")) {
827 short_output++;
828 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700829 picture.width = strtol(argv[++c], NULL, 0);
830 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800831 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700832 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800833 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200834 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530835 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
836 config.alpha_quality = strtol(argv[++c], NULL, 0);
837 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
838 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000839 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
840 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530841 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800842 ++c;
843 if (!strcmp(argv[c], "none")) {
844 config.alpha_filtering = 0;
845 } else if (!strcmp(argv[c], "fast")) {
846 config.alpha_filtering = 1;
847 } else if (!strcmp(argv[c], "best")) {
848 config.alpha_filtering = 2;
849 } else {
850 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
851 goto Error;
852 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530853 } else if (!strcmp(argv[c], "-noalpha")) {
854 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000855#ifdef USE_LOSSLESS_ENCODER
856 } else if (!strcmp(argv[c], "-lossless")) {
857 config.lossless = 1;
858 picture.use_argb_input = 1;
859#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800860 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700861 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800862 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200863 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800864 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700865 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800866 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700867 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800868 } else if (!strcmp(argv[c], "-af")) {
869 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700870 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800871 config.filter_type = 1;
872 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700873 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800874 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700875 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800876 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700877 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800878 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700879 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700880 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
881 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800882 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700883 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700884#ifdef WEBP_EXPERIMENTAL_FEATURES
885 } else if (!strcmp(argv[c], "-444")) {
886 picture.colorspace = WEBP_YUV444;
887 } else if (!strcmp(argv[c], "-422")) {
888 picture.colorspace = WEBP_YUV422;
889 } else if (!strcmp(argv[c], "-gray")) {
890 picture.colorspace = WEBP_YUV400;
891#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800892 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
893 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700894 crop_x = strtol(argv[++c], NULL, 0);
895 crop_y = strtol(argv[++c], NULL, 0);
896 crop_w = strtol(argv[++c], NULL, 0);
897 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700898 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
899 resize_w = strtol(argv[++c], NULL, 0);
900 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700901#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700902 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000903 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700904#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700905 } else if (!strcmp(argv[c], "-version")) {
906 const int version = WebPGetEncoderVersion();
907 printf("%d.%d.%d\n",
908 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
909 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800910 } else if (!strcmp(argv[c], "-progress")) {
911 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800912 } else if (!strcmp(argv[c], "-quiet")) {
913 quiet = 1;
914 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
915 WebPPreset preset;
916 ++c;
917 if (!strcmp(argv[c], "default")) {
918 preset = WEBP_PRESET_DEFAULT;
919 } else if (!strcmp(argv[c], "photo")) {
920 preset = WEBP_PRESET_PHOTO;
921 } else if (!strcmp(argv[c], "picture")) {
922 preset = WEBP_PRESET_PICTURE;
923 } else if (!strcmp(argv[c], "drawing")) {
924 preset = WEBP_PRESET_DRAWING;
925 } else if (!strcmp(argv[c], "icon")) {
926 preset = WEBP_PRESET_ICON;
927 } else if (!strcmp(argv[c], "text")) {
928 preset = WEBP_PRESET_TEXT;
929 } else {
930 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
931 goto Error;
932 }
933 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700934 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800935 goto Error;
936 }
937 } else if (!strcmp(argv[c], "-v")) {
938 verbose = 1;
939 } else if (argv[c][0] == '-') {
940 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
941 HelpLong();
942 return -1;
943 } else {
944 in_file = argv[c];
945 }
946 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700947 if (in_file == NULL) {
948 fprintf(stderr, "No input file specified!\n");
949 HelpShort();
950 goto Error;
951 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800952
953 if (!WebPValidateConfig(&config)) {
954 fprintf(stderr, "Error! Invalid configuration.\n");
955 goto Error;
956 }
957
Pascal Massimino0744e842011-02-25 12:03:27 -0800958 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700959 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800960 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700961 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700962 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800963 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700964 goto Error;
965 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800966 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
967
Pascal Massimino0744e842011-02-25 12:03:27 -0800968 if (verbose) {
969 const double time = StopwatchReadAndReset(&stop_watch);
970 fprintf(stderr, "Time to read input: %.3fs\n", time);
971 }
972
973 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800974 if (out_file) {
975 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800976 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800977 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
978 goto Error;
979 } else {
980 if (!short_output && !quiet) {
981 fprintf(stderr, "Saving file '%s'\n", out_file);
982 }
983 }
984 picture.writer = MyWriter;
985 picture.custom_ptr = (void*)out;
986 } else {
987 out = NULL;
988 if (!quiet && !short_output) {
989 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
990 fprintf(stderr, "be performed, but its results discarded.\n\n");
991 }
992 }
993 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -0800994 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800995
Pascal Massimino0744e842011-02-25 12:03:27 -0800996 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700997 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800998 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700999 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001000 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
1001 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001002 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -07001003 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001004 if ((resize_w | resize_h) > 0) {
1005 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1006 fprintf(stderr, "Error! Cannot resize picture\n");
1007 goto Error;
1008 }
1009 }
1010 if (picture.extra_info_type > 0) {
1011 AllocExtraInfo(&picture);
1012 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001013 if (print_distortion > 0) { // Save original picture for later comparison
1014 WebPPictureCopy(&picture, &original_picture);
1015 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001016 if (!WebPEncode(&config, &picture)) {
1017 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001018 fprintf(stderr, "Error code: %d (%s)\n",
1019 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001020 goto Error;
1021 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001022 if (verbose) {
1023 const double time = StopwatchReadAndReset(&stop_watch);
1024 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1025 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001026
1027 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001028 if (dump_file) {
1029 DumpPicture(&picture, dump_file);
1030 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001031
Pascal Massimino38369c02011-05-04 22:59:51 -07001032 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301033 if (config.lossless) {
1034 PrintExtraInfoLossless(&picture, short_output, in_file);
1035 } else {
1036 PrintExtraInfoLossy(&picture, short_output, in_file);
1037 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001038 }
1039 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1040 float values[5];
1041 WebPPictureDistortion(&picture, &original_picture,
1042 (print_distortion == 1) ? 0 : 1, values);
1043 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1044 (print_distortion == 1) ? "PSNR" : "SSIM",
1045 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001046 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001047 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001048
1049 Error:
1050 free(picture.extra_info);
1051 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001052 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001053 if (out != NULL) {
1054 fclose(out);
1055 }
1056
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001057 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001058}
1059
James Zernc7e86ab2011-08-25 14:22:32 -07001060//------------------------------------------------------------------------------