blob: 47a9c3b5c4aa74e520abcdae028aa76d891ac262 [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"
James Zern6a871d62012-12-06 13:48:21 -080036#include "./jpegdec.h"
James Zern63aba3a2012-12-03 18:20:00 -080037#include "./metadata.h"
James Zern2ee228f2012-12-06 13:06:37 -080038#include "./pngdec.h"
James Zern00b29e22012-05-15 13:48:11 -070039#include "./stopwatch.h"
James Zern1c1c5642012-12-06 14:04:36 -080040#include "./tiffdec.h"
James Zernb4d0ef82011-07-15 14:53:03 -070041#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070042#if defined(__cplusplus) || defined(c_plusplus)
43extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070044#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070045
James Zern04e84cf2011-11-04 15:20:08 -070046extern void* VP8GetCPUInfo; // opaque forward declaration.
47
48#if defined(__cplusplus) || defined(c_plusplus)
49} // extern "C"
50#endif
51#endif // WEBP_DLL
52
James Zernc7e86ab2011-08-25 14:22:32 -070053//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080054
55static int verbose = 0;
56
57static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000058 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080059 const int uv_width = (pic->width + 1) / 2;
60 const int uv_height = (pic->height + 1) / 2;
61 int y;
62 int ok = 0;
63
Pascal Massiminodd108172012-07-18 21:58:53 +000064 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080065 if (!WebPPictureAlloc(pic)) return ok;
66
67 for (y = 0; y < pic->height; ++y) {
68 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
69 goto End;
70 }
71 }
72 for (y = 0; y < uv_height; ++y) {
73 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
74 goto End;
75 }
76 for (y = 0; y < uv_height; ++y) {
77 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
78 goto End;
79 }
80 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000081 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080082
83 End:
84 return ok;
85}
86
James Zern31f9dc62011-06-06 17:56:50 -070087#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080088
James Zernf2b5d192012-10-08 18:33:18 -070089#define IFS(fn) \
90 do { \
91 if (SUCCEEDED(hr)) { \
92 hr = (fn); \
93 if (FAILED(hr)) fprintf(stderr, #fn " failed %08x\n", hr); \
94 } \
Pascal Massiminof61d14a2011-02-18 23:33:46 -080095 } while (0)
96
James Zern902d3e32012-05-08 17:49:39 -070097// modified version of DEFINE_GUID from guiddef.h.
98#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
99 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
100
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800101#ifdef __cplusplus
102#define MAKE_REFGUID(x) (x)
103#else
104#define MAKE_REFGUID(x) &(x)
105#endif
106
James Zerneda8ee42012-10-19 18:34:06 -0700107typedef struct WICFormatImporter {
108 const GUID* pixel_format;
109 int bytes_per_pixel;
110 int (*import)(WebPPicture* const, const uint8_t* const, int);
111} WICFormatImporter;
112
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800113static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
114 HRESULT hr = S_OK;
115 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
116 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800117 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800118 return hr;
119}
120
121static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700122 WebPPicture* const pic, int keep_alpha) {
James Zerneda8ee42012-10-19 18:34:06 -0700123 // From Microsoft SDK 7.0a -- wincodec.h
124 // Create local copies for compatibility when building against earlier
125 // versions of the SDK.
126 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_,
127 0x6fddc324, 0x4e03, 0x4bfe,
128 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c);
129 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
130 0x6fddc324, 0x4e03, 0x4bfe,
131 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
132 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
133 0x6fddc324, 0x4e03, 0x4bfe,
134 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
135 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
136 0xf5c7ad2d, 0x6a8d, 0x43dd,
137 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
138 const WICFormatImporter alphaFormatImporters[] = {
139 { &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
140 { &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
141 { NULL, 0, NULL },
142 };
143 const WICFormatImporter nonAlphaFormatImporters[] = {
144 { &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
145 { &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
146 { NULL, 0, NULL },
147 };
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800148 HRESULT hr = S_OK;
149 IWICBitmapFrameDecode* pFrame = NULL;
150 IWICFormatConverter* pConverter = NULL;
151 IWICImagingFactory* pFactory = NULL;
152 IWICBitmapDecoder* pDecoder = NULL;
153 IStream* pStream = NULL;
154 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700155 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800156 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700157 WICPixelFormatGUID srcPixelFormat = { 0 };
James Zerneda8ee42012-10-19 18:34:06 -0700158 const WICFormatImporter* importer = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700159 GUID srcContainerFormat = { 0 };
160 const GUID* alphaContainers[] = {
161 &GUID_ContainerFormatBmp,
162 &GUID_ContainerFormatPng,
163 &GUID_ContainerFormatTiff
164 };
165 int has_alpha = 0;
166 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800167
168 IFS(CoInitialize(NULL));
169 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
170 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
171 (LPVOID*)&pFactory));
172 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800173 fprintf(stderr,
174 "Couldn't access Windows Imaging Component (are you running "
175 "Windows XP SP3 or newer?). Most formats not available. "
176 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800177 }
178 // Prepare for image decoding.
179 IFS(OpenInputStream(filename, &pStream));
180 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
181 WICDecodeMetadataCacheOnDemand, &pDecoder));
182 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
183 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800184 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800185 hr = E_FAIL;
186 }
187 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700188 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
189 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
190
James Zernecd66f72012-10-08 18:15:30 -0700191 if (keep_alpha) {
192 for (i = 0;
193 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
194 ++i) {
195 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
196 MAKE_REFGUID(*alphaContainers[i]))) {
197 has_alpha =
198 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
199 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
200 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
201 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
202 break;
203 }
James Zern3cfe0882011-06-21 18:29:30 -0700204 }
205 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800206
207 // Prepare for pixel format conversion (if necessary).
208 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
James Zerneda8ee42012-10-19 18:34:06 -0700209
210 for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters;
211 hr == S_OK && importer->import != NULL; ++importer) {
212 BOOL canConvert;
213 const HRESULT cchr = IWICFormatConverter_CanConvert(
214 pConverter,
215 MAKE_REFGUID(srcPixelFormat),
216 MAKE_REFGUID(*importer->pixel_format),
217 &canConvert);
218 if (SUCCEEDED(cchr) && canConvert) break;
219 }
220 if (importer->import == NULL) hr = E_FAIL;
221
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800222 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zerneda8ee42012-10-19 18:34:06 -0700223 importer->pixel_format,
James Zern3cfe0882011-06-21 18:29:30 -0700224 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800225 NULL, 0.0, WICBitmapPaletteTypeCustom));
226
227 // Decode.
228 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zerneda8ee42012-10-19 18:34:06 -0700229 stride = importer->bytes_per_pixel * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800230 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700231 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800232 if (rgb == NULL)
233 hr = E_OUTOFMEMORY;
234 }
James Zern3cfe0882011-06-21 18:29:30 -0700235 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
236 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800237
238 // WebP conversion.
239 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700240 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800241 pic->width = width;
242 pic->height = height;
James Zerneda8ee42012-10-19 18:34:06 -0700243 ok = importer->import(pic, rgb, stride);
James Zern3cfe0882011-06-21 18:29:30 -0700244 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800245 hr = E_FAIL;
246 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000247 if (SUCCEEDED(hr)) {
248 if (has_alpha && keep_alpha == 2) {
249 WebPCleanupTransparentArea(pic);
250 }
251 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800252
253 // Cleanup.
254 if (pConverter != NULL) IUnknown_Release(pConverter);
255 if (pFrame != NULL) IUnknown_Release(pFrame);
256 if (pDecoder != NULL) IUnknown_Release(pDecoder);
257 if (pFactory != NULL) IUnknown_Release(pFactory);
258 if (pStream != NULL) IUnknown_Release(pStream);
259 free(rgb);
260 return hr;
261}
262
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700263static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800264 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800265 int ok;
James Zern63aba3a2012-12-03 18:20:00 -0800266 (void)metadata; // TODO(jzern): add metadata extraction using WIC
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800267 if (pic->width != 0 && pic->height != 0) {
268 // If image size is specified, infer it as YUV format.
269 FILE* in_file = fopen(filename, "rb");
270 if (in_file == NULL) {
271 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
272 return 0;
273 }
274 ok = ReadYUV(in_file, pic);
275 fclose(in_file);
276 } else {
277 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700278 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800279 }
280 if (!ok) {
281 fprintf(stderr, "Error! Could not process file %s\n", filename);
282 }
283 return ok;
284}
285
James Zern31f9dc62011-06-06 17:56:50 -0700286#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800287
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800288typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700289 PNG_ = 0,
290 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700291 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800292 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800293} InputFileFormat;
294
295static InputFileFormat GetImageType(FILE* in_file) {
296 InputFileFormat format = UNSUPPORTED;
297 unsigned int magic;
298 unsigned char buf[4];
299
300 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
301 (fseek(in_file, 0, SEEK_SET) != 0)) {
302 return format;
303 }
304
305 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
306 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700307 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800308 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700309 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700310 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
311 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800312 }
313 return format;
314}
315
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700316static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800317 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800318 int ok = 0;
319 FILE* in_file = fopen(filename, "rb");
James Zern63aba3a2012-12-03 18:20:00 -0800320 (void)metadata; // TODO(jzern): add metadata extraction to the formats below.
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800321 if (in_file == NULL) {
322 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
323 return ok;
324 }
325
326 if (pic->width == 0 || pic->height == 0) {
327 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
328 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700329 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700330 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700331 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800332 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700333 } else if (format == TIFF_) {
334 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800335 }
336 } else {
337 // If image size is specified, infer it as YUV format.
338 ok = ReadYUV(in_file, pic);
339 }
340 if (!ok) {
341 fprintf(stderr, "Error! Could not process file %s\n", filename);
342 }
343
344 fclose(in_file);
345 return ok;
346}
347
James Zern31f9dc62011-06-06 17:56:50 -0700348#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800349
350static void AllocExtraInfo(WebPPicture* const pic) {
351 const int mb_w = (pic->width + 15) / 16;
352 const int mb_h = (pic->height + 15) / 16;
353 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
354}
355
356static void PrintByteCount(const int bytes[4], int total_size,
357 int* const totals) {
358 int s;
359 int total = 0;
360 for (s = 0; s < 4; ++s) {
361 fprintf(stderr, "| %7d ", bytes[s]);
362 total += bytes[s];
363 if (totals) totals[s] += bytes[s];
364 }
James Zern04e84cf2011-11-04 15:20:08 -0700365 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800366}
367
368static void PrintPercents(const int counts[4], int total) {
369 int s;
370 for (s = 0; s < 4; ++s) {
371 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
372 }
James Zern04e84cf2011-11-04 15:20:08 -0700373 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800374}
375
376static void PrintValues(const int values[4]) {
377 int s;
378 for (s = 0; s < 4; ++s) {
379 fprintf(stderr, "| %7d ", values[s]);
380 }
James Zern04e84cf2011-11-04 15:20:08 -0700381 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800382}
383
Pascal Massimino7d853d72012-07-24 16:15:36 -0700384static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
385 const char* const description) {
386 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
387 description, stats->lossless_size);
388 if (stats->lossless_features) {
389 fprintf(stderr, " * Lossless features used:");
390 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
391 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
392 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
393 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
394 fprintf(stderr, "\n");
395 }
396 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
397 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
398 if (stats->palette_size > 0) {
399 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
400 }
401}
402
Vikas Arorac4ccab62012-05-09 11:27:46 +0530403static void PrintExtraInfoLossless(const WebPPicture* const pic,
404 int short_output,
405 const char* const file_name) {
406 const WebPAuxStats* const stats = pic->stats;
407 if (short_output) {
408 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
409 } else {
410 fprintf(stderr, "File: %s\n", file_name);
411 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
412 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700413 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530414 }
415}
416
417static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
418 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800419 const WebPAuxStats* const stats = pic->stats;
420 if (short_output) {
421 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700422 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800423 const int num_i4 = stats->block_count[0];
424 const int num_i16 = stats->block_count[1];
425 const int num_skip = stats->block_count[2];
426 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800427 fprintf(stderr, "File: %s\n", file_name);
428 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700429 pic->width, pic->height,
430 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800431 fprintf(stderr, "Output: "
432 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800433 stats->coded_size,
434 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
435 if (total > 0) {
436 int totals[4] = { 0, 0, 0, 0 };
437 fprintf(stderr, "block count: intra4: %d\n"
438 " intra16: %d (-> %.2f%%)\n",
439 num_i4, num_i16, 100.f * num_i16 / total);
440 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
441 num_skip, 100.f * num_skip / total);
442 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
443 " mode-partition: %6d (%.1f%%)\n",
444 stats->header_bytes[0],
445 100.f * stats->header_bytes[0] / stats->coded_size,
446 stats->header_bytes[1],
447 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700448 if (stats->alpha_data_size > 0) {
449 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
450 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700451 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700452 if (stats->layer_data_size) {
453 fprintf(stderr, " enhancement: %6d\n",
454 stats->layer_data_size);
455 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800456 fprintf(stderr, " Residuals bytes "
457 "|segment 1|segment 2|segment 3"
458 "|segment 4| total\n");
459 fprintf(stderr, " intra4-coeffs: ");
460 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
461 fprintf(stderr, " intra16-coeffs: ");
462 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
463 fprintf(stderr, " chroma coeffs: ");
464 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
465 fprintf(stderr, " macroblocks: ");
466 PrintPercents(stats->segment_size, total);
467 fprintf(stderr, " quantizer: ");
468 PrintValues(stats->segment_quant);
469 fprintf(stderr, " filter level: ");
470 PrintValues(stats->segment_level);
471 fprintf(stderr, "------------------+---------");
472 fprintf(stderr, "+---------+---------+---------+-----------------\n");
473 fprintf(stderr, " segments total: ");
474 PrintByteCount(totals, stats->coded_size, NULL);
475 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700476 if (stats->lossless_size > 0) {
477 PrintFullLosslessInfo(stats, "alpha");
478 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800479 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700480 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800481 const int mb_w = (pic->width + 15) / 16;
482 const int mb_h = (pic->height + 15) / 16;
483 const int type = pic->extra_info_type;
484 int x, y;
485 for (y = 0; y < mb_h; ++y) {
486 for (x = 0; x < mb_w; ++x) {
487 const int c = pic->extra_info[x + y * mb_w];
488 if (type == 1) { // intra4/intra16
489 printf("%c", "+."[c]);
490 } else if (type == 2) { // segments
491 printf("%c", ".-*X"[c]);
492 } else if (type == 3) { // quantizers
493 printf("%.2d ", c);
494 } else if (type == 6 || type == 7) {
495 printf("%3d ", c);
496 } else {
497 printf("0x%.2x ", c);
498 }
499 }
500 printf("\n");
501 }
502 }
503}
504
James Zernc7e86ab2011-08-25 14:22:32 -0700505//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800506
507static int MyWriter(const uint8_t* data, size_t data_size,
508 const WebPPicture* const pic) {
509 FILE* const out = (FILE*)pic->custom_ptr;
510 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
511}
512
513// Dumps a picture as a PGM file using the IMC4 layout.
514static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
515 int y;
516 const int uv_width = (picture->width + 1) / 2;
517 const int uv_height = (picture->height + 1) / 2;
518 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700519 const int alpha_height =
520 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700521 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800522 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800523 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800524 fprintf(f, "P5\n%d %d\n255\n", stride, height);
525 for (y = 0; y < picture->height; ++y) {
526 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
527 return 0;
528 if (picture->width & 1) fputc(0, f); // pad
529 }
530 for (y = 0; y < uv_height; ++y) {
531 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
532 return 0;
533 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
534 return 0;
535 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700536 for (y = 0; y < alpha_height; ++y) {
537 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
538 return 0;
539 if (picture->width & 1) fputc(0, f); // pad
540 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800541 fclose(f);
542 return 1;
543}
544
James Zernc7e86ab2011-08-25 14:22:32 -0700545//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800546
Pascal Massimino30971c92011-12-01 02:24:50 -0800547static int ProgressReport(int percent, const WebPPicture* const picture) {
548 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700549 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800550 fflush(stdout);
551 return 1; // all ok
552}
553
554//------------------------------------------------------------------------------
555
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700556static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800557 printf("Usage:\n\n");
558 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
559 printf("where quality is between 0 (poor) to 100 (very good).\n");
560 printf("Typical value is around 80.\n\n");
561 printf("Try -longhelp for an exhaustive list of advanced options.\n");
562}
563
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700564static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800565 printf("Usage:\n");
566 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
567 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700568 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700569#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800570 printf("Windows builds can take as input any of the files handled by WIC\n");
571#endif
572 printf("options:\n");
573 printf(" -h / -help ............ short help\n");
574 printf(" -H / -longhelp ........ long help\n");
575 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530576 printf(" -alpha_q <int> ......... Transparency-compression quality "
577 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800578 printf(" -preset <string> ....... Preset setting, one of:\n");
579 printf(" default, photo, picture,\n");
580 printf(" drawing, icon, text\n");
581 printf(" -preset must come first, as it overwrites other parameters.");
582 printf("\n");
583 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
584 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700585 printf(" -size <int> ............ Target size (in bytes)\n");
586 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800587 printf("\n");
588 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
589 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
590 printf(" -f <int> ............... filter strength (0=off..100)\n");
591 printf(" -sharpness <int> ....... "
592 "filter sharpness (0:most .. 7:least sharp)\n");
593 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700594 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
595 printf(" "
596 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800597 printf(" -pass <int> ............ analysis pass number (1..10)\n");
598 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700599 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
600#ifdef WEBP_EXPERIMENTAL_FEATURES
601 printf(" -444 / -422 / -gray ..... Change colorspace\n");
602#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800603 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800604 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700605 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
606 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800607 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530608 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800609 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
610 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000611 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530612 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530613 printf(" -lossless .............. Encode image losslessly.\n");
614 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700615 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700616
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800617 printf("\n");
618 printf(" -short ................. condense printed message\n");
619 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700620 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700621#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700622 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700623#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800624 printf(" -v ..................... verbose, e.g. print encoding/decoding "
625 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800626 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800627 printf("\n");
628 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800629 printf(" -af .................... auto-adjust filter strength.\n");
630 printf(" -pre <int> ............. pre-processing filter\n");
631 printf("\n");
632}
633
James Zernc7e86ab2011-08-25 14:22:32 -0700634//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700635// Error messages
636
637static const char* const kErrorMessages[] = {
638 "OK",
639 "OUT_OF_MEMORY: Out of memory allocating objects",
640 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
641 "NULL_PARAMETER: NULL parameter passed to function",
642 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700643 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
644 "allowed is 16383 pixels.",
645 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
646 "To reduce the size of this partition, try using less segments "
647 "with the -segments option, and eventually reduce the number of "
648 "header bits using -partition_limit. More details are available "
649 "in the manual (`man cwebp`)",
650 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800651 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800652 "FILE_TOO_BIG: File would be too big to fit in 4G",
653 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700654};
655
James Zernc7e86ab2011-08-25 14:22:32 -0700656//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800657
658int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700659 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800660 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
661 FILE *out = NULL;
662 int c;
663 int short_output = 0;
664 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530665 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800666 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700667 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800668 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700670 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800671 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800672 WebPConfig config;
673 WebPAuxStats stats;
James Zern63aba3a2012-12-03 18:20:00 -0800674 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800675 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700676
James Zern63aba3a2012-12-03 18:20:00 -0800677 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800678 if (!WebPPictureInit(&picture) ||
679 !WebPPictureInit(&original_picture) ||
680 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800681 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700682 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800683 }
684
685 if (argc == 1) {
686 HelpShort();
687 return 0;
688 }
689
690 for (c = 1; c < argc; ++c) {
691 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
692 HelpShort();
693 return 0;
694 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
695 HelpLong();
696 return 0;
697 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
698 out_file = argv[++c];
699 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
700 dump_file = argv[++c];
701 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800702 } else if (!strcmp(argv[c], "-print_psnr")) {
703 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700704 print_distortion = 0;
705 } else if (!strcmp(argv[c], "-print_ssim")) {
706 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800707 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700708 } else if (!strcmp(argv[c], "-print_lsim")) {
709 config.show_compressed = 1;
710 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 } else if (!strcmp(argv[c], "-short")) {
712 short_output++;
713 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700714 picture.width = strtol(argv[++c], NULL, 0);
715 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800716 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700717 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800718 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200719 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530720 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
721 config.alpha_quality = strtol(argv[++c], NULL, 0);
722 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
723 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000724 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
725 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530726 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800727 ++c;
728 if (!strcmp(argv[c], "none")) {
729 config.alpha_filtering = 0;
730 } else if (!strcmp(argv[c], "fast")) {
731 config.alpha_filtering = 1;
732 } else if (!strcmp(argv[c], "best")) {
733 config.alpha_filtering = 2;
734 } else {
735 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
736 goto Error;
737 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530738 } else if (!strcmp(argv[c], "-noalpha")) {
739 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000740 } else if (!strcmp(argv[c], "-lossless")) {
741 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000742 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530743 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
744 ++c;
745 if (!strcmp(argv[c], "photo")) {
746 config.image_hint = WEBP_HINT_PHOTO;
747 } else if (!strcmp(argv[c], "picture")) {
748 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700749 } else if (!strcmp(argv[c], "graph")) {
750 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530751 } else {
752 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
753 goto Error;
754 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800755 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700756 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800757 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200758 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800759 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700760 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800761 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700762 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800763 } else if (!strcmp(argv[c], "-af")) {
764 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700765 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800766 config.filter_type = 1;
767 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700768 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800769 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700770 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800771 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700772 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800773 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700774 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700775 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
776 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700778 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700779#ifdef WEBP_EXPERIMENTAL_FEATURES
780 } else if (!strcmp(argv[c], "-444")) {
781 picture.colorspace = WEBP_YUV444;
782 } else if (!strcmp(argv[c], "-422")) {
783 picture.colorspace = WEBP_YUV422;
784 } else if (!strcmp(argv[c], "-gray")) {
785 picture.colorspace = WEBP_YUV400;
786#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
788 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700789 crop_x = strtol(argv[++c], NULL, 0);
790 crop_y = strtol(argv[++c], NULL, 0);
791 crop_w = strtol(argv[++c], NULL, 0);
792 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700793 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
794 resize_w = strtol(argv[++c], NULL, 0);
795 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700796#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700797 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000798 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700799#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700800 } else if (!strcmp(argv[c], "-version")) {
801 const int version = WebPGetEncoderVersion();
802 printf("%d.%d.%d\n",
803 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
804 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800805 } else if (!strcmp(argv[c], "-progress")) {
806 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800807 } else if (!strcmp(argv[c], "-quiet")) {
808 quiet = 1;
809 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
810 WebPPreset preset;
811 ++c;
812 if (!strcmp(argv[c], "default")) {
813 preset = WEBP_PRESET_DEFAULT;
814 } else if (!strcmp(argv[c], "photo")) {
815 preset = WEBP_PRESET_PHOTO;
816 } else if (!strcmp(argv[c], "picture")) {
817 preset = WEBP_PRESET_PICTURE;
818 } else if (!strcmp(argv[c], "drawing")) {
819 preset = WEBP_PRESET_DRAWING;
820 } else if (!strcmp(argv[c], "icon")) {
821 preset = WEBP_PRESET_ICON;
822 } else if (!strcmp(argv[c], "text")) {
823 preset = WEBP_PRESET_TEXT;
824 } else {
825 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
826 goto Error;
827 }
828 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700829 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800830 goto Error;
831 }
832 } else if (!strcmp(argv[c], "-v")) {
833 verbose = 1;
834 } else if (argv[c][0] == '-') {
835 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
836 HelpLong();
837 return -1;
838 } else {
839 in_file = argv[c];
840 }
841 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700842 if (in_file == NULL) {
843 fprintf(stderr, "No input file specified!\n");
844 HelpShort();
845 goto Error;
846 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800847
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530848 // Check for unsupported command line options for lossless mode and log
849 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000850 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530851 if (config.target_size > 0 || config.target_PSNR > 0) {
852 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
853 " for lossless encoding. Ignoring such option(s)!\n");
854 }
855 if (config.partition_limit > 0) {
856 fprintf(stderr, "Partition limit option is not required for lossless"
857 " encoding. Ignoring this option!\n");
858 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530859 }
860
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800861 if (!WebPValidateConfig(&config)) {
862 fprintf(stderr, "Error! Invalid configuration.\n");
863 goto Error;
864 }
865
Pascal Massimino0744e842011-02-25 12:03:27 -0800866 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700867 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800868 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700869 }
James Zern63aba3a2012-12-03 18:20:00 -0800870 if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800871 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700872 goto Error;
873 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800874 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
875
Pascal Massimino0744e842011-02-25 12:03:27 -0800876 if (verbose) {
877 const double time = StopwatchReadAndReset(&stop_watch);
878 fprintf(stderr, "Time to read input: %.3fs\n", time);
879 }
880
881 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800882 if (out_file) {
883 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800884 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
886 goto Error;
887 } else {
888 if (!short_output && !quiet) {
889 fprintf(stderr, "Saving file '%s'\n", out_file);
890 }
891 }
892 picture.writer = MyWriter;
893 picture.custom_ptr = (void*)out;
894 } else {
895 out = NULL;
896 if (!quiet && !short_output) {
897 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
898 fprintf(stderr, "be performed, but its results discarded.\n\n");
899 }
900 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700901 if (!quiet) {
902 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700903 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700904 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800905
Pascal Massimino0744e842011-02-25 12:03:27 -0800906 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700907 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800908 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700909 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -0700910 if (crop != 0) {
911 // We use self-cropping using a view.
912 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
913 fprintf(stderr, "Error! Cannot crop picture\n");
914 goto Error;
915 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700916 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700917 if ((resize_w | resize_h) > 0) {
918 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
919 fprintf(stderr, "Error! Cannot resize picture\n");
920 goto Error;
921 }
922 }
923 if (picture.extra_info_type > 0) {
924 AllocExtraInfo(&picture);
925 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700926 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -0800927 WebPPictureCopy(&picture, &original_picture);
928 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700929 if (!WebPEncode(&config, &picture)) {
930 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700931 fprintf(stderr, "Error code: %d (%s)\n",
932 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700933 goto Error;
934 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800935 if (verbose) {
936 const double time = StopwatchReadAndReset(&stop_watch);
937 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
938 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800939
940 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700941 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +0000942 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -0700943 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
944 } else if (!DumpPicture(&picture, dump_file)) {
945 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
946 }
Pascal Massimino38369c02011-05-04 22:59:51 -0700947 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800948
Pascal Massimino38369c02011-05-04 22:59:51 -0700949 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +0530950 if (config.lossless) {
951 PrintExtraInfoLossless(&picture, short_output, in_file);
952 } else {
953 PrintExtraInfoLossy(&picture, short_output, in_file);
954 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800955 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700956 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
957 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -0800958 float values[5];
959 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700960 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800961 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700962 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -0800963 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -0700964 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700965 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800966
967 Error:
968 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -0800969 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800970 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800971 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800972 if (out != NULL) {
973 fclose(out);
974 }
975
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700976 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800977}
978
James Zernc7e86ab2011-08-25 14:22:32 -0700979//------------------------------------------------------------------------------