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