blob: 0f3e9d7b3e1d9707fe65f2c8e99e6a67c5fd2a80 [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
116static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
117 HRESULT hr = S_OK;
118 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
119 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800120 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800121 return hr;
122}
123
124static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700125 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800126 HRESULT hr = S_OK;
127 IWICBitmapFrameDecode* pFrame = NULL;
128 IWICFormatConverter* pConverter = NULL;
129 IWICImagingFactory* pFactory = NULL;
130 IWICBitmapDecoder* pDecoder = NULL;
131 IStream* pStream = NULL;
132 UINT frameCount = 0;
James Zern292ec5c2012-08-02 14:03:30 -0700133 UINT width = 0, height = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800134 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700135 WICPixelFormatGUID srcPixelFormat = { 0 };
136 GUID srcContainerFormat = { 0 };
137 const GUID* alphaContainers[] = {
138 &GUID_ContainerFormatBmp,
139 &GUID_ContainerFormatPng,
140 &GUID_ContainerFormatTiff
141 };
142 int has_alpha = 0;
143 int i, stride;
James Zern902d3e32012-05-08 17:49:39 -0700144 // From Microsoft SDK 7.0a
145 // Create local copies for compatibility when building against earlier
146 // versions of the SDK.
147 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
148 0x6fddc324, 0x4e03, 0x4bfe,
149 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
150 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
151 0xf5c7ad2d, 0x6a8d, 0x43dd,
152 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
153 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
154 0x6fddc324, 0x4e03, 0x4bfe,
155 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800156
157 IFS(CoInitialize(NULL));
158 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
159 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
160 (LPVOID*)&pFactory));
161 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800162 fprintf(stderr,
163 "Couldn't access Windows Imaging Component (are you running "
164 "Windows XP SP3 or newer?). Most formats not available. "
165 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800166 }
167 // Prepare for image decoding.
168 IFS(OpenInputStream(filename, &pStream));
169 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
170 WICDecodeMetadataCacheOnDemand, &pDecoder));
171 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
172 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800173 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800174 hr = E_FAIL;
175 }
176 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700177 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
178 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
179
180 has_alpha = keep_alpha;
181 for (i = 0;
182 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
183 ++i) {
James Zern1d38b252012-05-08 18:09:42 -0700184 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
185 MAKE_REFGUID(*alphaContainers[i]))) {
James Zern3cfe0882011-06-21 18:29:30 -0700186 has_alpha =
James Zern1d38b252012-05-08 18:09:42 -0700187 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
188 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
189 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
190 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
James Zern3cfe0882011-06-21 18:29:30 -0700191 break;
192 }
193 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800194
195 // Prepare for pixel format conversion (if necessary).
196 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
197 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700198 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
199 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700200 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800201 NULL, 0.0, WICBitmapPaletteTypeCustom));
202
203 // Decode.
204 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700205 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800206 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700207 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800208 if (rgb == NULL)
209 hr = E_OUTOFMEMORY;
210 }
James Zern3cfe0882011-06-21 18:29:30 -0700211 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
212 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800213
214 // WebP conversion.
215 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700216 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800217 pic->width = width;
218 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700219 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
220 : WebPPictureImportRGB(pic, rgb, stride);
221 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800222 hr = E_FAIL;
223 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000224 if (SUCCEEDED(hr)) {
225 if (has_alpha && keep_alpha == 2) {
226 WebPCleanupTransparentArea(pic);
227 }
228 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800229
230 // Cleanup.
231 if (pConverter != NULL) IUnknown_Release(pConverter);
232 if (pFrame != NULL) IUnknown_Release(pFrame);
233 if (pDecoder != NULL) IUnknown_Release(pDecoder);
234 if (pFactory != NULL) IUnknown_Release(pFactory);
235 if (pStream != NULL) IUnknown_Release(pStream);
236 free(rgb);
237 return hr;
238}
239
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700240static int ReadPicture(const char* const filename, WebPPicture* const pic,
241 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800242 int ok;
243 if (pic->width != 0 && pic->height != 0) {
244 // If image size is specified, infer it as YUV format.
245 FILE* in_file = fopen(filename, "rb");
246 if (in_file == NULL) {
247 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
248 return 0;
249 }
250 ok = ReadYUV(in_file, pic);
251 fclose(in_file);
252 } else {
253 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700254 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800255 }
256 if (!ok) {
257 fprintf(stderr, "Error! Could not process file %s\n", filename);
258 }
259 return ok;
260}
261
James Zern31f9dc62011-06-06 17:56:50 -0700262#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800263
264#ifdef WEBP_HAVE_JPEG
265struct my_error_mgr {
266 struct jpeg_error_mgr pub;
267 jmp_buf setjmp_buffer;
268};
269
270static void my_error_exit(j_common_ptr dinfo) {
271 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
272 (*dinfo->err->output_message) (dinfo);
273 longjmp(myerr->setjmp_buffer, 1);
274}
275
276static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
277 int ok = 0;
278 int stride, width, height;
279 uint8_t* rgb = NULL;
280 uint8_t* row_ptr = NULL;
281 struct jpeg_decompress_struct dinfo;
282 struct my_error_mgr jerr;
283 JSAMPARRAY buffer;
284
285 dinfo.err = jpeg_std_error(&jerr.pub);
286 jerr.pub.error_exit = my_error_exit;
287
James Zern04e84cf2011-11-04 15:20:08 -0700288 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800289 Error:
290 jpeg_destroy_decompress(&dinfo);
291 goto End;
292 }
293
294 jpeg_create_decompress(&dinfo);
295 jpeg_stdio_src(&dinfo, in_file);
296 jpeg_read_header(&dinfo, TRUE);
297
298 dinfo.out_color_space = JCS_RGB;
299 dinfo.dct_method = JDCT_IFAST;
300 dinfo.do_fancy_upsampling = TRUE;
301
302 jpeg_start_decompress(&dinfo);
303
304 if (dinfo.output_components != 3) {
305 goto Error;
306 }
307
308 width = dinfo.output_width;
309 height = dinfo.output_height;
310 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
311
312 rgb = (uint8_t*)malloc(stride * height);
313 if (rgb == NULL) {
314 goto End;
315 }
316 row_ptr = rgb;
317
318 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
319 JPOOL_IMAGE, stride, 1);
320 if (buffer == NULL) {
321 goto End;
322 }
323
324 while (dinfo.output_scanline < dinfo.output_height) {
325 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
326 goto End;
327 }
328 memcpy(row_ptr, buffer[0], stride);
329 row_ptr += stride;
330 }
331
James Zern04e84cf2011-11-04 15:20:08 -0700332 jpeg_finish_decompress(&dinfo);
333 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800334
335 // WebP conversion.
336 pic->width = width;
337 pic->height = height;
338 ok = WebPPictureImportRGB(pic, rgb, stride);
339
340 End:
341 if (rgb) {
342 free(rgb);
343 }
344 return ok;
345}
346
347#else
348static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400349 (void)in_file;
350 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800351 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
352 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800353 return 0;
354}
355#endif
356
357#ifdef WEBP_HAVE_PNG
358static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700359 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800360 longjmp(png_jmpbuf(png), 1);
361}
362
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700363static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800364 png_structp png;
365 png_infop info;
366 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700367 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800368 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700369 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800370 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700371 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800372 int stride;
373 uint8_t* rgb = NULL;
374
375 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
376 if (png == NULL) {
377 goto End;
378 }
379
380 png_set_error_fn(png, 0, error_function, NULL);
381 if (setjmp(png_jmpbuf(png))) {
382 Error:
383 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700384 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800385 goto End;
386 }
387
388 info = png_create_info_struct(png);
389 if (info == NULL) goto Error;
390
391 png_init_io(png, in_file);
392 png_read_info(png, info);
393 if (!png_get_IHDR(png, info,
394 &width, &height, &bit_depth, &color_type, &interlaced,
395 NULL, NULL)) goto Error;
396
397 png_set_strip_16(png);
398 png_set_packing(png);
399 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000400 if (color_type == PNG_COLOR_TYPE_GRAY ||
401 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800402 if (bit_depth < 8) {
403 png_set_expand_gray_1_2_4_to_8(png);
404 }
405 png_set_gray_to_rgb(png);
406 }
407 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
408 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700409 has_alpha = 1;
410 } else {
411 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800412 }
413
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700414 if (!keep_alpha) {
415 png_set_strip_alpha(png);
416 has_alpha = 0;
417 }
418
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800419 num_passes = png_set_interlace_handling(png);
420 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700421 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800422 rgb = (uint8_t*)malloc(stride * height);
423 if (rgb == NULL) goto Error;
424 for (p = 0; p < num_passes; ++p) {
425 for (y = 0; y < height; ++y) {
426 png_bytep row = rgb + y * stride;
427 png_read_rows(png, &row, NULL, 1);
428 }
429 }
430 png_read_end(png, info);
431 png_destroy_read_struct(&png, &info, NULL);
432
433 pic->width = width;
434 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700435 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
436 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800437 free(rgb);
438
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000439 if (ok && has_alpha && keep_alpha == 2) {
440 WebPCleanupTransparentArea(pic);
441 }
442
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800443 End:
444 return ok;
445}
446#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700447static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400448 (void)in_file;
449 (void)pic;
450 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800451 fprintf(stderr, "PNG support not compiled. Please install the libpng "
452 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800453 return 0;
454}
455#endif
456
James Zern6f76d242012-07-01 17:55:21 -0700457#ifdef WEBP_HAVE_TIFF
458static int ReadTIFF(const char* const filename,
459 WebPPicture* const pic, int keep_alpha) {
460 TIFF* const tif = TIFFOpen(filename, "r");
461 uint32 width, height;
462 uint32* raster;
463 int ok = 0;
464 int dircount = 1;
465
466 if (tif == NULL) {
467 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
468 return 0;
469 }
470
471 while (TIFFReadDirectory(tif)) ++dircount;
472
473 if (dircount > 1) {
474 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
475 "Only the first will be used, %d will be ignored.\n",
476 dircount - 1);
477 }
478
479 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
480 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
481 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
482 if (raster != NULL) {
483 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
484 ORIENTATION_TOPLEFT, 1)) {
485 const int stride = width * sizeof(*raster);
486 pic->width = width;
487 pic->height = height;
488 // TIFF data is ABGR
489#ifdef __BIG_ENDIAN__
490 TIFFSwabArrayOfLong(raster, width * height);
491#endif
492 ok = keep_alpha
493 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
494 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
495 }
496 _TIFFfree(raster);
497 } else {
498 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
499 }
500
501 if (ok && keep_alpha == 2) {
502 WebPCleanupTransparentArea(pic);
503 }
504
505 TIFFClose(tif);
506 return ok;
507}
508#else
509static int ReadTIFF(const char* const filename,
510 WebPPicture* const pic, int keep_alpha) {
511 (void)filename;
512 (void)pic;
513 (void)keep_alpha;
514 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
515 "development package before building.\n");
516 return 0;
517}
518#endif
519
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800520typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700521 PNG_ = 0,
522 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700523 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800524 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800525} InputFileFormat;
526
527static InputFileFormat GetImageType(FILE* in_file) {
528 InputFileFormat format = UNSUPPORTED;
529 unsigned int magic;
530 unsigned char buf[4];
531
532 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
533 (fseek(in_file, 0, SEEK_SET) != 0)) {
534 return format;
535 }
536
537 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
538 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700539 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800540 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700541 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700542 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
543 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800544 }
545 return format;
546}
547
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700548static int ReadPicture(const char* const filename, WebPPicture* const pic,
549 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800550 int ok = 0;
551 FILE* in_file = fopen(filename, "rb");
552 if (in_file == NULL) {
553 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
554 return ok;
555 }
556
557 if (pic->width == 0 || pic->height == 0) {
558 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
559 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700560 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700561 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700562 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800563 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700564 } else if (format == TIFF_) {
565 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800566 }
567 } else {
568 // If image size is specified, infer it as YUV format.
569 ok = ReadYUV(in_file, pic);
570 }
571 if (!ok) {
572 fprintf(stderr, "Error! Could not process file %s\n", filename);
573 }
574
575 fclose(in_file);
576 return ok;
577}
578
James Zern31f9dc62011-06-06 17:56:50 -0700579#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800580
581static void AllocExtraInfo(WebPPicture* const pic) {
582 const int mb_w = (pic->width + 15) / 16;
583 const int mb_h = (pic->height + 15) / 16;
584 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
585}
586
587static void PrintByteCount(const int bytes[4], int total_size,
588 int* const totals) {
589 int s;
590 int total = 0;
591 for (s = 0; s < 4; ++s) {
592 fprintf(stderr, "| %7d ", bytes[s]);
593 total += bytes[s];
594 if (totals) totals[s] += bytes[s];
595 }
James Zern04e84cf2011-11-04 15:20:08 -0700596 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800597}
598
599static void PrintPercents(const int counts[4], int total) {
600 int s;
601 for (s = 0; s < 4; ++s) {
602 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
603 }
James Zern04e84cf2011-11-04 15:20:08 -0700604 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800605}
606
607static void PrintValues(const int values[4]) {
608 int s;
609 for (s = 0; s < 4; ++s) {
610 fprintf(stderr, "| %7d ", values[s]);
611 }
James Zern04e84cf2011-11-04 15:20:08 -0700612 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800613}
614
Pascal Massimino7d853d72012-07-24 16:15:36 -0700615static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
616 const char* const description) {
617 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
618 description, stats->lossless_size);
619 if (stats->lossless_features) {
620 fprintf(stderr, " * Lossless features used:");
621 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
622 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
623 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
624 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
625 fprintf(stderr, "\n");
626 }
627 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
628 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
629 if (stats->palette_size > 0) {
630 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
631 }
632}
633
Vikas Arorac4ccab62012-05-09 11:27:46 +0530634static void PrintExtraInfoLossless(const WebPPicture* const pic,
635 int short_output,
636 const char* const file_name) {
637 const WebPAuxStats* const stats = pic->stats;
638 if (short_output) {
639 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
640 } else {
641 fprintf(stderr, "File: %s\n", file_name);
642 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
643 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700644 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530645 }
646}
647
648static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
649 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800650 const WebPAuxStats* const stats = pic->stats;
651 if (short_output) {
652 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700653 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800654 const int num_i4 = stats->block_count[0];
655 const int num_i16 = stats->block_count[1];
656 const int num_skip = stats->block_count[2];
657 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800658 fprintf(stderr, "File: %s\n", file_name);
659 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700660 pic->width, pic->height,
661 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800662 fprintf(stderr, "Output: "
663 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800664 stats->coded_size,
665 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
666 if (total > 0) {
667 int totals[4] = { 0, 0, 0, 0 };
668 fprintf(stderr, "block count: intra4: %d\n"
669 " intra16: %d (-> %.2f%%)\n",
670 num_i4, num_i16, 100.f * num_i16 / total);
671 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
672 num_skip, 100.f * num_skip / total);
673 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
674 " mode-partition: %6d (%.1f%%)\n",
675 stats->header_bytes[0],
676 100.f * stats->header_bytes[0] / stats->coded_size,
677 stats->header_bytes[1],
678 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700679 if (stats->alpha_data_size > 0) {
680 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
681 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700682 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700683 if (stats->layer_data_size) {
684 fprintf(stderr, " enhancement: %6d\n",
685 stats->layer_data_size);
686 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800687 fprintf(stderr, " Residuals bytes "
688 "|segment 1|segment 2|segment 3"
689 "|segment 4| total\n");
690 fprintf(stderr, " intra4-coeffs: ");
691 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
692 fprintf(stderr, " intra16-coeffs: ");
693 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
694 fprintf(stderr, " chroma coeffs: ");
695 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
696 fprintf(stderr, " macroblocks: ");
697 PrintPercents(stats->segment_size, total);
698 fprintf(stderr, " quantizer: ");
699 PrintValues(stats->segment_quant);
700 fprintf(stderr, " filter level: ");
701 PrintValues(stats->segment_level);
702 fprintf(stderr, "------------------+---------");
703 fprintf(stderr, "+---------+---------+---------+-----------------\n");
704 fprintf(stderr, " segments total: ");
705 PrintByteCount(totals, stats->coded_size, NULL);
706 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700707 if (stats->lossless_size > 0) {
708 PrintFullLosslessInfo(stats, "alpha");
709 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800710 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700711 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800712 const int mb_w = (pic->width + 15) / 16;
713 const int mb_h = (pic->height + 15) / 16;
714 const int type = pic->extra_info_type;
715 int x, y;
716 for (y = 0; y < mb_h; ++y) {
717 for (x = 0; x < mb_w; ++x) {
718 const int c = pic->extra_info[x + y * mb_w];
719 if (type == 1) { // intra4/intra16
720 printf("%c", "+."[c]);
721 } else if (type == 2) { // segments
722 printf("%c", ".-*X"[c]);
723 } else if (type == 3) { // quantizers
724 printf("%.2d ", c);
725 } else if (type == 6 || type == 7) {
726 printf("%3d ", c);
727 } else {
728 printf("0x%.2x ", c);
729 }
730 }
731 printf("\n");
732 }
733 }
734}
735
James Zernc7e86ab2011-08-25 14:22:32 -0700736//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800737
738static int MyWriter(const uint8_t* data, size_t data_size,
739 const WebPPicture* const pic) {
740 FILE* const out = (FILE*)pic->custom_ptr;
741 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
742}
743
744// Dumps a picture as a PGM file using the IMC4 layout.
745static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
746 int y;
747 const int uv_width = (picture->width + 1) / 2;
748 const int uv_height = (picture->height + 1) / 2;
749 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700750 const int alpha_height =
751 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700752 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800754 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800755 fprintf(f, "P5\n%d %d\n255\n", stride, height);
756 for (y = 0; y < picture->height; ++y) {
757 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
758 return 0;
759 if (picture->width & 1) fputc(0, f); // pad
760 }
761 for (y = 0; y < uv_height; ++y) {
762 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
763 return 0;
764 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
765 return 0;
766 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700767 for (y = 0; y < alpha_height; ++y) {
768 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
769 return 0;
770 if (picture->width & 1) fputc(0, f); // pad
771 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800772 fclose(f);
773 return 1;
774}
775
James Zernc7e86ab2011-08-25 14:22:32 -0700776//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777
Pascal Massimino30971c92011-12-01 02:24:50 -0800778static int ProgressReport(int percent, const WebPPicture* const picture) {
779 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700780 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800781 fflush(stdout);
782 return 1; // all ok
783}
784
785//------------------------------------------------------------------------------
786
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700787static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800788 printf("Usage:\n\n");
789 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
790 printf("where quality is between 0 (poor) to 100 (very good).\n");
791 printf("Typical value is around 80.\n\n");
792 printf("Try -longhelp for an exhaustive list of advanced options.\n");
793}
794
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700795static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800796 printf("Usage:\n");
797 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
798 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700799 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700800#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800801 printf("Windows builds can take as input any of the files handled by WIC\n");
802#endif
803 printf("options:\n");
804 printf(" -h / -help ............ short help\n");
805 printf(" -H / -longhelp ........ long help\n");
806 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530807 printf(" -alpha_q <int> ......... Transparency-compression quality "
808 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800809 printf(" -preset <string> ....... Preset setting, one of:\n");
810 printf(" default, photo, picture,\n");
811 printf(" drawing, icon, text\n");
812 printf(" -preset must come first, as it overwrites other parameters.");
813 printf("\n");
814 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
815 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700816 printf(" -size <int> ............ Target size (in bytes)\n");
817 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800818 printf("\n");
819 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
820 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
821 printf(" -f <int> ............... filter strength (0=off..100)\n");
822 printf(" -sharpness <int> ....... "
823 "filter sharpness (0:most .. 7:least sharp)\n");
824 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700825 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
826 printf(" "
827 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800828 printf(" -pass <int> ............ analysis pass number (1..10)\n");
829 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700830 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
831#ifdef WEBP_EXPERIMENTAL_FEATURES
832 printf(" -444 / -422 / -gray ..... Change colorspace\n");
833#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800834 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800835 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
836 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800837 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530838 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800839 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
840 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000841 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530842 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530843 printf(" -lossless .............. Encode image losslessly.\n");
844 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700845 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700846
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800847 printf("\n");
848 printf(" -short ................. condense printed message\n");
849 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700850 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700851#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700852 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700853#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800854 printf(" -v ..................... verbose, e.g. print encoding/decoding "
855 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800856 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800857 printf("\n");
858 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800859 printf(" -af .................... auto-adjust filter strength.\n");
860 printf(" -pre <int> ............. pre-processing filter\n");
861 printf("\n");
862}
863
James Zernc7e86ab2011-08-25 14:22:32 -0700864//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700865// Error messages
866
867static const char* const kErrorMessages[] = {
868 "OK",
869 "OUT_OF_MEMORY: Out of memory allocating objects",
870 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
871 "NULL_PARAMETER: NULL parameter passed to function",
872 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700873 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
874 "allowed is 16383 pixels.",
875 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
876 "To reduce the size of this partition, try using less segments "
877 "with the -segments option, and eventually reduce the number of "
878 "header bits using -partition_limit. More details are available "
879 "in the manual (`man cwebp`)",
880 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800881 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800882 "FILE_TOO_BIG: File would be too big to fit in 4G",
883 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700884};
885
James Zernc7e86ab2011-08-25 14:22:32 -0700886//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800887
888int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700889 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800890 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
891 FILE *out = NULL;
892 int c;
893 int short_output = 0;
894 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530895 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800896 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700897 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800898 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800899 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800900 int print_distortion = 0; // 1=PSNR, 2=SSIM
901 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800902 WebPConfig config;
903 WebPAuxStats stats;
904 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700905
Pascal Massiminod61479f2012-01-20 07:20:56 -0800906 if (!WebPPictureInit(&picture) ||
907 !WebPPictureInit(&original_picture) ||
908 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800909 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700910 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800911 }
912
913 if (argc == 1) {
914 HelpShort();
915 return 0;
916 }
917
918 for (c = 1; c < argc; ++c) {
919 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
920 HelpShort();
921 return 0;
922 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
923 HelpLong();
924 return 0;
925 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
926 out_file = argv[++c];
927 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
928 dump_file = argv[++c];
929 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800930 } else if (!strcmp(argv[c], "-print_ssim")) {
931 config.show_compressed = 1;
932 print_distortion = 2;
933 } else if (!strcmp(argv[c], "-print_psnr")) {
934 config.show_compressed = 1;
935 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800936 } else if (!strcmp(argv[c], "-short")) {
937 short_output++;
938 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700939 picture.width = strtol(argv[++c], NULL, 0);
940 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800941 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700942 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800943 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200944 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530945 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
946 config.alpha_quality = strtol(argv[++c], NULL, 0);
947 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
948 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000949 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
950 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530951 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800952 ++c;
953 if (!strcmp(argv[c], "none")) {
954 config.alpha_filtering = 0;
955 } else if (!strcmp(argv[c], "fast")) {
956 config.alpha_filtering = 1;
957 } else if (!strcmp(argv[c], "best")) {
958 config.alpha_filtering = 2;
959 } else {
960 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
961 goto Error;
962 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530963 } else if (!strcmp(argv[c], "-noalpha")) {
964 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000965 } else if (!strcmp(argv[c], "-lossless")) {
966 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000967 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530968 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
969 ++c;
970 if (!strcmp(argv[c], "photo")) {
971 config.image_hint = WEBP_HINT_PHOTO;
972 } else if (!strcmp(argv[c], "picture")) {
973 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700974 } else if (!strcmp(argv[c], "graph")) {
975 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530976 } else {
977 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
978 goto Error;
979 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800980 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700981 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800982 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200983 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800984 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700985 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800986 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700987 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800988 } else if (!strcmp(argv[c], "-af")) {
989 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700990 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800991 config.filter_type = 1;
992 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700993 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800994 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700995 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800996 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700997 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800998 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700999 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -07001000 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
1001 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001002 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001003 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001004#ifdef WEBP_EXPERIMENTAL_FEATURES
1005 } else if (!strcmp(argv[c], "-444")) {
1006 picture.colorspace = WEBP_YUV444;
1007 } else if (!strcmp(argv[c], "-422")) {
1008 picture.colorspace = WEBP_YUV422;
1009 } else if (!strcmp(argv[c], "-gray")) {
1010 picture.colorspace = WEBP_YUV400;
1011#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001012 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
1013 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001014 crop_x = strtol(argv[++c], NULL, 0);
1015 crop_y = strtol(argv[++c], NULL, 0);
1016 crop_w = strtol(argv[++c], NULL, 0);
1017 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001018 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
1019 resize_w = strtol(argv[++c], NULL, 0);
1020 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -07001021#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -07001022 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +00001023 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -07001024#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -07001025 } else if (!strcmp(argv[c], "-version")) {
1026 const int version = WebPGetEncoderVersion();
1027 printf("%d.%d.%d\n",
1028 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1029 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -08001030 } else if (!strcmp(argv[c], "-progress")) {
1031 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001032 } else if (!strcmp(argv[c], "-quiet")) {
1033 quiet = 1;
1034 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
1035 WebPPreset preset;
1036 ++c;
1037 if (!strcmp(argv[c], "default")) {
1038 preset = WEBP_PRESET_DEFAULT;
1039 } else if (!strcmp(argv[c], "photo")) {
1040 preset = WEBP_PRESET_PHOTO;
1041 } else if (!strcmp(argv[c], "picture")) {
1042 preset = WEBP_PRESET_PICTURE;
1043 } else if (!strcmp(argv[c], "drawing")) {
1044 preset = WEBP_PRESET_DRAWING;
1045 } else if (!strcmp(argv[c], "icon")) {
1046 preset = WEBP_PRESET_ICON;
1047 } else if (!strcmp(argv[c], "text")) {
1048 preset = WEBP_PRESET_TEXT;
1049 } else {
1050 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
1051 goto Error;
1052 }
1053 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -07001054 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001055 goto Error;
1056 }
1057 } else if (!strcmp(argv[c], "-v")) {
1058 verbose = 1;
1059 } else if (argv[c][0] == '-') {
1060 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1061 HelpLong();
1062 return -1;
1063 } else {
1064 in_file = argv[c];
1065 }
1066 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001067 if (in_file == NULL) {
1068 fprintf(stderr, "No input file specified!\n");
1069 HelpShort();
1070 goto Error;
1071 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001072
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301073 // Check for unsupported command line options for lossless mode and log
1074 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001075 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301076 if (config.target_size > 0 || config.target_PSNR > 0) {
1077 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1078 " for lossless encoding. Ignoring such option(s)!\n");
1079 }
1080 if (config.partition_limit > 0) {
1081 fprintf(stderr, "Partition limit option is not required for lossless"
1082 " encoding. Ignoring this option!\n");
1083 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301084 }
1085
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001086 if (!WebPValidateConfig(&config)) {
1087 fprintf(stderr, "Error! Invalid configuration.\n");
1088 goto Error;
1089 }
1090
Pascal Massimino0744e842011-02-25 12:03:27 -08001091 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001092 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001093 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001094 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -07001095 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001096 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001097 goto Error;
1098 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001099 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1100
Pascal Massimino0744e842011-02-25 12:03:27 -08001101 if (verbose) {
1102 const double time = StopwatchReadAndReset(&stop_watch);
1103 fprintf(stderr, "Time to read input: %.3fs\n", time);
1104 }
1105
1106 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001107 if (out_file) {
1108 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001109 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001110 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1111 goto Error;
1112 } else {
1113 if (!short_output && !quiet) {
1114 fprintf(stderr, "Saving file '%s'\n", out_file);
1115 }
1116 }
1117 picture.writer = MyWriter;
1118 picture.custom_ptr = (void*)out;
1119 } else {
1120 out = NULL;
1121 if (!quiet && !short_output) {
1122 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1123 fprintf(stderr, "be performed, but its results discarded.\n\n");
1124 }
1125 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001126 if (!quiet) {
1127 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001128 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001129 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001130
Pascal Massimino0744e842011-02-25 12:03:27 -08001131 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001132 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001133 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001134 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001135 if (crop != 0) {
1136 // We use self-cropping using a view.
1137 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1138 fprintf(stderr, "Error! Cannot crop picture\n");
1139 goto Error;
1140 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001141 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001142 if ((resize_w | resize_h) > 0) {
1143 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1144 fprintf(stderr, "Error! Cannot resize picture\n");
1145 goto Error;
1146 }
1147 }
1148 if (picture.extra_info_type > 0) {
1149 AllocExtraInfo(&picture);
1150 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001151 if (print_distortion > 0) { // Save original picture for later comparison
1152 WebPPictureCopy(&picture, &original_picture);
1153 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001154 if (!WebPEncode(&config, &picture)) {
1155 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001156 fprintf(stderr, "Error code: %d (%s)\n",
1157 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001158 goto Error;
1159 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001160 if (verbose) {
1161 const double time = StopwatchReadAndReset(&stop_watch);
1162 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1163 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001164
1165 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001166 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001167 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001168 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1169 } else if (!DumpPicture(&picture, dump_file)) {
1170 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1171 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001172 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001173
Pascal Massimino38369c02011-05-04 22:59:51 -07001174 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301175 if (config.lossless) {
1176 PrintExtraInfoLossless(&picture, short_output, in_file);
1177 } else {
1178 PrintExtraInfoLossy(&picture, short_output, in_file);
1179 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001180 }
1181 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1182 float values[5];
1183 WebPPictureDistortion(&picture, &original_picture,
1184 (print_distortion == 1) ? 0 : 1, values);
1185 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1186 (print_distortion == 1) ? "PSNR" : "SSIM",
1187 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001188 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001189 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001190
1191 Error:
1192 free(picture.extra_info);
1193 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001194 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001195 if (out != NULL) {
1196 fclose(out);
1197 }
1198
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001199 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001200}
1201
James Zernc7e86ab2011-08-25 14:22:32 -07001202//------------------------------------------------------------------------------