blob: 09c788de0f2acbafba532050c9596ec6fbbb5ba9 [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08002//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// simple command line calling the WebPEncode function.
9// Encodes a raw .YUV into WebP bitstream
10//
11// Author: Skal (pascal.massimino@gmail.com)
12
13#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070014#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080015#include <string.h>
16
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#ifdef WEBP_HAVE_PNG
22#include <png.h>
23#endif
24
25#ifdef WEBP_HAVE_JPEG
26#include <setjmp.h> // note: this must be included *after* png.h
27#include <jpeglib.h>
28#endif
29
James Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080042
43
44#include "webp/encode.h"
James Zern00b29e22012-05-15 13:48:11 -070045#include "./stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070046#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070047#if defined(__cplusplus) || defined(c_plusplus)
48extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070049#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070050
James Zern04e84cf2011-11-04 15:20:08 -070051extern void* VP8GetCPUInfo; // opaque forward declaration.
52
53#if defined(__cplusplus) || defined(c_plusplus)
54} // extern "C"
55#endif
56#endif // WEBP_DLL
57
James Zernc7e86ab2011-08-25 14:22:32 -070058//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080059
60static int verbose = 0;
61
62static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
63 const int uv_width = (pic->width + 1) / 2;
64 const int uv_height = (pic->height + 1) / 2;
65 int y;
66 int ok = 0;
67
68 if (!WebPPictureAlloc(pic)) return ok;
69
70 for (y = 0; y < pic->height; ++y) {
71 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
72 goto End;
73 }
74 }
75 for (y = 0; y < uv_height; ++y) {
76 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
77 goto End;
78 }
79 for (y = 0; y < uv_height; ++y) {
80 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
81 goto End;
82 }
83 ok = 1;
84
85 End:
86 return ok;
87}
88
James Zern31f9dc62011-06-06 17:56:50 -070089#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080090
91#define IFS(fn) \
92 do { \
93 if (SUCCEEDED(hr)) \
94 { \
95 hr = (fn); \
96 if (FAILED(hr) && verbose) \
James Zern974aaff2012-01-24 12:46:46 -080097 fprintf(stderr, #fn " failed %08x\n", hr); \
Pascal Massiminof61d14a2011-02-18 23:33:46 -080098 } \
99 } while (0)
100
James Zern902d3e32012-05-08 17:49:39 -0700101// modified version of DEFINE_GUID from guiddef.h.
102#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
103 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
104
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800105#ifdef __cplusplus
106#define MAKE_REFGUID(x) (x)
107#else
108#define MAKE_REFGUID(x) &(x)
109#endif
110
111static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
112 HRESULT hr = S_OK;
113 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
114 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800115 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800116 return hr;
117}
118
119static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700120 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800121 HRESULT hr = S_OK;
122 IWICBitmapFrameDecode* pFrame = NULL;
123 IWICFormatConverter* pConverter = NULL;
124 IWICImagingFactory* pFactory = NULL;
125 IWICBitmapDecoder* pDecoder = NULL;
126 IStream* pStream = NULL;
127 UINT frameCount = 0;
128 UINT width, height = 0;
129 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700130 WICPixelFormatGUID srcPixelFormat = { 0 };
131 GUID srcContainerFormat = { 0 };
132 const GUID* alphaContainers[] = {
133 &GUID_ContainerFormatBmp,
134 &GUID_ContainerFormatPng,
135 &GUID_ContainerFormatTiff
136 };
137 int has_alpha = 0;
138 int i, stride;
James Zern902d3e32012-05-08 17:49:39 -0700139 // From Microsoft SDK 7.0a
140 // Create local copies for compatibility when building against earlier
141 // versions of the SDK.
142 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
143 0x6fddc324, 0x4e03, 0x4bfe,
144 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
145 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
146 0xf5c7ad2d, 0x6a8d, 0x43dd,
147 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
148 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
149 0x6fddc324, 0x4e03, 0x4bfe,
150 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800151
152 IFS(CoInitialize(NULL));
153 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
154 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
155 (LPVOID*)&pFactory));
156 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800157 fprintf(stderr,
158 "Couldn't access Windows Imaging Component (are you running "
159 "Windows XP SP3 or newer?). Most formats not available. "
160 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800161 }
162 // Prepare for image decoding.
163 IFS(OpenInputStream(filename, &pStream));
164 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
165 WICDecodeMetadataCacheOnDemand, &pDecoder));
166 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
167 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800168 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800169 hr = E_FAIL;
170 }
171 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700172 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
173 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
174
175 has_alpha = keep_alpha;
176 for (i = 0;
177 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
178 ++i) {
James Zern1d38b252012-05-08 18:09:42 -0700179 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
180 MAKE_REFGUID(*alphaContainers[i]))) {
James Zern3cfe0882011-06-21 18:29:30 -0700181 has_alpha =
James Zern1d38b252012-05-08 18:09:42 -0700182 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
183 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
184 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
185 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
James Zern3cfe0882011-06-21 18:29:30 -0700186 break;
187 }
188 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800189
190 // Prepare for pixel format conversion (if necessary).
191 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
192 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700193 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
194 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700195 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800196 NULL, 0.0, WICBitmapPaletteTypeCustom));
197
198 // Decode.
199 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700200 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800201 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700202 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800203 if (rgb == NULL)
204 hr = E_OUTOFMEMORY;
205 }
James Zern3cfe0882011-06-21 18:29:30 -0700206 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
207 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800208
209 // WebP conversion.
210 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700211 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800212 pic->width = width;
213 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700214 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
215 : WebPPictureImportRGB(pic, rgb, stride);
216 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800217 hr = E_FAIL;
218 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000219 if (SUCCEEDED(hr)) {
220 if (has_alpha && keep_alpha == 2) {
221 WebPCleanupTransparentArea(pic);
222 }
223 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800224
225 // Cleanup.
226 if (pConverter != NULL) IUnknown_Release(pConverter);
227 if (pFrame != NULL) IUnknown_Release(pFrame);
228 if (pDecoder != NULL) IUnknown_Release(pDecoder);
229 if (pFactory != NULL) IUnknown_Release(pFactory);
230 if (pStream != NULL) IUnknown_Release(pStream);
231 free(rgb);
232 return hr;
233}
234
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700235static int ReadPicture(const char* const filename, WebPPicture* const pic,
236 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800237 int ok;
238 if (pic->width != 0 && pic->height != 0) {
239 // If image size is specified, infer it as YUV format.
240 FILE* in_file = fopen(filename, "rb");
241 if (in_file == NULL) {
242 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
243 return 0;
244 }
245 ok = ReadYUV(in_file, pic);
246 fclose(in_file);
247 } else {
248 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700249 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800250 }
251 if (!ok) {
252 fprintf(stderr, "Error! Could not process file %s\n", filename);
253 }
254 return ok;
255}
256
James Zern31f9dc62011-06-06 17:56:50 -0700257#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800258
259#ifdef WEBP_HAVE_JPEG
260struct my_error_mgr {
261 struct jpeg_error_mgr pub;
262 jmp_buf setjmp_buffer;
263};
264
265static void my_error_exit(j_common_ptr dinfo) {
266 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
267 (*dinfo->err->output_message) (dinfo);
268 longjmp(myerr->setjmp_buffer, 1);
269}
270
271static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
272 int ok = 0;
273 int stride, width, height;
274 uint8_t* rgb = NULL;
275 uint8_t* row_ptr = NULL;
276 struct jpeg_decompress_struct dinfo;
277 struct my_error_mgr jerr;
278 JSAMPARRAY buffer;
279
280 dinfo.err = jpeg_std_error(&jerr.pub);
281 jerr.pub.error_exit = my_error_exit;
282
James Zern04e84cf2011-11-04 15:20:08 -0700283 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800284 Error:
285 jpeg_destroy_decompress(&dinfo);
286 goto End;
287 }
288
289 jpeg_create_decompress(&dinfo);
290 jpeg_stdio_src(&dinfo, in_file);
291 jpeg_read_header(&dinfo, TRUE);
292
293 dinfo.out_color_space = JCS_RGB;
294 dinfo.dct_method = JDCT_IFAST;
295 dinfo.do_fancy_upsampling = TRUE;
296
297 jpeg_start_decompress(&dinfo);
298
299 if (dinfo.output_components != 3) {
300 goto Error;
301 }
302
303 width = dinfo.output_width;
304 height = dinfo.output_height;
305 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
306
307 rgb = (uint8_t*)malloc(stride * height);
308 if (rgb == NULL) {
309 goto End;
310 }
311 row_ptr = rgb;
312
313 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
314 JPOOL_IMAGE, stride, 1);
315 if (buffer == NULL) {
316 goto End;
317 }
318
319 while (dinfo.output_scanline < dinfo.output_height) {
320 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
321 goto End;
322 }
323 memcpy(row_ptr, buffer[0], stride);
324 row_ptr += stride;
325 }
326
James Zern04e84cf2011-11-04 15:20:08 -0700327 jpeg_finish_decompress(&dinfo);
328 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800329
330 // WebP conversion.
331 pic->width = width;
332 pic->height = height;
333 ok = WebPPictureImportRGB(pic, rgb, stride);
334
335 End:
336 if (rgb) {
337 free(rgb);
338 }
339 return ok;
340}
341
342#else
343static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400344 (void)in_file;
345 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800346 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
347 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800348 return 0;
349}
350#endif
351
352#ifdef WEBP_HAVE_PNG
353static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700354 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800355 longjmp(png_jmpbuf(png), 1);
356}
357
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700358static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800359 png_structp png;
360 png_infop info;
361 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700362 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800363 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700364 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800365 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700366 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800367 int stride;
368 uint8_t* rgb = NULL;
369
370 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
371 if (png == NULL) {
372 goto End;
373 }
374
375 png_set_error_fn(png, 0, error_function, NULL);
376 if (setjmp(png_jmpbuf(png))) {
377 Error:
378 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700379 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800380 goto End;
381 }
382
383 info = png_create_info_struct(png);
384 if (info == NULL) goto Error;
385
386 png_init_io(png, in_file);
387 png_read_info(png, info);
388 if (!png_get_IHDR(png, info,
389 &width, &height, &bit_depth, &color_type, &interlaced,
390 NULL, NULL)) goto Error;
391
392 png_set_strip_16(png);
393 png_set_packing(png);
394 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000395 if (color_type == PNG_COLOR_TYPE_GRAY ||
396 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800397 if (bit_depth < 8) {
398 png_set_expand_gray_1_2_4_to_8(png);
399 }
400 png_set_gray_to_rgb(png);
401 }
402 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
403 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700404 has_alpha = 1;
405 } else {
406 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800407 }
408
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700409 if (!keep_alpha) {
410 png_set_strip_alpha(png);
411 has_alpha = 0;
412 }
413
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800414 num_passes = png_set_interlace_handling(png);
415 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700416 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800417 rgb = (uint8_t*)malloc(stride * height);
418 if (rgb == NULL) goto Error;
419 for (p = 0; p < num_passes; ++p) {
420 for (y = 0; y < height; ++y) {
421 png_bytep row = rgb + y * stride;
422 png_read_rows(png, &row, NULL, 1);
423 }
424 }
425 png_read_end(png, info);
426 png_destroy_read_struct(&png, &info, NULL);
427
428 pic->width = width;
429 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700430 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
431 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800432 free(rgb);
433
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000434 if (ok && has_alpha && keep_alpha == 2) {
435 WebPCleanupTransparentArea(pic);
436 }
437
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800438 End:
439 return ok;
440}
441#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700442static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400443 (void)in_file;
444 (void)pic;
445 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800446 fprintf(stderr, "PNG support not compiled. Please install the libpng "
447 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800448 return 0;
449}
450#endif
451
452typedef enum {
453 PNG = 0,
454 JPEG,
James Zerna0b27362012-01-27 17:39:47 -0800455 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800456} InputFileFormat;
457
458static InputFileFormat GetImageType(FILE* in_file) {
459 InputFileFormat format = UNSUPPORTED;
460 unsigned int magic;
461 unsigned char buf[4];
462
463 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
464 (fseek(in_file, 0, SEEK_SET) != 0)) {
465 return format;
466 }
467
468 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
469 if (magic == 0x89504E47U) {
470 format = PNG;
471 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
472 format = JPEG;
473 }
474 return format;
475}
476
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700477static int ReadPicture(const char* const filename, WebPPicture* const pic,
478 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800479 int ok = 0;
480 FILE* in_file = fopen(filename, "rb");
481 if (in_file == NULL) {
482 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
483 return ok;
484 }
485
486 if (pic->width == 0 || pic->height == 0) {
487 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
488 const InputFileFormat format = GetImageType(in_file);
489 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700490 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800491 } else if (format == JPEG) {
492 ok = ReadJPEG(in_file, pic);
493 }
494 } else {
495 // If image size is specified, infer it as YUV format.
496 ok = ReadYUV(in_file, pic);
497 }
498 if (!ok) {
499 fprintf(stderr, "Error! Could not process file %s\n", filename);
500 }
501
502 fclose(in_file);
503 return ok;
504}
505
James Zern31f9dc62011-06-06 17:56:50 -0700506#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800507
508static void AllocExtraInfo(WebPPicture* const pic) {
509 const int mb_w = (pic->width + 15) / 16;
510 const int mb_h = (pic->height + 15) / 16;
511 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
512}
513
514static void PrintByteCount(const int bytes[4], int total_size,
515 int* const totals) {
516 int s;
517 int total = 0;
518 for (s = 0; s < 4; ++s) {
519 fprintf(stderr, "| %7d ", bytes[s]);
520 total += bytes[s];
521 if (totals) totals[s] += bytes[s];
522 }
James Zern04e84cf2011-11-04 15:20:08 -0700523 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800524}
525
526static void PrintPercents(const int counts[4], int total) {
527 int s;
528 for (s = 0; s < 4; ++s) {
529 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
530 }
James Zern04e84cf2011-11-04 15:20:08 -0700531 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800532}
533
534static void PrintValues(const int values[4]) {
535 int s;
536 for (s = 0; s < 4; ++s) {
537 fprintf(stderr, "| %7d ", values[s]);
538 }
James Zern04e84cf2011-11-04 15:20:08 -0700539 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800540}
541
Vikas Arorac4ccab62012-05-09 11:27:46 +0530542static void PrintExtraInfoLossless(const WebPPicture* const pic,
543 int short_output,
544 const char* const file_name) {
545 const WebPAuxStats* const stats = pic->stats;
546 if (short_output) {
547 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
548 } else {
549 fprintf(stderr, "File: %s\n", file_name);
550 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
551 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
552 }
553}
554
555static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
556 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800557 const WebPAuxStats* const stats = pic->stats;
558 if (short_output) {
559 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700560 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800561 const int num_i4 = stats->block_count[0];
562 const int num_i16 = stats->block_count[1];
563 const int num_skip = stats->block_count[2];
564 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800565 fprintf(stderr, "File: %s\n", file_name);
566 fprintf(stderr, "Dimension: %d x %d%s\n",
567 pic->width, pic->height, (pic->a != NULL) ? " (with alpha)" : "");
568 fprintf(stderr, "Output: "
569 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800570 stats->coded_size,
571 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
572 if (total > 0) {
573 int totals[4] = { 0, 0, 0, 0 };
574 fprintf(stderr, "block count: intra4: %d\n"
575 " intra16: %d (-> %.2f%%)\n",
576 num_i4, num_i16, 100.f * num_i16 / total);
577 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
578 num_skip, 100.f * num_skip / total);
579 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
580 " mode-partition: %6d (%.1f%%)\n",
581 stats->header_bytes[0],
582 100.f * stats->header_bytes[0] / stats->coded_size,
583 stats->header_bytes[1],
584 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700585 if (stats->alpha_data_size) {
586 fprintf(stderr, " transparency: %6d\n",
587 stats->alpha_data_size);
588 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700589 if (stats->layer_data_size) {
590 fprintf(stderr, " enhancement: %6d\n",
591 stats->layer_data_size);
592 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800593 fprintf(stderr, " Residuals bytes "
594 "|segment 1|segment 2|segment 3"
595 "|segment 4| total\n");
596 fprintf(stderr, " intra4-coeffs: ");
597 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
598 fprintf(stderr, " intra16-coeffs: ");
599 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
600 fprintf(stderr, " chroma coeffs: ");
601 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
602 fprintf(stderr, " macroblocks: ");
603 PrintPercents(stats->segment_size, total);
604 fprintf(stderr, " quantizer: ");
605 PrintValues(stats->segment_quant);
606 fprintf(stderr, " filter level: ");
607 PrintValues(stats->segment_level);
608 fprintf(stderr, "------------------+---------");
609 fprintf(stderr, "+---------+---------+---------+-----------------\n");
610 fprintf(stderr, " segments total: ");
611 PrintByteCount(totals, stats->coded_size, NULL);
612 }
613 }
614 if (pic->extra_info) {
615 const int mb_w = (pic->width + 15) / 16;
616 const int mb_h = (pic->height + 15) / 16;
617 const int type = pic->extra_info_type;
618 int x, y;
619 for (y = 0; y < mb_h; ++y) {
620 for (x = 0; x < mb_w; ++x) {
621 const int c = pic->extra_info[x + y * mb_w];
622 if (type == 1) { // intra4/intra16
623 printf("%c", "+."[c]);
624 } else if (type == 2) { // segments
625 printf("%c", ".-*X"[c]);
626 } else if (type == 3) { // quantizers
627 printf("%.2d ", c);
628 } else if (type == 6 || type == 7) {
629 printf("%3d ", c);
630 } else {
631 printf("0x%.2x ", c);
632 }
633 }
634 printf("\n");
635 }
636 }
637}
638
James Zernc7e86ab2011-08-25 14:22:32 -0700639//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800640
641static int MyWriter(const uint8_t* data, size_t data_size,
642 const WebPPicture* const pic) {
643 FILE* const out = (FILE*)pic->custom_ptr;
644 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
645}
646
647// Dumps a picture as a PGM file using the IMC4 layout.
648static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
649 int y;
650 const int uv_width = (picture->width + 1) / 2;
651 const int uv_height = (picture->height + 1) / 2;
652 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700653 const int alpha_height = picture->a ? picture->height : 0;
654 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800655 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800656 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800657 fprintf(f, "P5\n%d %d\n255\n", stride, height);
658 for (y = 0; y < picture->height; ++y) {
659 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
660 return 0;
661 if (picture->width & 1) fputc(0, f); // pad
662 }
663 for (y = 0; y < uv_height; ++y) {
664 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
665 return 0;
666 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
667 return 0;
668 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700669 for (y = 0; y < alpha_height; ++y) {
670 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
671 return 0;
672 if (picture->width & 1) fputc(0, f); // pad
673 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674 fclose(f);
675 return 1;
676}
677
James Zernc7e86ab2011-08-25 14:22:32 -0700678//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800679
Pascal Massimino30971c92011-12-01 02:24:50 -0800680static int ProgressReport(int percent, const WebPPicture* const picture) {
681 printf("[%s]: %3d %% \r",
682 (char*)picture->stats->user_data, percent);
683 fflush(stdout);
684 return 1; // all ok
685}
686
687//------------------------------------------------------------------------------
688
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700689static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800690 printf("Usage:\n\n");
691 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
692 printf("where quality is between 0 (poor) to 100 (very good).\n");
693 printf("Typical value is around 80.\n\n");
694 printf("Try -longhelp for an exhaustive list of advanced options.\n");
695}
696
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700697static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800698 printf("Usage:\n");
699 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
700 printf("If input size (-s) for an image is not specified, "
701 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700702#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800703 printf("Windows builds can take as input any of the files handled by WIC\n");
704#endif
705 printf("options:\n");
706 printf(" -h / -help ............ short help\n");
707 printf(" -H / -longhelp ........ long help\n");
708 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530709 printf(" -alpha_q <int> ......... Transparency-compression quality "
710 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 printf(" -preset <string> ....... Preset setting, one of:\n");
712 printf(" default, photo, picture,\n");
713 printf(" drawing, icon, text\n");
714 printf(" -preset must come first, as it overwrites other parameters.");
715 printf("\n");
716 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
717 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700718 printf(" -size <int> ............ Target size (in bytes)\n");
719 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800720 printf("\n");
721 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
722 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
723 printf(" -f <int> ............... filter strength (0=off..100)\n");
724 printf(" -sharpness <int> ....... "
725 "filter sharpness (0:most .. 7:least sharp)\n");
726 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700727 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
728 printf(" "
729 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 printf(" -pass <int> ............ analysis pass number (1..10)\n");
731 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700732 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
733#ifdef WEBP_EXPERIMENTAL_FEATURES
734 printf(" -444 / -422 / -gray ..... Change colorspace\n");
735#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800736 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800737 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
738 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800739 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530740 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800741 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
742 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000743 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530744 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700745
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800746 printf("\n");
747 printf(" -short ................. condense printed message\n");
748 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700749 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700750#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700751 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700752#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753 printf(" -v ..................... verbose, e.g. print encoding/decoding "
754 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800755 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800756 printf("\n");
757 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800758 printf(" -af .................... auto-adjust filter strength.\n");
759 printf(" -pre <int> ............. pre-processing filter\n");
760 printf("\n");
761}
762
James Zernc7e86ab2011-08-25 14:22:32 -0700763//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700764// Error messages
765
766static const char* const kErrorMessages[] = {
767 "OK",
768 "OUT_OF_MEMORY: Out of memory allocating objects",
769 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
770 "NULL_PARAMETER: NULL parameter passed to function",
771 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700772 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
773 "allowed is 16383 pixels.",
774 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
775 "To reduce the size of this partition, try using less segments "
776 "with the -segments option, and eventually reduce the number of "
777 "header bits using -partition_limit. More details are available "
778 "in the manual (`man cwebp`)",
779 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800780 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800781 "FILE_TOO_BIG: File would be too big to fit in 4G",
782 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700783};
784
James Zernc7e86ab2011-08-25 14:22:32 -0700785//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800786
787int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700788 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
790 FILE *out = NULL;
791 int c;
792 int short_output = 0;
793 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530794 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800795 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700796 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800797 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800798 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800799 int print_distortion = 0; // 1=PSNR, 2=SSIM
800 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800801 WebPConfig config;
802 WebPAuxStats stats;
803 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700804
Pascal Massiminod61479f2012-01-20 07:20:56 -0800805 if (!WebPPictureInit(&picture) ||
806 !WebPPictureInit(&original_picture) ||
807 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800808 fprintf(stderr, "Error! Version mismatch!\n");
809 goto Error;
810 }
811
812 if (argc == 1) {
813 HelpShort();
814 return 0;
815 }
816
817 for (c = 1; c < argc; ++c) {
818 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
819 HelpShort();
820 return 0;
821 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
822 HelpLong();
823 return 0;
824 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
825 out_file = argv[++c];
826 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
827 dump_file = argv[++c];
828 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800829 } else if (!strcmp(argv[c], "-print_ssim")) {
830 config.show_compressed = 1;
831 print_distortion = 2;
832 } else if (!strcmp(argv[c], "-print_psnr")) {
833 config.show_compressed = 1;
834 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 } else if (!strcmp(argv[c], "-short")) {
836 short_output++;
837 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700838 picture.width = strtol(argv[++c], NULL, 0);
839 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800840 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700841 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800842 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200843 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530844 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
845 config.alpha_quality = strtol(argv[++c], NULL, 0);
846 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
847 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000848 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
849 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530850 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800851 ++c;
852 if (!strcmp(argv[c], "none")) {
853 config.alpha_filtering = 0;
854 } else if (!strcmp(argv[c], "fast")) {
855 config.alpha_filtering = 1;
856 } else if (!strcmp(argv[c], "best")) {
857 config.alpha_filtering = 2;
858 } else {
859 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
860 goto Error;
861 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530862 } else if (!strcmp(argv[c], "-noalpha")) {
863 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000864#ifdef USE_LOSSLESS_ENCODER
865 } else if (!strcmp(argv[c], "-lossless")) {
866 config.lossless = 1;
867 picture.use_argb_input = 1;
868#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800869 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700870 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800871 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200872 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800873 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700874 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800875 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700876 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800877 } else if (!strcmp(argv[c], "-af")) {
878 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700879 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800880 config.filter_type = 1;
881 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700882 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800883 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700884 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700886 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800887 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700888 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700889 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
890 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800891 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700892 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700893#ifdef WEBP_EXPERIMENTAL_FEATURES
894 } else if (!strcmp(argv[c], "-444")) {
895 picture.colorspace = WEBP_YUV444;
896 } else if (!strcmp(argv[c], "-422")) {
897 picture.colorspace = WEBP_YUV422;
898 } else if (!strcmp(argv[c], "-gray")) {
899 picture.colorspace = WEBP_YUV400;
900#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800901 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
902 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700903 crop_x = strtol(argv[++c], NULL, 0);
904 crop_y = strtol(argv[++c], NULL, 0);
905 crop_w = strtol(argv[++c], NULL, 0);
906 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700907 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
908 resize_w = strtol(argv[++c], NULL, 0);
909 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700910#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700911 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000912 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700913#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700914 } else if (!strcmp(argv[c], "-version")) {
915 const int version = WebPGetEncoderVersion();
916 printf("%d.%d.%d\n",
917 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
918 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800919 } else if (!strcmp(argv[c], "-progress")) {
920 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800921 } else if (!strcmp(argv[c], "-quiet")) {
922 quiet = 1;
923 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
924 WebPPreset preset;
925 ++c;
926 if (!strcmp(argv[c], "default")) {
927 preset = WEBP_PRESET_DEFAULT;
928 } else if (!strcmp(argv[c], "photo")) {
929 preset = WEBP_PRESET_PHOTO;
930 } else if (!strcmp(argv[c], "picture")) {
931 preset = WEBP_PRESET_PICTURE;
932 } else if (!strcmp(argv[c], "drawing")) {
933 preset = WEBP_PRESET_DRAWING;
934 } else if (!strcmp(argv[c], "icon")) {
935 preset = WEBP_PRESET_ICON;
936 } else if (!strcmp(argv[c], "text")) {
937 preset = WEBP_PRESET_TEXT;
938 } else {
939 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
940 goto Error;
941 }
942 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700943 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800944 goto Error;
945 }
946 } else if (!strcmp(argv[c], "-v")) {
947 verbose = 1;
948 } else if (argv[c][0] == '-') {
949 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
950 HelpLong();
951 return -1;
952 } else {
953 in_file = argv[c];
954 }
955 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700956 if (in_file == NULL) {
957 fprintf(stderr, "No input file specified!\n");
958 HelpShort();
959 goto Error;
960 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800961
962 if (!WebPValidateConfig(&config)) {
963 fprintf(stderr, "Error! Invalid configuration.\n");
964 goto Error;
965 }
966
Pascal Massimino0744e842011-02-25 12:03:27 -0800967 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700968 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800969 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700970 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700971 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800972 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700973 goto Error;
974 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800975 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
976
Pascal Massimino0744e842011-02-25 12:03:27 -0800977 if (verbose) {
978 const double time = StopwatchReadAndReset(&stop_watch);
979 fprintf(stderr, "Time to read input: %.3fs\n", time);
980 }
981
982 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800983 if (out_file) {
984 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800985 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800986 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
987 goto Error;
988 } else {
989 if (!short_output && !quiet) {
990 fprintf(stderr, "Saving file '%s'\n", out_file);
991 }
992 }
993 picture.writer = MyWriter;
994 picture.custom_ptr = (void*)out;
995 } else {
996 out = NULL;
997 if (!quiet && !short_output) {
998 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
999 fprintf(stderr, "be performed, but its results discarded.\n\n");
1000 }
1001 }
1002 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -08001003 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001004
Pascal Massimino0744e842011-02-25 12:03:27 -08001005 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001006 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001007 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001008 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001009 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
1010 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001011 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -07001012 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001013 if ((resize_w | resize_h) > 0) {
1014 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1015 fprintf(stderr, "Error! Cannot resize picture\n");
1016 goto Error;
1017 }
1018 }
1019 if (picture.extra_info_type > 0) {
1020 AllocExtraInfo(&picture);
1021 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001022 if (print_distortion > 0) { // Save original picture for later comparison
1023 WebPPictureCopy(&picture, &original_picture);
1024 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001025 if (!WebPEncode(&config, &picture)) {
1026 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001027 fprintf(stderr, "Error code: %d (%s)\n",
1028 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001029 goto Error;
1030 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001031 if (verbose) {
1032 const double time = StopwatchReadAndReset(&stop_watch);
1033 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1034 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001035
1036 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001037 if (dump_file) {
1038 DumpPicture(&picture, dump_file);
1039 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001040
Pascal Massimino38369c02011-05-04 22:59:51 -07001041 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301042 if (config.lossless) {
1043 PrintExtraInfoLossless(&picture, short_output, in_file);
1044 } else {
1045 PrintExtraInfoLossy(&picture, short_output, in_file);
1046 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001047 }
1048 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1049 float values[5];
1050 WebPPictureDistortion(&picture, &original_picture,
1051 (print_distortion == 1) ? 0 : 1, values);
1052 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1053 (print_distortion == 1) ? "PSNR" : "SSIM",
1054 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001055 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001056 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001057
1058 Error:
1059 free(picture.extra_info);
1060 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001061 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001062 if (out != NULL) {
1063 fclose(out);
1064 }
1065
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001066 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001067}
1068
James Zernc7e86ab2011-08-25 14:22:32 -07001069//------------------------------------------------------------------------------