blob: 809240728f6df82bc1559352e2574b9e63558b7b [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
James Zerne9bfb112014-08-29 19:11:41 -070023#include "./example_util.h"
Pascal Massiminoae2a7222016-05-30 21:45:38 -070024#include "./image_dec.h"
James Zern00b29e22012-05-15 13:48:11 -070025#include "./stopwatch.h"
Pascal Massiminoae2a7222016-05-30 21:45:38 -070026#include "webp/encode.h"
skal1bd287a2012-12-11 11:02:39 +010027
James Zernb4d0ef82011-07-15 14:53:03 -070028#ifndef WEBP_DLL
James Zern605a7122013-11-25 14:43:12 -080029#ifdef __cplusplus
James Zern04e84cf2011-11-04 15:20:08 -070030extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070031#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070032
James Zern04e84cf2011-11-04 15:20:08 -070033extern void* VP8GetCPUInfo; // opaque forward declaration.
34
James Zern605a7122013-11-25 14:43:12 -080035#ifdef __cplusplus
James Zern04e84cf2011-11-04 15:20:08 -070036} // extern "C"
37#endif
38#endif // WEBP_DLL
39
James Zernc7e86ab2011-08-25 14:22:32 -070040//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080041
42static int verbose = 0;
43
Pascal Massiminoa9947c32015-12-08 08:22:41 +010044static int ReadYUV(const uint8_t* const data, size_t data_size,
45 WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000046 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080047 const int uv_width = (pic->width + 1) / 2;
48 const int uv_height = (pic->height + 1) / 2;
Pascal Massiminoab8d6692016-05-24 22:54:57 -070049 const int y_plane_size = pic->width * pic->height;
Pascal Massimino1c1702d2015-11-12 23:05:02 +000050 const int uv_plane_size = uv_width * uv_height;
Pascal Massiminoab8d6692016-05-24 22:54:57 -070051 const size_t expected_data_size = y_plane_size + 2 * uv_plane_size;
Pascal Massimino1c1702d2015-11-12 23:05:02 +000052
53 if (data_size != expected_data_size) {
54 fprintf(stderr,
Pascal Massiminoa9947c32015-12-08 08:22:41 +010055 "input data doesn't have the expected size (%d instead of %d)\n",
56 (int)data_size, (int)expected_data_size);
57 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080058 }
Pascal Massimino1c1702d2015-11-12 23:05:02 +000059
Pascal Massiminoa9947c32015-12-08 08:22:41 +010060 pic->use_argb = 0;
61 if (!WebPPictureAlloc(pic)) return 0;
Pascal Massiminoab8d6692016-05-24 22:54:57 -070062 ExUtilCopyPlane(data, pic->width, pic->y, pic->y_stride,
63 pic->width, pic->height);
64 ExUtilCopyPlane(data + y_plane_size, uv_width,
65 pic->u, pic->uv_stride, uv_width, uv_height);
66 ExUtilCopyPlane(data + y_plane_size + uv_plane_size, uv_width,
67 pic->v, pic->uv_stride, uv_width, uv_height);
Pascal Massiminoa9947c32015-12-08 08:22:41 +010068 return use_argb ? WebPPictureYUVAToARGB(pic) : 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080069}
70
James Zern31f9dc62011-06-06 17:56:50 -070071#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080072
Pascal Massimino2ab4b722011-04-25 16:58:04 -070073static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080074 int keep_alpha, Metadata* const metadata) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +010075 int ok = 0;
76 const uint8_t* data = NULL;
77 size_t data_size = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080078 if (pic->width != 0 && pic->height != 0) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +010079 ok = ExUtilReadFile(filename, &data, &data_size);
80 ok = ok && ReadYUV(data, data_size, pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080081 } else {
82 // If no size specified, try to decode it using WIC.
James Zerne83ff7d2013-01-24 15:37:49 -080083 ok = ReadPictureWithWIC(filename, pic, keep_alpha, metadata);
James Zern7d039fc2014-05-24 18:30:33 -070084 if (!ok) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +010085 ok = ExUtilReadFile(filename, &data, &data_size);
86 ok = ok && ReadWebP(data, data_size, pic, keep_alpha, metadata);
James Zern7d039fc2014-05-24 18:30:33 -070087 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -080088 }
89 if (!ok) {
90 fprintf(stderr, "Error! Could not process file %s\n", filename);
91 }
Pascal Massiminoa9947c32015-12-08 08:22:41 +010092 free((void*)data);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080093 return ok;
94}
95
James Zern31f9dc62011-06-06 17:56:50 -070096#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080097
Pascal Massimino2ab4b722011-04-25 16:58:04 -070098static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080099 int keep_alpha, Metadata* const metadata) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100100 const uint8_t* data = NULL;
101 size_t data_size = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800102 int ok = 0;
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100103
104 ok = ExUtilReadFile(filename, &data, &data_size);
105 if (!ok) goto End;
106
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800107 if (pic->width == 0 || pic->height == 0) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100108 ok = 0;
109 if (data_size >= 12) {
Pascal Massiminoae2a7222016-05-30 21:45:38 -0700110 const WebPInputFileFormat format = WebPGuessImageType(data, data_size);
111 if (format == WEBP_PNG_FORMAT) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100112 ok = ReadPNG(data, data_size, pic, keep_alpha, metadata);
Pascal Massiminoae2a7222016-05-30 21:45:38 -0700113 } else if (format == WEBP_JPEG_FORMAT) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100114 ok = ReadJPEG(data, data_size, pic, metadata);
Pascal Massiminoae2a7222016-05-30 21:45:38 -0700115 } else if (format == WEBP_TIFF_FORMAT) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100116 ok = ReadTIFF(data, data_size, pic, keep_alpha, metadata);
Pascal Massiminoae2a7222016-05-30 21:45:38 -0700117 } else if (format == WEBP_WEBP_FORMAT) {
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100118 ok = ReadWebP(data, data_size, pic, keep_alpha, metadata);
119 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800120 }
121 } else {
122 // If image size is specified, infer it as YUV format.
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100123 ok = ReadYUV(data, data_size, pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800124 }
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100125 End:
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800126 if (!ok) {
127 fprintf(stderr, "Error! Could not process file %s\n", filename);
128 }
Pascal Massiminoa9947c32015-12-08 08:22:41 +0100129 free((void*)data);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800130 return ok;
131}
132
James Zern31f9dc62011-06-06 17:56:50 -0700133#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800134
135static void AllocExtraInfo(WebPPicture* const pic) {
136 const int mb_w = (pic->width + 15) / 16;
137 const int mb_h = (pic->height + 15) / 16;
138 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
139}
140
141static void PrintByteCount(const int bytes[4], int total_size,
142 int* const totals) {
143 int s;
144 int total = 0;
145 for (s = 0; s < 4; ++s) {
146 fprintf(stderr, "| %7d ", bytes[s]);
147 total += bytes[s];
148 if (totals) totals[s] += bytes[s];
149 }
James Zern04e84cf2011-11-04 15:20:08 -0700150 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800151}
152
153static void PrintPercents(const int counts[4], int total) {
154 int s;
155 for (s = 0; s < 4; ++s) {
156 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
157 }
James Zern04e84cf2011-11-04 15:20:08 -0700158 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800159}
160
161static void PrintValues(const int values[4]) {
162 int s;
163 for (s = 0; s < 4; ++s) {
164 fprintf(stderr, "| %7d ", values[s]);
165 }
James Zern04e84cf2011-11-04 15:20:08 -0700166 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800167}
168
Pascal Massimino7d853d72012-07-24 16:15:36 -0700169static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
170 const char* const description) {
171 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
172 description, stats->lossless_size);
Vikas Arorab9014162014-09-17 14:11:52 -0700173 fprintf(stderr, " * Header size: %d bytes, image data size: %d\n",
174 stats->lossless_hdr_size, stats->lossless_data_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700175 if (stats->lossless_features) {
176 fprintf(stderr, " * Lossless features used:");
177 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
178 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
179 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
180 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
181 fprintf(stderr, "\n");
182 }
183 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
184 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
185 if (stats->palette_size > 0) {
186 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
187 }
188}
189
Vikas Arorac4ccab62012-05-09 11:27:46 +0530190static void PrintExtraInfoLossless(const WebPPicture* const pic,
191 int short_output,
192 const char* const file_name) {
193 const WebPAuxStats* const stats = pic->stats;
194 if (short_output) {
195 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
196 } else {
197 fprintf(stderr, "File: %s\n", file_name);
198 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
199 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700200 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530201 }
202}
203
204static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
skal9bfbdd12013-03-12 00:37:42 +0100205 int full_details,
Vikas Arorac4ccab62012-05-09 11:27:46 +0530206 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800207 const WebPAuxStats* const stats = pic->stats;
208 if (short_output) {
209 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700210 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800211 const int num_i4 = stats->block_count[0];
212 const int num_i16 = stats->block_count[1];
213 const int num_skip = stats->block_count[2];
214 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800215 fprintf(stderr, "File: %s\n", file_name);
216 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700217 pic->width, pic->height,
218 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800219 fprintf(stderr, "Output: "
220 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800221 stats->coded_size,
222 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
223 if (total > 0) {
224 int totals[4] = { 0, 0, 0, 0 };
225 fprintf(stderr, "block count: intra4: %d\n"
226 " intra16: %d (-> %.2f%%)\n",
227 num_i4, num_i16, 100.f * num_i16 / total);
228 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
229 num_skip, 100.f * num_skip / total);
230 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
231 " mode-partition: %6d (%.1f%%)\n",
232 stats->header_bytes[0],
233 100.f * stats->header_bytes[0] / stats->coded_size,
234 stats->header_bytes[1],
235 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700236 if (stats->alpha_data_size > 0) {
237 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
238 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700239 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800240 fprintf(stderr, " Residuals bytes "
241 "|segment 1|segment 2|segment 3"
242 "|segment 4| total\n");
skal9bfbdd12013-03-12 00:37:42 +0100243 if (full_details) {
244 fprintf(stderr, " intra4-coeffs: ");
245 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
246 fprintf(stderr, " intra16-coeffs: ");
247 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
248 fprintf(stderr, " chroma coeffs: ");
249 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
250 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800251 fprintf(stderr, " macroblocks: ");
252 PrintPercents(stats->segment_size, total);
253 fprintf(stderr, " quantizer: ");
254 PrintValues(stats->segment_quant);
255 fprintf(stderr, " filter level: ");
256 PrintValues(stats->segment_level);
skal9bfbdd12013-03-12 00:37:42 +0100257 if (full_details) {
258 fprintf(stderr, "------------------+---------");
259 fprintf(stderr, "+---------+---------+---------+-----------------\n");
260 fprintf(stderr, " segments total: ");
261 PrintByteCount(totals, stats->coded_size, NULL);
262 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800263 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700264 if (stats->lossless_size > 0) {
265 PrintFullLosslessInfo(stats, "alpha");
266 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800267 }
skalfff2a112013-12-23 12:03:00 +0100268}
269
270static void PrintMapInfo(const WebPPicture* const pic) {
Pascal Massimino7d853d72012-07-24 16:15:36 -0700271 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800272 const int mb_w = (pic->width + 15) / 16;
273 const int mb_h = (pic->height + 15) / 16;
274 const int type = pic->extra_info_type;
275 int x, y;
276 for (y = 0; y < mb_h; ++y) {
277 for (x = 0; x < mb_w; ++x) {
278 const int c = pic->extra_info[x + y * mb_w];
279 if (type == 1) { // intra4/intra16
skale12f8742014-03-12 19:48:00 +0100280 fprintf(stderr, "%c", "+."[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800281 } else if (type == 2) { // segments
skale12f8742014-03-12 19:48:00 +0100282 fprintf(stderr, "%c", ".-*X"[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800283 } else if (type == 3) { // quantizers
skale12f8742014-03-12 19:48:00 +0100284 fprintf(stderr, "%.2d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800285 } else if (type == 6 || type == 7) {
skale12f8742014-03-12 19:48:00 +0100286 fprintf(stderr, "%3d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800287 } else {
skale12f8742014-03-12 19:48:00 +0100288 fprintf(stderr, "0x%.2x ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800289 }
290 }
skale12f8742014-03-12 19:48:00 +0100291 fprintf(stderr, "\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800292 }
293 }
294}
295
James Zernc7e86ab2011-08-25 14:22:32 -0700296//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800297
298static int MyWriter(const uint8_t* data, size_t data_size,
299 const WebPPicture* const pic) {
300 FILE* const out = (FILE*)pic->custom_ptr;
301 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
302}
303
304// Dumps a picture as a PGM file using the IMC4 layout.
305static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
306 int y;
307 const int uv_width = (picture->width + 1) / 2;
308 const int uv_height = (picture->height + 1) / 2;
309 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700310 const int alpha_height =
311 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700312 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800313 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800314 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800315 fprintf(f, "P5\n%d %d\n255\n", stride, height);
316 for (y = 0; y < picture->height; ++y) {
317 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
318 return 0;
319 if (picture->width & 1) fputc(0, f); // pad
320 }
321 for (y = 0; y < uv_height; ++y) {
322 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
323 return 0;
324 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
325 return 0;
326 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700327 for (y = 0; y < alpha_height; ++y) {
328 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
329 return 0;
330 if (picture->width & 1) fputc(0, f); // pad
331 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800332 fclose(f);
333 return 1;
334}
335
James Zern7eaee9f2013-01-11 12:25:36 -0800336// -----------------------------------------------------------------------------
337// Metadata writing.
338
339enum {
340 METADATA_EXIF = (1 << 0),
James Zernd8dc72a2013-03-13 14:04:20 -0700341 METADATA_ICC = (1 << 1),
James Zern7eaee9f2013-01-11 12:25:36 -0800342 METADATA_XMP = (1 << 2),
James Zernd8dc72a2013-03-13 14:04:20 -0700343 METADATA_ALL = METADATA_EXIF | METADATA_ICC | METADATA_XMP
James Zern7eaee9f2013-01-11 12:25:36 -0800344};
345
James Zern76ec5fa2013-01-14 18:32:44 -0800346static const int kChunkHeaderSize = 8;
347static const int kTagSize = 4;
348
James Zern0bc42682013-03-13 14:41:38 -0700349static void PrintMetadataInfo(const Metadata* const metadata,
350 int metadata_written) {
351 if (metadata == NULL || metadata_written == 0) return;
352
353 fprintf(stderr, "Metadata:\n");
James Zern7dd288f2013-03-15 16:42:58 -0700354 if (metadata_written & METADATA_ICC) {
James Zern14d42af2013-03-20 16:59:35 -0700355 fprintf(stderr, " * ICC profile: %6d bytes\n", (int)metadata->iccp.size);
James Zern0bc42682013-03-13 14:41:38 -0700356 }
357 if (metadata_written & METADATA_EXIF) {
James Zern14d42af2013-03-20 16:59:35 -0700358 fprintf(stderr, " * EXIF data: %6d bytes\n", (int)metadata->exif.size);
James Zern0bc42682013-03-13 14:41:38 -0700359 }
360 if (metadata_written & METADATA_XMP) {
James Zern14d42af2013-03-20 16:59:35 -0700361 fprintf(stderr, " * XMP data: %6d bytes\n", (int)metadata->xmp.size);
James Zern0bc42682013-03-13 14:41:38 -0700362 }
363}
364
James Zern76ec5fa2013-01-14 18:32:44 -0800365// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
366static int WriteLE(FILE* const out, uint32_t val, int num) {
367 uint8_t buf[4];
368 int i;
369 for (i = 0; i < num; ++i) {
370 buf[i] = (uint8_t)(val & 0xff);
371 val >>= 8;
372 }
373 return (fwrite(buf, num, 1, out) == 1);
374}
375
376static int WriteLE24(FILE* const out, uint32_t val) {
377 return WriteLE(out, val, 3);
378}
379
380static int WriteLE32(FILE* const out, uint32_t val) {
381 return WriteLE(out, val, 4);
382}
383
384static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
385 const MetadataPayload* const payload) {
386 const uint8_t zero = 0;
387 const size_t need_padding = payload->size & 1;
388 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
389 ok = ok && WriteLE32(out, (uint32_t)payload->size);
390 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
391 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
392}
393
394// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
395// chunk if there is metadata and 'keep' is true.
396static int UpdateFlagsAndSize(const MetadataPayload* const payload,
397 int keep, int flag,
398 uint32_t* vp8x_flags, uint64_t* metadata_size) {
399 if (keep && payload->bytes != NULL && payload->size > 0) {
400 *vp8x_flags |= flag;
401 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
402 return 1;
403 }
404 return 0;
405}
406
407// Writes a WebP file using the image contained in 'memory_writer' and the
408// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
409// availability in 'metadata'. Returns true on success.
410// For details see doc/webp-container-spec.txt#extended-file-format.
411static int WriteWebPWithMetadata(FILE* const out,
412 const WebPPicture* const picture,
413 const WebPMemoryWriter* const memory_writer,
414 const Metadata* const metadata,
James Zern0bc42682013-03-13 14:41:38 -0700415 int keep_metadata,
416 int* const metadata_written) {
James Zern76ec5fa2013-01-14 18:32:44 -0800417 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
418 const int kAlphaFlag = 0x10;
419 const int kEXIFFlag = 0x08;
420 const int kICCPFlag = 0x20;
421 const int kXMPFlag = 0x04;
422 const size_t kRiffHeaderSize = 12;
423 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
424 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
425 uint32_t flags = 0;
426 uint64_t metadata_size = 0;
427 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
428 !!(keep_metadata & METADATA_EXIF),
429 kEXIFFlag, &flags, &metadata_size);
430 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
James Zernd8dc72a2013-03-13 14:04:20 -0700431 !!(keep_metadata & METADATA_ICC),
James Zern76ec5fa2013-01-14 18:32:44 -0800432 kICCPFlag, &flags, &metadata_size);
433 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
434 !!(keep_metadata & METADATA_XMP),
435 kXMPFlag, &flags, &metadata_size);
436 uint8_t* webp = memory_writer->mem;
437 size_t webp_size = memory_writer->size;
James Zern0bc42682013-03-13 14:41:38 -0700438
439 *metadata_written = 0;
440
James Zern76ec5fa2013-01-14 18:32:44 -0800441 if (webp_size < kMinSize) return 0;
442 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
443 fprintf(stderr, "Error! Addition of metadata would exceed "
444 "container size limit.\n");
445 return 0;
446 }
447
448 if (metadata_size > 0) {
449 const int kVP8XChunkSize = 18;
450 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
451 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
452 (has_vp8x ? 0 : kVP8XChunkSize) +
453 metadata_size);
454 // RIFF
455 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
456 // RIFF size (file header size is not recorded)
457 ok = ok && WriteLE32(out, riff_size);
458 webp += kChunkHeaderSize;
459 webp_size -= kChunkHeaderSize;
460 // WEBP
461 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
462 webp += kTagSize;
463 webp_size -= kTagSize;
464 if (has_vp8x) { // update the existing VP8X flags
465 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
466 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
James Zernb5b2e3c2013-12-19 10:17:08 -0800467 webp += kVP8XChunkSize;
James Zern76ec5fa2013-01-14 18:32:44 -0800468 webp_size -= kVP8XChunkSize;
469 } else {
470 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
Urvang Joshi8ba1bf62013-07-19 11:55:09 -0700471 if (is_lossless) {
472 // Presence of alpha is stored in the 29th bit of VP8L data.
473 if (webp[kChunkHeaderSize + 3] & (1 << 5)) flags |= kAlphaFlag;
474 }
James Zern76ec5fa2013-01-14 18:32:44 -0800475 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
476 ok = ok && WriteLE32(out, flags);
477 ok = ok && WriteLE24(out, picture->width - 1);
478 ok = ok && WriteLE24(out, picture->height - 1);
479 }
James Zern0bc42682013-03-13 14:41:38 -0700480 if (write_iccp) {
481 ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
James Zern7dd288f2013-03-15 16:42:58 -0700482 *metadata_written |= METADATA_ICC;
James Zern0bc42682013-03-13 14:41:38 -0700483 }
James Zern76ec5fa2013-01-14 18:32:44 -0800484 // Image
485 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
James Zern0bc42682013-03-13 14:41:38 -0700486 if (write_exif) {
487 ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
488 *metadata_written |= METADATA_EXIF;
489 }
490 if (write_xmp) {
491 ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
492 *metadata_written |= METADATA_XMP;
493 }
James Zern76ec5fa2013-01-14 18:32:44 -0800494 return ok;
495 } else {
496 // No metadata, just write the original image file.
497 return (fwrite(webp, webp_size, 1, out) == 1);
498 }
499}
500
James Zernc7e86ab2011-08-25 14:22:32 -0700501//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800502
Pascal Massimino30971c92011-12-01 02:24:50 -0800503static int ProgressReport(int percent, const WebPPicture* const picture) {
skale12f8742014-03-12 19:48:00 +0100504 fprintf(stderr, "[%s]: %3d %% \r",
505 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800506 return 1; // all ok
507}
508
509//------------------------------------------------------------------------------
510
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700511static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800512 printf("Usage:\n\n");
513 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
514 printf("where quality is between 0 (poor) to 100 (very good).\n");
515 printf("Typical value is around 80.\n\n");
516 printf("Try -longhelp for an exhaustive list of advanced options.\n");
517}
518
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700519static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800520 printf("Usage:\n");
521 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
skal0a8b8862014-06-18 16:45:34 +0200522 printf("If input size (-s) for an image is not specified, it is\n"
523 "assumed to be a PNG, JPEG, TIFF or WebP file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700524#ifdef HAVE_WINCODEC_H
skal0a8b8862014-06-18 16:45:34 +0200525 printf("Windows builds can take as input any of the files handled by WIC.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800526#endif
skal0a8b8862014-06-18 16:45:34 +0200527 printf("\nOptions:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800528 printf(" -h / -help ............ short help\n");
529 printf(" -H / -longhelp ........ long help\n");
530 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
skal0a8b8862014-06-18 16:45:34 +0200531 printf(" -alpha_q <int> ......... transparency-compression quality "
532 "(0..100)\n");
533 printf(" -preset <string> ....... preset setting, one of:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800534 printf(" default, photo, picture,\n");
535 printf(" drawing, icon, text\n");
skal0a8b8862014-06-18 16:45:34 +0200536 printf(" -preset must come first, as it overwrites other parameters\n");
537 printf(" -z <int> ............... activates lossless preset with given\n"
skal65b99f12014-03-11 23:25:35 +0100538 " level in [0:fast, ..., 9:slowest]\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800539 printf("\n");
540 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
541 printf(" -segments <int> ........ number of segments to use (1..4)\n");
skal0a8b8862014-06-18 16:45:34 +0200542 printf(" -size <int> ............ target size (in bytes)\n");
543 printf(" -psnr <float> .......... target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800544 printf("\n");
skal0a8b8862014-06-18 16:45:34 +0200545 printf(" -s <int> <int> ......... input size (width x height) for YUV\n");
546 printf(" -sns <int> ............. spatial noise shaping (0:off, 100:max)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800547 printf(" -f <int> ............... filter strength (0=off..100)\n");
548 printf(" -sharpness <int> ....... "
549 "filter sharpness (0:most .. 7:least sharp)\n");
Pascal Massimino92668da2013-02-15 01:08:52 -0800550 printf(" -strong ................ use strong filter instead "
skal0a8b8862014-06-18 16:45:34 +0200551 "of simple (default)\n");
552 printf(" -nostrong .............. use simple filter instead of strong\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700553 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
554 printf(" "
555 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800556 printf(" -pass <int> ............ analysis pass number (1..10)\n");
557 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700558 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
skalf8179302013-03-01 01:21:34 +0100559 printf(" -mt .................... use multi-threading if available\n");
skal9bfbdd12013-03-12 00:37:42 +0100560 printf(" -low_memory ............ reduce memory usage (slower encoding)\n");
skal0a8b8862014-06-18 16:45:34 +0200561 printf(" -map <int> ............. print map of extra info\n");
562 printf(" -print_psnr ............ prints averaged PSNR distortion\n");
563 printf(" -print_ssim ............ prints averaged SSIM distortion\n");
564 printf(" -print_lsim ............ prints local-similarity distortion\n");
565 printf(" -d <file.pgm> .......... dump the compressed output (PGM file)\n");
566 printf(" -alpha_method <int> .... transparency-compression method (0..1)\n");
567 printf(" -alpha_filter <string> . predictive filtering for alpha plane,\n");
568 printf(" one of: none, fast (default) or best\n");
Lode Vandevenne1f9be972015-11-16 13:14:57 +0000569 printf(" -exact ................. preserve RGB values in transparent area"
570 "\n");
skal0a8b8862014-06-18 16:45:34 +0200571 printf(" -blend_alpha <hex> ..... blend colors against background color\n"
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700572 " expressed as RGB values written in\n"
573 " hexadecimal, e.g. 0xc0e0d0 for red=0xc0\n"
skal0a8b8862014-06-18 16:45:34 +0200574 " green=0xe0 and blue=0xd0\n");
575 printf(" -noalpha ............... discard any transparency information\n");
576 printf(" -lossless .............. encode image losslessly\n");
Vikas Arora98c81382015-01-29 16:02:09 -0800577 printf(" -near_lossless <int> ... use near-lossless image\n"
Vikas Arora4c822842015-02-05 11:16:37 -0800578 " preprocessing (0..100=off)\n");
Mislav Bradac48f66b62015-09-11 12:29:07 +0000579#ifdef WEBP_EXPERIMENTAL_FEATURES
580 printf(" -delta_palettization ... use delta palettization\n");
581#endif // WEBP_EXPERIMENTAL_FEATURES
skal0a8b8862014-06-18 16:45:34 +0200582 printf(" -hint <string> ......... specify image characteristics hint,\n");
583 printf(" one of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700584
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800585 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800586 printf(" -metadata <string> ..... comma separated list of metadata to\n");
587 printf(" ");
588 printf("copy from the input to the output if present.\n");
589 printf(" "
James Zernd8dc72a2013-03-13 14:04:20 -0700590 "Valid values: all, none (default), exif, icc, xmp\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800591
592 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800593 printf(" -short ................. condense printed message\n");
skal0a8b8862014-06-18 16:45:34 +0200594 printf(" -quiet ................. don't print anything\n");
595 printf(" -version ............... print version number and exit\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700596#ifndef WEBP_DLL
skal0a8b8862014-06-18 16:45:34 +0200597 printf(" -noasm ................. disable all assembly optimizations\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700598#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800599 printf(" -v ..................... verbose, e.g. print encoding/decoding "
600 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800601 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800602 printf("\n");
603 printf("Experimental Options:\n");
skal0a8b8862014-06-18 16:45:34 +0200604 printf(" -jpeg_like ............. roughly match expected JPEG size\n");
605 printf(" -af .................... auto-adjust filter strength\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800606 printf(" -pre <int> ............. pre-processing filter\n");
607 printf("\n");
608}
609
James Zernc7e86ab2011-08-25 14:22:32 -0700610//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700611// Error messages
612
skal0b747b12014-07-21 15:44:43 +0200613static const char* const kErrorMessages[VP8_ENC_ERROR_LAST] = {
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700614 "OK",
615 "OUT_OF_MEMORY: Out of memory allocating objects",
616 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
617 "NULL_PARAMETER: NULL parameter passed to function",
618 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700619 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
620 "allowed is 16383 pixels.",
621 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
622 "To reduce the size of this partition, try using less segments "
623 "with the -segments option, and eventually reduce the number of "
624 "header bits using -partition_limit. More details are available "
625 "in the manual (`man cwebp`)",
626 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800627 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800628 "FILE_TOO_BIG: File would be too big to fit in 4G",
629 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700630};
631
James Zernc7e86ab2011-08-25 14:22:32 -0700632//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800633
634int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700635 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800636 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
637 FILE *out = NULL;
638 int c;
639 int short_output = 0;
640 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530641 int keep_alpha = 1;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700642 int blend_alpha = 0;
643 uint32_t background_color = 0xffffffu;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700645 int resize_w = 0, resize_h = 0;
skal65b99f12014-03-11 23:25:35 +0100646 int lossless_preset = 6;
647 int use_lossless_preset = -1; // -1=unset, 0=don't use, 1=use it
Pascal Massimino30971c92011-12-01 02:24:50 -0800648 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800649 int keep_metadata = 0;
James Zern0bc42682013-03-13 14:41:38 -0700650 int metadata_written = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800651 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700652 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800653 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800654 WebPConfig config;
655 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800656 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800657 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800658 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700659
James Zern63aba3a2012-12-03 18:20:00 -0800660 MetadataInit(&metadata);
James Zern88d382a2013-02-01 19:17:26 -0800661 WebPMemoryWriterInit(&memory_writer);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800662 if (!WebPPictureInit(&picture) ||
663 !WebPPictureInit(&original_picture) ||
664 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800665 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700666 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800667 }
668
669 if (argc == 1) {
670 HelpShort();
671 return 0;
672 }
673
674 for (c = 1; c < argc; ++c) {
James Zern96d43a82014-09-10 23:35:48 -0700675 int parse_error = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800676 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
677 HelpShort();
678 return 0;
679 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
680 HelpLong();
681 return 0;
682 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
683 out_file = argv[++c];
684 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
685 dump_file = argv[++c];
686 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800687 } else if (!strcmp(argv[c], "-print_psnr")) {
688 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700689 print_distortion = 0;
690 } else if (!strcmp(argv[c], "-print_ssim")) {
691 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800692 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700693 } else if (!strcmp(argv[c], "-print_lsim")) {
694 config.show_compressed = 1;
695 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800696 } else if (!strcmp(argv[c], "-short")) {
skalfff2a112013-12-23 12:03:00 +0100697 ++short_output;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800698 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
James Zern96d43a82014-09-10 23:35:48 -0700699 picture.width = ExUtilGetInt(argv[++c], 0, &parse_error);
700 picture.height = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino1c1702d2015-11-12 23:05:02 +0000701 if (picture.width > WEBP_MAX_DIMENSION || picture.width < 0 ||
702 picture.height > WEBP_MAX_DIMENSION || picture.height < 0) {
703 fprintf(stderr,
704 "Specified dimension (%d x %d) is out of range.\n",
705 picture.width, picture.height);
706 goto Error;
707 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800708 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700709 config.method = ExUtilGetInt(argv[++c], 0, &parse_error);
skal65b99f12014-03-11 23:25:35 +0100710 use_lossless_preset = 0; // disable -z option
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800711 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700712 config.quality = ExUtilGetFloat(argv[++c], &parse_error);
skal65b99f12014-03-11 23:25:35 +0100713 use_lossless_preset = 0; // disable -z option
714 } else if (!strcmp(argv[c], "-z") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700715 lossless_preset = ExUtilGetInt(argv[++c], 0, &parse_error);
skal65b99f12014-03-11 23:25:35 +0100716 if (use_lossless_preset != 0) use_lossless_preset = 1;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530717 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700718 config.alpha_quality = ExUtilGetInt(argv[++c], 0, &parse_error);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530719 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700720 config.alpha_compression = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000721 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
Lode Vandevenne1f9be972015-11-16 13:14:57 +0000722 // This flag is obsolete, does opposite of -exact.
723 config.exact = 0;
724 } else if (!strcmp(argv[c], "-exact")) {
725 config.exact = 1;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700726 } else if (!strcmp(argv[c], "-blend_alpha") && c < argc - 1) {
727 blend_alpha = 1;
James Zern96d43a82014-09-10 23:35:48 -0700728 // background color is given in hex with an optional '0x' prefix
729 background_color = ExUtilGetInt(argv[++c], 16, &parse_error);
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700730 background_color = background_color & 0x00ffffffu;
Vikas Arora252028a2012-01-05 13:04:30 +0530731 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800732 ++c;
733 if (!strcmp(argv[c], "none")) {
734 config.alpha_filtering = 0;
735 } else if (!strcmp(argv[c], "fast")) {
736 config.alpha_filtering = 1;
737 } else if (!strcmp(argv[c], "best")) {
738 config.alpha_filtering = 2;
739 } else {
740 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
741 goto Error;
742 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530743 } else if (!strcmp(argv[c], "-noalpha")) {
744 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000745 } else if (!strcmp(argv[c], "-lossless")) {
746 config.lossless = 1;
skalb5a36cc2014-08-05 19:14:57 +0200747 } else if (!strcmp(argv[c], "-near_lossless") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700748 config.near_lossless = ExUtilGetInt(argv[++c], 0, &parse_error);
skalb5a36cc2014-08-05 19:14:57 +0200749 config.lossless = 1; // use near-lossless only with lossless
Mislav Bradac48f66b62015-09-11 12:29:07 +0000750#ifdef WEBP_EXPERIMENTAL_FEATURES
751 } else if (!strcmp(argv[c], "-delta_palettization")) {
752 config.delta_palettization = 1;
753 config.lossless = 1; // use delta-palettization only with lossless
754#endif // WEBP_EXPERIMENTAL_FEATURES
Vikas Arorad3730762012-06-22 12:14:48 +0530755 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
756 ++c;
757 if (!strcmp(argv[c], "photo")) {
758 config.image_hint = WEBP_HINT_PHOTO;
759 } else if (!strcmp(argv[c], "picture")) {
760 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700761 } else if (!strcmp(argv[c], "graph")) {
762 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530763 } else {
764 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
765 goto Error;
766 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800767 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700768 config.target_size = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800769 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700770 config.target_PSNR = ExUtilGetFloat(argv[++c], &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800771 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700772 config.sns_strength = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800773 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700774 config.filter_strength = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800775 } else if (!strcmp(argv[c], "-af")) {
776 config.autofilter = 1;
skale8950592013-02-05 19:40:18 +0100777 } else if (!strcmp(argv[c], "-jpeg_like")) {
778 config.emulate_jpeg_size = 1;
skalf8179302013-03-01 01:21:34 +0100779 } else if (!strcmp(argv[c], "-mt")) {
780 ++config.thread_level; // increase thread level
skal9bfbdd12013-03-12 00:37:42 +0100781 } else if (!strcmp(argv[c], "-low_memory")) {
782 config.low_memory = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700783 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784 config.filter_type = 1;
Pascal Massimino92668da2013-02-15 01:08:52 -0800785 } else if (!strcmp(argv[c], "-nostrong")) {
786 config.filter_type = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700788 config.filter_sharpness = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800789 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700790 config.pass = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700792 config.preprocessing = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800793 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700794 config.segments = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino900286e2011-08-23 15:58:22 -0700795 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700796 config.partition_limit = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800797 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
James Zern96d43a82014-09-10 23:35:48 -0700798 picture.extra_info_type = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
800 crop = 1;
James Zern96d43a82014-09-10 23:35:48 -0700801 crop_x = ExUtilGetInt(argv[++c], 0, &parse_error);
802 crop_y = ExUtilGetInt(argv[++c], 0, &parse_error);
803 crop_w = ExUtilGetInt(argv[++c], 0, &parse_error);
804 crop_h = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700805 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
James Zern96d43a82014-09-10 23:35:48 -0700806 resize_w = ExUtilGetInt(argv[++c], 0, &parse_error);
807 resize_h = ExUtilGetInt(argv[++c], 0, &parse_error);
James Zernb4d0ef82011-07-15 14:53:03 -0700808#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700809 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000810 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700811#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700812 } else if (!strcmp(argv[c], "-version")) {
813 const int version = WebPGetEncoderVersion();
814 printf("%d.%d.%d\n",
James Zern64da45a2015-12-11 16:40:23 -0800815 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
Pascal Massimino650ffa32011-03-24 16:17:10 -0700816 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800817 } else if (!strcmp(argv[c], "-progress")) {
818 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800819 } else if (!strcmp(argv[c], "-quiet")) {
820 quiet = 1;
821 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
822 WebPPreset preset;
823 ++c;
824 if (!strcmp(argv[c], "default")) {
825 preset = WEBP_PRESET_DEFAULT;
826 } else if (!strcmp(argv[c], "photo")) {
827 preset = WEBP_PRESET_PHOTO;
828 } else if (!strcmp(argv[c], "picture")) {
829 preset = WEBP_PRESET_PICTURE;
830 } else if (!strcmp(argv[c], "drawing")) {
831 preset = WEBP_PRESET_DRAWING;
832 } else if (!strcmp(argv[c], "icon")) {
833 preset = WEBP_PRESET_ICON;
834 } else if (!strcmp(argv[c], "text")) {
835 preset = WEBP_PRESET_TEXT;
836 } else {
837 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
838 goto Error;
839 }
840 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700841 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800842 goto Error;
843 }
James Zern7eaee9f2013-01-11 12:25:36 -0800844 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
845 static const struct {
846 const char* option;
847 int flag;
848 } kTokens[] = {
849 { "all", METADATA_ALL },
850 { "none", 0 },
851 { "exif", METADATA_EXIF },
James Zernd8dc72a2013-03-13 14:04:20 -0700852 { "icc", METADATA_ICC },
James Zern7eaee9f2013-01-11 12:25:36 -0800853 { "xmp", METADATA_XMP },
854 };
855 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
856 const char* start = argv[++c];
857 const char* const end = start + strlen(start);
858
859 while (start < end) {
860 size_t i;
861 const char* token = strchr(start, ',');
862 if (token == NULL) token = end;
863
864 for (i = 0; i < kNumTokens; ++i) {
865 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
866 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
867 if (kTokens[i].flag != 0) {
868 keep_metadata |= kTokens[i].flag;
869 } else {
870 keep_metadata = 0;
871 }
872 break;
873 }
874 }
875 if (i == kNumTokens) {
876 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
877 (int)(token - start), start);
878 HelpLong();
879 return -1;
880 }
881 start = token + 1;
882 }
James Zern76ec5fa2013-01-14 18:32:44 -0800883#ifdef HAVE_WINCODEC_H
James Zernd8dc72a2013-03-13 14:04:20 -0700884 if (keep_metadata != 0 && keep_metadata != METADATA_ICC) {
James Zern7eaee9f2013-01-11 12:25:36 -0800885 // TODO(jzern): remove when -metadata is supported on all platforms.
James Zerne83ff7d2013-01-24 15:37:49 -0800886 fprintf(stderr, "Warning: only ICC profile extraction is currently"
887 " supported on this platform!\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800888 }
James Zern76ec5fa2013-01-14 18:32:44 -0800889#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800890 } else if (!strcmp(argv[c], "-v")) {
891 verbose = 1;
James Zern98af68f2013-12-12 20:20:08 -0800892 } else if (!strcmp(argv[c], "--")) {
893 if (c < argc - 1) in_file = argv[++c];
894 break;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800895 } else if (argv[c][0] == '-') {
896 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
897 HelpLong();
898 return -1;
899 } else {
900 in_file = argv[c];
901 }
James Zern96d43a82014-09-10 23:35:48 -0700902
903 if (parse_error) {
904 HelpLong();
905 return -1;
906 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800907 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700908 if (in_file == NULL) {
909 fprintf(stderr, "No input file specified!\n");
910 HelpShort();
911 goto Error;
912 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800913
skal65b99f12014-03-11 23:25:35 +0100914 if (use_lossless_preset == 1) {
915 if (!WebPConfigLosslessPreset(&config, lossless_preset)) {
916 fprintf(stderr, "Invalid lossless preset (-z %d)\n", lossless_preset);
917 goto Error;
918 }
919 }
920
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530921 // Check for unsupported command line options for lossless mode and log
922 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000923 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530924 if (config.target_size > 0 || config.target_PSNR > 0) {
925 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
926 " for lossless encoding. Ignoring such option(s)!\n");
927 }
928 if (config.partition_limit > 0) {
929 fprintf(stderr, "Partition limit option is not required for lossless"
930 " encoding. Ignoring this option!\n");
931 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530932 }
Pascal Massimino4ed650a2016-02-13 10:22:32 +0100933 // If a target size or PSNR was given, but somehow the -pass option was
934 // omitted, force a reasonable value.
935 if (config.target_size > 0 || config.target_PSNR > 0) {
936 if (config.pass == 1) config.pass = 6;
937 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530938
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800939 if (!WebPValidateConfig(&config)) {
940 fprintf(stderr, "Error! Invalid configuration.\n");
941 goto Error;
942 }
943
Pascal Massimino78615782015-10-27 22:54:11 +0100944 // Read the input. We need to decide if we prefer ARGB or YUVA
945 // samples, depending on the expected compression mode (this saves
946 // some conversion steps).
947 picture.use_argb = (config.lossless || config.preprocessing > 0 ||
948 crop || (resize_w | resize_h) > 0);
Pascal Massimino38369c02011-05-04 22:59:51 -0700949 if (verbose) {
skald51f45f2013-09-12 09:32:28 +0200950 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700951 }
James Zern7eaee9f2013-01-11 12:25:36 -0800952 if (!ReadPicture(in_file, &picture, keep_alpha,
953 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800954 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700955 goto Error;
956 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800957 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700958
959 if (blend_alpha) {
960 WebPBlendAlpha(&picture, background_color);
961 }
962
Pascal Massimino0744e842011-02-25 12:03:27 -0800963 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -0800964 const double read_time = StopwatchReadAndReset(&stop_watch);
965 fprintf(stderr, "Time to read input: %.3fs\n", read_time);
Pascal Massimino0744e842011-02-25 12:03:27 -0800966 }
967
968 // Open the output
skale12f8742014-03-12 19:48:00 +0100969 if (out_file != NULL) {
970 const int use_stdout = !strcmp(out_file, "-");
James Zerne9bfb112014-08-29 19:11:41 -0700971 out = use_stdout ? ExUtilSetBinaryMode(stdout) : fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800972 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800973 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
974 goto Error;
975 } else {
976 if (!short_output && !quiet) {
977 fprintf(stderr, "Saving file '%s'\n", out_file);
978 }
979 }
James Zern76ec5fa2013-01-14 18:32:44 -0800980 if (keep_metadata == 0) {
981 picture.writer = MyWriter;
982 picture.custom_ptr = (void*)out;
983 } else {
James Zern76ec5fa2013-01-14 18:32:44 -0800984 picture.writer = WebPMemoryWrite;
985 picture.custom_ptr = (void*)&memory_writer;
986 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800987 } else {
988 out = NULL;
989 if (!quiet && !short_output) {
990 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
991 fprintf(stderr, "be performed, but its results discarded.\n\n");
992 }
993 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700994 if (!quiet) {
995 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700996 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700997 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800998
Vikas Arorae09e9ff2014-07-28 16:04:54 -0700999 // Crop & resize.
Pascal Massimino38369c02011-05-04 22:59:51 -07001000 if (verbose) {
skald51f45f2013-09-12 09:32:28 +02001001 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001002 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001003 if (crop != 0) {
1004 // We use self-cropping using a view.
1005 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1006 fprintf(stderr, "Error! Cannot crop picture\n");
1007 goto Error;
1008 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001009 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001010 if ((resize_w | resize_h) > 0) {
1011 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1012 fprintf(stderr, "Error! Cannot resize picture\n");
1013 goto Error;
1014 }
1015 }
Vikas Arorae09e9ff2014-07-28 16:04:54 -07001016 if (verbose && (crop != 0 || (resize_w | resize_h) > 0)) {
1017 const double preproc_time = StopwatchReadAndReset(&stop_watch);
1018 fprintf(stderr, "Time to crop/resize picture: %.3fs\n", preproc_time);
1019 }
1020
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001021 if (picture.extra_info_type > 0) {
1022 AllocExtraInfo(&picture);
1023 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001024 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001025 WebPPictureCopy(&picture, &original_picture);
1026 }
Vikas Arorae09e9ff2014-07-28 16:04:54 -07001027
1028 // Compress.
1029 if (verbose) {
1030 StopwatchReset(&stop_watch);
1031 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001032 if (!WebPEncode(&config, &picture)) {
1033 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001034 fprintf(stderr, "Error code: %d (%s)\n",
1035 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001036 goto Error;
1037 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001038 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -08001039 const double encode_time = StopwatchReadAndReset(&stop_watch);
1040 fprintf(stderr, "Time to encode picture: %.3fs\n", encode_time);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001041 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001042
1043 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001044 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001045 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001046 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1047 } else if (!DumpPicture(&picture, dump_file)) {
1048 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1049 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001050 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001051
James Zern70490432013-12-09 20:15:54 -08001052 if (keep_metadata != 0) {
1053 if (out != NULL) {
1054 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
1055 &metadata, keep_metadata, &metadata_written)) {
1056 fprintf(stderr, "Error writing WebP file with metadata!\n");
1057 goto Error;
1058 }
1059 } else { // output is disabled, just display the metadata stats.
1060 const struct {
1061 const MetadataPayload* const payload;
1062 int flag;
1063 } *iter, info[] = {
1064 { &metadata.exif, METADATA_EXIF },
1065 { &metadata.iccp, METADATA_ICC },
1066 { &metadata.xmp, METADATA_XMP },
1067 { NULL, 0 }
1068 };
1069 uint32_t unused1 = 0;
1070 uint64_t unused2 = 0;
1071
1072 for (iter = info; iter->payload != NULL; ++iter) {
1073 if (UpdateFlagsAndSize(iter->payload, !!(keep_metadata & iter->flag),
1074 0, &unused1, &unused2)) {
1075 metadata_written |= iter->flag;
1076 }
1077 }
James Zern76ec5fa2013-01-14 18:32:44 -08001078 }
1079 }
1080
Pascal Massimino38369c02011-05-04 22:59:51 -07001081 if (!quiet) {
skalfff2a112013-12-23 12:03:00 +01001082 if (!short_output || print_distortion < 0) {
1083 if (config.lossless) {
1084 PrintExtraInfoLossless(&picture, short_output, in_file);
1085 } else {
1086 PrintExtraInfoLossy(&picture, short_output, config.low_memory, in_file);
1087 }
1088 }
1089 if (!short_output && picture.extra_info_type > 0) {
1090 PrintMapInfo(&picture);
1091 }
1092 if (print_distortion >= 0) { // print distortion
1093 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
1094 float values[5];
skal56a2e9f2015-08-11 17:28:29 -07001095 if (picture.use_argb != original_picture.use_argb) {
1096 // Somehow, the WebPEncode() call converted the original picture.
1097 // We need to make both match before calling WebPPictureDistortion().
1098 int ok = 0;
1099 if (picture.use_argb) {
1100 ok = WebPPictureYUVAToARGB(&original_picture);
1101 } else {
1102 ok = WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A);
1103 }
1104 if (!ok) {
1105 fprintf(stderr, "Error while converting original picture.\n");
1106 goto Error;
1107 }
skalfff2a112013-12-23 12:03:00 +01001108 }
1109 if (!WebPPictureDistortion(&picture, &original_picture,
1110 print_distortion, values)) {
1111 fprintf(stderr, "Error while computing the distortion.\n");
1112 goto Error;
1113 }
1114 if (!short_output) {
skal56a2e9f2015-08-11 17:28:29 -07001115 fprintf(stderr, "%s: ", distortion_names[print_distortion]);
1116 if (picture.use_argb) {
1117 fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n",
1118 values[0], values[1], values[2], values[3], values[4]);
1119 } else {
1120 fprintf(stderr, "Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1121 values[0], values[1], values[2], values[3], values[4]);
1122 }
skalfff2a112013-12-23 12:03:00 +01001123 } else {
1124 fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
1125 }
Vikas Arorac4ccab62012-05-09 11:27:46 +05301126 }
James Zern0bc42682013-03-13 14:41:38 -07001127 if (!short_output) {
1128 PrintMetadataInfo(&metadata, metadata_written);
1129 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001130 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001131 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001132
1133 Error:
skalaf93bdd2014-03-27 23:27:32 +01001134 WebPMemoryWriterClear(&memory_writer);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001135 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001136 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001137 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001138 WebPPictureFree(&original_picture);
skale12f8742014-03-12 19:48:00 +01001139 if (out != NULL && out != stdout) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001140 fclose(out);
1141 }
1142
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001143 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001144}
1145
James Zernc7e86ab2011-08-25 14:22:32 -07001146//------------------------------------------------------------------------------