blob: fc5da76030a28747b6140f0c80451e1b3b0f3272 [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08002//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// simple command line calling the WebPEncode function.
9// Encodes a raw .YUV into WebP bitstream
10//
11// Author: Skal (pascal.massimino@gmail.com)
12
13#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070014#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080015#include <string.h>
16
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#ifdef WEBP_HAVE_JPEG
James Zern2ee228f2012-12-06 13:06:37 -080022#include <setjmp.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080023#include <jpeglib.h>
24#endif
25
James Zern6f76d242012-07-01 17:55:21 -070026#ifdef WEBP_HAVE_TIFF
27#include <tiffio.h>
28#endif
29
James Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080042
43
44#include "webp/encode.h"
James Zern63aba3a2012-12-03 18:20:00 -080045#include "./metadata.h"
James Zern2ee228f2012-12-06 13:06:37 -080046#include "./pngdec.h"
James Zern00b29e22012-05-15 13:48:11 -070047#include "./stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070048#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070049#if defined(__cplusplus) || defined(c_plusplus)
50extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070051#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070052
James Zern04e84cf2011-11-04 15:20:08 -070053extern void* VP8GetCPUInfo; // opaque forward declaration.
54
55#if defined(__cplusplus) || defined(c_plusplus)
56} // extern "C"
57#endif
58#endif // WEBP_DLL
59
James Zernc7e86ab2011-08-25 14:22:32 -070060//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080061
62static int verbose = 0;
63
64static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000065 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080066 const int uv_width = (pic->width + 1) / 2;
67 const int uv_height = (pic->height + 1) / 2;
68 int y;
69 int ok = 0;
70
Pascal Massiminodd108172012-07-18 21:58:53 +000071 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080072 if (!WebPPictureAlloc(pic)) return ok;
73
74 for (y = 0; y < pic->height; ++y) {
75 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
76 goto End;
77 }
78 }
79 for (y = 0; y < uv_height; ++y) {
80 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
81 goto End;
82 }
83 for (y = 0; y < uv_height; ++y) {
84 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
85 goto End;
86 }
87 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000088 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080089
90 End:
91 return ok;
92}
93
James Zern31f9dc62011-06-06 17:56:50 -070094#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080095
James Zernf2b5d192012-10-08 18:33:18 -070096#define IFS(fn) \
97 do { \
98 if (SUCCEEDED(hr)) { \
99 hr = (fn); \
100 if (FAILED(hr)) fprintf(stderr, #fn " failed %08x\n", hr); \
101 } \
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800102 } while (0)
103
James Zern902d3e32012-05-08 17:49:39 -0700104// modified version of DEFINE_GUID from guiddef.h.
105#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
106 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
107
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800108#ifdef __cplusplus
109#define MAKE_REFGUID(x) (x)
110#else
111#define MAKE_REFGUID(x) &(x)
112#endif
113
James Zerneda8ee42012-10-19 18:34:06 -0700114typedef struct WICFormatImporter {
115 const GUID* pixel_format;
116 int bytes_per_pixel;
117 int (*import)(WebPPicture* const, const uint8_t* const, int);
118} WICFormatImporter;
119
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800120static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
121 HRESULT hr = S_OK;
122 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
123 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800124 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800125 return hr;
126}
127
128static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700129 WebPPicture* const pic, int keep_alpha) {
James Zerneda8ee42012-10-19 18:34:06 -0700130 // From Microsoft SDK 7.0a -- wincodec.h
131 // Create local copies for compatibility when building against earlier
132 // versions of the SDK.
133 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_,
134 0x6fddc324, 0x4e03, 0x4bfe,
135 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c);
136 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
137 0x6fddc324, 0x4e03, 0x4bfe,
138 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
139 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
140 0x6fddc324, 0x4e03, 0x4bfe,
141 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
142 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
143 0xf5c7ad2d, 0x6a8d, 0x43dd,
144 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
145 const WICFormatImporter alphaFormatImporters[] = {
146 { &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
147 { &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
148 { NULL, 0, NULL },
149 };
150 const WICFormatImporter nonAlphaFormatImporters[] = {
151 { &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
152 { &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
153 { NULL, 0, NULL },
154 };
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800155 HRESULT hr = S_OK;
156 IWICBitmapFrameDecode* pFrame = NULL;
157 IWICFormatConverter* pConverter = NULL;
158 IWICImagingFactory* pFactory = NULL;
159 IWICBitmapDecoder* pDecoder = NULL;
160 IStream* pStream = NULL;
161 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700162 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800163 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700164 WICPixelFormatGUID srcPixelFormat = { 0 };
James Zerneda8ee42012-10-19 18:34:06 -0700165 const WICFormatImporter* importer = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700166 GUID srcContainerFormat = { 0 };
167 const GUID* alphaContainers[] = {
168 &GUID_ContainerFormatBmp,
169 &GUID_ContainerFormatPng,
170 &GUID_ContainerFormatTiff
171 };
172 int has_alpha = 0;
173 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800174
175 IFS(CoInitialize(NULL));
176 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
177 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
178 (LPVOID*)&pFactory));
179 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800180 fprintf(stderr,
181 "Couldn't access Windows Imaging Component (are you running "
182 "Windows XP SP3 or newer?). Most formats not available. "
183 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800184 }
185 // Prepare for image decoding.
186 IFS(OpenInputStream(filename, &pStream));
187 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
188 WICDecodeMetadataCacheOnDemand, &pDecoder));
189 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
190 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800191 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800192 hr = E_FAIL;
193 }
194 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700195 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
196 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
197
James Zernecd66f72012-10-08 18:15:30 -0700198 if (keep_alpha) {
199 for (i = 0;
200 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
201 ++i) {
202 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
203 MAKE_REFGUID(*alphaContainers[i]))) {
204 has_alpha =
205 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
206 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
207 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
208 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
209 break;
210 }
James Zern3cfe0882011-06-21 18:29:30 -0700211 }
212 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800213
214 // Prepare for pixel format conversion (if necessary).
215 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
James Zerneda8ee42012-10-19 18:34:06 -0700216
217 for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters;
218 hr == S_OK && importer->import != NULL; ++importer) {
219 BOOL canConvert;
220 const HRESULT cchr = IWICFormatConverter_CanConvert(
221 pConverter,
222 MAKE_REFGUID(srcPixelFormat),
223 MAKE_REFGUID(*importer->pixel_format),
224 &canConvert);
225 if (SUCCEEDED(cchr) && canConvert) break;
226 }
227 if (importer->import == NULL) hr = E_FAIL;
228
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800229 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zerneda8ee42012-10-19 18:34:06 -0700230 importer->pixel_format,
James Zern3cfe0882011-06-21 18:29:30 -0700231 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800232 NULL, 0.0, WICBitmapPaletteTypeCustom));
233
234 // Decode.
235 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zerneda8ee42012-10-19 18:34:06 -0700236 stride = importer->bytes_per_pixel * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800237 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700238 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800239 if (rgb == NULL)
240 hr = E_OUTOFMEMORY;
241 }
James Zern3cfe0882011-06-21 18:29:30 -0700242 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
243 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800244
245 // WebP conversion.
246 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700247 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800248 pic->width = width;
249 pic->height = height;
James Zerneda8ee42012-10-19 18:34:06 -0700250 ok = importer->import(pic, rgb, stride);
James Zern3cfe0882011-06-21 18:29:30 -0700251 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800252 hr = E_FAIL;
253 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000254 if (SUCCEEDED(hr)) {
255 if (has_alpha && keep_alpha == 2) {
256 WebPCleanupTransparentArea(pic);
257 }
258 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800259
260 // Cleanup.
261 if (pConverter != NULL) IUnknown_Release(pConverter);
262 if (pFrame != NULL) IUnknown_Release(pFrame);
263 if (pDecoder != NULL) IUnknown_Release(pDecoder);
264 if (pFactory != NULL) IUnknown_Release(pFactory);
265 if (pStream != NULL) IUnknown_Release(pStream);
266 free(rgb);
267 return hr;
268}
269
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700270static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800271 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800272 int ok;
James Zern63aba3a2012-12-03 18:20:00 -0800273 (void)metadata; // TODO(jzern): add metadata extraction using WIC
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800274 if (pic->width != 0 && pic->height != 0) {
275 // If image size is specified, infer it as YUV format.
276 FILE* in_file = fopen(filename, "rb");
277 if (in_file == NULL) {
278 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
279 return 0;
280 }
281 ok = ReadYUV(in_file, pic);
282 fclose(in_file);
283 } else {
284 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700285 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800286 }
287 if (!ok) {
288 fprintf(stderr, "Error! Could not process file %s\n", filename);
289 }
290 return ok;
291}
292
James Zern31f9dc62011-06-06 17:56:50 -0700293#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800294
295#ifdef WEBP_HAVE_JPEG
296struct my_error_mgr {
297 struct jpeg_error_mgr pub;
298 jmp_buf setjmp_buffer;
299};
300
301static void my_error_exit(j_common_ptr dinfo) {
302 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
303 (*dinfo->err->output_message) (dinfo);
304 longjmp(myerr->setjmp_buffer, 1);
305}
306
307static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
308 int ok = 0;
309 int stride, width, height;
310 uint8_t* rgb = NULL;
311 uint8_t* row_ptr = NULL;
312 struct jpeg_decompress_struct dinfo;
313 struct my_error_mgr jerr;
314 JSAMPARRAY buffer;
315
316 dinfo.err = jpeg_std_error(&jerr.pub);
317 jerr.pub.error_exit = my_error_exit;
318
James Zern04e84cf2011-11-04 15:20:08 -0700319 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800320 Error:
321 jpeg_destroy_decompress(&dinfo);
322 goto End;
323 }
324
325 jpeg_create_decompress(&dinfo);
326 jpeg_stdio_src(&dinfo, in_file);
327 jpeg_read_header(&dinfo, TRUE);
328
329 dinfo.out_color_space = JCS_RGB;
330 dinfo.dct_method = JDCT_IFAST;
331 dinfo.do_fancy_upsampling = TRUE;
332
333 jpeg_start_decompress(&dinfo);
334
335 if (dinfo.output_components != 3) {
336 goto Error;
337 }
338
339 width = dinfo.output_width;
340 height = dinfo.output_height;
341 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
342
343 rgb = (uint8_t*)malloc(stride * height);
344 if (rgb == NULL) {
345 goto End;
346 }
347 row_ptr = rgb;
348
349 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
350 JPOOL_IMAGE, stride, 1);
351 if (buffer == NULL) {
352 goto End;
353 }
354
355 while (dinfo.output_scanline < dinfo.output_height) {
356 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
357 goto End;
358 }
359 memcpy(row_ptr, buffer[0], stride);
360 row_ptr += stride;
361 }
362
James Zern04e84cf2011-11-04 15:20:08 -0700363 jpeg_finish_decompress(&dinfo);
364 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800365
366 // WebP conversion.
367 pic->width = width;
368 pic->height = height;
369 ok = WebPPictureImportRGB(pic, rgb, stride);
370
371 End:
372 if (rgb) {
373 free(rgb);
374 }
375 return ok;
376}
377
378#else
379static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400380 (void)in_file;
381 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800382 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
383 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800384 return 0;
385}
386#endif
387
James Zern6f76d242012-07-01 17:55:21 -0700388#ifdef WEBP_HAVE_TIFF
389static int ReadTIFF(const char* const filename,
390 WebPPicture* const pic, int keep_alpha) {
391 TIFF* const tif = TIFFOpen(filename, "r");
392 uint32 width, height;
393 uint32* raster;
394 int ok = 0;
395 int dircount = 1;
396
397 if (tif == NULL) {
398 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
399 return 0;
400 }
401
402 while (TIFFReadDirectory(tif)) ++dircount;
403
404 if (dircount > 1) {
405 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
406 "Only the first will be used, %d will be ignored.\n",
407 dircount - 1);
408 }
409
410 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
411 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
412 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
413 if (raster != NULL) {
414 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
415 ORIENTATION_TOPLEFT, 1)) {
416 const int stride = width * sizeof(*raster);
417 pic->width = width;
418 pic->height = height;
419 // TIFF data is ABGR
420#ifdef __BIG_ENDIAN__
421 TIFFSwabArrayOfLong(raster, width * height);
422#endif
423 ok = keep_alpha
424 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
425 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
426 }
427 _TIFFfree(raster);
428 } else {
429 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
430 }
431
432 if (ok && keep_alpha == 2) {
433 WebPCleanupTransparentArea(pic);
434 }
435
436 TIFFClose(tif);
437 return ok;
438}
439#else
440static int ReadTIFF(const char* const filename,
441 WebPPicture* const pic, int keep_alpha) {
442 (void)filename;
443 (void)pic;
444 (void)keep_alpha;
445 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
446 "development package before building.\n");
447 return 0;
448}
449#endif
450
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800451typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700452 PNG_ = 0,
453 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700454 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800455 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800456} InputFileFormat;
457
458static InputFileFormat GetImageType(FILE* in_file) {
459 InputFileFormat format = UNSUPPORTED;
460 unsigned int magic;
461 unsigned char buf[4];
462
463 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
464 (fseek(in_file, 0, SEEK_SET) != 0)) {
465 return format;
466 }
467
468 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
469 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700470 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800471 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700472 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700473 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
474 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800475 }
476 return format;
477}
478
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700479static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800480 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800481 int ok = 0;
482 FILE* in_file = fopen(filename, "rb");
James Zern63aba3a2012-12-03 18:20:00 -0800483 (void)metadata; // TODO(jzern): add metadata extraction to the formats below.
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800484 if (in_file == NULL) {
485 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
486 return ok;
487 }
488
489 if (pic->width == 0 || pic->height == 0) {
490 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
491 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700492 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700493 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700494 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800495 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700496 } else if (format == TIFF_) {
497 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800498 }
499 } else {
500 // If image size is specified, infer it as YUV format.
501 ok = ReadYUV(in_file, pic);
502 }
503 if (!ok) {
504 fprintf(stderr, "Error! Could not process file %s\n", filename);
505 }
506
507 fclose(in_file);
508 return ok;
509}
510
James Zern31f9dc62011-06-06 17:56:50 -0700511#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800512
513static void AllocExtraInfo(WebPPicture* const pic) {
514 const int mb_w = (pic->width + 15) / 16;
515 const int mb_h = (pic->height + 15) / 16;
516 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
517}
518
519static void PrintByteCount(const int bytes[4], int total_size,
520 int* const totals) {
521 int s;
522 int total = 0;
523 for (s = 0; s < 4; ++s) {
524 fprintf(stderr, "| %7d ", bytes[s]);
525 total += bytes[s];
526 if (totals) totals[s] += bytes[s];
527 }
James Zern04e84cf2011-11-04 15:20:08 -0700528 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800529}
530
531static void PrintPercents(const int counts[4], int total) {
532 int s;
533 for (s = 0; s < 4; ++s) {
534 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
535 }
James Zern04e84cf2011-11-04 15:20:08 -0700536 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800537}
538
539static void PrintValues(const int values[4]) {
540 int s;
541 for (s = 0; s < 4; ++s) {
542 fprintf(stderr, "| %7d ", values[s]);
543 }
James Zern04e84cf2011-11-04 15:20:08 -0700544 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800545}
546
Pascal Massimino7d853d72012-07-24 16:15:36 -0700547static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
548 const char* const description) {
549 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
550 description, stats->lossless_size);
551 if (stats->lossless_features) {
552 fprintf(stderr, " * Lossless features used:");
553 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
554 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
555 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
556 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
557 fprintf(stderr, "\n");
558 }
559 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
560 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
561 if (stats->palette_size > 0) {
562 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
563 }
564}
565
Vikas Arorac4ccab62012-05-09 11:27:46 +0530566static void PrintExtraInfoLossless(const WebPPicture* const pic,
567 int short_output,
568 const char* const file_name) {
569 const WebPAuxStats* const stats = pic->stats;
570 if (short_output) {
571 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
572 } else {
573 fprintf(stderr, "File: %s\n", file_name);
574 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
575 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700576 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530577 }
578}
579
580static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
581 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800582 const WebPAuxStats* const stats = pic->stats;
583 if (short_output) {
584 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700585 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800586 const int num_i4 = stats->block_count[0];
587 const int num_i16 = stats->block_count[1];
588 const int num_skip = stats->block_count[2];
589 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800590 fprintf(stderr, "File: %s\n", file_name);
591 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700592 pic->width, pic->height,
593 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800594 fprintf(stderr, "Output: "
595 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800596 stats->coded_size,
597 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
598 if (total > 0) {
599 int totals[4] = { 0, 0, 0, 0 };
600 fprintf(stderr, "block count: intra4: %d\n"
601 " intra16: %d (-> %.2f%%)\n",
602 num_i4, num_i16, 100.f * num_i16 / total);
603 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
604 num_skip, 100.f * num_skip / total);
605 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
606 " mode-partition: %6d (%.1f%%)\n",
607 stats->header_bytes[0],
608 100.f * stats->header_bytes[0] / stats->coded_size,
609 stats->header_bytes[1],
610 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700611 if (stats->alpha_data_size > 0) {
612 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
613 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700614 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700615 if (stats->layer_data_size) {
616 fprintf(stderr, " enhancement: %6d\n",
617 stats->layer_data_size);
618 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800619 fprintf(stderr, " Residuals bytes "
620 "|segment 1|segment 2|segment 3"
621 "|segment 4| total\n");
622 fprintf(stderr, " intra4-coeffs: ");
623 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
624 fprintf(stderr, " intra16-coeffs: ");
625 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
626 fprintf(stderr, " chroma coeffs: ");
627 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
628 fprintf(stderr, " macroblocks: ");
629 PrintPercents(stats->segment_size, total);
630 fprintf(stderr, " quantizer: ");
631 PrintValues(stats->segment_quant);
632 fprintf(stderr, " filter level: ");
633 PrintValues(stats->segment_level);
634 fprintf(stderr, "------------------+---------");
635 fprintf(stderr, "+---------+---------+---------+-----------------\n");
636 fprintf(stderr, " segments total: ");
637 PrintByteCount(totals, stats->coded_size, NULL);
638 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700639 if (stats->lossless_size > 0) {
640 PrintFullLosslessInfo(stats, "alpha");
641 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800642 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700643 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 const int mb_w = (pic->width + 15) / 16;
645 const int mb_h = (pic->height + 15) / 16;
646 const int type = pic->extra_info_type;
647 int x, y;
648 for (y = 0; y < mb_h; ++y) {
649 for (x = 0; x < mb_w; ++x) {
650 const int c = pic->extra_info[x + y * mb_w];
651 if (type == 1) { // intra4/intra16
652 printf("%c", "+."[c]);
653 } else if (type == 2) { // segments
654 printf("%c", ".-*X"[c]);
655 } else if (type == 3) { // quantizers
656 printf("%.2d ", c);
657 } else if (type == 6 || type == 7) {
658 printf("%3d ", c);
659 } else {
660 printf("0x%.2x ", c);
661 }
662 }
663 printf("\n");
664 }
665 }
666}
667
James Zernc7e86ab2011-08-25 14:22:32 -0700668//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669
670static int MyWriter(const uint8_t* data, size_t data_size,
671 const WebPPicture* const pic) {
672 FILE* const out = (FILE*)pic->custom_ptr;
673 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
674}
675
676// Dumps a picture as a PGM file using the IMC4 layout.
677static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
678 int y;
679 const int uv_width = (picture->width + 1) / 2;
680 const int uv_height = (picture->height + 1) / 2;
681 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700682 const int alpha_height =
683 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700684 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800685 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800686 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800687 fprintf(f, "P5\n%d %d\n255\n", stride, height);
688 for (y = 0; y < picture->height; ++y) {
689 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
690 return 0;
691 if (picture->width & 1) fputc(0, f); // pad
692 }
693 for (y = 0; y < uv_height; ++y) {
694 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
695 return 0;
696 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
697 return 0;
698 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700699 for (y = 0; y < alpha_height; ++y) {
700 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
701 return 0;
702 if (picture->width & 1) fputc(0, f); // pad
703 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800704 fclose(f);
705 return 1;
706}
707
James Zernc7e86ab2011-08-25 14:22:32 -0700708//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800709
Pascal Massimino30971c92011-12-01 02:24:50 -0800710static int ProgressReport(int percent, const WebPPicture* const picture) {
711 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700712 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800713 fflush(stdout);
714 return 1; // all ok
715}
716
717//------------------------------------------------------------------------------
718
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700719static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800720 printf("Usage:\n\n");
721 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
722 printf("where quality is between 0 (poor) to 100 (very good).\n");
723 printf("Typical value is around 80.\n\n");
724 printf("Try -longhelp for an exhaustive list of advanced options.\n");
725}
726
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700727static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800728 printf("Usage:\n");
729 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
730 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700731 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700732#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800733 printf("Windows builds can take as input any of the files handled by WIC\n");
734#endif
735 printf("options:\n");
736 printf(" -h / -help ............ short help\n");
737 printf(" -H / -longhelp ........ long help\n");
738 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530739 printf(" -alpha_q <int> ......... Transparency-compression quality "
740 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800741 printf(" -preset <string> ....... Preset setting, one of:\n");
742 printf(" default, photo, picture,\n");
743 printf(" drawing, icon, text\n");
744 printf(" -preset must come first, as it overwrites other parameters.");
745 printf("\n");
746 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
747 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700748 printf(" -size <int> ............ Target size (in bytes)\n");
749 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800750 printf("\n");
751 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
752 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
753 printf(" -f <int> ............... filter strength (0=off..100)\n");
754 printf(" -sharpness <int> ....... "
755 "filter sharpness (0:most .. 7:least sharp)\n");
756 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700757 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
758 printf(" "
759 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800760 printf(" -pass <int> ............ analysis pass number (1..10)\n");
761 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700762 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
763#ifdef WEBP_EXPERIMENTAL_FEATURES
764 printf(" -444 / -422 / -gray ..... Change colorspace\n");
765#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800766 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800767 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700768 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
769 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800770 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530771 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800772 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
773 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000774 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530775 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530776 printf(" -lossless .............. Encode image losslessly.\n");
777 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700778 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700779
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800780 printf("\n");
781 printf(" -short ................. condense printed message\n");
782 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700783 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700784#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700785 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700786#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 printf(" -v ..................... verbose, e.g. print encoding/decoding "
788 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800789 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800790 printf("\n");
791 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800792 printf(" -af .................... auto-adjust filter strength.\n");
793 printf(" -pre <int> ............. pre-processing filter\n");
794 printf("\n");
795}
796
James Zernc7e86ab2011-08-25 14:22:32 -0700797//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700798// Error messages
799
800static const char* const kErrorMessages[] = {
801 "OK",
802 "OUT_OF_MEMORY: Out of memory allocating objects",
803 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
804 "NULL_PARAMETER: NULL parameter passed to function",
805 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700806 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
807 "allowed is 16383 pixels.",
808 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
809 "To reduce the size of this partition, try using less segments "
810 "with the -segments option, and eventually reduce the number of "
811 "header bits using -partition_limit. More details are available "
812 "in the manual (`man cwebp`)",
813 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800814 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800815 "FILE_TOO_BIG: File would be too big to fit in 4G",
816 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700817};
818
James Zernc7e86ab2011-08-25 14:22:32 -0700819//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800820
821int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700822 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
824 FILE *out = NULL;
825 int c;
826 int short_output = 0;
827 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530828 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800829 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700830 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800831 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800832 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700833 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800834 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 WebPConfig config;
836 WebPAuxStats stats;
James Zern63aba3a2012-12-03 18:20:00 -0800837 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800838 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700839
James Zern63aba3a2012-12-03 18:20:00 -0800840 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800841 if (!WebPPictureInit(&picture) ||
842 !WebPPictureInit(&original_picture) ||
843 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800844 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700845 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800846 }
847
848 if (argc == 1) {
849 HelpShort();
850 return 0;
851 }
852
853 for (c = 1; c < argc; ++c) {
854 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
855 HelpShort();
856 return 0;
857 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
858 HelpLong();
859 return 0;
860 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
861 out_file = argv[++c];
862 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
863 dump_file = argv[++c];
864 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800865 } else if (!strcmp(argv[c], "-print_psnr")) {
866 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700867 print_distortion = 0;
868 } else if (!strcmp(argv[c], "-print_ssim")) {
869 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800870 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700871 } else if (!strcmp(argv[c], "-print_lsim")) {
872 config.show_compressed = 1;
873 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800874 } else if (!strcmp(argv[c], "-short")) {
875 short_output++;
876 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700877 picture.width = strtol(argv[++c], NULL, 0);
878 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800879 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700880 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800881 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200882 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530883 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
884 config.alpha_quality = strtol(argv[++c], NULL, 0);
885 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
886 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000887 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
888 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530889 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800890 ++c;
891 if (!strcmp(argv[c], "none")) {
892 config.alpha_filtering = 0;
893 } else if (!strcmp(argv[c], "fast")) {
894 config.alpha_filtering = 1;
895 } else if (!strcmp(argv[c], "best")) {
896 config.alpha_filtering = 2;
897 } else {
898 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
899 goto Error;
900 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530901 } else if (!strcmp(argv[c], "-noalpha")) {
902 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000903 } else if (!strcmp(argv[c], "-lossless")) {
904 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000905 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530906 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
907 ++c;
908 if (!strcmp(argv[c], "photo")) {
909 config.image_hint = WEBP_HINT_PHOTO;
910 } else if (!strcmp(argv[c], "picture")) {
911 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700912 } else if (!strcmp(argv[c], "graph")) {
913 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530914 } else {
915 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
916 goto Error;
917 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800918 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700919 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800920 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200921 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800922 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700923 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800924 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700925 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800926 } else if (!strcmp(argv[c], "-af")) {
927 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700928 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800929 config.filter_type = 1;
930 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700931 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800932 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700933 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800934 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700935 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800936 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700937 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700938 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
939 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800940 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700941 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700942#ifdef WEBP_EXPERIMENTAL_FEATURES
943 } else if (!strcmp(argv[c], "-444")) {
944 picture.colorspace = WEBP_YUV444;
945 } else if (!strcmp(argv[c], "-422")) {
946 picture.colorspace = WEBP_YUV422;
947 } else if (!strcmp(argv[c], "-gray")) {
948 picture.colorspace = WEBP_YUV400;
949#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800950 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
951 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700952 crop_x = strtol(argv[++c], NULL, 0);
953 crop_y = strtol(argv[++c], NULL, 0);
954 crop_w = strtol(argv[++c], NULL, 0);
955 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700956 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
957 resize_w = strtol(argv[++c], NULL, 0);
958 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700959#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700960 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000961 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700962#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700963 } else if (!strcmp(argv[c], "-version")) {
964 const int version = WebPGetEncoderVersion();
965 printf("%d.%d.%d\n",
966 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
967 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800968 } else if (!strcmp(argv[c], "-progress")) {
969 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800970 } else if (!strcmp(argv[c], "-quiet")) {
971 quiet = 1;
972 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
973 WebPPreset preset;
974 ++c;
975 if (!strcmp(argv[c], "default")) {
976 preset = WEBP_PRESET_DEFAULT;
977 } else if (!strcmp(argv[c], "photo")) {
978 preset = WEBP_PRESET_PHOTO;
979 } else if (!strcmp(argv[c], "picture")) {
980 preset = WEBP_PRESET_PICTURE;
981 } else if (!strcmp(argv[c], "drawing")) {
982 preset = WEBP_PRESET_DRAWING;
983 } else if (!strcmp(argv[c], "icon")) {
984 preset = WEBP_PRESET_ICON;
985 } else if (!strcmp(argv[c], "text")) {
986 preset = WEBP_PRESET_TEXT;
987 } else {
988 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
989 goto Error;
990 }
991 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700992 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800993 goto Error;
994 }
995 } else if (!strcmp(argv[c], "-v")) {
996 verbose = 1;
997 } else if (argv[c][0] == '-') {
998 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
999 HelpLong();
1000 return -1;
1001 } else {
1002 in_file = argv[c];
1003 }
1004 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001005 if (in_file == NULL) {
1006 fprintf(stderr, "No input file specified!\n");
1007 HelpShort();
1008 goto Error;
1009 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001010
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301011 // Check for unsupported command line options for lossless mode and log
1012 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001013 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301014 if (config.target_size > 0 || config.target_PSNR > 0) {
1015 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1016 " for lossless encoding. Ignoring such option(s)!\n");
1017 }
1018 if (config.partition_limit > 0) {
1019 fprintf(stderr, "Partition limit option is not required for lossless"
1020 " encoding. Ignoring this option!\n");
1021 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301022 }
1023
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001024 if (!WebPValidateConfig(&config)) {
1025 fprintf(stderr, "Error! Invalid configuration.\n");
1026 goto Error;
1027 }
1028
Pascal Massimino0744e842011-02-25 12:03:27 -08001029 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001030 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001031 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001032 }
James Zern63aba3a2012-12-03 18:20:00 -08001033 if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001034 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001035 goto Error;
1036 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001037 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1038
Pascal Massimino0744e842011-02-25 12:03:27 -08001039 if (verbose) {
1040 const double time = StopwatchReadAndReset(&stop_watch);
1041 fprintf(stderr, "Time to read input: %.3fs\n", time);
1042 }
1043
1044 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001045 if (out_file) {
1046 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001047 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001048 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1049 goto Error;
1050 } else {
1051 if (!short_output && !quiet) {
1052 fprintf(stderr, "Saving file '%s'\n", out_file);
1053 }
1054 }
1055 picture.writer = MyWriter;
1056 picture.custom_ptr = (void*)out;
1057 } else {
1058 out = NULL;
1059 if (!quiet && !short_output) {
1060 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1061 fprintf(stderr, "be performed, but its results discarded.\n\n");
1062 }
1063 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001064 if (!quiet) {
1065 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001066 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001067 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001068
Pascal Massimino0744e842011-02-25 12:03:27 -08001069 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001070 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001071 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001072 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001073 if (crop != 0) {
1074 // We use self-cropping using a view.
1075 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1076 fprintf(stderr, "Error! Cannot crop picture\n");
1077 goto Error;
1078 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001079 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001080 if ((resize_w | resize_h) > 0) {
1081 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1082 fprintf(stderr, "Error! Cannot resize picture\n");
1083 goto Error;
1084 }
1085 }
1086 if (picture.extra_info_type > 0) {
1087 AllocExtraInfo(&picture);
1088 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001089 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001090 WebPPictureCopy(&picture, &original_picture);
1091 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001092 if (!WebPEncode(&config, &picture)) {
1093 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001094 fprintf(stderr, "Error code: %d (%s)\n",
1095 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001096 goto Error;
1097 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001098 if (verbose) {
1099 const double time = StopwatchReadAndReset(&stop_watch);
1100 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1101 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001102
1103 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001104 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001105 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001106 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1107 } else if (!DumpPicture(&picture, dump_file)) {
1108 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1109 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001110 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001111
Pascal Massimino38369c02011-05-04 22:59:51 -07001112 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301113 if (config.lossless) {
1114 PrintExtraInfoLossless(&picture, short_output, in_file);
1115 } else {
1116 PrintExtraInfoLossy(&picture, short_output, in_file);
1117 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001118 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001119 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
1120 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -08001121 float values[5];
1122 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001123 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001124 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001125 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -08001126 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001127 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001128 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001129
1130 Error:
1131 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001132 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001133 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001134 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001135 if (out != NULL) {
1136 fclose(out);
1137 }
1138
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001139 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001140}
1141
James Zernc7e86ab2011-08-25 14:22:32 -07001142//------------------------------------------------------------------------------