blob: 1d3ae477c56689cc4ad974fea8b269070ce086fe [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 Zern31f9dc62011-06-06 17:56:50 -070047#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080048
49
50#include "webp/encode.h"
51#include "stopwatch.h"
Pascal Massiminoa11009d2011-06-10 15:10:18 -070052extern void* VP8EncGetCPUInfo; // opaque forward declaration.
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070053
Pascal Massiminof61d14a2011-02-18 23:33:46 -080054//-----------------------------------------------------------------------------
55
56static int verbose = 0;
57
58static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
59 const int uv_width = (pic->width + 1) / 2;
60 const int uv_height = (pic->height + 1) / 2;
61 int y;
62 int ok = 0;
63
64 if (!WebPPictureAlloc(pic)) return ok;
65
66 for (y = 0; y < pic->height; ++y) {
67 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
68 goto End;
69 }
70 }
71 for (y = 0; y < uv_height; ++y) {
72 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
73 goto End;
74 }
75 for (y = 0; y < uv_height; ++y) {
76 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
77 goto End;
78 }
79 ok = 1;
80
81 End:
82 return ok;
83}
84
James Zern31f9dc62011-06-06 17:56:50 -070085#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080086
87#define IFS(fn) \
88 do { \
89 if (SUCCEEDED(hr)) \
90 { \
91 hr = (fn); \
92 if (FAILED(hr) && verbose) \
93 printf(#fn " failed %08x\n", hr); \
94 } \
95 } while (0)
96
97#ifdef __cplusplus
98#define MAKE_REFGUID(x) (x)
99#else
100#define MAKE_REFGUID(x) &(x)
101#endif
102
103static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
104 HRESULT hr = S_OK;
105 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
106 if (FAILED(hr))
107 printf("Error opening input file %s (%08x)\n", filename, hr);
108 return hr;
109}
110
111static HRESULT ReadPictureWithWIC(const char* filename,
112 WebPPicture* const pic) {
113 HRESULT hr = S_OK;
114 IWICBitmapFrameDecode* pFrame = NULL;
115 IWICFormatConverter* pConverter = NULL;
116 IWICImagingFactory* pFactory = NULL;
117 IWICBitmapDecoder* pDecoder = NULL;
118 IStream* pStream = NULL;
119 UINT frameCount = 0;
120 UINT width, height = 0;
121 BYTE* rgb = NULL;
122
123 IFS(CoInitialize(NULL));
124 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
125 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
126 (LPVOID*)&pFactory));
127 if (hr == REGDB_E_CLASSNOTREG) {
128 printf("Couldn't access Windows Imaging Component (are you running \n");
129 printf("Windows XP SP3 or newer?). Most formats not available.\n");
130 printf("Use -s for the available YUV input.\n");
131 }
132 // Prepare for image decoding.
133 IFS(OpenInputStream(filename, &pStream));
134 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
135 WICDecodeMetadataCacheOnDemand, &pDecoder));
136 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
137 if (SUCCEEDED(hr) && frameCount == 0) {
138 printf("No frame found in input file.\n");
139 hr = E_FAIL;
140 }
141 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
142
143 // Prepare for pixel format conversion (if necessary).
144 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
145 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
146 MAKE_REFGUID(GUID_WICPixelFormat24bppRGB), WICBitmapDitherTypeNone,
147 NULL, 0.0, WICBitmapPaletteTypeCustom));
148
149 // Decode.
150 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
151 if (SUCCEEDED(hr)) {
152 rgb = (BYTE*)malloc(3 * width * height);
153 if (rgb == NULL)
154 hr = E_OUTOFMEMORY;
155 }
156 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, 3 * width,
157 3 * width * height, rgb));
158
159 // WebP conversion.
160 if (SUCCEEDED(hr)) {
161 pic->width = width;
162 pic->height = height;
163 if (!WebPPictureImportRGB(pic, rgb, 3 * width))
164 hr = E_FAIL;
165 }
166
167 // Cleanup.
168 if (pConverter != NULL) IUnknown_Release(pConverter);
169 if (pFrame != NULL) IUnknown_Release(pFrame);
170 if (pDecoder != NULL) IUnknown_Release(pDecoder);
171 if (pFactory != NULL) IUnknown_Release(pFactory);
172 if (pStream != NULL) IUnknown_Release(pStream);
173 free(rgb);
174 return hr;
175}
176
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700177static int ReadPicture(const char* const filename, WebPPicture* const pic,
178 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800179 int ok;
180 if (pic->width != 0 && pic->height != 0) {
181 // If image size is specified, infer it as YUV format.
182 FILE* in_file = fopen(filename, "rb");
183 if (in_file == NULL) {
184 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
185 return 0;
186 }
187 ok = ReadYUV(in_file, pic);
188 fclose(in_file);
189 } else {
190 // If no size specified, try to decode it using WIC.
191 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic));
192 }
193 if (!ok) {
194 fprintf(stderr, "Error! Could not process file %s\n", filename);
195 }
196 return ok;
197}
198
James Zern31f9dc62011-06-06 17:56:50 -0700199#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800200
201#ifdef WEBP_HAVE_JPEG
202struct my_error_mgr {
203 struct jpeg_error_mgr pub;
204 jmp_buf setjmp_buffer;
205};
206
207static void my_error_exit(j_common_ptr dinfo) {
208 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
209 (*dinfo->err->output_message) (dinfo);
210 longjmp(myerr->setjmp_buffer, 1);
211}
212
213static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
214 int ok = 0;
215 int stride, width, height;
216 uint8_t* rgb = NULL;
217 uint8_t* row_ptr = NULL;
218 struct jpeg_decompress_struct dinfo;
219 struct my_error_mgr jerr;
220 JSAMPARRAY buffer;
221
222 dinfo.err = jpeg_std_error(&jerr.pub);
223 jerr.pub.error_exit = my_error_exit;
224
225 if (setjmp (jerr.setjmp_buffer)) {
226 Error:
227 jpeg_destroy_decompress(&dinfo);
228 goto End;
229 }
230
231 jpeg_create_decompress(&dinfo);
232 jpeg_stdio_src(&dinfo, in_file);
233 jpeg_read_header(&dinfo, TRUE);
234
235 dinfo.out_color_space = JCS_RGB;
236 dinfo.dct_method = JDCT_IFAST;
237 dinfo.do_fancy_upsampling = TRUE;
238
239 jpeg_start_decompress(&dinfo);
240
241 if (dinfo.output_components != 3) {
242 goto Error;
243 }
244
245 width = dinfo.output_width;
246 height = dinfo.output_height;
247 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
248
249 rgb = (uint8_t*)malloc(stride * height);
250 if (rgb == NULL) {
251 goto End;
252 }
253 row_ptr = rgb;
254
255 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
256 JPOOL_IMAGE, stride, 1);
257 if (buffer == NULL) {
258 goto End;
259 }
260
261 while (dinfo.output_scanline < dinfo.output_height) {
262 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
263 goto End;
264 }
265 memcpy(row_ptr, buffer[0], stride);
266 row_ptr += stride;
267 }
268
269 jpeg_finish_decompress (&dinfo);
270 jpeg_destroy_decompress (&dinfo);
271
272 // WebP conversion.
273 pic->width = width;
274 pic->height = height;
275 ok = WebPPictureImportRGB(pic, rgb, stride);
276
277 End:
278 if (rgb) {
279 free(rgb);
280 }
281 return ok;
282}
283
284#else
285static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
James Zern08057062011-06-24 16:50:02 -0400286 (void)in_file;
287 (void)pic;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800288 printf("JPEG support not compiled. Please install the libjpeg development "
289 "package before building.\n");
290 return 0;
291}
292#endif
293
294#ifdef WEBP_HAVE_PNG
295static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700296 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800297 longjmp(png_jmpbuf(png), 1);
298}
299
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700300static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800301 png_structp png;
302 png_infop info;
303 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700304 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800305 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700306 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800307 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700308 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800309 int stride;
310 uint8_t* rgb = NULL;
311
312 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
313 if (png == NULL) {
314 goto End;
315 }
316
317 png_set_error_fn(png, 0, error_function, NULL);
318 if (setjmp(png_jmpbuf(png))) {
319 Error:
320 png_destroy_read_struct(&png, NULL, NULL);
321 if (rgb) free(rgb);
322 goto End;
323 }
324
325 info = png_create_info_struct(png);
326 if (info == NULL) goto Error;
327
328 png_init_io(png, in_file);
329 png_read_info(png, info);
330 if (!png_get_IHDR(png, info,
331 &width, &height, &bit_depth, &color_type, &interlaced,
332 NULL, NULL)) goto Error;
333
334 png_set_strip_16(png);
335 png_set_packing(png);
336 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
337 if (color_type == PNG_COLOR_TYPE_GRAY) {
338 if (bit_depth < 8) {
339 png_set_expand_gray_1_2_4_to_8(png);
340 }
341 png_set_gray_to_rgb(png);
342 }
343 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
344 png_set_tRNS_to_alpha(png);
345 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700346 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800347
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700348 if (!keep_alpha) {
349 png_set_strip_alpha(png);
350 has_alpha = 0;
351 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700352#ifdef WEBP_EXPERIMENTAL_FEATURES
353 if (has_alpha) {
354 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
355 }
356#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700357
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800358 num_passes = png_set_interlace_handling(png);
359 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700360 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800361 rgb = (uint8_t*)malloc(stride * height);
362 if (rgb == NULL) goto Error;
363 for (p = 0; p < num_passes; ++p) {
364 for (y = 0; y < height; ++y) {
365 png_bytep row = rgb + y * stride;
366 png_read_rows(png, &row, NULL, 1);
367 }
368 }
369 png_read_end(png, info);
370 png_destroy_read_struct(&png, &info, NULL);
371
372 pic->width = width;
373 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700374 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
375 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800376 free(rgb);
377
378 End:
379 return ok;
380}
381#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700382static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
James Zern08057062011-06-24 16:50:02 -0400383 (void)in_file;
384 (void)pic;
385 (void)keep_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800386 printf("PNG support not compiled. Please install the libpng development "
387 "package before building.\n");
388 return 0;
389}
390#endif
391
392typedef enum {
393 PNG = 0,
394 JPEG,
395 UNSUPPORTED,
396} InputFileFormat;
397
398static InputFileFormat GetImageType(FILE* in_file) {
399 InputFileFormat format = UNSUPPORTED;
400 unsigned int magic;
401 unsigned char buf[4];
402
403 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
404 (fseek(in_file, 0, SEEK_SET) != 0)) {
405 return format;
406 }
407
408 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
409 if (magic == 0x89504E47U) {
410 format = PNG;
411 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
412 format = JPEG;
413 }
414 return format;
415}
416
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700417static int ReadPicture(const char* const filename, WebPPicture* const pic,
418 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800419 int ok = 0;
420 FILE* in_file = fopen(filename, "rb");
421 if (in_file == NULL) {
422 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
423 return ok;
424 }
425
426 if (pic->width == 0 || pic->height == 0) {
427 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
428 const InputFileFormat format = GetImageType(in_file);
429 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700430 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800431 } else if (format == JPEG) {
432 ok = ReadJPEG(in_file, pic);
433 }
434 } else {
435 // If image size is specified, infer it as YUV format.
436 ok = ReadYUV(in_file, pic);
437 }
438 if (!ok) {
439 fprintf(stderr, "Error! Could not process file %s\n", filename);
440 }
441
442 fclose(in_file);
443 return ok;
444}
445
James Zern31f9dc62011-06-06 17:56:50 -0700446#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800447
448static void AllocExtraInfo(WebPPicture* const pic) {
449 const int mb_w = (pic->width + 15) / 16;
450 const int mb_h = (pic->height + 15) / 16;
451 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
452}
453
454static void PrintByteCount(const int bytes[4], int total_size,
455 int* const totals) {
456 int s;
457 int total = 0;
458 for (s = 0; s < 4; ++s) {
459 fprintf(stderr, "| %7d ", bytes[s]);
460 total += bytes[s];
461 if (totals) totals[s] += bytes[s];
462 }
463 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
464}
465
466static void PrintPercents(const int counts[4], int total) {
467 int s;
468 for (s = 0; s < 4; ++s) {
469 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
470 }
471 fprintf(stderr,"| %7d\n", total);
472}
473
474static void PrintValues(const int values[4]) {
475 int s;
476 for (s = 0; s < 4; ++s) {
477 fprintf(stderr, "| %7d ", values[s]);
478 }
479 fprintf(stderr,"|\n");
480}
481
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700482static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800483 const WebPAuxStats* const stats = pic->stats;
484 if (short_output) {
485 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
486 } else{
487 const int num_i4 = stats->block_count[0];
488 const int num_i16 = stats->block_count[1];
489 const int num_skip = stats->block_count[2];
490 const int total = num_i4 + num_i16;
491 fprintf(stderr,
492 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
493 stats->coded_size,
494 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
495 if (total > 0) {
496 int totals[4] = { 0, 0, 0, 0 };
497 fprintf(stderr, "block count: intra4: %d\n"
498 " intra16: %d (-> %.2f%%)\n",
499 num_i4, num_i16, 100.f * num_i16 / total);
500 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
501 num_skip, 100.f * num_skip / total);
502 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
503 " mode-partition: %6d (%.1f%%)\n",
504 stats->header_bytes[0],
505 100.f * stats->header_bytes[0] / stats->coded_size,
506 stats->header_bytes[1],
507 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700508 if (stats->alpha_data_size) {
509 fprintf(stderr, " transparency: %6d\n",
510 stats->alpha_data_size);
511 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700512 if (stats->layer_data_size) {
513 fprintf(stderr, " enhancement: %6d\n",
514 stats->layer_data_size);
515 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800516 fprintf(stderr, " Residuals bytes "
517 "|segment 1|segment 2|segment 3"
518 "|segment 4| total\n");
519 fprintf(stderr, " intra4-coeffs: ");
520 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
521 fprintf(stderr, " intra16-coeffs: ");
522 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
523 fprintf(stderr, " chroma coeffs: ");
524 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
525 fprintf(stderr, " macroblocks: ");
526 PrintPercents(stats->segment_size, total);
527 fprintf(stderr, " quantizer: ");
528 PrintValues(stats->segment_quant);
529 fprintf(stderr, " filter level: ");
530 PrintValues(stats->segment_level);
531 fprintf(stderr, "------------------+---------");
532 fprintf(stderr, "+---------+---------+---------+-----------------\n");
533 fprintf(stderr, " segments total: ");
534 PrintByteCount(totals, stats->coded_size, NULL);
535 }
536 }
537 if (pic->extra_info) {
538 const int mb_w = (pic->width + 15) / 16;
539 const int mb_h = (pic->height + 15) / 16;
540 const int type = pic->extra_info_type;
541 int x, y;
542 for (y = 0; y < mb_h; ++y) {
543 for (x = 0; x < mb_w; ++x) {
544 const int c = pic->extra_info[x + y * mb_w];
545 if (type == 1) { // intra4/intra16
546 printf("%c", "+."[c]);
547 } else if (type == 2) { // segments
548 printf("%c", ".-*X"[c]);
549 } else if (type == 3) { // quantizers
550 printf("%.2d ", c);
551 } else if (type == 6 || type == 7) {
552 printf("%3d ", c);
553 } else {
554 printf("0x%.2x ", c);
555 }
556 }
557 printf("\n");
558 }
559 }
560}
561
562//-----------------------------------------------------------------------------
563
564static int MyWriter(const uint8_t* data, size_t data_size,
565 const WebPPicture* const pic) {
566 FILE* const out = (FILE*)pic->custom_ptr;
567 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
568}
569
570// Dumps a picture as a PGM file using the IMC4 layout.
571static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
572 int y;
573 const int uv_width = (picture->width + 1) / 2;
574 const int uv_height = (picture->height + 1) / 2;
575 const int stride = (picture->width + 1) & ~1;
576 const int height = picture->height + uv_height;
577 FILE* const f = fopen(PGM_name, "wb");
578 if (!f) return 0;
579 fprintf(f, "P5\n%d %d\n255\n", stride, height);
580 for (y = 0; y < picture->height; ++y) {
581 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
582 return 0;
583 if (picture->width & 1) fputc(0, f); // pad
584 }
585 for (y = 0; y < uv_height; ++y) {
586 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
587 return 0;
588 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
589 return 0;
590 }
591 fclose(f);
592 return 1;
593}
594
595//-----------------------------------------------------------------------------
596
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700597static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800598 printf("Usage:\n\n");
599 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
600 printf("where quality is between 0 (poor) to 100 (very good).\n");
601 printf("Typical value is around 80.\n\n");
602 printf("Try -longhelp for an exhaustive list of advanced options.\n");
603}
604
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700605static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800606 printf("Usage:\n");
607 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
608 printf("If input size (-s) for an image is not specified, "
609 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700610#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800611 printf("Windows builds can take as input any of the files handled by WIC\n");
612#endif
613 printf("options:\n");
614 printf(" -h / -help ............ short help\n");
615 printf(" -H / -longhelp ........ long help\n");
616 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
617 printf(" -preset <string> ....... Preset setting, one of:\n");
618 printf(" default, photo, picture,\n");
619 printf(" drawing, icon, text\n");
620 printf(" -preset must come first, as it overwrites other parameters.");
621 printf("\n");
622 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
623 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700624 printf(" -size <int> ............ Target size (in bytes)\n");
625 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800626 printf("\n");
627 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
628 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
629 printf(" -f <int> ............... filter strength (0=off..100)\n");
630 printf(" -sharpness <int> ....... "
631 "filter sharpness (0:most .. 7:least sharp)\n");
632 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700633 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
634 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 printf(" -pass <int> ............ analysis pass number (1..10)\n");
636 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700637 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
638#ifdef WEBP_EXPERIMENTAL_FEATURES
639 printf(" -444 / -422 / -gray ..... Change colorspace\n");
640#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800641 printf(" -map <int> ............. print map of extra info.\n");
642 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700643
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 printf("\n");
645 printf(" -short ................. condense printed message\n");
646 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700647 printf(" -version ............... print version number and exit.\n");
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700648 printf(" -noasm ................. disable all assembly optimizations.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800649 printf(" -v ..................... verbose, e.g. print encoding/decoding "
650 "times\n");
651 printf("\n");
652 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800653 printf(" -af .................... auto-adjust filter strength.\n");
654 printf(" -pre <int> ............. pre-processing filter\n");
655 printf("\n");
656}
657
658//-----------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700659// Error messages
660
661static const char* const kErrorMessages[] = {
662 "OK",
663 "OUT_OF_MEMORY: Out of memory allocating objects",
664 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
665 "NULL_PARAMETER: NULL parameter passed to function",
666 "INVALID_CONFIGURATION: configuration is invalid",
667 "BAD_DIMENSION: Bad picture dimension",
668 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k",
669 "PARTITION_OVERFLOW: Partition is too big to fir 16M",
670 "BAD_WRITE: Picture writer returned an error"
671};
672
673//-----------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674
675int main(int argc, const char *argv[]) {
676 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
677 FILE *out = NULL;
678 int c;
679 int short_output = 0;
680 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700681 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800682 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700683 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800684 WebPPicture picture;
685 WebPConfig config;
686 WebPAuxStats stats;
687 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700688
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700689#ifdef WEBP_EXPERIMENTAL_FEATURES
690 keep_alpha = 1;
691#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700692
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800693 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
694 fprintf(stderr, "Error! Version mismatch!\n");
695 goto Error;
696 }
697
698 if (argc == 1) {
699 HelpShort();
700 return 0;
701 }
702
703 for (c = 1; c < argc; ++c) {
704 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
705 HelpShort();
706 return 0;
707 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
708 HelpLong();
709 return 0;
710 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
711 out_file = argv[++c];
712 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
713 dump_file = argv[++c];
714 config.show_compressed = 1;
715 } else if (!strcmp(argv[c], "-short")) {
716 short_output++;
717 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700718 picture.width = strtol(argv[++c], NULL, 0);
719 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800720 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700721 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800722 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700723 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800724 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700725 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800726 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700727 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800728 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700729 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700731 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800732 } else if (!strcmp(argv[c], "-af")) {
733 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700734 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800735 config.filter_type = 1;
736 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700737 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800738 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700739 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800740 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700741 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800742 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700743 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700744 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
745 config.alpha_compression = strtol(argv[++c], NULL, 0);
746 } else if (!strcmp(argv[c], "-noalpha")) {
747 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800748 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700749 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700750#ifdef WEBP_EXPERIMENTAL_FEATURES
751 } else if (!strcmp(argv[c], "-444")) {
752 picture.colorspace = WEBP_YUV444;
753 } else if (!strcmp(argv[c], "-422")) {
754 picture.colorspace = WEBP_YUV422;
755 } else if (!strcmp(argv[c], "-gray")) {
756 picture.colorspace = WEBP_YUV400;
757#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800758 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
759 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700760 crop_x = strtol(argv[++c], NULL, 0);
761 crop_y = strtol(argv[++c], NULL, 0);
762 crop_w = strtol(argv[++c], NULL, 0);
763 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700764 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
765 resize_w = strtol(argv[++c], NULL, 0);
766 resize_h = strtol(argv[++c], NULL, 0);
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700767 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoa11009d2011-06-10 15:10:18 -0700768 VP8EncGetCPUInfo = NULL;
Pascal Massimino650ffa32011-03-24 16:17:10 -0700769 } else if (!strcmp(argv[c], "-version")) {
770 const int version = WebPGetEncoderVersion();
771 printf("%d.%d.%d\n",
772 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
773 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800774 } else if (!strcmp(argv[c], "-quiet")) {
775 quiet = 1;
776 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
777 WebPPreset preset;
778 ++c;
779 if (!strcmp(argv[c], "default")) {
780 preset = WEBP_PRESET_DEFAULT;
781 } else if (!strcmp(argv[c], "photo")) {
782 preset = WEBP_PRESET_PHOTO;
783 } else if (!strcmp(argv[c], "picture")) {
784 preset = WEBP_PRESET_PICTURE;
785 } else if (!strcmp(argv[c], "drawing")) {
786 preset = WEBP_PRESET_DRAWING;
787 } else if (!strcmp(argv[c], "icon")) {
788 preset = WEBP_PRESET_ICON;
789 } else if (!strcmp(argv[c], "text")) {
790 preset = WEBP_PRESET_TEXT;
791 } else {
792 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
793 goto Error;
794 }
795 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700796 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800797 goto Error;
798 }
799 } else if (!strcmp(argv[c], "-v")) {
800 verbose = 1;
801 } else if (argv[c][0] == '-') {
802 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
803 HelpLong();
804 return -1;
805 } else {
806 in_file = argv[c];
807 }
808 }
809
810 if (!WebPValidateConfig(&config)) {
811 fprintf(stderr, "Error! Invalid configuration.\n");
812 goto Error;
813 }
814
Pascal Massimino0744e842011-02-25 12:03:27 -0800815 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700816 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800817 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700818 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700819 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700820 fprintf(stderr, "Error! Cannot read input picture\n");
821 goto Error;
822 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800823 if (verbose) {
824 const double time = StopwatchReadAndReset(&stop_watch);
825 fprintf(stderr, "Time to read input: %.3fs\n", time);
826 }
827
828 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800829 if (out_file) {
830 out = fopen(out_file, "wb");
831 if (!out) {
832 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
833 goto Error;
834 } else {
835 if (!short_output && !quiet) {
836 fprintf(stderr, "Saving file '%s'\n", out_file);
837 }
838 }
839 picture.writer = MyWriter;
840 picture.custom_ptr = (void*)out;
841 } else {
842 out = NULL;
843 if (!quiet && !short_output) {
844 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
845 fprintf(stderr, "be performed, but its results discarded.\n\n");
846 }
847 }
848 picture.stats = &stats;
849
Pascal Massimino0744e842011-02-25 12:03:27 -0800850 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700851 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800852 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700853 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700854 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
855 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800856 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700857 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700858 if ((resize_w | resize_h) > 0) {
859 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
860 fprintf(stderr, "Error! Cannot resize picture\n");
861 goto Error;
862 }
863 }
864 if (picture.extra_info_type > 0) {
865 AllocExtraInfo(&picture);
866 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700867 if (!WebPEncode(&config, &picture)) {
868 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700869 fprintf(stderr, "Error code: %d (%s)\n",
870 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700871 goto Error;
872 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800873 if (verbose) {
874 const double time = StopwatchReadAndReset(&stop_watch);
875 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
876 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800877
878 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700879 if (dump_file) {
880 DumpPicture(&picture, dump_file);
881 }
882 if (!quiet) {
883 PrintExtraInfo(&picture, short_output);
884 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885
886 Error:
887 free(picture.extra_info);
888 WebPPictureFree(&picture);
889 if (out != NULL) {
890 fclose(out);
891 }
892
893 return 0;
894}
895
896//-----------------------------------------------------------------------------