blob: 7b76c83fb4e070291aa430e1507468fe83e594ca [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
98#define IFS(fn) \
99 do { \
100 if (SUCCEEDED(hr)) \
101 { \
102 hr = (fn); \
103 if (FAILED(hr) && verbose) \
James Zern974aaff2012-01-24 12:46:46 -0800104 fprintf(stderr, #fn " failed %08x\n", hr); \
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800105 } \
106 } while (0)
107
James Zern902d3e32012-05-08 17:49:39 -0700108// modified version of DEFINE_GUID from guiddef.h.
109#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
110 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
111
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800112#ifdef __cplusplus
113#define MAKE_REFGUID(x) (x)
114#else
115#define MAKE_REFGUID(x) &(x)
116#endif
117
118static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
119 HRESULT hr = S_OK;
120 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
121 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800122 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800123 return hr;
124}
125
126static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700127 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800128 HRESULT hr = S_OK;
129 IWICBitmapFrameDecode* pFrame = NULL;
130 IWICFormatConverter* pConverter = NULL;
131 IWICImagingFactory* pFactory = NULL;
132 IWICBitmapDecoder* pDecoder = NULL;
133 IStream* pStream = NULL;
134 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700135 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800136 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700137 WICPixelFormatGUID srcPixelFormat = { 0 };
138 GUID srcContainerFormat = { 0 };
139 const GUID* alphaContainers[] = {
140 &GUID_ContainerFormatBmp,
141 &GUID_ContainerFormatPng,
142 &GUID_ContainerFormatTiff
143 };
144 int has_alpha = 0;
145 int i, stride;
James Zern902d3e32012-05-08 17:49:39 -0700146 // From Microsoft SDK 7.0a
147 // Create local copies for compatibility when building against earlier
148 // versions of the SDK.
149 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
150 0x6fddc324, 0x4e03, 0x4bfe,
151 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
152 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
153 0xf5c7ad2d, 0x6a8d, 0x43dd,
154 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
155 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
156 0x6fddc324, 0x4e03, 0x4bfe,
157 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800158
159 IFS(CoInitialize(NULL));
160 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
161 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
162 (LPVOID*)&pFactory));
163 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800164 fprintf(stderr,
165 "Couldn't access Windows Imaging Component (are you running "
166 "Windows XP SP3 or newer?). Most formats not available. "
167 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800168 }
169 // Prepare for image decoding.
170 IFS(OpenInputStream(filename, &pStream));
171 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
172 WICDecodeMetadataCacheOnDemand, &pDecoder));
173 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
174 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800175 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800176 hr = E_FAIL;
177 }
178 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700179 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
180 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
181
James Zernecd66f72012-10-08 18:15:30 -0700182 if (keep_alpha) {
183 for (i = 0;
184 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
185 ++i) {
186 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
187 MAKE_REFGUID(*alphaContainers[i]))) {
188 has_alpha =
189 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
190 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
191 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
192 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
193 break;
194 }
James Zern3cfe0882011-06-21 18:29:30 -0700195 }
196 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800197
198 // Prepare for pixel format conversion (if necessary).
199 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
200 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700201 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
202 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700203 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800204 NULL, 0.0, WICBitmapPaletteTypeCustom));
205
206 // Decode.
207 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700208 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800209 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700210 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800211 if (rgb == NULL)
212 hr = E_OUTOFMEMORY;
213 }
James Zern3cfe0882011-06-21 18:29:30 -0700214 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
215 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800216
217 // WebP conversion.
218 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700219 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800220 pic->width = width;
221 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700222 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
223 : WebPPictureImportRGB(pic, rgb, stride);
224 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800225 hr = E_FAIL;
226 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000227 if (SUCCEEDED(hr)) {
228 if (has_alpha && keep_alpha == 2) {
229 WebPCleanupTransparentArea(pic);
230 }
231 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800232
233 // Cleanup.
234 if (pConverter != NULL) IUnknown_Release(pConverter);
235 if (pFrame != NULL) IUnknown_Release(pFrame);
236 if (pDecoder != NULL) IUnknown_Release(pDecoder);
237 if (pFactory != NULL) IUnknown_Release(pFactory);
238 if (pStream != NULL) IUnknown_Release(pStream);
239 free(rgb);
240 return hr;
241}
242
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700243static int ReadPicture(const char* const filename, WebPPicture* const pic,
244 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800245 int ok;
246 if (pic->width != 0 && pic->height != 0) {
247 // If image size is specified, infer it as YUV format.
248 FILE* in_file = fopen(filename, "rb");
249 if (in_file == NULL) {
250 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
251 return 0;
252 }
253 ok = ReadYUV(in_file, pic);
254 fclose(in_file);
255 } else {
256 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700257 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800258 }
259 if (!ok) {
260 fprintf(stderr, "Error! Could not process file %s\n", filename);
261 }
262 return ok;
263}
264
James Zern31f9dc62011-06-06 17:56:50 -0700265#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800266
267#ifdef WEBP_HAVE_JPEG
268struct my_error_mgr {
269 struct jpeg_error_mgr pub;
270 jmp_buf setjmp_buffer;
271};
272
273static void my_error_exit(j_common_ptr dinfo) {
274 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
275 (*dinfo->err->output_message) (dinfo);
276 longjmp(myerr->setjmp_buffer, 1);
277}
278
279static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
280 int ok = 0;
281 int stride, width, height;
282 uint8_t* rgb = NULL;
283 uint8_t* row_ptr = NULL;
284 struct jpeg_decompress_struct dinfo;
285 struct my_error_mgr jerr;
286 JSAMPARRAY buffer;
287
288 dinfo.err = jpeg_std_error(&jerr.pub);
289 jerr.pub.error_exit = my_error_exit;
290
James Zern04e84cf2011-11-04 15:20:08 -0700291 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800292 Error:
293 jpeg_destroy_decompress(&dinfo);
294 goto End;
295 }
296
297 jpeg_create_decompress(&dinfo);
298 jpeg_stdio_src(&dinfo, in_file);
299 jpeg_read_header(&dinfo, TRUE);
300
301 dinfo.out_color_space = JCS_RGB;
302 dinfo.dct_method = JDCT_IFAST;
303 dinfo.do_fancy_upsampling = TRUE;
304
305 jpeg_start_decompress(&dinfo);
306
307 if (dinfo.output_components != 3) {
308 goto Error;
309 }
310
311 width = dinfo.output_width;
312 height = dinfo.output_height;
313 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
314
315 rgb = (uint8_t*)malloc(stride * height);
316 if (rgb == NULL) {
317 goto End;
318 }
319 row_ptr = rgb;
320
321 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
322 JPOOL_IMAGE, stride, 1);
323 if (buffer == NULL) {
324 goto End;
325 }
326
327 while (dinfo.output_scanline < dinfo.output_height) {
328 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
329 goto End;
330 }
331 memcpy(row_ptr, buffer[0], stride);
332 row_ptr += stride;
333 }
334
James Zern04e84cf2011-11-04 15:20:08 -0700335 jpeg_finish_decompress(&dinfo);
336 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800337
338 // WebP conversion.
339 pic->width = width;
340 pic->height = height;
341 ok = WebPPictureImportRGB(pic, rgb, stride);
342
343 End:
344 if (rgb) {
345 free(rgb);
346 }
347 return ok;
348}
349
350#else
351static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400352 (void)in_file;
353 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800354 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
355 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800356 return 0;
357}
358#endif
359
360#ifdef WEBP_HAVE_PNG
361static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700362 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800363 longjmp(png_jmpbuf(png), 1);
364}
365
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700366static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800367 png_structp png;
368 png_infop info;
369 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700370 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800371 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700372 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800373 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700374 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800375 int stride;
376 uint8_t* rgb = NULL;
377
378 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
379 if (png == NULL) {
380 goto End;
381 }
382
383 png_set_error_fn(png, 0, error_function, NULL);
384 if (setjmp(png_jmpbuf(png))) {
385 Error:
386 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700387 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800388 goto End;
389 }
390
391 info = png_create_info_struct(png);
392 if (info == NULL) goto Error;
393
394 png_init_io(png, in_file);
395 png_read_info(png, info);
396 if (!png_get_IHDR(png, info,
397 &width, &height, &bit_depth, &color_type, &interlaced,
398 NULL, NULL)) goto Error;
399
400 png_set_strip_16(png);
401 png_set_packing(png);
402 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000403 if (color_type == PNG_COLOR_TYPE_GRAY ||
404 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800405 if (bit_depth < 8) {
406 png_set_expand_gray_1_2_4_to_8(png);
407 }
408 png_set_gray_to_rgb(png);
409 }
410 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
411 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700412 has_alpha = 1;
413 } else {
414 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800415 }
416
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700417 if (!keep_alpha) {
418 png_set_strip_alpha(png);
419 has_alpha = 0;
420 }
421
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800422 num_passes = png_set_interlace_handling(png);
423 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700424 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800425 rgb = (uint8_t*)malloc(stride * height);
426 if (rgb == NULL) goto Error;
427 for (p = 0; p < num_passes; ++p) {
428 for (y = 0; y < height; ++y) {
429 png_bytep row = rgb + y * stride;
430 png_read_rows(png, &row, NULL, 1);
431 }
432 }
433 png_read_end(png, info);
434 png_destroy_read_struct(&png, &info, NULL);
435
436 pic->width = width;
437 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700438 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
439 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800440 free(rgb);
441
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000442 if (ok && has_alpha && keep_alpha == 2) {
443 WebPCleanupTransparentArea(pic);
444 }
445
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800446 End:
447 return ok;
448}
449#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700450static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400451 (void)in_file;
452 (void)pic;
453 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800454 fprintf(stderr, "PNG support not compiled. Please install the libpng "
455 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800456 return 0;
457}
458#endif
459
James Zern6f76d242012-07-01 17:55:21 -0700460#ifdef WEBP_HAVE_TIFF
461static int ReadTIFF(const char* const filename,
462 WebPPicture* const pic, int keep_alpha) {
463 TIFF* const tif = TIFFOpen(filename, "r");
464 uint32 width, height;
465 uint32* raster;
466 int ok = 0;
467 int dircount = 1;
468
469 if (tif == NULL) {
470 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
471 return 0;
472 }
473
474 while (TIFFReadDirectory(tif)) ++dircount;
475
476 if (dircount > 1) {
477 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
478 "Only the first will be used, %d will be ignored.\n",
479 dircount - 1);
480 }
481
482 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
483 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
484 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
485 if (raster != NULL) {
486 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
487 ORIENTATION_TOPLEFT, 1)) {
488 const int stride = width * sizeof(*raster);
489 pic->width = width;
490 pic->height = height;
491 // TIFF data is ABGR
492#ifdef __BIG_ENDIAN__
493 TIFFSwabArrayOfLong(raster, width * height);
494#endif
495 ok = keep_alpha
496 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
497 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
498 }
499 _TIFFfree(raster);
500 } else {
501 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
502 }
503
504 if (ok && keep_alpha == 2) {
505 WebPCleanupTransparentArea(pic);
506 }
507
508 TIFFClose(tif);
509 return ok;
510}
511#else
512static int ReadTIFF(const char* const filename,
513 WebPPicture* const pic, int keep_alpha) {
514 (void)filename;
515 (void)pic;
516 (void)keep_alpha;
517 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
518 "development package before building.\n");
519 return 0;
520}
521#endif
522
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800523typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700524 PNG_ = 0,
525 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700526 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800527 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800528} InputFileFormat;
529
530static InputFileFormat GetImageType(FILE* in_file) {
531 InputFileFormat format = UNSUPPORTED;
532 unsigned int magic;
533 unsigned char buf[4];
534
535 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
536 (fseek(in_file, 0, SEEK_SET) != 0)) {
537 return format;
538 }
539
540 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
541 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700542 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800543 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700544 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700545 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
546 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800547 }
548 return format;
549}
550
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700551static int ReadPicture(const char* const filename, WebPPicture* const pic,
552 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800553 int ok = 0;
554 FILE* in_file = fopen(filename, "rb");
555 if (in_file == NULL) {
556 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
557 return ok;
558 }
559
560 if (pic->width == 0 || pic->height == 0) {
561 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
562 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700563 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700564 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700565 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800566 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700567 } else if (format == TIFF_) {
568 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800569 }
570 } else {
571 // If image size is specified, infer it as YUV format.
572 ok = ReadYUV(in_file, pic);
573 }
574 if (!ok) {
575 fprintf(stderr, "Error! Could not process file %s\n", filename);
576 }
577
578 fclose(in_file);
579 return ok;
580}
581
James Zern31f9dc62011-06-06 17:56:50 -0700582#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800583
584static void AllocExtraInfo(WebPPicture* const pic) {
585 const int mb_w = (pic->width + 15) / 16;
586 const int mb_h = (pic->height + 15) / 16;
587 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
588}
589
590static void PrintByteCount(const int bytes[4], int total_size,
591 int* const totals) {
592 int s;
593 int total = 0;
594 for (s = 0; s < 4; ++s) {
595 fprintf(stderr, "| %7d ", bytes[s]);
596 total += bytes[s];
597 if (totals) totals[s] += bytes[s];
598 }
James Zern04e84cf2011-11-04 15:20:08 -0700599 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800600}
601
602static void PrintPercents(const int counts[4], int total) {
603 int s;
604 for (s = 0; s < 4; ++s) {
605 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
606 }
James Zern04e84cf2011-11-04 15:20:08 -0700607 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800608}
609
610static void PrintValues(const int values[4]) {
611 int s;
612 for (s = 0; s < 4; ++s) {
613 fprintf(stderr, "| %7d ", values[s]);
614 }
James Zern04e84cf2011-11-04 15:20:08 -0700615 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800616}
617
Pascal Massimino7d853d72012-07-24 16:15:36 -0700618static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
619 const char* const description) {
620 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
621 description, stats->lossless_size);
622 if (stats->lossless_features) {
623 fprintf(stderr, " * Lossless features used:");
624 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
625 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
626 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
627 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
628 fprintf(stderr, "\n");
629 }
630 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
631 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
632 if (stats->palette_size > 0) {
633 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
634 }
635}
636
Vikas Arorac4ccab62012-05-09 11:27:46 +0530637static void PrintExtraInfoLossless(const WebPPicture* const pic,
638 int short_output,
639 const char* const file_name) {
640 const WebPAuxStats* const stats = pic->stats;
641 if (short_output) {
642 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
643 } else {
644 fprintf(stderr, "File: %s\n", file_name);
645 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
646 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700647 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530648 }
649}
650
651static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
652 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800653 const WebPAuxStats* const stats = pic->stats;
654 if (short_output) {
655 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700656 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800657 const int num_i4 = stats->block_count[0];
658 const int num_i16 = stats->block_count[1];
659 const int num_skip = stats->block_count[2];
660 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800661 fprintf(stderr, "File: %s\n", file_name);
662 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700663 pic->width, pic->height,
664 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800665 fprintf(stderr, "Output: "
666 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800667 stats->coded_size,
668 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
669 if (total > 0) {
670 int totals[4] = { 0, 0, 0, 0 };
671 fprintf(stderr, "block count: intra4: %d\n"
672 " intra16: %d (-> %.2f%%)\n",
673 num_i4, num_i16, 100.f * num_i16 / total);
674 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
675 num_skip, 100.f * num_skip / total);
676 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
677 " mode-partition: %6d (%.1f%%)\n",
678 stats->header_bytes[0],
679 100.f * stats->header_bytes[0] / stats->coded_size,
680 stats->header_bytes[1],
681 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700682 if (stats->alpha_data_size > 0) {
683 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
684 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700685 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700686 if (stats->layer_data_size) {
687 fprintf(stderr, " enhancement: %6d\n",
688 stats->layer_data_size);
689 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800690 fprintf(stderr, " Residuals bytes "
691 "|segment 1|segment 2|segment 3"
692 "|segment 4| total\n");
693 fprintf(stderr, " intra4-coeffs: ");
694 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
695 fprintf(stderr, " intra16-coeffs: ");
696 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
697 fprintf(stderr, " chroma coeffs: ");
698 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
699 fprintf(stderr, " macroblocks: ");
700 PrintPercents(stats->segment_size, total);
701 fprintf(stderr, " quantizer: ");
702 PrintValues(stats->segment_quant);
703 fprintf(stderr, " filter level: ");
704 PrintValues(stats->segment_level);
705 fprintf(stderr, "------------------+---------");
706 fprintf(stderr, "+---------+---------+---------+-----------------\n");
707 fprintf(stderr, " segments total: ");
708 PrintByteCount(totals, stats->coded_size, NULL);
709 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700710 if (stats->lossless_size > 0) {
711 PrintFullLosslessInfo(stats, "alpha");
712 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700714 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800715 const int mb_w = (pic->width + 15) / 16;
716 const int mb_h = (pic->height + 15) / 16;
717 const int type = pic->extra_info_type;
718 int x, y;
719 for (y = 0; y < mb_h; ++y) {
720 for (x = 0; x < mb_w; ++x) {
721 const int c = pic->extra_info[x + y * mb_w];
722 if (type == 1) { // intra4/intra16
723 printf("%c", "+."[c]);
724 } else if (type == 2) { // segments
725 printf("%c", ".-*X"[c]);
726 } else if (type == 3) { // quantizers
727 printf("%.2d ", c);
728 } else if (type == 6 || type == 7) {
729 printf("%3d ", c);
730 } else {
731 printf("0x%.2x ", c);
732 }
733 }
734 printf("\n");
735 }
736 }
737}
738
James Zernc7e86ab2011-08-25 14:22:32 -0700739//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800740
741static int MyWriter(const uint8_t* data, size_t data_size,
742 const WebPPicture* const pic) {
743 FILE* const out = (FILE*)pic->custom_ptr;
744 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
745}
746
747// Dumps a picture as a PGM file using the IMC4 layout.
748static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
749 int y;
750 const int uv_width = (picture->width + 1) / 2;
751 const int uv_height = (picture->height + 1) / 2;
752 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700753 const int alpha_height =
754 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700755 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800756 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800757 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800758 fprintf(f, "P5\n%d %d\n255\n", stride, height);
759 for (y = 0; y < picture->height; ++y) {
760 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
761 return 0;
762 if (picture->width & 1) fputc(0, f); // pad
763 }
764 for (y = 0; y < uv_height; ++y) {
765 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
766 return 0;
767 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
768 return 0;
769 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700770 for (y = 0; y < alpha_height; ++y) {
771 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
772 return 0;
773 if (picture->width & 1) fputc(0, f); // pad
774 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800775 fclose(f);
776 return 1;
777}
778
James Zernc7e86ab2011-08-25 14:22:32 -0700779//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800780
Pascal Massimino30971c92011-12-01 02:24:50 -0800781static int ProgressReport(int percent, const WebPPicture* const picture) {
782 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700783 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800784 fflush(stdout);
785 return 1; // all ok
786}
787
788//------------------------------------------------------------------------------
789
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700790static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791 printf("Usage:\n\n");
792 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
793 printf("where quality is between 0 (poor) to 100 (very good).\n");
794 printf("Typical value is around 80.\n\n");
795 printf("Try -longhelp for an exhaustive list of advanced options.\n");
796}
797
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700798static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799 printf("Usage:\n");
800 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
801 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700802 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700803#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800804 printf("Windows builds can take as input any of the files handled by WIC\n");
805#endif
806 printf("options:\n");
807 printf(" -h / -help ............ short help\n");
808 printf(" -H / -longhelp ........ long help\n");
809 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530810 printf(" -alpha_q <int> ......... Transparency-compression quality "
811 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800812 printf(" -preset <string> ....... Preset setting, one of:\n");
813 printf(" default, photo, picture,\n");
814 printf(" drawing, icon, text\n");
815 printf(" -preset must come first, as it overwrites other parameters.");
816 printf("\n");
817 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
818 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700819 printf(" -size <int> ............ Target size (in bytes)\n");
820 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800821 printf("\n");
822 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
823 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
824 printf(" -f <int> ............... filter strength (0=off..100)\n");
825 printf(" -sharpness <int> ....... "
826 "filter sharpness (0:most .. 7:least sharp)\n");
827 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700828 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
829 printf(" "
830 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800831 printf(" -pass <int> ............ analysis pass number (1..10)\n");
832 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700833 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
834#ifdef WEBP_EXPERIMENTAL_FEATURES
835 printf(" -444 / -422 / -gray ..... Change colorspace\n");
836#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800837 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800838 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
839 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800840 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530841 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800842 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
843 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000844 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530845 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530846 printf(" -lossless .............. Encode image losslessly.\n");
847 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700848 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700849
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800850 printf("\n");
851 printf(" -short ................. condense printed message\n");
852 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700853 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700854#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700855 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700856#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800857 printf(" -v ..................... verbose, e.g. print encoding/decoding "
858 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800859 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800860 printf("\n");
861 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800862 printf(" -af .................... auto-adjust filter strength.\n");
863 printf(" -pre <int> ............. pre-processing filter\n");
864 printf("\n");
865}
866
James Zernc7e86ab2011-08-25 14:22:32 -0700867//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700868// Error messages
869
870static const char* const kErrorMessages[] = {
871 "OK",
872 "OUT_OF_MEMORY: Out of memory allocating objects",
873 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
874 "NULL_PARAMETER: NULL parameter passed to function",
875 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700876 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
877 "allowed is 16383 pixels.",
878 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
879 "To reduce the size of this partition, try using less segments "
880 "with the -segments option, and eventually reduce the number of "
881 "header bits using -partition_limit. More details are available "
882 "in the manual (`man cwebp`)",
883 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800884 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800885 "FILE_TOO_BIG: File would be too big to fit in 4G",
886 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700887};
888
James Zernc7e86ab2011-08-25 14:22:32 -0700889//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800890
891int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700892 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800893 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
894 FILE *out = NULL;
895 int c;
896 int short_output = 0;
897 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530898 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800899 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700900 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800901 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800902 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800903 int print_distortion = 0; // 1=PSNR, 2=SSIM
904 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800905 WebPConfig config;
906 WebPAuxStats stats;
907 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700908
Pascal Massiminod61479f2012-01-20 07:20:56 -0800909 if (!WebPPictureInit(&picture) ||
910 !WebPPictureInit(&original_picture) ||
911 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800912 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700913 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800914 }
915
916 if (argc == 1) {
917 HelpShort();
918 return 0;
919 }
920
921 for (c = 1; c < argc; ++c) {
922 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
923 HelpShort();
924 return 0;
925 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
926 HelpLong();
927 return 0;
928 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
929 out_file = argv[++c];
930 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
931 dump_file = argv[++c];
932 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800933 } else if (!strcmp(argv[c], "-print_ssim")) {
934 config.show_compressed = 1;
935 print_distortion = 2;
936 } else if (!strcmp(argv[c], "-print_psnr")) {
937 config.show_compressed = 1;
938 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800939 } else if (!strcmp(argv[c], "-short")) {
940 short_output++;
941 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700942 picture.width = strtol(argv[++c], NULL, 0);
943 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800944 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700945 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800946 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200947 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530948 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
949 config.alpha_quality = strtol(argv[++c], NULL, 0);
950 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
951 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000952 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
953 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530954 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800955 ++c;
956 if (!strcmp(argv[c], "none")) {
957 config.alpha_filtering = 0;
958 } else if (!strcmp(argv[c], "fast")) {
959 config.alpha_filtering = 1;
960 } else if (!strcmp(argv[c], "best")) {
961 config.alpha_filtering = 2;
962 } else {
963 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
964 goto Error;
965 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530966 } else if (!strcmp(argv[c], "-noalpha")) {
967 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000968 } else if (!strcmp(argv[c], "-lossless")) {
969 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000970 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530971 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
972 ++c;
973 if (!strcmp(argv[c], "photo")) {
974 config.image_hint = WEBP_HINT_PHOTO;
975 } else if (!strcmp(argv[c], "picture")) {
976 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700977 } else if (!strcmp(argv[c], "graph")) {
978 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530979 } else {
980 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
981 goto Error;
982 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800983 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700984 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800985 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200986 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800987 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700988 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800989 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700990 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800991 } else if (!strcmp(argv[c], "-af")) {
992 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700993 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800994 config.filter_type = 1;
995 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700996 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800997 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700998 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800999 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001000 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001001 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001002 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -07001003 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
1004 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001005 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001006 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001007#ifdef WEBP_EXPERIMENTAL_FEATURES
1008 } else if (!strcmp(argv[c], "-444")) {
1009 picture.colorspace = WEBP_YUV444;
1010 } else if (!strcmp(argv[c], "-422")) {
1011 picture.colorspace = WEBP_YUV422;
1012 } else if (!strcmp(argv[c], "-gray")) {
1013 picture.colorspace = WEBP_YUV400;
1014#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001015 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
1016 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001017 crop_x = strtol(argv[++c], NULL, 0);
1018 crop_y = strtol(argv[++c], NULL, 0);
1019 crop_w = strtol(argv[++c], NULL, 0);
1020 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001021 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
1022 resize_w = strtol(argv[++c], NULL, 0);
1023 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -07001024#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -07001025 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +00001026 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -07001027#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -07001028 } else if (!strcmp(argv[c], "-version")) {
1029 const int version = WebPGetEncoderVersion();
1030 printf("%d.%d.%d\n",
1031 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1032 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -08001033 } else if (!strcmp(argv[c], "-progress")) {
1034 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001035 } else if (!strcmp(argv[c], "-quiet")) {
1036 quiet = 1;
1037 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
1038 WebPPreset preset;
1039 ++c;
1040 if (!strcmp(argv[c], "default")) {
1041 preset = WEBP_PRESET_DEFAULT;
1042 } else if (!strcmp(argv[c], "photo")) {
1043 preset = WEBP_PRESET_PHOTO;
1044 } else if (!strcmp(argv[c], "picture")) {
1045 preset = WEBP_PRESET_PICTURE;
1046 } else if (!strcmp(argv[c], "drawing")) {
1047 preset = WEBP_PRESET_DRAWING;
1048 } else if (!strcmp(argv[c], "icon")) {
1049 preset = WEBP_PRESET_ICON;
1050 } else if (!strcmp(argv[c], "text")) {
1051 preset = WEBP_PRESET_TEXT;
1052 } else {
1053 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
1054 goto Error;
1055 }
1056 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -07001057 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001058 goto Error;
1059 }
1060 } else if (!strcmp(argv[c], "-v")) {
1061 verbose = 1;
1062 } else if (argv[c][0] == '-') {
1063 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1064 HelpLong();
1065 return -1;
1066 } else {
1067 in_file = argv[c];
1068 }
1069 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001070 if (in_file == NULL) {
1071 fprintf(stderr, "No input file specified!\n");
1072 HelpShort();
1073 goto Error;
1074 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001075
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301076 // Check for unsupported command line options for lossless mode and log
1077 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001078 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301079 if (config.target_size > 0 || config.target_PSNR > 0) {
1080 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1081 " for lossless encoding. Ignoring such option(s)!\n");
1082 }
1083 if (config.partition_limit > 0) {
1084 fprintf(stderr, "Partition limit option is not required for lossless"
1085 " encoding. Ignoring this option!\n");
1086 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301087 }
1088
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001089 if (!WebPValidateConfig(&config)) {
1090 fprintf(stderr, "Error! Invalid configuration.\n");
1091 goto Error;
1092 }
1093
Pascal Massimino0744e842011-02-25 12:03:27 -08001094 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001095 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001096 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001097 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -07001098 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001099 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001100 goto Error;
1101 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001102 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1103
Pascal Massimino0744e842011-02-25 12:03:27 -08001104 if (verbose) {
1105 const double time = StopwatchReadAndReset(&stop_watch);
1106 fprintf(stderr, "Time to read input: %.3fs\n", time);
1107 }
1108
1109 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001110 if (out_file) {
1111 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001112 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001113 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1114 goto Error;
1115 } else {
1116 if (!short_output && !quiet) {
1117 fprintf(stderr, "Saving file '%s'\n", out_file);
1118 }
1119 }
1120 picture.writer = MyWriter;
1121 picture.custom_ptr = (void*)out;
1122 } else {
1123 out = NULL;
1124 if (!quiet && !short_output) {
1125 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1126 fprintf(stderr, "be performed, but its results discarded.\n\n");
1127 }
1128 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001129 if (!quiet) {
1130 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001131 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001132 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001133
Pascal Massimino0744e842011-02-25 12:03:27 -08001134 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001135 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001136 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001137 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001138 if (crop != 0) {
1139 // We use self-cropping using a view.
1140 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1141 fprintf(stderr, "Error! Cannot crop picture\n");
1142 goto Error;
1143 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001144 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001145 if ((resize_w | resize_h) > 0) {
1146 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1147 fprintf(stderr, "Error! Cannot resize picture\n");
1148 goto Error;
1149 }
1150 }
1151 if (picture.extra_info_type > 0) {
1152 AllocExtraInfo(&picture);
1153 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001154 if (print_distortion > 0) { // Save original picture for later comparison
1155 WebPPictureCopy(&picture, &original_picture);
1156 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001157 if (!WebPEncode(&config, &picture)) {
1158 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001159 fprintf(stderr, "Error code: %d (%s)\n",
1160 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001161 goto Error;
1162 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001163 if (verbose) {
1164 const double time = StopwatchReadAndReset(&stop_watch);
1165 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1166 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001167
1168 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001169 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001170 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001171 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1172 } else if (!DumpPicture(&picture, dump_file)) {
1173 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1174 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001175 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001176
Pascal Massimino38369c02011-05-04 22:59:51 -07001177 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301178 if (config.lossless) {
1179 PrintExtraInfoLossless(&picture, short_output, in_file);
1180 } else {
1181 PrintExtraInfoLossy(&picture, short_output, in_file);
1182 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001183 }
1184 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1185 float values[5];
1186 WebPPictureDistortion(&picture, &original_picture,
1187 (print_distortion == 1) ? 0 : 1, values);
1188 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1189 (print_distortion == 1) ? "PSNR" : "SSIM",
1190 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001191 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001192 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001193
1194 Error:
1195 free(picture.extra_info);
1196 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001197 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001198 if (out != NULL) {
1199 fclose(out);
1200 }
1201
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001202 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001203}
1204
James Zernc7e86ab2011-08-25 14:22:32 -07001205//------------------------------------------------------------------------------