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