blob: acd61062f84d5892364264a094b5da80a44b3ae7 [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
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#ifdef WEBP_HAVE_PNG
22#include <png.h>
23#endif
24
25#ifdef WEBP_HAVE_JPEG
26#include <setjmp.h> // note: this must be included *after* png.h
27#include <jpeglib.h>
28#endif
29
James Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041
42#ifndef GUID_WICPixelFormat24bppRGB
43// From Microsoft SDK 7.0a
44DEFINE_GUID(GUID_WICPixelFormat24bppRGB,
45 0x6fddc324, 0x4e03, 0x4bfe, 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046#endif
James Zern31f9dc62011-06-06 17:56:50 -070047#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080048
49
50#include "webp/encode.h"
51#include "stopwatch.h"
Pascal Massiminof7d9e262011-04-26 11:02:38 -070052extern void* VP8GetCPUInfo; // opaque forward declaration.
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070053
Pascal Massiminof61d14a2011-02-18 23:33:46 -080054//-----------------------------------------------------------------------------
55
56static int verbose = 0;
57
58static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
59 const int uv_width = (pic->width + 1) / 2;
60 const int uv_height = (pic->height + 1) / 2;
61 int y;
62 int ok = 0;
63
64 if (!WebPPictureAlloc(pic)) return ok;
65
66 for (y = 0; y < pic->height; ++y) {
67 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
68 goto End;
69 }
70 }
71 for (y = 0; y < uv_height; ++y) {
72 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
73 goto End;
74 }
75 for (y = 0; y < uv_height; ++y) {
76 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
77 goto End;
78 }
79 ok = 1;
80
81 End:
82 return ok;
83}
84
James Zern31f9dc62011-06-06 17:56:50 -070085#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080086
87#define IFS(fn) \
88 do { \
89 if (SUCCEEDED(hr)) \
90 { \
91 hr = (fn); \
92 if (FAILED(hr) && verbose) \
93 printf(#fn " failed %08x\n", hr); \
94 } \
95 } while (0)
96
97#ifdef __cplusplus
98#define MAKE_REFGUID(x) (x)
99#else
100#define MAKE_REFGUID(x) &(x)
101#endif
102
103static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
104 HRESULT hr = S_OK;
105 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
106 if (FAILED(hr))
107 printf("Error opening input file %s (%08x)\n", filename, hr);
108 return hr;
109}
110
111static HRESULT ReadPictureWithWIC(const char* filename,
112 WebPPicture* const pic) {
113 HRESULT hr = S_OK;
114 IWICBitmapFrameDecode* pFrame = NULL;
115 IWICFormatConverter* pConverter = NULL;
116 IWICImagingFactory* pFactory = NULL;
117 IWICBitmapDecoder* pDecoder = NULL;
118 IStream* pStream = NULL;
119 UINT frameCount = 0;
120 UINT width, height = 0;
121 BYTE* rgb = NULL;
122
123 IFS(CoInitialize(NULL));
124 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
125 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
126 (LPVOID*)&pFactory));
127 if (hr == REGDB_E_CLASSNOTREG) {
128 printf("Couldn't access Windows Imaging Component (are you running \n");
129 printf("Windows XP SP3 or newer?). Most formats not available.\n");
130 printf("Use -s for the available YUV input.\n");
131 }
132 // Prepare for image decoding.
133 IFS(OpenInputStream(filename, &pStream));
134 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
135 WICDecodeMetadataCacheOnDemand, &pDecoder));
136 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
137 if (SUCCEEDED(hr) && frameCount == 0) {
138 printf("No frame found in input file.\n");
139 hr = E_FAIL;
140 }
141 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
142
143 // Prepare for pixel format conversion (if necessary).
144 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
145 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
146 MAKE_REFGUID(GUID_WICPixelFormat24bppRGB), WICBitmapDitherTypeNone,
147 NULL, 0.0, WICBitmapPaletteTypeCustom));
148
149 // Decode.
150 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
151 if (SUCCEEDED(hr)) {
152 rgb = (BYTE*)malloc(3 * width * height);
153 if (rgb == NULL)
154 hr = E_OUTOFMEMORY;
155 }
156 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, 3 * width,
157 3 * width * height, rgb));
158
159 // WebP conversion.
160 if (SUCCEEDED(hr)) {
161 pic->width = width;
162 pic->height = height;
163 if (!WebPPictureImportRGB(pic, rgb, 3 * width))
164 hr = E_FAIL;
165 }
166
167 // Cleanup.
168 if (pConverter != NULL) IUnknown_Release(pConverter);
169 if (pFrame != NULL) IUnknown_Release(pFrame);
170 if (pDecoder != NULL) IUnknown_Release(pDecoder);
171 if (pFactory != NULL) IUnknown_Release(pFactory);
172 if (pStream != NULL) IUnknown_Release(pStream);
173 free(rgb);
174 return hr;
175}
176
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700177static int ReadPicture(const char* const filename, WebPPicture* const pic,
178 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800179 int ok;
180 if (pic->width != 0 && pic->height != 0) {
181 // If image size is specified, infer it as YUV format.
182 FILE* in_file = fopen(filename, "rb");
183 if (in_file == NULL) {
184 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
185 return 0;
186 }
187 ok = ReadYUV(in_file, pic);
188 fclose(in_file);
189 } else {
190 // If no size specified, try to decode it using WIC.
191 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic));
192 }
193 if (!ok) {
194 fprintf(stderr, "Error! Could not process file %s\n", filename);
195 }
196 return ok;
197}
198
James Zern31f9dc62011-06-06 17:56:50 -0700199#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800200
201#ifdef WEBP_HAVE_JPEG
202struct my_error_mgr {
203 struct jpeg_error_mgr pub;
204 jmp_buf setjmp_buffer;
205};
206
207static void my_error_exit(j_common_ptr dinfo) {
208 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
209 (*dinfo->err->output_message) (dinfo);
210 longjmp(myerr->setjmp_buffer, 1);
211}
212
213static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
214 int ok = 0;
215 int stride, width, height;
216 uint8_t* rgb = NULL;
217 uint8_t* row_ptr = NULL;
218 struct jpeg_decompress_struct dinfo;
219 struct my_error_mgr jerr;
220 JSAMPARRAY buffer;
221
222 dinfo.err = jpeg_std_error(&jerr.pub);
223 jerr.pub.error_exit = my_error_exit;
224
225 if (setjmp (jerr.setjmp_buffer)) {
226 Error:
227 jpeg_destroy_decompress(&dinfo);
228 goto End;
229 }
230
231 jpeg_create_decompress(&dinfo);
232 jpeg_stdio_src(&dinfo, in_file);
233 jpeg_read_header(&dinfo, TRUE);
234
235 dinfo.out_color_space = JCS_RGB;
236 dinfo.dct_method = JDCT_IFAST;
237 dinfo.do_fancy_upsampling = TRUE;
238
239 jpeg_start_decompress(&dinfo);
240
241 if (dinfo.output_components != 3) {
242 goto Error;
243 }
244
245 width = dinfo.output_width;
246 height = dinfo.output_height;
247 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
248
249 rgb = (uint8_t*)malloc(stride * height);
250 if (rgb == NULL) {
251 goto End;
252 }
253 row_ptr = rgb;
254
255 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
256 JPOOL_IMAGE, stride, 1);
257 if (buffer == NULL) {
258 goto End;
259 }
260
261 while (dinfo.output_scanline < dinfo.output_height) {
262 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
263 goto End;
264 }
265 memcpy(row_ptr, buffer[0], stride);
266 row_ptr += stride;
267 }
268
269 jpeg_finish_decompress (&dinfo);
270 jpeg_destroy_decompress (&dinfo);
271
272 // WebP conversion.
273 pic->width = width;
274 pic->height = height;
275 ok = WebPPictureImportRGB(pic, rgb, stride);
276
277 End:
278 if (rgb) {
279 free(rgb);
280 }
281 return ok;
282}
283
284#else
285static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
286 printf("JPEG support not compiled. Please install the libjpeg development "
287 "package before building.\n");
288 return 0;
289}
290#endif
291
292#ifdef WEBP_HAVE_PNG
293static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700294 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800295 longjmp(png_jmpbuf(png), 1);
296}
297
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700298static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800299 png_structp png;
300 png_infop info;
301 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700302 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800303 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700304 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800305 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700306 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800307 int stride;
308 uint8_t* rgb = NULL;
309
310 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
311 if (png == NULL) {
312 goto End;
313 }
314
315 png_set_error_fn(png, 0, error_function, NULL);
316 if (setjmp(png_jmpbuf(png))) {
317 Error:
318 png_destroy_read_struct(&png, NULL, NULL);
319 if (rgb) free(rgb);
320 goto End;
321 }
322
323 info = png_create_info_struct(png);
324 if (info == NULL) goto Error;
325
326 png_init_io(png, in_file);
327 png_read_info(png, info);
328 if (!png_get_IHDR(png, info,
329 &width, &height, &bit_depth, &color_type, &interlaced,
330 NULL, NULL)) goto Error;
331
332 png_set_strip_16(png);
333 png_set_packing(png);
334 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
335 if (color_type == PNG_COLOR_TYPE_GRAY) {
336 if (bit_depth < 8) {
337 png_set_expand_gray_1_2_4_to_8(png);
338 }
339 png_set_gray_to_rgb(png);
340 }
341 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
342 png_set_tRNS_to_alpha(png);
343 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700344 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800345
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700346 if (!keep_alpha) {
347 png_set_strip_alpha(png);
348 has_alpha = 0;
349 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700350#ifdef WEBP_EXPERIMENTAL_FEATURES
351 if (has_alpha) {
352 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
353 }
354#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700355
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800356 num_passes = png_set_interlace_handling(png);
357 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700358 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800359 rgb = (uint8_t*)malloc(stride * height);
360 if (rgb == NULL) goto Error;
361 for (p = 0; p < num_passes; ++p) {
362 for (y = 0; y < height; ++y) {
363 png_bytep row = rgb + y * stride;
364 png_read_rows(png, &row, NULL, 1);
365 }
366 }
367 png_read_end(png, info);
368 png_destroy_read_struct(&png, &info, NULL);
369
370 pic->width = width;
371 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700372 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
373 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800374 free(rgb);
375
376 End:
377 return ok;
378}
379#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700380static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800381 printf("PNG support not compiled. Please install the libpng development "
382 "package before building.\n");
383 return 0;
384}
385#endif
386
387typedef enum {
388 PNG = 0,
389 JPEG,
390 UNSUPPORTED,
391} InputFileFormat;
392
393static InputFileFormat GetImageType(FILE* in_file) {
394 InputFileFormat format = UNSUPPORTED;
395 unsigned int magic;
396 unsigned char buf[4];
397
398 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
399 (fseek(in_file, 0, SEEK_SET) != 0)) {
400 return format;
401 }
402
403 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
404 if (magic == 0x89504E47U) {
405 format = PNG;
406 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
407 format = JPEG;
408 }
409 return format;
410}
411
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700412static int ReadPicture(const char* const filename, WebPPicture* const pic,
413 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800414 int ok = 0;
415 FILE* in_file = fopen(filename, "rb");
416 if (in_file == NULL) {
417 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
418 return ok;
419 }
420
421 if (pic->width == 0 || pic->height == 0) {
422 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
423 const InputFileFormat format = GetImageType(in_file);
424 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700425 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800426 } else if (format == JPEG) {
427 ok = ReadJPEG(in_file, pic);
428 }
429 } else {
430 // If image size is specified, infer it as YUV format.
431 ok = ReadYUV(in_file, pic);
432 }
433 if (!ok) {
434 fprintf(stderr, "Error! Could not process file %s\n", filename);
435 }
436
437 fclose(in_file);
438 return ok;
439}
440
James Zern31f9dc62011-06-06 17:56:50 -0700441#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800442
443static void AllocExtraInfo(WebPPicture* const pic) {
444 const int mb_w = (pic->width + 15) / 16;
445 const int mb_h = (pic->height + 15) / 16;
446 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
447}
448
449static void PrintByteCount(const int bytes[4], int total_size,
450 int* const totals) {
451 int s;
452 int total = 0;
453 for (s = 0; s < 4; ++s) {
454 fprintf(stderr, "| %7d ", bytes[s]);
455 total += bytes[s];
456 if (totals) totals[s] += bytes[s];
457 }
458 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
459}
460
461static void PrintPercents(const int counts[4], int total) {
462 int s;
463 for (s = 0; s < 4; ++s) {
464 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
465 }
466 fprintf(stderr,"| %7d\n", total);
467}
468
469static void PrintValues(const int values[4]) {
470 int s;
471 for (s = 0; s < 4; ++s) {
472 fprintf(stderr, "| %7d ", values[s]);
473 }
474 fprintf(stderr,"|\n");
475}
476
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700477static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800478 const WebPAuxStats* const stats = pic->stats;
479 if (short_output) {
480 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
481 } else{
482 const int num_i4 = stats->block_count[0];
483 const int num_i16 = stats->block_count[1];
484 const int num_skip = stats->block_count[2];
485 const int total = num_i4 + num_i16;
486 fprintf(stderr,
487 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
488 stats->coded_size,
489 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
490 if (total > 0) {
491 int totals[4] = { 0, 0, 0, 0 };
492 fprintf(stderr, "block count: intra4: %d\n"
493 " intra16: %d (-> %.2f%%)\n",
494 num_i4, num_i16, 100.f * num_i16 / total);
495 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
496 num_skip, 100.f * num_skip / total);
497 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
498 " mode-partition: %6d (%.1f%%)\n",
499 stats->header_bytes[0],
500 100.f * stats->header_bytes[0] / stats->coded_size,
501 stats->header_bytes[1],
502 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700503 if (stats->alpha_data_size) {
504 fprintf(stderr, " transparency: %6d\n",
505 stats->alpha_data_size);
506 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700507 if (stats->layer_data_size) {
508 fprintf(stderr, " enhancement: %6d\n",
509 stats->layer_data_size);
510 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800511 fprintf(stderr, " Residuals bytes "
512 "|segment 1|segment 2|segment 3"
513 "|segment 4| total\n");
514 fprintf(stderr, " intra4-coeffs: ");
515 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
516 fprintf(stderr, " intra16-coeffs: ");
517 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
518 fprintf(stderr, " chroma coeffs: ");
519 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
520 fprintf(stderr, " macroblocks: ");
521 PrintPercents(stats->segment_size, total);
522 fprintf(stderr, " quantizer: ");
523 PrintValues(stats->segment_quant);
524 fprintf(stderr, " filter level: ");
525 PrintValues(stats->segment_level);
526 fprintf(stderr, "------------------+---------");
527 fprintf(stderr, "+---------+---------+---------+-----------------\n");
528 fprintf(stderr, " segments total: ");
529 PrintByteCount(totals, stats->coded_size, NULL);
530 }
531 }
532 if (pic->extra_info) {
533 const int mb_w = (pic->width + 15) / 16;
534 const int mb_h = (pic->height + 15) / 16;
535 const int type = pic->extra_info_type;
536 int x, y;
537 for (y = 0; y < mb_h; ++y) {
538 for (x = 0; x < mb_w; ++x) {
539 const int c = pic->extra_info[x + y * mb_w];
540 if (type == 1) { // intra4/intra16
541 printf("%c", "+."[c]);
542 } else if (type == 2) { // segments
543 printf("%c", ".-*X"[c]);
544 } else if (type == 3) { // quantizers
545 printf("%.2d ", c);
546 } else if (type == 6 || type == 7) {
547 printf("%3d ", c);
548 } else {
549 printf("0x%.2x ", c);
550 }
551 }
552 printf("\n");
553 }
554 }
555}
556
557//-----------------------------------------------------------------------------
558
559static int MyWriter(const uint8_t* data, size_t data_size,
560 const WebPPicture* const pic) {
561 FILE* const out = (FILE*)pic->custom_ptr;
562 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
563}
564
565// Dumps a picture as a PGM file using the IMC4 layout.
566static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
567 int y;
568 const int uv_width = (picture->width + 1) / 2;
569 const int uv_height = (picture->height + 1) / 2;
570 const int stride = (picture->width + 1) & ~1;
571 const int height = picture->height + uv_height;
572 FILE* const f = fopen(PGM_name, "wb");
573 if (!f) return 0;
574 fprintf(f, "P5\n%d %d\n255\n", stride, height);
575 for (y = 0; y < picture->height; ++y) {
576 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
577 return 0;
578 if (picture->width & 1) fputc(0, f); // pad
579 }
580 for (y = 0; y < uv_height; ++y) {
581 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
582 return 0;
583 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
584 return 0;
585 }
586 fclose(f);
587 return 1;
588}
589
590//-----------------------------------------------------------------------------
591
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700592static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800593 printf("Usage:\n\n");
594 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
595 printf("where quality is between 0 (poor) to 100 (very good).\n");
596 printf("Typical value is around 80.\n\n");
597 printf("Try -longhelp for an exhaustive list of advanced options.\n");
598}
599
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700600static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800601 printf("Usage:\n");
602 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
603 printf("If input size (-s) for an image is not specified, "
604 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700605#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800606 printf("Windows builds can take as input any of the files handled by WIC\n");
607#endif
608 printf("options:\n");
609 printf(" -h / -help ............ short help\n");
610 printf(" -H / -longhelp ........ long help\n");
611 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
612 printf(" -preset <string> ....... Preset setting, one of:\n");
613 printf(" default, photo, picture,\n");
614 printf(" drawing, icon, text\n");
615 printf(" -preset must come first, as it overwrites other parameters.");
616 printf("\n");
617 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
618 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700619 printf(" -size <int> ............ Target size (in bytes)\n");
620 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800621 printf("\n");
622 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
623 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
624 printf(" -f <int> ............... filter strength (0=off..100)\n");
625 printf(" -sharpness <int> ....... "
626 "filter sharpness (0:most .. 7:least sharp)\n");
627 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700628 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
629 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800630 printf(" -pass <int> ............ analysis pass number (1..10)\n");
631 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700632 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
633#ifdef WEBP_EXPERIMENTAL_FEATURES
634 printf(" -444 / -422 / -gray ..... Change colorspace\n");
635#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800636 printf(" -map <int> ............. print map of extra info.\n");
637 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700638
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800639 printf("\n");
640 printf(" -short ................. condense printed message\n");
641 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700642 printf(" -version ............... print version number and exit.\n");
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700643 printf(" -noasm ................. disable all assembly optimizations.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 printf(" -v ..................... verbose, e.g. print encoding/decoding "
645 "times\n");
646 printf("\n");
647 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800648 printf(" -af .................... auto-adjust filter strength.\n");
649 printf(" -pre <int> ............. pre-processing filter\n");
650 printf("\n");
651}
652
653//-----------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700654// Error messages
655
656static const char* const kErrorMessages[] = {
657 "OK",
658 "OUT_OF_MEMORY: Out of memory allocating objects",
659 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
660 "NULL_PARAMETER: NULL parameter passed to function",
661 "INVALID_CONFIGURATION: configuration is invalid",
662 "BAD_DIMENSION: Bad picture dimension",
663 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k",
664 "PARTITION_OVERFLOW: Partition is too big to fir 16M",
665 "BAD_WRITE: Picture writer returned an error"
666};
667
668//-----------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669
670int main(int argc, const char *argv[]) {
671 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
672 FILE *out = NULL;
673 int c;
674 int short_output = 0;
675 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700676 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800677 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700678 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800679 WebPPicture picture;
680 WebPConfig config;
681 WebPAuxStats stats;
682 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700683
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700684#ifdef WEBP_EXPERIMENTAL_FEATURES
685 keep_alpha = 1;
686#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700687
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800688 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
689 fprintf(stderr, "Error! Version mismatch!\n");
690 goto Error;
691 }
692
693 if (argc == 1) {
694 HelpShort();
695 return 0;
696 }
697
698 for (c = 1; c < argc; ++c) {
699 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
700 HelpShort();
701 return 0;
702 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
703 HelpLong();
704 return 0;
705 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
706 out_file = argv[++c];
707 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
708 dump_file = argv[++c];
709 config.show_compressed = 1;
710 } else if (!strcmp(argv[c], "-short")) {
711 short_output++;
712 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700713 picture.width = strtol(argv[++c], NULL, 0);
714 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800715 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700716 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800717 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700718 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800719 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700720 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800721 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700722 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800723 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700724 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800725 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700726 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800727 } else if (!strcmp(argv[c], "-af")) {
728 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700729 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 config.filter_type = 1;
731 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700732 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800733 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700734 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800735 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700736 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800737 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700738 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700739 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
740 config.alpha_compression = strtol(argv[++c], NULL, 0);
741 } else if (!strcmp(argv[c], "-noalpha")) {
742 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800743 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700744 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700745#ifdef WEBP_EXPERIMENTAL_FEATURES
746 } else if (!strcmp(argv[c], "-444")) {
747 picture.colorspace = WEBP_YUV444;
748 } else if (!strcmp(argv[c], "-422")) {
749 picture.colorspace = WEBP_YUV422;
750 } else if (!strcmp(argv[c], "-gray")) {
751 picture.colorspace = WEBP_YUV400;
752#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
754 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700755 crop_x = strtol(argv[++c], NULL, 0);
756 crop_y = strtol(argv[++c], NULL, 0);
757 crop_w = strtol(argv[++c], NULL, 0);
758 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700759 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
760 resize_w = strtol(argv[++c], NULL, 0);
761 resize_h = strtol(argv[++c], NULL, 0);
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700762 } else if (!strcmp(argv[c], "-noasm")) {
763 VP8GetCPUInfo = NULL;
Pascal Massimino650ffa32011-03-24 16:17:10 -0700764 } else if (!strcmp(argv[c], "-version")) {
765 const int version = WebPGetEncoderVersion();
766 printf("%d.%d.%d\n",
767 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
768 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800769 } else if (!strcmp(argv[c], "-quiet")) {
770 quiet = 1;
771 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
772 WebPPreset preset;
773 ++c;
774 if (!strcmp(argv[c], "default")) {
775 preset = WEBP_PRESET_DEFAULT;
776 } else if (!strcmp(argv[c], "photo")) {
777 preset = WEBP_PRESET_PHOTO;
778 } else if (!strcmp(argv[c], "picture")) {
779 preset = WEBP_PRESET_PICTURE;
780 } else if (!strcmp(argv[c], "drawing")) {
781 preset = WEBP_PRESET_DRAWING;
782 } else if (!strcmp(argv[c], "icon")) {
783 preset = WEBP_PRESET_ICON;
784 } else if (!strcmp(argv[c], "text")) {
785 preset = WEBP_PRESET_TEXT;
786 } else {
787 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
788 goto Error;
789 }
790 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700791 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800792 goto Error;
793 }
794 } else if (!strcmp(argv[c], "-v")) {
795 verbose = 1;
796 } else if (argv[c][0] == '-') {
797 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
798 HelpLong();
799 return -1;
800 } else {
801 in_file = argv[c];
802 }
803 }
804
805 if (!WebPValidateConfig(&config)) {
806 fprintf(stderr, "Error! Invalid configuration.\n");
807 goto Error;
808 }
809
Pascal Massimino0744e842011-02-25 12:03:27 -0800810 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700811 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800812 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700813 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700814 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700815 fprintf(stderr, "Error! Cannot read input picture\n");
816 goto Error;
817 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800818 if (verbose) {
819 const double time = StopwatchReadAndReset(&stop_watch);
820 fprintf(stderr, "Time to read input: %.3fs\n", time);
821 }
822
823 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800824 if (out_file) {
825 out = fopen(out_file, "wb");
826 if (!out) {
827 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
828 goto Error;
829 } else {
830 if (!short_output && !quiet) {
831 fprintf(stderr, "Saving file '%s'\n", out_file);
832 }
833 }
834 picture.writer = MyWriter;
835 picture.custom_ptr = (void*)out;
836 } else {
837 out = NULL;
838 if (!quiet && !short_output) {
839 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
840 fprintf(stderr, "be performed, but its results discarded.\n\n");
841 }
842 }
843 picture.stats = &stats;
844
Pascal Massimino0744e842011-02-25 12:03:27 -0800845 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700846 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800847 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700848 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700849 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
850 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800851 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700852 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700853 if ((resize_w | resize_h) > 0) {
854 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
855 fprintf(stderr, "Error! Cannot resize picture\n");
856 goto Error;
857 }
858 }
859 if (picture.extra_info_type > 0) {
860 AllocExtraInfo(&picture);
861 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700862 if (!WebPEncode(&config, &picture)) {
863 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700864 fprintf(stderr, "Error code: %d (%s)\n",
865 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700866 goto Error;
867 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800868 if (verbose) {
869 const double time = StopwatchReadAndReset(&stop_watch);
870 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
871 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800872
873 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700874 if (dump_file) {
875 DumpPicture(&picture, dump_file);
876 }
877 if (!quiet) {
878 PrintExtraInfo(&picture, short_output);
879 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800880
881 Error:
882 free(picture.extra_info);
883 WebPPictureFree(&picture);
884 if (out != NULL) {
885 fclose(out);
886 }
887
888 return 0;
889}
890
891//-----------------------------------------------------------------------------