blob: bc29880fe6adf0481a56d8909d39dd1c906dc2b4 [file] [log] [blame]
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001// Copyright 2011 Google Inc.
2//
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 Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041
42#ifndef GUID_WICPixelFormat24bppRGB
43// From Microsoft SDK 7.0a
44DEFINE_GUID(GUID_WICPixelFormat24bppRGB,
45 0x6fddc324, 0x4e03, 0x4bfe, 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046#endif
James Zern3cfe0882011-06-21 18:29:30 -070047#ifndef GUID_WICPixelFormat32bppRGBA
48DEFINE_GUID(GUID_WICPixelFormat32bppRGBA,
49 0xf5c7ad2d, 0x6a8d, 0x43dd, 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
50#endif
James Zern31f9dc62011-06-06 17:56:50 -070051#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080052
53
54#include "webp/encode.h"
55#include "stopwatch.h"
Pascal Massiminoa11009d2011-06-10 15:10:18 -070056extern void* VP8EncGetCPUInfo; // opaque forward declaration.
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070057
Pascal Massiminof61d14a2011-02-18 23:33:46 -080058//-----------------------------------------------------------------------------
59
60static int verbose = 0;
61
62static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
63 const int uv_width = (pic->width + 1) / 2;
64 const int uv_height = (pic->height + 1) / 2;
65 int y;
66 int ok = 0;
67
68 if (!WebPPictureAlloc(pic)) return ok;
69
70 for (y = 0; y < pic->height; ++y) {
71 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
72 goto End;
73 }
74 }
75 for (y = 0; y < uv_height; ++y) {
76 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
77 goto End;
78 }
79 for (y = 0; y < uv_height; ++y) {
80 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
81 goto End;
82 }
83 ok = 1;
84
85 End:
86 return ok;
87}
88
James Zern31f9dc62011-06-06 17:56:50 -070089#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080090
91#define IFS(fn) \
92 do { \
93 if (SUCCEEDED(hr)) \
94 { \
95 hr = (fn); \
96 if (FAILED(hr) && verbose) \
97 printf(#fn " failed %08x\n", hr); \
98 } \
99 } while (0)
100
101#ifdef __cplusplus
102#define MAKE_REFGUID(x) (x)
103#else
104#define MAKE_REFGUID(x) &(x)
105#endif
106
107static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
108 HRESULT hr = S_OK;
109 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
110 if (FAILED(hr))
111 printf("Error opening input file %s (%08x)\n", filename, hr);
112 return hr;
113}
114
115static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700116 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800117 HRESULT hr = S_OK;
118 IWICBitmapFrameDecode* pFrame = NULL;
119 IWICFormatConverter* pConverter = NULL;
120 IWICImagingFactory* pFactory = NULL;
121 IWICBitmapDecoder* pDecoder = NULL;
122 IStream* pStream = NULL;
123 UINT frameCount = 0;
124 UINT width, height = 0;
125 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700126 WICPixelFormatGUID srcPixelFormat = { 0 };
127 GUID srcContainerFormat = { 0 };
128 const GUID* alphaContainers[] = {
129 &GUID_ContainerFormatBmp,
130 &GUID_ContainerFormatPng,
131 &GUID_ContainerFormatTiff
132 };
133 int has_alpha = 0;
134 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800135
136 IFS(CoInitialize(NULL));
137 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
138 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
139 (LPVOID*)&pFactory));
140 if (hr == REGDB_E_CLASSNOTREG) {
141 printf("Couldn't access Windows Imaging Component (are you running \n");
142 printf("Windows XP SP3 or newer?). Most formats not available.\n");
143 printf("Use -s for the available YUV input.\n");
144 }
145 // Prepare for image decoding.
146 IFS(OpenInputStream(filename, &pStream));
147 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
148 WICDecodeMetadataCacheOnDemand, &pDecoder));
149 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
150 if (SUCCEEDED(hr) && frameCount == 0) {
151 printf("No frame found in input file.\n");
152 hr = E_FAIL;
153 }
154 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700155 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
156 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
157
158 has_alpha = keep_alpha;
159 for (i = 0;
160 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
161 ++i) {
162 if (IsEqualGUID(&srcContainerFormat, alphaContainers[i])) {
163 has_alpha =
164 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppRGBA) ||
165 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppBGRA);
166 break;
167 }
168 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800169
170 // Prepare for pixel format conversion (if necessary).
171 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
172 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern3cfe0882011-06-21 18:29:30 -0700173 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA)
174 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB),
175 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800176 NULL, 0.0, WICBitmapPaletteTypeCustom));
177
178 // Decode.
179 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700180 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800181 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700182 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800183 if (rgb == NULL)
184 hr = E_OUTOFMEMORY;
185 }
James Zern3cfe0882011-06-21 18:29:30 -0700186 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
187 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800188
189 // WebP conversion.
190 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700191 int ok;
192#ifdef WEBP_EXPERIMENTAL_FEATURES
193 if (has_alpha) {
194 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
195 }
196#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800197 pic->width = width;
198 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700199 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
200 : WebPPictureImportRGB(pic, rgb, stride);
201 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800202 hr = E_FAIL;
203 }
204
205 // Cleanup.
206 if (pConverter != NULL) IUnknown_Release(pConverter);
207 if (pFrame != NULL) IUnknown_Release(pFrame);
208 if (pDecoder != NULL) IUnknown_Release(pDecoder);
209 if (pFactory != NULL) IUnknown_Release(pFactory);
210 if (pStream != NULL) IUnknown_Release(pStream);
211 free(rgb);
212 return hr;
213}
214
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700215static int ReadPicture(const char* const filename, WebPPicture* const pic,
216 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800217 int ok;
218 if (pic->width != 0 && pic->height != 0) {
219 // If image size is specified, infer it as YUV format.
220 FILE* in_file = fopen(filename, "rb");
221 if (in_file == NULL) {
222 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
223 return 0;
224 }
225 ok = ReadYUV(in_file, pic);
226 fclose(in_file);
227 } else {
228 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700229 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800230 }
231 if (!ok) {
232 fprintf(stderr, "Error! Could not process file %s\n", filename);
233 }
234 return ok;
235}
236
James Zern31f9dc62011-06-06 17:56:50 -0700237#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800238
239#ifdef WEBP_HAVE_JPEG
240struct my_error_mgr {
241 struct jpeg_error_mgr pub;
242 jmp_buf setjmp_buffer;
243};
244
245static void my_error_exit(j_common_ptr dinfo) {
246 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
247 (*dinfo->err->output_message) (dinfo);
248 longjmp(myerr->setjmp_buffer, 1);
249}
250
251static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
252 int ok = 0;
253 int stride, width, height;
254 uint8_t* rgb = NULL;
255 uint8_t* row_ptr = NULL;
256 struct jpeg_decompress_struct dinfo;
257 struct my_error_mgr jerr;
258 JSAMPARRAY buffer;
259
260 dinfo.err = jpeg_std_error(&jerr.pub);
261 jerr.pub.error_exit = my_error_exit;
262
263 if (setjmp (jerr.setjmp_buffer)) {
264 Error:
265 jpeg_destroy_decompress(&dinfo);
266 goto End;
267 }
268
269 jpeg_create_decompress(&dinfo);
270 jpeg_stdio_src(&dinfo, in_file);
271 jpeg_read_header(&dinfo, TRUE);
272
273 dinfo.out_color_space = JCS_RGB;
274 dinfo.dct_method = JDCT_IFAST;
275 dinfo.do_fancy_upsampling = TRUE;
276
277 jpeg_start_decompress(&dinfo);
278
279 if (dinfo.output_components != 3) {
280 goto Error;
281 }
282
283 width = dinfo.output_width;
284 height = dinfo.output_height;
285 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
286
287 rgb = (uint8_t*)malloc(stride * height);
288 if (rgb == NULL) {
289 goto End;
290 }
291 row_ptr = rgb;
292
293 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
294 JPOOL_IMAGE, stride, 1);
295 if (buffer == NULL) {
296 goto End;
297 }
298
299 while (dinfo.output_scanline < dinfo.output_height) {
300 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
301 goto End;
302 }
303 memcpy(row_ptr, buffer[0], stride);
304 row_ptr += stride;
305 }
306
307 jpeg_finish_decompress (&dinfo);
308 jpeg_destroy_decompress (&dinfo);
309
310 // WebP conversion.
311 pic->width = width;
312 pic->height = height;
313 ok = WebPPictureImportRGB(pic, rgb, stride);
314
315 End:
316 if (rgb) {
317 free(rgb);
318 }
319 return ok;
320}
321
322#else
323static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400324 (void)in_file;
325 (void)pic;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800326 printf("JPEG support not compiled. Please install the libjpeg development "
327 "package before building.\n");
328 return 0;
329}
330#endif
331
332#ifdef WEBP_HAVE_PNG
333static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700334 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800335 longjmp(png_jmpbuf(png), 1);
336}
337
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700338static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800339 png_structp png;
340 png_infop info;
341 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700342 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800343 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700344 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800345 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700346 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800347 int stride;
348 uint8_t* rgb = NULL;
349
350 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
351 if (png == NULL) {
352 goto End;
353 }
354
355 png_set_error_fn(png, 0, error_function, NULL);
356 if (setjmp(png_jmpbuf(png))) {
357 Error:
358 png_destroy_read_struct(&png, NULL, NULL);
359 if (rgb) free(rgb);
360 goto End;
361 }
362
363 info = png_create_info_struct(png);
364 if (info == NULL) goto Error;
365
366 png_init_io(png, in_file);
367 png_read_info(png, info);
368 if (!png_get_IHDR(png, info,
369 &width, &height, &bit_depth, &color_type, &interlaced,
370 NULL, NULL)) goto Error;
371
372 png_set_strip_16(png);
373 png_set_packing(png);
374 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
375 if (color_type == PNG_COLOR_TYPE_GRAY) {
376 if (bit_depth < 8) {
377 png_set_expand_gray_1_2_4_to_8(png);
378 }
379 png_set_gray_to_rgb(png);
380 }
381 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
382 png_set_tRNS_to_alpha(png);
383 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700384 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800385
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700386 if (!keep_alpha) {
387 png_set_strip_alpha(png);
388 has_alpha = 0;
389 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700390#ifdef WEBP_EXPERIMENTAL_FEATURES
391 if (has_alpha) {
392 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
393 }
394#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700395
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800396 num_passes = png_set_interlace_handling(png);
397 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700398 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800399 rgb = (uint8_t*)malloc(stride * height);
400 if (rgb == NULL) goto Error;
401 for (p = 0; p < num_passes; ++p) {
402 for (y = 0; y < height; ++y) {
403 png_bytep row = rgb + y * stride;
404 png_read_rows(png, &row, NULL, 1);
405 }
406 }
407 png_read_end(png, info);
408 png_destroy_read_struct(&png, &info, NULL);
409
410 pic->width = width;
411 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700412 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
413 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800414 free(rgb);
415
416 End:
417 return ok;
418}
419#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700420static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400421 (void)in_file;
422 (void)pic;
423 (void)keep_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800424 printf("PNG support not compiled. Please install the libpng development "
425 "package before building.\n");
426 return 0;
427}
428#endif
429
430typedef enum {
431 PNG = 0,
432 JPEG,
433 UNSUPPORTED,
434} InputFileFormat;
435
436static InputFileFormat GetImageType(FILE* in_file) {
437 InputFileFormat format = UNSUPPORTED;
438 unsigned int magic;
439 unsigned char buf[4];
440
441 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
442 (fseek(in_file, 0, SEEK_SET) != 0)) {
443 return format;
444 }
445
446 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
447 if (magic == 0x89504E47U) {
448 format = PNG;
449 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
450 format = JPEG;
451 }
452 return format;
453}
454
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700455static int ReadPicture(const char* const filename, WebPPicture* const pic,
456 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800457 int ok = 0;
458 FILE* in_file = fopen(filename, "rb");
459 if (in_file == NULL) {
460 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
461 return ok;
462 }
463
464 if (pic->width == 0 || pic->height == 0) {
465 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
466 const InputFileFormat format = GetImageType(in_file);
467 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700468 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800469 } else if (format == JPEG) {
470 ok = ReadJPEG(in_file, pic);
471 }
472 } else {
473 // If image size is specified, infer it as YUV format.
474 ok = ReadYUV(in_file, pic);
475 }
476 if (!ok) {
477 fprintf(stderr, "Error! Could not process file %s\n", filename);
478 }
479
480 fclose(in_file);
481 return ok;
482}
483
James Zern31f9dc62011-06-06 17:56:50 -0700484#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800485
486static void AllocExtraInfo(WebPPicture* const pic) {
487 const int mb_w = (pic->width + 15) / 16;
488 const int mb_h = (pic->height + 15) / 16;
489 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
490}
491
492static void PrintByteCount(const int bytes[4], int total_size,
493 int* const totals) {
494 int s;
495 int total = 0;
496 for (s = 0; s < 4; ++s) {
497 fprintf(stderr, "| %7d ", bytes[s]);
498 total += bytes[s];
499 if (totals) totals[s] += bytes[s];
500 }
501 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
502}
503
504static void PrintPercents(const int counts[4], int total) {
505 int s;
506 for (s = 0; s < 4; ++s) {
507 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
508 }
509 fprintf(stderr,"| %7d\n", total);
510}
511
512static void PrintValues(const int values[4]) {
513 int s;
514 for (s = 0; s < 4; ++s) {
515 fprintf(stderr, "| %7d ", values[s]);
516 }
517 fprintf(stderr,"|\n");
518}
519
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700520static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800521 const WebPAuxStats* const stats = pic->stats;
522 if (short_output) {
523 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
524 } else{
525 const int num_i4 = stats->block_count[0];
526 const int num_i16 = stats->block_count[1];
527 const int num_skip = stats->block_count[2];
528 const int total = num_i4 + num_i16;
529 fprintf(stderr,
530 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
531 stats->coded_size,
532 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
533 if (total > 0) {
534 int totals[4] = { 0, 0, 0, 0 };
535 fprintf(stderr, "block count: intra4: %d\n"
536 " intra16: %d (-> %.2f%%)\n",
537 num_i4, num_i16, 100.f * num_i16 / total);
538 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
539 num_skip, 100.f * num_skip / total);
540 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
541 " mode-partition: %6d (%.1f%%)\n",
542 stats->header_bytes[0],
543 100.f * stats->header_bytes[0] / stats->coded_size,
544 stats->header_bytes[1],
545 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700546 if (stats->alpha_data_size) {
547 fprintf(stderr, " transparency: %6d\n",
548 stats->alpha_data_size);
549 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700550 if (stats->layer_data_size) {
551 fprintf(stderr, " enhancement: %6d\n",
552 stats->layer_data_size);
553 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800554 fprintf(stderr, " Residuals bytes "
555 "|segment 1|segment 2|segment 3"
556 "|segment 4| total\n");
557 fprintf(stderr, " intra4-coeffs: ");
558 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
559 fprintf(stderr, " intra16-coeffs: ");
560 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
561 fprintf(stderr, " chroma coeffs: ");
562 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
563 fprintf(stderr, " macroblocks: ");
564 PrintPercents(stats->segment_size, total);
565 fprintf(stderr, " quantizer: ");
566 PrintValues(stats->segment_quant);
567 fprintf(stderr, " filter level: ");
568 PrintValues(stats->segment_level);
569 fprintf(stderr, "------------------+---------");
570 fprintf(stderr, "+---------+---------+---------+-----------------\n");
571 fprintf(stderr, " segments total: ");
572 PrintByteCount(totals, stats->coded_size, NULL);
573 }
574 }
575 if (pic->extra_info) {
576 const int mb_w = (pic->width + 15) / 16;
577 const int mb_h = (pic->height + 15) / 16;
578 const int type = pic->extra_info_type;
579 int x, y;
580 for (y = 0; y < mb_h; ++y) {
581 for (x = 0; x < mb_w; ++x) {
582 const int c = pic->extra_info[x + y * mb_w];
583 if (type == 1) { // intra4/intra16
584 printf("%c", "+."[c]);
585 } else if (type == 2) { // segments
586 printf("%c", ".-*X"[c]);
587 } else if (type == 3) { // quantizers
588 printf("%.2d ", c);
589 } else if (type == 6 || type == 7) {
590 printf("%3d ", c);
591 } else {
592 printf("0x%.2x ", c);
593 }
594 }
595 printf("\n");
596 }
597 }
598}
599
600//-----------------------------------------------------------------------------
601
602static int MyWriter(const uint8_t* data, size_t data_size,
603 const WebPPicture* const pic) {
604 FILE* const out = (FILE*)pic->custom_ptr;
605 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
606}
607
608// Dumps a picture as a PGM file using the IMC4 layout.
609static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
610 int y;
611 const int uv_width = (picture->width + 1) / 2;
612 const int uv_height = (picture->height + 1) / 2;
613 const int stride = (picture->width + 1) & ~1;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700614 const int alpha_height = picture->a ? picture->height : 0;
615 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800616 FILE* const f = fopen(PGM_name, "wb");
617 if (!f) return 0;
618 fprintf(f, "P5\n%d %d\n255\n", stride, height);
619 for (y = 0; y < picture->height; ++y) {
620 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
621 return 0;
622 if (picture->width & 1) fputc(0, f); // pad
623 }
624 for (y = 0; y < uv_height; ++y) {
625 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
626 return 0;
627 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
628 return 0;
629 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700630 for (y = 0; y < alpha_height; ++y) {
631 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
632 return 0;
633 if (picture->width & 1) fputc(0, f); // pad
634 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 fclose(f);
636 return 1;
637}
638
639//-----------------------------------------------------------------------------
640
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700641static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800642 printf("Usage:\n\n");
643 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
644 printf("where quality is between 0 (poor) to 100 (very good).\n");
645 printf("Typical value is around 80.\n\n");
646 printf("Try -longhelp for an exhaustive list of advanced options.\n");
647}
648
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700649static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800650 printf("Usage:\n");
651 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
652 printf("If input size (-s) for an image is not specified, "
653 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700654#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800655 printf("Windows builds can take as input any of the files handled by WIC\n");
656#endif
657 printf("options:\n");
658 printf(" -h / -help ............ short help\n");
659 printf(" -H / -longhelp ........ long help\n");
660 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
661 printf(" -preset <string> ....... Preset setting, one of:\n");
662 printf(" default, photo, picture,\n");
663 printf(" drawing, icon, text\n");
664 printf(" -preset must come first, as it overwrites other parameters.");
665 printf("\n");
666 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
667 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700668 printf(" -size <int> ............ Target size (in bytes)\n");
669 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800670 printf("\n");
671 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
672 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
673 printf(" -f <int> ............... filter strength (0=off..100)\n");
674 printf(" -sharpness <int> ....... "
675 "filter sharpness (0:most .. 7:least sharp)\n");
676 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700677 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
678 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800679 printf(" -pass <int> ............ analysis pass number (1..10)\n");
680 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700681 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
682#ifdef WEBP_EXPERIMENTAL_FEATURES
683 printf(" -444 / -422 / -gray ..... Change colorspace\n");
684#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800685 printf(" -map <int> ............. print map of extra info.\n");
686 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700687
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 printf("\n");
689 printf(" -short ................. condense printed message\n");
690 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700691 printf(" -version ............... print version number and exit.\n");
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700692 printf(" -noasm ................. disable all assembly optimizations.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800693 printf(" -v ..................... verbose, e.g. print encoding/decoding "
694 "times\n");
695 printf("\n");
696 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800697 printf(" -af .................... auto-adjust filter strength.\n");
698 printf(" -pre <int> ............. pre-processing filter\n");
699 printf("\n");
700}
701
702//-----------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700703// Error messages
704
705static const char* const kErrorMessages[] = {
706 "OK",
707 "OUT_OF_MEMORY: Out of memory allocating objects",
708 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
709 "NULL_PARAMETER: NULL parameter passed to function",
710 "INVALID_CONFIGURATION: configuration is invalid",
711 "BAD_DIMENSION: Bad picture dimension",
712 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k",
713 "PARTITION_OVERFLOW: Partition is too big to fir 16M",
714 "BAD_WRITE: Picture writer returned an error"
715};
716
717//-----------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800718
719int main(int argc, const char *argv[]) {
720 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
721 FILE *out = NULL;
722 int c;
723 int short_output = 0;
724 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700725 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800726 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700727 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800728 WebPPicture picture;
729 WebPConfig config;
730 WebPAuxStats stats;
731 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700732
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700733#ifdef WEBP_EXPERIMENTAL_FEATURES
734 keep_alpha = 1;
735#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700736
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800737 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
738 fprintf(stderr, "Error! Version mismatch!\n");
739 goto Error;
740 }
741
742 if (argc == 1) {
743 HelpShort();
744 return 0;
745 }
746
747 for (c = 1; c < argc; ++c) {
748 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
749 HelpShort();
750 return 0;
751 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
752 HelpLong();
753 return 0;
754 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
755 out_file = argv[++c];
756 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
757 dump_file = argv[++c];
758 config.show_compressed = 1;
759 } else if (!strcmp(argv[c], "-short")) {
760 short_output++;
761 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700762 picture.width = strtol(argv[++c], NULL, 0);
763 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700765 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800766 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700767 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800768 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700769 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800770 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700771 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800772 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700773 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800774 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700775 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800776 } else if (!strcmp(argv[c], "-af")) {
777 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700778 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800779 config.filter_type = 1;
780 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700781 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800782 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700783 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700785 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800786 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700787 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700788 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
789 config.alpha_compression = strtol(argv[++c], NULL, 0);
790 } else if (!strcmp(argv[c], "-noalpha")) {
791 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800792 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700793 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700794#ifdef WEBP_EXPERIMENTAL_FEATURES
795 } else if (!strcmp(argv[c], "-444")) {
796 picture.colorspace = WEBP_YUV444;
797 } else if (!strcmp(argv[c], "-422")) {
798 picture.colorspace = WEBP_YUV422;
799 } else if (!strcmp(argv[c], "-gray")) {
800 picture.colorspace = WEBP_YUV400;
801#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800802 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
803 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700804 crop_x = strtol(argv[++c], NULL, 0);
805 crop_y = strtol(argv[++c], NULL, 0);
806 crop_w = strtol(argv[++c], NULL, 0);
807 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700808 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
809 resize_w = strtol(argv[++c], NULL, 0);
810 resize_h = strtol(argv[++c], NULL, 0);
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700811 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoa11009d2011-06-10 15:10:18 -0700812 VP8EncGetCPUInfo = NULL;
Pascal Massimino650ffa32011-03-24 16:17:10 -0700813 } else if (!strcmp(argv[c], "-version")) {
814 const int version = WebPGetEncoderVersion();
815 printf("%d.%d.%d\n",
816 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
817 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800818 } else if (!strcmp(argv[c], "-quiet")) {
819 quiet = 1;
820 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
821 WebPPreset preset;
822 ++c;
823 if (!strcmp(argv[c], "default")) {
824 preset = WEBP_PRESET_DEFAULT;
825 } else if (!strcmp(argv[c], "photo")) {
826 preset = WEBP_PRESET_PHOTO;
827 } else if (!strcmp(argv[c], "picture")) {
828 preset = WEBP_PRESET_PICTURE;
829 } else if (!strcmp(argv[c], "drawing")) {
830 preset = WEBP_PRESET_DRAWING;
831 } else if (!strcmp(argv[c], "icon")) {
832 preset = WEBP_PRESET_ICON;
833 } else if (!strcmp(argv[c], "text")) {
834 preset = WEBP_PRESET_TEXT;
835 } else {
836 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
837 goto Error;
838 }
839 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700840 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800841 goto Error;
842 }
843 } else if (!strcmp(argv[c], "-v")) {
844 verbose = 1;
845 } else if (argv[c][0] == '-') {
846 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
847 HelpLong();
848 return -1;
849 } else {
850 in_file = argv[c];
851 }
852 }
853
854 if (!WebPValidateConfig(&config)) {
855 fprintf(stderr, "Error! Invalid configuration.\n");
856 goto Error;
857 }
858
Pascal Massimino0744e842011-02-25 12:03:27 -0800859 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700860 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800861 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700862 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700863 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700864 fprintf(stderr, "Error! Cannot read input picture\n");
865 goto Error;
866 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800867 if (verbose) {
868 const double time = StopwatchReadAndReset(&stop_watch);
869 fprintf(stderr, "Time to read input: %.3fs\n", time);
870 }
871
872 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800873 if (out_file) {
874 out = fopen(out_file, "wb");
875 if (!out) {
876 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
877 goto Error;
878 } else {
879 if (!short_output && !quiet) {
880 fprintf(stderr, "Saving file '%s'\n", out_file);
881 }
882 }
883 picture.writer = MyWriter;
884 picture.custom_ptr = (void*)out;
885 } else {
886 out = NULL;
887 if (!quiet && !short_output) {
888 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
889 fprintf(stderr, "be performed, but its results discarded.\n\n");
890 }
891 }
892 picture.stats = &stats;
893
Pascal Massimino0744e842011-02-25 12:03:27 -0800894 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700895 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800896 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700897 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700898 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
899 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800900 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700901 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700902 if ((resize_w | resize_h) > 0) {
903 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
904 fprintf(stderr, "Error! Cannot resize picture\n");
905 goto Error;
906 }
907 }
908 if (picture.extra_info_type > 0) {
909 AllocExtraInfo(&picture);
910 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700911 if (!WebPEncode(&config, &picture)) {
912 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700913 fprintf(stderr, "Error code: %d (%s)\n",
914 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700915 goto Error;
916 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800917 if (verbose) {
918 const double time = StopwatchReadAndReset(&stop_watch);
919 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
920 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800921
922 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700923 if (dump_file) {
924 DumpPicture(&picture, dump_file);
925 }
926 if (!quiet) {
927 PrintExtraInfo(&picture, short_output);
928 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800929
930 Error:
931 free(picture.extra_info);
932 WebPPictureFree(&picture);
933 if (out != NULL) {
934 fclose(out);
935 }
936
937 return 0;
938}
939
940//-----------------------------------------------------------------------------