blob: fd9d76d2b37d501015a4c1ce7415e93a55b6dc65 [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>
14#include <stdlib.h> // for atoi
15#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) {
279 longjmp(png_jmpbuf(png), 1);
280}
281
282static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
283 png_structp png;
284 png_infop info;
285 int color_type, bit_depth, interlaced;
286 int num_passes;
287 int p, y;
288 int ok = 0;
289 png_uint_32 width, height;
290 int stride;
291 uint8_t* rgb = NULL;
292
293 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
294 if (png == NULL) {
295 goto End;
296 }
297
298 png_set_error_fn(png, 0, error_function, NULL);
299 if (setjmp(png_jmpbuf(png))) {
300 Error:
301 png_destroy_read_struct(&png, NULL, NULL);
302 if (rgb) free(rgb);
303 goto End;
304 }
305
306 info = png_create_info_struct(png);
307 if (info == NULL) goto Error;
308
309 png_init_io(png, in_file);
310 png_read_info(png, info);
311 if (!png_get_IHDR(png, info,
312 &width, &height, &bit_depth, &color_type, &interlaced,
313 NULL, NULL)) goto Error;
314
315 png_set_strip_16(png);
316 png_set_packing(png);
317 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
318 if (color_type == PNG_COLOR_TYPE_GRAY) {
319 if (bit_depth < 8) {
320 png_set_expand_gray_1_2_4_to_8(png);
321 }
322 png_set_gray_to_rgb(png);
323 }
324 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
325 png_set_tRNS_to_alpha(png);
326 }
327
328 // TODO(skal): Strip Alpha for now (till Alpha is supported).
329 png_set_strip_alpha(png);
330 num_passes = png_set_interlace_handling(png);
331 png_read_update_info(png, info);
332 stride = 3 * width * sizeof(*rgb);
333 rgb = (uint8_t*)malloc(stride * height);
334 if (rgb == NULL) goto Error;
335 for (p = 0; p < num_passes; ++p) {
336 for (y = 0; y < height; ++y) {
337 png_bytep row = rgb + y * stride;
338 png_read_rows(png, &row, NULL, 1);
339 }
340 }
341 png_read_end(png, info);
342 png_destroy_read_struct(&png, &info, NULL);
343
344 pic->width = width;
345 pic->height = height;
346 ok = WebPPictureImportRGB(pic, rgb, stride);
347 free(rgb);
348
349 End:
350 return ok;
351}
352#else
353static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
354 printf("PNG support not compiled. Please install the libpng development "
355 "package before building.\n");
356 return 0;
357}
358#endif
359
360typedef enum {
361 PNG = 0,
362 JPEG,
363 UNSUPPORTED,
364} InputFileFormat;
365
366static InputFileFormat GetImageType(FILE* in_file) {
367 InputFileFormat format = UNSUPPORTED;
368 unsigned int magic;
369 unsigned char buf[4];
370
371 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
372 (fseek(in_file, 0, SEEK_SET) != 0)) {
373 return format;
374 }
375
376 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
377 if (magic == 0x89504E47U) {
378 format = PNG;
379 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
380 format = JPEG;
381 }
382 return format;
383}
384
385static int ReadPicture(const char* const filename, WebPPicture* const pic) {
386 int ok = 0;
387 FILE* in_file = fopen(filename, "rb");
388 if (in_file == NULL) {
389 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
390 return ok;
391 }
392
393 if (pic->width == 0 || pic->height == 0) {
394 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
395 const InputFileFormat format = GetImageType(in_file);
396 if (format == PNG) {
397 ok = ReadPNG(in_file, pic);
398 } else if (format == JPEG) {
399 ok = ReadJPEG(in_file, pic);
400 }
401 } else {
402 // If image size is specified, infer it as YUV format.
403 ok = ReadYUV(in_file, pic);
404 }
405 if (!ok) {
406 fprintf(stderr, "Error! Could not process file %s\n", filename);
407 }
408
409 fclose(in_file);
410 return ok;
411}
412
413#endif // !_WIN32
414
415static void AllocExtraInfo(WebPPicture* const pic) {
416 const int mb_w = (pic->width + 15) / 16;
417 const int mb_h = (pic->height + 15) / 16;
418 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
419}
420
421static void PrintByteCount(const int bytes[4], int total_size,
422 int* const totals) {
423 int s;
424 int total = 0;
425 for (s = 0; s < 4; ++s) {
426 fprintf(stderr, "| %7d ", bytes[s]);
427 total += bytes[s];
428 if (totals) totals[s] += bytes[s];
429 }
430 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
431}
432
433static void PrintPercents(const int counts[4], int total) {
434 int s;
435 for (s = 0; s < 4; ++s) {
436 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
437 }
438 fprintf(stderr,"| %7d\n", total);
439}
440
441static void PrintValues(const int values[4]) {
442 int s;
443 for (s = 0; s < 4; ++s) {
444 fprintf(stderr, "| %7d ", values[s]);
445 }
446 fprintf(stderr,"|\n");
447}
448
449void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
450 const WebPAuxStats* const stats = pic->stats;
451 if (short_output) {
452 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
453 } else{
454 const int num_i4 = stats->block_count[0];
455 const int num_i16 = stats->block_count[1];
456 const int num_skip = stats->block_count[2];
457 const int total = num_i4 + num_i16;
458 fprintf(stderr,
459 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
460 stats->coded_size,
461 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
462 if (total > 0) {
463 int totals[4] = { 0, 0, 0, 0 };
464 fprintf(stderr, "block count: intra4: %d\n"
465 " intra16: %d (-> %.2f%%)\n",
466 num_i4, num_i16, 100.f * num_i16 / total);
467 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
468 num_skip, 100.f * num_skip / total);
469 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
470 " mode-partition: %6d (%.1f%%)\n",
471 stats->header_bytes[0],
472 100.f * stats->header_bytes[0] / stats->coded_size,
473 stats->header_bytes[1],
474 100.f * stats->header_bytes[1] / stats->coded_size);
475 fprintf(stderr, " Residuals bytes "
476 "|segment 1|segment 2|segment 3"
477 "|segment 4| total\n");
478 fprintf(stderr, " intra4-coeffs: ");
479 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
480 fprintf(stderr, " intra16-coeffs: ");
481 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
482 fprintf(stderr, " chroma coeffs: ");
483 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
484 fprintf(stderr, " macroblocks: ");
485 PrintPercents(stats->segment_size, total);
486 fprintf(stderr, " quantizer: ");
487 PrintValues(stats->segment_quant);
488 fprintf(stderr, " filter level: ");
489 PrintValues(stats->segment_level);
490 fprintf(stderr, "------------------+---------");
491 fprintf(stderr, "+---------+---------+---------+-----------------\n");
492 fprintf(stderr, " segments total: ");
493 PrintByteCount(totals, stats->coded_size, NULL);
494 }
495 }
496 if (pic->extra_info) {
497 const int mb_w = (pic->width + 15) / 16;
498 const int mb_h = (pic->height + 15) / 16;
499 const int type = pic->extra_info_type;
500 int x, y;
501 for (y = 0; y < mb_h; ++y) {
502 for (x = 0; x < mb_w; ++x) {
503 const int c = pic->extra_info[x + y * mb_w];
504 if (type == 1) { // intra4/intra16
505 printf("%c", "+."[c]);
506 } else if (type == 2) { // segments
507 printf("%c", ".-*X"[c]);
508 } else if (type == 3) { // quantizers
509 printf("%.2d ", c);
510 } else if (type == 6 || type == 7) {
511 printf("%3d ", c);
512 } else {
513 printf("0x%.2x ", c);
514 }
515 }
516 printf("\n");
517 }
518 }
519}
520
521//-----------------------------------------------------------------------------
522
523static int MyWriter(const uint8_t* data, size_t data_size,
524 const WebPPicture* const pic) {
525 FILE* const out = (FILE*)pic->custom_ptr;
526 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
527}
528
529// Dumps a picture as a PGM file using the IMC4 layout.
530static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
531 int y;
532 const int uv_width = (picture->width + 1) / 2;
533 const int uv_height = (picture->height + 1) / 2;
534 const int stride = (picture->width + 1) & ~1;
535 const int height = picture->height + uv_height;
536 FILE* const f = fopen(PGM_name, "wb");
537 if (!f) return 0;
538 fprintf(f, "P5\n%d %d\n255\n", stride, height);
539 for (y = 0; y < picture->height; ++y) {
540 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
541 return 0;
542 if (picture->width & 1) fputc(0, f); // pad
543 }
544 for (y = 0; y < uv_height; ++y) {
545 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
546 return 0;
547 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
548 return 0;
549 }
550 fclose(f);
551 return 1;
552}
553
554//-----------------------------------------------------------------------------
555
556static void HelpShort() {
557 printf("Usage:\n\n");
558 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
559 printf("where quality is between 0 (poor) to 100 (very good).\n");
560 printf("Typical value is around 80.\n\n");
561 printf("Try -longhelp for an exhaustive list of advanced options.\n");
562}
563
564static void HelpLong() {
565 printf("Usage:\n");
566 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
567 printf("If input size (-s) for an image is not specified, "
568 "it is assumed to be a PNG or JPEG file.\n");
569#ifdef _WIN32
570 printf("Windows builds can take as input any of the files handled by WIC\n");
571#endif
572 printf("options:\n");
573 printf(" -h / -help ............ short help\n");
574 printf(" -H / -longhelp ........ long help\n");
575 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
576 printf(" -preset <string> ....... Preset setting, one of:\n");
577 printf(" default, photo, picture,\n");
578 printf(" drawing, icon, text\n");
579 printf(" -preset must come first, as it overwrites other parameters.");
580 printf("\n");
581 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
582 printf(" -segments <int> ........ number of segments to use (1..4)\n");
583 printf("\n");
584 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
585 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
586 printf(" -f <int> ............... filter strength (0=off..100)\n");
587 printf(" -sharpness <int> ....... "
588 "filter sharpness (0:most .. 7:least sharp)\n");
589 printf(" -strong ................ use strong filter instead of simple.\n");
590 printf(" -pass <int> ............ analysis pass number (1..10)\n");
591 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
592 printf(" -map <int> ............. print map of extra info.\n");
593 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
594 printf("\n");
595 printf(" -short ................. condense printed message\n");
596 printf(" -quiet ................. don't print anything.\n");
597 printf(" -v ..................... verbose, e.g. print encoding/decoding "
598 "times\n");
599 printf("\n");
600 printf("Experimental Options:\n");
601 printf(" -size <int> ............ Target size (in bytes)\n");
602 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
603 printf(" -af .................... auto-adjust filter strength.\n");
604 printf(" -pre <int> ............. pre-processing filter\n");
605 printf("\n");
606}
607
608//-----------------------------------------------------------------------------
609
610int main(int argc, const char *argv[]) {
611 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
612 FILE *out = NULL;
613 int c;
614 int short_output = 0;
615 int quiet = 0;
616 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
617 WebPPicture picture;
618 WebPConfig config;
619 WebPAuxStats stats;
620 Stopwatch stop_watch;
621
622 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
623 fprintf(stderr, "Error! Version mismatch!\n");
624 goto Error;
625 }
626
627 if (argc == 1) {
628 HelpShort();
629 return 0;
630 }
631
632 for (c = 1; c < argc; ++c) {
633 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
634 HelpShort();
635 return 0;
636 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
637 HelpLong();
638 return 0;
639 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
640 out_file = argv[++c];
641 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
642 dump_file = argv[++c];
643 config.show_compressed = 1;
644 } else if (!strcmp(argv[c], "-short")) {
645 short_output++;
646 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
647 picture.width = atoi(argv[++c]);
648 picture.height = atoi(argv[++c]);
649 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
650 config.method = atoi(argv[++c]);
651 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
652 config.quality = (float)atof(argv[++c]);
653 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
654 config.target_size = atoi(argv[++c]);
655 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
656 config.target_PSNR = (float)atof(argv[++c]);
657 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
658 config.sns_strength = atoi(argv[++c]);
659 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
660 config.filter_strength = atoi(argv[++c]);
661 } else if (!strcmp(argv[c], "-af")) {
662 config.autofilter = 1;
663 } else if (!strcmp(argv[c], "-strong") && c < argc - 1) {
664 config.filter_type = 1;
665 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
666 config.filter_sharpness = atoi(argv[++c]);
667 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
668 config.pass = atoi(argv[++c]);
669 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
670 config.preprocessing = atoi(argv[++c]);
671 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
672 config.segments = atoi(argv[++c]);
673 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
674 picture.extra_info_type = atoi(argv[++c]);
675 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
676 crop = 1;
677 crop_x = atoi(argv[++c]);
678 crop_y = atoi(argv[++c]);
679 crop_w = atoi(argv[++c]);
680 crop_h = atoi(argv[++c]);
681 } else if (!strcmp(argv[c], "-quiet")) {
682 quiet = 1;
683 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
684 WebPPreset preset;
685 ++c;
686 if (!strcmp(argv[c], "default")) {
687 preset = WEBP_PRESET_DEFAULT;
688 } else if (!strcmp(argv[c], "photo")) {
689 preset = WEBP_PRESET_PHOTO;
690 } else if (!strcmp(argv[c], "picture")) {
691 preset = WEBP_PRESET_PICTURE;
692 } else if (!strcmp(argv[c], "drawing")) {
693 preset = WEBP_PRESET_DRAWING;
694 } else if (!strcmp(argv[c], "icon")) {
695 preset = WEBP_PRESET_ICON;
696 } else if (!strcmp(argv[c], "text")) {
697 preset = WEBP_PRESET_TEXT;
698 } else {
699 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
700 goto Error;
701 }
702 if (!WebPConfigPreset(&config, preset, config.quality)) {
703 fprintf(stderr, "Error! Could initialize configuration with preset.");
704 goto Error;
705 }
706 } else if (!strcmp(argv[c], "-v")) {
707 verbose = 1;
708 } else if (argv[c][0] == '-') {
709 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
710 HelpLong();
711 return -1;
712 } else {
713 in_file = argv[c];
714 }
715 }
716
717 if (!WebPValidateConfig(&config)) {
718 fprintf(stderr, "Error! Invalid configuration.\n");
719 goto Error;
720 }
721
Pascal Massimino0744e842011-02-25 12:03:27 -0800722 // Read the input
723 if (verbose)
724 StopwatchReadAndReset(&stop_watch);
725 if (!ReadPicture(in_file, &picture)) goto Error;
726 if (verbose) {
727 const double time = StopwatchReadAndReset(&stop_watch);
728 fprintf(stderr, "Time to read input: %.3fs\n", time);
729 }
730
731 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800732 if (out_file) {
733 out = fopen(out_file, "wb");
734 if (!out) {
735 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
736 goto Error;
737 } else {
738 if (!short_output && !quiet) {
739 fprintf(stderr, "Saving file '%s'\n", out_file);
740 }
741 }
742 picture.writer = MyWriter;
743 picture.custom_ptr = (void*)out;
744 } else {
745 out = NULL;
746 if (!quiet && !short_output) {
747 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
748 fprintf(stderr, "be performed, but its results discarded.\n\n");
749 }
750 }
751 picture.stats = &stats;
752
Pascal Massimino0744e842011-02-25 12:03:27 -0800753 // Compress
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800754 if (verbose)
755 StopwatchReadAndReset(&stop_watch);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800756 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h))
757 goto Error;
758 if (picture.extra_info_type > 0) AllocExtraInfo(&picture);
759 if (!WebPEncode(&config, &picture)) goto Error;
760 if (verbose) {
761 const double time = StopwatchReadAndReset(&stop_watch);
762 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
763 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800764
765 // Write info
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800766 if (dump_file) DumpPicture(&picture, dump_file);
767 if (!quiet) PrintExtraInfo(&picture, short_output);
768
769 Error:
770 free(picture.extra_info);
771 WebPPictureFree(&picture);
772 if (out != NULL) {
773 fclose(out);
774 }
775
776 return 0;
777}
778
779//-----------------------------------------------------------------------------