blob: 4902ae621ca4a2ea92e1db4bcc492067636cc2c1 [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massiminof61d14a2011-02-18 23:33:46 -08002//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// simple command line calling the WebPEncode function.
9// Encodes a raw .YUV into WebP bitstream
10//
11// Author: Skal (pascal.massimino@gmail.com)
12
13#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070014#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080015#include <string.h>
16
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#include "webp/encode.h"
skal1bd287a2012-12-11 11:02:39 +010022
James Zern63aba3a2012-12-03 18:20:00 -080023#include "./metadata.h"
James Zern00b29e22012-05-15 13:48:11 -070024#include "./stopwatch.h"
skal1bd287a2012-12-11 11:02:39 +010025
26#include "./jpegdec.h"
27#include "./pngdec.h"
James Zern1c1c5642012-12-06 14:04:36 -080028#include "./tiffdec.h"
James Zerna452a552013-01-22 18:28:52 -080029#include "./wicdec.h"
skal1bd287a2012-12-11 11:02:39 +010030
James Zernb4d0ef82011-07-15 14:53:03 -070031#ifndef WEBP_DLL
James Zern04e84cf2011-11-04 15:20:08 -070032#if defined(__cplusplus) || defined(c_plusplus)
33extern "C" {
James Zernb4d0ef82011-07-15 14:53:03 -070034#endif
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070035
James Zern04e84cf2011-11-04 15:20:08 -070036extern void* VP8GetCPUInfo; // opaque forward declaration.
37
38#if defined(__cplusplus) || defined(c_plusplus)
39} // extern "C"
40#endif
41#endif // WEBP_DLL
42
James Zernc7e86ab2011-08-25 14:22:32 -070043//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -080044
45static int verbose = 0;
46
47static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
Pascal Massiminodd108172012-07-18 21:58:53 +000048 const int use_argb = pic->use_argb;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080049 const int uv_width = (pic->width + 1) / 2;
50 const int uv_height = (pic->height + 1) / 2;
51 int y;
52 int ok = 0;
53
Pascal Massiminodd108172012-07-18 21:58:53 +000054 pic->use_argb = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -080055 if (!WebPPictureAlloc(pic)) return ok;
56
57 for (y = 0; y < pic->height; ++y) {
58 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
59 goto End;
60 }
61 }
62 for (y = 0; y < uv_height; ++y) {
63 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
64 goto End;
65 }
66 for (y = 0; y < uv_height; ++y) {
67 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
68 goto End;
69 }
70 ok = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +000071 if (use_argb) ok = WebPPictureYUVAToARGB(pic);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080072
73 End:
74 return ok;
75}
76
James Zern31f9dc62011-06-06 17:56:50 -070077#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080078
Pascal Massimino2ab4b722011-04-25 16:58:04 -070079static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -080080 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -080081 int ok;
James Zern63aba3a2012-12-03 18:20:00 -080082 (void)metadata; // TODO(jzern): add metadata extraction using WIC
Pascal Massiminof61d14a2011-02-18 23:33:46 -080083 if (pic->width != 0 && pic->height != 0) {
84 // If image size is specified, infer it as YUV format.
85 FILE* in_file = fopen(filename, "rb");
86 if (in_file == NULL) {
87 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
88 return 0;
89 }
90 ok = ReadYUV(in_file, pic);
91 fclose(in_file);
92 } else {
93 // If no size specified, try to decode it using WIC.
James Zerna452a552013-01-22 18:28:52 -080094 ok = ReadPictureWithWIC(filename, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080095 }
96 if (!ok) {
97 fprintf(stderr, "Error! Could not process file %s\n", filename);
98 }
99 return ok;
100}
101
James Zern31f9dc62011-06-06 17:56:50 -0700102#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800103
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800104typedef enum {
James Zernd8921dd2012-07-02 11:24:23 -0700105 PNG_ = 0,
106 JPEG_,
James Zern6f76d242012-07-01 17:55:21 -0700107 TIFF_, // 'TIFF' clashes with libtiff
James Zerna0b27362012-01-27 17:39:47 -0800108 UNSUPPORTED
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800109} InputFileFormat;
110
111static InputFileFormat GetImageType(FILE* in_file) {
112 InputFileFormat format = UNSUPPORTED;
113 unsigned int magic;
114 unsigned char buf[4];
115
116 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
117 (fseek(in_file, 0, SEEK_SET) != 0)) {
118 return format;
119 }
120
121 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
122 if (magic == 0x89504E47U) {
James Zernd8921dd2012-07-02 11:24:23 -0700123 format = PNG_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800124 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
James Zernd8921dd2012-07-02 11:24:23 -0700125 format = JPEG_;
James Zern6f76d242012-07-01 17:55:21 -0700126 } else if (magic == 0x49492A00 || magic == 0x4D4D002A) {
127 format = TIFF_;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800128 }
129 return format;
130}
131
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700132static int ReadPicture(const char* const filename, WebPPicture* const pic,
James Zern63aba3a2012-12-03 18:20:00 -0800133 int keep_alpha, Metadata* const metadata) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800134 int ok = 0;
135 FILE* in_file = fopen(filename, "rb");
James Zern63aba3a2012-12-03 18:20:00 -0800136 (void)metadata; // TODO(jzern): add metadata extraction to the formats below.
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800137 if (in_file == NULL) {
138 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
139 return ok;
140 }
141
142 if (pic->width == 0 || pic->height == 0) {
143 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
144 const InputFileFormat format = GetImageType(in_file);
James Zernd8921dd2012-07-02 11:24:23 -0700145 if (format == PNG_) {
James Zerndba64d92012-12-04 19:00:30 -0800146 ok = ReadPNG(in_file, pic, keep_alpha, metadata);
James Zernd8921dd2012-07-02 11:24:23 -0700147 } else if (format == JPEG_) {
James Zern2c724962012-12-16 19:13:56 -0800148 ok = ReadJPEG(in_file, pic, metadata);
James Zern6f76d242012-07-01 17:55:21 -0700149 } else if (format == TIFF_) {
James Zern2914ecf2012-12-15 19:41:03 -0800150 ok = ReadTIFF(filename, pic, keep_alpha, metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800151 }
152 } else {
153 // If image size is specified, infer it as YUV format.
154 ok = ReadYUV(in_file, pic);
155 }
156 if (!ok) {
157 fprintf(stderr, "Error! Could not process file %s\n", filename);
158 }
159
160 fclose(in_file);
161 return ok;
162}
163
James Zern31f9dc62011-06-06 17:56:50 -0700164#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800165
166static void AllocExtraInfo(WebPPicture* const pic) {
167 const int mb_w = (pic->width + 15) / 16;
168 const int mb_h = (pic->height + 15) / 16;
169 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
170}
171
172static void PrintByteCount(const int bytes[4], int total_size,
173 int* const totals) {
174 int s;
175 int total = 0;
176 for (s = 0; s < 4; ++s) {
177 fprintf(stderr, "| %7d ", bytes[s]);
178 total += bytes[s];
179 if (totals) totals[s] += bytes[s];
180 }
James Zern04e84cf2011-11-04 15:20:08 -0700181 fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800182}
183
184static void PrintPercents(const int counts[4], int total) {
185 int s;
186 for (s = 0; s < 4; ++s) {
187 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
188 }
James Zern04e84cf2011-11-04 15:20:08 -0700189 fprintf(stderr, "| %7d\n", total);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800190}
191
192static void PrintValues(const int values[4]) {
193 int s;
194 for (s = 0; s < 4; ++s) {
195 fprintf(stderr, "| %7d ", values[s]);
196 }
James Zern04e84cf2011-11-04 15:20:08 -0700197 fprintf(stderr, "|\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800198}
199
Pascal Massimino7d853d72012-07-24 16:15:36 -0700200static void PrintFullLosslessInfo(const WebPAuxStats* const stats,
201 const char* const description) {
202 fprintf(stderr, "Lossless-%s compressed size: %d bytes\n",
203 description, stats->lossless_size);
204 if (stats->lossless_features) {
205 fprintf(stderr, " * Lossless features used:");
206 if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION");
207 if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM");
208 if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN");
209 if (stats->lossless_features & 8) fprintf(stderr, " PALETTE");
210 fprintf(stderr, "\n");
211 }
212 fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n",
213 stats->histogram_bits, stats->transform_bits, stats->cache_bits);
214 if (stats->palette_size > 0) {
215 fprintf(stderr, " * Palette size: %d\n", stats->palette_size);
216 }
217}
218
Vikas Arorac4ccab62012-05-09 11:27:46 +0530219static void PrintExtraInfoLossless(const WebPPicture* const pic,
220 int short_output,
221 const char* const file_name) {
222 const WebPAuxStats* const stats = pic->stats;
223 if (short_output) {
224 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
225 } else {
226 fprintf(stderr, "File: %s\n", file_name);
227 fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
228 fprintf(stderr, "Output: %d bytes\n", stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700229 PrintFullLosslessInfo(stats, "ARGB");
Vikas Arorac4ccab62012-05-09 11:27:46 +0530230 }
231}
232
233static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
234 const char* const file_name) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800235 const WebPAuxStats* const stats = pic->stats;
236 if (short_output) {
237 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
James Zern04e84cf2011-11-04 15:20:08 -0700238 } else {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800239 const int num_i4 = stats->block_count[0];
240 const int num_i16 = stats->block_count[1];
241 const int num_skip = stats->block_count[2];
242 const int total = num_i4 + num_i16;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800243 fprintf(stderr, "File: %s\n", file_name);
244 fprintf(stderr, "Dimension: %d x %d%s\n",
James Zernc71ff9e2012-06-07 17:32:29 -0700245 pic->width, pic->height,
246 stats->alpha_data_size ? " (with alpha)" : "");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800247 fprintf(stderr, "Output: "
248 "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800249 stats->coded_size,
250 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
251 if (total > 0) {
252 int totals[4] = { 0, 0, 0, 0 };
253 fprintf(stderr, "block count: intra4: %d\n"
254 " intra16: %d (-> %.2f%%)\n",
255 num_i4, num_i16, 100.f * num_i16 / total);
256 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
257 num_skip, 100.f * num_skip / total);
258 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
259 " mode-partition: %6d (%.1f%%)\n",
260 stats->header_bytes[0],
261 100.f * stats->header_bytes[0] / stats->coded_size,
262 stats->header_bytes[1],
263 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino7d853d72012-07-24 16:15:36 -0700264 if (stats->alpha_data_size > 0) {
265 fprintf(stderr, " transparency: %6d (%.1f dB)\n",
266 stats->alpha_data_size, stats->PSNR[4]);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700267 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700268 if (stats->layer_data_size) {
269 fprintf(stderr, " enhancement: %6d\n",
270 stats->layer_data_size);
271 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800272 fprintf(stderr, " Residuals bytes "
273 "|segment 1|segment 2|segment 3"
274 "|segment 4| total\n");
275 fprintf(stderr, " intra4-coeffs: ");
276 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
277 fprintf(stderr, " intra16-coeffs: ");
278 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
279 fprintf(stderr, " chroma coeffs: ");
280 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
281 fprintf(stderr, " macroblocks: ");
282 PrintPercents(stats->segment_size, total);
283 fprintf(stderr, " quantizer: ");
284 PrintValues(stats->segment_quant);
285 fprintf(stderr, " filter level: ");
286 PrintValues(stats->segment_level);
287 fprintf(stderr, "------------------+---------");
288 fprintf(stderr, "+---------+---------+---------+-----------------\n");
289 fprintf(stderr, " segments total: ");
290 PrintByteCount(totals, stats->coded_size, NULL);
291 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700292 if (stats->lossless_size > 0) {
293 PrintFullLosslessInfo(stats, "alpha");
294 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800295 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700296 if (pic->extra_info != NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800297 const int mb_w = (pic->width + 15) / 16;
298 const int mb_h = (pic->height + 15) / 16;
299 const int type = pic->extra_info_type;
300 int x, y;
301 for (y = 0; y < mb_h; ++y) {
302 for (x = 0; x < mb_w; ++x) {
303 const int c = pic->extra_info[x + y * mb_w];
304 if (type == 1) { // intra4/intra16
305 printf("%c", "+."[c]);
306 } else if (type == 2) { // segments
307 printf("%c", ".-*X"[c]);
308 } else if (type == 3) { // quantizers
309 printf("%.2d ", c);
310 } else if (type == 6 || type == 7) {
311 printf("%3d ", c);
312 } else {
313 printf("0x%.2x ", c);
314 }
315 }
316 printf("\n");
317 }
318 }
319}
320
James Zernc7e86ab2011-08-25 14:22:32 -0700321//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800322
323static int MyWriter(const uint8_t* data, size_t data_size,
324 const WebPPicture* const pic) {
325 FILE* const out = (FILE*)pic->custom_ptr;
326 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
327}
328
329// Dumps a picture as a PGM file using the IMC4 layout.
330static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
331 int y;
332 const int uv_width = (picture->width + 1) / 2;
333 const int uv_height = (picture->height + 1) / 2;
334 const int stride = (picture->width + 1) & ~1;
Pascal Massimino437999f2012-06-04 15:50:05 -0700335 const int alpha_height =
336 WebPPictureHasTransparency(picture) ? picture->height : 0;
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700337 const int height = picture->height + uv_height + alpha_height;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800338 FILE* const f = fopen(PGM_name, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800339 if (f == NULL) return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800340 fprintf(f, "P5\n%d %d\n255\n", stride, height);
341 for (y = 0; y < picture->height; ++y) {
342 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
343 return 0;
344 if (picture->width & 1) fputc(0, f); // pad
345 }
346 for (y = 0; y < uv_height; ++y) {
347 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
348 return 0;
349 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
350 return 0;
351 }
Pascal Massimino5142a0b2011-07-07 16:08:15 -0700352 for (y = 0; y < alpha_height; ++y) {
353 if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1)
354 return 0;
355 if (picture->width & 1) fputc(0, f); // pad
356 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800357 fclose(f);
358 return 1;
359}
360
James Zern7eaee9f2013-01-11 12:25:36 -0800361// -----------------------------------------------------------------------------
362// Metadata writing.
363
364enum {
365 METADATA_EXIF = (1 << 0),
366 METADATA_ICCP = (1 << 1),
367 METADATA_XMP = (1 << 2),
368 METADATA_ALL = METADATA_EXIF | METADATA_ICCP | METADATA_XMP
369};
370
James Zernc7e86ab2011-08-25 14:22:32 -0700371//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800372
Pascal Massimino30971c92011-12-01 02:24:50 -0800373static int ProgressReport(int percent, const WebPPicture* const picture) {
374 printf("[%s]: %3d %% \r",
James Zern475d87d2012-07-27 19:53:16 -0700375 (char*)picture->user_data, percent);
Pascal Massimino30971c92011-12-01 02:24:50 -0800376 fflush(stdout);
377 return 1; // all ok
378}
379
380//------------------------------------------------------------------------------
381
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700382static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800383 printf("Usage:\n\n");
384 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
385 printf("where quality is between 0 (poor) to 100 (very good).\n");
386 printf("Typical value is around 80.\n\n");
387 printf("Try -longhelp for an exhaustive list of advanced options.\n");
388}
389
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700390static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800391 printf("Usage:\n");
392 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
393 printf("If input size (-s) for an image is not specified, "
James Zern12f9aed2012-07-13 14:55:39 -0700394 "it is assumed to be a PNG, JPEG or TIFF file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700395#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800396 printf("Windows builds can take as input any of the files handled by WIC\n");
397#endif
398 printf("options:\n");
399 printf(" -h / -help ............ short help\n");
400 printf(" -H / -longhelp ........ long help\n");
401 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530402 printf(" -alpha_q <int> ......... Transparency-compression quality "
403 "(0..100).\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800404 printf(" -preset <string> ....... Preset setting, one of:\n");
405 printf(" default, photo, picture,\n");
406 printf(" drawing, icon, text\n");
407 printf(" -preset must come first, as it overwrites other parameters.");
408 printf("\n");
409 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
410 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700411 printf(" -size <int> ............ Target size (in bytes)\n");
412 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800413 printf("\n");
414 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
415 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
416 printf(" -f <int> ............... filter strength (0=off..100)\n");
417 printf(" -sharpness <int> ....... "
418 "filter sharpness (0:most .. 7:least sharp)\n");
419 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino900286e2011-08-23 15:58:22 -0700420 printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n");
421 printf(" "
422 "the first partition (0=no degradation ... 100=full)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800423 printf(" -pass <int> ............ analysis pass number (1..10)\n");
424 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700425 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
426#ifdef WEBP_EXPERIMENTAL_FEATURES
427 printf(" -444 / -422 / -gray ..... Change colorspace\n");
428#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800429 printf(" -map <int> ............. print map of extra info.\n");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800430 printf(" -print_psnr ............ prints averaged PSNR distortion.\n");
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700431 printf(" -print_ssim ............ prints averaged SSIM distortion.\n");
432 printf(" -print_lsim ............ prints local-similarity distortion.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800433 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530434 printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n");
Pascal Massimino8ca20762012-01-08 19:27:21 -0800435 printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n");
436 printf(" One of: none, fast (default) or best.\n");
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000437 printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n");
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530438 printf(" -noalpha ............... discard any transparency information.\n");
Vikas Arorad3730762012-06-22 12:14:48 +0530439 printf(" -lossless .............. Encode image losslessly.\n");
440 printf(" -hint <string> ......... Specify image characteristics hint.\n");
Vikas Aroradd1c3872012-07-31 23:07:52 -0700441 printf(" One of: photo, picture or graph\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700442
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800443 printf("\n");
James Zern7eaee9f2013-01-11 12:25:36 -0800444 printf(" -metadata <string> ..... comma separated list of metadata to\n");
445 printf(" ");
446 printf("copy from the input to the output if present.\n");
447 printf(" "
448 "Valid values: all, none (default), exif, iccp, xmp\n");
449
450 printf("\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800451 printf(" -short ................. condense printed message\n");
452 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700453 printf(" -version ............... print version number and exit.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700454#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700455 printf(" -noasm ................. disable all assembly optimizations.\n");
James Zernb4d0ef82011-07-15 14:53:03 -0700456#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800457 printf(" -v ..................... verbose, e.g. print encoding/decoding "
458 "times\n");
Pascal Massimino30971c92011-12-01 02:24:50 -0800459 printf(" -progress .............. report encoding progress\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800460 printf("\n");
461 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800462 printf(" -af .................... auto-adjust filter strength.\n");
463 printf(" -pre <int> ............. pre-processing filter\n");
464 printf("\n");
465}
466
James Zernc7e86ab2011-08-25 14:22:32 -0700467//------------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700468// Error messages
469
470static const char* const kErrorMessages[] = {
471 "OK",
472 "OUT_OF_MEMORY: Out of memory allocating objects",
473 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
474 "NULL_PARAMETER: NULL parameter passed to function",
475 "INVALID_CONFIGURATION: configuration is invalid",
Pascal Massimino900286e2011-08-23 15:58:22 -0700476 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
477 "allowed is 16383 pixels.",
478 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
479 "To reduce the size of this partition, try using less segments "
480 "with the -segments option, and eventually reduce the number of "
481 "header bits using -partition_limit. More details are available "
482 "in the manual (`man cwebp`)",
483 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
Pascal Massiminod71fbdc2011-12-01 03:34:22 -0800484 "BAD_WRITE: Picture writer returned an I/O error",
Pascal Massimino30971c92011-12-01 02:24:50 -0800485 "FILE_TOO_BIG: File would be too big to fit in 4G",
486 "USER_ABORT: encoding abort requested by user"
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700487};
488
James Zernc7e86ab2011-08-25 14:22:32 -0700489//------------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800490
491int main(int argc, const char *argv[]) {
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700492 int return_value = -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800493 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
494 FILE *out = NULL;
495 int c;
496 int short_output = 0;
497 int quiet = 0;
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530498 int keep_alpha = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800499 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700500 int resize_w = 0, resize_h = 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800501 int show_progress = 0;
James Zern7eaee9f2013-01-11 12:25:36 -0800502 int keep_metadata = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800503 WebPPicture picture;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700504 int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM
Pascal Massiminod61479f2012-01-20 07:20:56 -0800505 WebPPicture original_picture; // when PSNR or SSIM is requested
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800506 WebPConfig config;
507 WebPAuxStats stats;
James Zern63aba3a2012-12-03 18:20:00 -0800508 Metadata metadata;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800509 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700510
James Zern63aba3a2012-12-03 18:20:00 -0800511 MetadataInit(&metadata);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800512 if (!WebPPictureInit(&picture) ||
513 !WebPPictureInit(&original_picture) ||
514 !WebPConfigInit(&config)) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800515 fprintf(stderr, "Error! Version mismatch!\n");
James Zern256afef2012-07-27 18:56:55 -0700516 return -1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800517 }
518
519 if (argc == 1) {
520 HelpShort();
521 return 0;
522 }
523
524 for (c = 1; c < argc; ++c) {
525 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
526 HelpShort();
527 return 0;
528 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
529 HelpLong();
530 return 0;
531 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
532 out_file = argv[++c];
533 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
534 dump_file = argv[++c];
535 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800536 } else if (!strcmp(argv[c], "-print_psnr")) {
537 config.show_compressed = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700538 print_distortion = 0;
539 } else if (!strcmp(argv[c], "-print_ssim")) {
540 config.show_compressed = 1;
Pascal Massiminod61479f2012-01-20 07:20:56 -0800541 print_distortion = 1;
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700542 } else if (!strcmp(argv[c], "-print_lsim")) {
543 config.show_compressed = 1;
544 print_distortion = 2;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800545 } else if (!strcmp(argv[c], "-short")) {
546 short_output++;
547 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700548 picture.width = strtol(argv[++c], NULL, 0);
549 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800550 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700551 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800552 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200553 config.quality = (float)strtod(argv[++c], NULL);
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530554 } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) {
555 config.alpha_quality = strtol(argv[++c], NULL, 0);
556 } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) {
557 config.alpha_compression = strtol(argv[++c], NULL, 0);
Pascal Massimino2e3e8b22012-01-17 08:18:22 +0000558 } else if (!strcmp(argv[c], "-alpha_cleanup")) {
559 keep_alpha = keep_alpha ? 2 : 0;
Vikas Arora252028a2012-01-05 13:04:30 +0530560 } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) {
Pascal Massimino8ca20762012-01-08 19:27:21 -0800561 ++c;
562 if (!strcmp(argv[c], "none")) {
563 config.alpha_filtering = 0;
564 } else if (!strcmp(argv[c], "fast")) {
565 config.alpha_filtering = 1;
566 } else if (!strcmp(argv[c], "best")) {
567 config.alpha_filtering = 2;
568 } else {
569 fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]);
570 goto Error;
571 }
Vikas Aroraa0ec9aa2011-12-01 15:11:34 +0530572 } else if (!strcmp(argv[c], "-noalpha")) {
573 keep_alpha = 0;
Pascal Massimino72920ca2012-04-24 10:59:08 +0000574 } else if (!strcmp(argv[c], "-lossless")) {
575 config.lossless = 1;
Pascal Massiminodd108172012-07-18 21:58:53 +0000576 picture.use_argb = 1;
Vikas Arorad3730762012-06-22 12:14:48 +0530577 } else if (!strcmp(argv[c], "-hint") && c < argc - 1) {
578 ++c;
579 if (!strcmp(argv[c], "photo")) {
580 config.image_hint = WEBP_HINT_PHOTO;
581 } else if (!strcmp(argv[c], "picture")) {
582 config.image_hint = WEBP_HINT_PICTURE;
Vikas Aroradd1c3872012-07-31 23:07:52 -0700583 } else if (!strcmp(argv[c], "graph")) {
584 config.image_hint = WEBP_HINT_GRAPH;
Vikas Arorad3730762012-06-22 12:14:48 +0530585 } else {
586 fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]);
587 goto Error;
588 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800589 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700590 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800591 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Mikolaj Zalewski2aa6b802011-06-28 16:02:56 +0200592 config.target_PSNR = (float)strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800593 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700594 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800595 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700596 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800597 } else if (!strcmp(argv[c], "-af")) {
598 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700599 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800600 config.filter_type = 1;
601 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700602 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800603 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700604 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800605 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700606 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800607 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700608 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino900286e2011-08-23 15:58:22 -0700609 } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) {
610 config.partition_limit = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800611 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700612 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700613#ifdef WEBP_EXPERIMENTAL_FEATURES
614 } else if (!strcmp(argv[c], "-444")) {
615 picture.colorspace = WEBP_YUV444;
616 } else if (!strcmp(argv[c], "-422")) {
617 picture.colorspace = WEBP_YUV422;
618 } else if (!strcmp(argv[c], "-gray")) {
619 picture.colorspace = WEBP_YUV400;
620#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800621 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
622 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700623 crop_x = strtol(argv[++c], NULL, 0);
624 crop_y = strtol(argv[++c], NULL, 0);
625 crop_w = strtol(argv[++c], NULL, 0);
626 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700627 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
628 resize_w = strtol(argv[++c], NULL, 0);
629 resize_h = strtol(argv[++c], NULL, 0);
James Zernb4d0ef82011-07-15 14:53:03 -0700630#ifndef WEBP_DLL
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700631 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoe06ac082011-09-02 21:30:08 +0000632 VP8GetCPUInfo = NULL;
James Zernb4d0ef82011-07-15 14:53:03 -0700633#endif
Pascal Massimino650ffa32011-03-24 16:17:10 -0700634 } else if (!strcmp(argv[c], "-version")) {
635 const int version = WebPGetEncoderVersion();
636 printf("%d.%d.%d\n",
637 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
638 return 0;
Pascal Massimino30971c92011-12-01 02:24:50 -0800639 } else if (!strcmp(argv[c], "-progress")) {
640 show_progress = 1;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800641 } else if (!strcmp(argv[c], "-quiet")) {
642 quiet = 1;
643 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
644 WebPPreset preset;
645 ++c;
646 if (!strcmp(argv[c], "default")) {
647 preset = WEBP_PRESET_DEFAULT;
648 } else if (!strcmp(argv[c], "photo")) {
649 preset = WEBP_PRESET_PHOTO;
650 } else if (!strcmp(argv[c], "picture")) {
651 preset = WEBP_PRESET_PICTURE;
652 } else if (!strcmp(argv[c], "drawing")) {
653 preset = WEBP_PRESET_DRAWING;
654 } else if (!strcmp(argv[c], "icon")) {
655 preset = WEBP_PRESET_ICON;
656 } else if (!strcmp(argv[c], "text")) {
657 preset = WEBP_PRESET_TEXT;
658 } else {
659 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
660 goto Error;
661 }
662 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700663 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800664 goto Error;
665 }
James Zern7eaee9f2013-01-11 12:25:36 -0800666 } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) {
667 static const struct {
668 const char* option;
669 int flag;
670 } kTokens[] = {
671 { "all", METADATA_ALL },
672 { "none", 0 },
673 { "exif", METADATA_EXIF },
674 { "iccp", METADATA_ICCP },
675 { "xmp", METADATA_XMP },
676 };
677 const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]);
678 const char* start = argv[++c];
679 const char* const end = start + strlen(start);
680
681 while (start < end) {
682 size_t i;
683 const char* token = strchr(start, ',');
684 if (token == NULL) token = end;
685
686 for (i = 0; i < kNumTokens; ++i) {
687 if ((size_t)(token - start) == strlen(kTokens[i].option) &&
688 !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) {
689 if (kTokens[i].flag != 0) {
690 keep_metadata |= kTokens[i].flag;
691 } else {
692 keep_metadata = 0;
693 }
694 break;
695 }
696 }
697 if (i == kNumTokens) {
698 fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
699 (int)(token - start), start);
700 HelpLong();
701 return -1;
702 }
703 start = token + 1;
704 }
705 if (keep_metadata != 0) {
706 // TODO(jzern): remove when -metadata is supported on all platforms.
707 fprintf(stderr, "Warning: -metadata is currently unsupported on this"
708 " platform. Ignoring this option!\n");
709 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800710 } else if (!strcmp(argv[c], "-v")) {
711 verbose = 1;
712 } else if (argv[c][0] == '-') {
713 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
714 HelpLong();
715 return -1;
716 } else {
717 in_file = argv[c];
718 }
719 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700720 if (in_file == NULL) {
721 fprintf(stderr, "No input file specified!\n");
722 HelpShort();
723 goto Error;
724 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800725
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530726 // Check for unsupported command line options for lossless mode and log
727 // warning for such options.
Pascal Massimino02751592012-06-20 09:20:34 +0000728 if (!quiet && config.lossless == 1) {
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530729 if (config.target_size > 0 || config.target_PSNR > 0) {
730 fprintf(stderr, "Encoding for specified size or PSNR is not supported"
731 " for lossless encoding. Ignoring such option(s)!\n");
732 }
733 if (config.partition_limit > 0) {
734 fprintf(stderr, "Partition limit option is not required for lossless"
735 " encoding. Ignoring this option!\n");
736 }
Vikas Arorab7fb0ed2012-06-20 09:02:25 +0530737 }
738
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800739 if (!WebPValidateConfig(&config)) {
740 fprintf(stderr, "Error! Invalid configuration.\n");
741 goto Error;
742 }
743
Pascal Massimino0744e842011-02-25 12:03:27 -0800744 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700745 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800746 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700747 }
James Zern7eaee9f2013-01-11 12:25:36 -0800748 if (!ReadPicture(in_file, &picture, keep_alpha,
749 (keep_metadata == 0) ? NULL : &metadata)) {
Pascal Massiminod61479f2012-01-20 07:20:56 -0800750 fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700751 goto Error;
752 }
Pascal Massimino30971c92011-12-01 02:24:50 -0800753 picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
754
Pascal Massimino0744e842011-02-25 12:03:27 -0800755 if (verbose) {
756 const double time = StopwatchReadAndReset(&stop_watch);
757 fprintf(stderr, "Time to read input: %.3fs\n", time);
758 }
759
760 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800761 if (out_file) {
762 out = fopen(out_file, "wb");
Pascal Massiminod61479f2012-01-20 07:20:56 -0800763 if (out == NULL) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800764 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
765 goto Error;
766 } else {
767 if (!short_output && !quiet) {
768 fprintf(stderr, "Saving file '%s'\n", out_file);
769 }
770 }
771 picture.writer = MyWriter;
772 picture.custom_ptr = (void*)out;
773 } else {
774 out = NULL;
775 if (!quiet && !short_output) {
776 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
777 fprintf(stderr, "be performed, but its results discarded.\n\n");
778 }
779 }
Pascal Massimino7d853d72012-07-24 16:15:36 -0700780 if (!quiet) {
781 picture.stats = &stats;
James Zern475d87d2012-07-27 19:53:16 -0700782 picture.user_data = (void*)in_file;
Pascal Massimino7d853d72012-07-24 16:15:36 -0700783 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800784
Pascal Massimino0744e842011-02-25 12:03:27 -0800785 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700786 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800787 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700788 }
Pascal Massimino31b68fe2012-06-21 00:30:43 -0700789 if (crop != 0) {
790 // We use self-cropping using a view.
791 if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) {
792 fprintf(stderr, "Error! Cannot crop picture\n");
793 goto Error;
794 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700795 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700796 if ((resize_w | resize_h) > 0) {
797 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
798 fprintf(stderr, "Error! Cannot resize picture\n");
799 goto Error;
800 }
801 }
802 if (picture.extra_info_type > 0) {
803 AllocExtraInfo(&picture);
804 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700805 if (print_distortion >= 0) { // Save original picture for later comparison
Pascal Massiminod61479f2012-01-20 07:20:56 -0800806 WebPPictureCopy(&picture, &original_picture);
807 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700808 if (!WebPEncode(&config, &picture)) {
809 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700810 fprintf(stderr, "Error code: %d (%s)\n",
811 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700812 goto Error;
813 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800814 if (verbose) {
815 const double time = StopwatchReadAndReset(&stop_watch);
816 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
817 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800818
819 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700820 if (dump_file) {
Pascal Massiminodd108172012-07-18 21:58:53 +0000821 if (picture.use_argb) {
Pascal Massimino437999f2012-06-04 15:50:05 -0700822 fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode.");
823 } else if (!DumpPicture(&picture, dump_file)) {
824 fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
825 }
Pascal Massimino38369c02011-05-04 22:59:51 -0700826 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800827
Pascal Massimino38369c02011-05-04 22:59:51 -0700828 if (!quiet) {
Vikas Arorac4ccab62012-05-09 11:27:46 +0530829 if (config.lossless) {
830 PrintExtraInfoLossless(&picture, short_output, in_file);
831 } else {
832 PrintExtraInfoLossy(&picture, short_output, in_file);
833 }
Pascal Massiminod61479f2012-01-20 07:20:56 -0800834 }
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700835 if (!quiet && !short_output && print_distortion >= 0) { // print distortion
836 static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
Pascal Massiminod61479f2012-01-20 07:20:56 -0800837 float values[5];
838 WebPPictureDistortion(&picture, &original_picture,
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700839 print_distortion, values);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800840 fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
Pascal Massiminof86e6ab2012-10-18 08:26:40 -0700841 distortion_names[print_distortion],
Pascal Massiminod61479f2012-01-20 07:20:56 -0800842 values[0], values[1], values[2], values[3], values[4]);
Pascal Massimino38369c02011-05-04 22:59:51 -0700843 }
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700844 return_value = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800845
846 Error:
847 free(picture.extra_info);
James Zern63aba3a2012-12-03 18:20:00 -0800848 MetadataFree(&metadata);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800849 WebPPictureFree(&picture);
Pascal Massiminod61479f2012-01-20 07:20:56 -0800850 WebPPictureFree(&original_picture);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800851 if (out != NULL) {
852 fclose(out);
853 }
854
Pascal Massimino4abe04a2012-05-09 00:32:20 -0700855 return return_value;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800856}
857
James Zernc7e86ab2011-08-25 14:22:32 -0700858//------------------------------------------------------------------------------