blob: f81d101c05dd3d402d5bdb74f44a8ba0ff62e636 [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//
James Zernd6406142013-06-06 23:05:58 -07003// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08008// -----------------------------------------------------------------------------
9//
10// simple command line calling the WebPEncode function.
11// Encodes a raw .YUV into WebP bitstream
12//
13// Author: Skal (pascal.massimino@gmail.com)
14
15#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070016#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080017#include <string.h>
18
James Zern31f9dc62011-06-06 17:56:50 -070019#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
Pascal Massiminof61d14a2011-02-18 23:33:46 -080023#include "webp/encode.h"
skal1bd287a2012-12-11 11:02:39 +010024
James Zern63aba3a2012-12-03 18:20:00 -080025#include "./metadata.h"
James Zern00b29e22012-05-15 13:48:11 -070026#include "./stopwatch.h"
skal1bd287a2012-12-11 11:02:39 +010027
28#include "./jpegdec.h"
29#include "./pngdec.h"
James Zern1c1c5642012-12-06 14:04:36 -080030#include "./tiffdec.h"
James Zernddeb6ac2014-04-22 19:30:27 -070031#include "./webpdec.h"
James Zerna452a552013-01-22 18:28:52 -080032#include "./wicdec.h"
skal1bd287a2012-12-11 11:02:39 +010033
James Zernb4d0ef82011-07-15 14:53:03 -070034#ifndef WEBP_DLL
James Zern605a7122013-11-25 14:43:12 -080035#ifdef __cplusplus
James Zern04e84cf2011-11-04 15:20:08 -070036extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070037#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070038
James Zern04e84cf2011-11-04 15:20:08 -070039extern void* VP8GetCPUInfo; // opaque forward declaration.
40
James Zern605a7122013-11-25 14:43:12 -080041#ifdef __cplusplus
James Zern04e84cf2011-11-04 15:20:08 -070042} // extern "C"
43#endif
44#endif // WEBP_DLL
45
James Zernc7e86ab2011-08-25 14:22:32 -070046//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080047
48static int verbose = 0;
49
50static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000051 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080052 const int uv_width = (pic->width + 1) / 2;
53 const int uv_height = (pic->height + 1) / 2;
54 int y;
55 int ok = 0;
56
Pascal Massiminodd108172012-07-18 21:58:53 +000057 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080058 if (!WebPPictureAlloc(pic)) return ok;
59
60 for (y = 0; y < pic->height; ++y) {
61 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
62 goto End;
63 }
64 }
65 for (y = 0; y < uv_height; ++y) {
66 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
67 goto End;
68 }
69 for (y = 0; y < uv_height; ++y) {
70 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
71 goto End;
72 }
73 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000074 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080075
76 End:
77 return ok;
78}
79
James Zern31f9dc62011-06-06 17:56:50 -070080#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080081
Pascal Massimino2ab4b722011-04-25 16:58:04 -070082static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080083 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -080084 int ok;
85 if (pic->width != 0 && pic->height != 0) {
86 // If image size is specified, infer it as YUV format.
87 FILE* in_file = fopen(filename, "rb");
88 if (in_file == NULL) {
89 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
90 return 0;
91 }
92 ok = ReadYUV(in_file, pic);
93 fclose(in_file);
94 } else {
95 // If no size specified, try to decode it using WIC.
James Zerne83ff7d2013-01-24 15:37:49 -080096 ok = ReadPictureWithWIC(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080097 }
98 if (!ok) {
99 fprintf(stderr, "Error! Could not process file %s\n", filename);
100 }
101 return ok;
102}
103
James Zern31f9dc62011-06-06 17:56:50 -0700104#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800105
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800106typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700107 PNG_ = 0,
108 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700109 TIFF_, // 'TIFF' clashes with libtiff
James Zernddeb6ac2014-04-22 19:30:27 -0700110 WEBP_,
James Zerna0b27362012-01-27 17:39:47 -0800111 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800112} InputFileFormat;
113
114static InputFileFormat GetImageType(FILE* in_file) {
115 InputFileFormat format = UNSUPPORTED;
James Zernddeb6ac2014-04-22 19:30:27 -0700116 uint32_t magic1, magic2;
117 uint8_t buf[12];
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800118
James Zernddeb6ac2014-04-22 19:30:27 -0700119 if ((fread(&buf[0], 12, 1, in_file) != 1) ||
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800120 (fseek(in_file, 0, SEEK_SET) != 0)) {
121 return format;
122 }
123
James Zernddeb6ac2014-04-22 19:30:27 -0700124 magic1 = ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
125 magic2 = ((uint32_t)buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
126 if (magic1 == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700127 format = PNG_;
James Zernddeb6ac2014-04-22 19:30:27 -0700128 } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700129 format = JPEG_;
James Zernddeb6ac2014-04-22 19:30:27 -0700130 } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) {
James Zern6f76d242012-07-01 17:55:21 -0700131 format = TIFF_;
James Zernddeb6ac2014-04-22 19:30:27 -0700132 } else if (magic1 == 0x52494646 && magic2 == 0x57454250) {
133 format = WEBP_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800134 }
135 return format;
136}
137
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700138static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800139 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800140 int ok = 0;
141 FILE* in_file = fopen(filename, "rb");
142 if (in_file == NULL) {
143 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
144 return ok;
145 }
146
147 if (pic->width == 0 || pic->height == 0) {
148 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
149 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700150 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800151 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700152 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800153 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700154 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800155 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
James Zernddeb6ac2014-04-22 19:30:27 -0700156 } else if (format == WEBP_) {
157 ok = ReadWebP(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800158 }
159 } else {
160 // If image size is specified, infer it as YUV format.
161 ok = ReadYUV(in_file, pic);
162 }
163 if (!ok) {
164 fprintf(stderr, "Error! Could not process file %s\n", filename);
165 }
166
167 fclose(in_file);
168 return ok;
169}
170
James Zern31f9dc62011-06-06 17:56:50 -0700171#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800172
173static void AllocExtraInfo(WebPPicture* const pic) {
174 const int mb_w = (pic->width + 15) / 16;
175 const int mb_h = (pic->height + 15) / 16;
176 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
177}
178
179static void PrintByteCount(const int bytes[4], int total_size,
180 int* const totals) {
181 int s;
182 int total = 0;
183 for (s = 0; s < 4; ++s) {
184 fprintf(stderr, "| %7d ", bytes[s]);
185 total += bytes[s];
186 if (totals) totals[s] += bytes[s];
187 }
James Zern04e84cf2011-11-04 15:20:08 -0700188 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800189}
190
191static void PrintPercents(const int counts[4], int total) {
192 int s;
193 for (s = 0; s < 4; ++s) {
194 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
195 }
James Zern04e84cf2011-11-04 15:20:08 -0700196 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800197}
198
199static void PrintValues(const int values[4]) {
200 int s;
201 for (s = 0; s < 4; ++s) {
202 fprintf(stderr, "| %7d ", values[s]);
203 }
James Zern04e84cf2011-11-04 15:20:08 -0700204 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800205}
206
Pascal Massimino7d853d72012-07-24 16:15:36 -0700207static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
208 const char* const description) {
209 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
210 description, stats->lossless_size);
211 if (stats->lossless_features) {
212 fprintf(stderr, " * Lossless features used:");
213 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
214 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
215 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
216 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
217 fprintf(stderr, "\n");
218 }
219 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
220 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
221 if (stats->palette_size > 0) {
222 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
223 }
224}
225
Vikas Arorac4ccab62012-05-09 11:27:46 +0530226static void PrintExtraInfoLossless(const WebPPicture* const pic,
227 int short_output,
228 const char* const file_name) {
229 const WebPAuxStats* const stats = pic->stats;
230 if (short_output) {
231 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
232 } else {
233 fprintf(stderr, "File: %s\n", file_name);
234 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
235 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700236 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530237 }
238}
239
240static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
skal9bfbdd12013-03-12 00:37:42 +0100241 int full_details,
Vikas Arorac4ccab62012-05-09 11:27:46 +0530242 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800243 const WebPAuxStats* const stats = pic->stats;
244 if (short_output) {
245 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700246 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800247 const int num_i4 = stats->block_count[0];
248 const int num_i16 = stats->block_count[1];
249 const int num_skip = stats->block_count[2];
250 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800251 fprintf(stderr, "File: %s\n", file_name);
252 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700253 pic->width, pic->height,
254 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800255 fprintf(stderr, "Output: "
256 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800257 stats->coded_size,
258 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
259 if (total > 0) {
260 int totals[4] = { 0, 0, 0, 0 };
261 fprintf(stderr, "block count: intra4: %d\n"
262 " intra16: %d (-> %.2f%%)\n",
263 num_i4, num_i16, 100.f * num_i16 / total);
264 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
265 num_skip, 100.f * num_skip / total);
266 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
267 " mode-partition: %6d (%.1f%%)\n",
268 stats->header_bytes[0],
269 100.f * stats->header_bytes[0] / stats->coded_size,
270 stats->header_bytes[1],
271 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700272 if (stats->alpha_data_size > 0) {
273 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
274 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700275 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800276 fprintf(stderr, " Residuals bytes "
277 "|segment 1|segment 2|segment 3"
278 "|segment 4| total\n");
skal9bfbdd12013-03-12 00:37:42 +0100279 if (full_details) {
280 fprintf(stderr, " intra4-coeffs: ");
281 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
282 fprintf(stderr, " intra16-coeffs: ");
283 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
284 fprintf(stderr, " chroma coeffs: ");
285 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
286 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800287 fprintf(stderr, " macroblocks: ");
288 PrintPercents(stats->segment_size, total);
289 fprintf(stderr, " quantizer: ");
290 PrintValues(stats->segment_quant);
291 fprintf(stderr, " filter level: ");
292 PrintValues(stats->segment_level);
skal9bfbdd12013-03-12 00:37:42 +0100293 if (full_details) {
294 fprintf(stderr, "------------------+---------");
295 fprintf(stderr, "+---------+---------+---------+-----------------\n");
296 fprintf(stderr, " segments total: ");
297 PrintByteCount(totals, stats->coded_size, NULL);
298 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800299 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700300 if (stats->lossless_size > 0) {
301 PrintFullLosslessInfo(stats, "alpha");
302 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800303 }
skalfff2a112013-12-23 12:03:00 +0100304}
305
306static void PrintMapInfo(const WebPPicture* const pic) {
Pascal Massimino7d853d72012-07-24 16:15:36 -0700307 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800308 const int mb_w = (pic->width + 15) / 16;
309 const int mb_h = (pic->height + 15) / 16;
310 const int type = pic->extra_info_type;
311 int x, y;
312 for (y = 0; y < mb_h; ++y) {
313 for (x = 0; x < mb_w; ++x) {
314 const int c = pic->extra_info[x + y * mb_w];
315 if (type == 1) { // intra4/intra16
skale12f8742014-03-12 19:48:00 +0100316 fprintf(stderr, "%c", "+."[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800317 } else if (type == 2) { // segments
skale12f8742014-03-12 19:48:00 +0100318 fprintf(stderr, "%c", ".-*X"[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800319 } else if (type == 3) { // quantizers
skale12f8742014-03-12 19:48:00 +0100320 fprintf(stderr, "%.2d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800321 } else if (type == 6 || type == 7) {
skale12f8742014-03-12 19:48:00 +0100322 fprintf(stderr, "%3d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800323 } else {
skale12f8742014-03-12 19:48:00 +0100324 fprintf(stderr, "0x%.2x ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800325 }
326 }
skale12f8742014-03-12 19:48:00 +0100327 fprintf(stderr, "\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800328 }
329 }
330}
331
James Zernc7e86ab2011-08-25 14:22:32 -0700332//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800333
334static int MyWriter(const uint8_t* data, size_t data_size,
335 const WebPPicture* const pic) {
336 FILE* const out = (FILE*)pic->custom_ptr;
337 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
338}
339
340// Dumps a picture as a PGM file using the IMC4 layout.
341static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
342 int y;
343 const int uv_width = (picture->width + 1) / 2;
344 const int uv_height = (picture->height + 1) / 2;
345 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700346 const int alpha_height =
347 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700348 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800349 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800350 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800351 fprintf(f, "P5\n%d %d\n255\n", stride, height);
352 for (y = 0; y < picture->height; ++y) {
353 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
354 return 0;
355 if (picture->width & 1) fputc(0, f); // pad
356 }
357 for (y = 0; y < uv_height; ++y) {
358 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
359 return 0;
360 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
361 return 0;
362 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700363 for (y = 0; y < alpha_height; ++y) {
364 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
365 return 0;
366 if (picture->width & 1) fputc(0, f); // pad
367 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800368 fclose(f);
369 return 1;
370}
371
James Zern7eaee9f2013-01-11 12:25:36 -0800372// -----------------------------------------------------------------------------
373// Metadata writing.
374
375enum {
376 METADATA_EXIF = (1 << 0),
James Zernd8dc72a2013-03-13 14:04:20 -0700377 METADATA_ICC = (1 << 1),
James Zern7eaee9f2013-01-11 12:25:36 -0800378 METADATA_XMP = (1 << 2),
James Zernd8dc72a2013-03-13 14:04:20 -0700379 METADATA_ALL = METADATA_EXIF | METADATA_ICC | METADATA_XMP
James Zern7eaee9f2013-01-11 12:25:36 -0800380};
381
James Zern76ec5fa2013-01-14 18:32:44 -0800382static const int kChunkHeaderSize = 8;
383static const int kTagSize = 4;
384
James Zern0bc42682013-03-13 14:41:38 -0700385static void PrintMetadataInfo(const Metadata* const metadata,
386 int metadata_written) {
387 if (metadata == NULL || metadata_written == 0) return;
388
389 fprintf(stderr, "Metadata:\n");
James Zern7dd288f2013-03-15 16:42:58 -0700390 if (metadata_written & METADATA_ICC) {
James Zern14d42af2013-03-20 16:59:35 -0700391 fprintf(stderr, " * ICC profile: %6d bytes\n", (int)metadata->iccp.size);
James Zern0bc42682013-03-13 14:41:38 -0700392 }
393 if (metadata_written & METADATA_EXIF) {
James Zern14d42af2013-03-20 16:59:35 -0700394 fprintf(stderr, " * EXIF data: %6d bytes\n", (int)metadata->exif.size);
James Zern0bc42682013-03-13 14:41:38 -0700395 }
396 if (metadata_written & METADATA_XMP) {
James Zern14d42af2013-03-20 16:59:35 -0700397 fprintf(stderr, " * XMP data: %6d bytes\n", (int)metadata->xmp.size);
James Zern0bc42682013-03-13 14:41:38 -0700398 }
399}
400
James Zern76ec5fa2013-01-14 18:32:44 -0800401// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
402static int WriteLE(FILE* const out, uint32_t val, int num) {
403 uint8_t buf[4];
404 int i;
405 for (i = 0; i < num; ++i) {
406 buf[i] = (uint8_t)(val & 0xff);
407 val >>= 8;
408 }
409 return (fwrite(buf, num, 1, out) == 1);
410}
411
412static int WriteLE24(FILE* const out, uint32_t val) {
413 return WriteLE(out, val, 3);
414}
415
416static int WriteLE32(FILE* const out, uint32_t val) {
417 return WriteLE(out, val, 4);
418}
419
420static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
421 const MetadataPayload* const payload) {
422 const uint8_t zero = 0;
423 const size_t need_padding = payload->size & 1;
424 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
425 ok = ok && WriteLE32(out, (uint32_t)payload->size);
426 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
427 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
428}
429
430// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
431// chunk if there is metadata and 'keep' is true.
432static int UpdateFlagsAndSize(const MetadataPayload* const payload,
433 int keep, int flag,
434 uint32_t* vp8x_flags, uint64_t* metadata_size) {
435 if (keep && payload->bytes != NULL && payload->size > 0) {
436 *vp8x_flags |= flag;
437 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
438 return 1;
439 }
440 return 0;
441}
442
443// Writes a WebP file using the image contained in 'memory_writer' and the
444// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
445// availability in 'metadata'. Returns true on success.
446// For details see doc/webp-container-spec.txt#extended-file-format.
447static int WriteWebPWithMetadata(FILE* const out,
448 const WebPPicture* const picture,
449 const WebPMemoryWriter* const memory_writer,
450 const Metadata* const metadata,
James Zern0bc42682013-03-13 14:41:38 -0700451 int keep_metadata,
452 int* const metadata_written) {
James Zern76ec5fa2013-01-14 18:32:44 -0800453 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
454 const int kAlphaFlag = 0x10;
455 const int kEXIFFlag = 0x08;
456 const int kICCPFlag = 0x20;
457 const int kXMPFlag = 0x04;
458 const size_t kRiffHeaderSize = 12;
459 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
460 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
461 uint32_t flags = 0;
462 uint64_t metadata_size = 0;
463 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
464 !!(keep_metadata & METADATA_EXIF),
465 kEXIFFlag, &flags, &metadata_size);
466 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
James Zernd8dc72a2013-03-13 14:04:20 -0700467 !!(keep_metadata & METADATA_ICC),
James Zern76ec5fa2013-01-14 18:32:44 -0800468 kICCPFlag, &flags, &metadata_size);
469 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
470 !!(keep_metadata & METADATA_XMP),
471 kXMPFlag, &flags, &metadata_size);
472 uint8_t* webp = memory_writer->mem;
473 size_t webp_size = memory_writer->size;
James Zern0bc42682013-03-13 14:41:38 -0700474
475 *metadata_written = 0;
476
James Zern76ec5fa2013-01-14 18:32:44 -0800477 if (webp_size < kMinSize) return 0;
478 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
479 fprintf(stderr, "Error! Addition of metadata would exceed "
480 "container size limit.\n");
481 return 0;
482 }
483
484 if (metadata_size > 0) {
485 const int kVP8XChunkSize = 18;
486 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
487 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
488 (has_vp8x ? 0 : kVP8XChunkSize) +
489 metadata_size);
490 // RIFF
491 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
492 // RIFF size (file header size is not recorded)
493 ok = ok && WriteLE32(out, riff_size);
494 webp += kChunkHeaderSize;
495 webp_size -= kChunkHeaderSize;
496 // WEBP
497 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
498 webp += kTagSize;
499 webp_size -= kTagSize;
500 if (has_vp8x) { // update the existing VP8X flags
501 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
502 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
James Zernb5b2e3c2013-12-19 10:17:08 -0800503 webp += kVP8XChunkSize;
James Zern76ec5fa2013-01-14 18:32:44 -0800504 webp_size -= kVP8XChunkSize;
505 } else {
506 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
Urvang Joshi8ba1bf62013-07-19 11:55:09 -0700507 if (is_lossless) {
508 // Presence of alpha is stored in the 29th bit of VP8L data.
509 if (webp[kChunkHeaderSize + 3] & (1 << 5)) flags |= kAlphaFlag;
510 }
James Zern76ec5fa2013-01-14 18:32:44 -0800511 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
512 ok = ok && WriteLE32(out, flags);
513 ok = ok && WriteLE24(out, picture->width - 1);
514 ok = ok && WriteLE24(out, picture->height - 1);
515 }
James Zern0bc42682013-03-13 14:41:38 -0700516 if (write_iccp) {
517 ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
James Zern7dd288f2013-03-15 16:42:58 -0700518 *metadata_written |= METADATA_ICC;
James Zern0bc42682013-03-13 14:41:38 -0700519 }
James Zern76ec5fa2013-01-14 18:32:44 -0800520 // Image
521 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
James Zern0bc42682013-03-13 14:41:38 -0700522 if (write_exif) {
523 ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
524 *metadata_written |= METADATA_EXIF;
525 }
526 if (write_xmp) {
527 ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
528 *metadata_written |= METADATA_XMP;
529 }
James Zern76ec5fa2013-01-14 18:32:44 -0800530 return ok;
531 } else {
532 // No metadata, just write the original image file.
533 return (fwrite(webp, webp_size, 1, out) == 1);
534 }
535}
536
James Zernc7e86ab2011-08-25 14:22:32 -0700537//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800538
Pascal Massimino30971c92011-12-01 02:24:50 -0800539static int ProgressReport(int percent, const WebPPicture* const picture) {
skale12f8742014-03-12 19:48:00 +0100540 fprintf(stderr, "[%s]: %3d %% \r",
541 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800542 return 1; // all ok
543}
544
545//------------------------------------------------------------------------------
546
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700547static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800548 printf("Usage:\n\n");
549 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
550 printf("where quality is between 0 (poor) to 100 (very good).\n");
551 printf("Typical value is around 80.\n\n");
552 printf("Try -longhelp for an exhaustive list of advanced options.\n");
553}
554
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700555static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800556 printf("Usage:\n");
557 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
558 printf("If input size (-s) for an image is not specified, "
James Zernddeb6ac2014-04-22 19:30:27 -0700559 "it is assumed to be a PNG, JPEG, TIFF or WebP file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700560#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800561 printf("Windows builds can take as input any of the files handled by WIC\n");
562#endif
563 printf("options:\n");
564 printf(" -h / -help ............ short help\n");
565 printf(" -H / -longhelp ........ long help\n");
566 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530567 printf(" -alpha_q <int> ......... Transparency-compression quality "
568 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800569 printf(" -preset <string> ....... Preset setting, one of:\n");
570 printf(" default, photo, picture,\n");
571 printf(" drawing, icon, text\n");
James Zernd471f422014-05-26 13:36:16 -0700572 printf(" -preset must come first, as it overwrites other parameters.\n");
573 printf(" -z <int> ............... Activates lossless preset with given\n"
skal65b99f12014-03-11 23:25:35 +0100574 " level in [0:fast, ..., 9:slowest]\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800575 printf("\n");
576 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
577 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700578 printf(" -size <int> ............ Target size (in bytes)\n");
579 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800580 printf("\n");
581 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
582 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
583 printf(" -f <int> ............... filter strength (0=off..100)\n");
584 printf(" -sharpness <int> ....... "
585 "filter sharpness (0:most .. 7:least sharp)\n");
Pascal Massimino92668da2013-02-15 01:08:52 -0800586 printf(" -strong ................ use strong filter instead "
587 "of simple (default).\n");
588 printf(" -nostrong .............. use simple filter instead of strong.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700589 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
590 printf(" "
591 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800592 printf(" -pass <int> ............ analysis pass number (1..10)\n");
593 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700594 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
skalf8179302013-03-01 01:21:34 +0100595 printf(" -mt .................... use multi-threading if available\n");
skal9bfbdd12013-03-12 00:37:42 +0100596 printf(" -low_memory ............ reduce memory usage (slower encoding)\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700597#ifdef WEBP_EXPERIMENTAL_FEATURES
598 printf(" -444 / -422 / -gray ..... Change colorspace\n");
599#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800600 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800601 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700602 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
603 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800604 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530605 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800606 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
607 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000608 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700609 printf(" -blend_alpha <hex> ..... Blend colors against background color\n"
610 " expressed as RGB values written in\n"
611 " hexadecimal, e.g. 0xc0e0d0 for red=0xc0\n"
612 " green=0xe0 and blue=0xd0.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530613 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530614 printf(" -lossless .............. Encode image losslessly.\n");
615 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700616 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700617
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800618 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800619 printf(" -metadata <string> ..... comma separated list of metadata to\n");
620 printf(" ");
621 printf("copy from the input to the output if present.\n");
622 printf(" "
James Zernd8dc72a2013-03-13 14:04:20 -0700623 "Valid values: all, none (default), exif, icc, xmp\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800624
625 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800626 printf(" -short ................. condense printed message\n");
627 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700628 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700629#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700630 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700631#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800632 printf(" -v ..................... verbose, e.g. print encoding/decoding "
633 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800634 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 printf("\n");
636 printf("Experimental Options:\n");
skale8950592013-02-05 19:40:18 +0100637 printf(" -jpeg_like ............. Roughly match expected JPEG size.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800638 printf(" -af .................... auto-adjust filter strength.\n");
639 printf(" -pre <int> ............. pre-processing filter\n");
640 printf("\n");
641}
642
James Zernc7e86ab2011-08-25 14:22:32 -0700643//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700644// Error messages
645
646static const char* const kErrorMessages[] = {
647 "OK",
648 "OUT_OF_MEMORY: Out of memory allocating objects",
649 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
650 "NULL_PARAMETER: NULL parameter passed to function",
651 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700652 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
653 "allowed is 16383 pixels.",
654 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
655 "To reduce the size of this partition, try using less segments "
656 "with the -segments option, and eventually reduce the number of "
657 "header bits using -partition_limit. More details are available "
658 "in the manual (`man cwebp`)",
659 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800660 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800661 "FILE_TOO_BIG: File would be too big to fit in 4G",
662 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700663};
664
James Zernc7e86ab2011-08-25 14:22:32 -0700665//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800666
667int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700668 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
670 FILE *out = NULL;
671 int c;
672 int short_output = 0;
673 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530674 int keep_alpha = 1;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700675 int blend_alpha = 0;
676 uint32_t background_color = 0xffffffu;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800677 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700678 int resize_w = 0, resize_h = 0;
skal65b99f12014-03-11 23:25:35 +0100679 int lossless_preset = 6;
680 int use_lossless_preset = -1; // -1=unset, 0=don't use, 1=use it
Pascal Massimino30971c92011-12-01 02:24:50 -0800681 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800682 int keep_metadata = 0;
James Zern0bc42682013-03-13 14:41:38 -0700683 int metadata_written = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800684 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700685 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800686 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800687 WebPConfig config;
688 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800689 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800690 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800691 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700692
James Zern63aba3a2012-12-03 18:20:00 -0800693 MetadataInit(&metadata);
James Zern88d382a2013-02-01 19:17:26 -0800694 WebPMemoryWriterInit(&memory_writer);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800695 if (!WebPPictureInit(&picture) ||
696 !WebPPictureInit(&original_picture) ||
697 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800698 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700699 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 }
701
702 if (argc == 1) {
703 HelpShort();
704 return 0;
705 }
706
707 for (c = 1; c < argc; ++c) {
708 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
709 HelpShort();
710 return 0;
711 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
712 HelpLong();
713 return 0;
714 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
715 out_file = argv[++c];
716 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
717 dump_file = argv[++c];
718 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800719 } else if (!strcmp(argv[c], "-print_psnr")) {
720 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700721 print_distortion = 0;
722 } else if (!strcmp(argv[c], "-print_ssim")) {
723 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800724 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700725 } else if (!strcmp(argv[c], "-print_lsim")) {
726 config.show_compressed = 1;
727 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800728 } else if (!strcmp(argv[c], "-short")) {
skalfff2a112013-12-23 12:03:00 +0100729 ++short_output;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800730 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700731 picture.width = strtol(argv[++c], NULL, 0);
732 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800733 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700734 config.method = strtol(argv[++c], NULL, 0);
skal65b99f12014-03-11 23:25:35 +0100735 use_lossless_preset = 0; // disable -z option
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800736 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200737 config.quality = (float)strtod(argv[++c], NULL);
skal65b99f12014-03-11 23:25:35 +0100738 use_lossless_preset = 0; // disable -z option
739 } else if (!strcmp(argv[c], "-z") && c < argc - 1) {
740 lossless_preset = strtol(argv[++c], NULL, 0);
741 if (use_lossless_preset != 0) use_lossless_preset = 1;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530742 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
743 config.alpha_quality = strtol(argv[++c], NULL, 0);
744 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
745 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000746 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
747 keep_alpha = keep_alpha ? 2 : 0;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700748 } else if (!strcmp(argv[c], "-blend_alpha") && c < argc - 1) {
749 blend_alpha = 1;
750 background_color = strtol(argv[++c], NULL, 16); // <- parses '0x' prefix
751 background_color = background_color & 0x00ffffffu;
Vikas Arora252028a2012-01-05 13:04:30 +0530752 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800753 ++c;
754 if (!strcmp(argv[c], "none")) {
755 config.alpha_filtering = 0;
756 } else if (!strcmp(argv[c], "fast")) {
757 config.alpha_filtering = 1;
758 } else if (!strcmp(argv[c], "best")) {
759 config.alpha_filtering = 2;
760 } else {
761 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
762 goto Error;
763 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530764 } else if (!strcmp(argv[c], "-noalpha")) {
765 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000766 } else if (!strcmp(argv[c], "-lossless")) {
767 config.lossless = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530768 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
769 ++c;
770 if (!strcmp(argv[c], "photo")) {
771 config.image_hint = WEBP_HINT_PHOTO;
772 } else if (!strcmp(argv[c], "picture")) {
773 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700774 } else if (!strcmp(argv[c], "graph")) {
775 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530776 } else {
777 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
778 goto Error;
779 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800780 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700781 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800782 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200783 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700785 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800786 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700787 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800788 } else if (!strcmp(argv[c], "-af")) {
789 config.autofilter = 1;
skale8950592013-02-05 19:40:18 +0100790 } else if (!strcmp(argv[c], "-jpeg_like")) {
791 config.emulate_jpeg_size = 1;
skalf8179302013-03-01 01:21:34 +0100792 } else if (!strcmp(argv[c], "-mt")) {
793 ++config.thread_level; // increase thread level
skal9bfbdd12013-03-12 00:37:42 +0100794 } else if (!strcmp(argv[c], "-low_memory")) {
795 config.low_memory = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700796 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800797 config.filter_type = 1;
Pascal Massimino92668da2013-02-15 01:08:52 -0800798 } else if (!strcmp(argv[c], "-nostrong")) {
799 config.filter_type = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800800 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700801 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800802 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700803 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800804 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700805 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800806 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700807 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700808 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
809 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800810 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700811 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700812#ifdef WEBP_EXPERIMENTAL_FEATURES
813 } else if (!strcmp(argv[c], "-444")) {
814 picture.colorspace = WEBP_YUV444;
815 } else if (!strcmp(argv[c], "-422")) {
816 picture.colorspace = WEBP_YUV422;
817 } else if (!strcmp(argv[c], "-gray")) {
818 picture.colorspace = WEBP_YUV400;
819#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800820 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
821 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700822 crop_x = strtol(argv[++c], NULL, 0);
823 crop_y = strtol(argv[++c], NULL, 0);
824 crop_w = strtol(argv[++c], NULL, 0);
825 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700826 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
827 resize_w = strtol(argv[++c], NULL, 0);
828 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700829#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700830 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000831 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700832#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700833 } else if (!strcmp(argv[c], "-version")) {
834 const int version = WebPGetEncoderVersion();
835 printf("%d.%d.%d\n",
836 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
837 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800838 } else if (!strcmp(argv[c], "-progress")) {
839 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800840 } else if (!strcmp(argv[c], "-quiet")) {
841 quiet = 1;
842 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
843 WebPPreset preset;
844 ++c;
845 if (!strcmp(argv[c], "default")) {
846 preset = WEBP_PRESET_DEFAULT;
847 } else if (!strcmp(argv[c], "photo")) {
848 preset = WEBP_PRESET_PHOTO;
849 } else if (!strcmp(argv[c], "picture")) {
850 preset = WEBP_PRESET_PICTURE;
851 } else if (!strcmp(argv[c], "drawing")) {
852 preset = WEBP_PRESET_DRAWING;
853 } else if (!strcmp(argv[c], "icon")) {
854 preset = WEBP_PRESET_ICON;
855 } else if (!strcmp(argv[c], "text")) {
856 preset = WEBP_PRESET_TEXT;
857 } else {
858 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
859 goto Error;
860 }
861 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700862 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800863 goto Error;
864 }
James Zern7eaee9f2013-01-11 12:25:36 -0800865 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
866 static const struct {
867 const char* option;
868 int flag;
869 } kTokens[] = {
870 { "all", METADATA_ALL },
871 { "none", 0 },
872 { "exif", METADATA_EXIF },
James Zernd8dc72a2013-03-13 14:04:20 -0700873 { "icc", METADATA_ICC },
James Zern7eaee9f2013-01-11 12:25:36 -0800874 { "xmp", METADATA_XMP },
875 };
876 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
877 const char* start = argv[++c];
878 const char* const end = start + strlen(start);
879
880 while (start < end) {
881 size_t i;
882 const char* token = strchr(start, ',');
883 if (token == NULL) token = end;
884
885 for (i = 0; i < kNumTokens; ++i) {
886 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
887 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
888 if (kTokens[i].flag != 0) {
889 keep_metadata |= kTokens[i].flag;
890 } else {
891 keep_metadata = 0;
892 }
893 break;
894 }
895 }
896 if (i == kNumTokens) {
897 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
898 (int)(token - start), start);
899 HelpLong();
900 return -1;
901 }
902 start = token + 1;
903 }
James Zern76ec5fa2013-01-14 18:32:44 -0800904#ifdef HAVE_WINCODEC_H
James Zernd8dc72a2013-03-13 14:04:20 -0700905 if (keep_metadata != 0 && keep_metadata != METADATA_ICC) {
James Zern7eaee9f2013-01-11 12:25:36 -0800906 // TODO(jzern): remove when -metadata is supported on all platforms.
James Zerne83ff7d2013-01-24 15:37:49 -0800907 fprintf(stderr, "Warning: only ICC profile extraction is currently"
908 " supported on this platform!\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800909 }
James Zern76ec5fa2013-01-14 18:32:44 -0800910#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800911 } else if (!strcmp(argv[c], "-v")) {
912 verbose = 1;
James Zern98af68f2013-12-12 20:20:08 -0800913 } else if (!strcmp(argv[c], "--")) {
914 if (c < argc - 1) in_file = argv[++c];
915 break;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800916 } else if (argv[c][0] == '-') {
917 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
918 HelpLong();
919 return -1;
920 } else {
921 in_file = argv[c];
922 }
923 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700924 if (in_file == NULL) {
925 fprintf(stderr, "No input file specified!\n");
926 HelpShort();
927 goto Error;
928 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800929
skal65b99f12014-03-11 23:25:35 +0100930 if (use_lossless_preset == 1) {
931 if (!WebPConfigLosslessPreset(&config, lossless_preset)) {
932 fprintf(stderr, "Invalid lossless preset (-z %d)\n", lossless_preset);
933 goto Error;
934 }
935 }
936
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530937 // Check for unsupported command line options for lossless mode and log
938 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000939 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530940 if (config.target_size > 0 || config.target_PSNR > 0) {
941 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
942 " for lossless encoding. Ignoring such option(s)!\n");
943 }
944 if (config.partition_limit > 0) {
945 fprintf(stderr, "Partition limit option is not required for lossless"
946 " encoding. Ignoring this option!\n");
947 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530948 }
949
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800950 if (!WebPValidateConfig(&config)) {
951 fprintf(stderr, "Error! Invalid configuration.\n");
952 goto Error;
953 }
954
Pascal Massimino0744e842011-02-25 12:03:27 -0800955 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700956 if (verbose) {
skald51f45f2013-09-12 09:32:28 +0200957 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700958 }
James Zern7eaee9f2013-01-11 12:25:36 -0800959 if (!ReadPicture(in_file, &picture, keep_alpha,
960 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800961 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700962 goto Error;
963 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800964 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700965
966 if (blend_alpha) {
967 WebPBlendAlpha(&picture, background_color);
968 }
969
James Zern7c732e52013-02-01 20:04:20 -0800970 if (keep_alpha == 2) {
971 WebPCleanupTransparentArea(&picture);
972 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800973
Pascal Massimino0744e842011-02-25 12:03:27 -0800974 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -0800975 const double read_time = StopwatchReadAndReset(&stop_watch);
976 fprintf(stderr, "Time to read input: %.3fs\n", read_time);
Pascal Massimino0744e842011-02-25 12:03:27 -0800977 }
978
979 // Open the output
skale12f8742014-03-12 19:48:00 +0100980 if (out_file != NULL) {
981 const int use_stdout = !strcmp(out_file, "-");
982 out = use_stdout ? stdout : fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800983 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800984 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
985 goto Error;
986 } else {
987 if (!short_output && !quiet) {
988 fprintf(stderr, "Saving file '%s'\n", out_file);
989 }
990 }
James Zern76ec5fa2013-01-14 18:32:44 -0800991 if (keep_metadata == 0) {
992 picture.writer = MyWriter;
993 picture.custom_ptr = (void*)out;
994 } else {
James Zern76ec5fa2013-01-14 18:32:44 -0800995 picture.writer = WebPMemoryWrite;
996 picture.custom_ptr = (void*)&memory_writer;
997 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800998 } else {
999 out = NULL;
1000 if (!quiet && !short_output) {
1001 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1002 fprintf(stderr, "be performed, but its results discarded.\n\n");
1003 }
1004 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001005 if (!quiet) {
1006 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001007 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001008 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001009
Pascal Massimino0744e842011-02-25 12:03:27 -08001010 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001011 if (verbose) {
skald51f45f2013-09-12 09:32:28 +02001012 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001013 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001014 if (crop != 0) {
1015 // We use self-cropping using a view.
1016 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1017 fprintf(stderr, "Error! Cannot crop picture\n");
1018 goto Error;
1019 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001020 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001021 if ((resize_w | resize_h) > 0) {
1022 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1023 fprintf(stderr, "Error! Cannot resize picture\n");
1024 goto Error;
1025 }
1026 }
1027 if (picture.extra_info_type > 0) {
1028 AllocExtraInfo(&picture);
1029 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001030 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001031 WebPPictureCopy(&picture, &original_picture);
1032 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001033 if (!WebPEncode(&config, &picture)) {
1034 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001035 fprintf(stderr, "Error code: %d (%s)\n",
1036 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001037 goto Error;
1038 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001039 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -08001040 const double encode_time = StopwatchReadAndReset(&stop_watch);
1041 fprintf(stderr, "Time to encode picture: %.3fs\n", encode_time);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001042 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001043
1044 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001045 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001046 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001047 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1048 } else if (!DumpPicture(&picture, dump_file)) {
1049 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1050 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001051 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001052
James Zern70490432013-12-09 20:15:54 -08001053 if (keep_metadata != 0) {
1054 if (out != NULL) {
1055 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
1056 &metadata, keep_metadata, &metadata_written)) {
1057 fprintf(stderr, "Error writing WebP file with metadata!\n");
1058 goto Error;
1059 }
1060 } else { // output is disabled, just display the metadata stats.
1061 const struct {
1062 const MetadataPayload* const payload;
1063 int flag;
1064 } *iter, info[] = {
1065 { &metadata.exif, METADATA_EXIF },
1066 { &metadata.iccp, METADATA_ICC },
1067 { &metadata.xmp, METADATA_XMP },
1068 { NULL, 0 }
1069 };
1070 uint32_t unused1 = 0;
1071 uint64_t unused2 = 0;
1072
1073 for (iter = info; iter->payload != NULL; ++iter) {
1074 if (UpdateFlagsAndSize(iter->payload, !!(keep_metadata & iter->flag),
1075 0, &unused1, &unused2)) {
1076 metadata_written |= iter->flag;
1077 }
1078 }
James Zern76ec5fa2013-01-14 18:32:44 -08001079 }
1080 }
1081
Pascal Massimino38369c02011-05-04 22:59:51 -07001082 if (!quiet) {
skalfff2a112013-12-23 12:03:00 +01001083 if (!short_output || print_distortion < 0) {
1084 if (config.lossless) {
1085 PrintExtraInfoLossless(&picture, short_output, in_file);
1086 } else {
1087 PrintExtraInfoLossy(&picture, short_output, config.low_memory, in_file);
1088 }
1089 }
1090 if (!short_output && picture.extra_info_type > 0) {
1091 PrintMapInfo(&picture);
1092 }
1093 if (print_distortion >= 0) { // print distortion
1094 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
1095 float values[5];
1096 // Comparison is performed in YUVA colorspace.
1097 if (original_picture.use_argb &&
1098 !WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A)) {
1099 fprintf(stderr, "Error while converting original picture to YUVA.\n");
1100 goto Error;
1101 }
1102 if (picture.use_argb &&
1103 !WebPPictureARGBToYUVA(&picture, WEBP_YUV420A)) {
1104 fprintf(stderr, "Error while converting compressed picture to YUVA.\n");
1105 goto Error;
1106 }
1107 if (!WebPPictureDistortion(&picture, &original_picture,
1108 print_distortion, values)) {
1109 fprintf(stderr, "Error while computing the distortion.\n");
1110 goto Error;
1111 }
1112 if (!short_output) {
1113 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1114 distortion_names[print_distortion],
1115 values[0], values[1], values[2], values[3], values[4]);
1116 } else {
1117 fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
1118 }
Vikas Arorac4ccab62012-05-09 11:27:46 +05301119 }
James Zern0bc42682013-03-13 14:41:38 -07001120 if (!short_output) {
1121 PrintMetadataInfo(&metadata, metadata_written);
1122 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001123 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001124 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001125
1126 Error:
skalaf93bdd2014-03-27 23:27:32 +01001127 WebPMemoryWriterClear(&memory_writer);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001128 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001129 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001130 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001131 WebPPictureFree(&original_picture);
skale12f8742014-03-12 19:48:00 +01001132 if (out != NULL && out != stdout) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001133 fclose(out);
1134 }
1135
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001136 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001137}
1138
James Zernc7e86ab2011-08-25 14:22:32 -07001139//------------------------------------------------------------------------------