blob: 0e5b8206dfcd2a478b5114e48d7338cb48d38439 [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
James Zernecd66f72012-10-08 18:15:30 -0700180 if (keep_alpha) {
181 for (i = 0;
182 i < sizeof(alphaContainers) / sizeof(alphaContainers[0]);
183 ++i) {
184 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
185 MAKE_REFGUID(*alphaContainers[i]))) {
186 has_alpha =
187 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
188 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
189 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
190 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
191 break;
192 }
James Zern3cfe0882011-06-21 18:29:30 -0700193 }
194 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800195
196 // Prepare for pixel format conversion (if necessary).
197 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
198 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700199 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
200 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700201 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800202 NULL, 0.0, WICBitmapPaletteTypeCustom));
203
204 // Decode.
205 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700206 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800207 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700208 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800209 if (rgb == NULL)
210 hr = E_OUTOFMEMORY;
211 }
James Zern3cfe0882011-06-21 18:29:30 -0700212 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
213 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800214
215 // WebP conversion.
216 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700217 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800218 pic->width = width;
219 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700220 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
221 : WebPPictureImportRGB(pic, rgb, stride);
222 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800223 hr = E_FAIL;
224 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000225 if (SUCCEEDED(hr)) {
226 if (has_alpha && keep_alpha == 2) {
227 WebPCleanupTransparentArea(pic);
228 }
229 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800230
231 // Cleanup.
232 if (pConverter != NULL) IUnknown_Release(pConverter);
233 if (pFrame != NULL) IUnknown_Release(pFrame);
234 if (pDecoder != NULL) IUnknown_Release(pDecoder);
235 if (pFactory != NULL) IUnknown_Release(pFactory);
236 if (pStream != NULL) IUnknown_Release(pStream);
237 free(rgb);
238 return hr;
239}
240
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700241static int ReadPicture(const char* const filename, WebPPicture* const pic,
242 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800243 int ok;
244 if (pic->width != 0 && pic->height != 0) {
245 // If image size is specified, infer it as YUV format.
246 FILE* in_file = fopen(filename, "rb");
247 if (in_file == NULL) {
248 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
249 return 0;
250 }
251 ok = ReadYUV(in_file, pic);
252 fclose(in_file);
253 } else {
254 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700255 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800256 }
257 if (!ok) {
258 fprintf(stderr, "Error! Could not process file %s\n", filename);
259 }
260 return ok;
261}
262
James Zern31f9dc62011-06-06 17:56:50 -0700263#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800264
265#ifdef WEBP_HAVE_JPEG
266struct my_error_mgr {
267 struct jpeg_error_mgr pub;
268 jmp_buf setjmp_buffer;
269};
270
271static void my_error_exit(j_common_ptr dinfo) {
272 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
273 (*dinfo->err->output_message) (dinfo);
274 longjmp(myerr->setjmp_buffer, 1);
275}
276
277static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
278 int ok = 0;
279 int stride, width, height;
280 uint8_t* rgb = NULL;
281 uint8_t* row_ptr = NULL;
282 struct jpeg_decompress_struct dinfo;
283 struct my_error_mgr jerr;
284 JSAMPARRAY buffer;
285
286 dinfo.err = jpeg_std_error(&jerr.pub);
287 jerr.pub.error_exit = my_error_exit;
288
James Zern04e84cf2011-11-04 15:20:08 -0700289 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800290 Error:
291 jpeg_destroy_decompress(&dinfo);
292 goto End;
293 }
294
295 jpeg_create_decompress(&dinfo);
296 jpeg_stdio_src(&dinfo, in_file);
297 jpeg_read_header(&dinfo, TRUE);
298
299 dinfo.out_color_space = JCS_RGB;
300 dinfo.dct_method = JDCT_IFAST;
301 dinfo.do_fancy_upsampling = TRUE;
302
303 jpeg_start_decompress(&dinfo);
304
305 if (dinfo.output_components != 3) {
306 goto Error;
307 }
308
309 width = dinfo.output_width;
310 height = dinfo.output_height;
311 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
312
313 rgb = (uint8_t*)malloc(stride * height);
314 if (rgb == NULL) {
315 goto End;
316 }
317 row_ptr = rgb;
318
319 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
320 JPOOL_IMAGE, stride, 1);
321 if (buffer == NULL) {
322 goto End;
323 }
324
325 while (dinfo.output_scanline < dinfo.output_height) {
326 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
327 goto End;
328 }
329 memcpy(row_ptr, buffer[0], stride);
330 row_ptr += stride;
331 }
332
James Zern04e84cf2011-11-04 15:20:08 -0700333 jpeg_finish_decompress(&dinfo);
334 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800335
336 // WebP conversion.
337 pic->width = width;
338 pic->height = height;
339 ok = WebPPictureImportRGB(pic, rgb, stride);
340
341 End:
342 if (rgb) {
343 free(rgb);
344 }
345 return ok;
346}
347
348#else
349static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400350 (void)in_file;
351 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800352 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
353 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800354 return 0;
355}
356#endif
357
358#ifdef WEBP_HAVE_PNG
359static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700360 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800361 longjmp(png_jmpbuf(png), 1);
362}
363
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700364static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800365 png_structp png;
366 png_infop info;
367 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700368 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800369 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700370 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800371 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700372 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800373 int stride;
374 uint8_t* rgb = NULL;
375
376 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
377 if (png == NULL) {
378 goto End;
379 }
380
381 png_set_error_fn(png, 0, error_function, NULL);
382 if (setjmp(png_jmpbuf(png))) {
383 Error:
384 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700385 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800386 goto End;
387 }
388
389 info = png_create_info_struct(png);
390 if (info == NULL) goto Error;
391
392 png_init_io(png, in_file);
393 png_read_info(png, info);
394 if (!png_get_IHDR(png, info,
395 &width, &height, &bit_depth, &color_type, &interlaced,
396 NULL, NULL)) goto Error;
397
398 png_set_strip_16(png);
399 png_set_packing(png);
400 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000401 if (color_type == PNG_COLOR_TYPE_GRAY ||
402 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800403 if (bit_depth < 8) {
404 png_set_expand_gray_1_2_4_to_8(png);
405 }
406 png_set_gray_to_rgb(png);
407 }
408 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
409 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700410 has_alpha = 1;
411 } else {
412 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800413 }
414
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700415 if (!keep_alpha) {
416 png_set_strip_alpha(png);
417 has_alpha = 0;
418 }
419
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800420 num_passes = png_set_interlace_handling(png);
421 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700422 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800423 rgb = (uint8_t*)malloc(stride * height);
424 if (rgb == NULL) goto Error;
425 for (p = 0; p < num_passes; ++p) {
426 for (y = 0; y < height; ++y) {
427 png_bytep row = rgb + y * stride;
428 png_read_rows(png, &row, NULL, 1);
429 }
430 }
431 png_read_end(png, info);
432 png_destroy_read_struct(&png, &info, NULL);
433
434 pic->width = width;
435 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700436 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
437 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800438 free(rgb);
439
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000440 if (ok && has_alpha && keep_alpha == 2) {
441 WebPCleanupTransparentArea(pic);
442 }
443
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800444 End:
445 return ok;
446}
447#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700448static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400449 (void)in_file;
450 (void)pic;
451 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800452 fprintf(stderr, "PNG support not compiled. Please install the libpng "
453 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800454 return 0;
455}
456#endif
457
James Zern6f76d242012-07-01 17:55:21 -0700458#ifdef WEBP_HAVE_TIFF
459static int ReadTIFF(const char* const filename,
460 WebPPicture* const pic, int keep_alpha) {
461 TIFF* const tif = TIFFOpen(filename, "r");
462 uint32 width, height;
463 uint32* raster;
464 int ok = 0;
465 int dircount = 1;
466
467 if (tif == NULL) {
468 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
469 return 0;
470 }
471
472 while (TIFFReadDirectory(tif)) ++dircount;
473
474 if (dircount > 1) {
475 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
476 "Only the first will be used, %d will be ignored.\n",
477 dircount - 1);
478 }
479
480 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
481 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
482 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
483 if (raster != NULL) {
484 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
485 ORIENTATION_TOPLEFT, 1)) {
486 const int stride = width * sizeof(*raster);
487 pic->width = width;
488 pic->height = height;
489 // TIFF data is ABGR
490#ifdef __BIG_ENDIAN__
491 TIFFSwabArrayOfLong(raster, width * height);
492#endif
493 ok = keep_alpha
494 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
495 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
496 }
497 _TIFFfree(raster);
498 } else {
499 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
500 }
501
502 if (ok && keep_alpha == 2) {
503 WebPCleanupTransparentArea(pic);
504 }
505
506 TIFFClose(tif);
507 return ok;
508}
509#else
510static int ReadTIFF(const char* const filename,
511 WebPPicture* const pic, int keep_alpha) {
512 (void)filename;
513 (void)pic;
514 (void)keep_alpha;
515 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
516 "development package before building.\n");
517 return 0;
518}
519#endif
520
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800521typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700522 PNG_ = 0,
523 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700524 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800525 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800526} InputFileFormat;
527
528static InputFileFormat GetImageType(FILE* in_file) {
529 InputFileFormat format = UNSUPPORTED;
530 unsigned int magic;
531 unsigned char buf[4];
532
533 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
534 (fseek(in_file, 0, SEEK_SET) != 0)) {
535 return format;
536 }
537
538 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
539 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700540 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800541 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700542 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700543 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
544 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800545 }
546 return format;
547}
548
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700549static int ReadPicture(const char* const filename, WebPPicture* const pic,
550 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800551 int ok = 0;
552 FILE* in_file = fopen(filename, "rb");
553 if (in_file == NULL) {
554 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
555 return ok;
556 }
557
558 if (pic->width == 0 || pic->height == 0) {
559 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
560 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700561 if (format == PNG_) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700562 ok = ReadPNG(in_file, pic, keep_alpha);
James Zernd8921dd2012-07-02 11:24:23 -0700563 } else if (format == JPEG_) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800564 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700565 } else if (format == TIFF_) {
566 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800567 }
568 } else {
569 // If image size is specified, infer it as YUV format.
570 ok = ReadYUV(in_file, pic);
571 }
572 if (!ok) {
573 fprintf(stderr, "Error! Could not process file %s\n", filename);
574 }
575
576 fclose(in_file);
577 return ok;
578}
579
James Zern31f9dc62011-06-06 17:56:50 -0700580#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800581
582static void AllocExtraInfo(WebPPicture* const pic) {
583 const int mb_w = (pic->width + 15) / 16;
584 const int mb_h = (pic->height + 15) / 16;
585 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
586}
587
588static void PrintByteCount(const int bytes[4], int total_size,
589 int* const totals) {
590 int s;
591 int total = 0;
592 for (s = 0; s < 4; ++s) {
593 fprintf(stderr, "| %7d ", bytes[s]);
594 total += bytes[s];
595 if (totals) totals[s] += bytes[s];
596 }
James Zern04e84cf2011-11-04 15:20:08 -0700597 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800598}
599
600static void PrintPercents(const int counts[4], int total) {
601 int s;
602 for (s = 0; s < 4; ++s) {
603 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
604 }
James Zern04e84cf2011-11-04 15:20:08 -0700605 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800606}
607
608static void PrintValues(const int values[4]) {
609 int s;
610 for (s = 0; s < 4; ++s) {
611 fprintf(stderr, "| %7d ", values[s]);
612 }
James Zern04e84cf2011-11-04 15:20:08 -0700613 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800614}
615
Pascal Massimino7d853d72012-07-24 16:15:36 -0700616static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
617 const char* const description) {
618 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
619 description, stats->lossless_size);
620 if (stats->lossless_features) {
621 fprintf(stderr, " * Lossless features used:");
622 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
623 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
624 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
625 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
626 fprintf(stderr, "\n");
627 }
628 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
629 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
630 if (stats->palette_size > 0) {
631 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
632 }
633}
634
Vikas Arorac4ccab62012-05-09 11:27:46 +0530635static void PrintExtraInfoLossless(const WebPPicture* const pic,
636 int short_output,
637 const char* const file_name) {
638 const WebPAuxStats* const stats = pic->stats;
639 if (short_output) {
640 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
641 } else {
642 fprintf(stderr, "File: %s\n", file_name);
643 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
644 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700645 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530646 }
647}
648
649static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
650 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800651 const WebPAuxStats* const stats = pic->stats;
652 if (short_output) {
653 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700654 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800655 const int num_i4 = stats->block_count[0];
656 const int num_i16 = stats->block_count[1];
657 const int num_skip = stats->block_count[2];
658 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800659 fprintf(stderr, "File: %s\n", file_name);
660 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700661 pic->width, pic->height,
662 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800663 fprintf(stderr, "Output: "
664 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800665 stats->coded_size,
666 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
667 if (total > 0) {
668 int totals[4] = { 0, 0, 0, 0 };
669 fprintf(stderr, "block count: intra4: %d\n"
670 " intra16: %d (-> %.2f%%)\n",
671 num_i4, num_i16, 100.f * num_i16 / total);
672 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
673 num_skip, 100.f * num_skip / total);
674 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
675 " mode-partition: %6d (%.1f%%)\n",
676 stats->header_bytes[0],
677 100.f * stats->header_bytes[0] / stats->coded_size,
678 stats->header_bytes[1],
679 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700680 if (stats->alpha_data_size > 0) {
681 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
682 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700683 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700684 if (stats->layer_data_size) {
685 fprintf(stderr, " enhancement: %6d\n",
686 stats->layer_data_size);
687 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 fprintf(stderr, " Residuals bytes "
689 "|segment 1|segment 2|segment 3"
690 "|segment 4| total\n");
691 fprintf(stderr, " intra4-coeffs: ");
692 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
693 fprintf(stderr, " intra16-coeffs: ");
694 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
695 fprintf(stderr, " chroma coeffs: ");
696 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
697 fprintf(stderr, " macroblocks: ");
698 PrintPercents(stats->segment_size, total);
699 fprintf(stderr, " quantizer: ");
700 PrintValues(stats->segment_quant);
701 fprintf(stderr, " filter level: ");
702 PrintValues(stats->segment_level);
703 fprintf(stderr, "------------------+---------");
704 fprintf(stderr, "+---------+---------+---------+-----------------\n");
705 fprintf(stderr, " segments total: ");
706 PrintByteCount(totals, stats->coded_size, NULL);
707 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700708 if (stats->lossless_size > 0) {
709 PrintFullLosslessInfo(stats, "alpha");
710 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700712 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 const int mb_w = (pic->width + 15) / 16;
714 const int mb_h = (pic->height + 15) / 16;
715 const int type = pic->extra_info_type;
716 int x, y;
717 for (y = 0; y < mb_h; ++y) {
718 for (x = 0; x < mb_w; ++x) {
719 const int c = pic->extra_info[x + y * mb_w];
720 if (type == 1) { // intra4/intra16
721 printf("%c", "+."[c]);
722 } else if (type == 2) { // segments
723 printf("%c", ".-*X"[c]);
724 } else if (type == 3) { // quantizers
725 printf("%.2d ", c);
726 } else if (type == 6 || type == 7) {
727 printf("%3d ", c);
728 } else {
729 printf("0x%.2x ", c);
730 }
731 }
732 printf("\n");
733 }
734 }
735}
736
James Zernc7e86ab2011-08-25 14:22:32 -0700737//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800738
739static int MyWriter(const uint8_t* data, size_t data_size,
740 const WebPPicture* const pic) {
741 FILE* const out = (FILE*)pic->custom_ptr;
742 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
743}
744
745// Dumps a picture as a PGM file using the IMC4 layout.
746static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
747 int y;
748 const int uv_width = (picture->width + 1) / 2;
749 const int uv_height = (picture->height + 1) / 2;
750 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700751 const int alpha_height =
752 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700753 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800754 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800755 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800756 fprintf(f, "P5\n%d %d\n255\n", stride, height);
757 for (y = 0; y < picture->height; ++y) {
758 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
759 return 0;
760 if (picture->width & 1) fputc(0, f); // pad
761 }
762 for (y = 0; y < uv_height; ++y) {
763 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
764 return 0;
765 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
766 return 0;
767 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700768 for (y = 0; y < alpha_height; ++y) {
769 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
770 return 0;
771 if (picture->width & 1) fputc(0, f); // pad
772 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800773 fclose(f);
774 return 1;
775}
776
James Zernc7e86ab2011-08-25 14:22:32 -0700777//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800778
Pascal Massimino30971c92011-12-01 02:24:50 -0800779static int ProgressReport(int percent, const WebPPicture* const picture) {
780 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700781 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800782 fflush(stdout);
783 return 1; // all ok
784}
785
786//------------------------------------------------------------------------------
787
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700788static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 printf("Usage:\n\n");
790 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
791 printf("where quality is between 0 (poor) to 100 (very good).\n");
792 printf("Typical value is around 80.\n\n");
793 printf("Try -longhelp for an exhaustive list of advanced options.\n");
794}
795
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700796static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800797 printf("Usage:\n");
798 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
799 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700800 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700801#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800802 printf("Windows builds can take as input any of the files handled by WIC\n");
803#endif
804 printf("options:\n");
805 printf(" -h / -help ............ short help\n");
806 printf(" -H / -longhelp ........ long help\n");
807 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530808 printf(" -alpha_q <int> ......... Transparency-compression quality "
809 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800810 printf(" -preset <string> ....... Preset setting, one of:\n");
811 printf(" default, photo, picture,\n");
812 printf(" drawing, icon, text\n");
813 printf(" -preset must come first, as it overwrites other parameters.");
814 printf("\n");
815 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
816 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700817 printf(" -size <int> ............ Target size (in bytes)\n");
818 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800819 printf("\n");
820 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
821 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
822 printf(" -f <int> ............... filter strength (0=off..100)\n");
823 printf(" -sharpness <int> ....... "
824 "filter sharpness (0:most .. 7:least sharp)\n");
825 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700826 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
827 printf(" "
828 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800829 printf(" -pass <int> ............ analysis pass number (1..10)\n");
830 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700831 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
832#ifdef WEBP_EXPERIMENTAL_FEATURES
833 printf(" -444 / -422 / -gray ..... Change colorspace\n");
834#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800836 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
837 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800838 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530839 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800840 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
841 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000842 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530843 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530844 printf(" -lossless .............. Encode image losslessly.\n");
845 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700846 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700847
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800848 printf("\n");
849 printf(" -short ................. condense printed message\n");
850 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700851 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700852#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700853 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700854#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800855 printf(" -v ..................... verbose, e.g. print encoding/decoding "
856 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800857 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800858 printf("\n");
859 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800860 printf(" -af .................... auto-adjust filter strength.\n");
861 printf(" -pre <int> ............. pre-processing filter\n");
862 printf("\n");
863}
864
James Zernc7e86ab2011-08-25 14:22:32 -0700865//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700866// Error messages
867
868static const char* const kErrorMessages[] = {
869 "OK",
870 "OUT_OF_MEMORY: Out of memory allocating objects",
871 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
872 "NULL_PARAMETER: NULL parameter passed to function",
873 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700874 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
875 "allowed is 16383 pixels.",
876 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
877 "To reduce the size of this partition, try using less segments "
878 "with the -segments option, and eventually reduce the number of "
879 "header bits using -partition_limit. More details are available "
880 "in the manual (`man cwebp`)",
881 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800882 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800883 "FILE_TOO_BIG: File would be too big to fit in 4G",
884 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700885};
886
James Zernc7e86ab2011-08-25 14:22:32 -0700887//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800888
889int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700890 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800891 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
892 FILE *out = NULL;
893 int c;
894 int short_output = 0;
895 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530896 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800897 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700898 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800899 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800900 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800901 int print_distortion = 0; // 1=PSNR, 2=SSIM
902 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800903 WebPConfig config;
904 WebPAuxStats stats;
905 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700906
Pascal Massiminod61479f2012-01-20 07:20:56 -0800907 if (!WebPPictureInit(&picture) ||
908 !WebPPictureInit(&original_picture) ||
909 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800910 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700911 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800912 }
913
914 if (argc == 1) {
915 HelpShort();
916 return 0;
917 }
918
919 for (c = 1; c < argc; ++c) {
920 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
921 HelpShort();
922 return 0;
923 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
924 HelpLong();
925 return 0;
926 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
927 out_file = argv[++c];
928 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
929 dump_file = argv[++c];
930 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800931 } else if (!strcmp(argv[c], "-print_ssim")) {
932 config.show_compressed = 1;
933 print_distortion = 2;
934 } else if (!strcmp(argv[c], "-print_psnr")) {
935 config.show_compressed = 1;
936 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800937 } else if (!strcmp(argv[c], "-short")) {
938 short_output++;
939 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700940 picture.width = strtol(argv[++c], NULL, 0);
941 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800942 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700943 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800944 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200945 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530946 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
947 config.alpha_quality = strtol(argv[++c], NULL, 0);
948 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
949 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000950 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
951 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530952 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800953 ++c;
954 if (!strcmp(argv[c], "none")) {
955 config.alpha_filtering = 0;
956 } else if (!strcmp(argv[c], "fast")) {
957 config.alpha_filtering = 1;
958 } else if (!strcmp(argv[c], "best")) {
959 config.alpha_filtering = 2;
960 } else {
961 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
962 goto Error;
963 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530964 } else if (!strcmp(argv[c], "-noalpha")) {
965 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000966 } else if (!strcmp(argv[c], "-lossless")) {
967 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000968 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530969 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
970 ++c;
971 if (!strcmp(argv[c], "photo")) {
972 config.image_hint = WEBP_HINT_PHOTO;
973 } else if (!strcmp(argv[c], "picture")) {
974 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700975 } else if (!strcmp(argv[c], "graph")) {
976 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530977 } else {
978 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
979 goto Error;
980 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800981 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700982 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800983 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200984 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800985 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700986 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800987 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700988 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800989 } else if (!strcmp(argv[c], "-af")) {
990 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700991 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800992 config.filter_type = 1;
993 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700994 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800995 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700996 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800997 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700998 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800999 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001000 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -07001001 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
1002 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001003 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001004 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001005#ifdef WEBP_EXPERIMENTAL_FEATURES
1006 } else if (!strcmp(argv[c], "-444")) {
1007 picture.colorspace = WEBP_YUV444;
1008 } else if (!strcmp(argv[c], "-422")) {
1009 picture.colorspace = WEBP_YUV422;
1010 } else if (!strcmp(argv[c], "-gray")) {
1011 picture.colorspace = WEBP_YUV400;
1012#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001013 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
1014 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -07001015 crop_x = strtol(argv[++c], NULL, 0);
1016 crop_y = strtol(argv[++c], NULL, 0);
1017 crop_w = strtol(argv[++c], NULL, 0);
1018 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001019 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
1020 resize_w = strtol(argv[++c], NULL, 0);
1021 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -07001022#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -07001023 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +00001024 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -07001025#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -07001026 } else if (!strcmp(argv[c], "-version")) {
1027 const int version = WebPGetEncoderVersion();
1028 printf("%d.%d.%d\n",
1029 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1030 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -08001031 } else if (!strcmp(argv[c], "-progress")) {
1032 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001033 } else if (!strcmp(argv[c], "-quiet")) {
1034 quiet = 1;
1035 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
1036 WebPPreset preset;
1037 ++c;
1038 if (!strcmp(argv[c], "default")) {
1039 preset = WEBP_PRESET_DEFAULT;
1040 } else if (!strcmp(argv[c], "photo")) {
1041 preset = WEBP_PRESET_PHOTO;
1042 } else if (!strcmp(argv[c], "picture")) {
1043 preset = WEBP_PRESET_PICTURE;
1044 } else if (!strcmp(argv[c], "drawing")) {
1045 preset = WEBP_PRESET_DRAWING;
1046 } else if (!strcmp(argv[c], "icon")) {
1047 preset = WEBP_PRESET_ICON;
1048 } else if (!strcmp(argv[c], "text")) {
1049 preset = WEBP_PRESET_TEXT;
1050 } else {
1051 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
1052 goto Error;
1053 }
1054 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -07001055 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001056 goto Error;
1057 }
1058 } else if (!strcmp(argv[c], "-v")) {
1059 verbose = 1;
1060 } else if (argv[c][0] == '-') {
1061 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1062 HelpLong();
1063 return -1;
1064 } else {
1065 in_file = argv[c];
1066 }
1067 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001068 if (in_file == NULL) {
1069 fprintf(stderr, "No input file specified!\n");
1070 HelpShort();
1071 goto Error;
1072 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001073
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301074 // Check for unsupported command line options for lossless mode and log
1075 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +00001076 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301077 if (config.target_size > 0 || config.target_PSNR > 0) {
1078 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1079 " for lossless encoding. Ignoring such option(s)!\n");
1080 }
1081 if (config.partition_limit > 0) {
1082 fprintf(stderr, "Partition limit option is not required for lossless"
1083 " encoding. Ignoring this option!\n");
1084 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301085 }
1086
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001087 if (!WebPValidateConfig(&config)) {
1088 fprintf(stderr, "Error! Invalid configuration.\n");
1089 goto Error;
1090 }
1091
Pascal Massimino0744e842011-02-25 12:03:27 -08001092 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001093 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001094 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001095 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -07001096 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001097 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001098 goto Error;
1099 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001100 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1101
Pascal Massimino0744e842011-02-25 12:03:27 -08001102 if (verbose) {
1103 const double time = StopwatchReadAndReset(&stop_watch);
1104 fprintf(stderr, "Time to read input: %.3fs\n", time);
1105 }
1106
1107 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001108 if (out_file) {
1109 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001110 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001111 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1112 goto Error;
1113 } else {
1114 if (!short_output && !quiet) {
1115 fprintf(stderr, "Saving file '%s'\n", out_file);
1116 }
1117 }
1118 picture.writer = MyWriter;
1119 picture.custom_ptr = (void*)out;
1120 } else {
1121 out = NULL;
1122 if (!quiet && !short_output) {
1123 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1124 fprintf(stderr, "be performed, but its results discarded.\n\n");
1125 }
1126 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001127 if (!quiet) {
1128 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001129 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001130 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001131
Pascal Massimino0744e842011-02-25 12:03:27 -08001132 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001133 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001134 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001135 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001136 if (crop != 0) {
1137 // We use self-cropping using a view.
1138 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1139 fprintf(stderr, "Error! Cannot crop picture\n");
1140 goto Error;
1141 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001142 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001143 if ((resize_w | resize_h) > 0) {
1144 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1145 fprintf(stderr, "Error! Cannot resize picture\n");
1146 goto Error;
1147 }
1148 }
1149 if (picture.extra_info_type > 0) {
1150 AllocExtraInfo(&picture);
1151 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001152 if (print_distortion > 0) { // Save original picture for later comparison
1153 WebPPictureCopy(&picture, &original_picture);
1154 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001155 if (!WebPEncode(&config, &picture)) {
1156 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001157 fprintf(stderr, "Error code: %d (%s)\n",
1158 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001159 goto Error;
1160 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001161 if (verbose) {
1162 const double time = StopwatchReadAndReset(&stop_watch);
1163 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1164 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001165
1166 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001167 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001168 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001169 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1170 } else if (!DumpPicture(&picture, dump_file)) {
1171 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1172 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001173 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001174
Pascal Massimino38369c02011-05-04 22:59:51 -07001175 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301176 if (config.lossless) {
1177 PrintExtraInfoLossless(&picture, short_output, in_file);
1178 } else {
1179 PrintExtraInfoLossy(&picture, short_output, in_file);
1180 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001181 }
1182 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1183 float values[5];
1184 WebPPictureDistortion(&picture, &original_picture,
1185 (print_distortion == 1) ? 0 : 1, values);
1186 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1187 (print_distortion == 1) ? "PSNR" : "SSIM",
1188 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001189 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001190 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001191
1192 Error:
1193 free(picture.extra_info);
1194 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001195 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001196 if (out != NULL) {
1197 fclose(out);
1198 }
1199
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001200 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001201}
1202
James Zernc7e86ab2011-08-25 14:22:32 -07001203//------------------------------------------------------------------------------