James Zern | ad1e163 | 2012-01-06 14:49:06 -0800 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 2 | // |
| 3 | // This code is licensed under the same terms as WebM: |
| 4 | // Software License Agreement: http://www.webmproject.org/license/software/ |
| 5 | // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
| 6 | // ----------------------------------------------------------------------------- |
| 7 | // |
| 8 | // simple command line calling the WebPEncode function. |
| 9 | // Encodes a raw .YUV into WebP bitstream |
| 10 | // |
| 11 | // Author: Skal (pascal.massimino@gmail.com) |
| 12 | |
| 13 | #include <stdio.h> |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 14 | #include <stdlib.h> |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 17 | #ifdef HAVE_CONFIG_H |
| 18 | #include "config.h" |
| 19 | #endif |
| 20 | |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 21 | #ifdef WEBP_HAVE_TIFF |
| 22 | #include <tiffio.h> |
| 23 | #endif |
| 24 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 25 | #ifdef HAVE_WINCODEC_H |
| 26 | #ifdef __MINGW32__ |
| 27 | #define INITGUID // Without this GUIDs are declared extern and fail to link |
| 28 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 29 | #define CINTERFACE |
| 30 | #define COBJMACROS |
| 31 | #define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++ |
| 32 | // code with COBJMACROS. |
| 33 | #include <shlwapi.h> |
| 34 | #include <windows.h> |
| 35 | #include <wincodec.h> |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 36 | #endif /* HAVE_WINCODEC_H */ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 37 | |
| 38 | |
| 39 | #include "webp/encode.h" |
James Zern | 6a871d6 | 2012-12-06 13:48:21 -0800 | [diff] [blame^] | 40 | #include "./jpegdec.h" |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 41 | #include "./metadata.h" |
James Zern | 2ee228f | 2012-12-06 13:06:37 -0800 | [diff] [blame] | 42 | #include "./pngdec.h" |
James Zern | 00b29e2 | 2012-05-15 13:48:11 -0700 | [diff] [blame] | 43 | #include "./stopwatch.h" |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 44 | #ifndef WEBP_DLL |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 45 | #if defined(__cplusplus) || defined(c_plusplus) |
| 46 | extern "C" { |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 47 | #endif |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 48 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 49 | extern void* VP8GetCPUInfo; // opaque forward declaration. |
| 50 | |
| 51 | #if defined(__cplusplus) || defined(c_plusplus) |
| 52 | } // extern "C" |
| 53 | #endif |
| 54 | #endif // WEBP_DLL |
| 55 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 56 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 57 | |
| 58 | static int verbose = 0; |
| 59 | |
| 60 | static int ReadYUV(FILE* in_file, WebPPicture* const pic) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 61 | const int use_argb = pic->use_argb; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 62 | const int uv_width = (pic->width + 1) / 2; |
| 63 | const int uv_height = (pic->height + 1) / 2; |
| 64 | int y; |
| 65 | int ok = 0; |
| 66 | |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 67 | pic->use_argb = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 68 | if (!WebPPictureAlloc(pic)) return ok; |
| 69 | |
| 70 | for (y = 0; y < pic->height; ++y) { |
| 71 | if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) { |
| 72 | goto End; |
| 73 | } |
| 74 | } |
| 75 | for (y = 0; y < uv_height; ++y) { |
| 76 | if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1) |
| 77 | goto End; |
| 78 | } |
| 79 | for (y = 0; y < uv_height; ++y) { |
| 80 | if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1) |
| 81 | goto End; |
| 82 | } |
| 83 | ok = 1; |
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: |
| 87 | return ok; |
| 88 | } |
| 89 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 90 | #ifdef HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 91 | |
James Zern | f2b5d19 | 2012-10-08 18:33:18 -0700 | [diff] [blame] | 92 | #define IFS(fn) \ |
| 93 | do { \ |
| 94 | if (SUCCEEDED(hr)) { \ |
| 95 | hr = (fn); \ |
| 96 | if (FAILED(hr)) fprintf(stderr, #fn " failed %08x\n", hr); \ |
| 97 | } \ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 98 | } while (0) |
| 99 | |
James Zern | 902d3e3 | 2012-05-08 17:49:39 -0700 | [diff] [blame] | 100 | // modified version of DEFINE_GUID from guiddef.h. |
| 101 | #define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ |
| 102 | const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } |
| 103 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 104 | #ifdef __cplusplus |
| 105 | #define MAKE_REFGUID(x) (x) |
| 106 | #else |
| 107 | #define MAKE_REFGUID(x) &(x) |
| 108 | #endif |
| 109 | |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 110 | typedef struct WICFormatImporter { |
| 111 | const GUID* pixel_format; |
| 112 | int bytes_per_pixel; |
| 113 | int (*import)(WebPPicture* const, const uint8_t* const, int); |
| 114 | } WICFormatImporter; |
| 115 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 116 | static HRESULT OpenInputStream(const char* filename, IStream** ppStream) { |
| 117 | HRESULT hr = S_OK; |
| 118 | IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream)); |
| 119 | if (FAILED(hr)) |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 120 | fprintf(stderr, "Error opening input file %s (%08x)\n", filename, hr); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 121 | return hr; |
| 122 | } |
| 123 | |
| 124 | static HRESULT ReadPictureWithWIC(const char* filename, |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 125 | WebPPicture* const pic, int keep_alpha) { |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 126 | // From Microsoft SDK 7.0a -- wincodec.h |
| 127 | // Create local copies for compatibility when building against earlier |
| 128 | // versions of the SDK. |
| 129 | WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_, |
| 130 | 0x6fddc324, 0x4e03, 0x4bfe, |
| 131 | 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c); |
| 132 | WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_, |
| 133 | 0x6fddc324, 0x4e03, 0x4bfe, |
| 134 | 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d); |
| 135 | WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_, |
| 136 | 0x6fddc324, 0x4e03, 0x4bfe, |
| 137 | 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f); |
| 138 | WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_, |
| 139 | 0xf5c7ad2d, 0x6a8d, 0x43dd, |
| 140 | 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9); |
| 141 | const WICFormatImporter alphaFormatImporters[] = { |
| 142 | { &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA }, |
| 143 | { &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA }, |
| 144 | { NULL, 0, NULL }, |
| 145 | }; |
| 146 | const WICFormatImporter nonAlphaFormatImporters[] = { |
| 147 | { &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR }, |
| 148 | { &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB }, |
| 149 | { NULL, 0, NULL }, |
| 150 | }; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 151 | HRESULT hr = S_OK; |
| 152 | IWICBitmapFrameDecode* pFrame = NULL; |
| 153 | IWICFormatConverter* pConverter = NULL; |
| 154 | IWICImagingFactory* pFactory = NULL; |
| 155 | IWICBitmapDecoder* pDecoder = NULL; |
| 156 | IStream* pStream = NULL; |
| 157 | UINT frameCount = 0; |
James Zern | 292ec5c | 2012-08-02 14:03:30 -0700 | [diff] [blame] | 158 | UINT width = 0, height = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 159 | BYTE* rgb = NULL; |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 160 | WICPixelFormatGUID srcPixelFormat = { 0 }; |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 161 | const WICFormatImporter* importer = NULL; |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 162 | GUID srcContainerFormat = { 0 }; |
| 163 | const GUID* alphaContainers[] = { |
| 164 | &GUID_ContainerFormatBmp, |
| 165 | &GUID_ContainerFormatPng, |
| 166 | &GUID_ContainerFormatTiff |
| 167 | }; |
| 168 | int has_alpha = 0; |
| 169 | int i, stride; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 170 | |
| 171 | IFS(CoInitialize(NULL)); |
| 172 | IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL, |
| 173 | CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory), |
| 174 | (LPVOID*)&pFactory)); |
| 175 | if (hr == REGDB_E_CLASSNOTREG) { |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 176 | fprintf(stderr, |
| 177 | "Couldn't access Windows Imaging Component (are you running " |
| 178 | "Windows XP SP3 or newer?). Most formats not available. " |
| 179 | "Use -s for the available YUV input.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 180 | } |
| 181 | // Prepare for image decoding. |
| 182 | IFS(OpenInputStream(filename, &pStream)); |
| 183 | IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL, |
| 184 | WICDecodeMetadataCacheOnDemand, &pDecoder)); |
| 185 | IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount)); |
| 186 | if (SUCCEEDED(hr) && frameCount == 0) { |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 187 | fprintf(stderr, "No frame found in input file.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 188 | hr = E_FAIL; |
| 189 | } |
| 190 | IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame)); |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 191 | IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat)); |
| 192 | IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat)); |
| 193 | |
James Zern | ecd66f7 | 2012-10-08 18:15:30 -0700 | [diff] [blame] | 194 | if (keep_alpha) { |
| 195 | for (i = 0; |
| 196 | i < sizeof(alphaContainers) / sizeof(alphaContainers[0]); |
| 197 | ++i) { |
| 198 | if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat), |
| 199 | MAKE_REFGUID(*alphaContainers[i]))) { |
| 200 | has_alpha = |
| 201 | IsEqualGUID(MAKE_REFGUID(srcPixelFormat), |
| 202 | MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) || |
| 203 | IsEqualGUID(MAKE_REFGUID(srcPixelFormat), |
| 204 | MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_)); |
| 205 | break; |
| 206 | } |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 209 | |
| 210 | // Prepare for pixel format conversion (if necessary). |
| 211 | IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter)); |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 212 | |
| 213 | for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters; |
| 214 | hr == S_OK && importer->import != NULL; ++importer) { |
| 215 | BOOL canConvert; |
| 216 | const HRESULT cchr = IWICFormatConverter_CanConvert( |
| 217 | pConverter, |
| 218 | MAKE_REFGUID(srcPixelFormat), |
| 219 | MAKE_REFGUID(*importer->pixel_format), |
| 220 | &canConvert); |
| 221 | if (SUCCEEDED(cchr) && canConvert) break; |
| 222 | } |
| 223 | if (importer->import == NULL) hr = E_FAIL; |
| 224 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 225 | IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame, |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 226 | importer->pixel_format, |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 227 | WICBitmapDitherTypeNone, |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 228 | NULL, 0.0, WICBitmapPaletteTypeCustom)); |
| 229 | |
| 230 | // Decode. |
| 231 | IFS(IWICFormatConverter_GetSize(pConverter, &width, &height)); |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 232 | stride = importer->bytes_per_pixel * width * sizeof(*rgb); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 233 | if (SUCCEEDED(hr)) { |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 234 | rgb = (BYTE*)malloc(stride * height); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 235 | if (rgb == NULL) |
| 236 | hr = E_OUTOFMEMORY; |
| 237 | } |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 238 | IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride, |
| 239 | stride * height, rgb)); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 240 | |
| 241 | // WebP conversion. |
| 242 | if (SUCCEEDED(hr)) { |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 243 | int ok; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 244 | pic->width = width; |
| 245 | pic->height = height; |
James Zern | eda8ee4 | 2012-10-19 18:34:06 -0700 | [diff] [blame] | 246 | ok = importer->import(pic, rgb, stride); |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 247 | if (!ok) |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 248 | hr = E_FAIL; |
| 249 | } |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 250 | if (SUCCEEDED(hr)) { |
| 251 | if (has_alpha && keep_alpha == 2) { |
| 252 | WebPCleanupTransparentArea(pic); |
| 253 | } |
| 254 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 255 | |
| 256 | // Cleanup. |
| 257 | if (pConverter != NULL) IUnknown_Release(pConverter); |
| 258 | if (pFrame != NULL) IUnknown_Release(pFrame); |
| 259 | if (pDecoder != NULL) IUnknown_Release(pDecoder); |
| 260 | if (pFactory != NULL) IUnknown_Release(pFactory); |
| 261 | if (pStream != NULL) IUnknown_Release(pStream); |
| 262 | free(rgb); |
| 263 | return hr; |
| 264 | } |
| 265 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 266 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 267 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 268 | int ok; |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 269 | (void)metadata; // TODO(jzern): add metadata extraction using WIC |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 270 | if (pic->width != 0 && pic->height != 0) { |
| 271 | // If image size is specified, infer it as YUV format. |
| 272 | FILE* in_file = fopen(filename, "rb"); |
| 273 | if (in_file == NULL) { |
| 274 | fprintf(stderr, "Error! Cannot open input file '%s'\n", filename); |
| 275 | return 0; |
| 276 | } |
| 277 | ok = ReadYUV(in_file, pic); |
| 278 | fclose(in_file); |
| 279 | } else { |
| 280 | // If no size specified, try to decode it using WIC. |
James Zern | 3cfe088 | 2011-06-21 18:29:30 -0700 | [diff] [blame] | 281 | ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha)); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 282 | } |
| 283 | if (!ok) { |
| 284 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 285 | } |
| 286 | return ok; |
| 287 | } |
| 288 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 289 | #else // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 290 | |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 291 | #ifdef WEBP_HAVE_TIFF |
| 292 | static int ReadTIFF(const char* const filename, |
| 293 | WebPPicture* const pic, int keep_alpha) { |
| 294 | TIFF* const tif = TIFFOpen(filename, "r"); |
| 295 | uint32 width, height; |
| 296 | uint32* raster; |
| 297 | int ok = 0; |
| 298 | int dircount = 1; |
| 299 | |
| 300 | if (tif == NULL) { |
| 301 | fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | while (TIFFReadDirectory(tif)) ++dircount; |
| 306 | |
| 307 | if (dircount > 1) { |
| 308 | fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n" |
| 309 | "Only the first will be used, %d will be ignored.\n", |
| 310 | dircount - 1); |
| 311 | } |
| 312 | |
| 313 | TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); |
| 314 | TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); |
| 315 | raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster)); |
| 316 | if (raster != NULL) { |
| 317 | if (TIFFReadRGBAImageOriented(tif, width, height, raster, |
| 318 | ORIENTATION_TOPLEFT, 1)) { |
| 319 | const int stride = width * sizeof(*raster); |
| 320 | pic->width = width; |
| 321 | pic->height = height; |
| 322 | // TIFF data is ABGR |
| 323 | #ifdef __BIG_ENDIAN__ |
| 324 | TIFFSwabArrayOfLong(raster, width * height); |
| 325 | #endif |
| 326 | ok = keep_alpha |
| 327 | ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride) |
| 328 | : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride); |
| 329 | } |
| 330 | _TIFFfree(raster); |
| 331 | } else { |
| 332 | fprintf(stderr, "Error allocating TIFF RGBA memory!\n"); |
| 333 | } |
| 334 | |
| 335 | if (ok && keep_alpha == 2) { |
| 336 | WebPCleanupTransparentArea(pic); |
| 337 | } |
| 338 | |
| 339 | TIFFClose(tif); |
| 340 | return ok; |
| 341 | } |
| 342 | #else |
| 343 | static int ReadTIFF(const char* const filename, |
| 344 | WebPPicture* const pic, int keep_alpha) { |
| 345 | (void)filename; |
| 346 | (void)pic; |
| 347 | (void)keep_alpha; |
| 348 | fprintf(stderr, "TIFF support not compiled. Please install the libtiff " |
| 349 | "development package before building.\n"); |
| 350 | return 0; |
| 351 | } |
| 352 | #endif |
| 353 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 354 | typedef enum { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 355 | PNG_ = 0, |
| 356 | JPEG_, |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 357 | TIFF_, // 'TIFF' clashes with libtiff |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 358 | UNSUPPORTED |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 359 | } InputFileFormat; |
| 360 | |
| 361 | static InputFileFormat GetImageType(FILE* in_file) { |
| 362 | InputFileFormat format = UNSUPPORTED; |
| 363 | unsigned int magic; |
| 364 | unsigned char buf[4]; |
| 365 | |
| 366 | if ((fread(&buf[0], 4, 1, in_file) != 1) || |
| 367 | (fseek(in_file, 0, SEEK_SET) != 0)) { |
| 368 | return format; |
| 369 | } |
| 370 | |
| 371 | magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
| 372 | if (magic == 0x89504E47U) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 373 | format = PNG_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 374 | } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 375 | format = JPEG_; |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 376 | } else if (magic == 0x49492A00 || magic == 0x4D4D002A) { |
| 377 | format = TIFF_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 378 | } |
| 379 | return format; |
| 380 | } |
| 381 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 382 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 383 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 384 | int ok = 0; |
| 385 | FILE* in_file = fopen(filename, "rb"); |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 386 | (void)metadata; // TODO(jzern): add metadata extraction to the formats below. |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 387 | if (in_file == NULL) { |
| 388 | fprintf(stderr, "Error! Cannot open input file '%s'\n", filename); |
| 389 | return ok; |
| 390 | } |
| 391 | |
| 392 | if (pic->width == 0 || pic->height == 0) { |
| 393 | // If no size specified, try to decode it as PNG/JPEG (as appropriate). |
| 394 | const InputFileFormat format = GetImageType(in_file); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 395 | if (format == PNG_) { |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 396 | ok = ReadPNG(in_file, pic, keep_alpha); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 397 | } else if (format == JPEG_) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 398 | ok = ReadJPEG(in_file, pic); |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 399 | } else if (format == TIFF_) { |
| 400 | ok = ReadTIFF(filename, pic, keep_alpha); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 401 | } |
| 402 | } else { |
| 403 | // If image size is specified, infer it as YUV format. |
| 404 | ok = ReadYUV(in_file, pic); |
| 405 | } |
| 406 | if (!ok) { |
| 407 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 408 | } |
| 409 | |
| 410 | fclose(in_file); |
| 411 | return ok; |
| 412 | } |
| 413 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 414 | #endif // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 415 | |
| 416 | static void AllocExtraInfo(WebPPicture* const pic) { |
| 417 | const int mb_w = (pic->width + 15) / 16; |
| 418 | const int mb_h = (pic->height + 15) / 16; |
| 419 | pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info)); |
| 420 | } |
| 421 | |
| 422 | static void PrintByteCount(const int bytes[4], int total_size, |
| 423 | int* const totals) { |
| 424 | int s; |
| 425 | int total = 0; |
| 426 | for (s = 0; s < 4; ++s) { |
| 427 | fprintf(stderr, "| %7d ", bytes[s]); |
| 428 | total += bytes[s]; |
| 429 | if (totals) totals[s] += bytes[s]; |
| 430 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 431 | fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void PrintPercents(const int counts[4], int total) { |
| 435 | int s; |
| 436 | for (s = 0; s < 4; ++s) { |
| 437 | fprintf(stderr, "| %2d%%", 100 * counts[s] / total); |
| 438 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 439 | fprintf(stderr, "| %7d\n", total); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void PrintValues(const int values[4]) { |
| 443 | int s; |
| 444 | for (s = 0; s < 4; ++s) { |
| 445 | fprintf(stderr, "| %7d ", values[s]); |
| 446 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 447 | fprintf(stderr, "|\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 450 | static void PrintFullLosslessInfo(const WebPAuxStats* const stats, |
| 451 | const char* const description) { |
| 452 | fprintf(stderr, "Lossless-%s compressed size: %d bytes\n", |
| 453 | description, stats->lossless_size); |
| 454 | if (stats->lossless_features) { |
| 455 | fprintf(stderr, " * Lossless features used:"); |
| 456 | if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION"); |
| 457 | if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM"); |
| 458 | if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN"); |
| 459 | if (stats->lossless_features & 8) fprintf(stderr, " PALETTE"); |
| 460 | fprintf(stderr, "\n"); |
| 461 | } |
| 462 | fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n", |
| 463 | stats->histogram_bits, stats->transform_bits, stats->cache_bits); |
| 464 | if (stats->palette_size > 0) { |
| 465 | fprintf(stderr, " * Palette size: %d\n", stats->palette_size); |
| 466 | } |
| 467 | } |
| 468 | |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 469 | static void PrintExtraInfoLossless(const WebPPicture* const pic, |
| 470 | int short_output, |
| 471 | const char* const file_name) { |
| 472 | const WebPAuxStats* const stats = pic->stats; |
| 473 | if (short_output) { |
| 474 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
| 475 | } else { |
| 476 | fprintf(stderr, "File: %s\n", file_name); |
| 477 | fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height); |
| 478 | fprintf(stderr, "Output: %d bytes\n", stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 479 | PrintFullLosslessInfo(stats, "ARGB"); |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output, |
| 484 | const char* const file_name) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 485 | const WebPAuxStats* const stats = pic->stats; |
| 486 | if (short_output) { |
| 487 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 488 | } else { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 489 | const int num_i4 = stats->block_count[0]; |
| 490 | const int num_i16 = stats->block_count[1]; |
| 491 | const int num_skip = stats->block_count[2]; |
| 492 | const int total = num_i4 + num_i16; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 493 | fprintf(stderr, "File: %s\n", file_name); |
| 494 | fprintf(stderr, "Dimension: %d x %d%s\n", |
James Zern | c71ff9e | 2012-06-07 17:32:29 -0700 | [diff] [blame] | 495 | pic->width, pic->height, |
| 496 | stats->alpha_data_size ? " (with alpha)" : ""); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 497 | fprintf(stderr, "Output: " |
| 498 | "%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] | 499 | stats->coded_size, |
| 500 | stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]); |
| 501 | if (total > 0) { |
| 502 | int totals[4] = { 0, 0, 0, 0 }; |
| 503 | fprintf(stderr, "block count: intra4: %d\n" |
| 504 | " intra16: %d (-> %.2f%%)\n", |
| 505 | num_i4, num_i16, 100.f * num_i16 / total); |
| 506 | fprintf(stderr, " skipped block: %d (%.2f%%)\n", |
| 507 | num_skip, 100.f * num_skip / total); |
| 508 | fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n" |
| 509 | " mode-partition: %6d (%.1f%%)\n", |
| 510 | stats->header_bytes[0], |
| 511 | 100.f * stats->header_bytes[0] / stats->coded_size, |
| 512 | stats->header_bytes[1], |
| 513 | 100.f * stats->header_bytes[1] / stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 514 | if (stats->alpha_data_size > 0) { |
| 515 | fprintf(stderr, " transparency: %6d (%.1f dB)\n", |
| 516 | stats->alpha_data_size, stats->PSNR[4]); |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 517 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 518 | if (stats->layer_data_size) { |
| 519 | fprintf(stderr, " enhancement: %6d\n", |
| 520 | stats->layer_data_size); |
| 521 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 522 | fprintf(stderr, " Residuals bytes " |
| 523 | "|segment 1|segment 2|segment 3" |
| 524 | "|segment 4| total\n"); |
| 525 | fprintf(stderr, " intra4-coeffs: "); |
| 526 | PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals); |
| 527 | fprintf(stderr, " intra16-coeffs: "); |
| 528 | PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals); |
| 529 | fprintf(stderr, " chroma coeffs: "); |
| 530 | PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals); |
| 531 | fprintf(stderr, " macroblocks: "); |
| 532 | PrintPercents(stats->segment_size, total); |
| 533 | fprintf(stderr, " quantizer: "); |
| 534 | PrintValues(stats->segment_quant); |
| 535 | fprintf(stderr, " filter level: "); |
| 536 | PrintValues(stats->segment_level); |
| 537 | fprintf(stderr, "------------------+---------"); |
| 538 | fprintf(stderr, "+---------+---------+---------+-----------------\n"); |
| 539 | fprintf(stderr, " segments total: "); |
| 540 | PrintByteCount(totals, stats->coded_size, NULL); |
| 541 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 542 | if (stats->lossless_size > 0) { |
| 543 | PrintFullLosslessInfo(stats, "alpha"); |
| 544 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 545 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 546 | if (pic->extra_info != NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 547 | const int mb_w = (pic->width + 15) / 16; |
| 548 | const int mb_h = (pic->height + 15) / 16; |
| 549 | const int type = pic->extra_info_type; |
| 550 | int x, y; |
| 551 | for (y = 0; y < mb_h; ++y) { |
| 552 | for (x = 0; x < mb_w; ++x) { |
| 553 | const int c = pic->extra_info[x + y * mb_w]; |
| 554 | if (type == 1) { // intra4/intra16 |
| 555 | printf("%c", "+."[c]); |
| 556 | } else if (type == 2) { // segments |
| 557 | printf("%c", ".-*X"[c]); |
| 558 | } else if (type == 3) { // quantizers |
| 559 | printf("%.2d ", c); |
| 560 | } else if (type == 6 || type == 7) { |
| 561 | printf("%3d ", c); |
| 562 | } else { |
| 563 | printf("0x%.2x ", c); |
| 564 | } |
| 565 | } |
| 566 | printf("\n"); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 571 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 572 | |
| 573 | static int MyWriter(const uint8_t* data, size_t data_size, |
| 574 | const WebPPicture* const pic) { |
| 575 | FILE* const out = (FILE*)pic->custom_ptr; |
| 576 | return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1; |
| 577 | } |
| 578 | |
| 579 | // Dumps a picture as a PGM file using the IMC4 layout. |
| 580 | static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) { |
| 581 | int y; |
| 582 | const int uv_width = (picture->width + 1) / 2; |
| 583 | const int uv_height = (picture->height + 1) / 2; |
| 584 | const int stride = (picture->width + 1) & ~1; |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 585 | const int alpha_height = |
| 586 | WebPPictureHasTransparency(picture) ? picture->height : 0; |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 587 | const int height = picture->height + uv_height + alpha_height; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 588 | FILE* const f = fopen(PGM_name, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 589 | if (f == NULL) return 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 590 | fprintf(f, "P5\n%d %d\n255\n", stride, height); |
| 591 | for (y = 0; y < picture->height; ++y) { |
| 592 | if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1) |
| 593 | return 0; |
| 594 | if (picture->width & 1) fputc(0, f); // pad |
| 595 | } |
| 596 | for (y = 0; y < uv_height; ++y) { |
| 597 | if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 598 | return 0; |
| 599 | if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 600 | return 0; |
| 601 | } |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 602 | for (y = 0; y < alpha_height; ++y) { |
| 603 | if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1) |
| 604 | return 0; |
| 605 | if (picture->width & 1) fputc(0, f); // pad |
| 606 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 607 | fclose(f); |
| 608 | return 1; |
| 609 | } |
| 610 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 611 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 612 | |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 613 | static int ProgressReport(int percent, const WebPPicture* const picture) { |
| 614 | printf("[%s]: %3d %% \r", |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 615 | (char*)picture->user_data, percent); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 616 | fflush(stdout); |
| 617 | return 1; // all ok |
| 618 | } |
| 619 | |
| 620 | //------------------------------------------------------------------------------ |
| 621 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 622 | static void HelpShort(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 623 | printf("Usage:\n\n"); |
| 624 | printf(" cwebp [options] -q quality input.png -o output.webp\n\n"); |
| 625 | printf("where quality is between 0 (poor) to 100 (very good).\n"); |
| 626 | printf("Typical value is around 80.\n\n"); |
| 627 | printf("Try -longhelp for an exhaustive list of advanced options.\n"); |
| 628 | } |
| 629 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 630 | static void HelpLong(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 631 | printf("Usage:\n"); |
| 632 | printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n"); |
| 633 | printf("If input size (-s) for an image is not specified, " |
James Zern | 12f9aed | 2012-07-13 14:55:39 -0700 | [diff] [blame] | 634 | "it is assumed to be a PNG, JPEG or TIFF file.\n"); |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 635 | #ifdef HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 636 | printf("Windows builds can take as input any of the files handled by WIC\n"); |
| 637 | #endif |
| 638 | printf("options:\n"); |
| 639 | printf(" -h / -help ............ short help\n"); |
| 640 | printf(" -H / -longhelp ........ long help\n"); |
| 641 | printf(" -q <float> ............. quality factor (0:small..100:big)\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 642 | printf(" -alpha_q <int> ......... Transparency-compression quality " |
| 643 | "(0..100).\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 644 | printf(" -preset <string> ....... Preset setting, one of:\n"); |
| 645 | printf(" default, photo, picture,\n"); |
| 646 | printf(" drawing, icon, text\n"); |
| 647 | printf(" -preset must come first, as it overwrites other parameters."); |
| 648 | printf("\n"); |
| 649 | printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n"); |
| 650 | printf(" -segments <int> ........ number of segments to use (1..4)\n"); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 651 | printf(" -size <int> ............ Target size (in bytes)\n"); |
| 652 | printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 653 | printf("\n"); |
| 654 | printf(" -s <int> <int> ......... Input size (width x height) for YUV\n"); |
| 655 | printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n"); |
| 656 | printf(" -f <int> ............... filter strength (0=off..100)\n"); |
| 657 | printf(" -sharpness <int> ....... " |
| 658 | "filter sharpness (0:most .. 7:least sharp)\n"); |
| 659 | printf(" -strong ................ use strong filter instead of simple.\n"); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 660 | printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n"); |
| 661 | printf(" " |
| 662 | "the first partition (0=no degradation ... 100=full)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 663 | printf(" -pass <int> ............ analysis pass number (1..10)\n"); |
| 664 | 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] | 665 | printf(" -resize <w> <h> ........ resize picture (after any cropping)\n"); |
| 666 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 667 | printf(" -444 / -422 / -gray ..... Change colorspace\n"); |
| 668 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 669 | printf(" -map <int> ............. print map of extra info.\n"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 670 | printf(" -print_psnr ............ prints averaged PSNR distortion.\n"); |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 671 | printf(" -print_ssim ............ prints averaged SSIM distortion.\n"); |
| 672 | printf(" -print_lsim ............ prints local-similarity distortion.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 673 | printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 674 | printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n"); |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 675 | printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n"); |
| 676 | printf(" One of: none, fast (default) or best.\n"); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 677 | printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 678 | printf(" -noalpha ............... discard any transparency information.\n"); |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 679 | printf(" -lossless .............. Encode image losslessly.\n"); |
| 680 | printf(" -hint <string> ......... Specify image characteristics hint.\n"); |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 681 | printf(" One of: photo, picture or graph\n"); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 682 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 683 | printf("\n"); |
| 684 | printf(" -short ................. condense printed message\n"); |
| 685 | printf(" -quiet ................. don't print anything.\n"); |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 686 | printf(" -version ............... print version number and exit.\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 687 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 688 | printf(" -noasm ................. disable all assembly optimizations.\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 689 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 690 | printf(" -v ..................... verbose, e.g. print encoding/decoding " |
| 691 | "times\n"); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 692 | printf(" -progress .............. report encoding progress\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 693 | printf("\n"); |
| 694 | printf("Experimental Options:\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 695 | printf(" -af .................... auto-adjust filter strength.\n"); |
| 696 | printf(" -pre <int> ............. pre-processing filter\n"); |
| 697 | printf("\n"); |
| 698 | } |
| 699 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 700 | //------------------------------------------------------------------------------ |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 701 | // Error messages |
| 702 | |
| 703 | static const char* const kErrorMessages[] = { |
| 704 | "OK", |
| 705 | "OUT_OF_MEMORY: Out of memory allocating objects", |
| 706 | "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer", |
| 707 | "NULL_PARAMETER: NULL parameter passed to function", |
| 708 | "INVALID_CONFIGURATION: configuration is invalid", |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 709 | "BAD_DIMENSION: Bad picture dimension. Maximum width and height " |
| 710 | "allowed is 16383 pixels.", |
| 711 | "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n" |
| 712 | "To reduce the size of this partition, try using less segments " |
| 713 | "with the -segments option, and eventually reduce the number of " |
| 714 | "header bits using -partition_limit. More details are available " |
| 715 | "in the manual (`man cwebp`)", |
| 716 | "PARTITION_OVERFLOW: Partition is too big to fit 16M", |
Pascal Massimino | d71fbdc | 2011-12-01 03:34:22 -0800 | [diff] [blame] | 717 | "BAD_WRITE: Picture writer returned an I/O error", |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 718 | "FILE_TOO_BIG: File would be too big to fit in 4G", |
| 719 | "USER_ABORT: encoding abort requested by user" |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 720 | }; |
| 721 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 722 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 723 | |
| 724 | int main(int argc, const char *argv[]) { |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 725 | int return_value = -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 726 | const char *in_file = NULL, *out_file = NULL, *dump_file = NULL; |
| 727 | FILE *out = NULL; |
| 728 | int c; |
| 729 | int short_output = 0; |
| 730 | int quiet = 0; |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 731 | int keep_alpha = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 732 | 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] | 733 | int resize_w = 0, resize_h = 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 734 | int show_progress = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 735 | WebPPicture picture; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 736 | int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 737 | WebPPicture original_picture; // when PSNR or SSIM is requested |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 738 | WebPConfig config; |
| 739 | WebPAuxStats stats; |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 740 | Metadata metadata; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 741 | Stopwatch stop_watch; |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 742 | |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 743 | MetadataInit(&metadata); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 744 | if (!WebPPictureInit(&picture) || |
| 745 | !WebPPictureInit(&original_picture) || |
| 746 | !WebPConfigInit(&config)) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 747 | fprintf(stderr, "Error! Version mismatch!\n"); |
James Zern | 256afef | 2012-07-27 18:56:55 -0700 | [diff] [blame] | 748 | return -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | if (argc == 1) { |
| 752 | HelpShort(); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | for (c = 1; c < argc; ++c) { |
| 757 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 758 | HelpShort(); |
| 759 | return 0; |
| 760 | } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) { |
| 761 | HelpLong(); |
| 762 | return 0; |
| 763 | } else if (!strcmp(argv[c], "-o") && c < argc - 1) { |
| 764 | out_file = argv[++c]; |
| 765 | } else if (!strcmp(argv[c], "-d") && c < argc - 1) { |
| 766 | dump_file = argv[++c]; |
| 767 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 768 | } else if (!strcmp(argv[c], "-print_psnr")) { |
| 769 | config.show_compressed = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 770 | print_distortion = 0; |
| 771 | } else if (!strcmp(argv[c], "-print_ssim")) { |
| 772 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 773 | print_distortion = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 774 | } else if (!strcmp(argv[c], "-print_lsim")) { |
| 775 | config.show_compressed = 1; |
| 776 | print_distortion = 2; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 777 | } else if (!strcmp(argv[c], "-short")) { |
| 778 | short_output++; |
| 779 | } else if (!strcmp(argv[c], "-s") && c < argc - 2) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 780 | picture.width = strtol(argv[++c], NULL, 0); |
| 781 | picture.height = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 782 | } else if (!strcmp(argv[c], "-m") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 783 | config.method = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 784 | } else if (!strcmp(argv[c], "-q") && c < argc - 1) { |
Mikolaj Zalewski | 2aa6b80 | 2011-06-28 16:02:56 +0200 | [diff] [blame] | 785 | config.quality = (float)strtod(argv[++c], NULL); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 786 | } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) { |
| 787 | config.alpha_quality = strtol(argv[++c], NULL, 0); |
| 788 | } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) { |
| 789 | config.alpha_compression = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 790 | } else if (!strcmp(argv[c], "-alpha_cleanup")) { |
| 791 | keep_alpha = keep_alpha ? 2 : 0; |
Vikas Arora | 252028a | 2012-01-05 13:04:30 +0530 | [diff] [blame] | 792 | } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) { |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 793 | ++c; |
| 794 | if (!strcmp(argv[c], "none")) { |
| 795 | config.alpha_filtering = 0; |
| 796 | } else if (!strcmp(argv[c], "fast")) { |
| 797 | config.alpha_filtering = 1; |
| 798 | } else if (!strcmp(argv[c], "best")) { |
| 799 | config.alpha_filtering = 2; |
| 800 | } else { |
| 801 | fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]); |
| 802 | goto Error; |
| 803 | } |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 804 | } else if (!strcmp(argv[c], "-noalpha")) { |
| 805 | keep_alpha = 0; |
Pascal Massimino | 72920ca | 2012-04-24 10:59:08 +0000 | [diff] [blame] | 806 | } else if (!strcmp(argv[c], "-lossless")) { |
| 807 | config.lossless = 1; |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 808 | picture.use_argb = 1; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 809 | } else if (!strcmp(argv[c], "-hint") && c < argc - 1) { |
| 810 | ++c; |
| 811 | if (!strcmp(argv[c], "photo")) { |
| 812 | config.image_hint = WEBP_HINT_PHOTO; |
| 813 | } else if (!strcmp(argv[c], "picture")) { |
| 814 | config.image_hint = WEBP_HINT_PICTURE; |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 815 | } else if (!strcmp(argv[c], "graph")) { |
| 816 | config.image_hint = WEBP_HINT_GRAPH; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 817 | } else { |
| 818 | fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]); |
| 819 | goto Error; |
| 820 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 821 | } else if (!strcmp(argv[c], "-size") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 822 | config.target_size = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 823 | } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) { |
Mikolaj Zalewski | 2aa6b80 | 2011-06-28 16:02:56 +0200 | [diff] [blame] | 824 | config.target_PSNR = (float)strtod(argv[++c], NULL); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 825 | } else if (!strcmp(argv[c], "-sns") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 826 | config.sns_strength = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 827 | } else if (!strcmp(argv[c], "-f") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 828 | config.filter_strength = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 829 | } else if (!strcmp(argv[c], "-af")) { |
| 830 | config.autofilter = 1; |
Pascal Massimino | 842c009 | 2011-05-05 18:10:08 -0700 | [diff] [blame] | 831 | } else if (!strcmp(argv[c], "-strong")) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 832 | config.filter_type = 1; |
| 833 | } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 834 | config.filter_sharpness = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 835 | } else if (!strcmp(argv[c], "-pass") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 836 | config.pass = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 837 | } else if (!strcmp(argv[c], "-pre") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 838 | config.preprocessing = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 839 | } else if (!strcmp(argv[c], "-segments") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 840 | config.segments = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 841 | } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) { |
| 842 | config.partition_limit = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 843 | } else if (!strcmp(argv[c], "-map") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 844 | picture.extra_info_type = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 845 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 846 | } else if (!strcmp(argv[c], "-444")) { |
| 847 | picture.colorspace = WEBP_YUV444; |
| 848 | } else if (!strcmp(argv[c], "-422")) { |
| 849 | picture.colorspace = WEBP_YUV422; |
| 850 | } else if (!strcmp(argv[c], "-gray")) { |
| 851 | picture.colorspace = WEBP_YUV400; |
| 852 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 853 | } else if (!strcmp(argv[c], "-crop") && c < argc - 4) { |
| 854 | crop = 1; |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 855 | crop_x = strtol(argv[++c], NULL, 0); |
| 856 | crop_y = strtol(argv[++c], NULL, 0); |
| 857 | crop_w = strtol(argv[++c], NULL, 0); |
| 858 | crop_h = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 859 | } else if (!strcmp(argv[c], "-resize") && c < argc - 2) { |
| 860 | resize_w = strtol(argv[++c], NULL, 0); |
| 861 | resize_h = strtol(argv[++c], NULL, 0); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 862 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 863 | } else if (!strcmp(argv[c], "-noasm")) { |
Pascal Massimino | e06ac08 | 2011-09-02 21:30:08 +0000 | [diff] [blame] | 864 | VP8GetCPUInfo = NULL; |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 865 | #endif |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 866 | } else if (!strcmp(argv[c], "-version")) { |
| 867 | const int version = WebPGetEncoderVersion(); |
| 868 | printf("%d.%d.%d\n", |
| 869 | (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff); |
| 870 | return 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 871 | } else if (!strcmp(argv[c], "-progress")) { |
| 872 | show_progress = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 873 | } else if (!strcmp(argv[c], "-quiet")) { |
| 874 | quiet = 1; |
| 875 | } else if (!strcmp(argv[c], "-preset") && c < argc - 1) { |
| 876 | WebPPreset preset; |
| 877 | ++c; |
| 878 | if (!strcmp(argv[c], "default")) { |
| 879 | preset = WEBP_PRESET_DEFAULT; |
| 880 | } else if (!strcmp(argv[c], "photo")) { |
| 881 | preset = WEBP_PRESET_PHOTO; |
| 882 | } else if (!strcmp(argv[c], "picture")) { |
| 883 | preset = WEBP_PRESET_PICTURE; |
| 884 | } else if (!strcmp(argv[c], "drawing")) { |
| 885 | preset = WEBP_PRESET_DRAWING; |
| 886 | } else if (!strcmp(argv[c], "icon")) { |
| 887 | preset = WEBP_PRESET_ICON; |
| 888 | } else if (!strcmp(argv[c], "text")) { |
| 889 | preset = WEBP_PRESET_TEXT; |
| 890 | } else { |
| 891 | fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]); |
| 892 | goto Error; |
| 893 | } |
| 894 | if (!WebPConfigPreset(&config, preset, config.quality)) { |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 895 | fprintf(stderr, "Error! Could initialize configuration with preset.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 896 | goto Error; |
| 897 | } |
| 898 | } else if (!strcmp(argv[c], "-v")) { |
| 899 | verbose = 1; |
| 900 | } else if (argv[c][0] == '-') { |
| 901 | fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]); |
| 902 | HelpLong(); |
| 903 | return -1; |
| 904 | } else { |
| 905 | in_file = argv[c]; |
| 906 | } |
| 907 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 908 | if (in_file == NULL) { |
| 909 | fprintf(stderr, "No input file specified!\n"); |
| 910 | HelpShort(); |
| 911 | goto Error; |
| 912 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 913 | |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 914 | // Check for unsupported command line options for lossless mode and log |
| 915 | // warning for such options. |
Pascal Massimino | 0275159 | 2012-06-20 09:20:34 +0000 | [diff] [blame] | 916 | if (!quiet && config.lossless == 1) { |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 917 | if (config.target_size > 0 || config.target_PSNR > 0) { |
| 918 | fprintf(stderr, "Encoding for specified size or PSNR is not supported" |
| 919 | " for lossless encoding. Ignoring such option(s)!\n"); |
| 920 | } |
| 921 | if (config.partition_limit > 0) { |
| 922 | fprintf(stderr, "Partition limit option is not required for lossless" |
| 923 | " encoding. Ignoring this option!\n"); |
| 924 | } |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 925 | } |
| 926 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 927 | if (!WebPValidateConfig(&config)) { |
| 928 | fprintf(stderr, "Error! Invalid configuration.\n"); |
| 929 | goto Error; |
| 930 | } |
| 931 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 932 | // Read the input |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 933 | if (verbose) { |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 934 | StopwatchReadAndReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 935 | } |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 936 | if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) { |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 937 | fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 938 | goto Error; |
| 939 | } |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 940 | picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL; |
| 941 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 942 | if (verbose) { |
| 943 | const double time = StopwatchReadAndReset(&stop_watch); |
| 944 | fprintf(stderr, "Time to read input: %.3fs\n", time); |
| 945 | } |
| 946 | |
| 947 | // Open the output |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 948 | if (out_file) { |
| 949 | out = fopen(out_file, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 950 | if (out == NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 951 | fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file); |
| 952 | goto Error; |
| 953 | } else { |
| 954 | if (!short_output && !quiet) { |
| 955 | fprintf(stderr, "Saving file '%s'\n", out_file); |
| 956 | } |
| 957 | } |
| 958 | picture.writer = MyWriter; |
| 959 | picture.custom_ptr = (void*)out; |
| 960 | } else { |
| 961 | out = NULL; |
| 962 | if (!quiet && !short_output) { |
| 963 | fprintf(stderr, "No output file specified (no -o flag). Encoding will\n"); |
| 964 | fprintf(stderr, "be performed, but its results discarded.\n\n"); |
| 965 | } |
| 966 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 967 | if (!quiet) { |
| 968 | picture.stats = &stats; |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 969 | picture.user_data = (void*)in_file; |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 970 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 971 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 972 | // Compress |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 973 | if (verbose) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 974 | StopwatchReadAndReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 975 | } |
Pascal Massimino | 31b68fe | 2012-06-21 00:30:43 -0700 | [diff] [blame] | 976 | if (crop != 0) { |
| 977 | // We use self-cropping using a view. |
| 978 | if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) { |
| 979 | fprintf(stderr, "Error! Cannot crop picture\n"); |
| 980 | goto Error; |
| 981 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 982 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 983 | if ((resize_w | resize_h) > 0) { |
| 984 | if (!WebPPictureRescale(&picture, resize_w, resize_h)) { |
| 985 | fprintf(stderr, "Error! Cannot resize picture\n"); |
| 986 | goto Error; |
| 987 | } |
| 988 | } |
| 989 | if (picture.extra_info_type > 0) { |
| 990 | AllocExtraInfo(&picture); |
| 991 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 992 | if (print_distortion >= 0) { // Save original picture for later comparison |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 993 | WebPPictureCopy(&picture, &original_picture); |
| 994 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 995 | if (!WebPEncode(&config, &picture)) { |
| 996 | fprintf(stderr, "Error! Cannot encode picture as WebP\n"); |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 997 | fprintf(stderr, "Error code: %d (%s)\n", |
| 998 | picture.error_code, kErrorMessages[picture.error_code]); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 999 | goto Error; |
| 1000 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1001 | if (verbose) { |
| 1002 | const double time = StopwatchReadAndReset(&stop_watch); |
| 1003 | fprintf(stderr, "Time to encode picture: %.3fs\n", time); |
| 1004 | } |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 1005 | |
| 1006 | // Write info |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1007 | if (dump_file) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 1008 | if (picture.use_argb) { |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 1009 | fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode."); |
| 1010 | } else if (!DumpPicture(&picture, dump_file)) { |
| 1011 | fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file); |
| 1012 | } |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1013 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1014 | |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1015 | if (!quiet) { |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 1016 | if (config.lossless) { |
| 1017 | PrintExtraInfoLossless(&picture, short_output, in_file); |
| 1018 | } else { |
| 1019 | PrintExtraInfoLossy(&picture, short_output, in_file); |
| 1020 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1021 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 1022 | if (!quiet && !short_output && print_distortion >= 0) { // print distortion |
| 1023 | static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" }; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1024 | float values[5]; |
| 1025 | WebPPictureDistortion(&picture, &original_picture, |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 1026 | print_distortion, values); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1027 | fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n", |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 1028 | distortion_names[print_distortion], |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1029 | values[0], values[1], values[2], values[3], values[4]); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 1030 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 1031 | return_value = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1032 | |
| 1033 | Error: |
| 1034 | free(picture.extra_info); |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 1035 | MetadataFree(&metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1036 | WebPPictureFree(&picture); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 1037 | WebPPictureFree(&original_picture); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1038 | if (out != NULL) { |
| 1039 | fclose(out); |
| 1040 | } |
| 1041 | |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 1042 | return return_value; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 1045 | //------------------------------------------------------------------------------ |