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 | // |
James Zern | d640614 | 2013-06-06 23:05:58 -0700 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the COPYING file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // simple command line calling the WebPEncode function. |
| 11 | // Encodes a raw .YUV into WebP bitstream |
| 12 | // |
| 13 | // Author: Skal (pascal.massimino@gmail.com) |
| 14 | |
| 15 | #include <stdio.h> |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 16 | #include <stdlib.h> |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 17 | #include <string.h> |
| 18 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
James Zern | 32b3137 | 2014-06-10 17:53:44 -0700 | [diff] [blame] | 20 | #include "webp/config.h" |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 21 | #endif |
| 22 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 23 | #include "webp/encode.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 24 | |
James Zern | e9bfb11 | 2014-08-29 19:11:41 -0700 | [diff] [blame] | 25 | #include "./example_util.h" |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 26 | #include "./metadata.h" |
James Zern | 00b29e2 | 2012-05-15 13:48:11 -0700 | [diff] [blame] | 27 | #include "./stopwatch.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 28 | |
| 29 | #include "./jpegdec.h" |
| 30 | #include "./pngdec.h" |
James Zern | 1c1c564 | 2012-12-06 14:04:36 -0800 | [diff] [blame] | 31 | #include "./tiffdec.h" |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 32 | #include "./webpdec.h" |
James Zern | a452a55 | 2013-01-22 18:28:52 -0800 | [diff] [blame] | 33 | #include "./wicdec.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 34 | |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 35 | #ifndef WEBP_DLL |
James Zern | 605a712 | 2013-11-25 14:43:12 -0800 | [diff] [blame] | 36 | #ifdef __cplusplus |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 37 | extern "C" { |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 38 | #endif |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 39 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 40 | extern void* VP8GetCPUInfo; // opaque forward declaration. |
| 41 | |
James Zern | 605a712 | 2013-11-25 14:43:12 -0800 | [diff] [blame] | 42 | #ifdef __cplusplus |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 43 | } // extern "C" |
| 44 | #endif |
| 45 | #endif // WEBP_DLL |
| 46 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 47 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 48 | |
| 49 | static int verbose = 0; |
| 50 | |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 51 | static int ReadYUV(const char filename[], WebPPicture* const pic) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 52 | const int use_argb = pic->use_argb; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 53 | const int uv_width = (pic->width + 1) / 2; |
| 54 | const int uv_height = (pic->height + 1) / 2; |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 55 | const int uv_plane_size = uv_width * uv_height; |
| 56 | const uint8_t* data = NULL; |
| 57 | size_t data_size = 0; |
| 58 | const size_t expected_data_size = |
| 59 | pic->width * pic->height + 2 * uv_plane_size; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 60 | int ok = 0; |
| 61 | |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 62 | pic->use_argb = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 63 | |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 64 | ok = ExUtilReadFile(filename, &data, &data_size); |
| 65 | if (!ok) goto End; |
| 66 | |
| 67 | if (data_size != expected_data_size) { |
| 68 | fprintf(stderr, |
| 69 | "file '%s' doesn't have the expected size (%d instead of %d)\n", |
| 70 | filename, (int)data_size, (int)expected_data_size); |
| 71 | ok = 0; |
| 72 | goto End; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 73 | } |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 74 | pic->y_stride = pic->width; |
| 75 | pic->uv_stride = uv_width; |
| 76 | pic->y = (uint8_t*)data; |
| 77 | pic->u = pic->y + pic->height * pic->y_stride; |
| 78 | pic->v = pic->u + uv_plane_size; |
| 79 | // Grab ownership 'data' |
| 80 | pic->memory_ = (void*)data; |
| 81 | data = NULL; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 82 | ok = 1; |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 83 | |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 84 | if (use_argb) ok = WebPPictureYUVAToARGB(pic); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 85 | |
| 86 | End: |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 87 | free((void*)data); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 88 | return ok; |
| 89 | } |
| 90 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 91 | #ifdef HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 92 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 93 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 94 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 95 | int ok; |
| 96 | if (pic->width != 0 && pic->height != 0) { |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 97 | ok = ReadYUV(filename, pic); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 98 | } else { |
| 99 | // If no size specified, try to decode it using WIC. |
James Zern | e83ff7d | 2013-01-24 15:37:49 -0800 | [diff] [blame] | 100 | ok = ReadPictureWithWIC(filename, pic, keep_alpha, metadata); |
James Zern | 7d039fc | 2014-05-24 18:30:33 -0700 | [diff] [blame] | 101 | if (!ok) { |
| 102 | ok = ReadWebP(filename, pic, keep_alpha, metadata); |
| 103 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 104 | } |
| 105 | if (!ok) { |
| 106 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 107 | } |
| 108 | return ok; |
| 109 | } |
| 110 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 111 | #else // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 112 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 113 | typedef enum { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 114 | PNG_ = 0, |
| 115 | JPEG_, |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 116 | TIFF_, // 'TIFF' clashes with libtiff |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 117 | WEBP_, |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 118 | UNSUPPORTED |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 119 | } InputFileFormat; |
| 120 | |
| 121 | static InputFileFormat GetImageType(FILE* in_file) { |
| 122 | InputFileFormat format = UNSUPPORTED; |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 123 | uint32_t magic1, magic2; |
| 124 | uint8_t buf[12]; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 125 | |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 126 | if ((fread(&buf[0], 12, 1, in_file) != 1) || |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 127 | (fseek(in_file, 0, SEEK_SET) != 0)) { |
| 128 | return format; |
| 129 | } |
| 130 | |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 131 | magic1 = ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
| 132 | magic2 = ((uint32_t)buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11]; |
| 133 | if (magic1 == 0x89504E47U) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 134 | format = PNG_; |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 135 | } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 136 | format = JPEG_; |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 137 | } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) { |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 138 | format = TIFF_; |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 139 | } else if (magic1 == 0x52494646 && magic2 == 0x57454250) { |
| 140 | format = WEBP_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 141 | } |
| 142 | return format; |
| 143 | } |
| 144 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 145 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 146 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 147 | int ok = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 148 | if (pic->width == 0 || pic->height == 0) { |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 149 | InputFileFormat format; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 150 | // If no size specified, try to decode it as PNG/JPEG (as appropriate). |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 151 | FILE* in_file = fopen(filename, "rb"); |
| 152 | if (in_file == NULL) { |
| 153 | fprintf(stderr, "Error! Cannot open input file '%s'\n", filename); |
| 154 | return ok; |
| 155 | } |
| 156 | format = GetImageType(in_file); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 157 | if (format == PNG_) { |
James Zern | dba64d9 | 2012-12-04 19:00:30 -0800 | [diff] [blame] | 158 | ok = ReadPNG(in_file, pic, keep_alpha, metadata); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 159 | } else if (format == JPEG_) { |
James Zern | 2c72496 | 2012-12-16 19:13:56 -0800 | [diff] [blame] | 160 | ok = ReadJPEG(in_file, pic, metadata); |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 161 | } else if (format == TIFF_) { |
James Zern | 2914ecf | 2012-12-15 19:41:03 -0800 | [diff] [blame] | 162 | ok = ReadTIFF(filename, pic, keep_alpha, metadata); |
James Zern | ddeb6ac | 2014-04-22 19:30:27 -0700 | [diff] [blame] | 163 | } else if (format == WEBP_) { |
| 164 | ok = ReadWebP(filename, pic, keep_alpha, metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 165 | } |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 166 | fclose(in_file); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 167 | } else { |
| 168 | // If image size is specified, infer it as YUV format. |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 169 | ok = ReadYUV(filename, pic); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 170 | } |
| 171 | if (!ok) { |
| 172 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 173 | } |
| 174 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 175 | return ok; |
| 176 | } |
| 177 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 178 | #endif // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 179 | |
| 180 | static void AllocExtraInfo(WebPPicture* const pic) { |
| 181 | const int mb_w = (pic->width + 15) / 16; |
| 182 | const int mb_h = (pic->height + 15) / 16; |
| 183 | pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info)); |
| 184 | } |
| 185 | |
| 186 | static void PrintByteCount(const int bytes[4], int total_size, |
| 187 | int* const totals) { |
| 188 | int s; |
| 189 | int total = 0; |
| 190 | for (s = 0; s < 4; ++s) { |
| 191 | fprintf(stderr, "| %7d ", bytes[s]); |
| 192 | total += bytes[s]; |
| 193 | if (totals) totals[s] += bytes[s]; |
| 194 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 195 | fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void PrintPercents(const int counts[4], int total) { |
| 199 | int s; |
| 200 | for (s = 0; s < 4; ++s) { |
| 201 | fprintf(stderr, "| %2d%%", 100 * counts[s] / total); |
| 202 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 203 | fprintf(stderr, "| %7d\n", total); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void PrintValues(const int values[4]) { |
| 207 | int s; |
| 208 | for (s = 0; s < 4; ++s) { |
| 209 | fprintf(stderr, "| %7d ", values[s]); |
| 210 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 211 | fprintf(stderr, "|\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 214 | static void PrintFullLosslessInfo(const WebPAuxStats* const stats, |
| 215 | const char* const description) { |
| 216 | fprintf(stderr, "Lossless-%s compressed size: %d bytes\n", |
| 217 | description, stats->lossless_size); |
Vikas Arora | b901416 | 2014-09-17 14:11:52 -0700 | [diff] [blame] | 218 | fprintf(stderr, " * Header size: %d bytes, image data size: %d\n", |
| 219 | stats->lossless_hdr_size, stats->lossless_data_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 220 | if (stats->lossless_features) { |
| 221 | fprintf(stderr, " * Lossless features used:"); |
| 222 | if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION"); |
| 223 | if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM"); |
| 224 | if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN"); |
| 225 | if (stats->lossless_features & 8) fprintf(stderr, " PALETTE"); |
| 226 | fprintf(stderr, "\n"); |
| 227 | } |
| 228 | fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n", |
| 229 | stats->histogram_bits, stats->transform_bits, stats->cache_bits); |
| 230 | if (stats->palette_size > 0) { |
| 231 | fprintf(stderr, " * Palette size: %d\n", stats->palette_size); |
| 232 | } |
| 233 | } |
| 234 | |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 235 | static void PrintExtraInfoLossless(const WebPPicture* const pic, |
| 236 | int short_output, |
| 237 | const char* const file_name) { |
| 238 | const WebPAuxStats* const stats = pic->stats; |
| 239 | if (short_output) { |
| 240 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
| 241 | } else { |
| 242 | fprintf(stderr, "File: %s\n", file_name); |
| 243 | fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height); |
| 244 | fprintf(stderr, "Output: %d bytes\n", stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 245 | PrintFullLosslessInfo(stats, "ARGB"); |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output, |
skal | 9bfbdd1 | 2013-03-12 00:37:42 +0100 | [diff] [blame] | 250 | int full_details, |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 251 | const char* const file_name) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 252 | const WebPAuxStats* const stats = pic->stats; |
| 253 | if (short_output) { |
| 254 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 255 | } else { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 256 | const int num_i4 = stats->block_count[0]; |
| 257 | const int num_i16 = stats->block_count[1]; |
| 258 | const int num_skip = stats->block_count[2]; |
| 259 | const int total = num_i4 + num_i16; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 260 | fprintf(stderr, "File: %s\n", file_name); |
| 261 | fprintf(stderr, "Dimension: %d x %d%s\n", |
James Zern | c71ff9e | 2012-06-07 17:32:29 -0700 | [diff] [blame] | 262 | pic->width, pic->height, |
| 263 | stats->alpha_data_size ? " (with alpha)" : ""); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 264 | fprintf(stderr, "Output: " |
| 265 | "%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] | 266 | stats->coded_size, |
| 267 | stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]); |
| 268 | if (total > 0) { |
| 269 | int totals[4] = { 0, 0, 0, 0 }; |
| 270 | fprintf(stderr, "block count: intra4: %d\n" |
| 271 | " intra16: %d (-> %.2f%%)\n", |
| 272 | num_i4, num_i16, 100.f * num_i16 / total); |
| 273 | fprintf(stderr, " skipped block: %d (%.2f%%)\n", |
| 274 | num_skip, 100.f * num_skip / total); |
| 275 | fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n" |
| 276 | " mode-partition: %6d (%.1f%%)\n", |
| 277 | stats->header_bytes[0], |
| 278 | 100.f * stats->header_bytes[0] / stats->coded_size, |
| 279 | stats->header_bytes[1], |
| 280 | 100.f * stats->header_bytes[1] / stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 281 | if (stats->alpha_data_size > 0) { |
| 282 | fprintf(stderr, " transparency: %6d (%.1f dB)\n", |
| 283 | stats->alpha_data_size, stats->PSNR[4]); |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 284 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 285 | fprintf(stderr, " Residuals bytes " |
| 286 | "|segment 1|segment 2|segment 3" |
| 287 | "|segment 4| total\n"); |
skal | 9bfbdd1 | 2013-03-12 00:37:42 +0100 | [diff] [blame] | 288 | if (full_details) { |
| 289 | fprintf(stderr, " intra4-coeffs: "); |
| 290 | PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals); |
| 291 | fprintf(stderr, " intra16-coeffs: "); |
| 292 | PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals); |
| 293 | fprintf(stderr, " chroma coeffs: "); |
| 294 | PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals); |
| 295 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 296 | fprintf(stderr, " macroblocks: "); |
| 297 | PrintPercents(stats->segment_size, total); |
| 298 | fprintf(stderr, " quantizer: "); |
| 299 | PrintValues(stats->segment_quant); |
| 300 | fprintf(stderr, " filter level: "); |
| 301 | PrintValues(stats->segment_level); |
skal | 9bfbdd1 | 2013-03-12 00:37:42 +0100 | [diff] [blame] | 302 | if (full_details) { |
| 303 | fprintf(stderr, "------------------+---------"); |
| 304 | fprintf(stderr, "+---------+---------+---------+-----------------\n"); |
| 305 | fprintf(stderr, " segments total: "); |
| 306 | PrintByteCount(totals, stats->coded_size, NULL); |
| 307 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 308 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 309 | if (stats->lossless_size > 0) { |
| 310 | PrintFullLosslessInfo(stats, "alpha"); |
| 311 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 312 | } |
skal | fff2a11 | 2013-12-23 12:03:00 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void PrintMapInfo(const WebPPicture* const pic) { |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 316 | if (pic->extra_info != NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 317 | const int mb_w = (pic->width + 15) / 16; |
| 318 | const int mb_h = (pic->height + 15) / 16; |
| 319 | const int type = pic->extra_info_type; |
| 320 | int x, y; |
| 321 | for (y = 0; y < mb_h; ++y) { |
| 322 | for (x = 0; x < mb_w; ++x) { |
| 323 | const int c = pic->extra_info[x + y * mb_w]; |
| 324 | if (type == 1) { // intra4/intra16 |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 325 | fprintf(stderr, "%c", "+."[c]); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 326 | } else if (type == 2) { // segments |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 327 | fprintf(stderr, "%c", ".-*X"[c]); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 328 | } else if (type == 3) { // quantizers |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 329 | fprintf(stderr, "%.2d ", c); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 330 | } else if (type == 6 || type == 7) { |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 331 | fprintf(stderr, "%3d ", c); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 332 | } else { |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 333 | fprintf(stderr, "0x%.2x ", c); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 336 | fprintf(stderr, "\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 341 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 342 | |
| 343 | static int MyWriter(const uint8_t* data, size_t data_size, |
| 344 | const WebPPicture* const pic) { |
| 345 | FILE* const out = (FILE*)pic->custom_ptr; |
| 346 | return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1; |
| 347 | } |
| 348 | |
| 349 | // Dumps a picture as a PGM file using the IMC4 layout. |
| 350 | static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) { |
| 351 | int y; |
| 352 | const int uv_width = (picture->width + 1) / 2; |
| 353 | const int uv_height = (picture->height + 1) / 2; |
| 354 | const int stride = (picture->width + 1) & ~1; |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 355 | const int alpha_height = |
| 356 | WebPPictureHasTransparency(picture) ? picture->height : 0; |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 357 | const int height = picture->height + uv_height + alpha_height; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 358 | FILE* const f = fopen(PGM_name, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 359 | if (f == NULL) return 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 360 | fprintf(f, "P5\n%d %d\n255\n", stride, height); |
| 361 | for (y = 0; y < picture->height; ++y) { |
| 362 | if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1) |
| 363 | return 0; |
| 364 | if (picture->width & 1) fputc(0, f); // pad |
| 365 | } |
| 366 | for (y = 0; y < uv_height; ++y) { |
| 367 | if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 368 | return 0; |
| 369 | if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 370 | return 0; |
| 371 | } |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 372 | for (y = 0; y < alpha_height; ++y) { |
| 373 | if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1) |
| 374 | return 0; |
| 375 | if (picture->width & 1) fputc(0, f); // pad |
| 376 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 377 | fclose(f); |
| 378 | return 1; |
| 379 | } |
| 380 | |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 381 | // ----------------------------------------------------------------------------- |
| 382 | // Metadata writing. |
| 383 | |
| 384 | enum { |
| 385 | METADATA_EXIF = (1 << 0), |
James Zern | d8dc72a | 2013-03-13 14:04:20 -0700 | [diff] [blame] | 386 | METADATA_ICC = (1 << 1), |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 387 | METADATA_XMP = (1 << 2), |
James Zern | d8dc72a | 2013-03-13 14:04:20 -0700 | [diff] [blame] | 388 | METADATA_ALL = METADATA_EXIF | METADATA_ICC | METADATA_XMP |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 389 | }; |
| 390 | |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 391 | static const int kChunkHeaderSize = 8; |
| 392 | static const int kTagSize = 4; |
| 393 | |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 394 | static void PrintMetadataInfo(const Metadata* const metadata, |
| 395 | int metadata_written) { |
| 396 | if (metadata == NULL || metadata_written == 0) return; |
| 397 | |
| 398 | fprintf(stderr, "Metadata:\n"); |
James Zern | 7dd288f | 2013-03-15 16:42:58 -0700 | [diff] [blame] | 399 | if (metadata_written & METADATA_ICC) { |
James Zern | 14d42af | 2013-03-20 16:59:35 -0700 | [diff] [blame] | 400 | fprintf(stderr, " * ICC profile: %6d bytes\n", (int)metadata->iccp.size); |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 401 | } |
| 402 | if (metadata_written & METADATA_EXIF) { |
James Zern | 14d42af | 2013-03-20 16:59:35 -0700 | [diff] [blame] | 403 | fprintf(stderr, " * EXIF data: %6d bytes\n", (int)metadata->exif.size); |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 404 | } |
| 405 | if (metadata_written & METADATA_XMP) { |
James Zern | 14d42af | 2013-03-20 16:59:35 -0700 | [diff] [blame] | 406 | fprintf(stderr, " * XMP data: %6d bytes\n", (int)metadata->xmp.size); |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 410 | // Outputs, in little endian, 'num' bytes from 'val' to 'out'. |
| 411 | static int WriteLE(FILE* const out, uint32_t val, int num) { |
| 412 | uint8_t buf[4]; |
| 413 | int i; |
| 414 | for (i = 0; i < num; ++i) { |
| 415 | buf[i] = (uint8_t)(val & 0xff); |
| 416 | val >>= 8; |
| 417 | } |
| 418 | return (fwrite(buf, num, 1, out) == 1); |
| 419 | } |
| 420 | |
| 421 | static int WriteLE24(FILE* const out, uint32_t val) { |
| 422 | return WriteLE(out, val, 3); |
| 423 | } |
| 424 | |
| 425 | static int WriteLE32(FILE* const out, uint32_t val) { |
| 426 | return WriteLE(out, val, 4); |
| 427 | } |
| 428 | |
| 429 | static int WriteMetadataChunk(FILE* const out, const char fourcc[4], |
| 430 | const MetadataPayload* const payload) { |
| 431 | const uint8_t zero = 0; |
| 432 | const size_t need_padding = payload->size & 1; |
| 433 | int ok = (fwrite(fourcc, kTagSize, 1, out) == 1); |
| 434 | ok = ok && WriteLE32(out, (uint32_t)payload->size); |
| 435 | ok = ok && (fwrite(payload->bytes, payload->size, 1, out) == 1); |
| 436 | return ok && (fwrite(&zero, need_padding, need_padding, out) == need_padding); |
| 437 | } |
| 438 | |
| 439 | // Sets 'flag' in 'vp8x_flags' and updates 'metadata_size' with the size of the |
| 440 | // chunk if there is metadata and 'keep' is true. |
| 441 | static int UpdateFlagsAndSize(const MetadataPayload* const payload, |
| 442 | int keep, int flag, |
| 443 | uint32_t* vp8x_flags, uint64_t* metadata_size) { |
| 444 | if (keep && payload->bytes != NULL && payload->size > 0) { |
| 445 | *vp8x_flags |= flag; |
| 446 | *metadata_size += kChunkHeaderSize + payload->size + (payload->size & 1); |
| 447 | return 1; |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | // Writes a WebP file using the image contained in 'memory_writer' and the |
| 453 | // metadata from 'metadata'. Metadata is controlled by 'keep_metadata' and the |
| 454 | // availability in 'metadata'. Returns true on success. |
| 455 | // For details see doc/webp-container-spec.txt#extended-file-format. |
| 456 | static int WriteWebPWithMetadata(FILE* const out, |
| 457 | const WebPPicture* const picture, |
| 458 | const WebPMemoryWriter* const memory_writer, |
| 459 | const Metadata* const metadata, |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 460 | int keep_metadata, |
| 461 | int* const metadata_written) { |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 462 | const char kVP8XHeader[] = "VP8X\x0a\x00\x00\x00"; |
| 463 | const int kAlphaFlag = 0x10; |
| 464 | const int kEXIFFlag = 0x08; |
| 465 | const int kICCPFlag = 0x20; |
| 466 | const int kXMPFlag = 0x04; |
| 467 | const size_t kRiffHeaderSize = 12; |
| 468 | const size_t kMaxChunkPayload = ~0 - kChunkHeaderSize - 1; |
| 469 | const size_t kMinSize = kRiffHeaderSize + kChunkHeaderSize; |
| 470 | uint32_t flags = 0; |
| 471 | uint64_t metadata_size = 0; |
| 472 | const int write_exif = UpdateFlagsAndSize(&metadata->exif, |
| 473 | !!(keep_metadata & METADATA_EXIF), |
| 474 | kEXIFFlag, &flags, &metadata_size); |
| 475 | const int write_iccp = UpdateFlagsAndSize(&metadata->iccp, |
James Zern | d8dc72a | 2013-03-13 14:04:20 -0700 | [diff] [blame] | 476 | !!(keep_metadata & METADATA_ICC), |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 477 | kICCPFlag, &flags, &metadata_size); |
| 478 | const int write_xmp = UpdateFlagsAndSize(&metadata->xmp, |
| 479 | !!(keep_metadata & METADATA_XMP), |
| 480 | kXMPFlag, &flags, &metadata_size); |
| 481 | uint8_t* webp = memory_writer->mem; |
| 482 | size_t webp_size = memory_writer->size; |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 483 | |
| 484 | *metadata_written = 0; |
| 485 | |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 486 | if (webp_size < kMinSize) return 0; |
| 487 | if (webp_size - kChunkHeaderSize + metadata_size > kMaxChunkPayload) { |
| 488 | fprintf(stderr, "Error! Addition of metadata would exceed " |
| 489 | "container size limit.\n"); |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | if (metadata_size > 0) { |
| 494 | const int kVP8XChunkSize = 18; |
| 495 | const int has_vp8x = !memcmp(webp + kRiffHeaderSize, "VP8X", kTagSize); |
| 496 | const uint32_t riff_size = (uint32_t)(webp_size - kChunkHeaderSize + |
| 497 | (has_vp8x ? 0 : kVP8XChunkSize) + |
| 498 | metadata_size); |
| 499 | // RIFF |
| 500 | int ok = (fwrite(webp, kTagSize, 1, out) == 1); |
| 501 | // RIFF size (file header size is not recorded) |
| 502 | ok = ok && WriteLE32(out, riff_size); |
| 503 | webp += kChunkHeaderSize; |
| 504 | webp_size -= kChunkHeaderSize; |
| 505 | // WEBP |
| 506 | ok = ok && (fwrite(webp, kTagSize, 1, out) == 1); |
| 507 | webp += kTagSize; |
| 508 | webp_size -= kTagSize; |
| 509 | if (has_vp8x) { // update the existing VP8X flags |
| 510 | webp[kChunkHeaderSize] |= (uint8_t)(flags & 0xff); |
| 511 | ok = ok && (fwrite(webp, kVP8XChunkSize, 1, out) == 1); |
James Zern | b5b2e3c | 2013-12-19 10:17:08 -0800 | [diff] [blame] | 512 | webp += kVP8XChunkSize; |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 513 | webp_size -= kVP8XChunkSize; |
| 514 | } else { |
| 515 | const int is_lossless = !memcmp(webp, "VP8L", kTagSize); |
Urvang Joshi | 8ba1bf6 | 2013-07-19 11:55:09 -0700 | [diff] [blame] | 516 | if (is_lossless) { |
| 517 | // Presence of alpha is stored in the 29th bit of VP8L data. |
| 518 | if (webp[kChunkHeaderSize + 3] & (1 << 5)) flags |= kAlphaFlag; |
| 519 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 520 | ok = ok && (fwrite(kVP8XHeader, kChunkHeaderSize, 1, out) == 1); |
| 521 | ok = ok && WriteLE32(out, flags); |
| 522 | ok = ok && WriteLE24(out, picture->width - 1); |
| 523 | ok = ok && WriteLE24(out, picture->height - 1); |
| 524 | } |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 525 | if (write_iccp) { |
| 526 | ok = ok && WriteMetadataChunk(out, "ICCP", &metadata->iccp); |
James Zern | 7dd288f | 2013-03-15 16:42:58 -0700 | [diff] [blame] | 527 | *metadata_written |= METADATA_ICC; |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 528 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 529 | // Image |
| 530 | ok = ok && (fwrite(webp, webp_size, 1, out) == 1); |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 531 | if (write_exif) { |
| 532 | ok = ok && WriteMetadataChunk(out, "EXIF", &metadata->exif); |
| 533 | *metadata_written |= METADATA_EXIF; |
| 534 | } |
| 535 | if (write_xmp) { |
| 536 | ok = ok && WriteMetadataChunk(out, "XMP ", &metadata->xmp); |
| 537 | *metadata_written |= METADATA_XMP; |
| 538 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 539 | return ok; |
| 540 | } else { |
| 541 | // No metadata, just write the original image file. |
| 542 | return (fwrite(webp, webp_size, 1, out) == 1); |
| 543 | } |
| 544 | } |
| 545 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 546 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 547 | |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 548 | static int ProgressReport(int percent, const WebPPicture* const picture) { |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 549 | fprintf(stderr, "[%s]: %3d %% \r", |
| 550 | (char*)picture->user_data, percent); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 551 | return 1; // all ok |
| 552 | } |
| 553 | |
| 554 | //------------------------------------------------------------------------------ |
| 555 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 556 | static void HelpShort(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 557 | printf("Usage:\n\n"); |
| 558 | printf(" cwebp [options] -q quality input.png -o output.webp\n\n"); |
| 559 | printf("where quality is between 0 (poor) to 100 (very good).\n"); |
| 560 | printf("Typical value is around 80.\n\n"); |
| 561 | printf("Try -longhelp for an exhaustive list of advanced options.\n"); |
| 562 | } |
| 563 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 564 | static void HelpLong(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 565 | printf("Usage:\n"); |
| 566 | printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 567 | printf("If input size (-s) for an image is not specified, it is\n" |
| 568 | "assumed to be a PNG, JPEG, TIFF or WebP file.\n"); |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 569 | #ifdef HAVE_WINCODEC_H |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 570 | printf("Windows builds can take as input any of the files handled by WIC.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 571 | #endif |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 572 | printf("\nOptions:\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 573 | printf(" -h / -help ............ short help\n"); |
| 574 | printf(" -H / -longhelp ........ long help\n"); |
| 575 | printf(" -q <float> ............. quality factor (0:small..100:big)\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 576 | printf(" -alpha_q <int> ......... transparency-compression quality " |
| 577 | "(0..100)\n"); |
| 578 | printf(" -preset <string> ....... preset setting, one of:\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 579 | printf(" default, photo, picture,\n"); |
| 580 | printf(" drawing, icon, text\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 581 | printf(" -preset must come first, as it overwrites other parameters\n"); |
| 582 | printf(" -z <int> ............... activates lossless preset with given\n" |
skal | 65b99f1 | 2014-03-11 23:25:35 +0100 | [diff] [blame] | 583 | " level in [0:fast, ..., 9:slowest]\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 584 | printf("\n"); |
| 585 | printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n"); |
| 586 | printf(" -segments <int> ........ number of segments to use (1..4)\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 587 | printf(" -size <int> ............ target size (in bytes)\n"); |
| 588 | printf(" -psnr <float> .......... target PSNR (in dB. typically: 42)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 589 | printf("\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 590 | printf(" -s <int> <int> ......... input size (width x height) for YUV\n"); |
| 591 | printf(" -sns <int> ............. spatial noise shaping (0:off, 100:max)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 592 | printf(" -f <int> ............... filter strength (0=off..100)\n"); |
| 593 | printf(" -sharpness <int> ....... " |
| 594 | "filter sharpness (0:most .. 7:least sharp)\n"); |
Pascal Massimino | 92668da | 2013-02-15 01:08:52 -0800 | [diff] [blame] | 595 | printf(" -strong ................ use strong filter instead " |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 596 | "of simple (default)\n"); |
| 597 | printf(" -nostrong .............. use simple filter instead of strong\n"); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 598 | printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n"); |
| 599 | printf(" " |
| 600 | "the first partition (0=no degradation ... 100=full)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 601 | printf(" -pass <int> ............ analysis pass number (1..10)\n"); |
| 602 | 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] | 603 | printf(" -resize <w> <h> ........ resize picture (after any cropping)\n"); |
skal | f817930 | 2013-03-01 01:21:34 +0100 | [diff] [blame] | 604 | printf(" -mt .................... use multi-threading if available\n"); |
skal | 9bfbdd1 | 2013-03-12 00:37:42 +0100 | [diff] [blame] | 605 | printf(" -low_memory ............ reduce memory usage (slower encoding)\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 606 | printf(" -map <int> ............. print map of extra info\n"); |
| 607 | printf(" -print_psnr ............ prints averaged PSNR distortion\n"); |
| 608 | printf(" -print_ssim ............ prints averaged SSIM distortion\n"); |
| 609 | printf(" -print_lsim ............ prints local-similarity distortion\n"); |
| 610 | printf(" -d <file.pgm> .......... dump the compressed output (PGM file)\n"); |
| 611 | printf(" -alpha_method <int> .... transparency-compression method (0..1)\n"); |
| 612 | printf(" -alpha_filter <string> . predictive filtering for alpha plane,\n"); |
| 613 | printf(" one of: none, fast (default) or best\n"); |
| 614 | printf(" -alpha_cleanup ......... clean RGB values in transparent area\n"); |
| 615 | printf(" -blend_alpha <hex> ..... blend colors against background color\n" |
Pascal Massimino | e7d9548 | 2013-04-02 19:14:14 -0700 | [diff] [blame] | 616 | " expressed as RGB values written in\n" |
| 617 | " hexadecimal, e.g. 0xc0e0d0 for red=0xc0\n" |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 618 | " green=0xe0 and blue=0xd0\n"); |
| 619 | printf(" -noalpha ............... discard any transparency information\n"); |
| 620 | printf(" -lossless .............. encode image losslessly\n"); |
Vikas Arora | 98c8138 | 2015-01-29 16:02:09 -0800 | [diff] [blame] | 621 | printf(" -near_lossless <int> ... use near-lossless image\n" |
Vikas Arora | 4c82284 | 2015-02-05 11:16:37 -0800 | [diff] [blame] | 622 | " preprocessing (0..100=off)\n"); |
Mislav Bradac | 48f66b6 | 2015-09-11 12:29:07 +0000 | [diff] [blame] | 623 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 624 | printf(" -delta_palettization ... use delta palettization\n"); |
| 625 | #endif // WEBP_EXPERIMENTAL_FEATURES |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 626 | printf(" -hint <string> ......... specify image characteristics hint,\n"); |
| 627 | printf(" one of: photo, picture or graph\n"); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 628 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 629 | printf("\n"); |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 630 | printf(" -metadata <string> ..... comma separated list of metadata to\n"); |
| 631 | printf(" "); |
| 632 | printf("copy from the input to the output if present.\n"); |
| 633 | printf(" " |
James Zern | d8dc72a | 2013-03-13 14:04:20 -0700 | [diff] [blame] | 634 | "Valid values: all, none (default), exif, icc, xmp\n"); |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 635 | |
| 636 | printf("\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 637 | printf(" -short ................. condense printed message\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 638 | printf(" -quiet ................. don't print anything\n"); |
| 639 | printf(" -version ............... print version number and exit\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 640 | #ifndef WEBP_DLL |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 641 | printf(" -noasm ................. disable all assembly optimizations\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 642 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 643 | printf(" -v ..................... verbose, e.g. print encoding/decoding " |
| 644 | "times\n"); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 645 | printf(" -progress .............. report encoding progress\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 646 | printf("\n"); |
| 647 | printf("Experimental Options:\n"); |
skal | 0a8b886 | 2014-06-18 16:45:34 +0200 | [diff] [blame] | 648 | printf(" -jpeg_like ............. roughly match expected JPEG size\n"); |
| 649 | printf(" -af .................... auto-adjust filter strength\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 650 | printf(" -pre <int> ............. pre-processing filter\n"); |
| 651 | printf("\n"); |
| 652 | } |
| 653 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 654 | //------------------------------------------------------------------------------ |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 655 | // Error messages |
| 656 | |
skal | 0b747b1 | 2014-07-21 15:44:43 +0200 | [diff] [blame] | 657 | static const char* const kErrorMessages[VP8_ENC_ERROR_LAST] = { |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 658 | "OK", |
| 659 | "OUT_OF_MEMORY: Out of memory allocating objects", |
| 660 | "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer", |
| 661 | "NULL_PARAMETER: NULL parameter passed to function", |
| 662 | "INVALID_CONFIGURATION: configuration is invalid", |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 663 | "BAD_DIMENSION: Bad picture dimension. Maximum width and height " |
| 664 | "allowed is 16383 pixels.", |
| 665 | "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n" |
| 666 | "To reduce the size of this partition, try using less segments " |
| 667 | "with the -segments option, and eventually reduce the number of " |
| 668 | "header bits using -partition_limit. More details are available " |
| 669 | "in the manual (`man cwebp`)", |
| 670 | "PARTITION_OVERFLOW: Partition is too big to fit 16M", |
Pascal Massimino | d71fbdc | 2011-12-01 03:34:22 -0800 | [diff] [blame] | 671 | "BAD_WRITE: Picture writer returned an I/O error", |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 672 | "FILE_TOO_BIG: File would be too big to fit in 4G", |
| 673 | "USER_ABORT: encoding abort requested by user" |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 674 | }; |
| 675 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 676 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 677 | |
| 678 | int main(int argc, const char *argv[]) { |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 679 | int return_value = -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 680 | const char *in_file = NULL, *out_file = NULL, *dump_file = NULL; |
| 681 | FILE *out = NULL; |
| 682 | int c; |
| 683 | int short_output = 0; |
| 684 | int quiet = 0; |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 685 | int keep_alpha = 1; |
Pascal Massimino | e7d9548 | 2013-04-02 19:14:14 -0700 | [diff] [blame] | 686 | int blend_alpha = 0; |
| 687 | uint32_t background_color = 0xffffffu; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 688 | 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] | 689 | int resize_w = 0, resize_h = 0; |
skal | 65b99f1 | 2014-03-11 23:25:35 +0100 | [diff] [blame] | 690 | int lossless_preset = 6; |
| 691 | int use_lossless_preset = -1; // -1=unset, 0=don't use, 1=use it |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 692 | int show_progress = 0; |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 693 | int keep_metadata = 0; |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 694 | int metadata_written = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 695 | WebPPicture picture; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 696 | int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 697 | WebPPicture original_picture; // when PSNR or SSIM is requested |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 698 | WebPConfig config; |
| 699 | WebPAuxStats stats; |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 700 | WebPMemoryWriter memory_writer; |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 701 | Metadata metadata; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 702 | Stopwatch stop_watch; |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 703 | |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 704 | MetadataInit(&metadata); |
James Zern | 88d382a | 2013-02-01 19:17:26 -0800 | [diff] [blame] | 705 | WebPMemoryWriterInit(&memory_writer); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 706 | if (!WebPPictureInit(&picture) || |
| 707 | !WebPPictureInit(&original_picture) || |
| 708 | !WebPConfigInit(&config)) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 709 | fprintf(stderr, "Error! Version mismatch!\n"); |
James Zern | 256afef | 2012-07-27 18:56:55 -0700 | [diff] [blame] | 710 | return -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | if (argc == 1) { |
| 714 | HelpShort(); |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | for (c = 1; c < argc; ++c) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 719 | int parse_error = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 720 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 721 | HelpShort(); |
| 722 | return 0; |
| 723 | } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) { |
| 724 | HelpLong(); |
| 725 | return 0; |
| 726 | } else if (!strcmp(argv[c], "-o") && c < argc - 1) { |
| 727 | out_file = argv[++c]; |
| 728 | } else if (!strcmp(argv[c], "-d") && c < argc - 1) { |
| 729 | dump_file = argv[++c]; |
| 730 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 731 | } else if (!strcmp(argv[c], "-print_psnr")) { |
| 732 | config.show_compressed = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 733 | print_distortion = 0; |
| 734 | } else if (!strcmp(argv[c], "-print_ssim")) { |
| 735 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 736 | print_distortion = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 737 | } else if (!strcmp(argv[c], "-print_lsim")) { |
| 738 | config.show_compressed = 1; |
| 739 | print_distortion = 2; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 740 | } else if (!strcmp(argv[c], "-short")) { |
skal | fff2a11 | 2013-12-23 12:03:00 +0100 | [diff] [blame] | 741 | ++short_output; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 742 | } else if (!strcmp(argv[c], "-s") && c < argc - 2) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 743 | picture.width = ExUtilGetInt(argv[++c], 0, &parse_error); |
| 744 | picture.height = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | 1c1702d | 2015-11-12 23:05:02 +0000 | [diff] [blame^] | 745 | if (picture.width > WEBP_MAX_DIMENSION || picture.width < 0 || |
| 746 | picture.height > WEBP_MAX_DIMENSION || picture.height < 0) { |
| 747 | fprintf(stderr, |
| 748 | "Specified dimension (%d x %d) is out of range.\n", |
| 749 | picture.width, picture.height); |
| 750 | goto Error; |
| 751 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 752 | } else if (!strcmp(argv[c], "-m") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 753 | config.method = ExUtilGetInt(argv[++c], 0, &parse_error); |
skal | 65b99f1 | 2014-03-11 23:25:35 +0100 | [diff] [blame] | 754 | use_lossless_preset = 0; // disable -z option |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 755 | } else if (!strcmp(argv[c], "-q") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 756 | config.quality = ExUtilGetFloat(argv[++c], &parse_error); |
skal | 65b99f1 | 2014-03-11 23:25:35 +0100 | [diff] [blame] | 757 | use_lossless_preset = 0; // disable -z option |
| 758 | } else if (!strcmp(argv[c], "-z") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 759 | lossless_preset = ExUtilGetInt(argv[++c], 0, &parse_error); |
skal | 65b99f1 | 2014-03-11 23:25:35 +0100 | [diff] [blame] | 760 | if (use_lossless_preset != 0) use_lossless_preset = 1; |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 761 | } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 762 | config.alpha_quality = ExUtilGetInt(argv[++c], 0, &parse_error); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 763 | } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 764 | config.alpha_compression = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 765 | } else if (!strcmp(argv[c], "-alpha_cleanup")) { |
| 766 | keep_alpha = keep_alpha ? 2 : 0; |
Pascal Massimino | e7d9548 | 2013-04-02 19:14:14 -0700 | [diff] [blame] | 767 | } else if (!strcmp(argv[c], "-blend_alpha") && c < argc - 1) { |
| 768 | blend_alpha = 1; |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 769 | // background color is given in hex with an optional '0x' prefix |
| 770 | background_color = ExUtilGetInt(argv[++c], 16, &parse_error); |
Pascal Massimino | e7d9548 | 2013-04-02 19:14:14 -0700 | [diff] [blame] | 771 | background_color = background_color & 0x00ffffffu; |
Vikas Arora | 252028a | 2012-01-05 13:04:30 +0530 | [diff] [blame] | 772 | } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) { |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 773 | ++c; |
| 774 | if (!strcmp(argv[c], "none")) { |
| 775 | config.alpha_filtering = 0; |
| 776 | } else if (!strcmp(argv[c], "fast")) { |
| 777 | config.alpha_filtering = 1; |
| 778 | } else if (!strcmp(argv[c], "best")) { |
| 779 | config.alpha_filtering = 2; |
| 780 | } else { |
| 781 | fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]); |
| 782 | goto Error; |
| 783 | } |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 784 | } else if (!strcmp(argv[c], "-noalpha")) { |
| 785 | keep_alpha = 0; |
Pascal Massimino | 72920ca | 2012-04-24 10:59:08 +0000 | [diff] [blame] | 786 | } else if (!strcmp(argv[c], "-lossless")) { |
| 787 | config.lossless = 1; |
skal | b5a36cc | 2014-08-05 19:14:57 +0200 | [diff] [blame] | 788 | } else if (!strcmp(argv[c], "-near_lossless") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 789 | config.near_lossless = ExUtilGetInt(argv[++c], 0, &parse_error); |
skal | b5a36cc | 2014-08-05 19:14:57 +0200 | [diff] [blame] | 790 | config.lossless = 1; // use near-lossless only with lossless |
Mislav Bradac | 48f66b6 | 2015-09-11 12:29:07 +0000 | [diff] [blame] | 791 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 792 | } else if (!strcmp(argv[c], "-delta_palettization")) { |
| 793 | config.delta_palettization = 1; |
| 794 | config.lossless = 1; // use delta-palettization only with lossless |
| 795 | #endif // WEBP_EXPERIMENTAL_FEATURES |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 796 | } else if (!strcmp(argv[c], "-hint") && c < argc - 1) { |
| 797 | ++c; |
| 798 | if (!strcmp(argv[c], "photo")) { |
| 799 | config.image_hint = WEBP_HINT_PHOTO; |
| 800 | } else if (!strcmp(argv[c], "picture")) { |
| 801 | config.image_hint = WEBP_HINT_PICTURE; |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 802 | } else if (!strcmp(argv[c], "graph")) { |
| 803 | config.image_hint = WEBP_HINT_GRAPH; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 804 | } else { |
| 805 | fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]); |
| 806 | goto Error; |
| 807 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 808 | } else if (!strcmp(argv[c], "-size") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 809 | config.target_size = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 810 | } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 811 | config.target_PSNR = ExUtilGetFloat(argv[++c], &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 812 | } else if (!strcmp(argv[c], "-sns") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 813 | config.sns_strength = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 814 | } else if (!strcmp(argv[c], "-f") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 815 | config.filter_strength = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 816 | } else if (!strcmp(argv[c], "-af")) { |
| 817 | config.autofilter = 1; |
skal | e895059 | 2013-02-05 19:40:18 +0100 | [diff] [blame] | 818 | } else if (!strcmp(argv[c], "-jpeg_like")) { |
| 819 | config.emulate_jpeg_size = 1; |
skal | f817930 | 2013-03-01 01:21:34 +0100 | [diff] [blame] | 820 | } else if (!strcmp(argv[c], "-mt")) { |
| 821 | ++config.thread_level; // increase thread level |
skal | 9bfbdd1 | 2013-03-12 00:37:42 +0100 | [diff] [blame] | 822 | } else if (!strcmp(argv[c], "-low_memory")) { |
| 823 | config.low_memory = 1; |
Pascal Massimino | 842c009 | 2011-05-05 18:10:08 -0700 | [diff] [blame] | 824 | } else if (!strcmp(argv[c], "-strong")) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 825 | config.filter_type = 1; |
Pascal Massimino | 92668da | 2013-02-15 01:08:52 -0800 | [diff] [blame] | 826 | } else if (!strcmp(argv[c], "-nostrong")) { |
| 827 | config.filter_type = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 828 | } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 829 | config.filter_sharpness = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 830 | } else if (!strcmp(argv[c], "-pass") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 831 | config.pass = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 832 | } else if (!strcmp(argv[c], "-pre") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 833 | config.preprocessing = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 834 | } else if (!strcmp(argv[c], "-segments") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 835 | config.segments = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 836 | } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 837 | config.partition_limit = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 838 | } else if (!strcmp(argv[c], "-map") && c < argc - 1) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 839 | picture.extra_info_type = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 840 | } else if (!strcmp(argv[c], "-crop") && c < argc - 4) { |
| 841 | crop = 1; |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 842 | crop_x = ExUtilGetInt(argv[++c], 0, &parse_error); |
| 843 | crop_y = ExUtilGetInt(argv[++c], 0, &parse_error); |
| 844 | crop_w = ExUtilGetInt(argv[++c], 0, &parse_error); |
| 845 | crop_h = ExUtilGetInt(argv[++c], 0, &parse_error); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 846 | } else if (!strcmp(argv[c], "-resize") && c < argc - 2) { |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 847 | resize_w = ExUtilGetInt(argv[++c], 0, &parse_error); |
| 848 | resize_h = ExUtilGetInt(argv[++c], 0, &parse_error); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 849 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 850 | } else if (!strcmp(argv[c], "-noasm")) { |
Pascal Massimino | e06ac08 | 2011-09-02 21:30:08 +0000 | [diff] [blame] | 851 | VP8GetCPUInfo = NULL; |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 852 | #endif |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 853 | } else if (!strcmp(argv[c], "-version")) { |
| 854 | const int version = WebPGetEncoderVersion(); |
| 855 | printf("%d.%d.%d\n", |
| 856 | (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff); |
| 857 | return 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 858 | } else if (!strcmp(argv[c], "-progress")) { |
| 859 | show_progress = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 860 | } else if (!strcmp(argv[c], "-quiet")) { |
| 861 | quiet = 1; |
| 862 | } else if (!strcmp(argv[c], "-preset") && c < argc - 1) { |
| 863 | WebPPreset preset; |
| 864 | ++c; |
| 865 | if (!strcmp(argv[c], "default")) { |
| 866 | preset = WEBP_PRESET_DEFAULT; |
| 867 | } else if (!strcmp(argv[c], "photo")) { |
| 868 | preset = WEBP_PRESET_PHOTO; |
| 869 | } else if (!strcmp(argv[c], "picture")) { |
| 870 | preset = WEBP_PRESET_PICTURE; |
| 871 | } else if (!strcmp(argv[c], "drawing")) { |
| 872 | preset = WEBP_PRESET_DRAWING; |
| 873 | } else if (!strcmp(argv[c], "icon")) { |
| 874 | preset = WEBP_PRESET_ICON; |
| 875 | } else if (!strcmp(argv[c], "text")) { |
| 876 | preset = WEBP_PRESET_TEXT; |
| 877 | } else { |
| 878 | fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]); |
| 879 | goto Error; |
| 880 | } |
| 881 | if (!WebPConfigPreset(&config, preset, config.quality)) { |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 882 | fprintf(stderr, "Error! Could initialize configuration with preset.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 883 | goto Error; |
| 884 | } |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 885 | } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) { |
| 886 | static const struct { |
| 887 | const char* option; |
| 888 | int flag; |
| 889 | } kTokens[] = { |
| 890 | { "all", METADATA_ALL }, |
| 891 | { "none", 0 }, |
| 892 | { "exif", METADATA_EXIF }, |
James Zern | d8dc72a | 2013-03-13 14:04:20 -0700 | [diff] [blame] | 893 | { "icc", METADATA_ICC }, |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 894 | { "xmp", METADATA_XMP }, |
| 895 | }; |
| 896 | const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]); |
| 897 | const char* start = argv[++c]; |
| 898 | const char* const end = start + strlen(start); |
| 899 | |
| 900 | while (start < end) { |
| 901 | size_t i; |
| 902 | const char* token = strchr(start, ','); |
| 903 | if (token == NULL) token = end; |
| 904 | |
| 905 | for (i = 0; i < kNumTokens; ++i) { |
| 906 | if ((size_t)(token - start) == strlen(kTokens[i].option) && |
| 907 | !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) { |
| 908 | if (kTokens[i].flag != 0) { |
| 909 | keep_metadata |= kTokens[i].flag; |
| 910 | } else { |
| 911 | keep_metadata = 0; |
| 912 | } |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | if (i == kNumTokens) { |
| 917 | fprintf(stderr, "Error! Unknown metadata type '%.*s'\n", |
| 918 | (int)(token - start), start); |
| 919 | HelpLong(); |
| 920 | return -1; |
| 921 | } |
| 922 | start = token + 1; |
| 923 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 924 | #ifdef HAVE_WINCODEC_H |
James Zern | d8dc72a | 2013-03-13 14:04:20 -0700 | [diff] [blame] | 925 | if (keep_metadata != 0 && keep_metadata != METADATA_ICC) { |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 926 | // TODO(jzern): remove when -metadata is supported on all platforms. |
James Zern | e83ff7d | 2013-01-24 15:37:49 -0800 | [diff] [blame] | 927 | fprintf(stderr, "Warning: only ICC profile extraction is currently" |
| 928 | " supported on this platform!\n"); |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 929 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 930 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 931 | } else if (!strcmp(argv[c], "-v")) { |
| 932 | verbose = 1; |
James Zern | 98af68f | 2013-12-12 20:20:08 -0800 | [diff] [blame] | 933 | } else if (!strcmp(argv[c], "--")) { |
| 934 | if (c < argc - 1) in_file = argv[++c]; |
| 935 | break; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 936 | } else if (argv[c][0] == '-') { |
| 937 | fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]); |
| 938 | HelpLong(); |
| 939 | return -1; |
| 940 | } else { |
| 941 | in_file = argv[c]; |
| 942 | } |
James Zern | 96d43a8 | 2014-09-10 23:35:48 -0700 | [diff] [blame] | 943 | |
| 944 | if (parse_error) { |
| 945 | HelpLong(); |
| 946 | return -1; |
| 947 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 948 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 949 | if (in_file == NULL) { |
| 950 | fprintf(stderr, "No input file specified!\n"); |
| 951 | HelpShort(); |
| 952 | goto Error; |
| 953 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 954 | |
skal | 65b99f1 | 2014-03-11 23:25:35 +0100 | [diff] [blame] | 955 | if (use_lossless_preset == 1) { |
| 956 | if (!WebPConfigLosslessPreset(&config, lossless_preset)) { |
| 957 | fprintf(stderr, "Invalid lossless preset (-z %d)\n", lossless_preset); |
| 958 | goto Error; |
| 959 | } |
| 960 | } |
| 961 | |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 962 | // Check for unsupported command line options for lossless mode and log |
| 963 | // warning for such options. |
Pascal Massimino | 0275159 | 2012-06-20 09:20:34 +0000 | [diff] [blame] | 964 | if (!quiet && config.lossless == 1) { |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 965 | if (config.target_size > 0 || config.target_PSNR > 0) { |
| 966 | fprintf(stderr, "Encoding for specified size or PSNR is not supported" |
| 967 | " for lossless encoding. Ignoring such option(s)!\n"); |
| 968 | } |
| 969 | if (config.partition_limit > 0) { |
| 970 | fprintf(stderr, "Partition limit option is not required for lossless" |
| 971 | " encoding. Ignoring this option!\n"); |
| 972 | } |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 973 | } |
| 974 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 975 | if (!WebPValidateConfig(&config)) { |
| 976 | fprintf(stderr, "Error! Invalid configuration.\n"); |
| 977 | goto Error; |
| 978 | } |
| 979 | |
Pascal Massimino | 7861578 | 2015-10-27 22:54:11 +0100 | [diff] [blame] | 980 | // Read the input. We need to decide if we prefer ARGB or YUVA |
| 981 | // samples, depending on the expected compression mode (this saves |
| 982 | // some conversion steps). |
| 983 | picture.use_argb = (config.lossless || config.preprocessing > 0 || |
| 984 | crop || (resize_w | resize_h) > 0); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 985 | if (verbose) { |
skal | d51f45f | 2013-09-12 09:32:28 +0200 | [diff] [blame] | 986 | StopwatchReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 987 | } |
James Zern | 7eaee9f | 2013-01-11 12:25:36 -0800 | [diff] [blame] | 988 | if (!ReadPicture(in_file, &picture, keep_alpha, |
| 989 | (keep_metadata == 0) ? NULL : &metadata)) { |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 990 | fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 991 | goto Error; |
| 992 | } |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 993 | picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL; |
Pascal Massimino | e7d9548 | 2013-04-02 19:14:14 -0700 | [diff] [blame] | 994 | |
| 995 | if (blend_alpha) { |
| 996 | WebPBlendAlpha(&picture, background_color); |
| 997 | } |
| 998 | |
James Zern | 7c732e5 | 2013-02-01 20:04:20 -0800 | [diff] [blame] | 999 | if (keep_alpha == 2) { |
| 1000 | WebPCleanupTransparentArea(&picture); |
| 1001 | } |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 1002 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 1003 | if (verbose) { |
Pascal Massimino | 126c035 | 2013-01-25 00:44:16 -0800 | [diff] [blame] | 1004 | const double read_time = StopwatchReadAndReset(&stop_watch); |
| 1005 | fprintf(stderr, "Time to read input: %.3fs\n", read_time); |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | // Open the output |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 1009 | if (out_file != NULL) { |
| 1010 | const int use_stdout = !strcmp(out_file, "-"); |
James Zern | e9bfb11 | 2014-08-29 19:11:41 -0700 | [diff] [blame] | 1011 | out = use_stdout ? ExUtilSetBinaryMode(stdout) : fopen(out_file, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1012 | if (out == NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1013 | fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file); |
| 1014 | goto Error; |
| 1015 | } else { |
| 1016 | if (!short_output && !quiet) { |
| 1017 | fprintf(stderr, "Saving file '%s'\n", out_file); |
| 1018 | } |
| 1019 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 1020 | if (keep_metadata == 0) { |
| 1021 | picture.writer = MyWriter; |
| 1022 | picture.custom_ptr = (void*)out; |
| 1023 | } else { |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 1024 | picture.writer = WebPMemoryWrite; |
| 1025 | picture.custom_ptr = (void*)&memory_writer; |
| 1026 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1027 | } else { |
| 1028 | out = NULL; |
| 1029 | if (!quiet && !short_output) { |
| 1030 | fprintf(stderr, "No output file specified (no -o flag). Encoding will\n"); |
| 1031 | fprintf(stderr, "be performed, but its results discarded.\n\n"); |
| 1032 | } |
| 1033 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 1034 | if (!quiet) { |
| 1035 | picture.stats = &stats; |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 1036 | picture.user_data = (void*)in_file; |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 1037 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1038 | |
Vikas Arora | e09e9ff | 2014-07-28 16:04:54 -0700 | [diff] [blame] | 1039 | // Crop & resize. |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1040 | if (verbose) { |
skal | d51f45f | 2013-09-12 09:32:28 +0200 | [diff] [blame] | 1041 | StopwatchReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1042 | } |
Pascal Massimino | 31b68fe | 2012-06-21 00:30:43 -0700 | [diff] [blame] | 1043 | if (crop != 0) { |
| 1044 | // We use self-cropping using a view. |
| 1045 | if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) { |
| 1046 | fprintf(stderr, "Error! Cannot crop picture\n"); |
| 1047 | goto Error; |
| 1048 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 1049 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 1050 | if ((resize_w | resize_h) > 0) { |
| 1051 | if (!WebPPictureRescale(&picture, resize_w, resize_h)) { |
| 1052 | fprintf(stderr, "Error! Cannot resize picture\n"); |
| 1053 | goto Error; |
| 1054 | } |
| 1055 | } |
Vikas Arora | e09e9ff | 2014-07-28 16:04:54 -0700 | [diff] [blame] | 1056 | if (verbose && (crop != 0 || (resize_w | resize_h) > 0)) { |
| 1057 | const double preproc_time = StopwatchReadAndReset(&stop_watch); |
| 1058 | fprintf(stderr, "Time to crop/resize picture: %.3fs\n", preproc_time); |
| 1059 | } |
| 1060 | |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 1061 | if (picture.extra_info_type > 0) { |
| 1062 | AllocExtraInfo(&picture); |
| 1063 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 1064 | if (print_distortion >= 0) { // Save original picture for later comparison |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1065 | WebPPictureCopy(&picture, &original_picture); |
| 1066 | } |
Vikas Arora | e09e9ff | 2014-07-28 16:04:54 -0700 | [diff] [blame] | 1067 | |
| 1068 | // Compress. |
| 1069 | if (verbose) { |
| 1070 | StopwatchReset(&stop_watch); |
| 1071 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 1072 | if (!WebPEncode(&config, &picture)) { |
| 1073 | fprintf(stderr, "Error! Cannot encode picture as WebP\n"); |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 1074 | fprintf(stderr, "Error code: %d (%s)\n", |
| 1075 | picture.error_code, kErrorMessages[picture.error_code]); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 1076 | goto Error; |
| 1077 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1078 | if (verbose) { |
Pascal Massimino | 126c035 | 2013-01-25 00:44:16 -0800 | [diff] [blame] | 1079 | const double encode_time = StopwatchReadAndReset(&stop_watch); |
| 1080 | fprintf(stderr, "Time to encode picture: %.3fs\n", encode_time); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1081 | } |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 1082 | |
| 1083 | // Write info |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1084 | if (dump_file) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 1085 | if (picture.use_argb) { |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 1086 | fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode."); |
| 1087 | } else if (!DumpPicture(&picture, dump_file)) { |
| 1088 | fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file); |
| 1089 | } |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1090 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1091 | |
James Zern | 7049043 | 2013-12-09 20:15:54 -0800 | [diff] [blame] | 1092 | if (keep_metadata != 0) { |
| 1093 | if (out != NULL) { |
| 1094 | if (!WriteWebPWithMetadata(out, &picture, &memory_writer, |
| 1095 | &metadata, keep_metadata, &metadata_written)) { |
| 1096 | fprintf(stderr, "Error writing WebP file with metadata!\n"); |
| 1097 | goto Error; |
| 1098 | } |
| 1099 | } else { // output is disabled, just display the metadata stats. |
| 1100 | const struct { |
| 1101 | const MetadataPayload* const payload; |
| 1102 | int flag; |
| 1103 | } *iter, info[] = { |
| 1104 | { &metadata.exif, METADATA_EXIF }, |
| 1105 | { &metadata.iccp, METADATA_ICC }, |
| 1106 | { &metadata.xmp, METADATA_XMP }, |
| 1107 | { NULL, 0 } |
| 1108 | }; |
| 1109 | uint32_t unused1 = 0; |
| 1110 | uint64_t unused2 = 0; |
| 1111 | |
| 1112 | for (iter = info; iter->payload != NULL; ++iter) { |
| 1113 | if (UpdateFlagsAndSize(iter->payload, !!(keep_metadata & iter->flag), |
| 1114 | 0, &unused1, &unused2)) { |
| 1115 | metadata_written |= iter->flag; |
| 1116 | } |
| 1117 | } |
James Zern | 76ec5fa | 2013-01-14 18:32:44 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1121 | if (!quiet) { |
skal | fff2a11 | 2013-12-23 12:03:00 +0100 | [diff] [blame] | 1122 | if (!short_output || print_distortion < 0) { |
| 1123 | if (config.lossless) { |
| 1124 | PrintExtraInfoLossless(&picture, short_output, in_file); |
| 1125 | } else { |
| 1126 | PrintExtraInfoLossy(&picture, short_output, config.low_memory, in_file); |
| 1127 | } |
| 1128 | } |
| 1129 | if (!short_output && picture.extra_info_type > 0) { |
| 1130 | PrintMapInfo(&picture); |
| 1131 | } |
| 1132 | if (print_distortion >= 0) { // print distortion |
| 1133 | static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" }; |
| 1134 | float values[5]; |
skal | 56a2e9f | 2015-08-11 17:28:29 -0700 | [diff] [blame] | 1135 | if (picture.use_argb != original_picture.use_argb) { |
| 1136 | // Somehow, the WebPEncode() call converted the original picture. |
| 1137 | // We need to make both match before calling WebPPictureDistortion(). |
| 1138 | int ok = 0; |
| 1139 | if (picture.use_argb) { |
| 1140 | ok = WebPPictureYUVAToARGB(&original_picture); |
| 1141 | } else { |
| 1142 | ok = WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A); |
| 1143 | } |
| 1144 | if (!ok) { |
| 1145 | fprintf(stderr, "Error while converting original picture.\n"); |
| 1146 | goto Error; |
| 1147 | } |
skal | fff2a11 | 2013-12-23 12:03:00 +0100 | [diff] [blame] | 1148 | } |
| 1149 | if (!WebPPictureDistortion(&picture, &original_picture, |
| 1150 | print_distortion, values)) { |
| 1151 | fprintf(stderr, "Error while computing the distortion.\n"); |
| 1152 | goto Error; |
| 1153 | } |
| 1154 | if (!short_output) { |
skal | 56a2e9f | 2015-08-11 17:28:29 -0700 | [diff] [blame] | 1155 | fprintf(stderr, "%s: ", distortion_names[print_distortion]); |
| 1156 | if (picture.use_argb) { |
| 1157 | fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n", |
| 1158 | values[0], values[1], values[2], values[3], values[4]); |
| 1159 | } else { |
| 1160 | fprintf(stderr, "Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n", |
| 1161 | values[0], values[1], values[2], values[3], values[4]); |
| 1162 | } |
skal | fff2a11 | 2013-12-23 12:03:00 +0100 | [diff] [blame] | 1163 | } else { |
| 1164 | fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]); |
| 1165 | } |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 1166 | } |
James Zern | 0bc4268 | 2013-03-13 14:41:38 -0700 | [diff] [blame] | 1167 | if (!short_output) { |
| 1168 | PrintMetadataInfo(&metadata, metadata_written); |
| 1169 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1170 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 1171 | return_value = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1172 | |
| 1173 | Error: |
skal | af93bdd | 2014-03-27 23:27:32 +0100 | [diff] [blame] | 1174 | WebPMemoryWriterClear(&memory_writer); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1175 | free(picture.extra_info); |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 1176 | MetadataFree(&metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1177 | WebPPictureFree(&picture); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1178 | WebPPictureFree(&original_picture); |
skal | e12f874 | 2014-03-12 19:48:00 +0100 | [diff] [blame] | 1179 | if (out != NULL && out != stdout) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1180 | fclose(out); |
| 1181 | } |
| 1182 | |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 1183 | return return_value; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 1186 | //------------------------------------------------------------------------------ |