blob: 9ad5e7b053e600db706ee0edec099c6624f235d9 [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"
Pascal Massiminof7d9e262011-04-26 11:02:38 -070039extern void* VP8GetCPUInfo; // opaque forward declaration.
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070040
Pascal Massiminof61d14a2011-02-18 23:33:46 -080041//-----------------------------------------------------------------------------
42
43static int verbose = 0;
44
45static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
46 const int uv_width = (pic->width + 1) / 2;
47 const int uv_height = (pic->height + 1) / 2;
48 int y;
49 int ok = 0;
50
51 if (!WebPPictureAlloc(pic)) return ok;
52
53 for (y = 0; y < pic->height; ++y) {
54 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
55 goto End;
56 }
57 }
58 for (y = 0; y < uv_height; ++y) {
59 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
60 goto End;
61 }
62 for (y = 0; y < uv_height; ++y) {
63 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
64 goto End;
65 }
66 ok = 1;
67
68 End:
69 return ok;
70}
71
72#ifdef _WIN32
73
74#define IFS(fn) \
75 do { \
76 if (SUCCEEDED(hr)) \
77 { \
78 hr = (fn); \
79 if (FAILED(hr) && verbose) \
80 printf(#fn " failed %08x\n", hr); \
81 } \
82 } while (0)
83
84#ifdef __cplusplus
85#define MAKE_REFGUID(x) (x)
86#else
87#define MAKE_REFGUID(x) &(x)
88#endif
89
90static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
91 HRESULT hr = S_OK;
92 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
93 if (FAILED(hr))
94 printf("Error opening input file %s (%08x)\n", filename, hr);
95 return hr;
96}
97
98static HRESULT ReadPictureWithWIC(const char* filename,
99 WebPPicture* const pic) {
100 HRESULT hr = S_OK;
101 IWICBitmapFrameDecode* pFrame = NULL;
102 IWICFormatConverter* pConverter = NULL;
103 IWICImagingFactory* pFactory = NULL;
104 IWICBitmapDecoder* pDecoder = NULL;
105 IStream* pStream = NULL;
106 UINT frameCount = 0;
107 UINT width, height = 0;
108 BYTE* rgb = NULL;
109
110 IFS(CoInitialize(NULL));
111 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
112 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
113 (LPVOID*)&pFactory));
114 if (hr == REGDB_E_CLASSNOTREG) {
115 printf("Couldn't access Windows Imaging Component (are you running \n");
116 printf("Windows XP SP3 or newer?). Most formats not available.\n");
117 printf("Use -s for the available YUV input.\n");
118 }
119 // Prepare for image decoding.
120 IFS(OpenInputStream(filename, &pStream));
121 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
122 WICDecodeMetadataCacheOnDemand, &pDecoder));
123 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
124 if (SUCCEEDED(hr) && frameCount == 0) {
125 printf("No frame found in input file.\n");
126 hr = E_FAIL;
127 }
128 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
129
130 // Prepare for pixel format conversion (if necessary).
131 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
132 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
133 MAKE_REFGUID(GUID_WICPixelFormat24bppRGB), WICBitmapDitherTypeNone,
134 NULL, 0.0, WICBitmapPaletteTypeCustom));
135
136 // Decode.
137 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
138 if (SUCCEEDED(hr)) {
139 rgb = (BYTE*)malloc(3 * width * height);
140 if (rgb == NULL)
141 hr = E_OUTOFMEMORY;
142 }
143 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, 3 * width,
144 3 * width * height, rgb));
145
146 // WebP conversion.
147 if (SUCCEEDED(hr)) {
148 pic->width = width;
149 pic->height = height;
150 if (!WebPPictureImportRGB(pic, rgb, 3 * width))
151 hr = E_FAIL;
152 }
153
154 // Cleanup.
155 if (pConverter != NULL) IUnknown_Release(pConverter);
156 if (pFrame != NULL) IUnknown_Release(pFrame);
157 if (pDecoder != NULL) IUnknown_Release(pDecoder);
158 if (pFactory != NULL) IUnknown_Release(pFactory);
159 if (pStream != NULL) IUnknown_Release(pStream);
160 free(rgb);
161 return hr;
162}
163
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700164static int ReadPicture(const char* const filename, WebPPicture* const pic,
165 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800166 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
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700285static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800286 png_structp png;
287 png_infop info;
288 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700289 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800290 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700291 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800292 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700293 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800294 int stride;
295 uint8_t* rgb = NULL;
296
297 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
298 if (png == NULL) {
299 goto End;
300 }
301
302 png_set_error_fn(png, 0, error_function, NULL);
303 if (setjmp(png_jmpbuf(png))) {
304 Error:
305 png_destroy_read_struct(&png, NULL, NULL);
306 if (rgb) free(rgb);
307 goto End;
308 }
309
310 info = png_create_info_struct(png);
311 if (info == NULL) goto Error;
312
313 png_init_io(png, in_file);
314 png_read_info(png, info);
315 if (!png_get_IHDR(png, info,
316 &width, &height, &bit_depth, &color_type, &interlaced,
317 NULL, NULL)) goto Error;
318
319 png_set_strip_16(png);
320 png_set_packing(png);
321 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
322 if (color_type == PNG_COLOR_TYPE_GRAY) {
323 if (bit_depth < 8) {
324 png_set_expand_gray_1_2_4_to_8(png);
325 }
326 png_set_gray_to_rgb(png);
327 }
328 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
329 png_set_tRNS_to_alpha(png);
330 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700331 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800332
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700333 if (!keep_alpha) {
334 png_set_strip_alpha(png);
335 has_alpha = 0;
336 }
337
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800338 num_passes = png_set_interlace_handling(png);
339 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700340 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800341 rgb = (uint8_t*)malloc(stride * height);
342 if (rgb == NULL) goto Error;
343 for (p = 0; p < num_passes; ++p) {
344 for (y = 0; y < height; ++y) {
345 png_bytep row = rgb + y * stride;
346 png_read_rows(png, &row, NULL, 1);
347 }
348 }
349 png_read_end(png, info);
350 png_destroy_read_struct(&png, &info, NULL);
351
352 pic->width = width;
353 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700354 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
355 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800356 free(rgb);
357
358 End:
359 return ok;
360}
361#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700362static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800363 printf("PNG support not compiled. Please install the libpng development "
364 "package before building.\n");
365 return 0;
366}
367#endif
368
369typedef enum {
370 PNG = 0,
371 JPEG,
372 UNSUPPORTED,
373} InputFileFormat;
374
375static InputFileFormat GetImageType(FILE* in_file) {
376 InputFileFormat format = UNSUPPORTED;
377 unsigned int magic;
378 unsigned char buf[4];
379
380 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
381 (fseek(in_file, 0, SEEK_SET) != 0)) {
382 return format;
383 }
384
385 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
386 if (magic == 0x89504E47U) {
387 format = PNG;
388 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
389 format = JPEG;
390 }
391 return format;
392}
393
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700394static int ReadPicture(const char* const filename, WebPPicture* const pic,
395 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800396 int ok = 0;
397 FILE* in_file = fopen(filename, "rb");
398 if (in_file == NULL) {
399 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
400 return ok;
401 }
402
403 if (pic->width == 0 || pic->height == 0) {
404 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
405 const InputFileFormat format = GetImageType(in_file);
406 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700407 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800408 } else if (format == JPEG) {
409 ok = ReadJPEG(in_file, pic);
410 }
411 } else {
412 // If image size is specified, infer it as YUV format.
413 ok = ReadYUV(in_file, pic);
414 }
415 if (!ok) {
416 fprintf(stderr, "Error! Could not process file %s\n", filename);
417 }
418
419 fclose(in_file);
420 return ok;
421}
422
423#endif // !_WIN32
424
425static void AllocExtraInfo(WebPPicture* const pic) {
426 const int mb_w = (pic->width + 15) / 16;
427 const int mb_h = (pic->height + 15) / 16;
428 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
429}
430
431static void PrintByteCount(const int bytes[4], int total_size,
432 int* const totals) {
433 int s;
434 int total = 0;
435 for (s = 0; s < 4; ++s) {
436 fprintf(stderr, "| %7d ", bytes[s]);
437 total += bytes[s];
438 if (totals) totals[s] += bytes[s];
439 }
440 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
441}
442
443static void PrintPercents(const int counts[4], int total) {
444 int s;
445 for (s = 0; s < 4; ++s) {
446 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
447 }
448 fprintf(stderr,"| %7d\n", total);
449}
450
451static void PrintValues(const int values[4]) {
452 int s;
453 for (s = 0; s < 4; ++s) {
454 fprintf(stderr, "| %7d ", values[s]);
455 }
456 fprintf(stderr,"|\n");
457}
458
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700459static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800460 const WebPAuxStats* const stats = pic->stats;
461 if (short_output) {
462 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
463 } else{
464 const int num_i4 = stats->block_count[0];
465 const int num_i16 = stats->block_count[1];
466 const int num_skip = stats->block_count[2];
467 const int total = num_i4 + num_i16;
468 fprintf(stderr,
469 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
470 stats->coded_size,
471 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
472 if (total > 0) {
473 int totals[4] = { 0, 0, 0, 0 };
474 fprintf(stderr, "block count: intra4: %d\n"
475 " intra16: %d (-> %.2f%%)\n",
476 num_i4, num_i16, 100.f * num_i16 / total);
477 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
478 num_skip, 100.f * num_skip / total);
479 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
480 " mode-partition: %6d (%.1f%%)\n",
481 stats->header_bytes[0],
482 100.f * stats->header_bytes[0] / stats->coded_size,
483 stats->header_bytes[1],
484 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700485 if (stats->alpha_data_size) {
486 fprintf(stderr, " transparency: %6d\n",
487 stats->alpha_data_size);
488 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800489 fprintf(stderr, " Residuals bytes "
490 "|segment 1|segment 2|segment 3"
491 "|segment 4| total\n");
492 fprintf(stderr, " intra4-coeffs: ");
493 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
494 fprintf(stderr, " intra16-coeffs: ");
495 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
496 fprintf(stderr, " chroma coeffs: ");
497 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
498 fprintf(stderr, " macroblocks: ");
499 PrintPercents(stats->segment_size, total);
500 fprintf(stderr, " quantizer: ");
501 PrintValues(stats->segment_quant);
502 fprintf(stderr, " filter level: ");
503 PrintValues(stats->segment_level);
504 fprintf(stderr, "------------------+---------");
505 fprintf(stderr, "+---------+---------+---------+-----------------\n");
506 fprintf(stderr, " segments total: ");
507 PrintByteCount(totals, stats->coded_size, NULL);
508 }
509 }
510 if (pic->extra_info) {
511 const int mb_w = (pic->width + 15) / 16;
512 const int mb_h = (pic->height + 15) / 16;
513 const int type = pic->extra_info_type;
514 int x, y;
515 for (y = 0; y < mb_h; ++y) {
516 for (x = 0; x < mb_w; ++x) {
517 const int c = pic->extra_info[x + y * mb_w];
518 if (type == 1) { // intra4/intra16
519 printf("%c", "+."[c]);
520 } else if (type == 2) { // segments
521 printf("%c", ".-*X"[c]);
522 } else if (type == 3) { // quantizers
523 printf("%.2d ", c);
524 } else if (type == 6 || type == 7) {
525 printf("%3d ", c);
526 } else {
527 printf("0x%.2x ", c);
528 }
529 }
530 printf("\n");
531 }
532 }
533}
534
535//-----------------------------------------------------------------------------
536
537static int MyWriter(const uint8_t* data, size_t data_size,
538 const WebPPicture* const pic) {
539 FILE* const out = (FILE*)pic->custom_ptr;
540 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
541}
542
543// Dumps a picture as a PGM file using the IMC4 layout.
544static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
545 int y;
546 const int uv_width = (picture->width + 1) / 2;
547 const int uv_height = (picture->height + 1) / 2;
548 const int stride = (picture->width + 1) & ~1;
549 const int height = picture->height + uv_height;
550 FILE* const f = fopen(PGM_name, "wb");
551 if (!f) return 0;
552 fprintf(f, "P5\n%d %d\n255\n", stride, height);
553 for (y = 0; y < picture->height; ++y) {
554 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
555 return 0;
556 if (picture->width & 1) fputc(0, f); // pad
557 }
558 for (y = 0; y < uv_height; ++y) {
559 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
560 return 0;
561 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
562 return 0;
563 }
564 fclose(f);
565 return 1;
566}
567
568//-----------------------------------------------------------------------------
569
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700570static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800571 printf("Usage:\n\n");
572 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
573 printf("where quality is between 0 (poor) to 100 (very good).\n");
574 printf("Typical value is around 80.\n\n");
575 printf("Try -longhelp for an exhaustive list of advanced options.\n");
576}
577
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700578static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800579 printf("Usage:\n");
580 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
581 printf("If input size (-s) for an image is not specified, "
582 "it is assumed to be a PNG or JPEG file.\n");
583#ifdef _WIN32
584 printf("Windows builds can take as input any of the files handled by WIC\n");
585#endif
586 printf("options:\n");
587 printf(" -h / -help ............ short help\n");
588 printf(" -H / -longhelp ........ long help\n");
589 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
590 printf(" -preset <string> ....... Preset setting, one of:\n");
591 printf(" default, photo, picture,\n");
592 printf(" drawing, icon, text\n");
593 printf(" -preset must come first, as it overwrites other parameters.");
594 printf("\n");
595 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
596 printf(" -segments <int> ........ number of segments to use (1..4)\n");
597 printf("\n");
598 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
599 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
600 printf(" -f <int> ............... filter strength (0=off..100)\n");
601 printf(" -sharpness <int> ....... "
602 "filter sharpness (0:most .. 7:least sharp)\n");
603 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700604 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
605 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800606 printf(" -pass <int> ............ analysis pass number (1..10)\n");
607 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
608 printf(" -map <int> ............. print map of extra info.\n");
609 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
610 printf("\n");
611 printf(" -short ................. condense printed message\n");
612 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700613 printf(" -version ............... print version number and exit.\n");
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700614 printf(" -noasm ................. disable all assembly optimizations.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800615 printf(" -v ..................... verbose, e.g. print encoding/decoding "
616 "times\n");
617 printf("\n");
618 printf("Experimental Options:\n");
619 printf(" -size <int> ............ Target size (in bytes)\n");
620 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
621 printf(" -af .................... auto-adjust filter strength.\n");
622 printf(" -pre <int> ............. pre-processing filter\n");
623 printf("\n");
624}
625
626//-----------------------------------------------------------------------------
627
628int main(int argc, const char *argv[]) {
629 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
630 FILE *out = NULL;
631 int c;
632 int short_output = 0;
633 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700634 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
636 WebPPicture picture;
637 WebPConfig config;
638 WebPAuxStats stats;
639 Stopwatch stop_watch;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700640#ifdef WEBP_EXPERIMENTAL_FEATURES
641 keep_alpha = 1;
642#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800643 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
644 fprintf(stderr, "Error! Version mismatch!\n");
645 goto Error;
646 }
647
648 if (argc == 1) {
649 HelpShort();
650 return 0;
651 }
652
653 for (c = 1; c < argc; ++c) {
654 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
655 HelpShort();
656 return 0;
657 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
658 HelpLong();
659 return 0;
660 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
661 out_file = argv[++c];
662 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
663 dump_file = argv[++c];
664 config.show_compressed = 1;
665 } else if (!strcmp(argv[c], "-short")) {
666 short_output++;
667 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700668 picture.width = strtol(argv[++c], NULL, 0);
669 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800670 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700671 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800672 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700673 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700675 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800676 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700677 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800678 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700679 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800680 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700681 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800682 } else if (!strcmp(argv[c], "-af")) {
683 config.autofilter = 1;
684 } else if (!strcmp(argv[c], "-strong") && c < argc - 1) {
685 config.filter_type = 1;
686 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700687 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700689 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800690 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700691 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800692 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700693 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700694 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
695 config.alpha_compression = strtol(argv[++c], NULL, 0);
696 } else if (!strcmp(argv[c], "-noalpha")) {
697 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800698 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700699 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
701 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700702 crop_x = strtol(argv[++c], NULL, 0);
703 crop_y = strtol(argv[++c], NULL, 0);
704 crop_w = strtol(argv[++c], NULL, 0);
705 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700706 } else if (!strcmp(argv[c], "-noasm")) {
707 VP8GetCPUInfo = NULL;
Pascal Massimino650ffa32011-03-24 16:17:10 -0700708 } else if (!strcmp(argv[c], "-version")) {
709 const int version = WebPGetEncoderVersion();
710 printf("%d.%d.%d\n",
711 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
712 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 } else if (!strcmp(argv[c], "-quiet")) {
714 quiet = 1;
715 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
716 WebPPreset preset;
717 ++c;
718 if (!strcmp(argv[c], "default")) {
719 preset = WEBP_PRESET_DEFAULT;
720 } else if (!strcmp(argv[c], "photo")) {
721 preset = WEBP_PRESET_PHOTO;
722 } else if (!strcmp(argv[c], "picture")) {
723 preset = WEBP_PRESET_PICTURE;
724 } else if (!strcmp(argv[c], "drawing")) {
725 preset = WEBP_PRESET_DRAWING;
726 } else if (!strcmp(argv[c], "icon")) {
727 preset = WEBP_PRESET_ICON;
728 } else if (!strcmp(argv[c], "text")) {
729 preset = WEBP_PRESET_TEXT;
730 } else {
731 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
732 goto Error;
733 }
734 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700735 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800736 goto Error;
737 }
738 } else if (!strcmp(argv[c], "-v")) {
739 verbose = 1;
740 } else if (argv[c][0] == '-') {
741 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
742 HelpLong();
743 return -1;
744 } else {
745 in_file = argv[c];
746 }
747 }
748
749 if (!WebPValidateConfig(&config)) {
750 fprintf(stderr, "Error! Invalid configuration.\n");
751 goto Error;
752 }
753
Pascal Massimino0744e842011-02-25 12:03:27 -0800754 // Read the input
755 if (verbose)
756 StopwatchReadAndReset(&stop_watch);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700757 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700758 fprintf(stderr, "Error! Cannot read input picture\n");
759 goto Error;
760 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800761 if (verbose) {
762 const double time = StopwatchReadAndReset(&stop_watch);
763 fprintf(stderr, "Time to read input: %.3fs\n", time);
764 }
765
766 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800767 if (out_file) {
768 out = fopen(out_file, "wb");
769 if (!out) {
770 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
771 goto Error;
772 } else {
773 if (!short_output && !quiet) {
774 fprintf(stderr, "Saving file '%s'\n", out_file);
775 }
776 }
777 picture.writer = MyWriter;
778 picture.custom_ptr = (void*)out;
779 } else {
780 out = NULL;
781 if (!quiet && !short_output) {
782 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
783 fprintf(stderr, "be performed, but its results discarded.\n\n");
784 }
785 }
786 picture.stats = &stats;
787
Pascal Massimino0744e842011-02-25 12:03:27 -0800788 // Compress
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 if (verbose)
790 StopwatchReadAndReset(&stop_watch);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700791 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
792 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800793 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700794 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800795 if (picture.extra_info_type > 0) AllocExtraInfo(&picture);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700796 if (!WebPEncode(&config, &picture)) {
797 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
798 goto Error;
799 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800800 if (verbose) {
801 const double time = StopwatchReadAndReset(&stop_watch);
802 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
803 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800804
805 // Write info
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800806 if (dump_file) DumpPicture(&picture, dump_file);
807 if (!quiet) PrintExtraInfo(&picture, short_output);
808
809 Error:
810 free(picture.extra_info);
811 WebPPictureFree(&picture);
812 if (out != NULL) {
813 fclose(out);
814 }
815
816 return 0;
817}
818
819//-----------------------------------------------------------------------------