blob: 839ba1d49a4709ada7ac192d78a3025196069103 [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 Zernd40e8852014-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
51static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000052 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080053 const int uv_width = (pic->width + 1) / 2;
54 const int uv_height = (pic->height + 1) / 2;
55 int y;
56 int ok = 0;
57
Pascal Massiminodd108172012-07-18 21:58:53 +000058 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080059 if (!WebPPictureAlloc(pic)) return ok;
60
61 for (y = 0; y < pic->height; ++y) {
62 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
63 goto End;
64 }
65 }
66 for (y = 0; y < uv_height; ++y) {
67 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
68 goto End;
69 }
70 for (y = 0; y < uv_height; ++y) {
71 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
72 goto End;
73 }
74 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000075 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080076
77 End:
78 return ok;
79}
80
James Zern31f9dc62011-06-06 17:56:50 -070081#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080082
Pascal Massimino2ab4b722011-04-25 16:58:04 -070083static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080084 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -080085 int ok;
86 if (pic->width != 0 && pic->height != 0) {
87 // If image size is specified, infer it as YUV format.
88 FILE* in_file = fopen(filename, "rb");
89 if (in_file == NULL) {
90 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
91 return 0;
92 }
93 ok = ReadYUV(in_file, pic);
94 fclose(in_file);
95 } 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) {
99 ok = ReadWebP(filename, pic, keep_alpha, metadata);
100 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800101 }
102 if (!ok) {
103 fprintf(stderr, "Error! Could not process file %s\n", filename);
104 }
105 return ok;
106}
107
James Zern31f9dc62011-06-06 17:56:50 -0700108#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800109
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800110typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700111 PNG_ = 0,
112 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700113 TIFF_, // 'TIFF' clashes with libtiff
James Zernddeb6ac2014-04-22 19:30:27 -0700114 WEBP_,
James Zerna0b27362012-01-27 17:39:47 -0800115 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800116} InputFileFormat;
117
118static InputFileFormat GetImageType(FILE* in_file) {
119 InputFileFormat format = UNSUPPORTED;
James Zernddeb6ac2014-04-22 19:30:27 -0700120 uint32_t magic1, magic2;
121 uint8_t buf[12];
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800122
James Zernddeb6ac2014-04-22 19:30:27 -0700123 if ((fread(&buf[0], 12, 1, in_file) != 1) ||
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800124 (fseek(in_file, 0, SEEK_SET) != 0)) {
125 return format;
126 }
127
James Zernddeb6ac2014-04-22 19:30:27 -0700128 magic1 = ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
129 magic2 = ((uint32_t)buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
130 if (magic1 == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700131 format = PNG_;
James Zernddeb6ac2014-04-22 19:30:27 -0700132 } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700133 format = JPEG_;
James Zernddeb6ac2014-04-22 19:30:27 -0700134 } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) {
James Zern6f76d242012-07-01 17:55:21 -0700135 format = TIFF_;
James Zernddeb6ac2014-04-22 19:30:27 -0700136 } else if (magic1 == 0x52494646 && magic2 == 0x57454250) {
137 format = WEBP_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800138 }
139 return format;
140}
141
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700142static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800143 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800144 int ok = 0;
145 FILE* in_file = fopen(filename, "rb");
146 if (in_file == NULL) {
147 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
148 return ok;
149 }
150
151 if (pic->width == 0 || pic->height == 0) {
152 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
153 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700154 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800155 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700156 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800157 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700158 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800159 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
James Zernddeb6ac2014-04-22 19:30:27 -0700160 } else if (format == WEBP_) {
161 ok = ReadWebP(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800162 }
163 } else {
164 // If image size is specified, infer it as YUV format.
165 ok = ReadYUV(in_file, pic);
166 }
167 if (!ok) {
168 fprintf(stderr, "Error! Could not process file %s\n", filename);
169 }
170
171 fclose(in_file);
172 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);
215 if (stats->lossless_features) {
216 fprintf(stderr, " * Lossless features used:");
217 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
218 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
219 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
220 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
221 fprintf(stderr, "\n");
222 }
223 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
224 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
225 if (stats->palette_size > 0) {
226 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
227 }
228}
229
Vikas Arorac4ccab62012-05-09 11:27:46 +0530230static void PrintExtraInfoLossless(const WebPPicture* const pic,
231 int short_output,
232 const char* const file_name) {
233 const WebPAuxStats* const stats = pic->stats;
234 if (short_output) {
235 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
236 } else {
237 fprintf(stderr, "File: %s\n", file_name);
238 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
239 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700240 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530241 }
242}
243
244static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
skal9bfbdd12013-03-12 00:37:42 +0100245 int full_details,
Vikas Arorac4ccab62012-05-09 11:27:46 +0530246 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800247 const WebPAuxStats* const stats = pic->stats;
248 if (short_output) {
249 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700250 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800251 const int num_i4 = stats->block_count[0];
252 const int num_i16 = stats->block_count[1];
253 const int num_skip = stats->block_count[2];
254 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800255 fprintf(stderr, "File: %s\n", file_name);
256 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700257 pic->width, pic->height,
258 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800259 fprintf(stderr, "Output: "
260 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800261 stats->coded_size,
262 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
263 if (total > 0) {
264 int totals[4] = { 0, 0, 0, 0 };
265 fprintf(stderr, "block count: intra4: %d\n"
266 " intra16: %d (-> %.2f%%)\n",
267 num_i4, num_i16, 100.f * num_i16 / total);
268 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
269 num_skip, 100.f * num_skip / total);
270 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
271 " mode-partition: %6d (%.1f%%)\n",
272 stats->header_bytes[0],
273 100.f * stats->header_bytes[0] / stats->coded_size,
274 stats->header_bytes[1],
275 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700276 if (stats->alpha_data_size > 0) {
277 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
278 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700279 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800280 fprintf(stderr, " Residuals bytes "
281 "|segment 1|segment 2|segment 3"
282 "|segment 4| total\n");
skal9bfbdd12013-03-12 00:37:42 +0100283 if (full_details) {
284 fprintf(stderr, " intra4-coeffs: ");
285 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
286 fprintf(stderr, " intra16-coeffs: ");
287 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
288 fprintf(stderr, " chroma coeffs: ");
289 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
290 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800291 fprintf(stderr, " macroblocks: ");
292 PrintPercents(stats->segment_size, total);
293 fprintf(stderr, " quantizer: ");
294 PrintValues(stats->segment_quant);
295 fprintf(stderr, " filter level: ");
296 PrintValues(stats->segment_level);
skal9bfbdd12013-03-12 00:37:42 +0100297 if (full_details) {
298 fprintf(stderr, "------------------+---------");
299 fprintf(stderr, "+---------+---------+---------+-----------------\n");
300 fprintf(stderr, " segments total: ");
301 PrintByteCount(totals, stats->coded_size, NULL);
302 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800303 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700304 if (stats->lossless_size > 0) {
305 PrintFullLosslessInfo(stats, "alpha");
306 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800307 }
skalfff2a112013-12-23 12:03:00 +0100308}
309
310static void PrintMapInfo(const WebPPicture* const pic) {
Pascal Massimino7d853d72012-07-24 16:15:36 -0700311 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800312 const int mb_w = (pic->width + 15) / 16;
313 const int mb_h = (pic->height + 15) / 16;
314 const int type = pic->extra_info_type;
315 int x, y;
316 for (y = 0; y < mb_h; ++y) {
317 for (x = 0; x < mb_w; ++x) {
318 const int c = pic->extra_info[x + y * mb_w];
319 if (type == 1) { // intra4/intra16
skale12f8742014-03-12 19:48:00 +0100320 fprintf(stderr, "%c", "+."[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800321 } else if (type == 2) { // segments
skale12f8742014-03-12 19:48:00 +0100322 fprintf(stderr, "%c", ".-*X"[c]);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800323 } else if (type == 3) { // quantizers
skale12f8742014-03-12 19:48:00 +0100324 fprintf(stderr, "%.2d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800325 } else if (type == 6 || type == 7) {
skale12f8742014-03-12 19:48:00 +0100326 fprintf(stderr, "%3d ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800327 } else {
skale12f8742014-03-12 19:48:00 +0100328 fprintf(stderr, "0x%.2x ", c);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800329 }
330 }
skale12f8742014-03-12 19:48:00 +0100331 fprintf(stderr, "\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800332 }
333 }
334}
335
James Zernc7e86ab2011-08-25 14:22:32 -0700336//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800337
338static int MyWriter(const uint8_t* data, size_t data_size,
339 const WebPPicture* const pic) {
340 FILE* const out = (FILE*)pic->custom_ptr;
341 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
342}
343
344// Dumps a picture as a PGM file using the IMC4 layout.
345static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
346 int y;
347 const int uv_width = (picture->width + 1) / 2;
348 const int uv_height = (picture->height + 1) / 2;
349 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700350 const int alpha_height =
351 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700352 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800353 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800354 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800355 fprintf(f, "P5\n%d %d\n255\n", stride, height);
356 for (y = 0; y < picture->height; ++y) {
357 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
358 return 0;
359 if (picture->width & 1) fputc(0, f); // pad
360 }
361 for (y = 0; y < uv_height; ++y) {
362 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
363 return 0;
364 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
365 return 0;
366 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700367 for (y = 0; y < alpha_height; ++y) {
368 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
369 return 0;
370 if (picture->width & 1) fputc(0, f); // pad
371 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800372 fclose(f);
373 return 1;
374}
375
James Zern7eaee9f2013-01-11 12:25:36 -0800376// -----------------------------------------------------------------------------
377// Metadata writing.
378
379enum {
380 METADATA_EXIF = (1 << 0),
James Zernd8dc72a2013-03-13 14:04:20 -0700381 METADATA_ICC = (1 << 1),
James Zern7eaee9f2013-01-11 12:25:36 -0800382 METADATA_XMP = (1 << 2),
James Zernd8dc72a2013-03-13 14:04:20 -0700383 METADATA_ALL = METADATA_EXIF | METADATA_ICC | METADATA_XMP
James Zern7eaee9f2013-01-11 12:25:36 -0800384};
385
James Zern76ec5fa2013-01-14 18:32:44 -0800386static const int kChunkHeaderSize = 8;
387static const int kTagSize = 4;
388
James Zern0bc42682013-03-13 14:41:38 -0700389static void PrintMetadataInfo(const Metadata* const metadata,
390 int metadata_written) {
391 if (metadata == NULL || metadata_written == 0) return;
392
393 fprintf(stderr, "Metadata:\n");
James Zern7dd288f2013-03-15 16:42:58 -0700394 if (metadata_written & METADATA_ICC) {
James Zern14d42af2013-03-20 16:59:35 -0700395 fprintf(stderr, " * ICC profile: %6d bytes\n", (int)metadata->iccp.size);
James Zern0bc42682013-03-13 14:41:38 -0700396 }
397 if (metadata_written & METADATA_EXIF) {
James Zern14d42af2013-03-20 16:59:35 -0700398 fprintf(stderr, " * EXIF data: %6d bytes\n", (int)metadata->exif.size);
James Zern0bc42682013-03-13 14:41:38 -0700399 }
400 if (metadata_written & METADATA_XMP) {
James Zern14d42af2013-03-20 16:59:35 -0700401 fprintf(stderr, " * XMP data: %6d bytes\n", (int)metadata->xmp.size);
James Zern0bc42682013-03-13 14:41:38 -0700402 }
403}
404
James Zern76ec5fa2013-01-14 18:32:44 -0800405// Outputs, in little endian, 'num' bytes from 'val' to 'out'.
406static int WriteLE(FILE* const out, uint32_t val, int num) {
407 uint8_t buf[4];
408 int i;
409 for (i = 0; i < num; ++i) {
410 buf[i] = (uint8_t)(val & 0xff);
411 val >>= 8;
412 }
413 return (fwrite(buf, num, 1, out) == 1);
414}
415
416static int WriteLE24(FILE* const out, uint32_t val) {
417 return WriteLE(out, val, 3);
418}
419
420static int WriteLE32(FILE* const out, uint32_t val) {
421 return WriteLE(out, val, 4);
422}
423
424static int WriteMetadataChunk(FILE* const out, const char fourcc[4],
425 const MetadataPayload* const payload) {
426 const uint8_t zero = 0;
427 const size_t need_padding = payload->size & 1;
428 int ok = (fwrite(fourcc, kTagSize, 1, out) == 1);
429 ok = ok && WriteLE32(out, (uint32_t)payload->size);
430 ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1);
431 return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding);
432}
433
434// Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the
435// chunk if there is metadata and 'keep' is true.
436static int UpdateFlagsAndSize(const MetadataPayload* const payload,
437 int keep, int flag,
438 uint32_t* vp8x_flags, uint64_t* metadata_size) {
439 if (keep && payload->bytes != NULL && payload->size > 0) {
440 *vp8x_flags |= flag;
441 *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1);
442 return 1;
443 }
444 return 0;
445}
446
447// Writes a WebP file using the image contained in 'memory_writer' and the
448// metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the
449// availability in 'metadata'. Returns true on success.
450// For details see doc/webp-container-spec.txt#extended-file-format.
451static int WriteWebPWithMetadata(FILE* const out,
452 const WebPPicture* const picture,
453 const WebPMemoryWriter* const memory_writer,
454 const Metadata* const metadata,
James Zern0bc42682013-03-13 14:41:38 -0700455 int keep_metadata,
456 int* const metadata_written) {
James Zern76ec5fa2013-01-14 18:32:44 -0800457 const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00";
458 const int kAlphaFlag = 0x10;
459 const int kEXIFFlag = 0x08;
460 const int kICCPFlag = 0x20;
461 const int kXMPFlag = 0x04;
462 const size_t kRiffHeaderSize = 12;
463 const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1;
464 const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize;
465 uint32_t flags = 0;
466 uint64_t metadata_size = 0;
467 const int write_exif = UpdateFlagsAndSize(&metadata->exif,
468 !!(keep_metadata & METADATA_EXIF),
469 kEXIFFlag, &flags, &metadata_size);
470 const int write_iccp = UpdateFlagsAndSize(&metadata->iccp,
James Zernd8dc72a2013-03-13 14:04:20 -0700471 !!(keep_metadata & METADATA_ICC),
James Zern76ec5fa2013-01-14 18:32:44 -0800472 kICCPFlag, &flags, &metadata_size);
473 const int write_xmp = UpdateFlagsAndSize(&metadata->xmp,
474 !!(keep_metadata & METADATA_XMP),
475 kXMPFlag, &flags, &metadata_size);
476 uint8_t* webp = memory_writer->mem;
477 size_t webp_size = memory_writer->size;
James Zern0bc42682013-03-13 14:41:38 -0700478
479 *metadata_written = 0;
480
James Zern76ec5fa2013-01-14 18:32:44 -0800481 if (webp_size < kMinSize) return 0;
482 if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) {
483 fprintf(stderr, "Error! Addition of metadata would exceed "
484 "container size limit.\n");
485 return 0;
486 }
487
488 if (metadata_size > 0) {
489 const int kVP8XChunkSize = 18;
490 const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize);
491 const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize +
492 (has_vp8x ? 0 : kVP8XChunkSize) +
493 metadata_size);
494 // RIFF
495 int ok = (fwrite(webp, kTagSize, 1, out) == 1);
496 // RIFF size (file header size is not recorded)
497 ok = ok && WriteLE32(out, riff_size);
498 webp += kChunkHeaderSize;
499 webp_size -= kChunkHeaderSize;
500 // WEBP
501 ok = ok && (fwrite(webp, kTagSize, 1, out) == 1);
502 webp += kTagSize;
503 webp_size -= kTagSize;
504 if (has_vp8x) { // update the existing VP8X flags
505 webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff);
506 ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1);
James Zernb5b2e3c2013-12-19 10:17:08 -0800507 webp += kVP8XChunkSize;
James Zern76ec5fa2013-01-14 18:32:44 -0800508 webp_size -= kVP8XChunkSize;
509 } else {
510 const int is_lossless = !memcmp(webp, "VP8L", kTagSize);
Urvang Joshi8ba1bf62013-07-19 11:55:09 -0700511 if (is_lossless) {
512 // Presence of alpha is stored in the 29th bit of VP8L data.
513 if (webp[kChunkHeaderSize + 3] & (1 << 5)) flags |= kAlphaFlag;
514 }
James Zern76ec5fa2013-01-14 18:32:44 -0800515 ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1);
516 ok = ok && WriteLE32(out, flags);
517 ok = ok && WriteLE24(out, picture->width - 1);
518 ok = ok && WriteLE24(out, picture->height - 1);
519 }
James Zern0bc42682013-03-13 14:41:38 -0700520 if (write_iccp) {
521 ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp);
James Zern7dd288f2013-03-15 16:42:58 -0700522 *metadata_written |= METADATA_ICC;
James Zern0bc42682013-03-13 14:41:38 -0700523 }
James Zern76ec5fa2013-01-14 18:32:44 -0800524 // Image
525 ok = ok && (fwrite(webp, webp_size, 1, out) == 1);
James Zern0bc42682013-03-13 14:41:38 -0700526 if (write_exif) {
527 ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif);
528 *metadata_written |= METADATA_EXIF;
529 }
530 if (write_xmp) {
531 ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp);
532 *metadata_written |= METADATA_XMP;
533 }
James Zern76ec5fa2013-01-14 18:32:44 -0800534 return ok;
535 } else {
536 // No metadata, just write the original image file.
537 return (fwrite(webp, webp_size, 1, out) == 1);
538 }
539}
540
James Zernc7e86ab2011-08-25 14:22:32 -0700541//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800542
Pascal Massimino30971c92011-12-01 02:24:50 -0800543static int ProgressReport(int percent, const WebPPicture* const picture) {
skale12f8742014-03-12 19:48:00 +0100544 fprintf(stderr, "[%s]: %3d %% \r",
545 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800546 return 1; // all ok
547}
548
549//------------------------------------------------------------------------------
550
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700551static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800552 printf("Usage:\n\n");
553 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
554 printf("where quality is between 0 (poor) to 100 (very good).\n");
555 printf("Typical value is around 80.\n\n");
556 printf("Try -longhelp for an exhaustive list of advanced options.\n");
557}
558
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700559static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800560 printf("Usage:\n");
561 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
skal0a8b8862014-06-18 16:45:34 +0200562 printf("If input size (-s) for an image is not specified, it is\n"
563 "assumed to be a PNG, JPEG, TIFF or WebP file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700564#ifdef HAVE_WINCODEC_H
skal0a8b8862014-06-18 16:45:34 +0200565 printf("Windows builds can take as input any of the files handled by WIC.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800566#endif
skal0a8b8862014-06-18 16:45:34 +0200567 printf("\nOptions:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800568 printf(" -h / -help ............ short help\n");
569 printf(" -H / -longhelp ........ long help\n");
570 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
skal0a8b8862014-06-18 16:45:34 +0200571 printf(" -alpha_q <int> ......... transparency-compression quality "
572 "(0..100)\n");
573 printf(" -preset <string> ....... preset setting, one of:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800574 printf(" default, photo, picture,\n");
575 printf(" drawing, icon, text\n");
skal0a8b8862014-06-18 16:45:34 +0200576 printf(" -preset must come first, as it overwrites other parameters\n");
James Zernc2fc52e2014-07-22 20:24:59 -0700577#if WEBP_ENCODER_ABI_VERSION > 0x0202
skal0a8b8862014-06-18 16:45:34 +0200578 printf(" -z <int> ............... activates lossless preset with given\n"
skal65b99f12014-03-11 23:25:35 +0100579 " level in [0:fast, ..., 9:slowest]\n");
James Zernc2fc52e2014-07-22 20:24:59 -0700580#endif
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");
611 printf(" -alpha_cleanup ......... clean RGB values in transparent area\n");
612 printf(" -blend_alpha <hex> ..... blend colors against background color\n"
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700613 " expressed as RGB values written in\n"
614 " hexadecimal, e.g. 0xc0e0d0 for red=0xc0\n"
skal0a8b8862014-06-18 16:45:34 +0200615 " green=0xe0 and blue=0xd0\n");
616 printf(" -noalpha ............... discard any transparency information\n");
617 printf(" -lossless .............. encode image losslessly\n");
618 printf(" -hint <string> ......... specify image characteristics hint,\n");
619 printf(" one of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700620
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800621 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800622 printf(" -metadata <string> ..... comma separated list of metadata to\n");
623 printf(" ");
624 printf("copy from the input to the output if present.\n");
625 printf(" "
James Zernd8dc72a2013-03-13 14:04:20 -0700626 "Valid values: all, none (default), exif, icc, xmp\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800627
628 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800629 printf(" -short ................. condense printed message\n");
skal0a8b8862014-06-18 16:45:34 +0200630 printf(" -quiet ................. don't print anything\n");
631 printf(" -version ............... print version number and exit\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700632#ifndef WEBP_DLL
skal0a8b8862014-06-18 16:45:34 +0200633 printf(" -noasm ................. disable all assembly optimizations\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700634#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800635 printf(" -v ..................... verbose, e.g. print encoding/decoding "
636 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800637 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800638 printf("\n");
639 printf("Experimental Options:\n");
skal0a8b8862014-06-18 16:45:34 +0200640 printf(" -jpeg_like ............. roughly match expected JPEG size\n");
641 printf(" -af .................... auto-adjust filter strength\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800642 printf(" -pre <int> ............. pre-processing filter\n");
643 printf("\n");
644}
645
James Zernc7e86ab2011-08-25 14:22:32 -0700646//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700647// Error messages
648
skal0b747b12014-07-21 15:44:43 +0200649static const char* const kErrorMessages[VP8_ENC_ERROR_LAST] = {
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700650 "OK",
651 "OUT_OF_MEMORY: Out of memory allocating objects",
652 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
653 "NULL_PARAMETER: NULL parameter passed to function",
654 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700655 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
656 "allowed is 16383 pixels.",
657 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
658 "To reduce the size of this partition, try using less segments "
659 "with the -segments option, and eventually reduce the number of "
660 "header bits using -partition_limit. More details are available "
661 "in the manual (`man cwebp`)",
662 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800663 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800664 "FILE_TOO_BIG: File would be too big to fit in 4G",
665 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700666};
667
James Zernc7e86ab2011-08-25 14:22:32 -0700668//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800669
670int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700671 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800672 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
673 FILE *out = NULL;
674 int c;
675 int short_output = 0;
676 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530677 int keep_alpha = 1;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700678 int blend_alpha = 0;
679 uint32_t background_color = 0xffffffu;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800680 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700681 int resize_w = 0, resize_h = 0;
James Zernc2fc52e2014-07-22 20:24:59 -0700682#if WEBP_ENCODER_ABI_VERSION > 0x0202
skal65b99f12014-03-11 23:25:35 +0100683 int lossless_preset = 6;
684 int use_lossless_preset = -1; // -1=unset, 0=don't use, 1=use it
James Zernc2fc52e2014-07-22 20:24:59 -0700685#endif
Pascal Massimino30971c92011-12-01 02:24:50 -0800686 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800687 int keep_metadata = 0;
James Zern0bc42682013-03-13 14:41:38 -0700688 int metadata_written = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800689 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700690 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800691 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800692 WebPConfig config;
693 WebPAuxStats stats;
James Zern76ec5fa2013-01-14 18:32:44 -0800694 WebPMemoryWriter memory_writer;
James Zern63aba3a2012-12-03 18:20:00 -0800695 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800696 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700697
James Zern63aba3a2012-12-03 18:20:00 -0800698 MetadataInit(&metadata);
James Zern88d382a2013-02-01 19:17:26 -0800699 WebPMemoryWriterInit(&memory_writer);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800700 if (!WebPPictureInit(&picture) ||
701 !WebPPictureInit(&original_picture) ||
702 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800703 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700704 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800705 }
706
707 if (argc == 1) {
708 HelpShort();
709 return 0;
710 }
711
712 for (c = 1; c < argc; ++c) {
James Zern40b3a612014-09-10 23:35:48 -0700713 int parse_error = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800714 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
715 HelpShort();
716 return 0;
717 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
718 HelpLong();
719 return 0;
720 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
721 out_file = argv[++c];
722 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
723 dump_file = argv[++c];
724 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800725 } else if (!strcmp(argv[c], "-print_psnr")) {
726 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700727 print_distortion = 0;
728 } else if (!strcmp(argv[c], "-print_ssim")) {
729 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800730 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700731 } else if (!strcmp(argv[c], "-print_lsim")) {
732 config.show_compressed = 1;
733 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800734 } else if (!strcmp(argv[c], "-short")) {
skalfff2a112013-12-23 12:03:00 +0100735 ++short_output;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800736 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
James Zern40b3a612014-09-10 23:35:48 -0700737 picture.width = ExUtilGetInt(argv[++c], 0, &parse_error);
738 picture.height = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800739 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700740 config.method = ExUtilGetInt(argv[++c], 0, &parse_error);
James Zernc2fc52e2014-07-22 20:24:59 -0700741#if WEBP_ENCODER_ABI_VERSION > 0x0202
skal65b99f12014-03-11 23:25:35 +0100742 use_lossless_preset = 0; // disable -z option
James Zernc2fc52e2014-07-22 20:24:59 -0700743#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800744 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700745 config.quality = ExUtilGetFloat(argv[++c], &parse_error);
James Zernc2fc52e2014-07-22 20:24:59 -0700746#if WEBP_ENCODER_ABI_VERSION > 0x0202
skal65b99f12014-03-11 23:25:35 +0100747 use_lossless_preset = 0; // disable -z option
748 } else if (!strcmp(argv[c], "-z") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700749 lossless_preset = ExUtilGetInt(argv[++c], 0, &parse_error);
skal65b99f12014-03-11 23:25:35 +0100750 if (use_lossless_preset != 0) use_lossless_preset = 1;
James Zernc2fc52e2014-07-22 20:24:59 -0700751#endif
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530752 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700753 config.alpha_quality = ExUtilGetInt(argv[++c], 0, &parse_error);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530754 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700755 config.alpha_compression = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000756 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
757 keep_alpha = keep_alpha ? 2 : 0;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700758 } else if (!strcmp(argv[c], "-blend_alpha") && c < argc - 1) {
759 blend_alpha = 1;
James Zern40b3a612014-09-10 23:35:48 -0700760 // background color is given in hex with an optional '0x' prefix
761 background_color = ExUtilGetInt(argv[++c], 16, &parse_error);
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700762 background_color = background_color & 0x00ffffffu;
Vikas Arora252028a2012-01-05 13:04:30 +0530763 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800764 ++c;
765 if (!strcmp(argv[c], "none")) {
766 config.alpha_filtering = 0;
767 } else if (!strcmp(argv[c], "fast")) {
768 config.alpha_filtering = 1;
769 } else if (!strcmp(argv[c], "best")) {
770 config.alpha_filtering = 2;
771 } else {
772 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
773 goto Error;
774 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530775 } else if (!strcmp(argv[c], "-noalpha")) {
776 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000777 } else if (!strcmp(argv[c], "-lossless")) {
778 config.lossless = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530779 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
780 ++c;
781 if (!strcmp(argv[c], "photo")) {
782 config.image_hint = WEBP_HINT_PHOTO;
783 } else if (!strcmp(argv[c], "picture")) {
784 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700785 } else if (!strcmp(argv[c], "graph")) {
786 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530787 } else {
788 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
789 goto Error;
790 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700792 config.target_size = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800793 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700794 config.target_PSNR = ExUtilGetFloat(argv[++c], &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800795 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700796 config.sns_strength = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800797 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700798 config.filter_strength = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800799 } else if (!strcmp(argv[c], "-af")) {
800 config.autofilter = 1;
skale8950592013-02-05 19:40:18 +0100801 } else if (!strcmp(argv[c], "-jpeg_like")) {
802 config.emulate_jpeg_size = 1;
skalf8179302013-03-01 01:21:34 +0100803 } else if (!strcmp(argv[c], "-mt")) {
804 ++config.thread_level; // increase thread level
skal9bfbdd12013-03-12 00:37:42 +0100805 } else if (!strcmp(argv[c], "-low_memory")) {
806 config.low_memory = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700807 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800808 config.filter_type = 1;
Pascal Massimino92668da2013-02-15 01:08:52 -0800809 } else if (!strcmp(argv[c], "-nostrong")) {
810 config.filter_type = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800811 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700812 config.filter_sharpness = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800813 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700814 config.pass = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800815 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700816 config.preprocessing = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800817 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700818 config.segments = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino900286e2011-08-23 15:58:22 -0700819 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700820 config.partition_limit = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800821 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
James Zern40b3a612014-09-10 23:35:48 -0700822 picture.extra_info_type = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800823 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
824 crop = 1;
James Zern40b3a612014-09-10 23:35:48 -0700825 crop_x = ExUtilGetInt(argv[++c], 0, &parse_error);
826 crop_y = ExUtilGetInt(argv[++c], 0, &parse_error);
827 crop_w = ExUtilGetInt(argv[++c], 0, &parse_error);
828 crop_h = ExUtilGetInt(argv[++c], 0, &parse_error);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700829 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
James Zern40b3a612014-09-10 23:35:48 -0700830 resize_w = ExUtilGetInt(argv[++c], 0, &parse_error);
831 resize_h = ExUtilGetInt(argv[++c], 0, &parse_error);
James Zernb4d0ef82011-07-15 14:53:03 -0700832#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700833 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000834 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700835#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700836 } else if (!strcmp(argv[c], "-version")) {
837 const int version = WebPGetEncoderVersion();
838 printf("%d.%d.%d\n",
839 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
840 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800841 } else if (!strcmp(argv[c], "-progress")) {
842 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800843 } else if (!strcmp(argv[c], "-quiet")) {
844 quiet = 1;
845 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
846 WebPPreset preset;
847 ++c;
848 if (!strcmp(argv[c], "default")) {
849 preset = WEBP_PRESET_DEFAULT;
850 } else if (!strcmp(argv[c], "photo")) {
851 preset = WEBP_PRESET_PHOTO;
852 } else if (!strcmp(argv[c], "picture")) {
853 preset = WEBP_PRESET_PICTURE;
854 } else if (!strcmp(argv[c], "drawing")) {
855 preset = WEBP_PRESET_DRAWING;
856 } else if (!strcmp(argv[c], "icon")) {
857 preset = WEBP_PRESET_ICON;
858 } else if (!strcmp(argv[c], "text")) {
859 preset = WEBP_PRESET_TEXT;
860 } else {
861 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
862 goto Error;
863 }
864 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700865 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800866 goto Error;
867 }
James Zern7eaee9f2013-01-11 12:25:36 -0800868 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
869 static const struct {
870 const char* option;
871 int flag;
872 } kTokens[] = {
873 { "all", METADATA_ALL },
874 { "none", 0 },
875 { "exif", METADATA_EXIF },
James Zernd8dc72a2013-03-13 14:04:20 -0700876 { "icc", METADATA_ICC },
James Zern7eaee9f2013-01-11 12:25:36 -0800877 { "xmp", METADATA_XMP },
878 };
879 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
880 const char* start = argv[++c];
881 const char* const end = start + strlen(start);
882
883 while (start < end) {
884 size_t i;
885 const char* token = strchr(start, ',');
886 if (token == NULL) token = end;
887
888 for (i = 0; i < kNumTokens; ++i) {
889 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
890 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
891 if (kTokens[i].flag != 0) {
892 keep_metadata |= kTokens[i].flag;
893 } else {
894 keep_metadata = 0;
895 }
896 break;
897 }
898 }
899 if (i == kNumTokens) {
900 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
901 (int)(token - start), start);
902 HelpLong();
903 return -1;
904 }
905 start = token + 1;
906 }
James Zern76ec5fa2013-01-14 18:32:44 -0800907#ifdef HAVE_WINCODEC_H
James Zernd8dc72a2013-03-13 14:04:20 -0700908 if (keep_metadata != 0 && keep_metadata != METADATA_ICC) {
James Zern7eaee9f2013-01-11 12:25:36 -0800909 // TODO(jzern): remove when -metadata is supported on all platforms.
James Zerne83ff7d2013-01-24 15:37:49 -0800910 fprintf(stderr, "Warning: only ICC profile extraction is currently"
911 " supported on this platform!\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800912 }
James Zern76ec5fa2013-01-14 18:32:44 -0800913#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800914 } else if (!strcmp(argv[c], "-v")) {
915 verbose = 1;
James Zern98af68f2013-12-12 20:20:08 -0800916 } else if (!strcmp(argv[c], "--")) {
917 if (c < argc - 1) in_file = argv[++c];
918 break;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800919 } else if (argv[c][0] == '-') {
920 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
921 HelpLong();
922 return -1;
923 } else {
924 in_file = argv[c];
925 }
James Zern40b3a612014-09-10 23:35:48 -0700926
927 if (parse_error) {
928 HelpLong();
929 return -1;
930 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800931 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700932 if (in_file == NULL) {
933 fprintf(stderr, "No input file specified!\n");
934 HelpShort();
935 goto Error;
936 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800937
James Zernc2fc52e2014-07-22 20:24:59 -0700938#if WEBP_ENCODER_ABI_VERSION > 0x0202
skal65b99f12014-03-11 23:25:35 +0100939 if (use_lossless_preset == 1) {
940 if (!WebPConfigLosslessPreset(&config, lossless_preset)) {
941 fprintf(stderr, "Invalid lossless preset (-z %d)\n", lossless_preset);
942 goto Error;
943 }
944 }
James Zernc2fc52e2014-07-22 20:24:59 -0700945#endif
skal65b99f12014-03-11 23:25:35 +0100946
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530947 // Check for unsupported command line options for lossless mode and log
948 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000949 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530950 if (config.target_size > 0 || config.target_PSNR > 0) {
951 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
952 " for lossless encoding. Ignoring such option(s)!\n");
953 }
954 if (config.partition_limit > 0) {
955 fprintf(stderr, "Partition limit option is not required for lossless"
956 " encoding. Ignoring this option!\n");
957 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530958 }
959
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800960 if (!WebPValidateConfig(&config)) {
961 fprintf(stderr, "Error! Invalid configuration.\n");
962 goto Error;
963 }
964
Pascal Massimino0744e842011-02-25 12:03:27 -0800965 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700966 if (verbose) {
skald51f45f2013-09-12 09:32:28 +0200967 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700968 }
James Zern7eaee9f2013-01-11 12:25:36 -0800969 if (!ReadPicture(in_file, &picture, keep_alpha,
970 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800971 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700972 goto Error;
973 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800974 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
Pascal Massiminoe7d95482013-04-02 19:14:14 -0700975
976 if (blend_alpha) {
977 WebPBlendAlpha(&picture, background_color);
978 }
979
James Zern7c732e52013-02-01 20:04:20 -0800980 if (keep_alpha == 2) {
981 WebPCleanupTransparentArea(&picture);
982 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800983
Pascal Massimino0744e842011-02-25 12:03:27 -0800984 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -0800985 const double read_time = StopwatchReadAndReset(&stop_watch);
986 fprintf(stderr, "Time to read input: %.3fs\n", read_time);
Pascal Massimino0744e842011-02-25 12:03:27 -0800987 }
988
989 // Open the output
skale12f8742014-03-12 19:48:00 +0100990 if (out_file != NULL) {
991 const int use_stdout = !strcmp(out_file, "-");
James Zernd40e8852014-08-29 19:11:41 -0700992 out = use_stdout ? ExUtilSetBinaryMode(stdout) : fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800993 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800994 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
995 goto Error;
996 } else {
997 if (!short_output && !quiet) {
998 fprintf(stderr, "Saving file '%s'\n", out_file);
999 }
1000 }
James Zern76ec5fa2013-01-14 18:32:44 -08001001 if (keep_metadata == 0) {
1002 picture.writer = MyWriter;
1003 picture.custom_ptr = (void*)out;
1004 } else {
James Zern76ec5fa2013-01-14 18:32:44 -08001005 picture.writer = WebPMemoryWrite;
1006 picture.custom_ptr = (void*)&memory_writer;
1007 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001008 } else {
1009 out = NULL;
1010 if (!quiet && !short_output) {
1011 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
1012 fprintf(stderr, "be performed, but its results discarded.\n\n");
1013 }
1014 }
Pascal Massimino7d853d72012-07-24 16:15:36 -07001015 if (!quiet) {
1016 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -07001017 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -07001018 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001019
Pascal Massimino0744e842011-02-25 12:03:27 -08001020 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -07001021 if (verbose) {
skald51f45f2013-09-12 09:32:28 +02001022 StopwatchReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -07001023 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -07001024 if (crop != 0) {
1025 // We use self-cropping using a view.
1026 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
1027 fprintf(stderr, "Error! Cannot crop picture\n");
1028 goto Error;
1029 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001030 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -07001031 if ((resize_w | resize_h) > 0) {
1032 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
1033 fprintf(stderr, "Error! Cannot resize picture\n");
1034 goto Error;
1035 }
1036 }
1037 if (picture.extra_info_type > 0) {
1038 AllocExtraInfo(&picture);
1039 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -07001040 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -08001041 WebPPictureCopy(&picture, &original_picture);
1042 }
Pascal Massimino6d978a62011-03-17 14:49:19 -07001043 if (!WebPEncode(&config, &picture)) {
1044 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -07001045 fprintf(stderr, "Error code: %d (%s)\n",
1046 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -07001047 goto Error;
1048 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001049 if (verbose) {
Pascal Massimino126c0352013-01-25 00:44:16 -08001050 const double encode_time = StopwatchReadAndReset(&stop_watch);
1051 fprintf(stderr, "Time to encode picture: %.3fs\n", encode_time);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001052 }
Pascal Massimino0744e842011-02-25 12:03:27 -08001053
1054 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -07001055 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +00001056 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -07001057 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
1058 } else if (!DumpPicture(&picture, dump_file)) {
1059 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
1060 }
Pascal Massimino38369c02011-05-04 22:59:51 -07001061 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001062
James Zern70490432013-12-09 20:15:54 -08001063 if (keep_metadata != 0) {
1064 if (out != NULL) {
1065 if (!WriteWebPWithMetadata(out, &picture, &memory_writer,
1066 &metadata, keep_metadata, &metadata_written)) {
1067 fprintf(stderr, "Error writing WebP file with metadata!\n");
1068 goto Error;
1069 }
1070 } else { // output is disabled, just display the metadata stats.
1071 const struct {
1072 const MetadataPayload* const payload;
1073 int flag;
1074 } *iter, info[] = {
1075 { &metadata.exif, METADATA_EXIF },
1076 { &metadata.iccp, METADATA_ICC },
1077 { &metadata.xmp, METADATA_XMP },
1078 { NULL, 0 }
1079 };
1080 uint32_t unused1 = 0;
1081 uint64_t unused2 = 0;
1082
1083 for (iter = info; iter->payload != NULL; ++iter) {
1084 if (UpdateFlagsAndSize(iter->payload, !!(keep_metadata & iter->flag),
1085 0, &unused1, &unused2)) {
1086 metadata_written |= iter->flag;
1087 }
1088 }
James Zern76ec5fa2013-01-14 18:32:44 -08001089 }
1090 }
1091
Pascal Massimino38369c02011-05-04 22:59:51 -07001092 if (!quiet) {
skalfff2a112013-12-23 12:03:00 +01001093 if (!short_output || print_distortion < 0) {
1094 if (config.lossless) {
1095 PrintExtraInfoLossless(&picture, short_output, in_file);
1096 } else {
1097 PrintExtraInfoLossy(&picture, short_output, config.low_memory, in_file);
1098 }
1099 }
1100 if (!short_output && picture.extra_info_type > 0) {
1101 PrintMapInfo(&picture);
1102 }
1103 if (print_distortion >= 0) { // print distortion
1104 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
1105 float values[5];
1106 // Comparison is performed in YUVA colorspace.
1107 if (original_picture.use_argb &&
1108 !WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A)) {
1109 fprintf(stderr, "Error while converting original picture to YUVA.\n");
1110 goto Error;
1111 }
1112 if (picture.use_argb &&
1113 !WebPPictureARGBToYUVA(&picture, WEBP_YUV420A)) {
1114 fprintf(stderr, "Error while converting compressed picture to YUVA.\n");
1115 goto Error;
1116 }
1117 if (!WebPPictureDistortion(&picture, &original_picture,
1118 print_distortion, values)) {
1119 fprintf(stderr, "Error while computing the distortion.\n");
1120 goto Error;
1121 }
1122 if (!short_output) {
1123 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
1124 distortion_names[print_distortion],
1125 values[0], values[1], values[2], values[3], values[4]);
1126 } else {
1127 fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
1128 }
Vikas Arorac4ccab62012-05-09 11:27:46 +05301129 }
James Zern0bc42682013-03-13 14:41:38 -07001130 if (!short_output) {
1131 PrintMetadataInfo(&metadata, metadata_written);
1132 }
Pascal Massiminod61479f2012-01-20 07:20:56 -08001133 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001134 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001135
1136 Error:
James Zern6c831572014-10-13 13:46:13 +02001137#if WEBP_ENCODER_ABI_VERSION > 0x0203
skalaf93bdd2014-03-27 23:27:32 +01001138 WebPMemoryWriterClear(&memory_writer);
James Zernc2fc52e2014-07-22 20:24:59 -07001139#else
1140 free(memory_writer.mem);
1141#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001142 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -08001143 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001144 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -08001145 WebPPictureFree(&original_picture);
skale12f8742014-03-12 19:48:00 +01001146 if (out != NULL && out != stdout) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001147 fclose(out);
1148 }
1149
Pascal Massimino4abe04a2012-05-09 00:32:20 -07001150 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001151}
1152
James Zernc7e86ab2011-08-25 14:22:32 -07001153//------------------------------------------------------------------------------