blob: e75100cfa8c1cd2d279999175b5ab26e4eab96e7 [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
James Zern31f9dc62011-06-06 17:56:50 -070021#ifdef HAVE_WINCODEC_H
22#ifdef __MINGW32__
23#define INITGUID // Without this GUIDs are declared extern and fail to link
24#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080025#define CINTERFACE
26#define COBJMACROS
27#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
28 // code with COBJMACROS.
29#include <shlwapi.h>
30#include <windows.h>
31#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070032#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080033
34
35#include "webp/encode.h"
skal1bd287a2012-12-11 11:02:39 +010036
James Zern63aba3a2012-12-03 18:20:00 -080037#include "./metadata.h"
James Zern00b29e22012-05-15 13:48:11 -070038#include "./stopwatch.h"
skal1bd287a2012-12-11 11:02:39 +010039
40#include "./jpegdec.h"
41#include "./pngdec.h"
James Zern1c1c5642012-12-06 14:04:36 -080042#include "./tiffdec.h"
skal1bd287a2012-12-11 11:02:39 +010043
James Zernb4d0ef82011-07-15 14:53:03 -070044#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070045#if defined(__cplusplus) || defined(c_plusplus)
46extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070047#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070048
James Zern04e84cf2011-11-04 15:20:08 -070049extern void* VP8GetCPUInfo; // opaque forward declaration.
50
51#if defined(__cplusplus) || defined(c_plusplus)
52} // extern "C"
53#endif
54#endif // WEBP_DLL
55
James Zernc7e86ab2011-08-25 14:22:32 -070056//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080057
58static int verbose = 0;
59
60static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000061 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080062 const int uv_width = (pic->width + 1) / 2;
63 const int uv_height = (pic->height + 1) / 2;
64 int y;
65 int ok = 0;
66
Pascal Massiminodd108172012-07-18 21:58:53 +000067 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080068 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;
Pascal Massiminodd108172012-07-18 21:58:53 +000084 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080085
86 End:
87 return ok;
88}
89
James Zern31f9dc62011-06-06 17:56:50 -070090#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080091
James Zernf2b5d192012-10-08 18:33:18 -070092#define IFS(fn) \
93 do { \
94 if (SUCCEEDED(hr)) { \
95 hr = (fn); \
96 if (FAILED(hr)) fprintf(stderr, #fn " failed %08x\n", hr); \
97 } \
Pascal Massiminof61d14a2011-02-18 23:33:46 -080098 } while (0)
99
James Zern902d3e32012-05-08 17:49:39 -0700100// modified version of DEFINE_GUID from guiddef.h.
101#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
102 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
103
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800104#ifdef __cplusplus
105#define MAKE_REFGUID(x) (x)
106#else
107#define MAKE_REFGUID(x) &(x)
108#endif
109
James Zerneda8ee42012-10-19 18:34:06 -0700110typedef struct WICFormatImporter {
111 const GUID* pixel_format;
112 int bytes_per_pixel;
113 int (*import)(WebPPicture* const, const uint8_t* const, int);
114} WICFormatImporter;
115
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800116static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
117 HRESULT hr = S_OK;
118 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
119 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800120 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800121 return hr;
122}
123
124static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700125 WebPPicture* const pic, int keep_alpha) {
James Zerneda8ee42012-10-19 18:34:06 -0700126 // From Microsoft SDK 7.0a -- wincodec.h
127 // Create local copies for compatibility when building against earlier
128 // versions of the SDK.
129 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_,
130 0x6fddc324, 0x4e03, 0x4bfe,
131 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c);
132 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
133 0x6fddc324, 0x4e03, 0x4bfe,
134 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
135 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
136 0x6fddc324, 0x4e03, 0x4bfe,
137 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
138 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
139 0xf5c7ad2d, 0x6a8d, 0x43dd,
140 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
141 const WICFormatImporter alphaFormatImporters[] = {
142 { &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
143 { &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
144 { NULL, 0, NULL },
145 };
146 const WICFormatImporter nonAlphaFormatImporters[] = {
147 { &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
148 { &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
149 { NULL, 0, NULL },
150 };
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800151 HRESULT hr = S_OK;
152 IWICBitmapFrameDecode* pFrame = NULL;
153 IWICFormatConverter* pConverter = NULL;
154 IWICImagingFactory* pFactory = NULL;
155 IWICBitmapDecoder* pDecoder = NULL;
156 IStream* pStream = NULL;
157 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700158 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800159 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700160 WICPixelFormatGUID srcPixelFormat = { 0 };
James Zerneda8ee42012-10-19 18:34:06 -0700161 const WICFormatImporter* importer = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700162 GUID srcContainerFormat = { 0 };
163 const GUID* alphaContainers[] = {
164 &GUID_ContainerFormatBmp,
165 &GUID_ContainerFormatPng,
166 &GUID_ContainerFormatTiff
167 };
168 int has_alpha = 0;
169 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800170
171 IFS(CoInitialize(NULL));
172 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
173 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
174 (LPVOID*)&pFactory));
175 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800176 fprintf(stderr,
177 "Couldn't access Windows Imaging Component (are you running "
178 "Windows XP SP3 or newer?). Most formats not available. "
179 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800180 }
181 // Prepare for image decoding.
182 IFS(OpenInputStream(filename, &pStream));
183 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
184 WICDecodeMetadataCacheOnDemand, &pDecoder));
185 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
186 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800187 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800188 hr = E_FAIL;
189 }
190 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700191 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
192 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
193
James Zernecd66f72012-10-08 18:15:30 -0700194 if (keep_alpha) {
195 for (i = 0;
196 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
197 ++i) {
198 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
199 MAKE_REFGUID(*alphaContainers[i]))) {
200 has_alpha =
201 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
202 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
203 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
204 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
205 break;
206 }
James Zern3cfe0882011-06-21 18:29:30 -0700207 }
208 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800209
210 // Prepare for pixel format conversion (if necessary).
211 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
James Zerneda8ee42012-10-19 18:34:06 -0700212
213 for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters;
214 hr == S_OK && importer->import != NULL; ++importer) {
215 BOOL canConvert;
216 const HRESULT cchr = IWICFormatConverter_CanConvert(
217 pConverter,
218 MAKE_REFGUID(srcPixelFormat),
219 MAKE_REFGUID(*importer->pixel_format),
220 &canConvert);
221 if (SUCCEEDED(cchr) && canConvert) break;
222 }
223 if (importer->import == NULL) hr = E_FAIL;
224
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800225 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zerneda8ee42012-10-19 18:34:06 -0700226 importer->pixel_format,
James Zern3cfe0882011-06-21 18:29:30 -0700227 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800228 NULL, 0.0, WICBitmapPaletteTypeCustom));
229
230 // Decode.
231 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zerneda8ee42012-10-19 18:34:06 -0700232 stride = importer->bytes_per_pixel * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800233 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700234 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800235 if (rgb == NULL)
236 hr = E_OUTOFMEMORY;
237 }
James Zern3cfe0882011-06-21 18:29:30 -0700238 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
239 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800240
241 // WebP conversion.
242 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700243 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800244 pic->width = width;
245 pic->height = height;
James Zerneda8ee42012-10-19 18:34:06 -0700246 ok = importer->import(pic, rgb, stride);
James Zern3cfe0882011-06-21 18:29:30 -0700247 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800248 hr = E_FAIL;
249 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000250 if (SUCCEEDED(hr)) {
251 if (has_alpha && keep_alpha == 2) {
252 WebPCleanupTransparentArea(pic);
253 }
254 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800255
256 // Cleanup.
257 if (pConverter != NULL) IUnknown_Release(pConverter);
258 if (pFrame != NULL) IUnknown_Release(pFrame);
259 if (pDecoder != NULL) IUnknown_Release(pDecoder);
260 if (pFactory != NULL) IUnknown_Release(pFactory);
261 if (pStream != NULL) IUnknown_Release(pStream);
262 free(rgb);
263 return hr;
264}
265
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700266static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800267 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800268 int ok;
James Zern63aba3a2012-12-03 18:20:00 -0800269 (void)metadata; // TODO(jzern): add metadata extraction using WIC
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800270 if (pic->width != 0 && pic->height != 0) {
271 // If image size is specified, infer it as YUV format.
272 FILE* in_file = fopen(filename, "rb");
273 if (in_file == NULL) {
274 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
275 return 0;
276 }
277 ok = ReadYUV(in_file, pic);
278 fclose(in_file);
279 } else {
280 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700281 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800282 }
283 if (!ok) {
284 fprintf(stderr, "Error! Could not process file %s\n", filename);
285 }
286 return ok;
287}
288
James Zern31f9dc62011-06-06 17:56:50 -0700289#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800290
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800291typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700292 PNG_ = 0,
293 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700294 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800295 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800296} InputFileFormat;
297
298static InputFileFormat GetImageType(FILE* in_file) {
299 InputFileFormat format = UNSUPPORTED;
300 unsigned int magic;
301 unsigned char buf[4];
302
303 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
304 (fseek(in_file, 0, SEEK_SET) != 0)) {
305 return format;
306 }
307
308 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
309 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700310 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800311 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700312 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700313 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
314 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800315 }
316 return format;
317}
318
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700319static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800320 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800321 int ok = 0;
322 FILE* in_file = fopen(filename, "rb");
James Zern63aba3a2012-12-03 18:20:00 -0800323 (void)metadata; // TODO(jzern): add metadata extraction to the formats below.
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800324 if (in_file == NULL) {
325 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
326 return ok;
327 }
328
329 if (pic->width == 0 || pic->height == 0) {
330 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
331 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700332 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800333 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700334 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800335 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700336 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800337 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800338 }
339 } else {
340 // If image size is specified, infer it as YUV format.
341 ok = ReadYUV(in_file, pic);
342 }
343 if (!ok) {
344 fprintf(stderr, "Error! Could not process file %s\n", filename);
345 }
346
347 fclose(in_file);
348 return ok;
349}
350
James Zern31f9dc62011-06-06 17:56:50 -0700351#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800352
353static void AllocExtraInfo(WebPPicture* const pic) {
354 const int mb_w = (pic->width + 15) / 16;
355 const int mb_h = (pic->height + 15) / 16;
356 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
357}
358
359static void PrintByteCount(const int bytes[4], int total_size,
360 int* const totals) {
361 int s;
362 int total = 0;
363 for (s = 0; s < 4; ++s) {
364 fprintf(stderr, "| %7d ", bytes[s]);
365 total += bytes[s];
366 if (totals) totals[s] += bytes[s];
367 }
James Zern04e84cf2011-11-04 15:20:08 -0700368 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800369}
370
371static void PrintPercents(const int counts[4], int total) {
372 int s;
373 for (s = 0; s < 4; ++s) {
374 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
375 }
James Zern04e84cf2011-11-04 15:20:08 -0700376 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800377}
378
379static void PrintValues(const int values[4]) {
380 int s;
381 for (s = 0; s < 4; ++s) {
382 fprintf(stderr, "| %7d ", values[s]);
383 }
James Zern04e84cf2011-11-04 15:20:08 -0700384 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800385}
386
Pascal Massimino7d853d72012-07-24 16:15:36 -0700387static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
388 const char* const description) {
389 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
390 description, stats->lossless_size);
391 if (stats->lossless_features) {
392 fprintf(stderr, " * Lossless features used:");
393 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
394 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
395 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
396 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
397 fprintf(stderr, "\n");
398 }
399 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
400 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
401 if (stats->palette_size > 0) {
402 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
403 }
404}
405
Vikas Arorac4ccab62012-05-09 11:27:46 +0530406static void PrintExtraInfoLossless(const WebPPicture* const pic,
407 int short_output,
408 const char* const file_name) {
409 const WebPAuxStats* const stats = pic->stats;
410 if (short_output) {
411 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
412 } else {
413 fprintf(stderr, "File: %s\n", file_name);
414 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
415 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700416 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530417 }
418}
419
420static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
421 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800422 const WebPAuxStats* const stats = pic->stats;
423 if (short_output) {
424 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700425 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800426 const int num_i4 = stats->block_count[0];
427 const int num_i16 = stats->block_count[1];
428 const int num_skip = stats->block_count[2];
429 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800430 fprintf(stderr, "File: %s\n", file_name);
431 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700432 pic->width, pic->height,
433 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800434 fprintf(stderr, "Output: "
435 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800436 stats->coded_size,
437 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
438 if (total > 0) {
439 int totals[4] = { 0, 0, 0, 0 };
440 fprintf(stderr, "block count: intra4: %d\n"
441 " intra16: %d (-> %.2f%%)\n",
442 num_i4, num_i16, 100.f * num_i16 / total);
443 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
444 num_skip, 100.f * num_skip / total);
445 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
446 " mode-partition: %6d (%.1f%%)\n",
447 stats->header_bytes[0],
448 100.f * stats->header_bytes[0] / stats->coded_size,
449 stats->header_bytes[1],
450 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700451 if (stats->alpha_data_size > 0) {
452 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
453 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700454 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700455 if (stats->layer_data_size) {
456 fprintf(stderr, " enhancement: %6d\n",
457 stats->layer_data_size);
458 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800459 fprintf(stderr, " Residuals bytes "
460 "|segment 1|segment 2|segment 3"
461 "|segment 4| total\n");
462 fprintf(stderr, " intra4-coeffs: ");
463 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
464 fprintf(stderr, " intra16-coeffs: ");
465 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
466 fprintf(stderr, " chroma coeffs: ");
467 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
468 fprintf(stderr, " macroblocks: ");
469 PrintPercents(stats->segment_size, total);
470 fprintf(stderr, " quantizer: ");
471 PrintValues(stats->segment_quant);
472 fprintf(stderr, " filter level: ");
473 PrintValues(stats->segment_level);
474 fprintf(stderr, "------------------+---------");
475 fprintf(stderr, "+---------+---------+---------+-----------------\n");
476 fprintf(stderr, " segments total: ");
477 PrintByteCount(totals, stats->coded_size, NULL);
478 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700479 if (stats->lossless_size > 0) {
480 PrintFullLosslessInfo(stats, "alpha");
481 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800482 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700483 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800484 const int mb_w = (pic->width + 15) / 16;
485 const int mb_h = (pic->height + 15) / 16;
486 const int type = pic->extra_info_type;
487 int x, y;
488 for (y = 0; y < mb_h; ++y) {
489 for (x = 0; x < mb_w; ++x) {
490 const int c = pic->extra_info[x + y * mb_w];
491 if (type == 1) { // intra4/intra16
492 printf("%c", "+."[c]);
493 } else if (type == 2) { // segments
494 printf("%c", ".-*X"[c]);
495 } else if (type == 3) { // quantizers
496 printf("%.2d ", c);
497 } else if (type == 6 || type == 7) {
498 printf("%3d ", c);
499 } else {
500 printf("0x%.2x ", c);
501 }
502 }
503 printf("\n");
504 }
505 }
506}
507
James Zernc7e86ab2011-08-25 14:22:32 -0700508//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800509
510static int MyWriter(const uint8_t* data, size_t data_size,
511 const WebPPicture* const pic) {
512 FILE* const out = (FILE*)pic->custom_ptr;
513 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
514}
515
516// Dumps a picture as a PGM file using the IMC4 layout.
517static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
518 int y;
519 const int uv_width = (picture->width + 1) / 2;
520 const int uv_height = (picture->height + 1) / 2;
521 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700522 const int alpha_height =
523 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700524 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800525 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800526 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800527 fprintf(f, "P5\n%d %d\n255\n", stride, height);
528 for (y = 0; y < picture->height; ++y) {
529 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
530 return 0;
531 if (picture->width & 1) fputc(0, f); // pad
532 }
533 for (y = 0; y < uv_height; ++y) {
534 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
535 return 0;
536 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
537 return 0;
538 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700539 for (y = 0; y < alpha_height; ++y) {
540 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
541 return 0;
542 if (picture->width & 1) fputc(0, f); // pad
543 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800544 fclose(f);
545 return 1;
546}
547
James Zernc7e86ab2011-08-25 14:22:32 -0700548//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800549
Pascal Massimino30971c92011-12-01 02:24:50 -0800550static int ProgressReport(int percent, const WebPPicture* const picture) {
551 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700552 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800553 fflush(stdout);
554 return 1; // all ok
555}
556
557//------------------------------------------------------------------------------
558
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700559static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800560 printf("Usage:\n\n");
561 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
562 printf("where quality is between 0 (poor) to 100 (very good).\n");
563 printf("Typical value is around 80.\n\n");
564 printf("Try -longhelp for an exhaustive list of advanced options.\n");
565}
566
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700567static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800568 printf("Usage:\n");
569 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
570 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700571 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700572#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800573 printf("Windows builds can take as input any of the files handled by WIC\n");
574#endif
575 printf("options:\n");
576 printf(" -h / -help ............ short help\n");
577 printf(" -H / -longhelp ........ long help\n");
578 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530579 printf(" -alpha_q <int> ......... Transparency-compression quality "
580 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800581 printf(" -preset <string> ....... Preset setting, one of:\n");
582 printf(" default, photo, picture,\n");
583 printf(" drawing, icon, text\n");
584 printf(" -preset must come first, as it overwrites other parameters.");
585 printf("\n");
586 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
587 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700588 printf(" -size <int> ............ Target size (in bytes)\n");
589 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800590 printf("\n");
591 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
592 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
593 printf(" -f <int> ............... filter strength (0=off..100)\n");
594 printf(" -sharpness <int> ....... "
595 "filter sharpness (0:most .. 7:least sharp)\n");
596 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700597 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
598 printf(" "
599 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800600 printf(" -pass <int> ............ analysis pass number (1..10)\n");
601 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700602 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
603#ifdef WEBP_EXPERIMENTAL_FEATURES
604 printf(" -444 / -422 / -gray ..... Change colorspace\n");
605#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800606 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800607 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700608 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
609 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800610 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530611 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800612 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
613 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000614 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530615 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530616 printf(" -lossless .............. Encode image losslessly.\n");
617 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700618 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700619
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800620 printf("\n");
621 printf(" -short ................. condense printed message\n");
622 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700623 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700624#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700625 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700626#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800627 printf(" -v ..................... verbose, e.g. print encoding/decoding "
628 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800629 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800630 printf("\n");
631 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800632 printf(" -af .................... auto-adjust filter strength.\n");
633 printf(" -pre <int> ............. pre-processing filter\n");
634 printf("\n");
635}
636
James Zernc7e86ab2011-08-25 14:22:32 -0700637//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700638// Error messages
639
640static const char* const kErrorMessages[] = {
641 "OK",
642 "OUT_OF_MEMORY: Out of memory allocating objects",
643 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
644 "NULL_PARAMETER: NULL parameter passed to function",
645 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700646 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
647 "allowed is 16383 pixels.",
648 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
649 "To reduce the size of this partition, try using less segments "
650 "with the -segments option, and eventually reduce the number of "
651 "header bits using -partition_limit. More details are available "
652 "in the manual (`man cwebp`)",
653 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800654 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800655 "FILE_TOO_BIG: File would be too big to fit in 4G",
656 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700657};
658
James Zernc7e86ab2011-08-25 14:22:32 -0700659//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800660
661int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700662 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800663 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
664 FILE *out = NULL;
665 int c;
666 int short_output = 0;
667 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530668 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700670 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800671 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800672 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700673 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800674 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800675 WebPConfig config;
676 WebPAuxStats stats;
James Zern63aba3a2012-12-03 18:20:00 -0800677 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800678 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700679
James Zern63aba3a2012-12-03 18:20:00 -0800680 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800681 if (!WebPPictureInit(&picture) ||
682 !WebPPictureInit(&original_picture) ||
683 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800684 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700685 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800686 }
687
688 if (argc == 1) {
689 HelpShort();
690 return 0;
691 }
692
693 for (c = 1; c < argc; ++c) {
694 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
695 HelpShort();
696 return 0;
697 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
698 HelpLong();
699 return 0;
700 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
701 out_file = argv[++c];
702 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
703 dump_file = argv[++c];
704 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800705 } else if (!strcmp(argv[c], "-print_psnr")) {
706 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700707 print_distortion = 0;
708 } else if (!strcmp(argv[c], "-print_ssim")) {
709 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800710 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700711 } else if (!strcmp(argv[c], "-print_lsim")) {
712 config.show_compressed = 1;
713 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800714 } else if (!strcmp(argv[c], "-short")) {
715 short_output++;
716 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700717 picture.width = strtol(argv[++c], NULL, 0);
718 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800719 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700720 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800721 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200722 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530723 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
724 config.alpha_quality = strtol(argv[++c], NULL, 0);
725 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
726 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000727 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
728 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530729 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800730 ++c;
731 if (!strcmp(argv[c], "none")) {
732 config.alpha_filtering = 0;
733 } else if (!strcmp(argv[c], "fast")) {
734 config.alpha_filtering = 1;
735 } else if (!strcmp(argv[c], "best")) {
736 config.alpha_filtering = 2;
737 } else {
738 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
739 goto Error;
740 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530741 } else if (!strcmp(argv[c], "-noalpha")) {
742 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000743 } else if (!strcmp(argv[c], "-lossless")) {
744 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000745 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530746 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
747 ++c;
748 if (!strcmp(argv[c], "photo")) {
749 config.image_hint = WEBP_HINT_PHOTO;
750 } else if (!strcmp(argv[c], "picture")) {
751 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700752 } else if (!strcmp(argv[c], "graph")) {
753 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530754 } else {
755 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
756 goto Error;
757 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800758 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700759 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800760 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200761 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800762 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700763 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700765 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800766 } else if (!strcmp(argv[c], "-af")) {
767 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700768 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800769 config.filter_type = 1;
770 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700771 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800772 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700773 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800774 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700775 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800776 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700777 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700778 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
779 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800780 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700781 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700782#ifdef WEBP_EXPERIMENTAL_FEATURES
783 } else if (!strcmp(argv[c], "-444")) {
784 picture.colorspace = WEBP_YUV444;
785 } else if (!strcmp(argv[c], "-422")) {
786 picture.colorspace = WEBP_YUV422;
787 } else if (!strcmp(argv[c], "-gray")) {
788 picture.colorspace = WEBP_YUV400;
789#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800790 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
791 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700792 crop_x = strtol(argv[++c], NULL, 0);
793 crop_y = strtol(argv[++c], NULL, 0);
794 crop_w = strtol(argv[++c], NULL, 0);
795 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700796 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
797 resize_w = strtol(argv[++c], NULL, 0);
798 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700799#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700800 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000801 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700802#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700803 } else if (!strcmp(argv[c], "-version")) {
804 const int version = WebPGetEncoderVersion();
805 printf("%d.%d.%d\n",
806 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
807 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800808 } else if (!strcmp(argv[c], "-progress")) {
809 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800810 } else if (!strcmp(argv[c], "-quiet")) {
811 quiet = 1;
812 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
813 WebPPreset preset;
814 ++c;
815 if (!strcmp(argv[c], "default")) {
816 preset = WEBP_PRESET_DEFAULT;
817 } else if (!strcmp(argv[c], "photo")) {
818 preset = WEBP_PRESET_PHOTO;
819 } else if (!strcmp(argv[c], "picture")) {
820 preset = WEBP_PRESET_PICTURE;
821 } else if (!strcmp(argv[c], "drawing")) {
822 preset = WEBP_PRESET_DRAWING;
823 } else if (!strcmp(argv[c], "icon")) {
824 preset = WEBP_PRESET_ICON;
825 } else if (!strcmp(argv[c], "text")) {
826 preset = WEBP_PRESET_TEXT;
827 } else {
828 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
829 goto Error;
830 }
831 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700832 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800833 goto Error;
834 }
835 } else if (!strcmp(argv[c], "-v")) {
836 verbose = 1;
837 } else if (argv[c][0] == '-') {
838 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
839 HelpLong();
840 return -1;
841 } else {
842 in_file = argv[c];
843 }
844 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700845 if (in_file == NULL) {
846 fprintf(stderr, "No input file specified!\n");
847 HelpShort();
848 goto Error;
849 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800850
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530851 // Check for unsupported command line options for lossless mode and log
852 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000853 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530854 if (config.target_size > 0 || config.target_PSNR > 0) {
855 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
856 " for lossless encoding. Ignoring such option(s)!\n");
857 }
858 if (config.partition_limit > 0) {
859 fprintf(stderr, "Partition limit option is not required for lossless"
860 " encoding. Ignoring this option!\n");
861 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530862 }
863
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800864 if (!WebPValidateConfig(&config)) {
865 fprintf(stderr, "Error! Invalid configuration.\n");
866 goto Error;
867 }
868
Pascal Massimino0744e842011-02-25 12:03:27 -0800869 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700870 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800871 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700872 }
James Zern63aba3a2012-12-03 18:20:00 -0800873 if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800874 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700875 goto Error;
876 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800877 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
878
Pascal Massimino0744e842011-02-25 12:03:27 -0800879 if (verbose) {
880 const double time = StopwatchReadAndReset(&stop_watch);
881 fprintf(stderr, "Time to read input: %.3fs\n", time);
882 }
883
884 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885 if (out_file) {
886 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800887 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800888 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
889 goto Error;
890 } else {
891 if (!short_output && !quiet) {
892 fprintf(stderr, "Saving file '%s'\n", out_file);
893 }
894 }
895 picture.writer = MyWriter;
896 picture.custom_ptr = (void*)out;
897 } else {
898 out = NULL;
899 if (!quiet && !short_output) {
900 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
901 fprintf(stderr, "be performed, but its results discarded.\n\n");
902 }
903 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700904 if (!quiet) {
905 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700906 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700907 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800908
Pascal Massimino0744e842011-02-25 12:03:27 -0800909 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700910 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800911 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700912 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -0700913 if (crop != 0) {
914 // We use self-cropping using a view.
915 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
916 fprintf(stderr, "Error! Cannot crop picture\n");
917 goto Error;
918 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700919 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700920 if ((resize_w | resize_h) > 0) {
921 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
922 fprintf(stderr, "Error! Cannot resize picture\n");
923 goto Error;
924 }
925 }
926 if (picture.extra_info_type > 0) {
927 AllocExtraInfo(&picture);
928 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700929 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -0800930 WebPPictureCopy(&picture, &original_picture);
931 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700932 if (!WebPEncode(&config, &picture)) {
933 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700934 fprintf(stderr, "Error code: %d (%s)\n",
935 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700936 goto Error;
937 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800938 if (verbose) {
939 const double time = StopwatchReadAndReset(&stop_watch);
940 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
941 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800942
943 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700944 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +0000945 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -0700946 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
947 } else if (!DumpPicture(&picture, dump_file)) {
948 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
949 }
Pascal Massimino38369c02011-05-04 22:59:51 -0700950 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800951
Pascal Massimino38369c02011-05-04 22:59:51 -0700952 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +0530953 if (config.lossless) {
954 PrintExtraInfoLossless(&picture, short_output, in_file);
955 } else {
956 PrintExtraInfoLossy(&picture, short_output, in_file);
957 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800958 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700959 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
960 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -0800961 float values[5];
962 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700963 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800964 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700965 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -0800966 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -0700967 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700968 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800969
970 Error:
971 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -0800972 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800973 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800974 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800975 if (out != NULL) {
976 fclose(out);
977 }
978
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700979 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800980}
981
James Zernc7e86ab2011-08-25 14:22:32 -0700982//------------------------------------------------------------------------------