blob: 5065b9132de62fae9d0e9d88cc0924897e38d93f [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;
82 if (pic->width != 0 && pic->height != 0) {
83 // If image size is specified, infer it as YUV format.
84 FILE* in_file = fopen(filename, "rb");
85 if (in_file == NULL) {
86 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
87 return 0;
88 }
89 ok = ReadYUV(in_file, pic);
90 fclose(in_file);
91 } else {
92 // If no size specified, try to decode it using WIC.
James Zerne83ff7d2013-01-24 15:37:49 -080093 ok = ReadPictureWithWIC(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080094 }
95 if (!ok) {
96 fprintf(stderr, "Error! Could not process file %s\n", filename);
97 }
98 return ok;
99}
100
James Zern31f9dc62011-06-06 17:56:50 -0700101#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800102
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800103typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700104 PNG_ = 0,
105 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700106 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800107 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800108} InputFileFormat;
109
110static InputFileFormat GetImageType(FILE* in_file) {
111 InputFileFormat format = UNSUPPORTED;
112 unsigned int magic;
113 unsigned char buf[4];
114
115 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
116 (fseek(in_file, 0, SEEK_SET) != 0)) {
117 return format;
118 }
119
120 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
121 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700122 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800123 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700124 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700125 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
126 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800127 }
128 return format;
129}
130
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700131static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800132 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800133 int ok = 0;
134 FILE* in_file = fopen(filename, "rb");
135 if (in_file == NULL) {
136 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
137 return ok;
138 }
139
140 if (pic->width == 0 || pic->height == 0) {
141 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
142 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700143 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800144 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700145 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800146 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700147 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800148 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800149 }
150 } else {
151 // If image size is specified, infer it as YUV format.
152 ok = ReadYUV(in_file, pic);
153 }
154 if (!ok) {
155 fprintf(stderr, "Error! Could not process file %s\n", filename);
156 }
157
158 fclose(in_file);
159 return ok;
160}
161
James Zern31f9dc62011-06-06 17:56:50 -0700162#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800163
164static void AllocExtraInfo(WebPPicture* const pic) {
165 const int mb_w = (pic->width + 15) / 16;
166 const int mb_h = (pic->height + 15) / 16;
167 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
168}
169
170static void PrintByteCount(const int bytes[4], int total_size,
171 int* const totals) {
172 int s;
173 int total = 0;
174 for (s = 0; s < 4; ++s) {
175 fprintf(stderr, "| %7d ", bytes[s]);
176 total += bytes[s];
177 if (totals) totals[s] += bytes[s];
178 }
James Zern04e84cf2011-11-04 15:20:08 -0700179 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800180}
181
182static void PrintPercents(const int counts[4], int total) {
183 int s;
184 for (s = 0; s < 4; ++s) {
185 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
186 }
James Zern04e84cf2011-11-04 15:20:08 -0700187 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800188}
189
190static void PrintValues(const int values[4]) {
191 int s;
192 for (s = 0; s < 4; ++s) {
193 fprintf(stderr, "| %7d ", values[s]);
194 }
James Zern04e84cf2011-11-04 15:20:08 -0700195 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800196}
197
Pascal Massimino7d853d72012-07-24 16:15:36 -0700198static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
199 const char* const description) {
200 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
201 description, stats->lossless_size);
202 if (stats->lossless_features) {
203 fprintf(stderr, " * Lossless features used:");
204 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
205 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
206 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
207 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
208 fprintf(stderr, "\n");
209 }
210 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
211 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
212 if (stats->palette_size > 0) {
213 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
214 }
215}
216
Vikas Arorac4ccab62012-05-09 11:27:46 +0530217static void PrintExtraInfoLossless(const WebPPicture* const pic,
218 int short_output,
219 const char* const file_name) {
220 const WebPAuxStats* const stats = pic->stats;
221 if (short_output) {
222 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
223 } else {
224 fprintf(stderr, "File: %s\n", file_name);
225 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
226 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700227 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530228 }
229}
230
231static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
232 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800233 const WebPAuxStats* const stats = pic->stats;
234 if (short_output) {
235 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700236 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800237 const int num_i4 = stats->block_count[0];
238 const int num_i16 = stats->block_count[1];
239 const int num_skip = stats->block_count[2];
240 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800241 fprintf(stderr, "File: %s\n", file_name);
242 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700243 pic->width, pic->height,
244 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800245 fprintf(stderr, "Output: "
246 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800247 stats->coded_size,
248 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
249 if (total > 0) {
250 int totals[4] = { 0, 0, 0, 0 };
251 fprintf(stderr, "block count: intra4: %d\n"
252 " intra16: %d (-> %.2f%%)\n",
253 num_i4, num_i16, 100.f * num_i16 / total);
254 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
255 num_skip, 100.f * num_skip / total);
256 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
257 " mode-partition: %6d (%.1f%%)\n",
258 stats->header_bytes[0],
259 100.f * stats->header_bytes[0] / stats->coded_size,
260 stats->header_bytes[1],
261 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700262 if (stats->alpha_data_size > 0) {
263 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
264 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700265 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700266 if (stats->layer_data_size) {
267 fprintf(stderr, " enhancement: %6d\n",
268 stats->layer_data_size);
269 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800270 fprintf(stderr, " Residuals bytes "
271 "|segment 1|segment 2|segment 3"
272 "|segment 4| total\n");
273 fprintf(stderr, " intra4-coeffs: ");
274 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
275 fprintf(stderr, " intra16-coeffs: ");
276 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
277 fprintf(stderr, " chroma coeffs: ");
278 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
279 fprintf(stderr, " macroblocks: ");
280 PrintPercents(stats->segment_size, total);
281 fprintf(stderr, " quantizer: ");
282 PrintValues(stats->segment_quant);
283 fprintf(stderr, " filter level: ");
284 PrintValues(stats->segment_level);
285 fprintf(stderr, "------------------+---------");
286 fprintf(stderr, "+---------+---------+---------+-----------------\n");
287 fprintf(stderr, " segments total: ");
288 PrintByteCount(totals, stats->coded_size, NULL);
289 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700290 if (stats->lossless_size > 0) {
291 PrintFullLosslessInfo(stats, "alpha");
292 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800293 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700294 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800295 const int mb_w = (pic->width + 15) / 16;
296 const int mb_h = (pic->height + 15) / 16;
297 const int type = pic->extra_info_type;
298 int x, y;
299 for (y = 0; y < mb_h; ++y) {
300 for (x = 0; x < mb_w; ++x) {
301 const int c = pic->extra_info[x + y * mb_w];
302 if (type == 1) { // intra4/intra16
303 printf("%c", "+."[c]);
304 } else if (type == 2) { // segments
305 printf("%c", ".-*X"[c]);
306 } else if (type == 3) { // quantizers
307 printf("%.2d ", c);
308 } else if (type == 6 || type == 7) {
309 printf("%3d ", c);
310 } else {
311 printf("0x%.2x ", c);
312 }
313 }
314 printf("\n");
315 }
316 }
317}
318
James Zernc7e86ab2011-08-25 14:22:32 -0700319//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800320
321static int MyWriter(const uint8_t* data, size_t data_size,
322 const WebPPicture* const pic) {
323 FILE* const out = (FILE*)pic->custom_ptr;
324 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
325}
326
327// Dumps a picture as a PGM file using the IMC4 layout.
328static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
329 int y;
330 const int uv_width = (picture->width + 1) / 2;
331 const int uv_height = (picture->height + 1) / 2;
332 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700333 const int alpha_height =
334 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700335 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800336 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800337 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800338 fprintf(f, "P5\n%d %d\n255\n", stride, height);
339 for (y = 0; y < picture->height; ++y) {
340 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
341 return 0;
342 if (picture->width & 1) fputc(0, f); // pad
343 }
344 for (y = 0; y < uv_height; ++y) {
345 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
346 return 0;
347 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
348 return 0;
349 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700350 for (y = 0; y < alpha_height; ++y) {
351 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
352 return 0;
353 if (picture->width & 1) fputc(0, f); // pad
354 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800355 fclose(f);
356 return 1;
357}
358
James Zern7eaee9f2013-01-11 12:25:36 -0800359// -----------------------------------------------------------------------------
360// Metadata writing.
361
362enum {
363 METADATA_EXIF = (1 << 0),
364 METADATA_ICCP = (1 << 1),
365 METADATA_XMP = (1 << 2),
366 METADATA_ALL = METADATA_EXIF | METADATA_ICCP | METADATA_XMP
367};
368
James Zern76ec5fa2013-01-14 18:32:44 -0800369static const int kChunkHeaderSize = 8;
370static const int kTagSize = 4;
371
372// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
373static int WriteLE(FILE* const out, uint32_t val, int num) {
374 uint8_t buf[4];
375 int i;
376 for (i = 0; i < num; ++i) {
377 buf[i] = (uint8_t)(val & 0xff);
378 val >>= 8;
379 }
380 return (fwrite(buf, num, 1, out) == 1);
381}
382
383static int WriteLE24(FILE* const out, uint32_t val) {
384 return WriteLE(out, val, 3);
385}
386
387static int WriteLE32(FILE* const out, uint32_t val) {
388 return WriteLE(out, val, 4);
389}
390
391static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
392 const MetadataPayload* const payload) {
393 const uint8_t zero = 0;
394 const size_t need_padding = payload->size & 1;
395 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
396 ok = ok && WriteLE32(out, (uint32_t)payload->size);
397 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
398 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
399}
400
401// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
402// chunk if there is metadata and 'keep' is true.
403static int UpdateFlagsAndSize(const MetadataPayload* const payload,
404 int keep, int flag,
405 uint32_t* vp8x_flags, uint64_t* metadata_size) {
406 if (keep && payload->bytes != NULL && payload->size > 0) {
407 *vp8x_flags |= flag;
408 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
409 return 1;
410 }
411 return 0;
412}
413
414// Writes a WebP file using the image contained in 'memory_writer' and the
415// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
416// availability in 'metadata'. Returns true on success.
417// For details see doc/webp-container-spec.txt#extended-file-format.
418static int WriteWebPWithMetadata(FILE* const out,
419 const WebPPicture* const picture,
420 const WebPMemoryWriter* const memory_writer,
421 const Metadata* const metadata,
422 int keep_metadata) {
423 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
424 const int kAlphaFlag = 0x10;
425 const int kEXIFFlag = 0x08;
426 const int kICCPFlag = 0x20;
427 const int kXMPFlag = 0x04;
428 const size_t kRiffHeaderSize = 12;
429 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
430 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
431 uint32_t flags = 0;
432 uint64_t metadata_size = 0;
433 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
434 !!(keep_metadata & METADATA_EXIF),
435 kEXIFFlag, &flags, &metadata_size);
436 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
437 !!(keep_metadata & METADATA_ICCP),
438 kICCPFlag, &flags, &metadata_size);
439 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
440 !!(keep_metadata & METADATA_XMP),
441 kXMPFlag, &flags, &metadata_size);
442 uint8_t* webp = memory_writer->mem;
443 size_t webp_size = memory_writer->size;
444 if (webp_size < kMinSize) return 0;
445 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
446 fprintf(stderr, "Error! Addition of metadata would exceed "
447 "container size limit.\n");
448 return 0;
449 }
450
451 if (metadata_size > 0) {
452 const int kVP8XChunkSize = 18;
453 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
454 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
455 (has_vp8x ? 0 : kVP8XChunkSize) +
456 metadata_size);
457 // RIFF
458 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
459 // RIFF size (file header size is not recorded)
460 ok = ok && WriteLE32(out, riff_size);
461 webp += kChunkHeaderSize;
462 webp_size -= kChunkHeaderSize;
463 // WEBP
464 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
465 webp += kTagSize;
466 webp_size -= kTagSize;
467 if (has_vp8x) { // update the existing VP8X flags
468 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
469 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
470 webp_size -= kVP8XChunkSize;
471 } else {
472 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
473 // The alpha flag is forced with lossless images.
474 if (is_lossless) flags |= kAlphaFlag;
475 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
476 ok = ok && WriteLE32(out, flags);
477 ok = ok && WriteLE24(out, picture->width - 1);
478 ok = ok && WriteLE24(out, picture->height - 1);
479 }
480 if (write_iccp) ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
481 // Image
482 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
483 if (write_exif) ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
484 if (write_xmp) ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
485 return ok;
486 } else {
487 // No metadata, just write the original image file.
488 return (fwrite(webp, webp_size, 1, out) == 1);
489 }
490}
491
James Zernc7e86ab2011-08-25 14:22:32 -0700492//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800493
Pascal Massimino30971c92011-12-01 02:24:50 -0800494static int ProgressReport(int percent, const WebPPicture* const picture) {
495 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700496 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800497 fflush(stdout);
498 return 1; // all ok
499}
500
501//------------------------------------------------------------------------------
502
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700503static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800504 printf("Usage:\n\n");
505 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
506 printf("where quality is between 0 (poor) to 100 (very good).\n");
507 printf("Typical value is around 80.\n\n");
508 printf("Try -longhelp for an exhaustive list of advanced options.\n");
509}
510
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700511static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800512 printf("Usage:\n");
513 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
514 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700515 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700516#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800517 printf("Windows builds can take as input any of the files handled by WIC\n");
518#endif
519 printf("options:\n");
520 printf(" -h / -help ............ short help\n");
521 printf(" -H / -longhelp ........ long help\n");
522 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530523 printf(" -alpha_q <int> ......... Transparency-compression quality "
524 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800525 printf(" -preset <string> ....... Preset setting, one of:\n");
526 printf(" default, photo, picture,\n");
527 printf(" drawing, icon, text\n");
528 printf(" -preset must come first, as it overwrites other parameters.");
529 printf("\n");
530 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
531 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700532 printf(" -size <int> ............ Target size (in bytes)\n");
533 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800534 printf("\n");
535 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
536 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
537 printf(" -f <int> ............... filter strength (0=off..100)\n");
538 printf(" -sharpness <int> ....... "
539 "filter sharpness (0:most .. 7:least sharp)\n");
540 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700541 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
542 printf(" "
543 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800544 printf(" -pass <int> ............ analysis pass number (1..10)\n");
545 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700546 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
547#ifdef WEBP_EXPERIMENTAL_FEATURES
548 printf(" -444 / -422 / -gray ..... Change colorspace\n");
549#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800550 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800551 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700552 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
553 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800554 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530555 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800556 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
557 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000558 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530559 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530560 printf(" -lossless .............. Encode image losslessly.\n");
561 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700562 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700563
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800564 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800565 printf(" -metadata <string> ..... comma separated list of metadata to\n");
566 printf(" ");
567 printf("copy from the input to the output if present.\n");
568 printf(" "
569 "Valid values: all, none (default), exif, iccp, xmp\n");
570
571 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800572 printf(" -short ................. condense printed message\n");
573 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700574 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700575#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700576 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700577#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800578 printf(" -v ..................... verbose, e.g. print encoding/decoding "
579 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800580 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800581 printf("\n");
582 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800583 printf(" -af .................... auto-adjust filter strength.\n");
584 printf(" -pre <int> ............. pre-processing filter\n");
585 printf("\n");
586}
587
James Zernc7e86ab2011-08-25 14:22:32 -0700588//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700589// Error messages
590
591static const char* const kErrorMessages[] = {
592 "OK",
593 "OUT_OF_MEMORY: Out of memory allocating objects",
594 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
595 "NULL_PARAMETER: NULL parameter passed to function",
596 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700597 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
598 "allowed is 16383 pixels.",
599 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
600 "To reduce the size of this partition, try using less segments "
601 "with the -segments option, and eventually reduce the number of "
602 "header bits using -partition_limit. More details are available "
603 "in the manual (`man cwebp`)",
604 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800605 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800606 "FILE_TOO_BIG: File would be too big to fit in 4G",
607 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700608};
609
James Zernc7e86ab2011-08-25 14:22:32 -0700610//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800611
612int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700613 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800614 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
615 FILE *out = NULL;
616 int c;
617 int short_output = 0;
618 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530619 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800620 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700621 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800622 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800623 int keep_metadata = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800624 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700625 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800626 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800627 WebPConfig config;
628 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800629 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800630 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800631 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700632
James Zern63aba3a2012-12-03 18:20:00 -0800633 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800634 if (!WebPPictureInit(&picture) ||
635 !WebPPictureInit(&original_picture) ||
636 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800637 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700638 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800639 }
640
641 if (argc == 1) {
642 HelpShort();
643 return 0;
644 }
645
646 for (c = 1; c < argc; ++c) {
647 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
648 HelpShort();
649 return 0;
650 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
651 HelpLong();
652 return 0;
653 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
654 out_file = argv[++c];
655 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
656 dump_file = argv[++c];
657 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800658 } else if (!strcmp(argv[c], "-print_psnr")) {
659 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700660 print_distortion = 0;
661 } else if (!strcmp(argv[c], "-print_ssim")) {
662 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800663 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700664 } else if (!strcmp(argv[c], "-print_lsim")) {
665 config.show_compressed = 1;
666 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800667 } else if (!strcmp(argv[c], "-short")) {
668 short_output++;
669 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700670 picture.width = strtol(argv[++c], NULL, 0);
671 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800672 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700673 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200675 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530676 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
677 config.alpha_quality = strtol(argv[++c], NULL, 0);
678 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
679 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000680 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
681 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530682 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800683 ++c;
684 if (!strcmp(argv[c], "none")) {
685 config.alpha_filtering = 0;
686 } else if (!strcmp(argv[c], "fast")) {
687 config.alpha_filtering = 1;
688 } else if (!strcmp(argv[c], "best")) {
689 config.alpha_filtering = 2;
690 } else {
691 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
692 goto Error;
693 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530694 } else if (!strcmp(argv[c], "-noalpha")) {
695 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000696 } else if (!strcmp(argv[c], "-lossless")) {
697 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000698 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530699 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
700 ++c;
701 if (!strcmp(argv[c], "photo")) {
702 config.image_hint = WEBP_HINT_PHOTO;
703 } else if (!strcmp(argv[c], "picture")) {
704 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700705 } else if (!strcmp(argv[c], "graph")) {
706 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530707 } else {
708 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
709 goto Error;
710 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700712 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800713 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200714 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800715 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700716 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800717 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700718 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800719 } else if (!strcmp(argv[c], "-af")) {
720 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700721 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800722 config.filter_type = 1;
723 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700724 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800725 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700726 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800727 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700728 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800729 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700730 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700731 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
732 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800733 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700734 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700735#ifdef WEBP_EXPERIMENTAL_FEATURES
736 } else if (!strcmp(argv[c], "-444")) {
737 picture.colorspace = WEBP_YUV444;
738 } else if (!strcmp(argv[c], "-422")) {
739 picture.colorspace = WEBP_YUV422;
740 } else if (!strcmp(argv[c], "-gray")) {
741 picture.colorspace = WEBP_YUV400;
742#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800743 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
744 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700745 crop_x = strtol(argv[++c], NULL, 0);
746 crop_y = strtol(argv[++c], NULL, 0);
747 crop_w = strtol(argv[++c], NULL, 0);
748 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700749 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
750 resize_w = strtol(argv[++c], NULL, 0);
751 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700752#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700753 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000754 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700755#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700756 } else if (!strcmp(argv[c], "-version")) {
757 const int version = WebPGetEncoderVersion();
758 printf("%d.%d.%d\n",
759 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
760 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800761 } else if (!strcmp(argv[c], "-progress")) {
762 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800763 } else if (!strcmp(argv[c], "-quiet")) {
764 quiet = 1;
765 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
766 WebPPreset preset;
767 ++c;
768 if (!strcmp(argv[c], "default")) {
769 preset = WEBP_PRESET_DEFAULT;
770 } else if (!strcmp(argv[c], "photo")) {
771 preset = WEBP_PRESET_PHOTO;
772 } else if (!strcmp(argv[c], "picture")) {
773 preset = WEBP_PRESET_PICTURE;
774 } else if (!strcmp(argv[c], "drawing")) {
775 preset = WEBP_PRESET_DRAWING;
776 } else if (!strcmp(argv[c], "icon")) {
777 preset = WEBP_PRESET_ICON;
778 } else if (!strcmp(argv[c], "text")) {
779 preset = WEBP_PRESET_TEXT;
780 } else {
781 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
782 goto Error;
783 }
784 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700785 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800786 goto Error;
787 }
James Zern7eaee9f2013-01-11 12:25:36 -0800788 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
789 static const struct {
790 const char* option;
791 int flag;
792 } kTokens[] = {
793 { "all", METADATA_ALL },
794 { "none", 0 },
795 { "exif", METADATA_EXIF },
796 { "iccp", METADATA_ICCP },
797 { "xmp", METADATA_XMP },
798 };
799 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
800 const char* start = argv[++c];
801 const char* const end = start + strlen(start);
802
803 while (start < end) {
804 size_t i;
805 const char* token = strchr(start, ',');
806 if (token == NULL) token = end;
807
808 for (i = 0; i < kNumTokens; ++i) {
809 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
810 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
811 if (kTokens[i].flag != 0) {
812 keep_metadata |= kTokens[i].flag;
813 } else {
814 keep_metadata = 0;
815 }
816 break;
817 }
818 }
819 if (i == kNumTokens) {
820 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
821 (int)(token - start), start);
822 HelpLong();
823 return -1;
824 }
825 start = token + 1;
826 }
James Zern76ec5fa2013-01-14 18:32:44 -0800827#ifdef HAVE_WINCODEC_H
James Zerne83ff7d2013-01-24 15:37:49 -0800828 if (keep_metadata != 0 && keep_metadata != METADATA_ICCP) {
James Zern7eaee9f2013-01-11 12:25:36 -0800829 // TODO(jzern): remove when -metadata is supported on all platforms.
James Zerne83ff7d2013-01-24 15:37:49 -0800830 fprintf(stderr, "Warning: only ICC profile extraction is currently"
831 " supported on this platform!\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800832 }
James Zern76ec5fa2013-01-14 18:32:44 -0800833#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800834 } else if (!strcmp(argv[c], "-v")) {
835 verbose = 1;
836 } else if (argv[c][0] == '-') {
837 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
838 HelpLong();
839 return -1;
840 } else {
841 in_file = argv[c];
842 }
843 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700844 if (in_file == NULL) {
845 fprintf(stderr, "No input file specified!\n");
846 HelpShort();
847 goto Error;
848 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800849
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530850 // Check for unsupported command line options for lossless mode and log
851 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000852 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530853 if (config.target_size > 0 || config.target_PSNR > 0) {
854 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
855 " for lossless encoding. Ignoring such option(s)!\n");
856 }
857 if (config.partition_limit > 0) {
858 fprintf(stderr, "Partition limit option is not required for lossless"
859 " encoding. Ignoring this option!\n");
860 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530861 }
862
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800863 if (!WebPValidateConfig(&config)) {
864 fprintf(stderr, "Error! Invalid configuration.\n");
865 goto Error;
866 }
867
Pascal Massimino0744e842011-02-25 12:03:27 -0800868 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700869 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800870 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700871 }
James Zern7eaee9f2013-01-11 12:25:36 -0800872 if (!ReadPicture(in_file, &picture, keep_alpha,
873 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800874 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700875 goto Error;
876 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800877 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
878
Pascal Massimino0744e842011-02-25 12:03:27 -0800879 if (verbose) {
880 const double time = StopwatchReadAndReset(&stop_watch);
881 fprintf(stderr, "Time to read input: %.3fs\n", time);
882 }
883
884 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885 if (out_file) {
886 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800887 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800888 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
889 goto Error;
890 } else {
891 if (!short_output && !quiet) {
892 fprintf(stderr, "Saving file '%s'\n", out_file);
893 }
894 }
James Zern76ec5fa2013-01-14 18:32:44 -0800895 if (keep_metadata == 0) {
896 picture.writer = MyWriter;
897 picture.custom_ptr = (void*)out;
898 } else {
899 WebPMemoryWriterInit(&memory_writer);
900 picture.writer = WebPMemoryWrite;
901 picture.custom_ptr = (void*)&memory_writer;
902 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800903 } else {
904 out = NULL;
905 if (!quiet && !short_output) {
906 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
907 fprintf(stderr, "be performed, but its results discarded.\n\n");
908 }
909 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700910 if (!quiet) {
911 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700912 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700913 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800914
Pascal Massimino0744e842011-02-25 12:03:27 -0800915 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700916 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800917 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700918 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -0700919 if (crop != 0) {
920 // We use self-cropping using a view.
921 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
922 fprintf(stderr, "Error! Cannot crop picture\n");
923 goto Error;
924 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700925 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700926 if ((resize_w | resize_h) > 0) {
927 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
928 fprintf(stderr, "Error! Cannot resize picture\n");
929 goto Error;
930 }
931 }
932 if (picture.extra_info_type > 0) {
933 AllocExtraInfo(&picture);
934 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700935 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -0800936 WebPPictureCopy(&picture, &original_picture);
937 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700938 if (!WebPEncode(&config, &picture)) {
939 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700940 fprintf(stderr, "Error code: %d (%s)\n",
941 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700942 goto Error;
943 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800944 if (verbose) {
945 const double time = StopwatchReadAndReset(&stop_watch);
946 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
947 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800948
949 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700950 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +0000951 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -0700952 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
953 } else if (!DumpPicture(&picture, dump_file)) {
954 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
955 }
Pascal Massimino38369c02011-05-04 22:59:51 -0700956 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800957
James Zern76ec5fa2013-01-14 18:32:44 -0800958 if (keep_metadata != 0 && out != NULL) {
959 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
960 &metadata, keep_metadata)) {
961 fprintf(stderr, "Error writing WebP file with metadata!\n");
962 goto Error;
963 }
964 }
965
Pascal Massimino38369c02011-05-04 22:59:51 -0700966 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +0530967 if (config.lossless) {
968 PrintExtraInfoLossless(&picture, short_output, in_file);
969 } else {
970 PrintExtraInfoLossy(&picture, short_output, in_file);
971 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800972 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700973 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
974 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -0800975 float values[5];
976 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700977 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800978 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700979 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -0800980 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -0700981 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700982 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800983
984 Error:
985 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -0800986 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800987 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800988 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800989 if (out != NULL) {
990 fclose(out);
991 }
992
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700993 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800994}
995
James Zernc7e86ab2011-08-25 14:22:32 -0700996//------------------------------------------------------------------------------