blob: 085ca808c72084fbce302e6cb48c7fde9cd5d2a8 [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 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700337#ifdef WEBP_EXPERIMENTAL_FEATURES
338 if (has_alpha) {
339 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
340 }
341#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700342
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800343 num_passes = png_set_interlace_handling(png);
344 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700345 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800346 rgb = (uint8_t*)malloc(stride * height);
347 if (rgb == NULL) goto Error;
348 for (p = 0; p < num_passes; ++p) {
349 for (y = 0; y < height; ++y) {
350 png_bytep row = rgb + y * stride;
351 png_read_rows(png, &row, NULL, 1);
352 }
353 }
354 png_read_end(png, info);
355 png_destroy_read_struct(&png, &info, NULL);
356
357 pic->width = width;
358 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700359 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
360 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800361 free(rgb);
362
363 End:
364 return ok;
365}
366#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700367static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800368 printf("PNG support not compiled. Please install the libpng development "
369 "package before building.\n");
370 return 0;
371}
372#endif
373
374typedef enum {
375 PNG = 0,
376 JPEG,
377 UNSUPPORTED,
378} InputFileFormat;
379
380static InputFileFormat GetImageType(FILE* in_file) {
381 InputFileFormat format = UNSUPPORTED;
382 unsigned int magic;
383 unsigned char buf[4];
384
385 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
386 (fseek(in_file, 0, SEEK_SET) != 0)) {
387 return format;
388 }
389
390 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
391 if (magic == 0x89504E47U) {
392 format = PNG;
393 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
394 format = JPEG;
395 }
396 return format;
397}
398
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700399static int ReadPicture(const char* const filename, WebPPicture* const pic,
400 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800401 int ok = 0;
402 FILE* in_file = fopen(filename, "rb");
403 if (in_file == NULL) {
404 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
405 return ok;
406 }
407
408 if (pic->width == 0 || pic->height == 0) {
409 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
410 const InputFileFormat format = GetImageType(in_file);
411 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700412 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800413 } else if (format == JPEG) {
414 ok = ReadJPEG(in_file, pic);
415 }
416 } else {
417 // If image size is specified, infer it as YUV format.
418 ok = ReadYUV(in_file, pic);
419 }
420 if (!ok) {
421 fprintf(stderr, "Error! Could not process file %s\n", filename);
422 }
423
424 fclose(in_file);
425 return ok;
426}
427
428#endif // !_WIN32
429
430static void AllocExtraInfo(WebPPicture* const pic) {
431 const int mb_w = (pic->width + 15) / 16;
432 const int mb_h = (pic->height + 15) / 16;
433 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
434}
435
436static void PrintByteCount(const int bytes[4], int total_size,
437 int* const totals) {
438 int s;
439 int total = 0;
440 for (s = 0; s < 4; ++s) {
441 fprintf(stderr, "| %7d ", bytes[s]);
442 total += bytes[s];
443 if (totals) totals[s] += bytes[s];
444 }
445 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
446}
447
448static void PrintPercents(const int counts[4], int total) {
449 int s;
450 for (s = 0; s < 4; ++s) {
451 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
452 }
453 fprintf(stderr,"| %7d\n", total);
454}
455
456static void PrintValues(const int values[4]) {
457 int s;
458 for (s = 0; s < 4; ++s) {
459 fprintf(stderr, "| %7d ", values[s]);
460 }
461 fprintf(stderr,"|\n");
462}
463
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700464static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800465 const WebPAuxStats* const stats = pic->stats;
466 if (short_output) {
467 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
468 } else{
469 const int num_i4 = stats->block_count[0];
470 const int num_i16 = stats->block_count[1];
471 const int num_skip = stats->block_count[2];
472 const int total = num_i4 + num_i16;
473 fprintf(stderr,
474 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
475 stats->coded_size,
476 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
477 if (total > 0) {
478 int totals[4] = { 0, 0, 0, 0 };
479 fprintf(stderr, "block count: intra4: %d\n"
480 " intra16: %d (-> %.2f%%)\n",
481 num_i4, num_i16, 100.f * num_i16 / total);
482 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
483 num_skip, 100.f * num_skip / total);
484 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
485 " mode-partition: %6d (%.1f%%)\n",
486 stats->header_bytes[0],
487 100.f * stats->header_bytes[0] / stats->coded_size,
488 stats->header_bytes[1],
489 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700490 if (stats->alpha_data_size) {
491 fprintf(stderr, " transparency: %6d\n",
492 stats->alpha_data_size);
493 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700494 if (stats->layer_data_size) {
495 fprintf(stderr, " enhancement: %6d\n",
496 stats->layer_data_size);
497 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800498 fprintf(stderr, " Residuals bytes "
499 "|segment 1|segment 2|segment 3"
500 "|segment 4| total\n");
501 fprintf(stderr, " intra4-coeffs: ");
502 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
503 fprintf(stderr, " intra16-coeffs: ");
504 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
505 fprintf(stderr, " chroma coeffs: ");
506 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
507 fprintf(stderr, " macroblocks: ");
508 PrintPercents(stats->segment_size, total);
509 fprintf(stderr, " quantizer: ");
510 PrintValues(stats->segment_quant);
511 fprintf(stderr, " filter level: ");
512 PrintValues(stats->segment_level);
513 fprintf(stderr, "------------------+---------");
514 fprintf(stderr, "+---------+---------+---------+-----------------\n");
515 fprintf(stderr, " segments total: ");
516 PrintByteCount(totals, stats->coded_size, NULL);
517 }
518 }
519 if (pic->extra_info) {
520 const int mb_w = (pic->width + 15) / 16;
521 const int mb_h = (pic->height + 15) / 16;
522 const int type = pic->extra_info_type;
523 int x, y;
524 for (y = 0; y < mb_h; ++y) {
525 for (x = 0; x < mb_w; ++x) {
526 const int c = pic->extra_info[x + y * mb_w];
527 if (type == 1) { // intra4/intra16
528 printf("%c", "+."[c]);
529 } else if (type == 2) { // segments
530 printf("%c", ".-*X"[c]);
531 } else if (type == 3) { // quantizers
532 printf("%.2d ", c);
533 } else if (type == 6 || type == 7) {
534 printf("%3d ", c);
535 } else {
536 printf("0x%.2x ", c);
537 }
538 }
539 printf("\n");
540 }
541 }
542}
543
544//-----------------------------------------------------------------------------
545
546static int MyWriter(const uint8_t* data, size_t data_size,
547 const WebPPicture* const pic) {
548 FILE* const out = (FILE*)pic->custom_ptr;
549 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
550}
551
552// Dumps a picture as a PGM file using the IMC4 layout.
553static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
554 int y;
555 const int uv_width = (picture->width + 1) / 2;
556 const int uv_height = (picture->height + 1) / 2;
557 const int stride = (picture->width + 1) & ~1;
558 const int height = picture->height + uv_height;
559 FILE* const f = fopen(PGM_name, "wb");
560 if (!f) return 0;
561 fprintf(f, "P5\n%d %d\n255\n", stride, height);
562 for (y = 0; y < picture->height; ++y) {
563 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
564 return 0;
565 if (picture->width & 1) fputc(0, f); // pad
566 }
567 for (y = 0; y < uv_height; ++y) {
568 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
569 return 0;
570 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
571 return 0;
572 }
573 fclose(f);
574 return 1;
575}
576
577//-----------------------------------------------------------------------------
578
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700579static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800580 printf("Usage:\n\n");
581 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
582 printf("where quality is between 0 (poor) to 100 (very good).\n");
583 printf("Typical value is around 80.\n\n");
584 printf("Try -longhelp for an exhaustive list of advanced options.\n");
585}
586
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700587static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800588 printf("Usage:\n");
589 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
590 printf("If input size (-s) for an image is not specified, "
591 "it is assumed to be a PNG or JPEG file.\n");
592#ifdef _WIN32
593 printf("Windows builds can take as input any of the files handled by WIC\n");
594#endif
595 printf("options:\n");
596 printf(" -h / -help ............ short help\n");
597 printf(" -H / -longhelp ........ long help\n");
598 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
599 printf(" -preset <string> ....... Preset setting, one of:\n");
600 printf(" default, photo, picture,\n");
601 printf(" drawing, icon, text\n");
602 printf(" -preset must come first, as it overwrites other parameters.");
603 printf("\n");
604 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
605 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700606 printf(" -size <int> ............ Target size (in bytes)\n");
607 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800608 printf("\n");
609 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
610 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
611 printf(" -f <int> ............... filter strength (0=off..100)\n");
612 printf(" -sharpness <int> ....... "
613 "filter sharpness (0:most .. 7:least sharp)\n");
614 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700615 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
616 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800617 printf(" -pass <int> ............ analysis pass number (1..10)\n");
618 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700619 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
620#ifdef WEBP_EXPERIMENTAL_FEATURES
621 printf(" -444 / -422 / -gray ..... Change colorspace\n");
622#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800623 printf(" -map <int> ............. print map of extra info.\n");
624 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700625
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800626 printf("\n");
627 printf(" -short ................. condense printed message\n");
628 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700629 printf(" -version ............... print version number and exit.\n");
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700630 printf(" -noasm ................. disable all assembly optimizations.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800631 printf(" -v ..................... verbose, e.g. print encoding/decoding "
632 "times\n");
633 printf("\n");
634 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 printf(" -af .................... auto-adjust filter strength.\n");
636 printf(" -pre <int> ............. pre-processing filter\n");
637 printf("\n");
638}
639
640//-----------------------------------------------------------------------------
641
642int main(int argc, const char *argv[]) {
643 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
644 FILE *out = NULL;
645 int c;
646 int short_output = 0;
647 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700648 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800649 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700650 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800651 WebPPicture picture;
652 WebPConfig config;
653 WebPAuxStats stats;
654 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700655
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700656#ifdef WEBP_EXPERIMENTAL_FEATURES
657 keep_alpha = 1;
658#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700659
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800660 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
661 fprintf(stderr, "Error! Version mismatch!\n");
662 goto Error;
663 }
664
665 if (argc == 1) {
666 HelpShort();
667 return 0;
668 }
669
670 for (c = 1; c < argc; ++c) {
671 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
672 HelpShort();
673 return 0;
674 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
675 HelpLong();
676 return 0;
677 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
678 out_file = argv[++c];
679 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
680 dump_file = argv[++c];
681 config.show_compressed = 1;
682 } else if (!strcmp(argv[c], "-short")) {
683 short_output++;
684 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700685 picture.width = strtol(argv[++c], NULL, 0);
686 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800687 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700688 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800689 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700690 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800691 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700692 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800693 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700694 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800695 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700696 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800697 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700698 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800699 } else if (!strcmp(argv[c], "-af")) {
700 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700701 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800702 config.filter_type = 1;
703 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700704 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800705 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700706 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800707 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700708 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800709 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700710 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700711 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
712 config.alpha_compression = strtol(argv[++c], NULL, 0);
713 } else if (!strcmp(argv[c], "-noalpha")) {
714 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800715 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700716 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700717#ifdef WEBP_EXPERIMENTAL_FEATURES
718 } else if (!strcmp(argv[c], "-444")) {
719 picture.colorspace = WEBP_YUV444;
720 } else if (!strcmp(argv[c], "-422")) {
721 picture.colorspace = WEBP_YUV422;
722 } else if (!strcmp(argv[c], "-gray")) {
723 picture.colorspace = WEBP_YUV400;
724#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800725 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
726 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700727 crop_x = strtol(argv[++c], NULL, 0);
728 crop_y = strtol(argv[++c], NULL, 0);
729 crop_w = strtol(argv[++c], NULL, 0);
730 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700731 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
732 resize_w = strtol(argv[++c], NULL, 0);
733 resize_h = strtol(argv[++c], NULL, 0);
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700734 } else if (!strcmp(argv[c], "-noasm")) {
735 VP8GetCPUInfo = NULL;
Pascal Massimino650ffa32011-03-24 16:17:10 -0700736 } else if (!strcmp(argv[c], "-version")) {
737 const int version = WebPGetEncoderVersion();
738 printf("%d.%d.%d\n",
739 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
740 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800741 } else if (!strcmp(argv[c], "-quiet")) {
742 quiet = 1;
743 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
744 WebPPreset preset;
745 ++c;
746 if (!strcmp(argv[c], "default")) {
747 preset = WEBP_PRESET_DEFAULT;
748 } else if (!strcmp(argv[c], "photo")) {
749 preset = WEBP_PRESET_PHOTO;
750 } else if (!strcmp(argv[c], "picture")) {
751 preset = WEBP_PRESET_PICTURE;
752 } else if (!strcmp(argv[c], "drawing")) {
753 preset = WEBP_PRESET_DRAWING;
754 } else if (!strcmp(argv[c], "icon")) {
755 preset = WEBP_PRESET_ICON;
756 } else if (!strcmp(argv[c], "text")) {
757 preset = WEBP_PRESET_TEXT;
758 } else {
759 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
760 goto Error;
761 }
762 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700763 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 goto Error;
765 }
766 } else if (!strcmp(argv[c], "-v")) {
767 verbose = 1;
768 } else if (argv[c][0] == '-') {
769 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
770 HelpLong();
771 return -1;
772 } else {
773 in_file = argv[c];
774 }
775 }
776
777 if (!WebPValidateConfig(&config)) {
778 fprintf(stderr, "Error! Invalid configuration.\n");
779 goto Error;
780 }
781
Pascal Massimino0744e842011-02-25 12:03:27 -0800782 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700783 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800784 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700785 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700786 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700787 fprintf(stderr, "Error! Cannot read input picture\n");
788 goto Error;
789 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800790 if (verbose) {
791 const double time = StopwatchReadAndReset(&stop_watch);
792 fprintf(stderr, "Time to read input: %.3fs\n", time);
793 }
794
795 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800796 if (out_file) {
797 out = fopen(out_file, "wb");
798 if (!out) {
799 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
800 goto Error;
801 } else {
802 if (!short_output && !quiet) {
803 fprintf(stderr, "Saving file '%s'\n", out_file);
804 }
805 }
806 picture.writer = MyWriter;
807 picture.custom_ptr = (void*)out;
808 } else {
809 out = NULL;
810 if (!quiet && !short_output) {
811 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
812 fprintf(stderr, "be performed, but its results discarded.\n\n");
813 }
814 }
815 picture.stats = &stats;
816
Pascal Massimino0744e842011-02-25 12:03:27 -0800817 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700818 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800819 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700820 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700821 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
822 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700824 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700825 if ((resize_w | resize_h) > 0) {
826 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
827 fprintf(stderr, "Error! Cannot resize picture\n");
828 goto Error;
829 }
830 }
831 if (picture.extra_info_type > 0) {
832 AllocExtraInfo(&picture);
833 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700834 if (!WebPEncode(&config, &picture)) {
835 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
836 goto Error;
837 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800838 if (verbose) {
839 const double time = StopwatchReadAndReset(&stop_watch);
840 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
841 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800842
843 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700844 if (dump_file) {
845 DumpPicture(&picture, dump_file);
846 }
847 if (!quiet) {
848 PrintExtraInfo(&picture, short_output);
849 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800850
851 Error:
852 free(picture.extra_info);
853 WebPPictureFree(&picture);
854 if (out != NULL) {
855 fclose(out);
856 }
857
858 return 0;
859}
860
861//-----------------------------------------------------------------------------