blob: 19def03d123e8dd9f33d972221d255484029aadb [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");
323 if (in_file == NULL) {
324 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
325 return ok;
326 }
327
328 if (pic->width == 0 || pic->height == 0) {
329 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
330 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700331 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800332 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700333 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800334 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700335 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800336 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800337 }
338 } else {
339 // If image size is specified, infer it as YUV format.
340 ok = ReadYUV(in_file, pic);
341 }
342 if (!ok) {
343 fprintf(stderr, "Error! Could not process file %s\n", filename);
344 }
345
346 fclose(in_file);
347 return ok;
348}
349
James Zern31f9dc62011-06-06 17:56:50 -0700350#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800351
352static void AllocExtraInfo(WebPPicture* const pic) {
353 const int mb_w = (pic->width + 15) / 16;
354 const int mb_h = (pic->height + 15) / 16;
355 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
356}
357
358static void PrintByteCount(const int bytes[4], int total_size,
359 int* const totals) {
360 int s;
361 int total = 0;
362 for (s = 0; s < 4; ++s) {
363 fprintf(stderr, "| %7d ", bytes[s]);
364 total += bytes[s];
365 if (totals) totals[s] += bytes[s];
366 }
James Zern04e84cf2011-11-04 15:20:08 -0700367 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800368}
369
370static void PrintPercents(const int counts[4], int total) {
371 int s;
372 for (s = 0; s < 4; ++s) {
373 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
374 }
James Zern04e84cf2011-11-04 15:20:08 -0700375 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800376}
377
378static void PrintValues(const int values[4]) {
379 int s;
380 for (s = 0; s < 4; ++s) {
381 fprintf(stderr, "| %7d ", values[s]);
382 }
James Zern04e84cf2011-11-04 15:20:08 -0700383 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800384}
385
Pascal Massimino7d853d72012-07-24 16:15:36 -0700386static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
387 const char* const description) {
388 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
389 description, stats->lossless_size);
390 if (stats->lossless_features) {
391 fprintf(stderr, " * Lossless features used:");
392 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
393 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
394 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
395 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
396 fprintf(stderr, "\n");
397 }
398 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
399 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
400 if (stats->palette_size > 0) {
401 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
402 }
403}
404
Vikas Arorac4ccab62012-05-09 11:27:46 +0530405static void PrintExtraInfoLossless(const WebPPicture* const pic,
406 int short_output,
407 const char* const file_name) {
408 const WebPAuxStats* const stats = pic->stats;
409 if (short_output) {
410 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
411 } else {
412 fprintf(stderr, "File: %s\n", file_name);
413 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
414 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700415 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530416 }
417}
418
419static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
420 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800421 const WebPAuxStats* const stats = pic->stats;
422 if (short_output) {
423 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700424 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800425 const int num_i4 = stats->block_count[0];
426 const int num_i16 = stats->block_count[1];
427 const int num_skip = stats->block_count[2];
428 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800429 fprintf(stderr, "File: %s\n", file_name);
430 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700431 pic->width, pic->height,
432 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800433 fprintf(stderr, "Output: "
434 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800435 stats->coded_size,
436 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
437 if (total > 0) {
438 int totals[4] = { 0, 0, 0, 0 };
439 fprintf(stderr, "block count: intra4: %d\n"
440 " intra16: %d (-> %.2f%%)\n",
441 num_i4, num_i16, 100.f * num_i16 / total);
442 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
443 num_skip, 100.f * num_skip / total);
444 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
445 " mode-partition: %6d (%.1f%%)\n",
446 stats->header_bytes[0],
447 100.f * stats->header_bytes[0] / stats->coded_size,
448 stats->header_bytes[1],
449 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700450 if (stats->alpha_data_size > 0) {
451 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
452 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700453 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700454 if (stats->layer_data_size) {
455 fprintf(stderr, " enhancement: %6d\n",
456 stats->layer_data_size);
457 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800458 fprintf(stderr, " Residuals bytes "
459 "|segment 1|segment 2|segment 3"
460 "|segment 4| total\n");
461 fprintf(stderr, " intra4-coeffs: ");
462 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
463 fprintf(stderr, " intra16-coeffs: ");
464 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
465 fprintf(stderr, " chroma coeffs: ");
466 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
467 fprintf(stderr, " macroblocks: ");
468 PrintPercents(stats->segment_size, total);
469 fprintf(stderr, " quantizer: ");
470 PrintValues(stats->segment_quant);
471 fprintf(stderr, " filter level: ");
472 PrintValues(stats->segment_level);
473 fprintf(stderr, "------------------+---------");
474 fprintf(stderr, "+---------+---------+---------+-----------------\n");
475 fprintf(stderr, " segments total: ");
476 PrintByteCount(totals, stats->coded_size, NULL);
477 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700478 if (stats->lossless_size > 0) {
479 PrintFullLosslessInfo(stats, "alpha");
480 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800481 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700482 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800483 const int mb_w = (pic->width + 15) / 16;
484 const int mb_h = (pic->height + 15) / 16;
485 const int type = pic->extra_info_type;
486 int x, y;
487 for (y = 0; y < mb_h; ++y) {
488 for (x = 0; x < mb_w; ++x) {
489 const int c = pic->extra_info[x + y * mb_w];
490 if (type == 1) { // intra4/intra16
491 printf("%c", "+."[c]);
492 } else if (type == 2) { // segments
493 printf("%c", ".-*X"[c]);
494 } else if (type == 3) { // quantizers
495 printf("%.2d ", c);
496 } else if (type == 6 || type == 7) {
497 printf("%3d ", c);
498 } else {
499 printf("0x%.2x ", c);
500 }
501 }
502 printf("\n");
503 }
504 }
505}
506
James Zernc7e86ab2011-08-25 14:22:32 -0700507//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800508
509static int MyWriter(const uint8_t* data, size_t data_size,
510 const WebPPicture* const pic) {
511 FILE* const out = (FILE*)pic->custom_ptr;
512 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
513}
514
515// Dumps a picture as a PGM file using the IMC4 layout.
516static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
517 int y;
518 const int uv_width = (picture->width + 1) / 2;
519 const int uv_height = (picture->height + 1) / 2;
520 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700521 const int alpha_height =
522 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700523 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800524 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800525 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800526 fprintf(f, "P5\n%d %d\n255\n", stride, height);
527 for (y = 0; y < picture->height; ++y) {
528 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
529 return 0;
530 if (picture->width & 1) fputc(0, f); // pad
531 }
532 for (y = 0; y < uv_height; ++y) {
533 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
534 return 0;
535 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
536 return 0;
537 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700538 for (y = 0; y < alpha_height; ++y) {
539 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
540 return 0;
541 if (picture->width & 1) fputc(0, f); // pad
542 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800543 fclose(f);
544 return 1;
545}
546
James Zern7eaee9f2013-01-11 12:25:36 -0800547// -----------------------------------------------------------------------------
548// Metadata writing.
549
550enum {
551 METADATA_EXIF = (1 << 0),
552 METADATA_ICCP = (1 << 1),
553 METADATA_XMP = (1 << 2),
554 METADATA_ALL = METADATA_EXIF | METADATA_ICCP | METADATA_XMP
555};
556
James Zern76ec5fa2013-01-14 18:32:44 -0800557static const int kChunkHeaderSize = 8;
558static const int kTagSize = 4;
559
560// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
561static int WriteLE(FILE* const out, uint32_t val, int num) {
562 uint8_t buf[4];
563 int i;
564 for (i = 0; i < num; ++i) {
565 buf[i] = (uint8_t)(val & 0xff);
566 val >>= 8;
567 }
568 return (fwrite(buf, num, 1, out) == 1);
569}
570
571static int WriteLE24(FILE* const out, uint32_t val) {
572 return WriteLE(out, val, 3);
573}
574
575static int WriteLE32(FILE* const out, uint32_t val) {
576 return WriteLE(out, val, 4);
577}
578
579static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
580 const MetadataPayload* const payload) {
581 const uint8_t zero = 0;
582 const size_t need_padding = payload->size & 1;
583 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
584 ok = ok && WriteLE32(out, (uint32_t)payload->size);
585 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
586 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
587}
588
589// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
590// chunk if there is metadata and 'keep' is true.
591static int UpdateFlagsAndSize(const MetadataPayload* const payload,
592 int keep, int flag,
593 uint32_t* vp8x_flags, uint64_t* metadata_size) {
594 if (keep && payload->bytes != NULL && payload->size > 0) {
595 *vp8x_flags |= flag;
596 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
597 return 1;
598 }
599 return 0;
600}
601
602// Writes a WebP file using the image contained in 'memory_writer' and the
603// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
604// availability in 'metadata'. Returns true on success.
605// For details see doc/webp-container-spec.txt#extended-file-format.
606static int WriteWebPWithMetadata(FILE* const out,
607 const WebPPicture* const picture,
608 const WebPMemoryWriter* const memory_writer,
609 const Metadata* const metadata,
610 int keep_metadata) {
611 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
612 const int kAlphaFlag = 0x10;
613 const int kEXIFFlag = 0x08;
614 const int kICCPFlag = 0x20;
615 const int kXMPFlag = 0x04;
616 const size_t kRiffHeaderSize = 12;
617 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
618 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
619 uint32_t flags = 0;
620 uint64_t metadata_size = 0;
621 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
622 !!(keep_metadata & METADATA_EXIF),
623 kEXIFFlag, &flags, &metadata_size);
624 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
625 !!(keep_metadata & METADATA_ICCP),
626 kICCPFlag, &flags, &metadata_size);
627 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
628 !!(keep_metadata & METADATA_XMP),
629 kXMPFlag, &flags, &metadata_size);
630 uint8_t* webp = memory_writer->mem;
631 size_t webp_size = memory_writer->size;
632 if (webp_size < kMinSize) return 0;
633 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
634 fprintf(stderr, "Error! Addition of metadata would exceed "
635 "container size limit.\n");
636 return 0;
637 }
638
639 if (metadata_size > 0) {
640 const int kVP8XChunkSize = 18;
641 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
642 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
643 (has_vp8x ? 0 : kVP8XChunkSize) +
644 metadata_size);
645 // RIFF
646 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
647 // RIFF size (file header size is not recorded)
648 ok = ok && WriteLE32(out, riff_size);
649 webp += kChunkHeaderSize;
650 webp_size -= kChunkHeaderSize;
651 // WEBP
652 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
653 webp += kTagSize;
654 webp_size -= kTagSize;
655 if (has_vp8x) { // update the existing VP8X flags
656 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
657 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
658 webp_size -= kVP8XChunkSize;
659 } else {
660 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
661 // The alpha flag is forced with lossless images.
662 if (is_lossless) flags |= kAlphaFlag;
663 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
664 ok = ok && WriteLE32(out, flags);
665 ok = ok && WriteLE24(out, picture->width - 1);
666 ok = ok && WriteLE24(out, picture->height - 1);
667 }
668 if (write_iccp) ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
669 // Image
670 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
671 if (write_exif) ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
672 if (write_xmp) ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
673 return ok;
674 } else {
675 // No metadata, just write the original image file.
676 return (fwrite(webp, webp_size, 1, out) == 1);
677 }
678}
679
James Zernc7e86ab2011-08-25 14:22:32 -0700680//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800681
Pascal Massimino30971c92011-12-01 02:24:50 -0800682static int ProgressReport(int percent, const WebPPicture* const picture) {
683 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700684 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800685 fflush(stdout);
686 return 1; // all ok
687}
688
689//------------------------------------------------------------------------------
690
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700691static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800692 printf("Usage:\n\n");
693 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
694 printf("where quality is between 0 (poor) to 100 (very good).\n");
695 printf("Typical value is around 80.\n\n");
696 printf("Try -longhelp for an exhaustive list of advanced options.\n");
697}
698
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700699static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 printf("Usage:\n");
701 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
702 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700703 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700704#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800705 printf("Windows builds can take as input any of the files handled by WIC\n");
706#endif
707 printf("options:\n");
708 printf(" -h / -help ............ short help\n");
709 printf(" -H / -longhelp ........ long help\n");
710 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530711 printf(" -alpha_q <int> ......... Transparency-compression quality "
712 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 printf(" -preset <string> ....... Preset setting, one of:\n");
714 printf(" default, photo, picture,\n");
715 printf(" drawing, icon, text\n");
716 printf(" -preset must come first, as it overwrites other parameters.");
717 printf("\n");
718 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
719 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700720 printf(" -size <int> ............ Target size (in bytes)\n");
721 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800722 printf("\n");
723 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
724 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
725 printf(" -f <int> ............... filter strength (0=off..100)\n");
726 printf(" -sharpness <int> ....... "
727 "filter sharpness (0:most .. 7:least sharp)\n");
728 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700729 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
730 printf(" "
731 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800732 printf(" -pass <int> ............ analysis pass number (1..10)\n");
733 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700734 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
735#ifdef WEBP_EXPERIMENTAL_FEATURES
736 printf(" -444 / -422 / -gray ..... Change colorspace\n");
737#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800738 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800739 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700740 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
741 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800742 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530743 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800744 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
745 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000746 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530747 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530748 printf(" -lossless .............. Encode image losslessly.\n");
749 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700750 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700751
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800752 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800753 printf(" -metadata <string> ..... comma separated list of metadata to\n");
754 printf(" ");
755 printf("copy from the input to the output if present.\n");
756 printf(" "
757 "Valid values: all, none (default), exif, iccp, xmp\n");
758
759 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800760 printf(" -short ................. condense printed message\n");
761 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700762 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700763#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700764 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700765#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800766 printf(" -v ..................... verbose, e.g. print encoding/decoding "
767 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800768 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800769 printf("\n");
770 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800771 printf(" -af .................... auto-adjust filter strength.\n");
772 printf(" -pre <int> ............. pre-processing filter\n");
773 printf("\n");
774}
775
James Zernc7e86ab2011-08-25 14:22:32 -0700776//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700777// Error messages
778
779static const char* const kErrorMessages[] = {
780 "OK",
781 "OUT_OF_MEMORY: Out of memory allocating objects",
782 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
783 "NULL_PARAMETER: NULL parameter passed to function",
784 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700785 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
786 "allowed is 16383 pixels.",
787 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
788 "To reduce the size of this partition, try using less segments "
789 "with the -segments option, and eventually reduce the number of "
790 "header bits using -partition_limit. More details are available "
791 "in the manual (`man cwebp`)",
792 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800793 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800794 "FILE_TOO_BIG: File would be too big to fit in 4G",
795 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700796};
797
James Zernc7e86ab2011-08-25 14:22:32 -0700798//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799
800int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700801 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800802 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
803 FILE *out = NULL;
804 int c;
805 int short_output = 0;
806 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530807 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800808 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700809 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800810 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800811 int keep_metadata = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800812 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700813 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800814 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800815 WebPConfig config;
816 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800817 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800818 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800819 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700820
James Zern63aba3a2012-12-03 18:20:00 -0800821 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800822 if (!WebPPictureInit(&picture) ||
823 !WebPPictureInit(&original_picture) ||
824 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800825 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700826 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800827 }
828
829 if (argc == 1) {
830 HelpShort();
831 return 0;
832 }
833
834 for (c = 1; c < argc; ++c) {
835 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
836 HelpShort();
837 return 0;
838 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
839 HelpLong();
840 return 0;
841 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
842 out_file = argv[++c];
843 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
844 dump_file = argv[++c];
845 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800846 } else if (!strcmp(argv[c], "-print_psnr")) {
847 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700848 print_distortion = 0;
849 } else if (!strcmp(argv[c], "-print_ssim")) {
850 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800851 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700852 } else if (!strcmp(argv[c], "-print_lsim")) {
853 config.show_compressed = 1;
854 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800855 } else if (!strcmp(argv[c], "-short")) {
856 short_output++;
857 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700858 picture.width = strtol(argv[++c], NULL, 0);
859 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800860 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700861 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800862 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200863 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530864 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
865 config.alpha_quality = strtol(argv[++c], NULL, 0);
866 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
867 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000868 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
869 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530870 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800871 ++c;
872 if (!strcmp(argv[c], "none")) {
873 config.alpha_filtering = 0;
874 } else if (!strcmp(argv[c], "fast")) {
875 config.alpha_filtering = 1;
876 } else if (!strcmp(argv[c], "best")) {
877 config.alpha_filtering = 2;
878 } else {
879 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
880 goto Error;
881 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530882 } else if (!strcmp(argv[c], "-noalpha")) {
883 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000884 } else if (!strcmp(argv[c], "-lossless")) {
885 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000886 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530887 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
888 ++c;
889 if (!strcmp(argv[c], "photo")) {
890 config.image_hint = WEBP_HINT_PHOTO;
891 } else if (!strcmp(argv[c], "picture")) {
892 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700893 } else if (!strcmp(argv[c], "graph")) {
894 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530895 } else {
896 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
897 goto Error;
898 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800899 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700900 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800901 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200902 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800903 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700904 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800905 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700906 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800907 } else if (!strcmp(argv[c], "-af")) {
908 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700909 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800910 config.filter_type = 1;
911 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700912 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800913 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700914 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800915 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700916 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800917 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700918 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700919 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
920 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800921 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700922 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700923#ifdef WEBP_EXPERIMENTAL_FEATURES
924 } else if (!strcmp(argv[c], "-444")) {
925 picture.colorspace = WEBP_YUV444;
926 } else if (!strcmp(argv[c], "-422")) {
927 picture.colorspace = WEBP_YUV422;
928 } else if (!strcmp(argv[c], "-gray")) {
929 picture.colorspace = WEBP_YUV400;
930#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800931 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
932 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700933 crop_x = strtol(argv[++c], NULL, 0);
934 crop_y = strtol(argv[++c], NULL, 0);
935 crop_w = strtol(argv[++c], NULL, 0);
936 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700937 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
938 resize_w = strtol(argv[++c], NULL, 0);
939 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700940#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700941 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000942 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700943#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700944 } else if (!strcmp(argv[c], "-version")) {
945 const int version = WebPGetEncoderVersion();
946 printf("%d.%d.%d\n",
947 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
948 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800949 } else if (!strcmp(argv[c], "-progress")) {
950 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800951 } else if (!strcmp(argv[c], "-quiet")) {
952 quiet = 1;
953 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
954 WebPPreset preset;
955 ++c;
956 if (!strcmp(argv[c], "default")) {
957 preset = WEBP_PRESET_DEFAULT;
958 } else if (!strcmp(argv[c], "photo")) {
959 preset = WEBP_PRESET_PHOTO;
960 } else if (!strcmp(argv[c], "picture")) {
961 preset = WEBP_PRESET_PICTURE;
962 } else if (!strcmp(argv[c], "drawing")) {
963 preset = WEBP_PRESET_DRAWING;
964 } else if (!strcmp(argv[c], "icon")) {
965 preset = WEBP_PRESET_ICON;
966 } else if (!strcmp(argv[c], "text")) {
967 preset = WEBP_PRESET_TEXT;
968 } else {
969 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
970 goto Error;
971 }
972 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700973 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800974 goto Error;
975 }
James Zern7eaee9f2013-01-11 12:25:36 -0800976 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
977 static const struct {
978 const char* option;
979 int flag;
980 } kTokens[] = {
981 { "all", METADATA_ALL },
982 { "none", 0 },
983 { "exif", METADATA_EXIF },
984 { "iccp", METADATA_ICCP },
985 { "xmp", METADATA_XMP },
986 };
987 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
988 const char* start = argv[++c];
989 const char* const end = start + strlen(start);
990
991 while (start < end) {
992 size_t i;
993 const char* token = strchr(start, ',');
994 if (token == NULL) token = end;
995
996 for (i = 0; i < kNumTokens; ++i) {
997 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
998 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
999 if (kTokens[i].flag != 0) {
1000 keep_metadata |= kTokens[i].flag;
1001 } else {
1002 keep_metadata = 0;
1003 }
1004 break;
1005 }
1006 }
1007 if (i == kNumTokens) {
1008 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
1009 (int)(token - start), start);
1010 HelpLong();
1011 return -1;
1012 }
1013 start = token + 1;
1014 }
James Zern76ec5fa2013-01-14 18:32:44 -08001015#ifdef HAVE_WINCODEC_H
James Zern7eaee9f2013-01-11 12:25:36 -08001016 if (keep_metadata != 0) {
1017 // TODO(jzern): remove when -metadata is supported on all platforms.
1018 fprintf(stderr, "Warning: -metadata is currently unsupported on this"
1019 " platform. Ignoring this option!\n");
1020 }
James Zern76ec5fa2013-01-14 18:32:44 -08001021#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001022 } else if (!strcmp(argv[c], "-v")) {
1023 verbose = 1;
1024 } else if (argv[c][0] == '-') {
1025 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1026 HelpLong();
1027 return -1;
1028 } else {
1029 in_file = argv[c];
1030 }
1031 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001032 if (in_file == NULL) {
1033 fprintf(stderr, "No input file specified!\n");
1034 HelpShort();
1035 goto Error;
1036 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001037
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301038 // Check for unsupported command line options for lossless mode and log
1039 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001040 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301041 if (config.target_size > 0 || config.target_PSNR > 0) {
1042 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1043 " for lossless encoding. Ignoring such option(s)!\n");
1044 }
1045 if (config.partition_limit > 0) {
1046 fprintf(stderr, "Partition limit option is not required for lossless"
1047 " encoding. Ignoring this option!\n");
1048 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301049 }
1050
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001051 if (!WebPValidateConfig(&config)) {
1052 fprintf(stderr, "Error! Invalid configuration.\n");
1053 goto Error;
1054 }
1055
Pascal Massimino0744e842011-02-25 12:03:27 -08001056 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001057 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001058 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001059 }
James Zern7eaee9f2013-01-11 12:25:36 -08001060 if (!ReadPicture(in_file, &picture, keep_alpha,
1061 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001062 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001063 goto Error;
1064 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001065 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1066
Pascal Massimino0744e842011-02-25 12:03:27 -08001067 if (verbose) {
1068 const double time = StopwatchReadAndReset(&stop_watch);
1069 fprintf(stderr, "Time to read input: %.3fs\n", time);
1070 }
1071
1072 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001073 if (out_file) {
1074 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001075 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001076 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1077 goto Error;
1078 } else {
1079 if (!short_output && !quiet) {
1080 fprintf(stderr, "Saving file '%s'\n", out_file);
1081 }
1082 }
James Zern76ec5fa2013-01-14 18:32:44 -08001083 if (keep_metadata == 0) {
1084 picture.writer = MyWriter;
1085 picture.custom_ptr = (void*)out;
1086 } else {
1087 WebPMemoryWriterInit(&memory_writer);
1088 picture.writer = WebPMemoryWrite;
1089 picture.custom_ptr = (void*)&memory_writer;
1090 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001091 } else {
1092 out = NULL;
1093 if (!quiet && !short_output) {
1094 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1095 fprintf(stderr, "be performed, but its results discarded.\n\n");
1096 }
1097 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001098 if (!quiet) {
1099 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001100 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001101 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001102
Pascal Massimino0744e842011-02-25 12:03:27 -08001103 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001104 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001105 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001106 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001107 if (crop != 0) {
1108 // We use self-cropping using a view.
1109 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1110 fprintf(stderr, "Error! Cannot crop picture\n");
1111 goto Error;
1112 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001113 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001114 if ((resize_w | resize_h) > 0) {
1115 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1116 fprintf(stderr, "Error! Cannot resize picture\n");
1117 goto Error;
1118 }
1119 }
1120 if (picture.extra_info_type > 0) {
1121 AllocExtraInfo(&picture);
1122 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001123 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001124 WebPPictureCopy(&picture, &original_picture);
1125 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001126 if (!WebPEncode(&config, &picture)) {
1127 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001128 fprintf(stderr, "Error code: %d (%s)\n",
1129 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001130 goto Error;
1131 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001132 if (verbose) {
1133 const double time = StopwatchReadAndReset(&stop_watch);
1134 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1135 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001136
1137 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001138 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001139 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001140 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1141 } else if (!DumpPicture(&picture, dump_file)) {
1142 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1143 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001144 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001145
James Zern76ec5fa2013-01-14 18:32:44 -08001146 if (keep_metadata != 0 && out != NULL) {
1147 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
1148 &metadata, keep_metadata)) {
1149 fprintf(stderr, "Error writing WebP file with metadata!\n");
1150 goto Error;
1151 }
1152 }
1153
Pascal Massimino38369c02011-05-04 22:59:51 -07001154 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301155 if (config.lossless) {
1156 PrintExtraInfoLossless(&picture, short_output, in_file);
1157 } else {
1158 PrintExtraInfoLossy(&picture, short_output, in_file);
1159 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001160 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001161 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
1162 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -08001163 float values[5];
1164 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001165 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001166 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001167 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -08001168 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001169 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001170 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001171
1172 Error:
1173 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001174 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001175 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001176 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001177 if (out != NULL) {
1178 fclose(out);
1179 }
1180
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001181 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001182}
1183
James Zernc7e86ab2011-08-25 14:22:32 -07001184//------------------------------------------------------------------------------