blob: c7d0b7137ff30519afa4af14b97e3653eb9b46d9 [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
James Zern32b31372014-06-10 17:53:44 -070020#include "webp/config.h"
James Zern31f9dc62011-06-06 17:56:50 -070021#endif
22
Pascal Massiminof61d14a2011-02-18 23:33:46 -080023#include "webp/encode.h"
skal1bd287a2012-12-11 11:02:39 +010024
James Zerne9bfb112014-08-29 19:11:41 -070025#include "./example_util.h"
James Zern63aba3a2012-12-03 18:20:00 -080026#include "./metadata.h"
James Zern00b29e22012-05-15 13:48:11 -070027#include "./stopwatch.h"
skal1bd287a2012-12-11 11:02:39 +010028
29#include "./jpegdec.h"
30#include "./pngdec.h"
James Zern1c1c5642012-12-06 14:04:36 -080031#include "./tiffdec.h"
James Zernddeb6ac2014-04-22 19:30:27 -070032#include "./webpdec.h"
James Zerna452a552013-01-22 18:28:52 -080033#include "./wicdec.h"
skal1bd287a2012-12-11 11:02:39 +010034
James Zernb4d0ef82011-07-15 14:53:03 -070035#ifndef WEBP_DLL
James Zern605a7122013-11-25 14:43:12 -080036#ifdef __cplusplus
James Zern04e84cf2011-11-04 15:20:08 -070037extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070038#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070039
James Zern04e84cf2011-11-04 15:20:08 -070040extern void* VP8GetCPUInfo; // opaque forward declaration.
41
James Zern605a7122013-11-25 14:43:12 -080042#ifdef __cplusplus
James Zern04e84cf2011-11-04 15:20:08 -070043} // extern "C"
44#endif
45#endif // WEBP_DLL
46
James Zernc7e86ab2011-08-25 14:22:32 -070047//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080048
49static int verbose = 0;
50
Pascal Massiminoa9947c32015-12-08 08:22:41 +010051static int ReadYUV(const uint8_t* const data, size_t data_size,
52 WebPPicture* const pic) {
53 int y;
Pascal Massiminodd108172012-07-18 21:58:53 +000054 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080055 const int uv_width = (pic->width + 1) / 2;
56 const int uv_height = (pic->height + 1) / 2;
Pascal Massimino1c1702d2015-11-12 23:05:02 +000057 const int uv_plane_size = uv_width * uv_height;
Pascal Massimino1c1702d2015-11-12 23:05:02 +000058 const size_t expected_data_size =
59 pic->width * pic->height + 2 * uv_plane_size;
Pascal Massimino1c1702d2015-11-12 23:05:02 +000060
61 if (data_size != expected_data_size) {
62 fprintf(stderr,
Pascal Massiminoa9947c32015-12-08 08:22:41 +010063 "input data doesn't have the expected size (%d instead of %d)\n",
64 (int)data_size, (int)expected_data_size);
65 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080066 }
Pascal Massimino1c1702d2015-11-12 23:05:02 +000067
Pascal Massiminoa9947c32015-12-08 08:22:41 +010068 pic->use_argb = 0;
69 if (!WebPPictureAlloc(pic)) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080070
Pascal Massiminoa9947c32015-12-08 08:22:41 +010071 for (y = 0; y < pic->height; ++y) {
72 memcpy(pic->y + y * pic->y_stride, data + y * pic->width,
73 pic->width * sizeof(*pic->y));
74 }
75 for (y = 0; y < uv_height; ++y) {
76 const uint8_t* const uv_data = data + pic->height * pic->y_stride;
77 memcpy(pic->u + y * pic->uv_stride, uv_data + y * uv_width,
78 uv_width * sizeof(*uv_data));
79 memcpy(pic->v + y * pic->uv_stride, uv_data + y * uv_width + uv_plane_size,
80 uv_width * sizeof(*uv_data));
81 }
82 return use_argb ? WebPPictureYUVAToARGB(pic) : 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080083}
84
James Zern31f9dc62011-06-06 17:56:50 -070085#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080086
Pascal Massimino2ab4b722011-04-25 16:58:04 -070087static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080088 int keep_alpha, Metadata* const metadata) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +010089 int ok = 0;
90 const uint8_t* data = NULL;
91 size_t data_size = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080092 if (pic->width != 0 && pic->height != 0) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +010093 ok = ExUtilReadFile(filename, &data, &data_size);
94 ok = ok && ReadYUV(data, data_size, pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080095 } else {
96 // If no size specified, try to decode it using WIC.
James Zerne83ff7d2013-01-24 15:37:49 -080097 ok = ReadPictureWithWIC(filename, pic, keep_alpha, metadata);
James Zern7d039fc2014-05-24 18:30:33 -070098 if (!ok) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +010099 ok = ExUtilReadFile(filename, &data, &data_size);
100 ok = ok && ReadWebP(data, data_size, pic, keep_alpha, metadata);
James Zern7d039fc2014-05-24 18:30:33 -0700101 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800102 }
103 if (!ok) {
104 fprintf(stderr, "Error! Could not process file %s\n", filename);
105 }
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100106 free((void*)data);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800107 return ok;
108}
109
James Zern31f9dc62011-06-06 17:56:50 -0700110#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800111
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800112typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700113 PNG_ = 0,
114 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700115 TIFF_, // 'TIFF' clashes with libtiff
James Zernddeb6ac2014-04-22 19:30:27 -0700116 WEBP_,
James Zerna0b27362012-01-27 17:39:47 -0800117 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800118} InputFileFormat;
119
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100120static uint32_t GetBE32(const uint8_t buf[]) {
121 return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
122}
123
124static InputFileFormat GuessImageType(const uint8_t buf[12]) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800125 InputFileFormat format = UNSUPPORTED;
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100126 const uint32_t magic1 = GetBE32(buf + 0);
127 const uint32_t magic2 = GetBE32(buf + 8);
James Zernddeb6ac2014-04-22 19:30:27 -0700128 if (magic1 == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700129 format = PNG_;
James Zernddeb6ac2014-04-22 19:30:27 -0700130 } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700131 format = JPEG_;
James Zernddeb6ac2014-04-22 19:30:27 -0700132 } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) {
James Zern6f76d242012-07-01 17:55:21 -0700133 format = TIFF_;
James Zernddeb6ac2014-04-22 19:30:27 -0700134 } else if (magic1 == 0x52494646 && magic2 == 0x57454250) {
135 format = WEBP_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800136 }
137 return format;
138}
139
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700140static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800141 int keep_alpha, Metadata* const metadata) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100142 const uint8_t* data = NULL;
143 size_t data_size = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800144 int ok = 0;
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100145
146 ok = ExUtilReadFile(filename, &data, &data_size);
147 if (!ok) goto End;
148
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800149 if (pic->width == 0 || pic->height == 0) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100150 ok = 0;
151 if (data_size >= 12) {
152 const InputFileFormat format = GuessImageType(data);
James Zernd8921dd2012-07-02 11:24:23 -0700153 if (format == PNG_) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100154 ok = ReadPNG(data, data_size, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700155 } else if (format == JPEG_) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100156 ok = ReadJPEG(data, data_size, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700157 } else if (format == TIFF_) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100158 ok = ReadTIFF(data, data_size, pic, keep_alpha, metadata);
James Zernddeb6ac2014-04-22 19:30:27 -0700159 } else if (format == WEBP_) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100160 ok = ReadWebP(data, data_size, pic, keep_alpha, metadata);
161 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800162 }
163 } else {
164 // If image size is specified, infer it as YUV format.
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100165 ok = ReadYUV(data, data_size, pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800166 }
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100167 End:
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800168 if (!ok) {
169 fprintf(stderr, "Error! Could not process file %s\n", filename);
170 }
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100171 free((void*)data);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800172 return ok;
173}
174
James Zern31f9dc62011-06-06 17:56:50 -0700175#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800176
177static void AllocExtraInfo(WebPPicture* const pic) {
178 const int mb_w = (pic->width + 15) / 16;
179 const int mb_h = (pic->height + 15) / 16;
180 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
181}
182
183static void PrintByteCount(const int bytes[4], int total_size,
184 int* const totals) {
185 int s;
186 int total = 0;
187 for (s = 0; s < 4; ++s) {
188 fprintf(stderr, "| %7d ", bytes[s]);
189 total += bytes[s];
190 if (totals) totals[s] += bytes[s];
191 }
James Zern04e84cf2011-11-04 15:20:08 -0700192 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800193}
194
195static void PrintPercents(const int counts[4], int total) {
196 int s;
197 for (s = 0; s < 4; ++s) {
198 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
199 }
James Zern04e84cf2011-11-04 15:20:08 -0700200 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800201}
202
203static void PrintValues(const int values[4]) {
204 int s;
205 for (s = 0; s < 4; ++s) {
206 fprintf(stderr, "| %7d ", values[s]);
207 }
James Zern04e84cf2011-11-04 15:20:08 -0700208 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800209}
210
Pascal Massimino7d853d72012-07-24 16:15:36 -0700211static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
212 const char* const description) {
213 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
214 description, stats->lossless_size);
Vikas Arorab9014162014-09-17 14:11:52 -0700215 fprintf(stderr, " * Header size: %d bytes, image data size: %d\n",
216 stats->lossless_hdr_size, stats->lossless_data_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700217 if (stats->lossless_features) {
218 fprintf(stderr, " * Lossless features used:");
219 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
220 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
221 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
222 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
223 fprintf(stderr, "\n");
224 }
225 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
226 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
227 if (stats->palette_size > 0) {
228 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
229 }
230}
231
Vikas Arorac4ccab62012-05-09 11:27:46 +0530232static void PrintExtraInfoLossless(const WebPPicture* const pic,
233 int short_output,
234 const char* const file_name) {
235 const WebPAuxStats* const stats = pic->stats;
236 if (short_output) {
237 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
238 } else {
239 fprintf(stderr, "File: %s\n", file_name);
240 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
241 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700242 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530243 }
244}
245
246static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
skal9bfbdd12013-03-12 00:37:42 +0100247 int full_details,
Vikas Arorac4ccab62012-05-09 11:27:46 +0530248 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800249 const WebPAuxStats* const stats = pic->stats;
250 if (short_output) {
251 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700252 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800253 const int num_i4 = stats->block_count[0];
254 const int num_i16 = stats->block_count[1];
255 const int num_skip = stats->block_count[2];
256 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800257 fprintf(stderr, "File: %s\n", file_name);
258 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700259 pic->width, pic->height,
260 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800261 fprintf(stderr, "Output: "
262 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800263 stats->coded_size,
264 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
265 if (total > 0) {
266 int totals[4] = { 0, 0, 0, 0 };
267 fprintf(stderr, "block count: intra4: %d\n"
268 " intra16: %d (-> %.2f%%)\n",
269 num_i4, num_i16, 100.f * num_i16 / total);
270 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
271 num_skip, 100.f * num_skip / total);
272 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
273 " mode-partition: %6d (%.1f%%)\n",
274 stats->header_bytes[0],
275 100.f * stats->header_bytes[0] / stats->coded_size,
276 stats->header_bytes[1],
277 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700278 if (stats->alpha_data_size > 0) {
279 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
280 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700281 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800282 fprintf(stderr, " Residuals bytes "
283 "|segment 1|segment 2|segment 3"
284 "|segment 4| total\n");
skal9bfbdd12013-03-12 00:37:42 +0100285 if (full_details) {
286 fprintf(stderr, " intra4-coeffs: ");
287 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
288 fprintf(stderr, " intra16-coeffs: ");
289 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
290 fprintf(stderr, " chroma coeffs: ");
291 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
292 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800293 fprintf(stderr, " macroblocks: ");
294 PrintPercents(stats->segment_size, total);
295 fprintf(stderr, " quantizer: ");
296 PrintValues(stats->segment_quant);
297 fprintf(stderr, " filter level: ");
298 PrintValues(stats->segment_level);
skal9bfbdd12013-03-12 00:37:42 +0100299 if (full_details) {
300 fprintf(stderr, "------------------+---------");
301 fprintf(stderr, "+---------+---------+---------+-----------------\n");
302 fprintf(stderr, " segments total: ");
303 PrintByteCount(totals, stats->coded_size, NULL);
304 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800305 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700306 if (stats->lossless_size > 0) {
307 PrintFullLosslessInfo(stats, "alpha");
308 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800309 }
skalfff2a112013-12-23 12:03:00 +0100310}
311
312static void PrintMapInfo(const WebPPicture* const pic) {
Pascal Massimino7d853d72012-07-24 16:15:36 -0700313 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800314 const int mb_w = (pic->width + 15) / 16;
315 const int mb_h = (pic->height + 15) / 16;
316 const int type = pic->extra_info_type;
317 int x, y;
318 for (y = 0; y < mb_h; ++y) {
319 for (x = 0; x < mb_w; ++x) {
320 const int c = pic->extra_info[x + y * mb_w];
321 if (type == 1) { // intra4/intra16
skale12f8742014-03-12 19:48:00 +0100322 fprintf(stderr, "%c", "+."[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800323 } else if (type == 2) { // segments
skale12f8742014-03-12 19:48:00 +0100324 fprintf(stderr, "%c", ".-*X"[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800325 } else if (type == 3) { // quantizers
skale12f8742014-03-12 19:48:00 +0100326 fprintf(stderr, "%.2d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800327 } else if (type == 6 || type == 7) {
skale12f8742014-03-12 19:48:00 +0100328 fprintf(stderr, "%3d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800329 } else {
skale12f8742014-03-12 19:48:00 +0100330 fprintf(stderr, "0x%.2x ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800331 }
332 }
skale12f8742014-03-12 19:48:00 +0100333 fprintf(stderr, "\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800334 }
335 }
336}
337
James Zernc7e86ab2011-08-25 14:22:32 -0700338//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800339
340static int MyWriter(const uint8_t* data, size_t data_size,
341 const WebPPicture* const pic) {
342 FILE* const out = (FILE*)pic->custom_ptr;
343 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
344}
345
346// Dumps a picture as a PGM file using the IMC4 layout.
347static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
348 int y;
349 const int uv_width = (picture->width + 1) / 2;
350 const int uv_height = (picture->height + 1) / 2;
351 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700352 const int alpha_height =
353 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700354 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800355 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800356 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800357 fprintf(f, "P5\n%d %d\n255\n", stride, height);
358 for (y = 0; y < picture->height; ++y) {
359 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
360 return 0;
361 if (picture->width & 1) fputc(0, f); // pad
362 }
363 for (y = 0; y < uv_height; ++y) {
364 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
365 return 0;
366 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
367 return 0;
368 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700369 for (y = 0; y < alpha_height; ++y) {
370 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
371 return 0;
372 if (picture->width & 1) fputc(0, f); // pad
373 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800374 fclose(f);
375 return 1;
376}
377
James Zern7eaee9f2013-01-11 12:25:36 -0800378// -----------------------------------------------------------------------------
379// Metadata writing.
380
381enum {
382 METADATA_EXIF = (1 << 0),
James Zernd8dc72a2013-03-13 14:04:20 -0700383 METADATA_ICC = (1 << 1),
James Zern7eaee9f2013-01-11 12:25:36 -0800384 METADATA_XMP = (1 << 2),
James Zernd8dc72a2013-03-13 14:04:20 -0700385 METADATA_ALL = METADATA_EXIF | METADATA_ICC | METADATA_XMP
James Zern7eaee9f2013-01-11 12:25:36 -0800386};
387
James Zern76ec5fa2013-01-14 18:32:44 -0800388static const int kChunkHeaderSize = 8;
389static const int kTagSize = 4;
390
James Zern0bc42682013-03-13 14:41:38 -0700391static void PrintMetadataInfo(const Metadata* const metadata,
392 int metadata_written) {
393 if (metadata == NULL || metadata_written == 0) return;
394
395 fprintf(stderr, "Metadata:\n");
James Zern7dd288f2013-03-15 16:42:58 -0700396 if (metadata_written & METADATA_ICC) {
James Zern14d42af2013-03-20 16:59:35 -0700397 fprintf(stderr, " * ICC profile: %6d bytes\n", (int)metadata->iccp.size);
James Zern0bc42682013-03-13 14:41:38 -0700398 }
399 if (metadata_written & METADATA_EXIF) {
James Zern14d42af2013-03-20 16:59:35 -0700400 fprintf(stderr, " * EXIF data: %6d bytes\n", (int)metadata->exif.size);
James Zern0bc42682013-03-13 14:41:38 -0700401 }
402 if (metadata_written & METADATA_XMP) {
James Zern14d42af2013-03-20 16:59:35 -0700403 fprintf(stderr, " * XMP data: %6d bytes\n", (int)metadata->xmp.size);
James Zern0bc42682013-03-13 14:41:38 -0700404 }
405}
406
James Zern76ec5fa2013-01-14 18:32:44 -0800407// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
408static int WriteLE(FILE* const out, uint32_t val, int num) {
409 uint8_t buf[4];
410 int i;
411 for (i = 0; i < num; ++i) {
412 buf[i] = (uint8_t)(val & 0xff);
413 val >>= 8;
414 }
415 return (fwrite(buf, num, 1, out) == 1);
416}
417
418static int WriteLE24(FILE* const out, uint32_t val) {
419 return WriteLE(out, val, 3);
420}
421
422static int WriteLE32(FILE* const out, uint32_t val) {
423 return WriteLE(out, val, 4);
424}
425
426static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
427 const MetadataPayload* const payload) {
428 const uint8_t zero = 0;
429 const size_t need_padding = payload->size & 1;
430 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
431 ok = ok && WriteLE32(out, (uint32_t)payload->size);
432 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
433 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
434}
435
436// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
437// chunk if there is metadata and 'keep' is true.
438static int UpdateFlagsAndSize(const MetadataPayload* const payload,
439 int keep, int flag,
440 uint32_t* vp8x_flags, uint64_t* metadata_size) {
441 if (keep && payload->bytes != NULL && payload->size > 0) {
442 *vp8x_flags |= flag;
443 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
444 return 1;
445 }
446 return 0;
447}
448
449// Writes a WebP file using the image contained in 'memory_writer' and the
450// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
451// availability in 'metadata'. Returns true on success.
452// For details see doc/webp-container-spec.txt#extended-file-format.
453static int WriteWebPWithMetadata(FILE* const out,
454 const WebPPicture* const picture,
455 const WebPMemoryWriter* const memory_writer,
456 const Metadata* const metadata,
James Zern0bc42682013-03-13 14:41:38 -0700457 int keep_metadata,
458 int* const metadata_written) {
James Zern76ec5fa2013-01-14 18:32:44 -0800459 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
460 const int kAlphaFlag = 0x10;
461 const int kEXIFFlag = 0x08;
462 const int kICCPFlag = 0x20;
463 const int kXMPFlag = 0x04;
464 const size_t kRiffHeaderSize = 12;
465 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
466 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
467 uint32_t flags = 0;
468 uint64_t metadata_size = 0;
469 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
470 !!(keep_metadata & METADATA_EXIF),
471 kEXIFFlag, &flags, &metadata_size);
472 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
James Zernd8dc72a2013-03-13 14:04:20 -0700473 !!(keep_metadata & METADATA_ICC),
James Zern76ec5fa2013-01-14 18:32:44 -0800474 kICCPFlag, &flags, &metadata_size);
475 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
476 !!(keep_metadata & METADATA_XMP),
477 kXMPFlag, &flags, &metadata_size);
478 uint8_t* webp = memory_writer->mem;
479 size_t webp_size = memory_writer->size;
James Zern0bc42682013-03-13 14:41:38 -0700480
481 *metadata_written = 0;
482
James Zern76ec5fa2013-01-14 18:32:44 -0800483 if (webp_size < kMinSize) return 0;
484 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
485 fprintf(stderr, "Error! Addition of metadata would exceed "
486 "container size limit.\n");
487 return 0;
488 }
489
490 if (metadata_size > 0) {
491 const int kVP8XChunkSize = 18;
492 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
493 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
494 (has_vp8x ? 0 : kVP8XChunkSize) +
495 metadata_size);
496 // RIFF
497 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
498 // RIFF size (file header size is not recorded)
499 ok = ok && WriteLE32(out, riff_size);
500 webp += kChunkHeaderSize;
501 webp_size -= kChunkHeaderSize;
502 // WEBP
503 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
504 webp += kTagSize;
505 webp_size -= kTagSize;
506 if (has_vp8x) { // update the existing VP8X flags
507 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
508 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
James Zernb5b2e3c2013-12-19 10:17:08 -0800509 webp += kVP8XChunkSize;
James Zern76ec5fa2013-01-14 18:32:44 -0800510 webp_size -= kVP8XChunkSize;
511 } else {
512 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
Urvang Joshi8ba1bf62013-07-19 11:55:09 -0700513 if (is_lossless) {
514 // Presence of alpha is stored in the 29th bit of VP8L data.
515 if (webp[kChunkHeaderSize + 3] & (1 << 5)) flags |= kAlphaFlag;
516 }
James Zern76ec5fa2013-01-14 18:32:44 -0800517 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
518 ok = ok && WriteLE32(out, flags);
519 ok = ok && WriteLE24(out, picture->width - 1);
520 ok = ok && WriteLE24(out, picture->height - 1);
521 }
James Zern0bc42682013-03-13 14:41:38 -0700522 if (write_iccp) {
523 ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
James Zern7dd288f2013-03-15 16:42:58 -0700524 *metadata_written |= METADATA_ICC;
James Zern0bc42682013-03-13 14:41:38 -0700525 }
James Zern76ec5fa2013-01-14 18:32:44 -0800526 // Image
527 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
James Zern0bc42682013-03-13 14:41:38 -0700528 if (write_exif) {
529 ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
530 *metadata_written |= METADATA_EXIF;
531 }
532 if (write_xmp) {
533 ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
534 *metadata_written |= METADATA_XMP;
535 }
James Zern76ec5fa2013-01-14 18:32:44 -0800536 return ok;
537 } else {
538 // No metadata, just write the original image file.
539 return (fwrite(webp, webp_size, 1, out) == 1);
540 }
541}
542
James Zernc7e86ab2011-08-25 14:22:32 -0700543//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800544
Pascal Massimino30971c92011-12-01 02:24:50 -0800545static int ProgressReport(int percent, const WebPPicture* const picture) {
skale12f8742014-03-12 19:48:00 +0100546 fprintf(stderr, "[%s]: %3d %% \r",
547 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800548 return 1; // all ok
549}
550
551//------------------------------------------------------------------------------
552
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700553static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800554 printf("Usage:\n\n");
555 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
556 printf("where quality is between 0 (poor) to 100 (very good).\n");
557 printf("Typical value is around 80.\n\n");
558 printf("Try -longhelp for an exhaustive list of advanced options.\n");
559}
560
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700561static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800562 printf("Usage:\n");
563 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
skal0a8b8862014-06-18 16:45:34 +0200564 printf("If input size (-s) for an image is not specified, it is\n"
565 "assumed to be a PNG, JPEG, TIFF or WebP file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700566#ifdef HAVE_WINCODEC_H
skal0a8b8862014-06-18 16:45:34 +0200567 printf("Windows builds can take as input any of the files handled by WIC.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800568#endif
skal0a8b8862014-06-18 16:45:34 +0200569 printf("\nOptions:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800570 printf(" -h / -help ............ short help\n");
571 printf(" -H / -longhelp ........ long help\n");
572 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
skal0a8b8862014-06-18 16:45:34 +0200573 printf(" -alpha_q <int> ......... transparency-compression quality "
574 "(0..100)\n");
575 printf(" -preset <string> ....... preset setting, one of:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800576 printf(" default, photo, picture,\n");
577 printf(" drawing, icon, text\n");
skal0a8b8862014-06-18 16:45:34 +0200578 printf(" -preset must come first, as it overwrites other parameters\n");
579 printf(" -z <int> ............... activates lossless preset with given\n"
skal65b99f12014-03-11 23:25:35 +0100580 " level in [0:fast, ..., 9:slowest]\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800581 printf("\n");
582 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
583 printf(" -segments <int> ........ number of segments to use (1..4)\n");
skal0a8b8862014-06-18 16:45:34 +0200584 printf(" -size <int> ............ target size (in bytes)\n");
585 printf(" -psnr <float> .......... target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800586 printf("\n");
skal0a8b8862014-06-18 16:45:34 +0200587 printf(" -s <int> <int> ......... input size (width x height) for YUV\n");
588 printf(" -sns <int> ............. spatial noise shaping (0:off, 100:max)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800589 printf(" -f <int> ............... filter strength (0=off..100)\n");
590 printf(" -sharpness <int> ....... "
591 "filter sharpness (0:most .. 7:least sharp)\n");
Pascal Massimino92668da2013-02-15 01:08:52 -0800592 printf(" -strong ................ use strong filter instead "
skal0a8b8862014-06-18 16:45:34 +0200593 "of simple (default)\n");
594 printf(" -nostrong .............. use simple filter instead of strong\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700595 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
596 printf(" "
597 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800598 printf(" -pass <int> ............ analysis pass number (1..10)\n");
599 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700600 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
skalf8179302013-03-01 01:21:34 +0100601 printf(" -mt .................... use multi-threading if available\n");
skal9bfbdd12013-03-12 00:37:42 +0100602 printf(" -low_memory ............ reduce memory usage (slower encoding)\n");
skal0a8b8862014-06-18 16:45:34 +0200603 printf(" -map <int> ............. print map of extra info\n");
604 printf(" -print_psnr ............ prints averaged PSNR distortion\n");
605 printf(" -print_ssim ............ prints averaged SSIM distortion\n");
606 printf(" -print_lsim ............ prints local-similarity distortion\n");
607 printf(" -d <file.pgm> .......... dump the compressed output (PGM file)\n");
608 printf(" -alpha_method <int> .... transparency-compression method (0..1)\n");
609 printf(" -alpha_filter <string> . predictive filtering for alpha plane,\n");
610 printf(" one of: none, fast (default) or best\n");
Lode Vandevenne1f9be972015-11-16 13:14:57 +0000611 printf(" -exact ................. preserve RGB values in transparent area"
612 "\n");
skal0a8b8862014-06-18 16:45:34 +0200613 printf(" -blend_alpha <hex> ..... blend colors against background color\n"
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700614 " expressed as RGB values written in\n"
615 " hexadecimal, e.g. 0xc0e0d0 for red=0xc0\n"
skal0a8b8862014-06-18 16:45:34 +0200616 " green=0xe0 and blue=0xd0\n");
617 printf(" -noalpha ............... discard any transparency information\n");
618 printf(" -lossless .............. encode image losslessly\n");
Vikas Arora98c81382015-01-29 16:02:09 -0800619 printf(" -near_lossless <int> ... use near-lossless image\n"
Vikas Arora4c822842015-02-05 11:16:37 -0800620 " preprocessing (0..100=off)\n");
Mislav Bradac48f66b62015-09-11 12:29:07 +0000621#ifdef WEBP_EXPERIMENTAL_FEATURES
622 printf(" -delta_palettization ... use delta palettization\n");
623#endif // WEBP_EXPERIMENTAL_FEATURES
skal0a8b8862014-06-18 16:45:34 +0200624 printf(" -hint <string> ......... specify image characteristics hint,\n");
625 printf(" one of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700626
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800627 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800628 printf(" -metadata <string> ..... comma separated list of metadata to\n");
629 printf(" ");
630 printf("copy from the input to the output if present.\n");
631 printf(" "
James Zernd8dc72a2013-03-13 14:04:20 -0700632 "Valid values: all, none (default), exif, icc, xmp\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800633
634 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 printf(" -short ................. condense printed message\n");
skal0a8b8862014-06-18 16:45:34 +0200636 printf(" -quiet ................. don't print anything\n");
637 printf(" -version ............... print version number and exit\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700638#ifndef WEBP_DLL
skal0a8b8862014-06-18 16:45:34 +0200639 printf(" -noasm ................. disable all assembly optimizations\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700640#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800641 printf(" -v ..................... verbose, e.g. print encoding/decoding "
642 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800643 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 printf("\n");
645 printf("Experimental Options:\n");
skal0a8b8862014-06-18 16:45:34 +0200646 printf(" -jpeg_like ............. roughly match expected JPEG size\n");
647 printf(" -af .................... auto-adjust filter strength\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800648 printf(" -pre <int> ............. pre-processing filter\n");
649 printf("\n");
650}
651
James Zernc7e86ab2011-08-25 14:22:32 -0700652//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700653// Error messages
654
skal0b747b12014-07-21 15:44:43 +0200655static const char* const kErrorMessages[VP8_ENC_ERROR_LAST] = {
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700656 "OK",
657 "OUT_OF_MEMORY: Out of memory allocating objects",
658 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
659 "NULL_PARAMETER: NULL parameter passed to function",
660 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700661 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
662 "allowed is 16383 pixels.",
663 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
664 "To reduce the size of this partition, try using less segments "
665 "with the -segments option, and eventually reduce the number of "
666 "header bits using -partition_limit. More details are available "
667 "in the manual (`man cwebp`)",
668 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800669 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800670 "FILE_TOO_BIG: File would be too big to fit in 4G",
671 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700672};
673
James Zernc7e86ab2011-08-25 14:22:32 -0700674//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800675
676int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700677 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800678 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
679 FILE *out = NULL;
680 int c;
681 int short_output = 0;
682 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530683 int keep_alpha = 1;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700684 int blend_alpha = 0;
685 uint32_t background_color = 0xffffffu;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800686 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700687 int resize_w = 0, resize_h = 0;
skal65b99f12014-03-11 23:25:35 +0100688 int lossless_preset = 6;
689 int use_lossless_preset = -1; // -1=unset, 0=don't use, 1=use it
Pascal Massimino30971c92011-12-01 02:24:50 -0800690 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800691 int keep_metadata = 0;
James Zern0bc42682013-03-13 14:41:38 -0700692 int metadata_written = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800693 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700694 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800695 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800696 WebPConfig config;
697 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800698 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800699 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800700 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700701
James Zern63aba3a2012-12-03 18:20:00 -0800702 MetadataInit(&metadata);
James Zern88d382a2013-02-01 19:17:26 -0800703 WebPMemoryWriterInit(&memory_writer);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800704 if (!WebPPictureInit(&picture) ||
705 !WebPPictureInit(&original_picture) ||
706 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800707 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700708 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800709 }
710
711 if (argc == 1) {
712 HelpShort();
713 return 0;
714 }
715
716 for (c = 1; c < argc; ++c) {
James Zern96d43a82014-09-10 23:35:48 -0700717 int parse_error = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800718 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
719 HelpShort();
720 return 0;
721 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
722 HelpLong();
723 return 0;
724 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
725 out_file = argv[++c];
726 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
727 dump_file = argv[++c];
728 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800729 } else if (!strcmp(argv[c], "-print_psnr")) {
730 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700731 print_distortion = 0;
732 } else if (!strcmp(argv[c], "-print_ssim")) {
733 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800734 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700735 } else if (!strcmp(argv[c], "-print_lsim")) {
736 config.show_compressed = 1;
737 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800738 } else if (!strcmp(argv[c], "-short")) {
skalfff2a112013-12-23 12:03:00 +0100739 ++short_output;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800740 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
James Zern96d43a82014-09-10 23:35:48 -0700741 picture.width = ExUtilGetInt(argv[++c], 0, &parse_error);
742 picture.height = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino1c1702d2015-11-12 23:05:02 +0000743 if (picture.width > WEBP_MAX_DIMENSION || picture.width < 0 ||
744 picture.height > WEBP_MAX_DIMENSION || picture.height < 0) {
745 fprintf(stderr,
746 "Specified dimension (%d x %d) is out of range.\n",
747 picture.width, picture.height);
748 goto Error;
749 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800750 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700751 config.method = ExUtilGetInt(argv[++c], 0, &parse_error);
skal65b99f12014-03-11 23:25:35 +0100752 use_lossless_preset = 0; // disable -z option
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700754 config.quality = ExUtilGetFloat(argv[++c], &parse_error);
skal65b99f12014-03-11 23:25:35 +0100755 use_lossless_preset = 0; // disable -z option
756 } else if (!strcmp(argv[c], "-z") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700757 lossless_preset = ExUtilGetInt(argv[++c], 0, &parse_error);
skal65b99f12014-03-11 23:25:35 +0100758 if (use_lossless_preset != 0) use_lossless_preset = 1;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530759 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700760 config.alpha_quality = ExUtilGetInt(argv[++c], 0, &parse_error);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530761 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700762 config.alpha_compression = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000763 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
Lode Vandevenne1f9be972015-11-16 13:14:57 +0000764 // This flag is obsolete, does opposite of -exact.
765 config.exact = 0;
766 } else if (!strcmp(argv[c], "-exact")) {
767 config.exact = 1;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700768 } else if (!strcmp(argv[c], "-blend_alpha") && c < argc - 1) {
769 blend_alpha = 1;
James Zern96d43a82014-09-10 23:35:48 -0700770 // background color is given in hex with an optional '0x' prefix
771 background_color = ExUtilGetInt(argv[++c], 16, &parse_error);
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700772 background_color = background_color & 0x00ffffffu;
Vikas Arora252028a2012-01-05 13:04:30 +0530773 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800774 ++c;
775 if (!strcmp(argv[c], "none")) {
776 config.alpha_filtering = 0;
777 } else if (!strcmp(argv[c], "fast")) {
778 config.alpha_filtering = 1;
779 } else if (!strcmp(argv[c], "best")) {
780 config.alpha_filtering = 2;
781 } else {
782 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
783 goto Error;
784 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530785 } else if (!strcmp(argv[c], "-noalpha")) {
786 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000787 } else if (!strcmp(argv[c], "-lossless")) {
788 config.lossless = 1;
skalb5a36cc2014-08-05 19:14:57 +0200789 } else if (!strcmp(argv[c], "-near_lossless") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700790 config.near_lossless = ExUtilGetInt(argv[++c], 0, &parse_error);
skalb5a36cc2014-08-05 19:14:57 +0200791 config.lossless = 1; // use near-lossless only with lossless
Mislav Bradac48f66b62015-09-11 12:29:07 +0000792#ifdef WEBP_EXPERIMENTAL_FEATURES
793 } else if (!strcmp(argv[c], "-delta_palettization")) {
794 config.delta_palettization = 1;
795 config.lossless = 1; // use delta-palettization only with lossless
796#endif // WEBP_EXPERIMENTAL_FEATURES
Vikas Arorad3730762012-06-22 12:14:48 +0530797 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
798 ++c;
799 if (!strcmp(argv[c], "photo")) {
800 config.image_hint = WEBP_HINT_PHOTO;
801 } else if (!strcmp(argv[c], "picture")) {
802 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700803 } else if (!strcmp(argv[c], "graph")) {
804 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530805 } else {
806 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
807 goto Error;
808 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800809 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700810 config.target_size = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800811 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700812 config.target_PSNR = ExUtilGetFloat(argv[++c], &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800813 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700814 config.sns_strength = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800815 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700816 config.filter_strength = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800817 } else if (!strcmp(argv[c], "-af")) {
818 config.autofilter = 1;
skale8950592013-02-05 19:40:18 +0100819 } else if (!strcmp(argv[c], "-jpeg_like")) {
820 config.emulate_jpeg_size = 1;
skalf8179302013-03-01 01:21:34 +0100821 } else if (!strcmp(argv[c], "-mt")) {
822 ++config.thread_level; // increase thread level
skal9bfbdd12013-03-12 00:37:42 +0100823 } else if (!strcmp(argv[c], "-low_memory")) {
824 config.low_memory = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700825 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800826 config.filter_type = 1;
Pascal Massimino92668da2013-02-15 01:08:52 -0800827 } else if (!strcmp(argv[c], "-nostrong")) {
828 config.filter_type = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800829 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700830 config.filter_sharpness = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800831 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700832 config.pass = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800833 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700834 config.preprocessing = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800835 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700836 config.segments = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino900286e2011-08-23 15:58:22 -0700837 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700838 config.partition_limit = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800839 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700840 picture.extra_info_type = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800841 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
842 crop = 1;
James Zern96d43a82014-09-10 23:35:48 -0700843 crop_x = ExUtilGetInt(argv[++c], 0, &parse_error);
844 crop_y = ExUtilGetInt(argv[++c], 0, &parse_error);
845 crop_w = ExUtilGetInt(argv[++c], 0, &parse_error);
846 crop_h = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700847 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
James Zern96d43a82014-09-10 23:35:48 -0700848 resize_w = ExUtilGetInt(argv[++c], 0, &parse_error);
849 resize_h = ExUtilGetInt(argv[++c], 0, &parse_error);
James Zernb4d0ef82011-07-15 14:53:03 -0700850#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700851 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000852 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700853#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700854 } else if (!strcmp(argv[c], "-version")) {
855 const int version = WebPGetEncoderVersion();
856 printf("%d.%d.%d\n",
857 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
858 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800859 } else if (!strcmp(argv[c], "-progress")) {
860 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800861 } else if (!strcmp(argv[c], "-quiet")) {
862 quiet = 1;
863 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
864 WebPPreset preset;
865 ++c;
866 if (!strcmp(argv[c], "default")) {
867 preset = WEBP_PRESET_DEFAULT;
868 } else if (!strcmp(argv[c], "photo")) {
869 preset = WEBP_PRESET_PHOTO;
870 } else if (!strcmp(argv[c], "picture")) {
871 preset = WEBP_PRESET_PICTURE;
872 } else if (!strcmp(argv[c], "drawing")) {
873 preset = WEBP_PRESET_DRAWING;
874 } else if (!strcmp(argv[c], "icon")) {
875 preset = WEBP_PRESET_ICON;
876 } else if (!strcmp(argv[c], "text")) {
877 preset = WEBP_PRESET_TEXT;
878 } else {
879 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
880 goto Error;
881 }
882 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700883 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800884 goto Error;
885 }
James Zern7eaee9f2013-01-11 12:25:36 -0800886 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
887 static const struct {
888 const char* option;
889 int flag;
890 } kTokens[] = {
891 { "all", METADATA_ALL },
892 { "none", 0 },
893 { "exif", METADATA_EXIF },
James Zernd8dc72a2013-03-13 14:04:20 -0700894 { "icc", METADATA_ICC },
James Zern7eaee9f2013-01-11 12:25:36 -0800895 { "xmp", METADATA_XMP },
896 };
897 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
898 const char* start = argv[++c];
899 const char* const end = start + strlen(start);
900
901 while (start < end) {
902 size_t i;
903 const char* token = strchr(start, ',');
904 if (token == NULL) token = end;
905
906 for (i = 0; i < kNumTokens; ++i) {
907 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
908 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
909 if (kTokens[i].flag != 0) {
910 keep_metadata |= kTokens[i].flag;
911 } else {
912 keep_metadata = 0;
913 }
914 break;
915 }
916 }
917 if (i == kNumTokens) {
918 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
919 (int)(token - start), start);
920 HelpLong();
921 return -1;
922 }
923 start = token + 1;
924 }
James Zern76ec5fa2013-01-14 18:32:44 -0800925#ifdef HAVE_WINCODEC_H
James Zernd8dc72a2013-03-13 14:04:20 -0700926 if (keep_metadata != 0 && keep_metadata != METADATA_ICC) {
James Zern7eaee9f2013-01-11 12:25:36 -0800927 // TODO(jzern): remove when -metadata is supported on all platforms.
James Zerne83ff7d2013-01-24 15:37:49 -0800928 fprintf(stderr, "Warning: only ICC profile extraction is currently"
929 " supported on this platform!\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800930 }
James Zern76ec5fa2013-01-14 18:32:44 -0800931#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800932 } else if (!strcmp(argv[c], "-v")) {
933 verbose = 1;
James Zern98af68f2013-12-12 20:20:08 -0800934 } else if (!strcmp(argv[c], "--")) {
935 if (c < argc - 1) in_file = argv[++c];
936 break;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800937 } else if (argv[c][0] == '-') {
938 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
939 HelpLong();
940 return -1;
941 } else {
942 in_file = argv[c];
943 }
James Zern96d43a82014-09-10 23:35:48 -0700944
945 if (parse_error) {
946 HelpLong();
947 return -1;
948 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800949 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700950 if (in_file == NULL) {
951 fprintf(stderr, "No input file specified!\n");
952 HelpShort();
953 goto Error;
954 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800955
skal65b99f12014-03-11 23:25:35 +0100956 if (use_lossless_preset == 1) {
957 if (!WebPConfigLosslessPreset(&config, lossless_preset)) {
958 fprintf(stderr, "Invalid lossless preset (-z %d)\n", lossless_preset);
959 goto Error;
960 }
961 }
962
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530963 // Check for unsupported command line options for lossless mode and log
964 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000965 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530966 if (config.target_size > 0 || config.target_PSNR > 0) {
967 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
968 " for lossless encoding. Ignoring such option(s)!\n");
969 }
970 if (config.partition_limit > 0) {
971 fprintf(stderr, "Partition limit option is not required for lossless"
972 " encoding. Ignoring this option!\n");
973 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530974 }
975
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800976 if (!WebPValidateConfig(&config)) {
977 fprintf(stderr, "Error! Invalid configuration.\n");
978 goto Error;
979 }
980
Pascal Massimino78615782015-10-27 22:54:11 +0100981 // Read the input. We need to decide if we prefer ARGB or YUVA
982 // samples, depending on the expected compression mode (this saves
983 // some conversion steps).
984 picture.use_argb = (config.lossless || config.preprocessing > 0 ||
985 crop || (resize_w | resize_h) > 0);
Pascal Massimino38369c02011-05-04 22:59:51 -0700986 if (verbose) {
skald51f45f2013-09-12 09:32:28 +0200987 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700988 }
James Zern7eaee9f2013-01-11 12:25:36 -0800989 if (!ReadPicture(in_file, &picture, keep_alpha,
990 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800991 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700992 goto Error;
993 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800994 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700995
996 if (blend_alpha) {
997 WebPBlendAlpha(&picture, background_color);
998 }
999
Pascal Massimino0744e842011-02-25 12:03:27 -08001000 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -08001001 const double read_time = StopwatchReadAndReset(&stop_watch);
1002 fprintf(stderr, "Time to read input: %.3fs\n", read_time);
Pascal Massimino0744e842011-02-25 12:03:27 -08001003 }
1004
1005 // Open the output
skale12f8742014-03-12 19:48:00 +01001006 if (out_file != NULL) {
1007 const int use_stdout = !strcmp(out_file, "-");
James Zerne9bfb112014-08-29 19:11:41 -07001008 out = use_stdout ? ExUtilSetBinaryMode(stdout) : fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -08001009 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001010 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
1011 goto Error;
1012 } else {
1013 if (!short_output && !quiet) {
1014 fprintf(stderr, "Saving file '%s'\n", out_file);
1015 }
1016 }
James Zern76ec5fa2013-01-14 18:32:44 -08001017 if (keep_metadata == 0) {
1018 picture.writer = MyWriter;
1019 picture.custom_ptr = (void*)out;
1020 } else {
James Zern76ec5fa2013-01-14 18:32:44 -08001021 picture.writer = WebPMemoryWrite;
1022 picture.custom_ptr = (void*)&memory_writer;
1023 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001024 } else {
1025 out = NULL;
1026 if (!quiet && !short_output) {
1027 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1028 fprintf(stderr, "be performed, but its results discarded.\n\n");
1029 }
1030 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001031 if (!quiet) {
1032 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001033 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001034 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001035
Vikas Arorae09e9ff2014-07-28 16:04:54 -07001036 // Crop & resize.
Pascal Massimino38369c02011-05-04 22:59:51 -07001037 if (verbose) {
skald51f45f2013-09-12 09:32:28 +02001038 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001039 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001040 if (crop != 0) {
1041 // We use self-cropping using a view.
1042 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1043 fprintf(stderr, "Error! Cannot crop picture\n");
1044 goto Error;
1045 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001046 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001047 if ((resize_w | resize_h) > 0) {
1048 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1049 fprintf(stderr, "Error! Cannot resize picture\n");
1050 goto Error;
1051 }
1052 }
Vikas Arorae09e9ff2014-07-28 16:04:54 -07001053 if (verbose && (crop != 0 || (resize_w | resize_h) > 0)) {
1054 const double preproc_time = StopwatchReadAndReset(&stop_watch);
1055 fprintf(stderr, "Time to crop/resize picture: %.3fs\n", preproc_time);
1056 }
1057
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001058 if (picture.extra_info_type > 0) {
1059 AllocExtraInfo(&picture);
1060 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001061 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001062 WebPPictureCopy(&picture, &original_picture);
1063 }
Vikas Arorae09e9ff2014-07-28 16:04:54 -07001064
1065 // Compress.
1066 if (verbose) {
1067 StopwatchReset(&stop_watch);
1068 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001069 if (!WebPEncode(&config, &picture)) {
1070 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001071 fprintf(stderr, "Error code: %d (%s)\n",
1072 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001073 goto Error;
1074 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001075 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -08001076 const double encode_time = StopwatchReadAndReset(&stop_watch);
1077 fprintf(stderr, "Time to encode picture: %.3fs\n", encode_time);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001078 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001079
1080 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001081 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001082 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001083 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1084 } else if (!DumpPicture(&picture, dump_file)) {
1085 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1086 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001087 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001088
James Zern70490432013-12-09 20:15:54 -08001089 if (keep_metadata != 0) {
1090 if (out != NULL) {
1091 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
1092 &metadata, keep_metadata, &metadata_written)) {
1093 fprintf(stderr, "Error writing WebP file with metadata!\n");
1094 goto Error;
1095 }
1096 } else { // output is disabled, just display the metadata stats.
1097 const struct {
1098 const MetadataPayload* const payload;
1099 int flag;
1100 } *iter, info[] = {
1101 { &metadata.exif, METADATA_EXIF },
1102 { &metadata.iccp, METADATA_ICC },
1103 { &metadata.xmp, METADATA_XMP },
1104 { NULL, 0 }
1105 };
1106 uint32_t unused1 = 0;
1107 uint64_t unused2 = 0;
1108
1109 for (iter = info; iter->payload != NULL; ++iter) {
1110 if (UpdateFlagsAndSize(iter->payload, !!(keep_metadata & iter->flag),
1111 0, &unused1, &unused2)) {
1112 metadata_written |= iter->flag;
1113 }
1114 }
James Zern76ec5fa2013-01-14 18:32:44 -08001115 }
1116 }
1117
Pascal Massimino38369c02011-05-04 22:59:51 -07001118 if (!quiet) {
skalfff2a112013-12-23 12:03:00 +01001119 if (!short_output || print_distortion < 0) {
1120 if (config.lossless) {
1121 PrintExtraInfoLossless(&picture, short_output, in_file);
1122 } else {
1123 PrintExtraInfoLossy(&picture, short_output, config.low_memory, in_file);
1124 }
1125 }
1126 if (!short_output && picture.extra_info_type > 0) {
1127 PrintMapInfo(&picture);
1128 }
1129 if (print_distortion >= 0) { // print distortion
1130 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
1131 float values[5];
skal56a2e9f2015-08-11 17:28:29 -07001132 if (picture.use_argb != original_picture.use_argb) {
1133 // Somehow, the WebPEncode() call converted the original picture.
1134 // We need to make both match before calling WebPPictureDistortion().
1135 int ok = 0;
1136 if (picture.use_argb) {
1137 ok = WebPPictureYUVAToARGB(&original_picture);
1138 } else {
1139 ok = WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A);
1140 }
1141 if (!ok) {
1142 fprintf(stderr, "Error while converting original picture.\n");
1143 goto Error;
1144 }
skalfff2a112013-12-23 12:03:00 +01001145 }
1146 if (!WebPPictureDistortion(&picture, &original_picture,
1147 print_distortion, values)) {
1148 fprintf(stderr, "Error while computing the distortion.\n");
1149 goto Error;
1150 }
1151 if (!short_output) {
skal56a2e9f2015-08-11 17:28:29 -07001152 fprintf(stderr, "%s: ", distortion_names[print_distortion]);
1153 if (picture.use_argb) {
1154 fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n",
1155 values[0], values[1], values[2], values[3], values[4]);
1156 } else {
1157 fprintf(stderr, "Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1158 values[0], values[1], values[2], values[3], values[4]);
1159 }
skalfff2a112013-12-23 12:03:00 +01001160 } else {
1161 fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
1162 }
Vikas Arorac4ccab62012-05-09 11:27:46 +05301163 }
James Zern0bc42682013-03-13 14:41:38 -07001164 if (!short_output) {
1165 PrintMetadataInfo(&metadata, metadata_written);
1166 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001167 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001168 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001169
1170 Error:
skalaf93bdd2014-03-27 23:27:32 +01001171 WebPMemoryWriterClear(&memory_writer);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001172 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001173 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001174 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001175 WebPPictureFree(&original_picture);
skale12f8742014-03-12 19:48:00 +01001176 if (out != NULL && out != stdout) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001177 fclose(out);
1178 }
1179
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001180 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001181}
1182
James Zernc7e86ab2011-08-25 14:22:32 -07001183//------------------------------------------------------------------------------