blob: 01e6bf1e9260a6b21c4f5733f1f863d0cf5b16e3 [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
17#ifdef WEBP_HAVE_PNG
18#include <png.h>
19#endif
20
21#ifdef WEBP_HAVE_JPEG
22#include <setjmp.h> // note: this must be included *after* png.h
23#include <jpeglib.h>
24#endif
25
26#ifdef _WIN32
27#define CINTERFACE
28#define COBJMACROS
29#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
30 // code with COBJMACROS.
31#include <shlwapi.h>
32#include <windows.h>
33#include <wincodec.h>
34#endif
35
36
37#include "webp/encode.h"
38#include "stopwatch.h"
39
40//-----------------------------------------------------------------------------
41
42static int verbose = 0;
43
44static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
45 const int uv_width = (pic->width + 1) / 2;
46 const int uv_height = (pic->height + 1) / 2;
47 int y;
48 int ok = 0;
49
50 if (!WebPPictureAlloc(pic)) return ok;
51
52 for (y = 0; y < pic->height; ++y) {
53 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
54 goto End;
55 }
56 }
57 for (y = 0; y < uv_height; ++y) {
58 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
59 goto End;
60 }
61 for (y = 0; y < uv_height; ++y) {
62 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
63 goto End;
64 }
65 ok = 1;
66
67 End:
68 return ok;
69}
70
71#ifdef _WIN32
72
73#define IFS(fn) \
74 do { \
75 if (SUCCEEDED(hr)) \
76 { \
77 hr = (fn); \
78 if (FAILED(hr) && verbose) \
79 printf(#fn " failed %08x\n", hr); \
80 } \
81 } while (0)
82
83#ifdef __cplusplus
84#define MAKE_REFGUID(x) (x)
85#else
86#define MAKE_REFGUID(x) &(x)
87#endif
88
89static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
90 HRESULT hr = S_OK;
91 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
92 if (FAILED(hr))
93 printf("Error opening input file %s (%08x)\n", filename, hr);
94 return hr;
95}
96
97static HRESULT ReadPictureWithWIC(const char* filename,
98 WebPPicture* const pic) {
99 HRESULT hr = S_OK;
100 IWICBitmapFrameDecode* pFrame = NULL;
101 IWICFormatConverter* pConverter = NULL;
102 IWICImagingFactory* pFactory = NULL;
103 IWICBitmapDecoder* pDecoder = NULL;
104 IStream* pStream = NULL;
105 UINT frameCount = 0;
106 UINT width, height = 0;
107 BYTE* rgb = NULL;
108
109 IFS(CoInitialize(NULL));
110 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
111 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
112 (LPVOID*)&pFactory));
113 if (hr == REGDB_E_CLASSNOTREG) {
114 printf("Couldn't access Windows Imaging Component (are you running \n");
115 printf("Windows XP SP3 or newer?). Most formats not available.\n");
116 printf("Use -s for the available YUV input.\n");
117 }
118 // Prepare for image decoding.
119 IFS(OpenInputStream(filename, &pStream));
120 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
121 WICDecodeMetadataCacheOnDemand, &pDecoder));
122 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
123 if (SUCCEEDED(hr) && frameCount == 0) {
124 printf("No frame found in input file.\n");
125 hr = E_FAIL;
126 }
127 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
128
129 // Prepare for pixel format conversion (if necessary).
130 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
131 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
132 MAKE_REFGUID(GUID_WICPixelFormat24bppRGB), WICBitmapDitherTypeNone,
133 NULL, 0.0, WICBitmapPaletteTypeCustom));
134
135 // Decode.
136 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
137 if (SUCCEEDED(hr)) {
138 rgb = (BYTE*)malloc(3 * width * height);
139 if (rgb == NULL)
140 hr = E_OUTOFMEMORY;
141 }
142 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, 3 * width,
143 3 * width * height, rgb));
144
145 // WebP conversion.
146 if (SUCCEEDED(hr)) {
147 pic->width = width;
148 pic->height = height;
149 if (!WebPPictureImportRGB(pic, rgb, 3 * width))
150 hr = E_FAIL;
151 }
152
153 // Cleanup.
154 if (pConverter != NULL) IUnknown_Release(pConverter);
155 if (pFrame != NULL) IUnknown_Release(pFrame);
156 if (pDecoder != NULL) IUnknown_Release(pDecoder);
157 if (pFactory != NULL) IUnknown_Release(pFactory);
158 if (pStream != NULL) IUnknown_Release(pStream);
159 free(rgb);
160 return hr;
161}
162
163static int ReadPicture(const char* const filename, WebPPicture* const pic) {
164 int ok;
165 if (pic->width != 0 && pic->height != 0) {
166 // If image size is specified, infer it as YUV format.
167 FILE* in_file = fopen(filename, "rb");
168 if (in_file == NULL) {
169 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
170 return 0;
171 }
172 ok = ReadYUV(in_file, pic);
173 fclose(in_file);
174 } else {
175 // If no size specified, try to decode it using WIC.
176 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic));
177 }
178 if (!ok) {
179 fprintf(stderr, "Error! Could not process file %s\n", filename);
180 }
181 return ok;
182}
183
184#else // !_WIN32
185
186#ifdef WEBP_HAVE_JPEG
187struct my_error_mgr {
188 struct jpeg_error_mgr pub;
189 jmp_buf setjmp_buffer;
190};
191
192static void my_error_exit(j_common_ptr dinfo) {
193 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
194 (*dinfo->err->output_message) (dinfo);
195 longjmp(myerr->setjmp_buffer, 1);
196}
197
198static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
199 int ok = 0;
200 int stride, width, height;
201 uint8_t* rgb = NULL;
202 uint8_t* row_ptr = NULL;
203 struct jpeg_decompress_struct dinfo;
204 struct my_error_mgr jerr;
205 JSAMPARRAY buffer;
206
207 dinfo.err = jpeg_std_error(&jerr.pub);
208 jerr.pub.error_exit = my_error_exit;
209
210 if (setjmp (jerr.setjmp_buffer)) {
211 Error:
212 jpeg_destroy_decompress(&dinfo);
213 goto End;
214 }
215
216 jpeg_create_decompress(&dinfo);
217 jpeg_stdio_src(&dinfo, in_file);
218 jpeg_read_header(&dinfo, TRUE);
219
220 dinfo.out_color_space = JCS_RGB;
221 dinfo.dct_method = JDCT_IFAST;
222 dinfo.do_fancy_upsampling = TRUE;
223
224 jpeg_start_decompress(&dinfo);
225
226 if (dinfo.output_components != 3) {
227 goto Error;
228 }
229
230 width = dinfo.output_width;
231 height = dinfo.output_height;
232 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
233
234 rgb = (uint8_t*)malloc(stride * height);
235 if (rgb == NULL) {
236 goto End;
237 }
238 row_ptr = rgb;
239
240 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
241 JPOOL_IMAGE, stride, 1);
242 if (buffer == NULL) {
243 goto End;
244 }
245
246 while (dinfo.output_scanline < dinfo.output_height) {
247 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
248 goto End;
249 }
250 memcpy(row_ptr, buffer[0], stride);
251 row_ptr += stride;
252 }
253
254 jpeg_finish_decompress (&dinfo);
255 jpeg_destroy_decompress (&dinfo);
256
257 // WebP conversion.
258 pic->width = width;
259 pic->height = height;
260 ok = WebPPictureImportRGB(pic, rgb, stride);
261
262 End:
263 if (rgb) {
264 free(rgb);
265 }
266 return ok;
267}
268
269#else
270static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
271 printf("JPEG support not compiled. Please install the libjpeg development "
272 "package before building.\n");
273 return 0;
274}
275#endif
276
277#ifdef WEBP_HAVE_PNG
278static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700279 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800280 longjmp(png_jmpbuf(png), 1);
281}
282
283static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
284 png_structp png;
285 png_infop info;
286 int color_type, bit_depth, interlaced;
287 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700288 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800289 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700290 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800291 int stride;
292 uint8_t* rgb = NULL;
293
294 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
295 if (png == NULL) {
296 goto End;
297 }
298
299 png_set_error_fn(png, 0, error_function, NULL);
300 if (setjmp(png_jmpbuf(png))) {
301 Error:
302 png_destroy_read_struct(&png, NULL, NULL);
303 if (rgb) free(rgb);
304 goto End;
305 }
306
307 info = png_create_info_struct(png);
308 if (info == NULL) goto Error;
309
310 png_init_io(png, in_file);
311 png_read_info(png, info);
312 if (!png_get_IHDR(png, info,
313 &width, &height, &bit_depth, &color_type, &interlaced,
314 NULL, NULL)) goto Error;
315
316 png_set_strip_16(png);
317 png_set_packing(png);
318 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
319 if (color_type == PNG_COLOR_TYPE_GRAY) {
320 if (bit_depth < 8) {
321 png_set_expand_gray_1_2_4_to_8(png);
322 }
323 png_set_gray_to_rgb(png);
324 }
325 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
326 png_set_tRNS_to_alpha(png);
327 }
328
329 // TODO(skal): Strip Alpha for now (till Alpha is supported).
330 png_set_strip_alpha(png);
331 num_passes = png_set_interlace_handling(png);
332 png_read_update_info(png, info);
333 stride = 3 * width * sizeof(*rgb);
334 rgb = (uint8_t*)malloc(stride * height);
335 if (rgb == NULL) goto Error;
336 for (p = 0; p < num_passes; ++p) {
337 for (y = 0; y < height; ++y) {
338 png_bytep row = rgb + y * stride;
339 png_read_rows(png, &row, NULL, 1);
340 }
341 }
342 png_read_end(png, info);
343 png_destroy_read_struct(&png, &info, NULL);
344
345 pic->width = width;
346 pic->height = height;
347 ok = WebPPictureImportRGB(pic, rgb, stride);
348 free(rgb);
349
350 End:
351 return ok;
352}
353#else
354static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
355 printf("PNG support not compiled. Please install the libpng development "
356 "package before building.\n");
357 return 0;
358}
359#endif
360
361typedef enum {
362 PNG = 0,
363 JPEG,
364 UNSUPPORTED,
365} InputFileFormat;
366
367static InputFileFormat GetImageType(FILE* in_file) {
368 InputFileFormat format = UNSUPPORTED;
369 unsigned int magic;
370 unsigned char buf[4];
371
372 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
373 (fseek(in_file, 0, SEEK_SET) != 0)) {
374 return format;
375 }
376
377 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
378 if (magic == 0x89504E47U) {
379 format = PNG;
380 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
381 format = JPEG;
382 }
383 return format;
384}
385
386static int ReadPicture(const char* const filename, WebPPicture* const pic) {
387 int ok = 0;
388 FILE* in_file = fopen(filename, "rb");
389 if (in_file == NULL) {
390 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
391 return ok;
392 }
393
394 if (pic->width == 0 || pic->height == 0) {
395 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
396 const InputFileFormat format = GetImageType(in_file);
397 if (format == PNG) {
398 ok = ReadPNG(in_file, pic);
399 } else if (format == JPEG) {
400 ok = ReadJPEG(in_file, pic);
401 }
402 } else {
403 // If image size is specified, infer it as YUV format.
404 ok = ReadYUV(in_file, pic);
405 }
406 if (!ok) {
407 fprintf(stderr, "Error! Could not process file %s\n", filename);
408 }
409
410 fclose(in_file);
411 return ok;
412}
413
414#endif // !_WIN32
415
416static void AllocExtraInfo(WebPPicture* const pic) {
417 const int mb_w = (pic->width + 15) / 16;
418 const int mb_h = (pic->height + 15) / 16;
419 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
420}
421
422static void PrintByteCount(const int bytes[4], int total_size,
423 int* const totals) {
424 int s;
425 int total = 0;
426 for (s = 0; s < 4; ++s) {
427 fprintf(stderr, "| %7d ", bytes[s]);
428 total += bytes[s];
429 if (totals) totals[s] += bytes[s];
430 }
431 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
432}
433
434static void PrintPercents(const int counts[4], int total) {
435 int s;
436 for (s = 0; s < 4; ++s) {
437 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
438 }
439 fprintf(stderr,"| %7d\n", total);
440}
441
442static void PrintValues(const int values[4]) {
443 int s;
444 for (s = 0; s < 4; ++s) {
445 fprintf(stderr, "| %7d ", values[s]);
446 }
447 fprintf(stderr,"|\n");
448}
449
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700450static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800451 const WebPAuxStats* const stats = pic->stats;
452 if (short_output) {
453 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
454 } else{
455 const int num_i4 = stats->block_count[0];
456 const int num_i16 = stats->block_count[1];
457 const int num_skip = stats->block_count[2];
458 const int total = num_i4 + num_i16;
459 fprintf(stderr,
460 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
461 stats->coded_size,
462 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
463 if (total > 0) {
464 int totals[4] = { 0, 0, 0, 0 };
465 fprintf(stderr, "block count: intra4: %d\n"
466 " intra16: %d (-> %.2f%%)\n",
467 num_i4, num_i16, 100.f * num_i16 / total);
468 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
469 num_skip, 100.f * num_skip / total);
470 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
471 " mode-partition: %6d (%.1f%%)\n",
472 stats->header_bytes[0],
473 100.f * stats->header_bytes[0] / stats->coded_size,
474 stats->header_bytes[1],
475 100.f * stats->header_bytes[1] / stats->coded_size);
476 fprintf(stderr, " Residuals bytes "
477 "|segment 1|segment 2|segment 3"
478 "|segment 4| total\n");
479 fprintf(stderr, " intra4-coeffs: ");
480 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
481 fprintf(stderr, " intra16-coeffs: ");
482 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
483 fprintf(stderr, " chroma coeffs: ");
484 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
485 fprintf(stderr, " macroblocks: ");
486 PrintPercents(stats->segment_size, total);
487 fprintf(stderr, " quantizer: ");
488 PrintValues(stats->segment_quant);
489 fprintf(stderr, " filter level: ");
490 PrintValues(stats->segment_level);
491 fprintf(stderr, "------------------+---------");
492 fprintf(stderr, "+---------+---------+---------+-----------------\n");
493 fprintf(stderr, " segments total: ");
494 PrintByteCount(totals, stats->coded_size, NULL);
495 }
496 }
497 if (pic->extra_info) {
498 const int mb_w = (pic->width + 15) / 16;
499 const int mb_h = (pic->height + 15) / 16;
500 const int type = pic->extra_info_type;
501 int x, y;
502 for (y = 0; y < mb_h; ++y) {
503 for (x = 0; x < mb_w; ++x) {
504 const int c = pic->extra_info[x + y * mb_w];
505 if (type == 1) { // intra4/intra16
506 printf("%c", "+."[c]);
507 } else if (type == 2) { // segments
508 printf("%c", ".-*X"[c]);
509 } else if (type == 3) { // quantizers
510 printf("%.2d ", c);
511 } else if (type == 6 || type == 7) {
512 printf("%3d ", c);
513 } else {
514 printf("0x%.2x ", c);
515 }
516 }
517 printf("\n");
518 }
519 }
520}
521
522//-----------------------------------------------------------------------------
523
524static int MyWriter(const uint8_t* data, size_t data_size,
525 const WebPPicture* const pic) {
526 FILE* const out = (FILE*)pic->custom_ptr;
527 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
528}
529
530// Dumps a picture as a PGM file using the IMC4 layout.
531static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
532 int y;
533 const int uv_width = (picture->width + 1) / 2;
534 const int uv_height = (picture->height + 1) / 2;
535 const int stride = (picture->width + 1) & ~1;
536 const int height = picture->height + uv_height;
537 FILE* const f = fopen(PGM_name, "wb");
538 if (!f) return 0;
539 fprintf(f, "P5\n%d %d\n255\n", stride, height);
540 for (y = 0; y < picture->height; ++y) {
541 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
542 return 0;
543 if (picture->width & 1) fputc(0, f); // pad
544 }
545 for (y = 0; y < uv_height; ++y) {
546 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
547 return 0;
548 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
549 return 0;
550 }
551 fclose(f);
552 return 1;
553}
554
555//-----------------------------------------------------------------------------
556
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700557static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800558 printf("Usage:\n\n");
559 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
560 printf("where quality is between 0 (poor) to 100 (very good).\n");
561 printf("Typical value is around 80.\n\n");
562 printf("Try -longhelp for an exhaustive list of advanced options.\n");
563}
564
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700565static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800566 printf("Usage:\n");
567 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
568 printf("If input size (-s) for an image is not specified, "
569 "it is assumed to be a PNG or JPEG file.\n");
570#ifdef _WIN32
571 printf("Windows builds can take as input any of the files handled by WIC\n");
572#endif
573 printf("options:\n");
574 printf(" -h / -help ............ short help\n");
575 printf(" -H / -longhelp ........ long help\n");
576 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
577 printf(" -preset <string> ....... Preset setting, one of:\n");
578 printf(" default, photo, picture,\n");
579 printf(" drawing, icon, text\n");
580 printf(" -preset must come first, as it overwrites other parameters.");
581 printf("\n");
582 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
583 printf(" -segments <int> ........ number of segments to use (1..4)\n");
584 printf("\n");
585 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
586 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
587 printf(" -f <int> ............... filter strength (0=off..100)\n");
588 printf(" -sharpness <int> ....... "
589 "filter sharpness (0:most .. 7:least sharp)\n");
590 printf(" -strong ................ use strong filter instead of simple.\n");
591 printf(" -pass <int> ............ analysis pass number (1..10)\n");
592 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
593 printf(" -map <int> ............. print map of extra info.\n");
594 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
595 printf("\n");
596 printf(" -short ................. condense printed message\n");
597 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700598 printf(" -version ............... print version number and exit.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800599 printf(" -v ..................... verbose, e.g. print encoding/decoding "
600 "times\n");
601 printf("\n");
602 printf("Experimental Options:\n");
603 printf(" -size <int> ............ Target size (in bytes)\n");
604 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
605 printf(" -af .................... auto-adjust filter strength.\n");
606 printf(" -pre <int> ............. pre-processing filter\n");
607 printf("\n");
608}
609
610//-----------------------------------------------------------------------------
611
612int main(int argc, const char *argv[]) {
613 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
614 FILE *out = NULL;
615 int c;
616 int short_output = 0;
617 int quiet = 0;
618 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
619 WebPPicture picture;
620 WebPConfig config;
621 WebPAuxStats stats;
622 Stopwatch stop_watch;
623
624 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
625 fprintf(stderr, "Error! Version mismatch!\n");
626 goto Error;
627 }
628
629 if (argc == 1) {
630 HelpShort();
631 return 0;
632 }
633
634 for (c = 1; c < argc; ++c) {
635 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
636 HelpShort();
637 return 0;
638 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
639 HelpLong();
640 return 0;
641 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
642 out_file = argv[++c];
643 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
644 dump_file = argv[++c];
645 config.show_compressed = 1;
646 } else if (!strcmp(argv[c], "-short")) {
647 short_output++;
648 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700649 picture.width = strtol(argv[++c], NULL, 0);
650 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800651 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700652 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800653 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700654 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800655 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700656 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800657 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700658 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800659 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700660 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800661 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700662 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800663 } else if (!strcmp(argv[c], "-af")) {
664 config.autofilter = 1;
665 } else if (!strcmp(argv[c], "-strong") && c < argc - 1) {
666 config.filter_type = 1;
667 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700668 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700670 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800671 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700672 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800673 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700674 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800675 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700676 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800677 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
678 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700679 crop_x = strtol(argv[++c], NULL, 0);
680 crop_y = strtol(argv[++c], NULL, 0);
681 crop_w = strtol(argv[++c], NULL, 0);
682 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino650ffa32011-03-24 16:17:10 -0700683 } else if (!strcmp(argv[c], "-version")) {
684 const int version = WebPGetEncoderVersion();
685 printf("%d.%d.%d\n",
686 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
687 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 } else if (!strcmp(argv[c], "-quiet")) {
689 quiet = 1;
690 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
691 WebPPreset preset;
692 ++c;
693 if (!strcmp(argv[c], "default")) {
694 preset = WEBP_PRESET_DEFAULT;
695 } else if (!strcmp(argv[c], "photo")) {
696 preset = WEBP_PRESET_PHOTO;
697 } else if (!strcmp(argv[c], "picture")) {
698 preset = WEBP_PRESET_PICTURE;
699 } else if (!strcmp(argv[c], "drawing")) {
700 preset = WEBP_PRESET_DRAWING;
701 } else if (!strcmp(argv[c], "icon")) {
702 preset = WEBP_PRESET_ICON;
703 } else if (!strcmp(argv[c], "text")) {
704 preset = WEBP_PRESET_TEXT;
705 } else {
706 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
707 goto Error;
708 }
709 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700710 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 goto Error;
712 }
713 } else if (!strcmp(argv[c], "-v")) {
714 verbose = 1;
715 } else if (argv[c][0] == '-') {
716 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
717 HelpLong();
718 return -1;
719 } else {
720 in_file = argv[c];
721 }
722 }
723
724 if (!WebPValidateConfig(&config)) {
725 fprintf(stderr, "Error! Invalid configuration.\n");
726 goto Error;
727 }
728
Pascal Massimino0744e842011-02-25 12:03:27 -0800729 // Read the input
730 if (verbose)
731 StopwatchReadAndReset(&stop_watch);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700732 if (!ReadPicture(in_file, &picture)) {
733 fprintf(stderr, "Error! Cannot read input picture\n");
734 goto Error;
735 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800736 if (verbose) {
737 const double time = StopwatchReadAndReset(&stop_watch);
738 fprintf(stderr, "Time to read input: %.3fs\n", time);
739 }
740
741 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800742 if (out_file) {
743 out = fopen(out_file, "wb");
744 if (!out) {
745 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
746 goto Error;
747 } else {
748 if (!short_output && !quiet) {
749 fprintf(stderr, "Saving file '%s'\n", out_file);
750 }
751 }
752 picture.writer = MyWriter;
753 picture.custom_ptr = (void*)out;
754 } else {
755 out = NULL;
756 if (!quiet && !short_output) {
757 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
758 fprintf(stderr, "be performed, but its results discarded.\n\n");
759 }
760 }
761 picture.stats = &stats;
762
Pascal Massimino0744e842011-02-25 12:03:27 -0800763 // Compress
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 if (verbose)
765 StopwatchReadAndReset(&stop_watch);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700766 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
767 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800768 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700769 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800770 if (picture.extra_info_type > 0) AllocExtraInfo(&picture);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700771 if (!WebPEncode(&config, &picture)) {
772 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
773 goto Error;
774 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800775 if (verbose) {
776 const double time = StopwatchReadAndReset(&stop_watch);
777 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
778 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800779
780 // Write info
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800781 if (dump_file) DumpPicture(&picture, dump_file);
782 if (!quiet) PrintExtraInfo(&picture, short_output);
783
784 Error:
785 free(picture.extra_info);
786 WebPPictureFree(&picture);
787 if (out != NULL) {
788 fclose(out);
789 }
790
791 return 0;
792}
793
794//-----------------------------------------------------------------------------