blob: 1358b1e34599e6d77a005097c895856dc4bcd0d7 [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 Zern6f76d242012-07-01 17:55:21 -070021#ifdef WEBP_HAVE_TIFF
22#include <tiffio.h>
23#endif
24
James Zern31f9dc62011-06-06 17:56:50 -070025#ifdef HAVE_WINCODEC_H
26#ifdef __MINGW32__
27#define INITGUID // Without this GUIDs are declared extern and fail to link
28#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080029#define CINTERFACE
30#define COBJMACROS
31#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
32 // code with COBJMACROS.
33#include <shlwapi.h>
34#include <windows.h>
35#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070036#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080037
38
39#include "webp/encode.h"
James Zern6a871d62012-12-06 13:48:21 -080040#include "./jpegdec.h"
James Zern63aba3a2012-12-03 18:20:00 -080041#include "./metadata.h"
James Zern2ee228f2012-12-06 13:06:37 -080042#include "./pngdec.h"
James Zern00b29e22012-05-15 13:48:11 -070043#include "./stopwatch.h"
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
James Zern6f76d242012-07-01 17:55:21 -0700291#ifdef WEBP_HAVE_TIFF
292static int ReadTIFF(const char* const filename,
293 WebPPicture* const pic, int keep_alpha) {
294 TIFF* const tif = TIFFOpen(filename, "r");
295 uint32 width, height;
296 uint32* raster;
297 int ok = 0;
298 int dircount = 1;
299
300 if (tif == NULL) {
301 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
302 return 0;
303 }
304
305 while (TIFFReadDirectory(tif)) ++dircount;
306
307 if (dircount > 1) {
308 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
309 "Only the first will be used, %d will be ignored.\n",
310 dircount - 1);
311 }
312
313 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
314 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
315 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
316 if (raster != NULL) {
317 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
318 ORIENTATION_TOPLEFT, 1)) {
319 const int stride = width * sizeof(*raster);
320 pic->width = width;
321 pic->height = height;
322 // TIFF data is ABGR
323#ifdef __BIG_ENDIAN__
324 TIFFSwabArrayOfLong(raster, width * height);
325#endif
326 ok = keep_alpha
327 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
328 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
329 }
330 _TIFFfree(raster);
331 } else {
332 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
333 }
334
335 if (ok && keep_alpha == 2) {
336 WebPCleanupTransparentArea(pic);
337 }
338
339 TIFFClose(tif);
340 return ok;
341}
342#else
343static int ReadTIFF(const char* const filename,
344 WebPPicture* const pic, int keep_alpha) {
345 (void)filename;
346 (void)pic;
347 (void)keep_alpha;
348 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
349 "development package before building.\n");
350 return 0;
351}
352#endif
353
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800354typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700355 PNG_ = 0,
356 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700357 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800358 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800359} InputFileFormat;
360
361static InputFileFormat GetImageType(FILE* in_file) {
362 InputFileFormat format = UNSUPPORTED;
363 unsigned int magic;
364 unsigned char buf[4];
365
366 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
367 (fseek(in_file, 0, SEEK_SET) != 0)) {
368 return format;
369 }
370
371 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
372 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700373 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800374 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700375 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700376 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
377 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800378 }
379 return format;
380}
381
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700382static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800383 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800384 int ok = 0;
385 FILE* in_file = fopen(filename, "rb");
James Zern63aba3a2012-12-03 18:20:00 -0800386 (void)metadata; // TODO(jzern): add metadata extraction to the formats below.
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800387 if (in_file == NULL) {
388 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
389 return ok;
390 }
391
392 if (pic->width == 0 || pic->height == 0) {
393 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
394 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700395 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700396 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700397 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800398 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700399 } else if (format == TIFF_) {
400 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800401 }
402 } else {
403 // If image size is specified, infer it as YUV format.
404 ok = ReadYUV(in_file, pic);
405 }
406 if (!ok) {
407 fprintf(stderr, "Error! Could not process file %s\n", filename);
408 }
409
410 fclose(in_file);
411 return ok;
412}
413
James Zern31f9dc62011-06-06 17:56:50 -0700414#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800415
416static void AllocExtraInfo(WebPPicture* const pic) {
417 const int mb_w = (pic->width + 15) / 16;
418 const int mb_h = (pic->height + 15) / 16;
419 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
420}
421
422static void PrintByteCount(const int bytes[4], int total_size,
423 int* const totals) {
424 int s;
425 int total = 0;
426 for (s = 0; s < 4; ++s) {
427 fprintf(stderr, "| %7d ", bytes[s]);
428 total += bytes[s];
429 if (totals) totals[s] += bytes[s];
430 }
James Zern04e84cf2011-11-04 15:20:08 -0700431 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800432}
433
434static void PrintPercents(const int counts[4], int total) {
435 int s;
436 for (s = 0; s < 4; ++s) {
437 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
438 }
James Zern04e84cf2011-11-04 15:20:08 -0700439 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800440}
441
442static void PrintValues(const int values[4]) {
443 int s;
444 for (s = 0; s < 4; ++s) {
445 fprintf(stderr, "| %7d ", values[s]);
446 }
James Zern04e84cf2011-11-04 15:20:08 -0700447 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800448}
449
Pascal Massimino7d853d72012-07-24 16:15:36 -0700450static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
451 const char* const description) {
452 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
453 description, stats->lossless_size);
454 if (stats->lossless_features) {
455 fprintf(stderr, " * Lossless features used:");
456 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
457 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
458 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
459 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
460 fprintf(stderr, "\n");
461 }
462 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
463 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
464 if (stats->palette_size > 0) {
465 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
466 }
467}
468
Vikas Arorac4ccab62012-05-09 11:27:46 +0530469static void PrintExtraInfoLossless(const WebPPicture* const pic,
470 int short_output,
471 const char* const file_name) {
472 const WebPAuxStats* const stats = pic->stats;
473 if (short_output) {
474 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
475 } else {
476 fprintf(stderr, "File: %s\n", file_name);
477 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
478 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700479 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530480 }
481}
482
483static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
484 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800485 const WebPAuxStats* const stats = pic->stats;
486 if (short_output) {
487 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700488 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800489 const int num_i4 = stats->block_count[0];
490 const int num_i16 = stats->block_count[1];
491 const int num_skip = stats->block_count[2];
492 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800493 fprintf(stderr, "File: %s\n", file_name);
494 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700495 pic->width, pic->height,
496 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800497 fprintf(stderr, "Output: "
498 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800499 stats->coded_size,
500 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
501 if (total > 0) {
502 int totals[4] = { 0, 0, 0, 0 };
503 fprintf(stderr, "block count: intra4: %d\n"
504 " intra16: %d (-> %.2f%%)\n",
505 num_i4, num_i16, 100.f * num_i16 / total);
506 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
507 num_skip, 100.f * num_skip / total);
508 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
509 " mode-partition: %6d (%.1f%%)\n",
510 stats->header_bytes[0],
511 100.f * stats->header_bytes[0] / stats->coded_size,
512 stats->header_bytes[1],
513 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700514 if (stats->alpha_data_size > 0) {
515 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
516 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700517 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700518 if (stats->layer_data_size) {
519 fprintf(stderr, " enhancement: %6d\n",
520 stats->layer_data_size);
521 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800522 fprintf(stderr, " Residuals bytes "
523 "|segment 1|segment 2|segment 3"
524 "|segment 4| total\n");
525 fprintf(stderr, " intra4-coeffs: ");
526 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
527 fprintf(stderr, " intra16-coeffs: ");
528 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
529 fprintf(stderr, " chroma coeffs: ");
530 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
531 fprintf(stderr, " macroblocks: ");
532 PrintPercents(stats->segment_size, total);
533 fprintf(stderr, " quantizer: ");
534 PrintValues(stats->segment_quant);
535 fprintf(stderr, " filter level: ");
536 PrintValues(stats->segment_level);
537 fprintf(stderr, "------------------+---------");
538 fprintf(stderr, "+---------+---------+---------+-----------------\n");
539 fprintf(stderr, " segments total: ");
540 PrintByteCount(totals, stats->coded_size, NULL);
541 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700542 if (stats->lossless_size > 0) {
543 PrintFullLosslessInfo(stats, "alpha");
544 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800545 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700546 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800547 const int mb_w = (pic->width + 15) / 16;
548 const int mb_h = (pic->height + 15) / 16;
549 const int type = pic->extra_info_type;
550 int x, y;
551 for (y = 0; y < mb_h; ++y) {
552 for (x = 0; x < mb_w; ++x) {
553 const int c = pic->extra_info[x + y * mb_w];
554 if (type == 1) { // intra4/intra16
555 printf("%c", "+."[c]);
556 } else if (type == 2) { // segments
557 printf("%c", ".-*X"[c]);
558 } else if (type == 3) { // quantizers
559 printf("%.2d ", c);
560 } else if (type == 6 || type == 7) {
561 printf("%3d ", c);
562 } else {
563 printf("0x%.2x ", c);
564 }
565 }
566 printf("\n");
567 }
568 }
569}
570
James Zernc7e86ab2011-08-25 14:22:32 -0700571//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800572
573static int MyWriter(const uint8_t* data, size_t data_size,
574 const WebPPicture* const pic) {
575 FILE* const out = (FILE*)pic->custom_ptr;
576 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
577}
578
579// Dumps a picture as a PGM file using the IMC4 layout.
580static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
581 int y;
582 const int uv_width = (picture->width + 1) / 2;
583 const int uv_height = (picture->height + 1) / 2;
584 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700585 const int alpha_height =
586 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700587 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800588 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800589 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800590 fprintf(f, "P5\n%d %d\n255\n", stride, height);
591 for (y = 0; y < picture->height; ++y) {
592 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
593 return 0;
594 if (picture->width & 1) fputc(0, f); // pad
595 }
596 for (y = 0; y < uv_height; ++y) {
597 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
598 return 0;
599 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
600 return 0;
601 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700602 for (y = 0; y < alpha_height; ++y) {
603 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
604 return 0;
605 if (picture->width & 1) fputc(0, f); // pad
606 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800607 fclose(f);
608 return 1;
609}
610
James Zernc7e86ab2011-08-25 14:22:32 -0700611//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800612
Pascal Massimino30971c92011-12-01 02:24:50 -0800613static int ProgressReport(int percent, const WebPPicture* const picture) {
614 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700615 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800616 fflush(stdout);
617 return 1; // all ok
618}
619
620//------------------------------------------------------------------------------
621
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700622static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800623 printf("Usage:\n\n");
624 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
625 printf("where quality is between 0 (poor) to 100 (very good).\n");
626 printf("Typical value is around 80.\n\n");
627 printf("Try -longhelp for an exhaustive list of advanced options.\n");
628}
629
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700630static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800631 printf("Usage:\n");
632 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
633 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700634 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700635#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800636 printf("Windows builds can take as input any of the files handled by WIC\n");
637#endif
638 printf("options:\n");
639 printf(" -h / -help ............ short help\n");
640 printf(" -H / -longhelp ........ long help\n");
641 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530642 printf(" -alpha_q <int> ......... Transparency-compression quality "
643 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 printf(" -preset <string> ....... Preset setting, one of:\n");
645 printf(" default, photo, picture,\n");
646 printf(" drawing, icon, text\n");
647 printf(" -preset must come first, as it overwrites other parameters.");
648 printf("\n");
649 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
650 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700651 printf(" -size <int> ............ Target size (in bytes)\n");
652 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800653 printf("\n");
654 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
655 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
656 printf(" -f <int> ............... filter strength (0=off..100)\n");
657 printf(" -sharpness <int> ....... "
658 "filter sharpness (0:most .. 7:least sharp)\n");
659 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700660 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
661 printf(" "
662 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800663 printf(" -pass <int> ............ analysis pass number (1..10)\n");
664 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700665 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
666#ifdef WEBP_EXPERIMENTAL_FEATURES
667 printf(" -444 / -422 / -gray ..... Change colorspace\n");
668#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800670 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700671 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
672 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800673 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530674 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800675 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
676 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000677 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530678 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530679 printf(" -lossless .............. Encode image losslessly.\n");
680 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700681 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700682
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800683 printf("\n");
684 printf(" -short ................. condense printed message\n");
685 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700686 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700687#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700688 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700689#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800690 printf(" -v ..................... verbose, e.g. print encoding/decoding "
691 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800692 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800693 printf("\n");
694 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800695 printf(" -af .................... auto-adjust filter strength.\n");
696 printf(" -pre <int> ............. pre-processing filter\n");
697 printf("\n");
698}
699
James Zernc7e86ab2011-08-25 14:22:32 -0700700//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700701// Error messages
702
703static const char* const kErrorMessages[] = {
704 "OK",
705 "OUT_OF_MEMORY: Out of memory allocating objects",
706 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
707 "NULL_PARAMETER: NULL parameter passed to function",
708 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700709 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
710 "allowed is 16383 pixels.",
711 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
712 "To reduce the size of this partition, try using less segments "
713 "with the -segments option, and eventually reduce the number of "
714 "header bits using -partition_limit. More details are available "
715 "in the manual (`man cwebp`)",
716 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800717 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800718 "FILE_TOO_BIG: File would be too big to fit in 4G",
719 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700720};
721
James Zernc7e86ab2011-08-25 14:22:32 -0700722//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800723
724int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700725 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800726 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
727 FILE *out = NULL;
728 int c;
729 int short_output = 0;
730 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530731 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800732 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700733 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800734 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800735 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700736 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800737 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800738 WebPConfig config;
739 WebPAuxStats stats;
James Zern63aba3a2012-12-03 18:20:00 -0800740 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800741 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700742
James Zern63aba3a2012-12-03 18:20:00 -0800743 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800744 if (!WebPPictureInit(&picture) ||
745 !WebPPictureInit(&original_picture) ||
746 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800747 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700748 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800749 }
750
751 if (argc == 1) {
752 HelpShort();
753 return 0;
754 }
755
756 for (c = 1; c < argc; ++c) {
757 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
758 HelpShort();
759 return 0;
760 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
761 HelpLong();
762 return 0;
763 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
764 out_file = argv[++c];
765 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
766 dump_file = argv[++c];
767 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800768 } else if (!strcmp(argv[c], "-print_psnr")) {
769 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700770 print_distortion = 0;
771 } else if (!strcmp(argv[c], "-print_ssim")) {
772 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800773 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700774 } else if (!strcmp(argv[c], "-print_lsim")) {
775 config.show_compressed = 1;
776 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777 } else if (!strcmp(argv[c], "-short")) {
778 short_output++;
779 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700780 picture.width = strtol(argv[++c], NULL, 0);
781 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800782 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700783 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200785 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530786 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
787 config.alpha_quality = strtol(argv[++c], NULL, 0);
788 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
789 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000790 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
791 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530792 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800793 ++c;
794 if (!strcmp(argv[c], "none")) {
795 config.alpha_filtering = 0;
796 } else if (!strcmp(argv[c], "fast")) {
797 config.alpha_filtering = 1;
798 } else if (!strcmp(argv[c], "best")) {
799 config.alpha_filtering = 2;
800 } else {
801 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
802 goto Error;
803 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530804 } else if (!strcmp(argv[c], "-noalpha")) {
805 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000806 } else if (!strcmp(argv[c], "-lossless")) {
807 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000808 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530809 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
810 ++c;
811 if (!strcmp(argv[c], "photo")) {
812 config.image_hint = WEBP_HINT_PHOTO;
813 } else if (!strcmp(argv[c], "picture")) {
814 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700815 } else if (!strcmp(argv[c], "graph")) {
816 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530817 } else {
818 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
819 goto Error;
820 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800821 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700822 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200824 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800825 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700826 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800827 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700828 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800829 } else if (!strcmp(argv[c], "-af")) {
830 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700831 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800832 config.filter_type = 1;
833 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700834 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700836 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800837 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700838 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800839 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700840 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700841 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
842 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800843 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700844 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700845#ifdef WEBP_EXPERIMENTAL_FEATURES
846 } else if (!strcmp(argv[c], "-444")) {
847 picture.colorspace = WEBP_YUV444;
848 } else if (!strcmp(argv[c], "-422")) {
849 picture.colorspace = WEBP_YUV422;
850 } else if (!strcmp(argv[c], "-gray")) {
851 picture.colorspace = WEBP_YUV400;
852#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800853 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
854 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700855 crop_x = strtol(argv[++c], NULL, 0);
856 crop_y = strtol(argv[++c], NULL, 0);
857 crop_w = strtol(argv[++c], NULL, 0);
858 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700859 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
860 resize_w = strtol(argv[++c], NULL, 0);
861 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700862#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700863 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000864 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700865#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700866 } else if (!strcmp(argv[c], "-version")) {
867 const int version = WebPGetEncoderVersion();
868 printf("%d.%d.%d\n",
869 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
870 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800871 } else if (!strcmp(argv[c], "-progress")) {
872 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800873 } else if (!strcmp(argv[c], "-quiet")) {
874 quiet = 1;
875 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
876 WebPPreset preset;
877 ++c;
878 if (!strcmp(argv[c], "default")) {
879 preset = WEBP_PRESET_DEFAULT;
880 } else if (!strcmp(argv[c], "photo")) {
881 preset = WEBP_PRESET_PHOTO;
882 } else if (!strcmp(argv[c], "picture")) {
883 preset = WEBP_PRESET_PICTURE;
884 } else if (!strcmp(argv[c], "drawing")) {
885 preset = WEBP_PRESET_DRAWING;
886 } else if (!strcmp(argv[c], "icon")) {
887 preset = WEBP_PRESET_ICON;
888 } else if (!strcmp(argv[c], "text")) {
889 preset = WEBP_PRESET_TEXT;
890 } else {
891 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
892 goto Error;
893 }
894 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700895 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800896 goto Error;
897 }
898 } else if (!strcmp(argv[c], "-v")) {
899 verbose = 1;
900 } else if (argv[c][0] == '-') {
901 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
902 HelpLong();
903 return -1;
904 } else {
905 in_file = argv[c];
906 }
907 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700908 if (in_file == NULL) {
909 fprintf(stderr, "No input file specified!\n");
910 HelpShort();
911 goto Error;
912 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800913
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530914 // Check for unsupported command line options for lossless mode and log
915 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000916 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530917 if (config.target_size > 0 || config.target_PSNR > 0) {
918 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
919 " for lossless encoding. Ignoring such option(s)!\n");
920 }
921 if (config.partition_limit > 0) {
922 fprintf(stderr, "Partition limit option is not required for lossless"
923 " encoding. Ignoring this option!\n");
924 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530925 }
926
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800927 if (!WebPValidateConfig(&config)) {
928 fprintf(stderr, "Error! Invalid configuration.\n");
929 goto Error;
930 }
931
Pascal Massimino0744e842011-02-25 12:03:27 -0800932 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700933 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800934 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700935 }
James Zern63aba3a2012-12-03 18:20:00 -0800936 if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800937 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700938 goto Error;
939 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800940 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
941
Pascal Massimino0744e842011-02-25 12:03:27 -0800942 if (verbose) {
943 const double time = StopwatchReadAndReset(&stop_watch);
944 fprintf(stderr, "Time to read input: %.3fs\n", time);
945 }
946
947 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800948 if (out_file) {
949 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800950 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800951 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
952 goto Error;
953 } else {
954 if (!short_output && !quiet) {
955 fprintf(stderr, "Saving file '%s'\n", out_file);
956 }
957 }
958 picture.writer = MyWriter;
959 picture.custom_ptr = (void*)out;
960 } else {
961 out = NULL;
962 if (!quiet && !short_output) {
963 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
964 fprintf(stderr, "be performed, but its results discarded.\n\n");
965 }
966 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700967 if (!quiet) {
968 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700969 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700970 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800971
Pascal Massimino0744e842011-02-25 12:03:27 -0800972 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700973 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800974 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700975 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -0700976 if (crop != 0) {
977 // We use self-cropping using a view.
978 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
979 fprintf(stderr, "Error! Cannot crop picture\n");
980 goto Error;
981 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700982 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700983 if ((resize_w | resize_h) > 0) {
984 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
985 fprintf(stderr, "Error! Cannot resize picture\n");
986 goto Error;
987 }
988 }
989 if (picture.extra_info_type > 0) {
990 AllocExtraInfo(&picture);
991 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700992 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -0800993 WebPPictureCopy(&picture, &original_picture);
994 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700995 if (!WebPEncode(&config, &picture)) {
996 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700997 fprintf(stderr, "Error code: %d (%s)\n",
998 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700999 goto Error;
1000 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001001 if (verbose) {
1002 const double time = StopwatchReadAndReset(&stop_watch);
1003 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1004 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001005
1006 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001007 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001008 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001009 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1010 } else if (!DumpPicture(&picture, dump_file)) {
1011 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1012 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001013 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001014
Pascal Massimino38369c02011-05-04 22:59:51 -07001015 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301016 if (config.lossless) {
1017 PrintExtraInfoLossless(&picture, short_output, in_file);
1018 } else {
1019 PrintExtraInfoLossy(&picture, short_output, in_file);
1020 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001021 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001022 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
1023 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -08001024 float values[5];
1025 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001026 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001027 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001028 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -08001029 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001030 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001031 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001032
1033 Error:
1034 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001035 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001036 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001037 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001038 if (out != NULL) {
1039 fclose(out);
1040 }
1041
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001042 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001043}
1044
James Zernc7e86ab2011-08-25 14:22:32 -07001045//------------------------------------------------------------------------------