blob: 1b2bc44fc462713b343bd6ecd8eb26a3a1c75d5d [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08002//
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#include "webp/encode.h"
skal1bd287a2012-12-11 11:02:39 +010022
James Zern63aba3a2012-12-03 18:20:00 -080023#include "./metadata.h"
James Zern00b29e22012-05-15 13:48:11 -070024#include "./stopwatch.h"
skal1bd287a2012-12-11 11:02:39 +010025
26#include "./jpegdec.h"
27#include "./pngdec.h"
James Zern1c1c5642012-12-06 14:04:36 -080028#include "./tiffdec.h"
James Zerna452a552013-01-22 18:28:52 -080029#include "./wicdec.h"
skal1bd287a2012-12-11 11:02:39 +010030
James Zernb4d0ef82011-07-15 14:53:03 -070031#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070032#if defined(__cplusplus) || defined(c_plusplus)
33extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070034#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070035
James Zern04e84cf2011-11-04 15:20:08 -070036extern void* VP8GetCPUInfo; // opaque forward declaration.
37
38#if defined(__cplusplus) || defined(c_plusplus)
39} // extern "C"
40#endif
41#endif // WEBP_DLL
42
James Zernc7e86ab2011-08-25 14:22:32 -070043//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080044
45static int verbose = 0;
46
47static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000048 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080049 const int uv_width = (pic->width + 1) / 2;
50 const int uv_height = (pic->height + 1) / 2;
51 int y;
52 int ok = 0;
53
Pascal Massiminodd108172012-07-18 21:58:53 +000054 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080055 if (!WebPPictureAlloc(pic)) return ok;
56
57 for (y = 0; y < pic->height; ++y) {
58 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
59 goto End;
60 }
61 }
62 for (y = 0; y < uv_height; ++y) {
63 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
64 goto End;
65 }
66 for (y = 0; y < uv_height; ++y) {
67 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
68 goto End;
69 }
70 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000071 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080072
73 End:
74 return ok;
75}
76
James Zern31f9dc62011-06-06 17:56:50 -070077#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080078
Pascal Massimino2ab4b722011-04-25 16:58:04 -070079static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080080 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -080081 int ok;
James Zern63aba3a2012-12-03 18:20:00 -080082 (void)metadata; // TODO(jzern): add metadata extraction using WIC
Pascal Massiminof61d14a2011-02-18 23:33:46 -080083 if (pic->width != 0 && pic->height != 0) {
84 // If image size is specified, infer it as YUV format.
85 FILE* in_file = fopen(filename, "rb");
86 if (in_file == NULL) {
87 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
88 return 0;
89 }
90 ok = ReadYUV(in_file, pic);
91 fclose(in_file);
92 } else {
93 // If no size specified, try to decode it using WIC.
James Zerna452a552013-01-22 18:28:52 -080094 ok = ReadPictureWithWIC(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080095 }
96 if (!ok) {
97 fprintf(stderr, "Error! Could not process file %s\n", filename);
98 }
99 return ok;
100}
101
James Zern31f9dc62011-06-06 17:56:50 -0700102#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800103
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800104typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700105 PNG_ = 0,
106 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700107 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800108 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800109} InputFileFormat;
110
111static InputFileFormat GetImageType(FILE* in_file) {
112 InputFileFormat format = UNSUPPORTED;
113 unsigned int magic;
114 unsigned char buf[4];
115
116 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
117 (fseek(in_file, 0, SEEK_SET) != 0)) {
118 return format;
119 }
120
121 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
122 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700123 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800124 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700125 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700126 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
127 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800128 }
129 return format;
130}
131
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700132static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800133 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800134 int ok = 0;
135 FILE* in_file = fopen(filename, "rb");
136 if (in_file == NULL) {
137 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
138 return ok;
139 }
140
141 if (pic->width == 0 || pic->height == 0) {
142 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
143 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700144 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800145 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700146 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800147 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700148 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800149 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800150 }
151 } else {
152 // If image size is specified, infer it as YUV format.
153 ok = ReadYUV(in_file, pic);
154 }
155 if (!ok) {
156 fprintf(stderr, "Error! Could not process file %s\n", filename);
157 }
158
159 fclose(in_file);
160 return ok;
161}
162
James Zern31f9dc62011-06-06 17:56:50 -0700163#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800164
165static void AllocExtraInfo(WebPPicture* const pic) {
166 const int mb_w = (pic->width + 15) / 16;
167 const int mb_h = (pic->height + 15) / 16;
168 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
169}
170
171static void PrintByteCount(const int bytes[4], int total_size,
172 int* const totals) {
173 int s;
174 int total = 0;
175 for (s = 0; s < 4; ++s) {
176 fprintf(stderr, "| %7d ", bytes[s]);
177 total += bytes[s];
178 if (totals) totals[s] += bytes[s];
179 }
James Zern04e84cf2011-11-04 15:20:08 -0700180 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800181}
182
183static void PrintPercents(const int counts[4], int total) {
184 int s;
185 for (s = 0; s < 4; ++s) {
186 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
187 }
James Zern04e84cf2011-11-04 15:20:08 -0700188 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800189}
190
191static void PrintValues(const int values[4]) {
192 int s;
193 for (s = 0; s < 4; ++s) {
194 fprintf(stderr, "| %7d ", values[s]);
195 }
James Zern04e84cf2011-11-04 15:20:08 -0700196 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800197}
198
Pascal Massimino7d853d72012-07-24 16:15:36 -0700199static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
200 const char* const description) {
201 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
202 description, stats->lossless_size);
203 if (stats->lossless_features) {
204 fprintf(stderr, " * Lossless features used:");
205 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
206 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
207 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
208 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
209 fprintf(stderr, "\n");
210 }
211 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
212 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
213 if (stats->palette_size > 0) {
214 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
215 }
216}
217
Vikas Arorac4ccab62012-05-09 11:27:46 +0530218static void PrintExtraInfoLossless(const WebPPicture* const pic,
219 int short_output,
220 const char* const file_name) {
221 const WebPAuxStats* const stats = pic->stats;
222 if (short_output) {
223 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
224 } else {
225 fprintf(stderr, "File: %s\n", file_name);
226 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
227 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700228 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530229 }
230}
231
232static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
233 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800234 const WebPAuxStats* const stats = pic->stats;
235 if (short_output) {
236 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700237 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800238 const int num_i4 = stats->block_count[0];
239 const int num_i16 = stats->block_count[1];
240 const int num_skip = stats->block_count[2];
241 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800242 fprintf(stderr, "File: %s\n", file_name);
243 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700244 pic->width, pic->height,
245 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800246 fprintf(stderr, "Output: "
247 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800248 stats->coded_size,
249 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
250 if (total > 0) {
251 int totals[4] = { 0, 0, 0, 0 };
252 fprintf(stderr, "block count: intra4: %d\n"
253 " intra16: %d (-> %.2f%%)\n",
254 num_i4, num_i16, 100.f * num_i16 / total);
255 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
256 num_skip, 100.f * num_skip / total);
257 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
258 " mode-partition: %6d (%.1f%%)\n",
259 stats->header_bytes[0],
260 100.f * stats->header_bytes[0] / stats->coded_size,
261 stats->header_bytes[1],
262 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700263 if (stats->alpha_data_size > 0) {
264 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
265 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700266 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700267 if (stats->layer_data_size) {
268 fprintf(stderr, " enhancement: %6d\n",
269 stats->layer_data_size);
270 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800271 fprintf(stderr, " Residuals bytes "
272 "|segment 1|segment 2|segment 3"
273 "|segment 4| total\n");
274 fprintf(stderr, " intra4-coeffs: ");
275 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
276 fprintf(stderr, " intra16-coeffs: ");
277 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
278 fprintf(stderr, " chroma coeffs: ");
279 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
280 fprintf(stderr, " macroblocks: ");
281 PrintPercents(stats->segment_size, total);
282 fprintf(stderr, " quantizer: ");
283 PrintValues(stats->segment_quant);
284 fprintf(stderr, " filter level: ");
285 PrintValues(stats->segment_level);
286 fprintf(stderr, "------------------+---------");
287 fprintf(stderr, "+---------+---------+---------+-----------------\n");
288 fprintf(stderr, " segments total: ");
289 PrintByteCount(totals, stats->coded_size, NULL);
290 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700291 if (stats->lossless_size > 0) {
292 PrintFullLosslessInfo(stats, "alpha");
293 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800294 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700295 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800296 const int mb_w = (pic->width + 15) / 16;
297 const int mb_h = (pic->height + 15) / 16;
298 const int type = pic->extra_info_type;
299 int x, y;
300 for (y = 0; y < mb_h; ++y) {
301 for (x = 0; x < mb_w; ++x) {
302 const int c = pic->extra_info[x + y * mb_w];
303 if (type == 1) { // intra4/intra16
304 printf("%c", "+."[c]);
305 } else if (type == 2) { // segments
306 printf("%c", ".-*X"[c]);
307 } else if (type == 3) { // quantizers
308 printf("%.2d ", c);
309 } else if (type == 6 || type == 7) {
310 printf("%3d ", c);
311 } else {
312 printf("0x%.2x ", c);
313 }
314 }
315 printf("\n");
316 }
317 }
318}
319
James Zernc7e86ab2011-08-25 14:22:32 -0700320//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800321
322static int MyWriter(const uint8_t* data, size_t data_size,
323 const WebPPicture* const pic) {
324 FILE* const out = (FILE*)pic->custom_ptr;
325 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
326}
327
328// Dumps a picture as a PGM file using the IMC4 layout.
329static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
330 int y;
331 const int uv_width = (picture->width + 1) / 2;
332 const int uv_height = (picture->height + 1) / 2;
333 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700334 const int alpha_height =
335 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700336 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800337 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800338 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800339 fprintf(f, "P5\n%d %d\n255\n", stride, height);
340 for (y = 0; y < picture->height; ++y) {
341 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
342 return 0;
343 if (picture->width & 1) fputc(0, f); // pad
344 }
345 for (y = 0; y < uv_height; ++y) {
346 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
347 return 0;
348 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
349 return 0;
350 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700351 for (y = 0; y < alpha_height; ++y) {
352 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
353 return 0;
354 if (picture->width & 1) fputc(0, f); // pad
355 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800356 fclose(f);
357 return 1;
358}
359
James Zern7eaee9f2013-01-11 12:25:36 -0800360// -----------------------------------------------------------------------------
361// Metadata writing.
362
363enum {
364 METADATA_EXIF = (1 << 0),
365 METADATA_ICCP = (1 << 1),
366 METADATA_XMP = (1 << 2),
367 METADATA_ALL = METADATA_EXIF | METADATA_ICCP | METADATA_XMP
368};
369
James Zern76ec5fa2013-01-14 18:32:44 -0800370static const int kChunkHeaderSize = 8;
371static const int kTagSize = 4;
372
373// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
374static int WriteLE(FILE* const out, uint32_t val, int num) {
375 uint8_t buf[4];
376 int i;
377 for (i = 0; i < num; ++i) {
378 buf[i] = (uint8_t)(val & 0xff);
379 val >>= 8;
380 }
381 return (fwrite(buf, num, 1, out) == 1);
382}
383
384static int WriteLE24(FILE* const out, uint32_t val) {
385 return WriteLE(out, val, 3);
386}
387
388static int WriteLE32(FILE* const out, uint32_t val) {
389 return WriteLE(out, val, 4);
390}
391
392static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
393 const MetadataPayload* const payload) {
394 const uint8_t zero = 0;
395 const size_t need_padding = payload->size & 1;
396 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
397 ok = ok && WriteLE32(out, (uint32_t)payload->size);
398 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
399 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
400}
401
402// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
403// chunk if there is metadata and 'keep' is true.
404static int UpdateFlagsAndSize(const MetadataPayload* const payload,
405 int keep, int flag,
406 uint32_t* vp8x_flags, uint64_t* metadata_size) {
407 if (keep && payload->bytes != NULL && payload->size > 0) {
408 *vp8x_flags |= flag;
409 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
410 return 1;
411 }
412 return 0;
413}
414
415// Writes a WebP file using the image contained in 'memory_writer' and the
416// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
417// availability in 'metadata'. Returns true on success.
418// For details see doc/webp-container-spec.txt#extended-file-format.
419static int WriteWebPWithMetadata(FILE* const out,
420 const WebPPicture* const picture,
421 const WebPMemoryWriter* const memory_writer,
422 const Metadata* const metadata,
423 int keep_metadata) {
424 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
425 const int kAlphaFlag = 0x10;
426 const int kEXIFFlag = 0x08;
427 const int kICCPFlag = 0x20;
428 const int kXMPFlag = 0x04;
429 const size_t kRiffHeaderSize = 12;
430 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
431 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
432 uint32_t flags = 0;
433 uint64_t metadata_size = 0;
434 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
435 !!(keep_metadata & METADATA_EXIF),
436 kEXIFFlag, &flags, &metadata_size);
437 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
438 !!(keep_metadata & METADATA_ICCP),
439 kICCPFlag, &flags, &metadata_size);
440 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
441 !!(keep_metadata & METADATA_XMP),
442 kXMPFlag, &flags, &metadata_size);
443 uint8_t* webp = memory_writer->mem;
444 size_t webp_size = memory_writer->size;
445 if (webp_size < kMinSize) return 0;
446 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
447 fprintf(stderr, "Error! Addition of metadata would exceed "
448 "container size limit.\n");
449 return 0;
450 }
451
452 if (metadata_size > 0) {
453 const int kVP8XChunkSize = 18;
454 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
455 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
456 (has_vp8x ? 0 : kVP8XChunkSize) +
457 metadata_size);
458 // RIFF
459 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
460 // RIFF size (file header size is not recorded)
461 ok = ok && WriteLE32(out, riff_size);
462 webp += kChunkHeaderSize;
463 webp_size -= kChunkHeaderSize;
464 // WEBP
465 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
466 webp += kTagSize;
467 webp_size -= kTagSize;
468 if (has_vp8x) { // update the existing VP8X flags
469 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
470 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
471 webp_size -= kVP8XChunkSize;
472 } else {
473 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
474 // The alpha flag is forced with lossless images.
475 if (is_lossless) flags |= kAlphaFlag;
476 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
477 ok = ok && WriteLE32(out, flags);
478 ok = ok && WriteLE24(out, picture->width - 1);
479 ok = ok && WriteLE24(out, picture->height - 1);
480 }
481 if (write_iccp) ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
482 // Image
483 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
484 if (write_exif) ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
485 if (write_xmp) ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
486 return ok;
487 } else {
488 // No metadata, just write the original image file.
489 return (fwrite(webp, webp_size, 1, out) == 1);
490 }
491}
492
James Zernc7e86ab2011-08-25 14:22:32 -0700493//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800494
Pascal Massimino30971c92011-12-01 02:24:50 -0800495static int ProgressReport(int percent, const WebPPicture* const picture) {
496 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700497 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800498 fflush(stdout);
499 return 1; // all ok
500}
501
502//------------------------------------------------------------------------------
503
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700504static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800505 printf("Usage:\n\n");
506 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
507 printf("where quality is between 0 (poor) to 100 (very good).\n");
508 printf("Typical value is around 80.\n\n");
509 printf("Try -longhelp for an exhaustive list of advanced options.\n");
510}
511
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700512static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800513 printf("Usage:\n");
514 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
515 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700516 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700517#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800518 printf("Windows builds can take as input any of the files handled by WIC\n");
519#endif
520 printf("options:\n");
521 printf(" -h / -help ............ short help\n");
522 printf(" -H / -longhelp ........ long help\n");
523 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530524 printf(" -alpha_q <int> ......... Transparency-compression quality "
525 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800526 printf(" -preset <string> ....... Preset setting, one of:\n");
527 printf(" default, photo, picture,\n");
528 printf(" drawing, icon, text\n");
529 printf(" -preset must come first, as it overwrites other parameters.");
530 printf("\n");
531 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
532 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700533 printf(" -size <int> ............ Target size (in bytes)\n");
534 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800535 printf("\n");
536 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
537 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
538 printf(" -f <int> ............... filter strength (0=off..100)\n");
539 printf(" -sharpness <int> ....... "
540 "filter sharpness (0:most .. 7:least sharp)\n");
541 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700542 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
543 printf(" "
544 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800545 printf(" -pass <int> ............ analysis pass number (1..10)\n");
546 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700547 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
548#ifdef WEBP_EXPERIMENTAL_FEATURES
549 printf(" -444 / -422 / -gray ..... Change colorspace\n");
550#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800551 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800552 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700553 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
554 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800555 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530556 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800557 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
558 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000559 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530560 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530561 printf(" -lossless .............. Encode image losslessly.\n");
562 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700563 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700564
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800565 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800566 printf(" -metadata <string> ..... comma separated list of metadata to\n");
567 printf(" ");
568 printf("copy from the input to the output if present.\n");
569 printf(" "
570 "Valid values: all, none (default), exif, iccp, xmp\n");
571
572 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800573 printf(" -short ................. condense printed message\n");
574 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700575 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700576#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700577 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700578#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800579 printf(" -v ..................... verbose, e.g. print encoding/decoding "
580 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800581 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800582 printf("\n");
583 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800584 printf(" -af .................... auto-adjust filter strength.\n");
585 printf(" -pre <int> ............. pre-processing filter\n");
586 printf("\n");
587}
588
James Zernc7e86ab2011-08-25 14:22:32 -0700589//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700590// Error messages
591
592static const char* const kErrorMessages[] = {
593 "OK",
594 "OUT_OF_MEMORY: Out of memory allocating objects",
595 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
596 "NULL_PARAMETER: NULL parameter passed to function",
597 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700598 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
599 "allowed is 16383 pixels.",
600 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
601 "To reduce the size of this partition, try using less segments "
602 "with the -segments option, and eventually reduce the number of "
603 "header bits using -partition_limit. More details are available "
604 "in the manual (`man cwebp`)",
605 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800606 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800607 "FILE_TOO_BIG: File would be too big to fit in 4G",
608 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700609};
610
James Zernc7e86ab2011-08-25 14:22:32 -0700611//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800612
613int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700614 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800615 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
616 FILE *out = NULL;
617 int c;
618 int short_output = 0;
619 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530620 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800621 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700622 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800623 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800624 int keep_metadata = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800625 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700626 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800627 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800628 WebPConfig config;
629 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800630 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800631 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800632 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700633
James Zern63aba3a2012-12-03 18:20:00 -0800634 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800635 if (!WebPPictureInit(&picture) ||
636 !WebPPictureInit(&original_picture) ||
637 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800638 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700639 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800640 }
641
642 if (argc == 1) {
643 HelpShort();
644 return 0;
645 }
646
647 for (c = 1; c < argc; ++c) {
648 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
649 HelpShort();
650 return 0;
651 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
652 HelpLong();
653 return 0;
654 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
655 out_file = argv[++c];
656 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
657 dump_file = argv[++c];
658 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800659 } else if (!strcmp(argv[c], "-print_psnr")) {
660 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700661 print_distortion = 0;
662 } else if (!strcmp(argv[c], "-print_ssim")) {
663 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800664 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700665 } else if (!strcmp(argv[c], "-print_lsim")) {
666 config.show_compressed = 1;
667 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800668 } else if (!strcmp(argv[c], "-short")) {
669 short_output++;
670 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700671 picture.width = strtol(argv[++c], NULL, 0);
672 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800673 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700674 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800675 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200676 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530677 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
678 config.alpha_quality = strtol(argv[++c], NULL, 0);
679 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
680 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000681 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
682 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530683 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800684 ++c;
685 if (!strcmp(argv[c], "none")) {
686 config.alpha_filtering = 0;
687 } else if (!strcmp(argv[c], "fast")) {
688 config.alpha_filtering = 1;
689 } else if (!strcmp(argv[c], "best")) {
690 config.alpha_filtering = 2;
691 } else {
692 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
693 goto Error;
694 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530695 } else if (!strcmp(argv[c], "-noalpha")) {
696 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000697 } else if (!strcmp(argv[c], "-lossless")) {
698 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000699 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530700 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
701 ++c;
702 if (!strcmp(argv[c], "photo")) {
703 config.image_hint = WEBP_HINT_PHOTO;
704 } else if (!strcmp(argv[c], "picture")) {
705 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700706 } else if (!strcmp(argv[c], "graph")) {
707 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530708 } else {
709 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
710 goto Error;
711 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800712 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700713 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800714 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200715 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800716 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700717 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800718 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700719 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800720 } else if (!strcmp(argv[c], "-af")) {
721 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700722 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800723 config.filter_type = 1;
724 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700725 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800726 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700727 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800728 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700729 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700731 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700732 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
733 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800734 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700735 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700736#ifdef WEBP_EXPERIMENTAL_FEATURES
737 } else if (!strcmp(argv[c], "-444")) {
738 picture.colorspace = WEBP_YUV444;
739 } else if (!strcmp(argv[c], "-422")) {
740 picture.colorspace = WEBP_YUV422;
741 } else if (!strcmp(argv[c], "-gray")) {
742 picture.colorspace = WEBP_YUV400;
743#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800744 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
745 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700746 crop_x = strtol(argv[++c], NULL, 0);
747 crop_y = strtol(argv[++c], NULL, 0);
748 crop_w = strtol(argv[++c], NULL, 0);
749 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700750 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
751 resize_w = strtol(argv[++c], NULL, 0);
752 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700753#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700754 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000755 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700756#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700757 } else if (!strcmp(argv[c], "-version")) {
758 const int version = WebPGetEncoderVersion();
759 printf("%d.%d.%d\n",
760 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
761 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800762 } else if (!strcmp(argv[c], "-progress")) {
763 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 } else if (!strcmp(argv[c], "-quiet")) {
765 quiet = 1;
766 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
767 WebPPreset preset;
768 ++c;
769 if (!strcmp(argv[c], "default")) {
770 preset = WEBP_PRESET_DEFAULT;
771 } else if (!strcmp(argv[c], "photo")) {
772 preset = WEBP_PRESET_PHOTO;
773 } else if (!strcmp(argv[c], "picture")) {
774 preset = WEBP_PRESET_PICTURE;
775 } else if (!strcmp(argv[c], "drawing")) {
776 preset = WEBP_PRESET_DRAWING;
777 } else if (!strcmp(argv[c], "icon")) {
778 preset = WEBP_PRESET_ICON;
779 } else if (!strcmp(argv[c], "text")) {
780 preset = WEBP_PRESET_TEXT;
781 } else {
782 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
783 goto Error;
784 }
785 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700786 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 goto Error;
788 }
James Zern7eaee9f2013-01-11 12:25:36 -0800789 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
790 static const struct {
791 const char* option;
792 int flag;
793 } kTokens[] = {
794 { "all", METADATA_ALL },
795 { "none", 0 },
796 { "exif", METADATA_EXIF },
797 { "iccp", METADATA_ICCP },
798 { "xmp", METADATA_XMP },
799 };
800 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
801 const char* start = argv[++c];
802 const char* const end = start + strlen(start);
803
804 while (start < end) {
805 size_t i;
806 const char* token = strchr(start, ',');
807 if (token == NULL) token = end;
808
809 for (i = 0; i < kNumTokens; ++i) {
810 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
811 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
812 if (kTokens[i].flag != 0) {
813 keep_metadata |= kTokens[i].flag;
814 } else {
815 keep_metadata = 0;
816 }
817 break;
818 }
819 }
820 if (i == kNumTokens) {
821 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
822 (int)(token - start), start);
823 HelpLong();
824 return -1;
825 }
826 start = token + 1;
827 }
James Zern76ec5fa2013-01-14 18:32:44 -0800828#ifdef HAVE_WINCODEC_H
James Zern7eaee9f2013-01-11 12:25:36 -0800829 if (keep_metadata != 0) {
830 // TODO(jzern): remove when -metadata is supported on all platforms.
831 fprintf(stderr, "Warning: -metadata is currently unsupported on this"
832 " platform. Ignoring this option!\n");
833 }
James Zern76ec5fa2013-01-14 18:32:44 -0800834#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 } else if (!strcmp(argv[c], "-v")) {
836 verbose = 1;
837 } else if (argv[c][0] == '-') {
838 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
839 HelpLong();
840 return -1;
841 } else {
842 in_file = argv[c];
843 }
844 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700845 if (in_file == NULL) {
846 fprintf(stderr, "No input file specified!\n");
847 HelpShort();
848 goto Error;
849 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800850
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530851 // Check for unsupported command line options for lossless mode and log
852 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000853 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530854 if (config.target_size > 0 || config.target_PSNR > 0) {
855 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
856 " for lossless encoding. Ignoring such option(s)!\n");
857 }
858 if (config.partition_limit > 0) {
859 fprintf(stderr, "Partition limit option is not required for lossless"
860 " encoding. Ignoring this option!\n");
861 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530862 }
863
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800864 if (!WebPValidateConfig(&config)) {
865 fprintf(stderr, "Error! Invalid configuration.\n");
866 goto Error;
867 }
868
Pascal Massimino0744e842011-02-25 12:03:27 -0800869 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700870 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800871 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700872 }
James Zern7eaee9f2013-01-11 12:25:36 -0800873 if (!ReadPicture(in_file, &picture, keep_alpha,
874 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800875 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700876 goto Error;
877 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800878 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
879
Pascal Massimino0744e842011-02-25 12:03:27 -0800880 if (verbose) {
881 const double time = StopwatchReadAndReset(&stop_watch);
882 fprintf(stderr, "Time to read input: %.3fs\n", time);
883 }
884
885 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800886 if (out_file) {
887 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800888 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800889 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
890 goto Error;
891 } else {
892 if (!short_output && !quiet) {
893 fprintf(stderr, "Saving file '%s'\n", out_file);
894 }
895 }
James Zern76ec5fa2013-01-14 18:32:44 -0800896 if (keep_metadata == 0) {
897 picture.writer = MyWriter;
898 picture.custom_ptr = (void*)out;
899 } else {
900 WebPMemoryWriterInit(&memory_writer);
901 picture.writer = WebPMemoryWrite;
902 picture.custom_ptr = (void*)&memory_writer;
903 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800904 } else {
905 out = NULL;
906 if (!quiet && !short_output) {
907 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
908 fprintf(stderr, "be performed, but its results discarded.\n\n");
909 }
910 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700911 if (!quiet) {
912 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700913 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700914 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800915
Pascal Massimino0744e842011-02-25 12:03:27 -0800916 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700917 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800918 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700919 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -0700920 if (crop != 0) {
921 // We use self-cropping using a view.
922 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
923 fprintf(stderr, "Error! Cannot crop picture\n");
924 goto Error;
925 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700926 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700927 if ((resize_w | resize_h) > 0) {
928 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
929 fprintf(stderr, "Error! Cannot resize picture\n");
930 goto Error;
931 }
932 }
933 if (picture.extra_info_type > 0) {
934 AllocExtraInfo(&picture);
935 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700936 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -0800937 WebPPictureCopy(&picture, &original_picture);
938 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700939 if (!WebPEncode(&config, &picture)) {
940 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700941 fprintf(stderr, "Error code: %d (%s)\n",
942 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700943 goto Error;
944 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800945 if (verbose) {
946 const double time = StopwatchReadAndReset(&stop_watch);
947 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
948 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800949
950 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700951 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +0000952 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -0700953 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
954 } else if (!DumpPicture(&picture, dump_file)) {
955 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
956 }
Pascal Massimino38369c02011-05-04 22:59:51 -0700957 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800958
James Zern76ec5fa2013-01-14 18:32:44 -0800959 if (keep_metadata != 0 && out != NULL) {
960 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
961 &metadata, keep_metadata)) {
962 fprintf(stderr, "Error writing WebP file with metadata!\n");
963 goto Error;
964 }
965 }
966
Pascal Massimino38369c02011-05-04 22:59:51 -0700967 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +0530968 if (config.lossless) {
969 PrintExtraInfoLossless(&picture, short_output, in_file);
970 } else {
971 PrintExtraInfoLossy(&picture, short_output, in_file);
972 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800973 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700974 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
975 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -0800976 float values[5];
977 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700978 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800979 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700980 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -0800981 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -0700982 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700983 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800984
985 Error:
986 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -0800987 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800988 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800989 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800990 if (out != NULL) {
991 fclose(out);
992 }
993
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700994 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800995}
996
James Zernc7e86ab2011-08-25 14:22:32 -0700997//------------------------------------------------------------------------------