blob: c99e93959cb08cedb43a2471a619a6d49de09b55 [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 Zern6f76d242012-07-01 17:55:21 -070030#ifdef WEBP_HAVE_TIFF
31#include <tiffio.h>
32#endif
33
James Zern31f9dc62011-06-06 17:56:50 -070034#ifdef HAVE_WINCODEC_H
35#ifdef __MINGW32__
36#define INITGUID // Without this GUIDs are declared extern and fail to link
37#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080038#define CINTERFACE
39#define COBJMACROS
40#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
41 // code with COBJMACROS.
42#include <shlwapi.h>
43#include <windows.h>
44#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070045#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046
47
48#include "webp/encode.h"
James Zern63aba3a2012-12-03 18:20:00 -080049#include "./metadata.h"
James Zern00b29e22012-05-15 13:48:11 -070050#include "./stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070051#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070052#if defined(__cplusplus) || defined(c_plusplus)
53extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070054#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070055
James Zern04e84cf2011-11-04 15:20:08 -070056extern void* VP8GetCPUInfo; // opaque forward declaration.
57
58#if defined(__cplusplus) || defined(c_plusplus)
59} // extern "C"
60#endif
61#endif // WEBP_DLL
62
James Zernc7e86ab2011-08-25 14:22:32 -070063//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080064
65static int verbose = 0;
66
67static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000068 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080069 const int uv_width = (pic->width + 1) / 2;
70 const int uv_height = (pic->height + 1) / 2;
71 int y;
72 int ok = 0;
73
Pascal Massiminodd108172012-07-18 21:58:53 +000074 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080075 if (!WebPPictureAlloc(pic)) return ok;
76
77 for (y = 0; y < pic->height; ++y) {
78 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
79 goto End;
80 }
81 }
82 for (y = 0; y < uv_height; ++y) {
83 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
84 goto End;
85 }
86 for (y = 0; y < uv_height; ++y) {
87 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
88 goto End;
89 }
90 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000091 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080092
93 End:
94 return ok;
95}
96
James Zern31f9dc62011-06-06 17:56:50 -070097#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080098
James Zernf2b5d192012-10-08 18:33:18 -070099#define IFS(fn) \
100 do { \
101 if (SUCCEEDED(hr)) { \
102 hr = (fn); \
103 if (FAILED(hr)) fprintf(stderr, #fn " failed %08x\n", hr); \
104 } \
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800105 } while (0)
106
James Zern902d3e32012-05-08 17:49:39 -0700107// modified version of DEFINE_GUID from guiddef.h.
108#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
109 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
110
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800111#ifdef __cplusplus
112#define MAKE_REFGUID(x) (x)
113#else
114#define MAKE_REFGUID(x) &(x)
115#endif
116
James Zerneda8ee42012-10-19 18:34:06 -0700117typedef struct WICFormatImporter {
118 const GUID* pixel_format;
119 int bytes_per_pixel;
120 int (*import)(WebPPicture* const, const uint8_t* const, int);
121} WICFormatImporter;
122
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800123static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
124 HRESULT hr = S_OK;
125 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
126 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800127 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800128 return hr;
129}
130
131static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700132 WebPPicture* const pic, int keep_alpha) {
James Zerneda8ee42012-10-19 18:34:06 -0700133 // From Microsoft SDK 7.0a -- wincodec.h
134 // Create local copies for compatibility when building against earlier
135 // versions of the SDK.
136 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_,
137 0x6fddc324, 0x4e03, 0x4bfe,
138 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c);
139 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
140 0x6fddc324, 0x4e03, 0x4bfe,
141 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
142 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
143 0x6fddc324, 0x4e03, 0x4bfe,
144 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
145 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
146 0xf5c7ad2d, 0x6a8d, 0x43dd,
147 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
148 const WICFormatImporter alphaFormatImporters[] = {
149 { &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
150 { &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
151 { NULL, 0, NULL },
152 };
153 const WICFormatImporter nonAlphaFormatImporters[] = {
154 { &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
155 { &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
156 { NULL, 0, NULL },
157 };
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800158 HRESULT hr = S_OK;
159 IWICBitmapFrameDecode* pFrame = NULL;
160 IWICFormatConverter* pConverter = NULL;
161 IWICImagingFactory* pFactory = NULL;
162 IWICBitmapDecoder* pDecoder = NULL;
163 IStream* pStream = NULL;
164 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700165 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800166 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700167 WICPixelFormatGUID srcPixelFormat = { 0 };
James Zerneda8ee42012-10-19 18:34:06 -0700168 const WICFormatImporter* importer = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700169 GUID srcContainerFormat = { 0 };
170 const GUID* alphaContainers[] = {
171 &GUID_ContainerFormatBmp,
172 &GUID_ContainerFormatPng,
173 &GUID_ContainerFormatTiff
174 };
175 int has_alpha = 0;
176 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800177
178 IFS(CoInitialize(NULL));
179 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
180 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
181 (LPVOID*)&pFactory));
182 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800183 fprintf(stderr,
184 "Couldn't access Windows Imaging Component (are you running "
185 "Windows XP SP3 or newer?). Most formats not available. "
186 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800187 }
188 // Prepare for image decoding.
189 IFS(OpenInputStream(filename, &pStream));
190 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
191 WICDecodeMetadataCacheOnDemand, &pDecoder));
192 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
193 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800194 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800195 hr = E_FAIL;
196 }
197 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700198 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
199 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
200
James Zernecd66f72012-10-08 18:15:30 -0700201 if (keep_alpha) {
202 for (i = 0;
203 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
204 ++i) {
205 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
206 MAKE_REFGUID(*alphaContainers[i]))) {
207 has_alpha =
208 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
209 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
210 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
211 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
212 break;
213 }
James Zern3cfe0882011-06-21 18:29:30 -0700214 }
215 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800216
217 // Prepare for pixel format conversion (if necessary).
218 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
James Zerneda8ee42012-10-19 18:34:06 -0700219
220 for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters;
221 hr == S_OK && importer->import != NULL; ++importer) {
222 BOOL canConvert;
223 const HRESULT cchr = IWICFormatConverter_CanConvert(
224 pConverter,
225 MAKE_REFGUID(srcPixelFormat),
226 MAKE_REFGUID(*importer->pixel_format),
227 &canConvert);
228 if (SUCCEEDED(cchr) && canConvert) break;
229 }
230 if (importer->import == NULL) hr = E_FAIL;
231
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800232 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zerneda8ee42012-10-19 18:34:06 -0700233 importer->pixel_format,
James Zern3cfe0882011-06-21 18:29:30 -0700234 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800235 NULL, 0.0, WICBitmapPaletteTypeCustom));
236
237 // Decode.
238 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zerneda8ee42012-10-19 18:34:06 -0700239 stride = importer->bytes_per_pixel * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800240 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700241 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800242 if (rgb == NULL)
243 hr = E_OUTOFMEMORY;
244 }
James Zern3cfe0882011-06-21 18:29:30 -0700245 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
246 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800247
248 // WebP conversion.
249 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700250 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800251 pic->width = width;
252 pic->height = height;
James Zerneda8ee42012-10-19 18:34:06 -0700253 ok = importer->import(pic, rgb, stride);
James Zern3cfe0882011-06-21 18:29:30 -0700254 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800255 hr = E_FAIL;
256 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000257 if (SUCCEEDED(hr)) {
258 if (has_alpha && keep_alpha == 2) {
259 WebPCleanupTransparentArea(pic);
260 }
261 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800262
263 // Cleanup.
264 if (pConverter != NULL) IUnknown_Release(pConverter);
265 if (pFrame != NULL) IUnknown_Release(pFrame);
266 if (pDecoder != NULL) IUnknown_Release(pDecoder);
267 if (pFactory != NULL) IUnknown_Release(pFactory);
268 if (pStream != NULL) IUnknown_Release(pStream);
269 free(rgb);
270 return hr;
271}
272
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700273static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800274 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800275 int ok;
James Zern63aba3a2012-12-03 18:20:00 -0800276 (void)metadata; // TODO(jzern): add metadata extraction using WIC
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800277 if (pic->width != 0 && pic->height != 0) {
278 // If image size is specified, infer it as YUV format.
279 FILE* in_file = fopen(filename, "rb");
280 if (in_file == NULL) {
281 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
282 return 0;
283 }
284 ok = ReadYUV(in_file, pic);
285 fclose(in_file);
286 } else {
287 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700288 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800289 }
290 if (!ok) {
291 fprintf(stderr, "Error! Could not process file %s\n", filename);
292 }
293 return ok;
294}
295
James Zern31f9dc62011-06-06 17:56:50 -0700296#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800297
298#ifdef WEBP_HAVE_JPEG
299struct my_error_mgr {
300 struct jpeg_error_mgr pub;
301 jmp_buf setjmp_buffer;
302};
303
304static void my_error_exit(j_common_ptr dinfo) {
305 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
306 (*dinfo->err->output_message) (dinfo);
307 longjmp(myerr->setjmp_buffer, 1);
308}
309
310static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
311 int ok = 0;
312 int stride, width, height;
313 uint8_t* rgb = NULL;
314 uint8_t* row_ptr = NULL;
315 struct jpeg_decompress_struct dinfo;
316 struct my_error_mgr jerr;
317 JSAMPARRAY buffer;
318
319 dinfo.err = jpeg_std_error(&jerr.pub);
320 jerr.pub.error_exit = my_error_exit;
321
James Zern04e84cf2011-11-04 15:20:08 -0700322 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800323 Error:
324 jpeg_destroy_decompress(&dinfo);
325 goto End;
326 }
327
328 jpeg_create_decompress(&dinfo);
329 jpeg_stdio_src(&dinfo, in_file);
330 jpeg_read_header(&dinfo, TRUE);
331
332 dinfo.out_color_space = JCS_RGB;
333 dinfo.dct_method = JDCT_IFAST;
334 dinfo.do_fancy_upsampling = TRUE;
335
336 jpeg_start_decompress(&dinfo);
337
338 if (dinfo.output_components != 3) {
339 goto Error;
340 }
341
342 width = dinfo.output_width;
343 height = dinfo.output_height;
344 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
345
346 rgb = (uint8_t*)malloc(stride * height);
347 if (rgb == NULL) {
348 goto End;
349 }
350 row_ptr = rgb;
351
352 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
353 JPOOL_IMAGE, stride, 1);
354 if (buffer == NULL) {
355 goto End;
356 }
357
358 while (dinfo.output_scanline < dinfo.output_height) {
359 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
360 goto End;
361 }
362 memcpy(row_ptr, buffer[0], stride);
363 row_ptr += stride;
364 }
365
James Zern04e84cf2011-11-04 15:20:08 -0700366 jpeg_finish_decompress(&dinfo);
367 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800368
369 // WebP conversion.
370 pic->width = width;
371 pic->height = height;
372 ok = WebPPictureImportRGB(pic, rgb, stride);
373
374 End:
375 if (rgb) {
376 free(rgb);
377 }
378 return ok;
379}
380
381#else
382static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400383 (void)in_file;
384 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800385 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
386 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800387 return 0;
388}
389#endif
390
391#ifdef WEBP_HAVE_PNG
392static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700393 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800394 longjmp(png_jmpbuf(png), 1);
395}
396
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700397static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800398 png_structp png;
399 png_infop info;
400 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700401 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800402 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700403 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800404 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700405 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800406 int stride;
407 uint8_t* rgb = NULL;
408
409 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
410 if (png == NULL) {
411 goto End;
412 }
413
414 png_set_error_fn(png, 0, error_function, NULL);
415 if (setjmp(png_jmpbuf(png))) {
416 Error:
417 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700418 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800419 goto End;
420 }
421
422 info = png_create_info_struct(png);
423 if (info == NULL) goto Error;
424
425 png_init_io(png, in_file);
426 png_read_info(png, info);
427 if (!png_get_IHDR(png, info,
428 &width, &height, &bit_depth, &color_type, &interlaced,
429 NULL, NULL)) goto Error;
430
431 png_set_strip_16(png);
432 png_set_packing(png);
433 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000434 if (color_type == PNG_COLOR_TYPE_GRAY ||
435 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800436 if (bit_depth < 8) {
437 png_set_expand_gray_1_2_4_to_8(png);
438 }
439 png_set_gray_to_rgb(png);
440 }
441 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
442 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700443 has_alpha = 1;
444 } else {
445 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800446 }
447
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700448 if (!keep_alpha) {
449 png_set_strip_alpha(png);
450 has_alpha = 0;
451 }
452
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800453 num_passes = png_set_interlace_handling(png);
454 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700455 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800456 rgb = (uint8_t*)malloc(stride * height);
457 if (rgb == NULL) goto Error;
458 for (p = 0; p < num_passes; ++p) {
459 for (y = 0; y < height; ++y) {
460 png_bytep row = rgb + y * stride;
461 png_read_rows(png, &row, NULL, 1);
462 }
463 }
464 png_read_end(png, info);
465 png_destroy_read_struct(&png, &info, NULL);
466
467 pic->width = width;
468 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700469 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
470 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800471 free(rgb);
472
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000473 if (ok && has_alpha && keep_alpha == 2) {
474 WebPCleanupTransparentArea(pic);
475 }
476
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800477 End:
478 return ok;
479}
480#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700481static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400482 (void)in_file;
483 (void)pic;
484 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800485 fprintf(stderr, "PNG support not compiled. Please install the libpng "
486 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800487 return 0;
488}
489#endif
490
James Zern6f76d242012-07-01 17:55:21 -0700491#ifdef WEBP_HAVE_TIFF
492static int ReadTIFF(const char* const filename,
493 WebPPicture* const pic, int keep_alpha) {
494 TIFF* const tif = TIFFOpen(filename, "r");
495 uint32 width, height;
496 uint32* raster;
497 int ok = 0;
498 int dircount = 1;
499
500 if (tif == NULL) {
501 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
502 return 0;
503 }
504
505 while (TIFFReadDirectory(tif)) ++dircount;
506
507 if (dircount > 1) {
508 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
509 "Only the first will be used, %d will be ignored.\n",
510 dircount - 1);
511 }
512
513 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
514 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
515 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
516 if (raster != NULL) {
517 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
518 ORIENTATION_TOPLEFT, 1)) {
519 const int stride = width * sizeof(*raster);
520 pic->width = width;
521 pic->height = height;
522 // TIFF data is ABGR
523#ifdef __BIG_ENDIAN__
524 TIFFSwabArrayOfLong(raster, width * height);
525#endif
526 ok = keep_alpha
527 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
528 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
529 }
530 _TIFFfree(raster);
531 } else {
532 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
533 }
534
535 if (ok && keep_alpha == 2) {
536 WebPCleanupTransparentArea(pic);
537 }
538
539 TIFFClose(tif);
540 return ok;
541}
542#else
543static int ReadTIFF(const char* const filename,
544 WebPPicture* const pic, int keep_alpha) {
545 (void)filename;
546 (void)pic;
547 (void)keep_alpha;
548 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
549 "development package before building.\n");
550 return 0;
551}
552#endif
553
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800554typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700555 PNG_ = 0,
556 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700557 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800558 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800559} InputFileFormat;
560
561static InputFileFormat GetImageType(FILE* in_file) {
562 InputFileFormat format = UNSUPPORTED;
563 unsigned int magic;
564 unsigned char buf[4];
565
566 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
567 (fseek(in_file, 0, SEEK_SET) != 0)) {
568 return format;
569 }
570
571 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
572 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700573 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800574 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700575 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700576 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
577 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800578 }
579 return format;
580}
581
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700582static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800583 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800584 int ok = 0;
585 FILE* in_file = fopen(filename, "rb");
James Zern63aba3a2012-12-03 18:20:00 -0800586 (void)metadata; // TODO(jzern): add metadata extraction to the formats below.
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800587 if (in_file == NULL) {
588 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
589 return ok;
590 }
591
592 if (pic->width == 0 || pic->height == 0) {
593 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
594 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700595 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700596 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700597 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800598 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700599 } else if (format == TIFF_) {
600 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800601 }
602 } else {
603 // If image size is specified, infer it as YUV format.
604 ok = ReadYUV(in_file, pic);
605 }
606 if (!ok) {
607 fprintf(stderr, "Error! Could not process file %s\n", filename);
608 }
609
610 fclose(in_file);
611 return ok;
612}
613
James Zern31f9dc62011-06-06 17:56:50 -0700614#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800615
616static void AllocExtraInfo(WebPPicture* const pic) {
617 const int mb_w = (pic->width + 15) / 16;
618 const int mb_h = (pic->height + 15) / 16;
619 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
620}
621
622static void PrintByteCount(const int bytes[4], int total_size,
623 int* const totals) {
624 int s;
625 int total = 0;
626 for (s = 0; s < 4; ++s) {
627 fprintf(stderr, "| %7d ", bytes[s]);
628 total += bytes[s];
629 if (totals) totals[s] += bytes[s];
630 }
James Zern04e84cf2011-11-04 15:20:08 -0700631 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800632}
633
634static void PrintPercents(const int counts[4], int total) {
635 int s;
636 for (s = 0; s < 4; ++s) {
637 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
638 }
James Zern04e84cf2011-11-04 15:20:08 -0700639 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800640}
641
642static void PrintValues(const int values[4]) {
643 int s;
644 for (s = 0; s < 4; ++s) {
645 fprintf(stderr, "| %7d ", values[s]);
646 }
James Zern04e84cf2011-11-04 15:20:08 -0700647 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800648}
649
Pascal Massimino7d853d72012-07-24 16:15:36 -0700650static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
651 const char* const description) {
652 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
653 description, stats->lossless_size);
654 if (stats->lossless_features) {
655 fprintf(stderr, " * Lossless features used:");
656 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
657 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
658 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
659 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
660 fprintf(stderr, "\n");
661 }
662 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
663 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
664 if (stats->palette_size > 0) {
665 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
666 }
667}
668
Vikas Arorac4ccab62012-05-09 11:27:46 +0530669static void PrintExtraInfoLossless(const WebPPicture* const pic,
670 int short_output,
671 const char* const file_name) {
672 const WebPAuxStats* const stats = pic->stats;
673 if (short_output) {
674 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
675 } else {
676 fprintf(stderr, "File: %s\n", file_name);
677 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
678 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700679 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530680 }
681}
682
683static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
684 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800685 const WebPAuxStats* const stats = pic->stats;
686 if (short_output) {
687 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700688 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800689 const int num_i4 = stats->block_count[0];
690 const int num_i16 = stats->block_count[1];
691 const int num_skip = stats->block_count[2];
692 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800693 fprintf(stderr, "File: %s\n", file_name);
694 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700695 pic->width, pic->height,
696 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800697 fprintf(stderr, "Output: "
698 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800699 stats->coded_size,
700 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
701 if (total > 0) {
702 int totals[4] = { 0, 0, 0, 0 };
703 fprintf(stderr, "block count: intra4: %d\n"
704 " intra16: %d (-> %.2f%%)\n",
705 num_i4, num_i16, 100.f * num_i16 / total);
706 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
707 num_skip, 100.f * num_skip / total);
708 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
709 " mode-partition: %6d (%.1f%%)\n",
710 stats->header_bytes[0],
711 100.f * stats->header_bytes[0] / stats->coded_size,
712 stats->header_bytes[1],
713 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700714 if (stats->alpha_data_size > 0) {
715 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
716 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700717 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700718 if (stats->layer_data_size) {
719 fprintf(stderr, " enhancement: %6d\n",
720 stats->layer_data_size);
721 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800722 fprintf(stderr, " Residuals bytes "
723 "|segment 1|segment 2|segment 3"
724 "|segment 4| total\n");
725 fprintf(stderr, " intra4-coeffs: ");
726 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
727 fprintf(stderr, " intra16-coeffs: ");
728 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
729 fprintf(stderr, " chroma coeffs: ");
730 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
731 fprintf(stderr, " macroblocks: ");
732 PrintPercents(stats->segment_size, total);
733 fprintf(stderr, " quantizer: ");
734 PrintValues(stats->segment_quant);
735 fprintf(stderr, " filter level: ");
736 PrintValues(stats->segment_level);
737 fprintf(stderr, "------------------+---------");
738 fprintf(stderr, "+---------+---------+---------+-----------------\n");
739 fprintf(stderr, " segments total: ");
740 PrintByteCount(totals, stats->coded_size, NULL);
741 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700742 if (stats->lossless_size > 0) {
743 PrintFullLosslessInfo(stats, "alpha");
744 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800745 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700746 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800747 const int mb_w = (pic->width + 15) / 16;
748 const int mb_h = (pic->height + 15) / 16;
749 const int type = pic->extra_info_type;
750 int x, y;
751 for (y = 0; y < mb_h; ++y) {
752 for (x = 0; x < mb_w; ++x) {
753 const int c = pic->extra_info[x + y * mb_w];
754 if (type == 1) { // intra4/intra16
755 printf("%c", "+."[c]);
756 } else if (type == 2) { // segments
757 printf("%c", ".-*X"[c]);
758 } else if (type == 3) { // quantizers
759 printf("%.2d ", c);
760 } else if (type == 6 || type == 7) {
761 printf("%3d ", c);
762 } else {
763 printf("0x%.2x ", c);
764 }
765 }
766 printf("\n");
767 }
768 }
769}
770
James Zernc7e86ab2011-08-25 14:22:32 -0700771//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800772
773static int MyWriter(const uint8_t* data, size_t data_size,
774 const WebPPicture* const pic) {
775 FILE* const out = (FILE*)pic->custom_ptr;
776 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
777}
778
779// Dumps a picture as a PGM file using the IMC4 layout.
780static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
781 int y;
782 const int uv_width = (picture->width + 1) / 2;
783 const int uv_height = (picture->height + 1) / 2;
784 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700785 const int alpha_height =
786 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700787 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800788 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800789 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800790 fprintf(f, "P5\n%d %d\n255\n", stride, height);
791 for (y = 0; y < picture->height; ++y) {
792 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
793 return 0;
794 if (picture->width & 1) fputc(0, f); // pad
795 }
796 for (y = 0; y < uv_height; ++y) {
797 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
798 return 0;
799 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
800 return 0;
801 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700802 for (y = 0; y < alpha_height; ++y) {
803 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
804 return 0;
805 if (picture->width & 1) fputc(0, f); // pad
806 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800807 fclose(f);
808 return 1;
809}
810
James Zernc7e86ab2011-08-25 14:22:32 -0700811//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800812
Pascal Massimino30971c92011-12-01 02:24:50 -0800813static int ProgressReport(int percent, const WebPPicture* const picture) {
814 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700815 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800816 fflush(stdout);
817 return 1; // all ok
818}
819
820//------------------------------------------------------------------------------
821
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700822static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 printf("Usage:\n\n");
824 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
825 printf("where quality is between 0 (poor) to 100 (very good).\n");
826 printf("Typical value is around 80.\n\n");
827 printf("Try -longhelp for an exhaustive list of advanced options.\n");
828}
829
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700830static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800831 printf("Usage:\n");
832 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
833 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700834 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700835#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800836 printf("Windows builds can take as input any of the files handled by WIC\n");
837#endif
838 printf("options:\n");
839 printf(" -h / -help ............ short help\n");
840 printf(" -H / -longhelp ........ long help\n");
841 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530842 printf(" -alpha_q <int> ......... Transparency-compression quality "
843 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800844 printf(" -preset <string> ....... Preset setting, one of:\n");
845 printf(" default, photo, picture,\n");
846 printf(" drawing, icon, text\n");
847 printf(" -preset must come first, as it overwrites other parameters.");
848 printf("\n");
849 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
850 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700851 printf(" -size <int> ............ Target size (in bytes)\n");
852 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800853 printf("\n");
854 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
855 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
856 printf(" -f <int> ............... filter strength (0=off..100)\n");
857 printf(" -sharpness <int> ....... "
858 "filter sharpness (0:most .. 7:least sharp)\n");
859 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700860 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
861 printf(" "
862 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800863 printf(" -pass <int> ............ analysis pass number (1..10)\n");
864 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700865 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
866#ifdef WEBP_EXPERIMENTAL_FEATURES
867 printf(" -444 / -422 / -gray ..... Change colorspace\n");
868#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800869 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800870 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700871 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
872 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800873 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530874 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800875 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
876 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000877 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530878 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530879 printf(" -lossless .............. Encode image losslessly.\n");
880 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700881 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700882
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800883 printf("\n");
884 printf(" -short ................. condense printed message\n");
885 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700886 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700887#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700888 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700889#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800890 printf(" -v ..................... verbose, e.g. print encoding/decoding "
891 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800892 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800893 printf("\n");
894 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800895 printf(" -af .................... auto-adjust filter strength.\n");
896 printf(" -pre <int> ............. pre-processing filter\n");
897 printf("\n");
898}
899
James Zernc7e86ab2011-08-25 14:22:32 -0700900//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700901// Error messages
902
903static const char* const kErrorMessages[] = {
904 "OK",
905 "OUT_OF_MEMORY: Out of memory allocating objects",
906 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
907 "NULL_PARAMETER: NULL parameter passed to function",
908 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700909 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
910 "allowed is 16383 pixels.",
911 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
912 "To reduce the size of this partition, try using less segments "
913 "with the -segments option, and eventually reduce the number of "
914 "header bits using -partition_limit. More details are available "
915 "in the manual (`man cwebp`)",
916 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800917 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800918 "FILE_TOO_BIG: File would be too big to fit in 4G",
919 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700920};
921
James Zernc7e86ab2011-08-25 14:22:32 -0700922//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800923
924int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700925 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800926 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
927 FILE *out = NULL;
928 int c;
929 int short_output = 0;
930 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530931 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800932 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700933 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800934 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800935 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700936 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800937 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800938 WebPConfig config;
939 WebPAuxStats stats;
James Zern63aba3a2012-12-03 18:20:00 -0800940 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800941 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700942
James Zern63aba3a2012-12-03 18:20:00 -0800943 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800944 if (!WebPPictureInit(&picture) ||
945 !WebPPictureInit(&original_picture) ||
946 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800947 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700948 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800949 }
950
951 if (argc == 1) {
952 HelpShort();
953 return 0;
954 }
955
956 for (c = 1; c < argc; ++c) {
957 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
958 HelpShort();
959 return 0;
960 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
961 HelpLong();
962 return 0;
963 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
964 out_file = argv[++c];
965 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
966 dump_file = argv[++c];
967 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800968 } else if (!strcmp(argv[c], "-print_psnr")) {
969 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700970 print_distortion = 0;
971 } else if (!strcmp(argv[c], "-print_ssim")) {
972 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800973 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700974 } else if (!strcmp(argv[c], "-print_lsim")) {
975 config.show_compressed = 1;
976 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800977 } else if (!strcmp(argv[c], "-short")) {
978 short_output++;
979 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700980 picture.width = strtol(argv[++c], NULL, 0);
981 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800982 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700983 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800984 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200985 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530986 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
987 config.alpha_quality = strtol(argv[++c], NULL, 0);
988 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
989 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000990 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
991 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530992 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800993 ++c;
994 if (!strcmp(argv[c], "none")) {
995 config.alpha_filtering = 0;
996 } else if (!strcmp(argv[c], "fast")) {
997 config.alpha_filtering = 1;
998 } else if (!strcmp(argv[c], "best")) {
999 config.alpha_filtering = 2;
1000 } else {
1001 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
1002 goto Error;
1003 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +05301004 } else if (!strcmp(argv[c], "-noalpha")) {
1005 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +00001006 } else if (!strcmp(argv[c], "-lossless")) {
1007 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +00001008 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +05301009 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
1010 ++c;
1011 if (!strcmp(argv[c], "photo")) {
1012 config.image_hint = WEBP_HINT_PHOTO;
1013 } else if (!strcmp(argv[c], "picture")) {
1014 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -07001015 } else if (!strcmp(argv[c], "graph")) {
1016 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +05301017 } else {
1018 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
1019 goto Error;
1020 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001021 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001022 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001023 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +02001024 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001025 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001026 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001027 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001028 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001029 } else if (!strcmp(argv[c], "-af")) {
1030 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -07001031 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001032 config.filter_type = 1;
1033 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001034 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001035 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001036 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001037 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001038 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001039 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001040 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -07001041 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
1042 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001043 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001044 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001045#ifdef WEBP_EXPERIMENTAL_FEATURES
1046 } else if (!strcmp(argv[c], "-444")) {
1047 picture.colorspace = WEBP_YUV444;
1048 } else if (!strcmp(argv[c], "-422")) {
1049 picture.colorspace = WEBP_YUV422;
1050 } else if (!strcmp(argv[c], "-gray")) {
1051 picture.colorspace = WEBP_YUV400;
1052#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001053 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
1054 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001055 crop_x = strtol(argv[++c], NULL, 0);
1056 crop_y = strtol(argv[++c], NULL, 0);
1057 crop_w = strtol(argv[++c], NULL, 0);
1058 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001059 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
1060 resize_w = strtol(argv[++c], NULL, 0);
1061 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -07001062#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -07001063 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +00001064 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -07001065#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -07001066 } else if (!strcmp(argv[c], "-version")) {
1067 const int version = WebPGetEncoderVersion();
1068 printf("%d.%d.%d\n",
1069 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1070 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -08001071 } else if (!strcmp(argv[c], "-progress")) {
1072 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001073 } else if (!strcmp(argv[c], "-quiet")) {
1074 quiet = 1;
1075 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
1076 WebPPreset preset;
1077 ++c;
1078 if (!strcmp(argv[c], "default")) {
1079 preset = WEBP_PRESET_DEFAULT;
1080 } else if (!strcmp(argv[c], "photo")) {
1081 preset = WEBP_PRESET_PHOTO;
1082 } else if (!strcmp(argv[c], "picture")) {
1083 preset = WEBP_PRESET_PICTURE;
1084 } else if (!strcmp(argv[c], "drawing")) {
1085 preset = WEBP_PRESET_DRAWING;
1086 } else if (!strcmp(argv[c], "icon")) {
1087 preset = WEBP_PRESET_ICON;
1088 } else if (!strcmp(argv[c], "text")) {
1089 preset = WEBP_PRESET_TEXT;
1090 } else {
1091 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
1092 goto Error;
1093 }
1094 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -07001095 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001096 goto Error;
1097 }
1098 } else if (!strcmp(argv[c], "-v")) {
1099 verbose = 1;
1100 } else if (argv[c][0] == '-') {
1101 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1102 HelpLong();
1103 return -1;
1104 } else {
1105 in_file = argv[c];
1106 }
1107 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001108 if (in_file == NULL) {
1109 fprintf(stderr, "No input file specified!\n");
1110 HelpShort();
1111 goto Error;
1112 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001113
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301114 // Check for unsupported command line options for lossless mode and log
1115 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001116 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301117 if (config.target_size > 0 || config.target_PSNR > 0) {
1118 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1119 " for lossless encoding. Ignoring such option(s)!\n");
1120 }
1121 if (config.partition_limit > 0) {
1122 fprintf(stderr, "Partition limit option is not required for lossless"
1123 " encoding. Ignoring this option!\n");
1124 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301125 }
1126
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001127 if (!WebPValidateConfig(&config)) {
1128 fprintf(stderr, "Error! Invalid configuration.\n");
1129 goto Error;
1130 }
1131
Pascal Massimino0744e842011-02-25 12:03:27 -08001132 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001133 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001134 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001135 }
James Zern63aba3a2012-12-03 18:20:00 -08001136 if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001137 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001138 goto Error;
1139 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001140 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1141
Pascal Massimino0744e842011-02-25 12:03:27 -08001142 if (verbose) {
1143 const double time = StopwatchReadAndReset(&stop_watch);
1144 fprintf(stderr, "Time to read input: %.3fs\n", time);
1145 }
1146
1147 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001148 if (out_file) {
1149 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001150 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001151 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1152 goto Error;
1153 } else {
1154 if (!short_output && !quiet) {
1155 fprintf(stderr, "Saving file '%s'\n", out_file);
1156 }
1157 }
1158 picture.writer = MyWriter;
1159 picture.custom_ptr = (void*)out;
1160 } else {
1161 out = NULL;
1162 if (!quiet && !short_output) {
1163 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1164 fprintf(stderr, "be performed, but its results discarded.\n\n");
1165 }
1166 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001167 if (!quiet) {
1168 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001169 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001170 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001171
Pascal Massimino0744e842011-02-25 12:03:27 -08001172 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001173 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001174 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001175 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001176 if (crop != 0) {
1177 // We use self-cropping using a view.
1178 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1179 fprintf(stderr, "Error! Cannot crop picture\n");
1180 goto Error;
1181 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001182 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001183 if ((resize_w | resize_h) > 0) {
1184 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1185 fprintf(stderr, "Error! Cannot resize picture\n");
1186 goto Error;
1187 }
1188 }
1189 if (picture.extra_info_type > 0) {
1190 AllocExtraInfo(&picture);
1191 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001192 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001193 WebPPictureCopy(&picture, &original_picture);
1194 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001195 if (!WebPEncode(&config, &picture)) {
1196 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001197 fprintf(stderr, "Error code: %d (%s)\n",
1198 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001199 goto Error;
1200 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001201 if (verbose) {
1202 const double time = StopwatchReadAndReset(&stop_watch);
1203 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1204 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001205
1206 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001207 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001208 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001209 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1210 } else if (!DumpPicture(&picture, dump_file)) {
1211 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1212 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001213 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001214
Pascal Massimino38369c02011-05-04 22:59:51 -07001215 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301216 if (config.lossless) {
1217 PrintExtraInfoLossless(&picture, short_output, in_file);
1218 } else {
1219 PrintExtraInfoLossy(&picture, short_output, in_file);
1220 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001221 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001222 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
1223 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -08001224 float values[5];
1225 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001226 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001227 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001228 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -08001229 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001230 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001231 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001232
1233 Error:
1234 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001235 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001236 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001237 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001238 if (out != NULL) {
1239 fclose(out);
1240 }
1241
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001242 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001243}
1244
James Zernc7e86ab2011-08-25 14:22:32 -07001245//------------------------------------------------------------------------------