blob: 35140ee5b115e88e099c7b9e3d5dc98f2845841d [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) {
67 const int uv_width = (pic->width + 1) / 2;
68 const int uv_height = (pic->height + 1) / 2;
69 int y;
70 int ok = 0;
71
72 if (!WebPPictureAlloc(pic)) return ok;
73
74 for (y = 0; y < pic->height; ++y) {
75 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
76 goto End;
77 }
78 }
79 for (y = 0; y < uv_height; ++y) {
80 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
81 goto End;
82 }
83 for (y = 0; y < uv_height; ++y) {
84 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
85 goto End;
86 }
87 ok = 1;
88
89 End:
90 return ok;
91}
92
James Zern31f9dc62011-06-06 17:56:50 -070093#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080094
95#define IFS(fn) \
96 do { \
97 if (SUCCEEDED(hr)) \
98 { \
99 hr = (fn); \
100 if (FAILED(hr) && verbose) \
James Zern974aaff2012-01-24 12:46:46 -0800101 fprintf(stderr, #fn " failed %08x\n", hr); \
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800102 } \
103 } while (0)
104
James Zern902d3e32012-05-08 17:49:39 -0700105// modified version of DEFINE_GUID from guiddef.h.
106#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
107 const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
108
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800109#ifdef __cplusplus
110#define MAKE_REFGUID(x) (x)
111#else
112#define MAKE_REFGUID(x) &(x)
113#endif
114
115static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
116 HRESULT hr = S_OK;
117 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
118 if (FAILED(hr))
James Zern974aaff2012-01-24 12:46:46 -0800119 fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800120 return hr;
121}
122
123static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700124 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800125 HRESULT hr = S_OK;
126 IWICBitmapFrameDecode* pFrame = NULL;
127 IWICFormatConverter* pConverter = NULL;
128 IWICImagingFactory* pFactory = NULL;
129 IWICBitmapDecoder* pDecoder = NULL;
130 IStream* pStream = NULL;
131 UINT frameCount = 0;
132 UINT width, height = 0;
133 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700134 WICPixelFormatGUID srcPixelFormat = { 0 };
135 GUID srcContainerFormat = { 0 };
136 const GUID* alphaContainers[] = {
137 &GUID_ContainerFormatBmp,
138 &GUID_ContainerFormatPng,
139 &GUID_ContainerFormatTiff
140 };
141 int has_alpha = 0;
142 int i, stride;
James Zern902d3e32012-05-08 17:49:39 -0700143 // From Microsoft SDK 7.0a
144 // Create local copies for compatibility when building against earlier
145 // versions of the SDK.
146 WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
147 0x6fddc324, 0x4e03, 0x4bfe,
148 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
149 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
150 0xf5c7ad2d, 0x6a8d, 0x43dd,
151 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
152 WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
153 0x6fddc324, 0x4e03, 0x4bfe,
154 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800155
156 IFS(CoInitialize(NULL));
157 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
158 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
159 (LPVOID*)&pFactory));
160 if (hr == REGDB_E_CLASSNOTREG) {
James Zern974aaff2012-01-24 12:46:46 -0800161 fprintf(stderr,
162 "Couldn't access Windows Imaging Component (are you running "
163 "Windows XP SP3 or newer?). Most formats not available. "
164 "Use -s for the available YUV input.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800165 }
166 // Prepare for image decoding.
167 IFS(OpenInputStream(filename, &pStream));
168 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
169 WICDecodeMetadataCacheOnDemand, &pDecoder));
170 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
171 if (SUCCEEDED(hr) && frameCount == 0) {
James Zern974aaff2012-01-24 12:46:46 -0800172 fprintf(stderr, "No frame found in input file.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800173 hr = E_FAIL;
174 }
175 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700176 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
177 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
178
179 has_alpha = keep_alpha;
180 for (i = 0;
181 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
182 ++i) {
James Zern1d38b252012-05-08 18:09:42 -0700183 if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat),
184 MAKE_REFGUID(*alphaContainers[i]))) {
James Zern3cfe0882011-06-21 18:29:30 -0700185 has_alpha =
James Zern1d38b252012-05-08 18:09:42 -0700186 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
187 MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
188 IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
189 MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
James Zern3cfe0882011-06-21 18:29:30 -0700190 break;
191 }
192 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800193
194 // Prepare for pixel format conversion (if necessary).
195 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
196 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern902d3e32012-05-08 17:49:39 -0700197 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)
198 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB_),
James Zern3cfe0882011-06-21 18:29:30 -0700199 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800200 NULL, 0.0, WICBitmapPaletteTypeCustom));
201
202 // Decode.
203 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700204 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800205 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700206 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800207 if (rgb == NULL)
208 hr = E_OUTOFMEMORY;
209 }
James Zern3cfe0882011-06-21 18:29:30 -0700210 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
211 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800212
213 // WebP conversion.
214 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700215 int ok;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800216 pic->width = width;
217 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700218 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
219 : WebPPictureImportRGB(pic, rgb, stride);
220 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800221 hr = E_FAIL;
222 }
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000223 if (SUCCEEDED(hr)) {
224 if (has_alpha && keep_alpha == 2) {
225 WebPCleanupTransparentArea(pic);
226 }
227 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800228
229 // Cleanup.
230 if (pConverter != NULL) IUnknown_Release(pConverter);
231 if (pFrame != NULL) IUnknown_Release(pFrame);
232 if (pDecoder != NULL) IUnknown_Release(pDecoder);
233 if (pFactory != NULL) IUnknown_Release(pFactory);
234 if (pStream != NULL) IUnknown_Release(pStream);
235 free(rgb);
236 return hr;
237}
238
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700239static int ReadPicture(const char* const filename, WebPPicture* const pic,
240 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800241 int ok;
242 if (pic->width != 0 && pic->height != 0) {
243 // If image size is specified, infer it as YUV format.
244 FILE* in_file = fopen(filename, "rb");
245 if (in_file == NULL) {
246 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
247 return 0;
248 }
249 ok = ReadYUV(in_file, pic);
250 fclose(in_file);
251 } else {
252 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700253 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800254 }
255 if (!ok) {
256 fprintf(stderr, "Error! Could not process file %s\n", filename);
257 }
258 return ok;
259}
260
James Zern31f9dc62011-06-06 17:56:50 -0700261#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800262
263#ifdef WEBP_HAVE_JPEG
264struct my_error_mgr {
265 struct jpeg_error_mgr pub;
266 jmp_buf setjmp_buffer;
267};
268
269static void my_error_exit(j_common_ptr dinfo) {
270 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
271 (*dinfo->err->output_message) (dinfo);
272 longjmp(myerr->setjmp_buffer, 1);
273}
274
275static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
276 int ok = 0;
277 int stride, width, height;
278 uint8_t* rgb = NULL;
279 uint8_t* row_ptr = NULL;
280 struct jpeg_decompress_struct dinfo;
281 struct my_error_mgr jerr;
282 JSAMPARRAY buffer;
283
284 dinfo.err = jpeg_std_error(&jerr.pub);
285 jerr.pub.error_exit = my_error_exit;
286
James Zern04e84cf2011-11-04 15:20:08 -0700287 if (setjmp(jerr.setjmp_buffer)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800288 Error:
289 jpeg_destroy_decompress(&dinfo);
290 goto End;
291 }
292
293 jpeg_create_decompress(&dinfo);
294 jpeg_stdio_src(&dinfo, in_file);
295 jpeg_read_header(&dinfo, TRUE);
296
297 dinfo.out_color_space = JCS_RGB;
298 dinfo.dct_method = JDCT_IFAST;
299 dinfo.do_fancy_upsampling = TRUE;
300
301 jpeg_start_decompress(&dinfo);
302
303 if (dinfo.output_components != 3) {
304 goto Error;
305 }
306
307 width = dinfo.output_width;
308 height = dinfo.output_height;
309 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
310
311 rgb = (uint8_t*)malloc(stride * height);
312 if (rgb == NULL) {
313 goto End;
314 }
315 row_ptr = rgb;
316
317 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
318 JPOOL_IMAGE, stride, 1);
319 if (buffer == NULL) {
320 goto End;
321 }
322
323 while (dinfo.output_scanline < dinfo.output_height) {
324 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
325 goto End;
326 }
327 memcpy(row_ptr, buffer[0], stride);
328 row_ptr += stride;
329 }
330
James Zern04e84cf2011-11-04 15:20:08 -0700331 jpeg_finish_decompress(&dinfo);
332 jpeg_destroy_decompress(&dinfo);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800333
334 // WebP conversion.
335 pic->width = width;
336 pic->height = height;
337 ok = WebPPictureImportRGB(pic, rgb, stride);
338
339 End:
340 if (rgb) {
341 free(rgb);
342 }
343 return ok;
344}
345
346#else
347static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400348 (void)in_file;
349 (void)pic;
James Zern974aaff2012-01-24 12:46:46 -0800350 fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
351 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800352 return 0;
353}
354#endif
355
356#ifdef WEBP_HAVE_PNG
357static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700358 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800359 longjmp(png_jmpbuf(png), 1);
360}
361
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700362static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800363 png_structp png;
364 png_infop info;
365 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700366 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800367 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700368 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800369 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700370 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800371 int stride;
372 uint8_t* rgb = NULL;
373
374 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
375 if (png == NULL) {
376 goto End;
377 }
378
379 png_set_error_fn(png, 0, error_function, NULL);
380 if (setjmp(png_jmpbuf(png))) {
381 Error:
382 png_destroy_read_struct(&png, NULL, NULL);
Pascal Massiminof6a7d752011-11-01 05:18:46 -0700383 free(rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800384 goto End;
385 }
386
387 info = png_create_info_struct(png);
388 if (info == NULL) goto Error;
389
390 png_init_io(png, in_file);
391 png_read_info(png, info);
392 if (!png_get_IHDR(png, info,
393 &width, &height, &bit_depth, &color_type, &interlaced,
394 NULL, NULL)) goto Error;
395
396 png_set_strip_16(png);
397 png_set_packing(png);
398 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
Urvang Joshi372e2b42011-11-10 11:35:54 +0000399 if (color_type == PNG_COLOR_TYPE_GRAY ||
400 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800401 if (bit_depth < 8) {
402 png_set_expand_gray_1_2_4_to_8(png);
403 }
404 png_set_gray_to_rgb(png);
405 }
406 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
407 png_set_tRNS_to_alpha(png);
Pascal Massimino28ad70c2011-09-20 07:51:02 -0700408 has_alpha = 1;
409 } else {
410 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800411 }
412
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700413 if (!keep_alpha) {
414 png_set_strip_alpha(png);
415 has_alpha = 0;
416 }
417
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800418 num_passes = png_set_interlace_handling(png);
419 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700420 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800421 rgb = (uint8_t*)malloc(stride * height);
422 if (rgb == NULL) goto Error;
423 for (p = 0; p < num_passes; ++p) {
424 for (y = 0; y < height; ++y) {
425 png_bytep row = rgb + y * stride;
426 png_read_rows(png, &row, NULL, 1);
427 }
428 }
429 png_read_end(png, info);
430 png_destroy_read_struct(&png, &info, NULL);
431
432 pic->width = width;
433 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700434 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
435 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800436 free(rgb);
437
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000438 if (ok && has_alpha && keep_alpha == 2) {
439 WebPCleanupTransparentArea(pic);
440 }
441
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800442 End:
443 return ok;
444}
445#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700446static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400447 (void)in_file;
448 (void)pic;
449 (void)keep_alpha;
James Zern974aaff2012-01-24 12:46:46 -0800450 fprintf(stderr, "PNG support not compiled. Please install the libpng "
451 "development package before building.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800452 return 0;
453}
454#endif
455
James Zern6f76d242012-07-01 17:55:21 -0700456#ifdef WEBP_HAVE_TIFF
457static int ReadTIFF(const char* const filename,
458 WebPPicture* const pic, int keep_alpha) {
459 TIFF* const tif = TIFFOpen(filename, "r");
460 uint32 width, height;
461 uint32* raster;
462 int ok = 0;
463 int dircount = 1;
464
465 if (tif == NULL) {
466 fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename);
467 return 0;
468 }
469
470 while (TIFFReadDirectory(tif)) ++dircount;
471
472 if (dircount > 1) {
473 fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n"
474 "Only the first will be used, %d will be ignored.\n",
475 dircount - 1);
476 }
477
478 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
479 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
480 raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster));
481 if (raster != NULL) {
482 if (TIFFReadRGBAImageOriented(tif, width, height, raster,
483 ORIENTATION_TOPLEFT, 1)) {
484 const int stride = width * sizeof(*raster);
485 pic->width = width;
486 pic->height = height;
487 // TIFF data is ABGR
488#ifdef __BIG_ENDIAN__
489 TIFFSwabArrayOfLong(raster, width * height);
490#endif
491 ok = keep_alpha
492 ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride)
493 : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride);
494 }
495 _TIFFfree(raster);
496 } else {
497 fprintf(stderr, "Error allocating TIFF RGBA memory!\n");
498 }
499
500 if (ok && keep_alpha == 2) {
501 WebPCleanupTransparentArea(pic);
502 }
503
504 TIFFClose(tif);
505 return ok;
506}
507#else
508static int ReadTIFF(const char* const filename,
509 WebPPicture* const pic, int keep_alpha) {
510 (void)filename;
511 (void)pic;
512 (void)keep_alpha;
513 fprintf(stderr, "TIFF support not compiled. Please install the libtiff "
514 "development package before building.\n");
515 return 0;
516}
517#endif
518
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800519typedef enum {
520 PNG = 0,
521 JPEG,
James Zern6f76d242012-07-01 17:55:21 -0700522 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800523 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800524} InputFileFormat;
525
526static InputFileFormat GetImageType(FILE* in_file) {
527 InputFileFormat format = UNSUPPORTED;
528 unsigned int magic;
529 unsigned char buf[4];
530
531 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
532 (fseek(in_file, 0, SEEK_SET) != 0)) {
533 return format;
534 }
535
536 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
537 if (magic == 0x89504E47U) {
538 format = PNG;
539 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
540 format = JPEG;
James Zern6f76d242012-07-01 17:55:21 -0700541 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
542 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800543 }
544 return format;
545}
546
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700547static int ReadPicture(const char* const filename, WebPPicture* const pic,
548 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800549 int ok = 0;
550 FILE* in_file = fopen(filename, "rb");
551 if (in_file == NULL) {
552 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
553 return ok;
554 }
555
556 if (pic->width == 0 || pic->height == 0) {
557 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
558 const InputFileFormat format = GetImageType(in_file);
559 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700560 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800561 } else if (format == JPEG) {
562 ok = ReadJPEG(in_file, pic);
James Zern6f76d242012-07-01 17:55:21 -0700563 } else if (format == TIFF_) {
564 ok = ReadTIFF(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800565 }
566 } else {
567 // If image size is specified, infer it as YUV format.
568 ok = ReadYUV(in_file, pic);
569 }
570 if (!ok) {
571 fprintf(stderr, "Error! Could not process file %s\n", filename);
572 }
573
574 fclose(in_file);
575 return ok;
576}
577
James Zern31f9dc62011-06-06 17:56:50 -0700578#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800579
580static void AllocExtraInfo(WebPPicture* const pic) {
581 const int mb_w = (pic->width + 15) / 16;
582 const int mb_h = (pic->height + 15) / 16;
583 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
584}
585
586static void PrintByteCount(const int bytes[4], int total_size,
587 int* const totals) {
588 int s;
589 int total = 0;
590 for (s = 0; s < 4; ++s) {
591 fprintf(stderr, "| %7d ", bytes[s]);
592 total += bytes[s];
593 if (totals) totals[s] += bytes[s];
594 }
James Zern04e84cf2011-11-04 15:20:08 -0700595 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800596}
597
598static void PrintPercents(const int counts[4], int total) {
599 int s;
600 for (s = 0; s < 4; ++s) {
601 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
602 }
James Zern04e84cf2011-11-04 15:20:08 -0700603 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800604}
605
606static void PrintValues(const int values[4]) {
607 int s;
608 for (s = 0; s < 4; ++s) {
609 fprintf(stderr, "| %7d ", values[s]);
610 }
James Zern04e84cf2011-11-04 15:20:08 -0700611 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800612}
613
Vikas Arorac4ccab62012-05-09 11:27:46 +0530614static void PrintExtraInfoLossless(const WebPPicture* const pic,
615 int short_output,
616 const char* const file_name) {
617 const WebPAuxStats* const stats = pic->stats;
618 if (short_output) {
619 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
620 } else {
621 fprintf(stderr, "File: %s\n", file_name);
622 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
623 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
624 }
625}
626
627static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
628 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800629 const WebPAuxStats* const stats = pic->stats;
630 if (short_output) {
631 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700632 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800633 const int num_i4 = stats->block_count[0];
634 const int num_i16 = stats->block_count[1];
635 const int num_skip = stats->block_count[2];
636 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800637 fprintf(stderr, "File: %s\n", file_name);
638 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700639 pic->width, pic->height,
640 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800641 fprintf(stderr, "Output: "
642 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800643 stats->coded_size,
644 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
645 if (total > 0) {
646 int totals[4] = { 0, 0, 0, 0 };
647 fprintf(stderr, "block count: intra4: %d\n"
648 " intra16: %d (-> %.2f%%)\n",
649 num_i4, num_i16, 100.f * num_i16 / total);
650 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
651 num_skip, 100.f * num_skip / total);
652 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
653 " mode-partition: %6d (%.1f%%)\n",
654 stats->header_bytes[0],
655 100.f * stats->header_bytes[0] / stats->coded_size,
656 stats->header_bytes[1],
657 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700658 if (stats->alpha_data_size) {
659 fprintf(stderr, " transparency: %6d\n",
660 stats->alpha_data_size);
661 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700662 if (stats->layer_data_size) {
663 fprintf(stderr, " enhancement: %6d\n",
664 stats->layer_data_size);
665 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800666 fprintf(stderr, " Residuals bytes "
667 "|segment 1|segment 2|segment 3"
668 "|segment 4| total\n");
669 fprintf(stderr, " intra4-coeffs: ");
670 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
671 fprintf(stderr, " intra16-coeffs: ");
672 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
673 fprintf(stderr, " chroma coeffs: ");
674 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
675 fprintf(stderr, " macroblocks: ");
676 PrintPercents(stats->segment_size, total);
677 fprintf(stderr, " quantizer: ");
678 PrintValues(stats->segment_quant);
679 fprintf(stderr, " filter level: ");
680 PrintValues(stats->segment_level);
681 fprintf(stderr, "------------------+---------");
682 fprintf(stderr, "+---------+---------+---------+-----------------\n");
683 fprintf(stderr, " segments total: ");
684 PrintByteCount(totals, stats->coded_size, NULL);
685 }
686 }
687 if (pic->extra_info) {
688 const int mb_w = (pic->width + 15) / 16;
689 const int mb_h = (pic->height + 15) / 16;
690 const int type = pic->extra_info_type;
691 int x, y;
692 for (y = 0; y < mb_h; ++y) {
693 for (x = 0; x < mb_w; ++x) {
694 const int c = pic->extra_info[x + y * mb_w];
695 if (type == 1) { // intra4/intra16
696 printf("%c", "+."[c]);
697 } else if (type == 2) { // segments
698 printf("%c", ".-*X"[c]);
699 } else if (type == 3) { // quantizers
700 printf("%.2d ", c);
701 } else if (type == 6 || type == 7) {
702 printf("%3d ", c);
703 } else {
704 printf("0x%.2x ", c);
705 }
706 }
707 printf("\n");
708 }
709 }
710}
711
James Zernc7e86ab2011-08-25 14:22:32 -0700712//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713
714static int MyWriter(const uint8_t* data, size_t data_size,
715 const WebPPicture* const pic) {
716 FILE* const out = (FILE*)pic->custom_ptr;
717 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
718}
719
720// Dumps a picture as a PGM file using the IMC4 layout.
721static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
722 int y;
723 const int uv_width = (picture->width + 1) / 2;
724 const int uv_height = (picture->height + 1) / 2;
725 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700726 const int alpha_height =
727 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700728 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800729 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800730 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800731 fprintf(f, "P5\n%d %d\n255\n", stride, height);
732 for (y = 0; y < picture->height; ++y) {
733 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
734 return 0;
735 if (picture->width & 1) fputc(0, f); // pad
736 }
737 for (y = 0; y < uv_height; ++y) {
738 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
739 return 0;
740 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
741 return 0;
742 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700743 for (y = 0; y < alpha_height; ++y) {
744 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
745 return 0;
746 if (picture->width & 1) fputc(0, f); // pad
747 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800748 fclose(f);
749 return 1;
750}
751
James Zernc7e86ab2011-08-25 14:22:32 -0700752//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753
Pascal Massimino30971c92011-12-01 02:24:50 -0800754static int ProgressReport(int percent, const WebPPicture* const picture) {
755 printf("[%s]: %3d %% \r",
756 (char*)picture->stats->user_data, percent);
757 fflush(stdout);
758 return 1; // all ok
759}
760
761//------------------------------------------------------------------------------
762
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700763static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 printf("Usage:\n\n");
765 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
766 printf("where quality is between 0 (poor) to 100 (very good).\n");
767 printf("Typical value is around 80.\n\n");
768 printf("Try -longhelp for an exhaustive list of advanced options.\n");
769}
770
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700771static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800772 printf("Usage:\n");
773 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
774 printf("If input size (-s) for an image is not specified, "
775 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700776#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800777 printf("Windows builds can take as input any of the files handled by WIC\n");
778#endif
779 printf("options:\n");
780 printf(" -h / -help ............ short help\n");
781 printf(" -H / -longhelp ........ long help\n");
782 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530783 printf(" -alpha_q <int> ......... Transparency-compression quality "
784 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800785 printf(" -preset <string> ....... Preset setting, one of:\n");
786 printf(" default, photo, picture,\n");
787 printf(" drawing, icon, text\n");
788 printf(" -preset must come first, as it overwrites other parameters.");
789 printf("\n");
790 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
791 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700792 printf(" -size <int> ............ Target size (in bytes)\n");
793 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800794 printf("\n");
795 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
796 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
797 printf(" -f <int> ............... filter strength (0=off..100)\n");
798 printf(" -sharpness <int> ....... "
799 "filter sharpness (0:most .. 7:least sharp)\n");
800 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700801 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
802 printf(" "
803 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800804 printf(" -pass <int> ............ analysis pass number (1..10)\n");
805 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700806 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
807#ifdef WEBP_EXPERIMENTAL_FEATURES
808 printf(" -444 / -422 / -gray ..... Change colorspace\n");
809#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800810 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800811 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
812 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800813 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530814 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800815 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
816 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000817 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530818 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530819 printf(" -lossless .............. Encode image losslessly.\n");
820 printf(" -hint <string> ......... Specify image characteristics hint.\n");
821 printf(" One of: photo or picture\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700822
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 printf("\n");
824 printf(" -short ................. condense printed message\n");
825 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700826 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700827#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700828 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700829#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800830 printf(" -v ..................... verbose, e.g. print encoding/decoding "
831 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800832 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800833 printf("\n");
834 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 printf(" -af .................... auto-adjust filter strength.\n");
836 printf(" -pre <int> ............. pre-processing filter\n");
837 printf("\n");
838}
839
James Zernc7e86ab2011-08-25 14:22:32 -0700840//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700841// Error messages
842
843static const char* const kErrorMessages[] = {
844 "OK",
845 "OUT_OF_MEMORY: Out of memory allocating objects",
846 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
847 "NULL_PARAMETER: NULL parameter passed to function",
848 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700849 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
850 "allowed is 16383 pixels.",
851 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
852 "To reduce the size of this partition, try using less segments "
853 "with the -segments option, and eventually reduce the number of "
854 "header bits using -partition_limit. More details are available "
855 "in the manual (`man cwebp`)",
856 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800857 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800858 "FILE_TOO_BIG: File would be too big to fit in 4G",
859 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700860};
861
James Zernc7e86ab2011-08-25 14:22:32 -0700862//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800863
864int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700865 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800866 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
867 FILE *out = NULL;
868 int c;
869 int short_output = 0;
870 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530871 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800872 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700873 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800874 int show_progress = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800875 WebPPicture picture;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800876 int print_distortion = 0; // 1=PSNR, 2=SSIM
877 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800878 WebPConfig config;
879 WebPAuxStats stats;
880 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700881
Pascal Massiminod61479f2012-01-20 07:20:56 -0800882 if (!WebPPictureInit(&picture) ||
883 !WebPPictureInit(&original_picture) ||
884 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885 fprintf(stderr, "Error! Version mismatch!\n");
886 goto Error;
887 }
888
889 if (argc == 1) {
890 HelpShort();
891 return 0;
892 }
893
894 for (c = 1; c < argc; ++c) {
895 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
896 HelpShort();
897 return 0;
898 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
899 HelpLong();
900 return 0;
901 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
902 out_file = argv[++c];
903 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
904 dump_file = argv[++c];
905 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800906 } else if (!strcmp(argv[c], "-print_ssim")) {
907 config.show_compressed = 1;
908 print_distortion = 2;
909 } else if (!strcmp(argv[c], "-print_psnr")) {
910 config.show_compressed = 1;
911 print_distortion = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800912 } else if (!strcmp(argv[c], "-short")) {
913 short_output++;
914 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700915 picture.width = strtol(argv[++c], NULL, 0);
916 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800917 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700918 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800919 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200920 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530921 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
922 config.alpha_quality = strtol(argv[++c], NULL, 0);
923 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
924 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000925 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
926 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530927 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800928 ++c;
929 if (!strcmp(argv[c], "none")) {
930 config.alpha_filtering = 0;
931 } else if (!strcmp(argv[c], "fast")) {
932 config.alpha_filtering = 1;
933 } else if (!strcmp(argv[c], "best")) {
934 config.alpha_filtering = 2;
935 } else {
936 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
937 goto Error;
938 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530939 } else if (!strcmp(argv[c], "-noalpha")) {
940 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000941 } else if (!strcmp(argv[c], "-lossless")) {
942 config.lossless = 1;
943 picture.use_argb_input = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530944 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
945 ++c;
946 if (!strcmp(argv[c], "photo")) {
947 config.image_hint = WEBP_HINT_PHOTO;
948 } else if (!strcmp(argv[c], "picture")) {
949 config.image_hint = WEBP_HINT_PICTURE;
950 } else {
951 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
952 goto Error;
953 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800954 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700955 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800956 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200957 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800958 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700959 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800960 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700961 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800962 } else if (!strcmp(argv[c], "-af")) {
963 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700964 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800965 config.filter_type = 1;
966 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700967 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800968 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700969 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800970 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700971 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800972 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700973 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700974 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
975 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800976 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700977 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700978#ifdef WEBP_EXPERIMENTAL_FEATURES
979 } else if (!strcmp(argv[c], "-444")) {
980 picture.colorspace = WEBP_YUV444;
981 } else if (!strcmp(argv[c], "-422")) {
982 picture.colorspace = WEBP_YUV422;
983 } else if (!strcmp(argv[c], "-gray")) {
984 picture.colorspace = WEBP_YUV400;
985#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800986 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
987 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700988 crop_x = strtol(argv[++c], NULL, 0);
989 crop_y = strtol(argv[++c], NULL, 0);
990 crop_w = strtol(argv[++c], NULL, 0);
991 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700992 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
993 resize_w = strtol(argv[++c], NULL, 0);
994 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700995#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700996 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000997 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700998#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700999 } else if (!strcmp(argv[c], "-version")) {
1000 const int version = WebPGetEncoderVersion();
1001 printf("%d.%d.%d\n",
1002 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1003 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -08001004 } else if (!strcmp(argv[c], "-progress")) {
1005 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001006 } else if (!strcmp(argv[c], "-quiet")) {
1007 quiet = 1;
1008 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
1009 WebPPreset preset;
1010 ++c;
1011 if (!strcmp(argv[c], "default")) {
1012 preset = WEBP_PRESET_DEFAULT;
1013 } else if (!strcmp(argv[c], "photo")) {
1014 preset = WEBP_PRESET_PHOTO;
1015 } else if (!strcmp(argv[c], "picture")) {
1016 preset = WEBP_PRESET_PICTURE;
1017 } else if (!strcmp(argv[c], "drawing")) {
1018 preset = WEBP_PRESET_DRAWING;
1019 } else if (!strcmp(argv[c], "icon")) {
1020 preset = WEBP_PRESET_ICON;
1021 } else if (!strcmp(argv[c], "text")) {
1022 preset = WEBP_PRESET_TEXT;
1023 } else {
1024 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
1025 goto Error;
1026 }
1027 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -07001028 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001029 goto Error;
1030 }
1031 } else if (!strcmp(argv[c], "-v")) {
1032 verbose = 1;
1033 } else if (argv[c][0] == '-') {
1034 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
1035 HelpLong();
1036 return -1;
1037 } else {
1038 in_file = argv[c];
1039 }
1040 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001041 if (in_file == NULL) {
1042 fprintf(stderr, "No input file specified!\n");
1043 HelpShort();
1044 goto Error;
1045 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001046
Vikas Arorab7fb0ed2012-06-20 09:02:25 +05301047 // Check for unsupported command line options for lossless mode and log
1048 // warning for such options.
1049 if (config.lossless == 1) {
1050 if (config.target_size > 0 || config.target_PSNR > 0) {
1051 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
1052 " for lossless encoding. Ignoring such option(s)!\n");
1053 }
1054 if (config.partition_limit > 0) {
1055 fprintf(stderr, "Partition limit option is not required for lossless"
1056 " encoding. Ignoring this option!\n");
1057 }
1058 if (show_progress) {
1059 fprintf(stderr, "Progress reporting option is not supported for lossless"
1060 " encoding. Ignoring this option!\n");
1061 }
1062 }
1063
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001064 if (!WebPValidateConfig(&config)) {
1065 fprintf(stderr, "Error! Invalid configuration.\n");
1066 goto Error;
1067 }
1068
Pascal Massimino0744e842011-02-25 12:03:27 -08001069 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -07001070 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -08001071 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001072 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -07001073 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -08001074 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001075 goto Error;
1076 }
Pascal Massimino30971c92011-12-01 02:24:50 -08001077 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
1078
Pascal Massimino0744e842011-02-25 12:03:27 -08001079 if (verbose) {
1080 const double time = StopwatchReadAndReset(&stop_watch);
1081 fprintf(stderr, "Time to read input: %.3fs\n", time);
1082 }
1083
1084 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001085 if (out_file) {
1086 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001087 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001088 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1089 goto Error;
1090 } else {
1091 if (!short_output && !quiet) {
1092 fprintf(stderr, "Saving file '%s'\n", out_file);
1093 }
1094 }
1095 picture.writer = MyWriter;
1096 picture.custom_ptr = (void*)out;
1097 } else {
1098 out = NULL;
1099 if (!quiet && !short_output) {
1100 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1101 fprintf(stderr, "be performed, but its results discarded.\n\n");
1102 }
1103 }
1104 picture.stats = &stats;
Pascal Massimino30971c92011-12-01 02:24:50 -08001105 stats.user_data = (void*)in_file;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001106
Pascal Massimino0744e842011-02-25 12:03:27 -08001107 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001108 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001109 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001110 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001111 if (crop != 0) {
1112 // We use self-cropping using a view.
1113 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1114 fprintf(stderr, "Error! Cannot crop picture\n");
1115 goto Error;
1116 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001117 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001118 if ((resize_w | resize_h) > 0) {
1119 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1120 fprintf(stderr, "Error! Cannot resize picture\n");
1121 goto Error;
1122 }
1123 }
1124 if (picture.extra_info_type > 0) {
1125 AllocExtraInfo(&picture);
1126 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001127 if (print_distortion > 0) { // Save original picture for later comparison
1128 WebPPictureCopy(&picture, &original_picture);
1129 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001130 if (!WebPEncode(&config, &picture)) {
1131 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001132 fprintf(stderr, "Error code: %d (%s)\n",
1133 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001134 goto Error;
1135 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001136 if (verbose) {
1137 const double time = StopwatchReadAndReset(&stop_watch);
1138 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
1139 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001140
1141 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001142 if (dump_file) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001143 if (picture.use_argb_input) {
1144 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1145 } else if (!DumpPicture(&picture, dump_file)) {
1146 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1147 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001148 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001149
Pascal Massimino38369c02011-05-04 22:59:51 -07001150 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +05301151 if (config.lossless) {
1152 PrintExtraInfoLossless(&picture, short_output, in_file);
1153 } else {
1154 PrintExtraInfoLossy(&picture, short_output, in_file);
1155 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001156 }
1157 if (!quiet && !short_output && print_distortion > 0) { // print distortion
1158 float values[5];
1159 WebPPictureDistortion(&picture, &original_picture,
1160 (print_distortion == 1) ? 0 : 1, values);
1161 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1162 (print_distortion == 1) ? "PSNR" : "SSIM",
1163 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -07001164 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001165 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001166
1167 Error:
1168 free(picture.extra_info);
1169 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001170 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001171 if (out != NULL) {
1172 fclose(out);
1173 }
1174
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001175 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001176}
1177
James Zernc7e86ab2011-08-25 14:22:32 -07001178//------------------------------------------------------------------------------