blob: 07616895f09f7dfd6ffe22df1164ce313f9a5e2d [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_PNG
22#include <png.h>
23#endif
24
25#ifdef WEBP_HAVE_JPEG
26#include <setjmp.h> // note: this must be included *after* png.h
27#include <jpeglib.h>
28#endif
29
James Zern6f76d242012-07-01 17:55:21 -070030#ifdef WEBP_HAVE_TIFF
31#include <tiffio.h>
32#endif
33
James Zern31f9dc62011-06-06 17:56:50 -070034#ifdef HAVE_WINCODEC_H
35#ifdef __MINGW32__
36#define INITGUID // Without this GUIDs are declared extern and fail to link
37#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080038#define CINTERFACE
39#define COBJMACROS
40#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
41 // code with COBJMACROS.
42#include <shlwapi.h>
43#include <windows.h>
44#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070045#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046
47
48#include "webp/encode.h"
James Zern00b29e22012-05-15 13:48:11 -070049#include "./stopwatch.h"
James Zernb4d0ef82011-07-15 14:53:03 -070050#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070051#if defined(__cplusplus) || defined(c_plusplus)
52extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070053#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070054
James Zern04e84cf2011-11-04 15:20:08 -070055extern void* VP8GetCPUInfo; // opaque forward declaration.
56
57#if defined(__cplusplus) || defined(c_plusplus)
58} // extern "C"
59#endif
60#endif // WEBP_DLL
61
James Zernc7e86ab2011-08-25 14:22:32 -070062//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080063
64static int verbose = 0;
65
66static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000067 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080068 const int uv_width = (pic->width + 1) / 2;
69 const int uv_height = (pic->height + 1) / 2;
70 int y;
71 int ok = 0;
72
Pascal Massiminodd108172012-07-18 21:58:53 +000073 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080074 if (!WebPPictureAlloc(pic)) return ok;
75
76 for (y = 0; y < pic->height; ++y) {
77 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
78 goto End;
79 }
80 }
81 for (y = 0; y < uv_height; ++y) {
82 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
83 goto End;
84 }
85 for (y = 0; y < uv_height; ++y) {
86 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
87 goto End;
88 }
89 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000090 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080091
92 End:
93 return ok;
94}
95
James Zern31f9dc62011-06-06 17:56:50 -070096#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080097
James Zernf2b5d192012-10-08 18:33:18 -070098#define IFS(fn) \
99 do { \
100 if (SUCCEEDED(hr)) { \
101 hr = (fn); \
102 if (FAILED(hr)) fprintf(stderr, #fn " failed %08x\n", hr); \
103 } \
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800104 } while (0)
105
James Zern902d3e32012-05-08 17:49:39 -0700106// modified version of DEFINE_GUID from guiddef.h.
107#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
108 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
109
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800110#ifdef __cplusplus
111#define MAKE_REFGUID(x) (x)
112#else
113#define MAKE_REFGUID(x) &(x)
114#endif
115
James Zerneda8ee42012-10-19 18:34:06 -0700116typedef struct WICFormatImporter {
117 const GUID* pixel_format;
118 int bytes_per_pixel;
119 int (*import)(WebPPicture* const, const uint8_t* const, int);
120} WICFormatImporter;
121
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800122static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
123 HRESULT hr = S_OK;
124 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
125 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800126 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800127 return hr;
128}
129
130static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700131 WebPPicture* const pic, int keep_alpha) {
James Zerneda8ee42012-10-19 18:34:06 -0700132 // From Microsoft SDK 7.0a -- wincodec.h
133 // Create local copies for compatibility when building against earlier
134 // versions of the SDK.
135 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_,
136 0x6fddc324, 0x4e03, 0x4bfe,
137 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c);
138 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
139 0x6fddc324, 0x4e03, 0x4bfe,
140 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
141 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
142 0x6fddc324, 0x4e03, 0x4bfe,
143 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
144 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
145 0xf5c7ad2d, 0x6a8d, 0x43dd,
146 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
147 const WICFormatImporter alphaFormatImporters[] = {
148 { &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
149 { &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
150 { NULL, 0, NULL },
151 };
152 const WICFormatImporter nonAlphaFormatImporters[] = {
153 { &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
154 { &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
155 { NULL, 0, NULL },
156 };
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800157 HRESULT hr = S_OK;
158 IWICBitmapFrameDecode* pFrame = NULL;
159 IWICFormatConverter* pConverter = NULL;
160 IWICImagingFactory* pFactory = NULL;
161 IWICBitmapDecoder* pDecoder = NULL;
162 IStream* pStream = NULL;
163 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700164 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800165 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700166 WICPixelFormatGUID srcPixelFormat = { 0 };
James Zerneda8ee42012-10-19 18:34:06 -0700167 const WICFormatImporter* importer = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700168 GUID srcContainerFormat = { 0 };
169 const GUID* alphaContainers[] = {
170 &GUID_ContainerFormatBmp,
171 &GUID_ContainerFormatPng,
172 &GUID_ContainerFormatTiff
173 };
174 int has_alpha = 0;
175 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800176
177 IFS(CoInitialize(NULL));
178 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
179 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
180 (LPVOID*)&pFactory));
181 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800182 fprintf(stderr,
183 "Couldn't access Windows Imaging Component (are you running "
184 "Windows XP SP3 or newer?). Most formats not available. "
185 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800186 }
187 // Prepare for image decoding.
188 IFS(OpenInputStream(filename, &pStream));
189 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
190 WICDecodeMetadataCacheOnDemand, &pDecoder));
191 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
192 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800193 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800194 hr = E_FAIL;
195 }
196 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700197 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
198 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
199
James Zernecd66f72012-10-08 18:15:30 -0700200 if (keep_alpha) {
201 for (i = 0;
202 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
203 ++i) {
204 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
205 MAKE_REFGUID(*alphaContainers[i]))) {
206 has_alpha =
207 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
208 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
209 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
210 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
211 break;
212 }
James Zern3cfe0882011-06-21 18:29:30 -0700213 }
214 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800215
216 // Prepare for pixel format conversion (if necessary).
217 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
James Zerneda8ee42012-10-19 18:34:06 -0700218
219 for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters;
220 hr == S_OK && importer->import != NULL; ++importer) {
221 BOOL canConvert;
222 const HRESULT cchr = IWICFormatConverter_CanConvert(
223 pConverter,
224 MAKE_REFGUID(srcPixelFormat),
225 MAKE_REFGUID(*importer->pixel_format),
226 &canConvert);
227 if (SUCCEEDED(cchr) && canConvert) break;
228 }
229 if (importer->import == NULL) hr = E_FAIL;
230
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800231 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zerneda8ee42012-10-19 18:34:06 -0700232 importer->pixel_format,
James Zern3cfe0882011-06-21 18:29:30 -0700233 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800234 NULL, 0.0, WICBitmapPaletteTypeCustom));
235
236 // Decode.
237 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zerneda8ee42012-10-19 18:34:06 -0700238 stride = importer->bytes_per_pixel * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800239 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700240 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800241 if (rgb == NULL)
242 hr = E_OUTOFMEMORY;
243 }
James Zern3cfe0882011-06-21 18:29:30 -0700244 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
245 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800246
247 // WebP conversion.
248 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700249 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800250 pic->width = width;
251 pic->height = height;
James Zerneda8ee42012-10-19 18:34:06 -0700252 ok = importer->import(pic, rgb, stride);
James Zern3cfe0882011-06-21 18:29:30 -0700253 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800254 hr = E_FAIL;
255 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000256 if (SUCCEEDED(hr)) {
257 if (has_alpha && keep_alpha == 2) {
258 WebPCleanupTransparentArea(pic);
259 }
260 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800261
262 // Cleanup.
263 if (pConverter != NULL) IUnknown_Release(pConverter);
264 if (pFrame != NULL) IUnknown_Release(pFrame);
265 if (pDecoder != NULL) IUnknown_Release(pDecoder);
266 if (pFactory != NULL) IUnknown_Release(pFactory);
267 if (pStream != NULL) IUnknown_Release(pStream);
268 free(rgb);
269 return hr;
270}
271
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700272static int ReadPicture(const char* const filename, WebPPicture* const pic,
273 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800274 int ok;
275 if (pic->width != 0 && pic->height != 0) {
276 // If image size is specified, infer it as YUV format.
277 FILE* in_file = fopen(filename, "rb");
278 if (in_file == NULL) {
279 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
280 return 0;
281 }
282 ok = ReadYUV(in_file, pic);
283 fclose(in_file);
284 } else {
285 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700286 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800287 }
288 if (!ok) {
289 fprintf(stderr, "Error! Could not process file %s\n", filename);
290 }
291 return ok;
292}
293
James Zern31f9dc62011-06-06 17:56:50 -0700294#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800295
296#ifdef WEBP_HAVE_JPEG
297struct my_error_mgr {
298 struct jpeg_error_mgr pub;
299 jmp_buf setjmp_buffer;
300};
301
302static void my_error_exit(j_common_ptr dinfo) {
303 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
304 (*dinfo->err->output_message) (dinfo);
305 longjmp(myerr->setjmp_buffer, 1);
306}
307
308static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
309 int ok = 0;
310 int stride, width, height;
311 uint8_t* rgb = NULL;
312 uint8_t* row_ptr = NULL;
313 struct jpeg_decompress_struct dinfo;
314 struct my_error_mgr jerr;
315 JSAMPARRAY buffer;
316
317 dinfo.err = jpeg_std_error(&jerr.pub);
318 jerr.pub.error_exit = my_error_exit;
319
James Zern04e84cf2011-11-04 15:20:08 -0700320 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800321 Error:
322 jpeg_destroy_decompress(&dinfo);
323 goto End;
324 }
325
326 jpeg_create_decompress(&dinfo);
327 jpeg_stdio_src(&dinfo, in_file);
328 jpeg_read_header(&dinfo, TRUE);
329
330 dinfo.out_color_space = JCS_RGB;
331 dinfo.dct_method = JDCT_IFAST;
332 dinfo.do_fancy_upsampling = TRUE;
333
334 jpeg_start_decompress(&dinfo);
335
336 if (dinfo.output_components != 3) {
337 goto Error;
338 }
339
340 width = dinfo.output_width;
341 height = dinfo.output_height;
342 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
343
344 rgb = (uint8_t*)malloc(stride * height);
345 if (rgb == NULL) {
346 goto End;
347 }
348 row_ptr = rgb;
349
350 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
351 JPOOL_IMAGE, stride, 1);
352 if (buffer == NULL) {
353 goto End;
354 }
355
356 while (dinfo.output_scanline < dinfo.output_height) {
357 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
358 goto End;
359 }
360 memcpy(row_ptr, buffer[0], stride);
361 row_ptr += stride;
362 }
363
James Zern04e84cf2011-11-04 15:20:08 -0700364 jpeg_finish_decompress(&dinfo);
365 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800366
367 // WebP conversion.
368 pic->width = width;
369 pic->height = height;
370 ok = WebPPictureImportRGB(pic, rgb, stride);
371
372 End:
373 if (rgb) {
374 free(rgb);
375 }
376 return ok;
377}
378
379#else
380static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400381 (void)in_file;
382 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800383 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
384 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800385 return 0;
386}
387#endif
388
389#ifdef WEBP_HAVE_PNG
390static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700391 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800392 longjmp(png_jmpbuf(png), 1);
393}
394
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700395static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800396 png_structp png;
397 png_infop info;
398 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700399 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800400 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700401 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800402 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700403 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800404 int stride;
405 uint8_t* rgb = NULL;
406
407 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
408 if (png == NULL) {
409 goto End;
410 }
411
412 png_set_error_fn(png, 0, error_function, NULL);
413 if (setjmp(png_jmpbuf(png))) {
414 Error:
415 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700416 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800417 goto End;
418 }
419
420 info = png_create_info_struct(png);
421 if (info == NULL) goto Error;
422
423 png_init_io(png, in_file);
424 png_read_info(png, info);
425 if (!png_get_IHDR(png, info,
426 &width, &height, &bit_depth, &color_type, &interlaced,
427 NULL, NULL)) goto Error;
428
429 png_set_strip_16(png);
430 png_set_packing(png);
431 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000432 if (color_type == PNG_COLOR_TYPE_GRAY ||
433 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800434 if (bit_depth < 8) {
435 png_set_expand_gray_1_2_4_to_8(png);
436 }
437 png_set_gray_to_rgb(png);
438 }
439 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
440 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700441 has_alpha = 1;
442 } else {
443 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800444 }
445
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700446 if (!keep_alpha) {
447 png_set_strip_alpha(png);
448 has_alpha = 0;
449 }
450
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800451 num_passes = png_set_interlace_handling(png);
452 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700453 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800454 rgb = (uint8_t*)malloc(stride * height);
455 if (rgb == NULL) goto Error;
456 for (p = 0; p < num_passes; ++p) {
457 for (y = 0; y < height; ++y) {
458 png_bytep row = rgb + y * stride;
459 png_read_rows(png, &row, NULL, 1);
460 }
461 }
462 png_read_end(png, info);
463 png_destroy_read_struct(&png, &info, NULL);
464
465 pic->width = width;
466 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700467 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
468 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800469 free(rgb);
470
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000471 if (ok && has_alpha && keep_alpha == 2) {
472 WebPCleanupTransparentArea(pic);
473 }
474
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800475 End:
476 return ok;
477}
478#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700479static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400480 (void)in_file;
481 (void)pic;
482 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800483 fprintf(stderr, "PNG support not compiled. Please install the libpng "
484 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800485 return 0;
486}
487#endif
488
James Zern6f76d242012-07-01 17:55:21 -0700489#ifdef WEBP_HAVE_TIFF
490static int ReadTIFF(const char* const filename,
491 WebPPicture* const pic, int keep_alpha) {
492 TIFF* const tif = TIFFOpen(filename, "r");
493 uint32 width, height;
494 uint32* raster;
495 int ok = 0;
496 int dircount = 1;
497
498 if (tif == NULL) {
499 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
500 return 0;
501 }
502
503 while (TIFFReadDirectory(tif)) ++dircount;
504
505 if (dircount > 1) {
506 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
507 "Only the first will be used, %d will be ignored.\n",
508 dircount - 1);
509 }
510
511 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
512 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
513 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
514 if (raster != NULL) {
515 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
516 ORIENTATION_TOPLEFT, 1)) {
517 const int stride = width * sizeof(*raster);
518 pic->width = width;
519 pic->height = height;
520 // TIFF data is ABGR
521#ifdef __BIG_ENDIAN__
522 TIFFSwabArrayOfLong(raster, width * height);
523#endif
524 ok = keep_alpha
525 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
526 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
527 }
528 _TIFFfree(raster);
529 } else {
530 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
531 }
532
533 if (ok && keep_alpha == 2) {
534 WebPCleanupTransparentArea(pic);
535 }
536
537 TIFFClose(tif);
538 return ok;
539}
540#else
541static int ReadTIFF(const char* const filename,
542 WebPPicture* const pic, int keep_alpha) {
543 (void)filename;
544 (void)pic;
545 (void)keep_alpha;
546 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
547 "development package before building.\n");
548 return 0;
549}
550#endif
551
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800552typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700553 PNG_ = 0,
554 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700555 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800556 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800557} InputFileFormat;
558
559static InputFileFormat GetImageType(FILE* in_file) {
560 InputFileFormat format = UNSUPPORTED;
561 unsigned int magic;
562 unsigned char buf[4];
563
564 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
565 (fseek(in_file, 0, SEEK_SET) != 0)) {
566 return format;
567 }
568
569 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
570 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700571 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800572 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700573 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700574 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
575 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800576 }
577 return format;
578}
579
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700580static int ReadPicture(const char* const filename, WebPPicture* const pic,
581 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800582 int ok = 0;
583 FILE* in_file = fopen(filename, "rb");
584 if (in_file == NULL) {
585 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
586 return ok;
587 }
588
589 if (pic->width == 0 || pic->height == 0) {
590 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
591 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700592 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700593 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700594 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800595 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700596 } else if (format == TIFF_) {
597 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800598 }
599 } else {
600 // If image size is specified, infer it as YUV format.
601 ok = ReadYUV(in_file, pic);
602 }
603 if (!ok) {
604 fprintf(stderr, "Error! Could not process file %s\n", filename);
605 }
606
607 fclose(in_file);
608 return ok;
609}
610
James Zern31f9dc62011-06-06 17:56:50 -0700611#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800612
613static void AllocExtraInfo(WebPPicture* const pic) {
614 const int mb_w = (pic->width + 15) / 16;
615 const int mb_h = (pic->height + 15) / 16;
616 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
617}
618
619static void PrintByteCount(const int bytes[4], int total_size,
620 int* const totals) {
621 int s;
622 int total = 0;
623 for (s = 0; s < 4; ++s) {
624 fprintf(stderr, "| %7d ", bytes[s]);
625 total += bytes[s];
626 if (totals) totals[s] += bytes[s];
627 }
James Zern04e84cf2011-11-04 15:20:08 -0700628 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800629}
630
631static void PrintPercents(const int counts[4], int total) {
632 int s;
633 for (s = 0; s < 4; ++s) {
634 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
635 }
James Zern04e84cf2011-11-04 15:20:08 -0700636 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800637}
638
639static void PrintValues(const int values[4]) {
640 int s;
641 for (s = 0; s < 4; ++s) {
642 fprintf(stderr, "| %7d ", values[s]);
643 }
James Zern04e84cf2011-11-04 15:20:08 -0700644 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800645}
646
Pascal Massimino7d853d72012-07-24 16:15:36 -0700647static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
648 const char* const description) {
649 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
650 description, stats->lossless_size);
651 if (stats->lossless_features) {
652 fprintf(stderr, " * Lossless features used:");
653 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
654 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
655 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
656 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
657 fprintf(stderr, "\n");
658 }
659 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
660 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
661 if (stats->palette_size > 0) {
662 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
663 }
664}
665
Vikas Arorac4ccab62012-05-09 11:27:46 +0530666static void PrintExtraInfoLossless(const WebPPicture* const pic,
667 int short_output,
668 const char* const file_name) {
669 const WebPAuxStats* const stats = pic->stats;
670 if (short_output) {
671 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
672 } else {
673 fprintf(stderr, "File: %s\n", file_name);
674 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
675 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700676 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530677 }
678}
679
680static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
681 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800682 const WebPAuxStats* const stats = pic->stats;
683 if (short_output) {
684 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700685 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800686 const int num_i4 = stats->block_count[0];
687 const int num_i16 = stats->block_count[1];
688 const int num_skip = stats->block_count[2];
689 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800690 fprintf(stderr, "File: %s\n", file_name);
691 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700692 pic->width, pic->height,
693 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800694 fprintf(stderr, "Output: "
695 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800696 stats->coded_size,
697 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
698 if (total > 0) {
699 int totals[4] = { 0, 0, 0, 0 };
700 fprintf(stderr, "block count: intra4: %d\n"
701 " intra16: %d (-> %.2f%%)\n",
702 num_i4, num_i16, 100.f * num_i16 / total);
703 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
704 num_skip, 100.f * num_skip / total);
705 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
706 " mode-partition: %6d (%.1f%%)\n",
707 stats->header_bytes[0],
708 100.f * stats->header_bytes[0] / stats->coded_size,
709 stats->header_bytes[1],
710 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700711 if (stats->alpha_data_size > 0) {
712 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
713 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700714 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700715 if (stats->layer_data_size) {
716 fprintf(stderr, " enhancement: %6d\n",
717 stats->layer_data_size);
718 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800719 fprintf(stderr, " Residuals bytes "
720 "|segment 1|segment 2|segment 3"
721 "|segment 4| total\n");
722 fprintf(stderr, " intra4-coeffs: ");
723 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
724 fprintf(stderr, " intra16-coeffs: ");
725 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
726 fprintf(stderr, " chroma coeffs: ");
727 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
728 fprintf(stderr, " macroblocks: ");
729 PrintPercents(stats->segment_size, total);
730 fprintf(stderr, " quantizer: ");
731 PrintValues(stats->segment_quant);
732 fprintf(stderr, " filter level: ");
733 PrintValues(stats->segment_level);
734 fprintf(stderr, "------------------+---------");
735 fprintf(stderr, "+---------+---------+---------+-----------------\n");
736 fprintf(stderr, " segments total: ");
737 PrintByteCount(totals, stats->coded_size, NULL);
738 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700739 if (stats->lossless_size > 0) {
740 PrintFullLosslessInfo(stats, "alpha");
741 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800742 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700743 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800744 const int mb_w = (pic->width + 15) / 16;
745 const int mb_h = (pic->height + 15) / 16;
746 const int type = pic->extra_info_type;
747 int x, y;
748 for (y = 0; y < mb_h; ++y) {
749 for (x = 0; x < mb_w; ++x) {
750 const int c = pic->extra_info[x + y * mb_w];
751 if (type == 1) { // intra4/intra16
752 printf("%c", "+."[c]);
753 } else if (type == 2) { // segments
754 printf("%c", ".-*X"[c]);
755 } else if (type == 3) { // quantizers
756 printf("%.2d ", c);
757 } else if (type == 6 || type == 7) {
758 printf("%3d ", c);
759 } else {
760 printf("0x%.2x ", c);
761 }
762 }
763 printf("\n");
764 }
765 }
766}
767
James Zernc7e86ab2011-08-25 14:22:32 -0700768//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800769
770static int MyWriter(const uint8_t* data, size_t data_size,
771 const WebPPicture* const pic) {
772 FILE* const out = (FILE*)pic->custom_ptr;
773 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
774}
775
776// Dumps a picture as a PGM file using the IMC4 layout.
777static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
778 int y;
779 const int uv_width = (picture->width + 1) / 2;
780 const int uv_height = (picture->height + 1) / 2;
781 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700782 const int alpha_height =
783 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700784 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800785 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800786 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 fprintf(f, "P5\n%d %d\n255\n", stride, height);
788 for (y = 0; y < picture->height; ++y) {
789 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
790 return 0;
791 if (picture->width & 1) fputc(0, f); // pad
792 }
793 for (y = 0; y < uv_height; ++y) {
794 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
795 return 0;
796 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
797 return 0;
798 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700799 for (y = 0; y < alpha_height; ++y) {
800 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
801 return 0;
802 if (picture->width & 1) fputc(0, f); // pad
803 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800804 fclose(f);
805 return 1;
806}
807
James Zernc7e86ab2011-08-25 14:22:32 -0700808//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800809
Pascal Massimino30971c92011-12-01 02:24:50 -0800810static int ProgressReport(int percent, const WebPPicture* const picture) {
811 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700812 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800813 fflush(stdout);
814 return 1; // all ok
815}
816
817//------------------------------------------------------------------------------
818
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700819static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800820 printf("Usage:\n\n");
821 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
822 printf("where quality is between 0 (poor) to 100 (very good).\n");
823 printf("Typical value is around 80.\n\n");
824 printf("Try -longhelp for an exhaustive list of advanced options.\n");
825}
826
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700827static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800828 printf("Usage:\n");
829 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
830 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700831 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700832#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800833 printf("Windows builds can take as input any of the files handled by WIC\n");
834#endif
835 printf("options:\n");
836 printf(" -h / -help ............ short help\n");
837 printf(" -H / -longhelp ........ long help\n");
838 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530839 printf(" -alpha_q <int> ......... Transparency-compression quality "
840 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800841 printf(" -preset <string> ....... Preset setting, one of:\n");
842 printf(" default, photo, picture,\n");
843 printf(" drawing, icon, text\n");
844 printf(" -preset must come first, as it overwrites other parameters.");
845 printf("\n");
846 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
847 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700848 printf(" -size <int> ............ Target size (in bytes)\n");
849 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800850 printf("\n");
851 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
852 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
853 printf(" -f <int> ............... filter strength (0=off..100)\n");
854 printf(" -sharpness <int> ....... "
855 "filter sharpness (0:most .. 7:least sharp)\n");
856 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700857 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
858 printf(" "
859 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800860 printf(" -pass <int> ............ analysis pass number (1..10)\n");
861 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700862 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
863#ifdef WEBP_EXPERIMENTAL_FEATURES
864 printf(" -444 / -422 / -gray ..... Change colorspace\n");
865#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800866 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800867 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
868 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800869 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530870 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800871 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
872 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000873 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530874 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530875 printf(" -lossless .............. Encode image losslessly.\n");
876 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700877 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700878
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800879 printf("\n");
880 printf(" -short ................. condense printed message\n");
881 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700882 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700883#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700884 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700885#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800886 printf(" -v ..................... verbose, e.g. print encoding/decoding "
887 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800888 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800889 printf("\n");
890 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800891 printf(" -af .................... auto-adjust filter strength.\n");
892 printf(" -pre <int> ............. pre-processing filter\n");
893 printf("\n");
894}
895
James Zernc7e86ab2011-08-25 14:22:32 -0700896//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700897// Error messages
898
899static const char* const kErrorMessages[] = {
900 "OK",
901 "OUT_OF_MEMORY: Out of memory allocating objects",
902 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
903 "NULL_PARAMETER: NULL parameter passed to function",
904 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700905 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
906 "allowed is 16383 pixels.",
907 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
908 "To reduce the size of this partition, try using less segments "
909 "with the -segments option, and eventually reduce the number of "
910 "header bits using -partition_limit. More details are available "
911 "in the manual (`man cwebp`)",
912 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800913 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800914 "FILE_TOO_BIG: File would be too big to fit in 4G",
915 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700916};
917
James Zernc7e86ab2011-08-25 14:22:32 -0700918//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800919
920int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700921 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800922 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
923 FILE *out = NULL;
924 int c;
925 int short_output = 0;
926 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530927 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800928 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700929 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800930 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800931 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800932 int print_distortion = 0; // 1=PSNR, 2=SSIM
933 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800934 WebPConfig config;
935 WebPAuxStats stats;
936 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700937
Pascal Massiminod61479f2012-01-20 07:20:56 -0800938 if (!WebPPictureInit(&picture) ||
939 !WebPPictureInit(&original_picture) ||
940 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800941 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700942 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800943 }
944
945 if (argc == 1) {
946 HelpShort();
947 return 0;
948 }
949
950 for (c = 1; c < argc; ++c) {
951 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
952 HelpShort();
953 return 0;
954 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
955 HelpLong();
956 return 0;
957 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
958 out_file = argv[++c];
959 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
960 dump_file = argv[++c];
961 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800962 } else if (!strcmp(argv[c], "-print_ssim")) {
963 config.show_compressed = 1;
964 print_distortion = 2;
965 } else if (!strcmp(argv[c], "-print_psnr")) {
966 config.show_compressed = 1;
967 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800968 } else if (!strcmp(argv[c], "-short")) {
969 short_output++;
970 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700971 picture.width = strtol(argv[++c], NULL, 0);
972 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800973 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700974 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800975 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200976 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530977 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
978 config.alpha_quality = strtol(argv[++c], NULL, 0);
979 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
980 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000981 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
982 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530983 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800984 ++c;
985 if (!strcmp(argv[c], "none")) {
986 config.alpha_filtering = 0;
987 } else if (!strcmp(argv[c], "fast")) {
988 config.alpha_filtering = 1;
989 } else if (!strcmp(argv[c], "best")) {
990 config.alpha_filtering = 2;
991 } else {
992 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
993 goto Error;
994 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530995 } else if (!strcmp(argv[c], "-noalpha")) {
996 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000997 } else if (!strcmp(argv[c], "-lossless")) {
998 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000999 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +05301000 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
1001 ++c;
1002 if (!strcmp(argv[c], "photo")) {
1003 config.image_hint = WEBP_HINT_PHOTO;
1004 } else if (!strcmp(argv[c], "picture")) {
1005 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -07001006 } else if (!strcmp(argv[c], "graph")) {
1007 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +05301008 } else {
1009 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
1010 goto Error;
1011 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001012 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001013 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001014 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +02001015 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001016 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001017 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001018 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001019 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001020 } else if (!strcmp(argv[c], "-af")) {
1021 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -07001022 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001023 config.filter_type = 1;
1024 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001025 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001026 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001027 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001028 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001029 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001030 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001031 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -07001032 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
1033 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001034 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001035 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001036#ifdef WEBP_EXPERIMENTAL_FEATURES
1037 } else if (!strcmp(argv[c], "-444")) {
1038 picture.colorspace = WEBP_YUV444;
1039 } else if (!strcmp(argv[c], "-422")) {
1040 picture.colorspace = WEBP_YUV422;
1041 } else if (!strcmp(argv[c], "-gray")) {
1042 picture.colorspace = WEBP_YUV400;
1043#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001044 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
1045 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001046 crop_x = strtol(argv[++c], NULL, 0);
1047 crop_y = strtol(argv[++c], NULL, 0);
1048 crop_w = strtol(argv[++c], NULL, 0);
1049 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001050 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
1051 resize_w = strtol(argv[++c], NULL, 0);
1052 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -07001053#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -07001054 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +00001055 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -07001056#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -07001057 } else if (!strcmp(argv[c], "-version")) {
1058 const int version = WebPGetEncoderVersion();
1059 printf("%d.%d.%d\n",
1060 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1061 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -08001062 } else if (!strcmp(argv[c], "-progress")) {
1063 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001064 } else if (!strcmp(argv[c], "-quiet")) {
1065 quiet = 1;
1066 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
1067 WebPPreset preset;
1068 ++c;
1069 if (!strcmp(argv[c], "default")) {
1070 preset = WEBP_PRESET_DEFAULT;
1071 } else if (!strcmp(argv[c], "photo")) {
1072 preset = WEBP_PRESET_PHOTO;
1073 } else if (!strcmp(argv[c], "picture")) {
1074 preset = WEBP_PRESET_PICTURE;
1075 } else if (!strcmp(argv[c], "drawing")) {
1076 preset = WEBP_PRESET_DRAWING;
1077 } else if (!strcmp(argv[c], "icon")) {
1078 preset = WEBP_PRESET_ICON;
1079 } else if (!strcmp(argv[c], "text")) {
1080 preset = WEBP_PRESET_TEXT;
1081 } else {
1082 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
1083 goto Error;
1084 }
1085 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -07001086 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001087 goto Error;
1088 }
1089 } else if (!strcmp(argv[c], "-v")) {
1090 verbose = 1;
1091 } else if (argv[c][0] == '-') {
1092 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1093 HelpLong();
1094 return -1;
1095 } else {
1096 in_file = argv[c];
1097 }
1098 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001099 if (in_file == NULL) {
1100 fprintf(stderr, "No input file specified!\n");
1101 HelpShort();
1102 goto Error;
1103 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001104
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301105 // Check for unsupported command line options for lossless mode and log
1106 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001107 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301108 if (config.target_size > 0 || config.target_PSNR > 0) {
1109 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1110 " for lossless encoding. Ignoring such option(s)!\n");
1111 }
1112 if (config.partition_limit > 0) {
1113 fprintf(stderr, "Partition limit option is not required for lossless"
1114 " encoding. Ignoring this option!\n");
1115 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301116 }
1117
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001118 if (!WebPValidateConfig(&config)) {
1119 fprintf(stderr, "Error! Invalid configuration.\n");
1120 goto Error;
1121 }
1122
Pascal Massimino0744e842011-02-25 12:03:27 -08001123 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001124 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001125 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001126 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -07001127 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001128 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001129 goto Error;
1130 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001131 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1132
Pascal Massimino0744e842011-02-25 12:03:27 -08001133 if (verbose) {
1134 const double time = StopwatchReadAndReset(&stop_watch);
1135 fprintf(stderr, "Time to read input: %.3fs\n", time);
1136 }
1137
1138 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001139 if (out_file) {
1140 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001141 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001142 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1143 goto Error;
1144 } else {
1145 if (!short_output && !quiet) {
1146 fprintf(stderr, "Saving file '%s'\n", out_file);
1147 }
1148 }
1149 picture.writer = MyWriter;
1150 picture.custom_ptr = (void*)out;
1151 } else {
1152 out = NULL;
1153 if (!quiet && !short_output) {
1154 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1155 fprintf(stderr, "be performed, but its results discarded.\n\n");
1156 }
1157 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001158 if (!quiet) {
1159 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001160 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001161 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001162
Pascal Massimino0744e842011-02-25 12:03:27 -08001163 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001164 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001165 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001166 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001167 if (crop != 0) {
1168 // We use self-cropping using a view.
1169 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1170 fprintf(stderr, "Error! Cannot crop picture\n");
1171 goto Error;
1172 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001173 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001174 if ((resize_w | resize_h) > 0) {
1175 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1176 fprintf(stderr, "Error! Cannot resize picture\n");
1177 goto Error;
1178 }
1179 }
1180 if (picture.extra_info_type > 0) {
1181 AllocExtraInfo(&picture);
1182 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001183 if (print_distortion > 0) { // Save original picture for later comparison
1184 WebPPictureCopy(&picture, &original_picture);
1185 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001186 if (!WebPEncode(&config, &picture)) {
1187 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001188 fprintf(stderr, "Error code: %d (%s)\n",
1189 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001190 goto Error;
1191 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001192 if (verbose) {
1193 const double time = StopwatchReadAndReset(&stop_watch);
1194 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1195 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001196
1197 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001198 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001199 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001200 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1201 } else if (!DumpPicture(&picture, dump_file)) {
1202 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1203 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001204 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001205
Pascal Massimino38369c02011-05-04 22:59:51 -07001206 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301207 if (config.lossless) {
1208 PrintExtraInfoLossless(&picture, short_output, in_file);
1209 } else {
1210 PrintExtraInfoLossy(&picture, short_output, in_file);
1211 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001212 }
1213 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1214 float values[5];
1215 WebPPictureDistortion(&picture, &original_picture,
1216 (print_distortion == 1) ? 0 : 1, values);
1217 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1218 (print_distortion == 1) ? "PSNR" : "SSIM",
1219 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001220 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001221 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001222
1223 Error:
1224 free(picture.extra_info);
1225 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001226 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001227 if (out != NULL) {
1228 fclose(out);
1229 }
1230
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001231 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001232}
1233
James Zernc7e86ab2011-08-25 14:22:32 -07001234//------------------------------------------------------------------------------