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