James Zern | ad1e163 | 2012-01-06 14:49:06 -0800 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 2 | // |
| 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 Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 14 | #include <stdlib.h> |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 17 | #ifdef HAVE_CONFIG_H |
| 18 | #include "config.h" |
| 19 | #endif |
| 20 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 21 | #include "webp/encode.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 22 | |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 23 | #include "./metadata.h" |
James Zern | 00b29e2 | 2012-05-15 13:48:11 -0700 | [diff] [blame] | 24 | #include "./stopwatch.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 25 | |
| 26 | #include "./jpegdec.h" |
| 27 | #include "./pngdec.h" |
James Zern | 1c1c564 | 2012-12-06 14:04:36 -0800 | [diff] [blame] | 28 | #include "./tiffdec.h" |
James Zern | a452a55 | 2013-01-22 18:28:52 -0800 | [diff] [blame] | 29 | #include "./wicdec.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 30 | |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 31 | #ifndef WEBP_DLL |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 32 | #if defined(__cplusplus) || defined(c_plusplus) |
| 33 | extern "C" { |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 34 | #endif |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 35 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 36 | extern void* VP8GetCPUInfo; // opaque forward declaration. |
| 37 | |
| 38 | #if defined(__cplusplus) || defined(c_plusplus) |
| 39 | } // extern "C" |
| 40 | #endif |
| 41 | #endif // WEBP_DLL |
| 42 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 43 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 44 | |
| 45 | static int verbose = 0; |
| 46 | |
| 47 | static int ReadYUV(FILE* in_file, WebPPicture* const pic) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 48 | const int use_argb = pic->use_argb; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 49 | 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 Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 54 | pic->use_argb = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 55 | 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 Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 71 | if (use_argb) ok = WebPPictureYUVAToARGB(pic); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 72 | |
| 73 | End: |
| 74 | return ok; |
| 75 | } |
| 76 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 77 | #ifdef HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 78 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 79 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 80 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 81 | int ok; |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 82 | (void)metadata; // TODO(jzern): add metadata extraction using WIC |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 83 | 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 Zern | a452a55 | 2013-01-22 18:28:52 -0800 | [diff] [blame] | 94 | ok = ReadPictureWithWIC(filename, pic, keep_alpha); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 95 | } |
| 96 | if (!ok) { |
| 97 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 98 | } |
| 99 | return ok; |
| 100 | } |
| 101 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 102 | #else // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 103 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 104 | typedef enum { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 105 | PNG_ = 0, |
| 106 | JPEG_, |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 107 | TIFF_, // 'TIFF' clashes with libtiff |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 108 | UNSUPPORTED |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 109 | } InputFileFormat; |
| 110 | |
| 111 | static 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 Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 123 | format = PNG_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 124 | } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 125 | format = JPEG_; |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 126 | } else if (magic == 0x49492A00 || magic == 0x4D4D002A) { |
| 127 | format = TIFF_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 128 | } |
| 129 | return format; |
| 130 | } |
| 131 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 132 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 133 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 134 | int ok = 0; |
| 135 | FILE* in_file = fopen(filename, "rb"); |
| 136 | if (in_file == NULL) { |
| 137 | fprintf(stderr, "Error! Cannot open input file '%s'\n", filename); |
| 138 | return ok; |
| 139 | } |
| 140 | |
| 141 | if (pic->width == 0 || pic->height == 0) { |
| 142 | // If no size specified, try to decode it as PNG/JPEG (as appropriate). |
| 143 | const InputFileFormat format = GetImageType(in_file); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 144 | if (format == PNG_) { |
James Zern | dba64d9 | 2012-12-04 19:00:30 -0800 | [diff] [blame] | 145 | ok = ReadPNG(in_file, pic, keep_alpha, metadata); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 146 | } else if (format == JPEG_) { |
James Zern | 2c72496 | 2012-12-16 19:13:56 -0800 | [diff] [blame] | 147 | ok = ReadJPEG(in_file, pic, metadata); |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 148 | } else if (format == TIFF_) { |
James Zern | 2914ecf | 2012-12-15 19:41:03 -0800 | [diff] [blame] | 149 | ok = ReadTIFF(filename, pic, keep_alpha, metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 150 | } |
| 151 | } else { |
| 152 | // If image size is specified, infer it as YUV format. |
| 153 | ok = ReadYUV(in_file, pic); |
| 154 | } |
| 155 | if (!ok) { |
| 156 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 157 | } |
| 158 | |
| 159 | fclose(in_file); |
| 160 | return ok; |
| 161 | } |
| 162 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 163 | #endif // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 164 | |
| 165 | static void AllocExtraInfo(WebPPicture* const pic) { |
| 166 | const int mb_w = (pic->width + 15) / 16; |
| 167 | const int mb_h = (pic->height + 15) / 16; |
| 168 | pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info)); |
| 169 | } |
| 170 | |
| 171 | static void PrintByteCount(const int bytes[4], int total_size, |
| 172 | int* const totals) { |
| 173 | int s; |
| 174 | int total = 0; |
| 175 | for (s = 0; s < 4; ++s) { |
| 176 | fprintf(stderr, "| %7d ", bytes[s]); |
| 177 | total += bytes[s]; |
| 178 | if (totals) totals[s] += bytes[s]; |
| 179 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 180 | fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void PrintPercents(const int counts[4], int total) { |
| 184 | int s; |
| 185 | for (s = 0; s < 4; ++s) { |
| 186 | fprintf(stderr, "| %2d%%", 100 * counts[s] / total); |
| 187 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 188 | fprintf(stderr, "| %7d\n", total); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void PrintValues(const int values[4]) { |
| 192 | int s; |
| 193 | for (s = 0; s < 4; ++s) { |
| 194 | fprintf(stderr, "| %7d ", values[s]); |
| 195 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 196 | fprintf(stderr, "|\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 199 | static void PrintFullLosslessInfo(const WebPAuxStats* const stats, |
| 200 | const char* const description) { |
| 201 | fprintf(stderr, "Lossless-%s compressed size: %d bytes\n", |
| 202 | description, stats->lossless_size); |
| 203 | if (stats->lossless_features) { |
| 204 | fprintf(stderr, " * Lossless features used:"); |
| 205 | if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION"); |
| 206 | if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM"); |
| 207 | if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN"); |
| 208 | if (stats->lossless_features & 8) fprintf(stderr, " PALETTE"); |
| 209 | fprintf(stderr, "\n"); |
| 210 | } |
| 211 | fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n", |
| 212 | stats->histogram_bits, stats->transform_bits, stats->cache_bits); |
| 213 | if (stats->palette_size > 0) { |
| 214 | fprintf(stderr, " * Palette size: %d\n", stats->palette_size); |
| 215 | } |
| 216 | } |
| 217 | |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 218 | static void PrintExtraInfoLossless(const WebPPicture* const pic, |
| 219 | int short_output, |
| 220 | const char* const file_name) { |
| 221 | const WebPAuxStats* const stats = pic->stats; |
| 222 | if (short_output) { |
| 223 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
| 224 | } else { |
| 225 | fprintf(stderr, "File: %s\n", file_name); |
| 226 | fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height); |
| 227 | fprintf(stderr, "Output: %d bytes\n", stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 228 | PrintFullLosslessInfo(stats, "ARGB"); |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output, |
| 233 | const char* const file_name) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 234 | const WebPAuxStats* const stats = pic->stats; |
| 235 | if (short_output) { |
| 236 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 237 | } else { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 238 | const int num_i4 = stats->block_count[0]; |
| 239 | const int num_i16 = stats->block_count[1]; |
| 240 | const int num_skip = stats->block_count[2]; |
| 241 | const int total = num_i4 + num_i16; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 242 | fprintf(stderr, "File: %s\n", file_name); |
| 243 | fprintf(stderr, "Dimension: %d x %d%s\n", |
James Zern | c71ff9e | 2012-06-07 17:32:29 -0700 | [diff] [blame] | 244 | pic->width, pic->height, |
| 245 | stats->alpha_data_size ? " (with alpha)" : ""); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 246 | fprintf(stderr, "Output: " |
| 247 | "%d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n", |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 248 | stats->coded_size, |
| 249 | stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]); |
| 250 | if (total > 0) { |
| 251 | int totals[4] = { 0, 0, 0, 0 }; |
| 252 | fprintf(stderr, "block count: intra4: %d\n" |
| 253 | " intra16: %d (-> %.2f%%)\n", |
| 254 | num_i4, num_i16, 100.f * num_i16 / total); |
| 255 | fprintf(stderr, " skipped block: %d (%.2f%%)\n", |
| 256 | num_skip, 100.f * num_skip / total); |
| 257 | fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n" |
| 258 | " mode-partition: %6d (%.1f%%)\n", |
| 259 | stats->header_bytes[0], |
| 260 | 100.f * stats->header_bytes[0] / stats->coded_size, |
| 261 | stats->header_bytes[1], |
| 262 | 100.f * stats->header_bytes[1] / stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 263 | if (stats->alpha_data_size > 0) { |
| 264 | fprintf(stderr, " transparency: %6d (%.1f dB)\n", |
| 265 | stats->alpha_data_size, stats->PSNR[4]); |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 266 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 267 | if (stats->layer_data_size) { |
| 268 | fprintf(stderr, " enhancement: %6d\n", |
| 269 | stats->layer_data_size); |
| 270 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 271 | fprintf(stderr, " Residuals bytes " |
| 272 | "|segment 1|segment 2|segment 3" |
| 273 | "|segment 4| total\n"); |
| 274 | fprintf(stderr, " intra4-coeffs: "); |
| 275 | PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals); |
| 276 | fprintf(stderr, " intra16-coeffs: "); |
| 277 | PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals); |
| 278 | fprintf(stderr, " chroma coeffs: "); |
| 279 | PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals); |
| 280 | fprintf(stderr, " macroblocks: "); |
| 281 | PrintPercents(stats->segment_size, total); |
| 282 | fprintf(stderr, " quantizer: "); |
| 283 | PrintValues(stats->segment_quant); |
| 284 | fprintf(stderr, " filter level: "); |
| 285 | PrintValues(stats->segment_level); |
| 286 | fprintf(stderr, "------------------+---------"); |
| 287 | fprintf(stderr, "+---------+---------+---------+-----------------\n"); |
| 288 | fprintf(stderr, " segments total: "); |
| 289 | PrintByteCount(totals, stats->coded_size, NULL); |
| 290 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 291 | if (stats->lossless_size > 0) { |
| 292 | PrintFullLosslessInfo(stats, "alpha"); |
| 293 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 294 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 295 | if (pic->extra_info != NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 296 | const int mb_w = (pic->width + 15) / 16; |
| 297 | const int mb_h = (pic->height + 15) / 16; |
| 298 | const int type = pic->extra_info_type; |
| 299 | int x, y; |
| 300 | for (y = 0; y < mb_h; ++y) { |
| 301 | for (x = 0; x < mb_w; ++x) { |
| 302 | const int c = pic->extra_info[x + y * mb_w]; |
| 303 | if (type == 1) { // intra4/intra16 |
| 304 | printf("%c", "+."[c]); |
| 305 | } else if (type == 2) { // segments |
| 306 | printf("%c", ".-*X"[c]); |
| 307 | } else if (type == 3) { // quantizers |
| 308 | printf("%.2d ", c); |
| 309 | } else if (type == 6 || type == 7) { |
| 310 | printf("%3d ", c); |
| 311 | } else { |
| 312 | printf("0x%.2x ", c); |
| 313 | } |
| 314 | } |
| 315 | printf("\n"); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 320 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 321 | |
| 322 | static int MyWriter(const uint8_t* data, size_t data_size, |
| 323 | const WebPPicture* const pic) { |
| 324 | FILE* const out = (FILE*)pic->custom_ptr; |
| 325 | return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1; |
| 326 | } |
| 327 | |
| 328 | // Dumps a picture as a PGM file using the IMC4 layout. |
| 329 | static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) { |
| 330 | int y; |
| 331 | const int uv_width = (picture->width + 1) / 2; |
| 332 | const int uv_height = (picture->height + 1) / 2; |
| 333 | const int stride = (picture->width + 1) & ~1; |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 334 | const int alpha_height = |
| 335 | WebPPictureHasTransparency(picture) ? picture->height : 0; |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 336 | const int height = picture->height + uv_height + alpha_height; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 337 | FILE* const f = fopen(PGM_name, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 338 | if (f == NULL) return 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 339 | fprintf(f, "P5\n%d %d\n255\n", stride, height); |
| 340 | for (y = 0; y < picture->height; ++y) { |
| 341 | if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1) |
| 342 | return 0; |
| 343 | if (picture->width & 1) fputc(0, f); // pad |
| 344 | } |
| 345 | for (y = 0; y < uv_height; ++y) { |
| 346 | if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 347 | return 0; |
| 348 | if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 349 | return 0; |
| 350 | } |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 351 | for (y = 0; y < alpha_height; ++y) { |
| 352 | if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1) |
| 353 | return 0; |
| 354 | if (picture->width & 1) fputc(0, f); // pad |
| 355 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 356 | fclose(f); |
| 357 | return 1; |
| 358 | } |
| 359 | |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 360 | // ----------------------------------------------------------------------------- |
| 361 | // Metadata writing. |
| 362 | |
| 363 | enum { |
| 364 | METADATA_EXIF = (1 << 0), |
| 365 | METADATA_ICCP = (1 << 1), |
| 366 | METADATA_XMP = (1 << 2), |
| 367 | METADATA_ALL = METADATA_EXIF | METADATA_ICCP | METADATA_XMP |
| 368 | }; |
| 369 | |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 370 | static const int kChunkHeaderSize = 8; |
| 371 | static const int kTagSize = 4; |
| 372 | |
| 373 | // Outputs, in little endian, 'num' bytes from 'val' to 'out'. |
| 374 | static int WriteLE(FILE* const out, uint32_t val, int num) { |
| 375 | uint8_t buf[4]; |
| 376 | int i; |
| 377 | for (i = 0; i < num; ++i) { |
| 378 | buf[i] = (uint8_t)(val & 0xff); |
| 379 | val >>= 8; |
| 380 | } |
| 381 | return (fwrite(buf, num, 1, out) == 1); |
| 382 | } |
| 383 | |
| 384 | static int WriteLE24(FILE* const out, uint32_t val) { |
| 385 | return WriteLE(out, val, 3); |
| 386 | } |
| 387 | |
| 388 | static int WriteLE32(FILE* const out, uint32_t val) { |
| 389 | return WriteLE(out, val, 4); |
| 390 | } |
| 391 | |
| 392 | static int WriteMetadataChunk(FILE* const out, const char fourcc[4], |
| 393 | const MetadataPayload* const payload) { |
| 394 | const uint8_t zero = 0; |
| 395 | const size_t need_padding = payload->size & 1; |
| 396 | int ok = (fwrite(fourcc, kTagSize, 1, out) == 1); |
| 397 | ok = ok && WriteLE32(out, (uint32_t)payload->size); |
| 398 | ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1); |
| 399 | return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding); |
| 400 | } |
| 401 | |
| 402 | // Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the |
| 403 | // chunk if there is metadata and 'keep' is true. |
| 404 | static int UpdateFlagsAndSize(const MetadataPayload* const payload, |
| 405 | int keep, int flag, |
| 406 | uint32_t* vp8x_flags, uint64_t* metadata_size) { |
| 407 | if (keep && payload->bytes != NULL && payload->size > 0) { |
| 408 | *vp8x_flags |= flag; |
| 409 | *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1); |
| 410 | return 1; |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | // Writes a WebP file using the image contained in 'memory_writer' and the |
| 416 | // metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the |
| 417 | // availability in 'metadata'. Returns true on success. |
| 418 | // For details see doc/webp-container-spec.txt#extended-file-format. |
| 419 | static int WriteWebPWithMetadata(FILE* const out, |
| 420 | const WebPPicture* const picture, |
| 421 | const WebPMemoryWriter* const memory_writer, |
| 422 | const Metadata* const metadata, |
| 423 | int keep_metadata) { |
| 424 | const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00"; |
| 425 | const int kAlphaFlag = 0x10; |
| 426 | const int kEXIFFlag = 0x08; |
| 427 | const int kICCPFlag = 0x20; |
| 428 | const int kXMPFlag = 0x04; |
| 429 | const size_t kRiffHeaderSize = 12; |
| 430 | const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1; |
| 431 | const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize; |
| 432 | uint32_t flags = 0; |
| 433 | uint64_t metadata_size = 0; |
| 434 | const int write_exif = UpdateFlagsAndSize(&metadata->exif, |
| 435 | !!(keep_metadata & METADATA_EXIF), |
| 436 | kEXIFFlag, &flags, &metadata_size); |
| 437 | const int write_iccp = UpdateFlagsAndSize(&metadata->iccp, |
| 438 | !!(keep_metadata & METADATA_ICCP), |
| 439 | kICCPFlag, &flags, &metadata_size); |
| 440 | const int write_xmp = UpdateFlagsAndSize(&metadata->xmp, |
| 441 | !!(keep_metadata & METADATA_XMP), |
| 442 | kXMPFlag, &flags, &metadata_size); |
| 443 | uint8_t* webp = memory_writer->mem; |
| 444 | size_t webp_size = memory_writer->size; |
| 445 | if (webp_size < kMinSize) return 0; |
| 446 | if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) { |
| 447 | fprintf(stderr, "Error! Addition of metadata would exceed " |
| 448 | "container size limit.\n"); |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | if (metadata_size > 0) { |
| 453 | const int kVP8XChunkSize = 18; |
| 454 | const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize); |
| 455 | const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize + |
| 456 | (has_vp8x ? 0 : kVP8XChunkSize) + |
| 457 | metadata_size); |
| 458 | // RIFF |
| 459 | int ok = (fwrite(webp, kTagSize, 1, out) == 1); |
| 460 | // RIFF size (file header size is not recorded) |
| 461 | ok = ok && WriteLE32(out, riff_size); |
| 462 | webp += kChunkHeaderSize; |
| 463 | webp_size -= kChunkHeaderSize; |
| 464 | // WEBP |
| 465 | ok = ok && (fwrite(webp, kTagSize, 1, out) == 1); |
| 466 | webp += kTagSize; |
| 467 | webp_size -= kTagSize; |
| 468 | if (has_vp8x) { // update the existing VP8X flags |
| 469 | webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff); |
| 470 | ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1); |
| 471 | webp_size -= kVP8XChunkSize; |
| 472 | } else { |
| 473 | const int is_lossless = !memcmp(webp, "VP8L", kTagSize); |
| 474 | // The alpha flag is forced with lossless images. |
| 475 | if (is_lossless) flags |= kAlphaFlag; |
| 476 | ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1); |
| 477 | ok = ok && WriteLE32(out, flags); |
| 478 | ok = ok && WriteLE24(out, picture->width - 1); |
| 479 | ok = ok && WriteLE24(out, picture->height - 1); |
| 480 | } |
| 481 | if (write_iccp) ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp); |
| 482 | // Image |
| 483 | ok = ok && (fwrite(webp, webp_size, 1, out) == 1); |
| 484 | if (write_exif) ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif); |
| 485 | if (write_xmp) ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp); |
| 486 | return ok; |
| 487 | } else { |
| 488 | // No metadata, just write the original image file. |
| 489 | return (fwrite(webp, webp_size, 1, out) == 1); |
| 490 | } |
| 491 | } |
| 492 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 493 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 494 | |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 495 | static int ProgressReport(int percent, const WebPPicture* const picture) { |
| 496 | printf("[%s]: %3d %% \r", |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 497 | (char*)picture->user_data, percent); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 498 | fflush(stdout); |
| 499 | return 1; // all ok |
| 500 | } |
| 501 | |
| 502 | //------------------------------------------------------------------------------ |
| 503 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 504 | static void HelpShort(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 505 | printf("Usage:\n\n"); |
| 506 | printf(" cwebp [options] -q quality input.png -o output.webp\n\n"); |
| 507 | printf("where quality is between 0 (poor) to 100 (very good).\n"); |
| 508 | printf("Typical value is around 80.\n\n"); |
| 509 | printf("Try -longhelp for an exhaustive list of advanced options.\n"); |
| 510 | } |
| 511 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 512 | static void HelpLong(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 513 | printf("Usage:\n"); |
| 514 | printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n"); |
| 515 | printf("If input size (-s) for an image is not specified, " |
James Zern | 12f9aed | 2012-07-13 14:55:39 -0700 | [diff] [blame] | 516 | "it is assumed to be a PNG, JPEG or TIFF file.\n"); |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 517 | #ifdef HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 518 | printf("Windows builds can take as input any of the files handled by WIC\n"); |
| 519 | #endif |
| 520 | printf("options:\n"); |
| 521 | printf(" -h / -help ............ short help\n"); |
| 522 | printf(" -H / -longhelp ........ long help\n"); |
| 523 | printf(" -q <float> ............. quality factor (0:small..100:big)\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 524 | printf(" -alpha_q <int> ......... Transparency-compression quality " |
| 525 | "(0..100).\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 526 | printf(" -preset <string> ....... Preset setting, one of:\n"); |
| 527 | printf(" default, photo, picture,\n"); |
| 528 | printf(" drawing, icon, text\n"); |
| 529 | printf(" -preset must come first, as it overwrites other parameters."); |
| 530 | printf("\n"); |
| 531 | printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n"); |
| 532 | printf(" -segments <int> ........ number of segments to use (1..4)\n"); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 533 | printf(" -size <int> ............ Target size (in bytes)\n"); |
| 534 | printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 535 | printf("\n"); |
| 536 | printf(" -s <int> <int> ......... Input size (width x height) for YUV\n"); |
| 537 | printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n"); |
| 538 | printf(" -f <int> ............... filter strength (0=off..100)\n"); |
| 539 | printf(" -sharpness <int> ....... " |
| 540 | "filter sharpness (0:most .. 7:least sharp)\n"); |
| 541 | printf(" -strong ................ use strong filter instead of simple.\n"); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 542 | printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n"); |
| 543 | printf(" " |
| 544 | "the first partition (0=no degradation ... 100=full)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 545 | printf(" -pass <int> ............ analysis pass number (1..10)\n"); |
| 546 | printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n"); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 547 | printf(" -resize <w> <h> ........ resize picture (after any cropping)\n"); |
| 548 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 549 | printf(" -444 / -422 / -gray ..... Change colorspace\n"); |
| 550 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 551 | printf(" -map <int> ............. print map of extra info.\n"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 552 | printf(" -print_psnr ............ prints averaged PSNR distortion.\n"); |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 553 | printf(" -print_ssim ............ prints averaged SSIM distortion.\n"); |
| 554 | printf(" -print_lsim ............ prints local-similarity distortion.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 555 | printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 556 | printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n"); |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 557 | printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n"); |
| 558 | printf(" One of: none, fast (default) or best.\n"); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 559 | printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 560 | printf(" -noalpha ............... discard any transparency information.\n"); |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 561 | printf(" -lossless .............. Encode image losslessly.\n"); |
| 562 | printf(" -hint <string> ......... Specify image characteristics hint.\n"); |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 563 | printf(" One of: photo, picture or graph\n"); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 564 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 565 | printf("\n"); |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 566 | printf(" -metadata <string> ..... comma separated list of metadata to\n"); |
| 567 | printf(" "); |
| 568 | printf("copy from the input to the output if present.\n"); |
| 569 | printf(" " |
| 570 | "Valid values: all, none (default), exif, iccp, xmp\n"); |
| 571 | |
| 572 | printf("\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 573 | printf(" -short ................. condense printed message\n"); |
| 574 | printf(" -quiet ................. don't print anything.\n"); |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 575 | printf(" -version ............... print version number and exit.\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 576 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 577 | printf(" -noasm ................. disable all assembly optimizations.\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 578 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 579 | printf(" -v ..................... verbose, e.g. print encoding/decoding " |
| 580 | "times\n"); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 581 | printf(" -progress .............. report encoding progress\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 582 | printf("\n"); |
| 583 | printf("Experimental Options:\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 584 | printf(" -af .................... auto-adjust filter strength.\n"); |
| 585 | printf(" -pre <int> ............. pre-processing filter\n"); |
| 586 | printf("\n"); |
| 587 | } |
| 588 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 589 | //------------------------------------------------------------------------------ |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 590 | // Error messages |
| 591 | |
| 592 | static const char* const kErrorMessages[] = { |
| 593 | "OK", |
| 594 | "OUT_OF_MEMORY: Out of memory allocating objects", |
| 595 | "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer", |
| 596 | "NULL_PARAMETER: NULL parameter passed to function", |
| 597 | "INVALID_CONFIGURATION: configuration is invalid", |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 598 | "BAD_DIMENSION: Bad picture dimension. Maximum width and height " |
| 599 | "allowed is 16383 pixels.", |
| 600 | "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n" |
| 601 | "To reduce the size of this partition, try using less segments " |
| 602 | "with the -segments option, and eventually reduce the number of " |
| 603 | "header bits using -partition_limit. More details are available " |
| 604 | "in the manual (`man cwebp`)", |
| 605 | "PARTITION_OVERFLOW: Partition is too big to fit 16M", |
Pascal Massimino | d71fbdc | 2011-12-01 03:34:22 -0800 | [diff] [blame] | 606 | "BAD_WRITE: Picture writer returned an I/O error", |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 607 | "FILE_TOO_BIG: File would be too big to fit in 4G", |
| 608 | "USER_ABORT: encoding abort requested by user" |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 609 | }; |
| 610 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 611 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 612 | |
| 613 | int main(int argc, const char *argv[]) { |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 614 | int return_value = -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 615 | const char *in_file = NULL, *out_file = NULL, *dump_file = NULL; |
| 616 | FILE *out = NULL; |
| 617 | int c; |
| 618 | int short_output = 0; |
| 619 | int quiet = 0; |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 620 | int keep_alpha = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 621 | int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0; |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 622 | int resize_w = 0, resize_h = 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 623 | int show_progress = 0; |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 624 | int keep_metadata = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 625 | WebPPicture picture; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 626 | int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 627 | WebPPicture original_picture; // when PSNR or SSIM is requested |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 628 | WebPConfig config; |
| 629 | WebPAuxStats stats; |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 630 | WebPMemoryWriter memory_writer; |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 631 | Metadata metadata; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 632 | Stopwatch stop_watch; |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 633 | |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 634 | MetadataInit(&metadata); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 635 | if (!WebPPictureInit(&picture) || |
| 636 | !WebPPictureInit(&original_picture) || |
| 637 | !WebPConfigInit(&config)) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 638 | fprintf(stderr, "Error! Version mismatch!\n"); |
James Zern | 256afef | 2012-07-27 18:56:55 -0700 | [diff] [blame] | 639 | return -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | if (argc == 1) { |
| 643 | HelpShort(); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | for (c = 1; c < argc; ++c) { |
| 648 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 649 | HelpShort(); |
| 650 | return 0; |
| 651 | } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) { |
| 652 | HelpLong(); |
| 653 | return 0; |
| 654 | } else if (!strcmp(argv[c], "-o") && c < argc - 1) { |
| 655 | out_file = argv[++c]; |
| 656 | } else if (!strcmp(argv[c], "-d") && c < argc - 1) { |
| 657 | dump_file = argv[++c]; |
| 658 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 659 | } else if (!strcmp(argv[c], "-print_psnr")) { |
| 660 | config.show_compressed = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 661 | print_distortion = 0; |
| 662 | } else if (!strcmp(argv[c], "-print_ssim")) { |
| 663 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 664 | print_distortion = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 665 | } else if (!strcmp(argv[c], "-print_lsim")) { |
| 666 | config.show_compressed = 1; |
| 667 | print_distortion = 2; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 668 | } else if (!strcmp(argv[c], "-short")) { |
| 669 | short_output++; |
| 670 | } else if (!strcmp(argv[c], "-s") && c < argc - 2) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 671 | picture.width = strtol(argv[++c], NULL, 0); |
| 672 | picture.height = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 673 | } else if (!strcmp(argv[c], "-m") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 674 | config.method = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 675 | } else if (!strcmp(argv[c], "-q") && c < argc - 1) { |
Mikolaj Zalewski | 2aa6b80 | 2011-06-28 16:02:56 +0200 | [diff] [blame] | 676 | config.quality = (float)strtod(argv[++c], NULL); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 677 | } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) { |
| 678 | config.alpha_quality = strtol(argv[++c], NULL, 0); |
| 679 | } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) { |
| 680 | config.alpha_compression = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 681 | } else if (!strcmp(argv[c], "-alpha_cleanup")) { |
| 682 | keep_alpha = keep_alpha ? 2 : 0; |
Vikas Arora | 252028a | 2012-01-05 13:04:30 +0530 | [diff] [blame] | 683 | } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) { |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 684 | ++c; |
| 685 | if (!strcmp(argv[c], "none")) { |
| 686 | config.alpha_filtering = 0; |
| 687 | } else if (!strcmp(argv[c], "fast")) { |
| 688 | config.alpha_filtering = 1; |
| 689 | } else if (!strcmp(argv[c], "best")) { |
| 690 | config.alpha_filtering = 2; |
| 691 | } else { |
| 692 | fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]); |
| 693 | goto Error; |
| 694 | } |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 695 | } else if (!strcmp(argv[c], "-noalpha")) { |
| 696 | keep_alpha = 0; |
Pascal Massimino | 72920ca | 2012-04-24 10:59:08 +0000 | [diff] [blame] | 697 | } else if (!strcmp(argv[c], "-lossless")) { |
| 698 | config.lossless = 1; |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 699 | picture.use_argb = 1; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 700 | } else if (!strcmp(argv[c], "-hint") && c < argc - 1) { |
| 701 | ++c; |
| 702 | if (!strcmp(argv[c], "photo")) { |
| 703 | config.image_hint = WEBP_HINT_PHOTO; |
| 704 | } else if (!strcmp(argv[c], "picture")) { |
| 705 | config.image_hint = WEBP_HINT_PICTURE; |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 706 | } else if (!strcmp(argv[c], "graph")) { |
| 707 | config.image_hint = WEBP_HINT_GRAPH; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 708 | } else { |
| 709 | fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]); |
| 710 | goto Error; |
| 711 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 712 | } else if (!strcmp(argv[c], "-size") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 713 | config.target_size = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 714 | } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) { |
Mikolaj Zalewski | 2aa6b80 | 2011-06-28 16:02:56 +0200 | [diff] [blame] | 715 | config.target_PSNR = (float)strtod(argv[++c], NULL); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 716 | } else if (!strcmp(argv[c], "-sns") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 717 | config.sns_strength = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 718 | } else if (!strcmp(argv[c], "-f") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 719 | config.filter_strength = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 720 | } else if (!strcmp(argv[c], "-af")) { |
| 721 | config.autofilter = 1; |
Pascal Massimino | 842c009 | 2011-05-05 18:10:08 -0700 | [diff] [blame] | 722 | } else if (!strcmp(argv[c], "-strong")) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 723 | config.filter_type = 1; |
| 724 | } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 725 | config.filter_sharpness = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 726 | } else if (!strcmp(argv[c], "-pass") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 727 | config.pass = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 728 | } else if (!strcmp(argv[c], "-pre") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 729 | config.preprocessing = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 730 | } else if (!strcmp(argv[c], "-segments") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 731 | config.segments = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 732 | } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) { |
| 733 | config.partition_limit = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 734 | } else if (!strcmp(argv[c], "-map") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 735 | picture.extra_info_type = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 736 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 737 | } else if (!strcmp(argv[c], "-444")) { |
| 738 | picture.colorspace = WEBP_YUV444; |
| 739 | } else if (!strcmp(argv[c], "-422")) { |
| 740 | picture.colorspace = WEBP_YUV422; |
| 741 | } else if (!strcmp(argv[c], "-gray")) { |
| 742 | picture.colorspace = WEBP_YUV400; |
| 743 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 744 | } else if (!strcmp(argv[c], "-crop") && c < argc - 4) { |
| 745 | crop = 1; |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 746 | crop_x = strtol(argv[++c], NULL, 0); |
| 747 | crop_y = strtol(argv[++c], NULL, 0); |
| 748 | crop_w = strtol(argv[++c], NULL, 0); |
| 749 | crop_h = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 750 | } else if (!strcmp(argv[c], "-resize") && c < argc - 2) { |
| 751 | resize_w = strtol(argv[++c], NULL, 0); |
| 752 | resize_h = strtol(argv[++c], NULL, 0); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 753 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 754 | } else if (!strcmp(argv[c], "-noasm")) { |
Pascal Massimino | e06ac08 | 2011-09-02 21:30:08 +0000 | [diff] [blame] | 755 | VP8GetCPUInfo = NULL; |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 756 | #endif |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 757 | } else if (!strcmp(argv[c], "-version")) { |
| 758 | const int version = WebPGetEncoderVersion(); |
| 759 | printf("%d.%d.%d\n", |
| 760 | (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff); |
| 761 | return 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 762 | } else if (!strcmp(argv[c], "-progress")) { |
| 763 | show_progress = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 764 | } else if (!strcmp(argv[c], "-quiet")) { |
| 765 | quiet = 1; |
| 766 | } else if (!strcmp(argv[c], "-preset") && c < argc - 1) { |
| 767 | WebPPreset preset; |
| 768 | ++c; |
| 769 | if (!strcmp(argv[c], "default")) { |
| 770 | preset = WEBP_PRESET_DEFAULT; |
| 771 | } else if (!strcmp(argv[c], "photo")) { |
| 772 | preset = WEBP_PRESET_PHOTO; |
| 773 | } else if (!strcmp(argv[c], "picture")) { |
| 774 | preset = WEBP_PRESET_PICTURE; |
| 775 | } else if (!strcmp(argv[c], "drawing")) { |
| 776 | preset = WEBP_PRESET_DRAWING; |
| 777 | } else if (!strcmp(argv[c], "icon")) { |
| 778 | preset = WEBP_PRESET_ICON; |
| 779 | } else if (!strcmp(argv[c], "text")) { |
| 780 | preset = WEBP_PRESET_TEXT; |
| 781 | } else { |
| 782 | fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]); |
| 783 | goto Error; |
| 784 | } |
| 785 | if (!WebPConfigPreset(&config, preset, config.quality)) { |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 786 | fprintf(stderr, "Error! Could initialize configuration with preset.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 787 | goto Error; |
| 788 | } |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 789 | } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) { |
| 790 | static const struct { |
| 791 | const char* option; |
| 792 | int flag; |
| 793 | } kTokens[] = { |
| 794 | { "all", METADATA_ALL }, |
| 795 | { "none", 0 }, |
| 796 | { "exif", METADATA_EXIF }, |
| 797 | { "iccp", METADATA_ICCP }, |
| 798 | { "xmp", METADATA_XMP }, |
| 799 | }; |
| 800 | const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]); |
| 801 | const char* start = argv[++c]; |
| 802 | const char* const end = start + strlen(start); |
| 803 | |
| 804 | while (start < end) { |
| 805 | size_t i; |
| 806 | const char* token = strchr(start, ','); |
| 807 | if (token == NULL) token = end; |
| 808 | |
| 809 | for (i = 0; i < kNumTokens; ++i) { |
| 810 | if ((size_t)(token - start) == strlen(kTokens[i].option) && |
| 811 | !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) { |
| 812 | if (kTokens[i].flag != 0) { |
| 813 | keep_metadata |= kTokens[i].flag; |
| 814 | } else { |
| 815 | keep_metadata = 0; |
| 816 | } |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | if (i == kNumTokens) { |
| 821 | fprintf(stderr, "Error! Unknown metadata type '%.*s'\n", |
| 822 | (int)(token - start), start); |
| 823 | HelpLong(); |
| 824 | return -1; |
| 825 | } |
| 826 | start = token + 1; |
| 827 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 828 | #ifdef HAVE_WINCODEC_H |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 829 | if (keep_metadata != 0) { |
| 830 | // TODO(jzern): remove when -metadata is supported on all platforms. |
| 831 | fprintf(stderr, "Warning: -metadata is currently unsupported on this" |
| 832 | " platform. Ignoring this option!\n"); |
| 833 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 834 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 835 | } else if (!strcmp(argv[c], "-v")) { |
| 836 | verbose = 1; |
| 837 | } else if (argv[c][0] == '-') { |
| 838 | fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]); |
| 839 | HelpLong(); |
| 840 | return -1; |
| 841 | } else { |
| 842 | in_file = argv[c]; |
| 843 | } |
| 844 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 845 | if (in_file == NULL) { |
| 846 | fprintf(stderr, "No input file specified!\n"); |
| 847 | HelpShort(); |
| 848 | goto Error; |
| 849 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 850 | |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 851 | // Check for unsupported command line options for lossless mode and log |
| 852 | // warning for such options. |
Pascal Massimino | 0275159 | 2012-06-20 09:20:34 +0000 | [diff] [blame] | 853 | if (!quiet && config.lossless == 1) { |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 854 | if (config.target_size > 0 || config.target_PSNR > 0) { |
| 855 | fprintf(stderr, "Encoding for specified size or PSNR is not supported" |
| 856 | " for lossless encoding. Ignoring such option(s)!\n"); |
| 857 | } |
| 858 | if (config.partition_limit > 0) { |
| 859 | fprintf(stderr, "Partition limit option is not required for lossless" |
| 860 | " encoding. Ignoring this option!\n"); |
| 861 | } |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 862 | } |
| 863 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 864 | if (!WebPValidateConfig(&config)) { |
| 865 | fprintf(stderr, "Error! Invalid configuration.\n"); |
| 866 | goto Error; |
| 867 | } |
| 868 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 869 | // Read the input |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 870 | if (verbose) { |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 871 | StopwatchReadAndReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 872 | } |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 873 | if (!ReadPicture(in_file, &picture, keep_alpha, |
| 874 | (keep_metadata == 0) ? NULL : &metadata)) { |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 875 | fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 876 | goto Error; |
| 877 | } |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 878 | picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL; |
| 879 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 880 | if (verbose) { |
| 881 | const double time = StopwatchReadAndReset(&stop_watch); |
| 882 | fprintf(stderr, "Time to read input: %.3fs\n", time); |
| 883 | } |
| 884 | |
| 885 | // Open the output |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 886 | if (out_file) { |
| 887 | out = fopen(out_file, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 888 | if (out == NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 889 | fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file); |
| 890 | goto Error; |
| 891 | } else { |
| 892 | if (!short_output && !quiet) { |
| 893 | fprintf(stderr, "Saving file '%s'\n", out_file); |
| 894 | } |
| 895 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 896 | if (keep_metadata == 0) { |
| 897 | picture.writer = MyWriter; |
| 898 | picture.custom_ptr = (void*)out; |
| 899 | } else { |
| 900 | WebPMemoryWriterInit(&memory_writer); |
| 901 | picture.writer = WebPMemoryWrite; |
| 902 | picture.custom_ptr = (void*)&memory_writer; |
| 903 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 904 | } else { |
| 905 | out = NULL; |
| 906 | if (!quiet && !short_output) { |
| 907 | fprintf(stderr, "No output file specified (no -o flag). Encoding will\n"); |
| 908 | fprintf(stderr, "be performed, but its results discarded.\n\n"); |
| 909 | } |
| 910 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 911 | if (!quiet) { |
| 912 | picture.stats = &stats; |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 913 | picture.user_data = (void*)in_file; |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 914 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 915 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 916 | // Compress |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 917 | if (verbose) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 918 | StopwatchReadAndReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 919 | } |
Pascal Massimino | 31b68fe | 2012-06-21 00:30:43 -0700 | [diff] [blame] | 920 | if (crop != 0) { |
| 921 | // We use self-cropping using a view. |
| 922 | if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) { |
| 923 | fprintf(stderr, "Error! Cannot crop picture\n"); |
| 924 | goto Error; |
| 925 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 926 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 927 | if ((resize_w | resize_h) > 0) { |
| 928 | if (!WebPPictureRescale(&picture, resize_w, resize_h)) { |
| 929 | fprintf(stderr, "Error! Cannot resize picture\n"); |
| 930 | goto Error; |
| 931 | } |
| 932 | } |
| 933 | if (picture.extra_info_type > 0) { |
| 934 | AllocExtraInfo(&picture); |
| 935 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 936 | if (print_distortion >= 0) { // Save original picture for later comparison |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 937 | WebPPictureCopy(&picture, &original_picture); |
| 938 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 939 | if (!WebPEncode(&config, &picture)) { |
| 940 | fprintf(stderr, "Error! Cannot encode picture as WebP\n"); |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 941 | fprintf(stderr, "Error code: %d (%s)\n", |
| 942 | picture.error_code, kErrorMessages[picture.error_code]); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 943 | goto Error; |
| 944 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 945 | if (verbose) { |
| 946 | const double time = StopwatchReadAndReset(&stop_watch); |
| 947 | fprintf(stderr, "Time to encode picture: %.3fs\n", time); |
| 948 | } |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 949 | |
| 950 | // Write info |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 951 | if (dump_file) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 952 | if (picture.use_argb) { |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 953 | fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode."); |
| 954 | } else if (!DumpPicture(&picture, dump_file)) { |
| 955 | fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file); |
| 956 | } |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 957 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 958 | |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 959 | if (keep_metadata != 0 && out != NULL) { |
| 960 | if (!WriteWebPWithMetadata(out, &picture, &memory_writer, |
| 961 | &metadata, keep_metadata)) { |
| 962 | fprintf(stderr, "Error writing WebP file with metadata!\n"); |
| 963 | goto Error; |
| 964 | } |
| 965 | } |
| 966 | |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 967 | if (!quiet) { |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 968 | if (config.lossless) { |
| 969 | PrintExtraInfoLossless(&picture, short_output, in_file); |
| 970 | } else { |
| 971 | PrintExtraInfoLossy(&picture, short_output, in_file); |
| 972 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 973 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 974 | if (!quiet && !short_output && print_distortion >= 0) { // print distortion |
| 975 | static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" }; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 976 | float values[5]; |
| 977 | WebPPictureDistortion(&picture, &original_picture, |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 978 | print_distortion, values); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 979 | fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n", |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 980 | distortion_names[print_distortion], |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 981 | values[0], values[1], values[2], values[3], values[4]); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 982 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 983 | return_value = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 984 | |
| 985 | Error: |
| 986 | free(picture.extra_info); |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 987 | MetadataFree(&metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 988 | WebPPictureFree(&picture); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 989 | WebPPictureFree(&original_picture); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 990 | if (out != NULL) { |
| 991 | fclose(out); |
| 992 | } |
| 993 | |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 994 | return return_value; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 995 | } |
| 996 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 997 | //------------------------------------------------------------------------------ |