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 | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 21 | #ifdef HAVE_WINCODEC_H |
| 22 | #ifdef __MINGW32__ |
| 23 | #define INITGUID // Without this GUIDs are declared extern and fail to link |
| 24 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 25 | #define CINTERFACE |
| 26 | #define COBJMACROS |
| 27 | #define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++ |
| 28 | // code with COBJMACROS. |
| 29 | #include <shlwapi.h> |
| 30 | #include <windows.h> |
| 31 | #include <wincodec.h> |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 32 | #endif /* HAVE_WINCODEC_H */ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | #include "webp/encode.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 36 | |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 37 | #include "./metadata.h" |
James Zern | 00b29e2 | 2012-05-15 13:48:11 -0700 | [diff] [blame] | 38 | #include "./stopwatch.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 39 | |
| 40 | #include "./jpegdec.h" |
| 41 | #include "./pngdec.h" |
James Zern | 1c1c564 | 2012-12-06 14:04:36 -0800 | [diff] [blame] | 42 | #include "./tiffdec.h" |
skal | 1bd287a | 2012-12-11 11:02:39 +0100 | [diff] [blame] | 43 | |
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 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 291 | typedef enum { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 292 | PNG_ = 0, |
| 293 | JPEG_, |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 294 | TIFF_, // 'TIFF' clashes with libtiff |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 295 | UNSUPPORTED |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 296 | } InputFileFormat; |
| 297 | |
| 298 | static InputFileFormat GetImageType(FILE* in_file) { |
| 299 | InputFileFormat format = UNSUPPORTED; |
| 300 | unsigned int magic; |
| 301 | unsigned char buf[4]; |
| 302 | |
| 303 | if ((fread(&buf[0], 4, 1, in_file) != 1) || |
| 304 | (fseek(in_file, 0, SEEK_SET) != 0)) { |
| 305 | return format; |
| 306 | } |
| 307 | |
| 308 | magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
| 309 | if (magic == 0x89504E47U) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 310 | format = PNG_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 311 | } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) { |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 312 | format = JPEG_; |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 313 | } else if (magic == 0x49492A00 || magic == 0x4D4D002A) { |
| 314 | format = TIFF_; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 315 | } |
| 316 | return format; |
| 317 | } |
| 318 | |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 319 | static int ReadPicture(const char* const filename, WebPPicture* const pic, |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 320 | int keep_alpha, Metadata* const metadata) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 321 | int ok = 0; |
| 322 | FILE* in_file = fopen(filename, "rb"); |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 323 | (void)metadata; // TODO(jzern): add metadata extraction to the formats below. |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 324 | if (in_file == NULL) { |
| 325 | fprintf(stderr, "Error! Cannot open input file '%s'\n", filename); |
| 326 | return ok; |
| 327 | } |
| 328 | |
| 329 | if (pic->width == 0 || pic->height == 0) { |
| 330 | // If no size specified, try to decode it as PNG/JPEG (as appropriate). |
| 331 | const InputFileFormat format = GetImageType(in_file); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 332 | if (format == PNG_) { |
James Zern | dba64d9 | 2012-12-04 19:00:30 -0800 | [diff] [blame] | 333 | ok = ReadPNG(in_file, pic, keep_alpha, metadata); |
James Zern | d8921dd | 2012-07-02 11:24:23 -0700 | [diff] [blame] | 334 | } else if (format == JPEG_) { |
James Zern | 2c72496 | 2012-12-16 19:13:56 -0800 | [diff] [blame] | 335 | ok = ReadJPEG(in_file, pic, metadata); |
James Zern | 6f76d24 | 2012-07-01 17:55:21 -0700 | [diff] [blame] | 336 | } else if (format == TIFF_) { |
James Zern | 2914ecf | 2012-12-15 19:41:03 -0800 | [diff] [blame] | 337 | ok = ReadTIFF(filename, pic, keep_alpha, metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 338 | } |
| 339 | } else { |
| 340 | // If image size is specified, infer it as YUV format. |
| 341 | ok = ReadYUV(in_file, pic); |
| 342 | } |
| 343 | if (!ok) { |
| 344 | fprintf(stderr, "Error! Could not process file %s\n", filename); |
| 345 | } |
| 346 | |
| 347 | fclose(in_file); |
| 348 | return ok; |
| 349 | } |
| 350 | |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 351 | #endif // !HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 352 | |
| 353 | static void AllocExtraInfo(WebPPicture* const pic) { |
| 354 | const int mb_w = (pic->width + 15) / 16; |
| 355 | const int mb_h = (pic->height + 15) / 16; |
| 356 | pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info)); |
| 357 | } |
| 358 | |
| 359 | static void PrintByteCount(const int bytes[4], int total_size, |
| 360 | int* const totals) { |
| 361 | int s; |
| 362 | int total = 0; |
| 363 | for (s = 0; s < 4; ++s) { |
| 364 | fprintf(stderr, "| %7d ", bytes[s]); |
| 365 | total += bytes[s]; |
| 366 | if (totals) totals[s] += bytes[s]; |
| 367 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 368 | fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static void PrintPercents(const int counts[4], int total) { |
| 372 | int s; |
| 373 | for (s = 0; s < 4; ++s) { |
| 374 | fprintf(stderr, "| %2d%%", 100 * counts[s] / total); |
| 375 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 376 | fprintf(stderr, "| %7d\n", total); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void PrintValues(const int values[4]) { |
| 380 | int s; |
| 381 | for (s = 0; s < 4; ++s) { |
| 382 | fprintf(stderr, "| %7d ", values[s]); |
| 383 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 384 | fprintf(stderr, "|\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 387 | static void PrintFullLosslessInfo(const WebPAuxStats* const stats, |
| 388 | const char* const description) { |
| 389 | fprintf(stderr, "Lossless-%s compressed size: %d bytes\n", |
| 390 | description, stats->lossless_size); |
| 391 | if (stats->lossless_features) { |
| 392 | fprintf(stderr, " * Lossless features used:"); |
| 393 | if (stats->lossless_features & 1) fprintf(stderr, " PREDICTION"); |
| 394 | if (stats->lossless_features & 2) fprintf(stderr, " CROSS-COLOR-TRANSFORM"); |
| 395 | if (stats->lossless_features & 4) fprintf(stderr, " SUBTRACT-GREEN"); |
| 396 | if (stats->lossless_features & 8) fprintf(stderr, " PALETTE"); |
| 397 | fprintf(stderr, "\n"); |
| 398 | } |
| 399 | fprintf(stderr, " * Precision Bits: histogram=%d transform=%d cache=%d\n", |
| 400 | stats->histogram_bits, stats->transform_bits, stats->cache_bits); |
| 401 | if (stats->palette_size > 0) { |
| 402 | fprintf(stderr, " * Palette size: %d\n", stats->palette_size); |
| 403 | } |
| 404 | } |
| 405 | |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 406 | static void PrintExtraInfoLossless(const WebPPicture* const pic, |
| 407 | int short_output, |
| 408 | const char* const file_name) { |
| 409 | const WebPAuxStats* const stats = pic->stats; |
| 410 | if (short_output) { |
| 411 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
| 412 | } else { |
| 413 | fprintf(stderr, "File: %s\n", file_name); |
| 414 | fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height); |
| 415 | fprintf(stderr, "Output: %d bytes\n", stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 416 | PrintFullLosslessInfo(stats, "ARGB"); |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output, |
| 421 | const char* const file_name) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 422 | const WebPAuxStats* const stats = pic->stats; |
| 423 | if (short_output) { |
| 424 | fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 425 | } else { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 426 | const int num_i4 = stats->block_count[0]; |
| 427 | const int num_i16 = stats->block_count[1]; |
| 428 | const int num_skip = stats->block_count[2]; |
| 429 | const int total = num_i4 + num_i16; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 430 | fprintf(stderr, "File: %s\n", file_name); |
| 431 | fprintf(stderr, "Dimension: %d x %d%s\n", |
James Zern | c71ff9e | 2012-06-07 17:32:29 -0700 | [diff] [blame] | 432 | pic->width, pic->height, |
| 433 | stats->alpha_data_size ? " (with alpha)" : ""); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 434 | fprintf(stderr, "Output: " |
| 435 | "%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] | 436 | stats->coded_size, |
| 437 | stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]); |
| 438 | if (total > 0) { |
| 439 | int totals[4] = { 0, 0, 0, 0 }; |
| 440 | fprintf(stderr, "block count: intra4: %d\n" |
| 441 | " intra16: %d (-> %.2f%%)\n", |
| 442 | num_i4, num_i16, 100.f * num_i16 / total); |
| 443 | fprintf(stderr, " skipped block: %d (%.2f%%)\n", |
| 444 | num_skip, 100.f * num_skip / total); |
| 445 | fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n" |
| 446 | " mode-partition: %6d (%.1f%%)\n", |
| 447 | stats->header_bytes[0], |
| 448 | 100.f * stats->header_bytes[0] / stats->coded_size, |
| 449 | stats->header_bytes[1], |
| 450 | 100.f * stats->header_bytes[1] / stats->coded_size); |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 451 | if (stats->alpha_data_size > 0) { |
| 452 | fprintf(stderr, " transparency: %6d (%.1f dB)\n", |
| 453 | stats->alpha_data_size, stats->PSNR[4]); |
Pascal Massimino | 2ab4b72 | 2011-04-25 16:58:04 -0700 | [diff] [blame] | 454 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 455 | if (stats->layer_data_size) { |
| 456 | fprintf(stderr, " enhancement: %6d\n", |
| 457 | stats->layer_data_size); |
| 458 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 459 | fprintf(stderr, " Residuals bytes " |
| 460 | "|segment 1|segment 2|segment 3" |
| 461 | "|segment 4| total\n"); |
| 462 | fprintf(stderr, " intra4-coeffs: "); |
| 463 | PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals); |
| 464 | fprintf(stderr, " intra16-coeffs: "); |
| 465 | PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals); |
| 466 | fprintf(stderr, " chroma coeffs: "); |
| 467 | PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals); |
| 468 | fprintf(stderr, " macroblocks: "); |
| 469 | PrintPercents(stats->segment_size, total); |
| 470 | fprintf(stderr, " quantizer: "); |
| 471 | PrintValues(stats->segment_quant); |
| 472 | fprintf(stderr, " filter level: "); |
| 473 | PrintValues(stats->segment_level); |
| 474 | fprintf(stderr, "------------------+---------"); |
| 475 | fprintf(stderr, "+---------+---------+---------+-----------------\n"); |
| 476 | fprintf(stderr, " segments total: "); |
| 477 | PrintByteCount(totals, stats->coded_size, NULL); |
| 478 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 479 | if (stats->lossless_size > 0) { |
| 480 | PrintFullLosslessInfo(stats, "alpha"); |
| 481 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 482 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 483 | if (pic->extra_info != NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 484 | const int mb_w = (pic->width + 15) / 16; |
| 485 | const int mb_h = (pic->height + 15) / 16; |
| 486 | const int type = pic->extra_info_type; |
| 487 | int x, y; |
| 488 | for (y = 0; y < mb_h; ++y) { |
| 489 | for (x = 0; x < mb_w; ++x) { |
| 490 | const int c = pic->extra_info[x + y * mb_w]; |
| 491 | if (type == 1) { // intra4/intra16 |
| 492 | printf("%c", "+."[c]); |
| 493 | } else if (type == 2) { // segments |
| 494 | printf("%c", ".-*X"[c]); |
| 495 | } else if (type == 3) { // quantizers |
| 496 | printf("%.2d ", c); |
| 497 | } else if (type == 6 || type == 7) { |
| 498 | printf("%3d ", c); |
| 499 | } else { |
| 500 | printf("0x%.2x ", c); |
| 501 | } |
| 502 | } |
| 503 | printf("\n"); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 508 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 509 | |
| 510 | static int MyWriter(const uint8_t* data, size_t data_size, |
| 511 | const WebPPicture* const pic) { |
| 512 | FILE* const out = (FILE*)pic->custom_ptr; |
| 513 | return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1; |
| 514 | } |
| 515 | |
| 516 | // Dumps a picture as a PGM file using the IMC4 layout. |
| 517 | static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) { |
| 518 | int y; |
| 519 | const int uv_width = (picture->width + 1) / 2; |
| 520 | const int uv_height = (picture->height + 1) / 2; |
| 521 | const int stride = (picture->width + 1) & ~1; |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 522 | const int alpha_height = |
| 523 | WebPPictureHasTransparency(picture) ? picture->height : 0; |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 524 | const int height = picture->height + uv_height + alpha_height; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 525 | FILE* const f = fopen(PGM_name, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 526 | if (f == NULL) return 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 527 | fprintf(f, "P5\n%d %d\n255\n", stride, height); |
| 528 | for (y = 0; y < picture->height; ++y) { |
| 529 | if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1) |
| 530 | return 0; |
| 531 | if (picture->width & 1) fputc(0, f); // pad |
| 532 | } |
| 533 | for (y = 0; y < uv_height; ++y) { |
| 534 | if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 535 | return 0; |
| 536 | if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1) |
| 537 | return 0; |
| 538 | } |
Pascal Massimino | 5142a0b | 2011-07-07 16:08:15 -0700 | [diff] [blame] | 539 | for (y = 0; y < alpha_height; ++y) { |
| 540 | if (fwrite(picture->a + y * picture->a_stride, picture->width, 1, f) != 1) |
| 541 | return 0; |
| 542 | if (picture->width & 1) fputc(0, f); // pad |
| 543 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 544 | fclose(f); |
| 545 | return 1; |
| 546 | } |
| 547 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 548 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 549 | |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 550 | static int ProgressReport(int percent, const WebPPicture* const picture) { |
| 551 | printf("[%s]: %3d %% \r", |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 552 | (char*)picture->user_data, percent); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 553 | fflush(stdout); |
| 554 | return 1; // all ok |
| 555 | } |
| 556 | |
| 557 | //------------------------------------------------------------------------------ |
| 558 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 559 | static void HelpShort(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 560 | printf("Usage:\n\n"); |
| 561 | printf(" cwebp [options] -q quality input.png -o output.webp\n\n"); |
| 562 | printf("where quality is between 0 (poor) to 100 (very good).\n"); |
| 563 | printf("Typical value is around 80.\n\n"); |
| 564 | printf("Try -longhelp for an exhaustive list of advanced options.\n"); |
| 565 | } |
| 566 | |
Pascal Massimino | f8db5d5 | 2011-03-25 15:04:11 -0700 | [diff] [blame] | 567 | static void HelpLong(void) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 568 | printf("Usage:\n"); |
| 569 | printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n"); |
| 570 | printf("If input size (-s) for an image is not specified, " |
James Zern | 12f9aed | 2012-07-13 14:55:39 -0700 | [diff] [blame] | 571 | "it is assumed to be a PNG, JPEG or TIFF file.\n"); |
James Zern | 31f9dc6 | 2011-06-06 17:56:50 -0700 | [diff] [blame] | 572 | #ifdef HAVE_WINCODEC_H |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 573 | printf("Windows builds can take as input any of the files handled by WIC\n"); |
| 574 | #endif |
| 575 | printf("options:\n"); |
| 576 | printf(" -h / -help ............ short help\n"); |
| 577 | printf(" -H / -longhelp ........ long help\n"); |
| 578 | printf(" -q <float> ............. quality factor (0:small..100:big)\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 579 | printf(" -alpha_q <int> ......... Transparency-compression quality " |
| 580 | "(0..100).\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 581 | printf(" -preset <string> ....... Preset setting, one of:\n"); |
| 582 | printf(" default, photo, picture,\n"); |
| 583 | printf(" drawing, icon, text\n"); |
| 584 | printf(" -preset must come first, as it overwrites other parameters."); |
| 585 | printf("\n"); |
| 586 | printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n"); |
| 587 | printf(" -segments <int> ........ number of segments to use (1..4)\n"); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 588 | printf(" -size <int> ............ Target size (in bytes)\n"); |
| 589 | printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 590 | printf("\n"); |
| 591 | printf(" -s <int> <int> ......... Input size (width x height) for YUV\n"); |
| 592 | printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n"); |
| 593 | printf(" -f <int> ............... filter strength (0=off..100)\n"); |
| 594 | printf(" -sharpness <int> ....... " |
| 595 | "filter sharpness (0:most .. 7:least sharp)\n"); |
| 596 | printf(" -strong ................ use strong filter instead of simple.\n"); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 597 | printf(" -partition_limit <int> . limit quality to fit the 512k limit on\n"); |
| 598 | printf(" " |
| 599 | "the first partition (0=no degradation ... 100=full)\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 600 | printf(" -pass <int> ............ analysis pass number (1..10)\n"); |
| 601 | 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] | 602 | printf(" -resize <w> <h> ........ resize picture (after any cropping)\n"); |
| 603 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 604 | printf(" -444 / -422 / -gray ..... Change colorspace\n"); |
| 605 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 606 | printf(" -map <int> ............. print map of extra info.\n"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 607 | printf(" -print_psnr ............ prints averaged PSNR distortion.\n"); |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 608 | printf(" -print_ssim ............ prints averaged SSIM distortion.\n"); |
| 609 | printf(" -print_lsim ............ prints local-similarity distortion.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 610 | printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 611 | printf(" -alpha_method <int> .... Transparency-compression method (0..1)\n"); |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 612 | printf(" -alpha_filter <string> . predictive filtering for alpha plane.\n"); |
| 613 | printf(" One of: none, fast (default) or best.\n"); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 614 | printf(" -alpha_cleanup ......... Clean RGB values in transparent area.\n"); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 615 | printf(" -noalpha ............... discard any transparency information.\n"); |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 616 | printf(" -lossless .............. Encode image losslessly.\n"); |
| 617 | printf(" -hint <string> ......... Specify image characteristics hint.\n"); |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 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"); |
| 621 | printf(" -short ................. condense printed message\n"); |
| 622 | printf(" -quiet ................. don't print anything.\n"); |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 623 | printf(" -version ............... print version number and exit.\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 624 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 625 | printf(" -noasm ................. disable all assembly optimizations.\n"); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 626 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 627 | printf(" -v ..................... verbose, e.g. print encoding/decoding " |
| 628 | "times\n"); |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 629 | printf(" -progress .............. report encoding progress\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 630 | printf("\n"); |
| 631 | printf("Experimental Options:\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 632 | printf(" -af .................... auto-adjust filter strength.\n"); |
| 633 | printf(" -pre <int> ............. pre-processing filter\n"); |
| 634 | printf("\n"); |
| 635 | } |
| 636 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 637 | //------------------------------------------------------------------------------ |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 638 | // Error messages |
| 639 | |
| 640 | static const char* const kErrorMessages[] = { |
| 641 | "OK", |
| 642 | "OUT_OF_MEMORY: Out of memory allocating objects", |
| 643 | "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer", |
| 644 | "NULL_PARAMETER: NULL parameter passed to function", |
| 645 | "INVALID_CONFIGURATION: configuration is invalid", |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 646 | "BAD_DIMENSION: Bad picture dimension. Maximum width and height " |
| 647 | "allowed is 16383 pixels.", |
| 648 | "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n" |
| 649 | "To reduce the size of this partition, try using less segments " |
| 650 | "with the -segments option, and eventually reduce the number of " |
| 651 | "header bits using -partition_limit. More details are available " |
| 652 | "in the manual (`man cwebp`)", |
| 653 | "PARTITION_OVERFLOW: Partition is too big to fit 16M", |
Pascal Massimino | d71fbdc | 2011-12-01 03:34:22 -0800 | [diff] [blame] | 654 | "BAD_WRITE: Picture writer returned an I/O error", |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 655 | "FILE_TOO_BIG: File would be too big to fit in 4G", |
| 656 | "USER_ABORT: encoding abort requested by user" |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 657 | }; |
| 658 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 659 | //------------------------------------------------------------------------------ |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 660 | |
| 661 | int main(int argc, const char *argv[]) { |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 662 | int return_value = -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 663 | const char *in_file = NULL, *out_file = NULL, *dump_file = NULL; |
| 664 | FILE *out = NULL; |
| 665 | int c; |
| 666 | int short_output = 0; |
| 667 | int quiet = 0; |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 668 | int keep_alpha = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 669 | 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] | 670 | int resize_w = 0, resize_h = 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 671 | int show_progress = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 672 | WebPPicture picture; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 673 | int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 674 | WebPPicture original_picture; // when PSNR or SSIM is requested |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 675 | WebPConfig config; |
| 676 | WebPAuxStats stats; |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 677 | Metadata metadata; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 678 | Stopwatch stop_watch; |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 679 | |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 680 | MetadataInit(&metadata); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 681 | if (!WebPPictureInit(&picture) || |
| 682 | !WebPPictureInit(&original_picture) || |
| 683 | !WebPConfigInit(&config)) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 684 | fprintf(stderr, "Error! Version mismatch!\n"); |
James Zern | 256afef | 2012-07-27 18:56:55 -0700 | [diff] [blame] | 685 | return -1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | if (argc == 1) { |
| 689 | HelpShort(); |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | for (c = 1; c < argc; ++c) { |
| 694 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 695 | HelpShort(); |
| 696 | return 0; |
| 697 | } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) { |
| 698 | HelpLong(); |
| 699 | return 0; |
| 700 | } else if (!strcmp(argv[c], "-o") && c < argc - 1) { |
| 701 | out_file = argv[++c]; |
| 702 | } else if (!strcmp(argv[c], "-d") && c < argc - 1) { |
| 703 | dump_file = argv[++c]; |
| 704 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 705 | } else if (!strcmp(argv[c], "-print_psnr")) { |
| 706 | config.show_compressed = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 707 | print_distortion = 0; |
| 708 | } else if (!strcmp(argv[c], "-print_ssim")) { |
| 709 | config.show_compressed = 1; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 710 | print_distortion = 1; |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 711 | } else if (!strcmp(argv[c], "-print_lsim")) { |
| 712 | config.show_compressed = 1; |
| 713 | print_distortion = 2; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 714 | } else if (!strcmp(argv[c], "-short")) { |
| 715 | short_output++; |
| 716 | } else if (!strcmp(argv[c], "-s") && c < argc - 2) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 717 | picture.width = strtol(argv[++c], NULL, 0); |
| 718 | picture.height = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 719 | } else if (!strcmp(argv[c], "-m") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 720 | config.method = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 721 | } else if (!strcmp(argv[c], "-q") && c < argc - 1) { |
Mikolaj Zalewski | 2aa6b80 | 2011-06-28 16:02:56 +0200 | [diff] [blame] | 722 | config.quality = (float)strtod(argv[++c], NULL); |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 723 | } else if (!strcmp(argv[c], "-alpha_q") && c < argc - 1) { |
| 724 | config.alpha_quality = strtol(argv[++c], NULL, 0); |
| 725 | } else if (!strcmp(argv[c], "-alpha_method") && c < argc - 1) { |
| 726 | config.alpha_compression = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 2e3e8b2 | 2012-01-17 08:18:22 +0000 | [diff] [blame] | 727 | } else if (!strcmp(argv[c], "-alpha_cleanup")) { |
| 728 | keep_alpha = keep_alpha ? 2 : 0; |
Vikas Arora | 252028a | 2012-01-05 13:04:30 +0530 | [diff] [blame] | 729 | } else if (!strcmp(argv[c], "-alpha_filter") && c < argc - 1) { |
Pascal Massimino | 8ca2076 | 2012-01-08 19:27:21 -0800 | [diff] [blame] | 730 | ++c; |
| 731 | if (!strcmp(argv[c], "none")) { |
| 732 | config.alpha_filtering = 0; |
| 733 | } else if (!strcmp(argv[c], "fast")) { |
| 734 | config.alpha_filtering = 1; |
| 735 | } else if (!strcmp(argv[c], "best")) { |
| 736 | config.alpha_filtering = 2; |
| 737 | } else { |
| 738 | fprintf(stderr, "Error! Unrecognized alpha filter: %s\n", argv[c]); |
| 739 | goto Error; |
| 740 | } |
Vikas Arora | a0ec9aa | 2011-12-01 15:11:34 +0530 | [diff] [blame] | 741 | } else if (!strcmp(argv[c], "-noalpha")) { |
| 742 | keep_alpha = 0; |
Pascal Massimino | 72920ca | 2012-04-24 10:59:08 +0000 | [diff] [blame] | 743 | } else if (!strcmp(argv[c], "-lossless")) { |
| 744 | config.lossless = 1; |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 745 | picture.use_argb = 1; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 746 | } else if (!strcmp(argv[c], "-hint") && c < argc - 1) { |
| 747 | ++c; |
| 748 | if (!strcmp(argv[c], "photo")) { |
| 749 | config.image_hint = WEBP_HINT_PHOTO; |
| 750 | } else if (!strcmp(argv[c], "picture")) { |
| 751 | config.image_hint = WEBP_HINT_PICTURE; |
Vikas Arora | dd1c387 | 2012-07-31 23:07:52 -0700 | [diff] [blame] | 752 | } else if (!strcmp(argv[c], "graph")) { |
| 753 | config.image_hint = WEBP_HINT_GRAPH; |
Vikas Arora | d373076 | 2012-06-22 12:14:48 +0530 | [diff] [blame] | 754 | } else { |
| 755 | fprintf(stderr, "Error! Unrecognized image hint: %s\n", argv[c]); |
| 756 | goto Error; |
| 757 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 758 | } else if (!strcmp(argv[c], "-size") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 759 | config.target_size = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 760 | } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) { |
Mikolaj Zalewski | 2aa6b80 | 2011-06-28 16:02:56 +0200 | [diff] [blame] | 761 | config.target_PSNR = (float)strtod(argv[++c], NULL); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 762 | } else if (!strcmp(argv[c], "-sns") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 763 | config.sns_strength = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 764 | } else if (!strcmp(argv[c], "-f") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 765 | config.filter_strength = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 766 | } else if (!strcmp(argv[c], "-af")) { |
| 767 | config.autofilter = 1; |
Pascal Massimino | 842c009 | 2011-05-05 18:10:08 -0700 | [diff] [blame] | 768 | } else if (!strcmp(argv[c], "-strong")) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 769 | config.filter_type = 1; |
| 770 | } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 771 | config.filter_sharpness = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 772 | } else if (!strcmp(argv[c], "-pass") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 773 | config.pass = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 774 | } else if (!strcmp(argv[c], "-pre") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 775 | config.preprocessing = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 776 | } else if (!strcmp(argv[c], "-segments") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 777 | config.segments = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 900286e | 2011-08-23 15:58:22 -0700 | [diff] [blame] | 778 | } else if (!strcmp(argv[c], "-partition_limit") && c < argc - 1) { |
| 779 | config.partition_limit = strtol(argv[++c], NULL, 0); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 780 | } else if (!strcmp(argv[c], "-map") && c < argc - 1) { |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 781 | picture.extra_info_type = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 782 | #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 783 | } else if (!strcmp(argv[c], "-444")) { |
| 784 | picture.colorspace = WEBP_YUV444; |
| 785 | } else if (!strcmp(argv[c], "-422")) { |
| 786 | picture.colorspace = WEBP_YUV422; |
| 787 | } else if (!strcmp(argv[c], "-gray")) { |
| 788 | picture.colorspace = WEBP_YUV400; |
| 789 | #endif |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 790 | } else if (!strcmp(argv[c], "-crop") && c < argc - 4) { |
| 791 | crop = 1; |
Pascal Massimino | 4b0b0d6 | 2011-03-26 09:27:45 -0700 | [diff] [blame] | 792 | crop_x = strtol(argv[++c], NULL, 0); |
| 793 | crop_y = strtol(argv[++c], NULL, 0); |
| 794 | crop_w = strtol(argv[++c], NULL, 0); |
| 795 | crop_h = strtol(argv[++c], NULL, 0); |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 796 | } else if (!strcmp(argv[c], "-resize") && c < argc - 2) { |
| 797 | resize_w = strtol(argv[++c], NULL, 0); |
| 798 | resize_h = strtol(argv[++c], NULL, 0); |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 799 | #ifndef WEBP_DLL |
Pascal Massimino | cfbf88a | 2011-04-22 12:14:45 -0700 | [diff] [blame] | 800 | } else if (!strcmp(argv[c], "-noasm")) { |
Pascal Massimino | e06ac08 | 2011-09-02 21:30:08 +0000 | [diff] [blame] | 801 | VP8GetCPUInfo = NULL; |
James Zern | b4d0ef8 | 2011-07-15 14:53:03 -0700 | [diff] [blame] | 802 | #endif |
Pascal Massimino | 650ffa3 | 2011-03-24 16:17:10 -0700 | [diff] [blame] | 803 | } else if (!strcmp(argv[c], "-version")) { |
| 804 | const int version = WebPGetEncoderVersion(); |
| 805 | printf("%d.%d.%d\n", |
| 806 | (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff); |
| 807 | return 0; |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 808 | } else if (!strcmp(argv[c], "-progress")) { |
| 809 | show_progress = 1; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 810 | } else if (!strcmp(argv[c], "-quiet")) { |
| 811 | quiet = 1; |
| 812 | } else if (!strcmp(argv[c], "-preset") && c < argc - 1) { |
| 813 | WebPPreset preset; |
| 814 | ++c; |
| 815 | if (!strcmp(argv[c], "default")) { |
| 816 | preset = WEBP_PRESET_DEFAULT; |
| 817 | } else if (!strcmp(argv[c], "photo")) { |
| 818 | preset = WEBP_PRESET_PHOTO; |
| 819 | } else if (!strcmp(argv[c], "picture")) { |
| 820 | preset = WEBP_PRESET_PICTURE; |
| 821 | } else if (!strcmp(argv[c], "drawing")) { |
| 822 | preset = WEBP_PRESET_DRAWING; |
| 823 | } else if (!strcmp(argv[c], "icon")) { |
| 824 | preset = WEBP_PRESET_ICON; |
| 825 | } else if (!strcmp(argv[c], "text")) { |
| 826 | preset = WEBP_PRESET_TEXT; |
| 827 | } else { |
| 828 | fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]); |
| 829 | goto Error; |
| 830 | } |
| 831 | if (!WebPConfigPreset(&config, preset, config.quality)) { |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 832 | fprintf(stderr, "Error! Could initialize configuration with preset.\n"); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 833 | goto Error; |
| 834 | } |
| 835 | } else if (!strcmp(argv[c], "-v")) { |
| 836 | verbose = 1; |
| 837 | } else if (argv[c][0] == '-') { |
| 838 | fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]); |
| 839 | HelpLong(); |
| 840 | return -1; |
| 841 | } else { |
| 842 | in_file = argv[c]; |
| 843 | } |
| 844 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 845 | if (in_file == NULL) { |
| 846 | fprintf(stderr, "No input file specified!\n"); |
| 847 | HelpShort(); |
| 848 | goto Error; |
| 849 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 850 | |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 851 | // Check for unsupported command line options for lossless mode and log |
| 852 | // warning for such options. |
Pascal Massimino | 0275159 | 2012-06-20 09:20:34 +0000 | [diff] [blame] | 853 | if (!quiet && config.lossless == 1) { |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 854 | if (config.target_size > 0 || config.target_PSNR > 0) { |
| 855 | fprintf(stderr, "Encoding for specified size or PSNR is not supported" |
| 856 | " for lossless encoding. Ignoring such option(s)!\n"); |
| 857 | } |
| 858 | if (config.partition_limit > 0) { |
| 859 | fprintf(stderr, "Partition limit option is not required for lossless" |
| 860 | " encoding. Ignoring this option!\n"); |
| 861 | } |
Vikas Arora | b7fb0ed | 2012-06-20 09:02:25 +0530 | [diff] [blame] | 862 | } |
| 863 | |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 864 | if (!WebPValidateConfig(&config)) { |
| 865 | fprintf(stderr, "Error! Invalid configuration.\n"); |
| 866 | goto Error; |
| 867 | } |
| 868 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 869 | // Read the input |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 870 | if (verbose) { |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 871 | StopwatchReadAndReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 872 | } |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 873 | if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) { |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 874 | fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 875 | goto Error; |
| 876 | } |
Pascal Massimino | 30971c9 | 2011-12-01 02:24:50 -0800 | [diff] [blame] | 877 | picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL; |
| 878 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 879 | if (verbose) { |
| 880 | const double time = StopwatchReadAndReset(&stop_watch); |
| 881 | fprintf(stderr, "Time to read input: %.3fs\n", time); |
| 882 | } |
| 883 | |
| 884 | // Open the output |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 885 | if (out_file) { |
| 886 | out = fopen(out_file, "wb"); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 887 | if (out == NULL) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 888 | fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file); |
| 889 | goto Error; |
| 890 | } else { |
| 891 | if (!short_output && !quiet) { |
| 892 | fprintf(stderr, "Saving file '%s'\n", out_file); |
| 893 | } |
| 894 | } |
| 895 | picture.writer = MyWriter; |
| 896 | picture.custom_ptr = (void*)out; |
| 897 | } else { |
| 898 | out = NULL; |
| 899 | if (!quiet && !short_output) { |
| 900 | fprintf(stderr, "No output file specified (no -o flag). Encoding will\n"); |
| 901 | fprintf(stderr, "be performed, but its results discarded.\n\n"); |
| 902 | } |
| 903 | } |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 904 | if (!quiet) { |
| 905 | picture.stats = &stats; |
James Zern | 475d87d | 2012-07-27 19:53:16 -0700 | [diff] [blame] | 906 | picture.user_data = (void*)in_file; |
Pascal Massimino | 7d853d7 | 2012-07-24 16:15:36 -0700 | [diff] [blame] | 907 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 908 | |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 909 | // Compress |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 910 | if (verbose) { |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 911 | StopwatchReadAndReset(&stop_watch); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 912 | } |
Pascal Massimino | 31b68fe | 2012-06-21 00:30:43 -0700 | [diff] [blame] | 913 | if (crop != 0) { |
| 914 | // We use self-cropping using a view. |
| 915 | if (!WebPPictureView(&picture, crop_x, crop_y, crop_w, crop_h, &picture)) { |
| 916 | fprintf(stderr, "Error! Cannot crop picture\n"); |
| 917 | goto Error; |
| 918 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 919 | } |
Pascal Massimino | 6d0e66c | 2011-05-02 17:19:00 -0700 | [diff] [blame] | 920 | if ((resize_w | resize_h) > 0) { |
| 921 | if (!WebPPictureRescale(&picture, resize_w, resize_h)) { |
| 922 | fprintf(stderr, "Error! Cannot resize picture\n"); |
| 923 | goto Error; |
| 924 | } |
| 925 | } |
| 926 | if (picture.extra_info_type > 0) { |
| 927 | AllocExtraInfo(&picture); |
| 928 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 929 | if (print_distortion >= 0) { // Save original picture for later comparison |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 930 | WebPPictureCopy(&picture, &original_picture); |
| 931 | } |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 932 | if (!WebPEncode(&config, &picture)) { |
| 933 | fprintf(stderr, "Error! Cannot encode picture as WebP\n"); |
Pascal Massimino | ca7a2fd | 2011-06-02 06:55:03 -0700 | [diff] [blame] | 934 | fprintf(stderr, "Error code: %d (%s)\n", |
| 935 | picture.error_code, kErrorMessages[picture.error_code]); |
Pascal Massimino | 6d978a6 | 2011-03-17 14:49:19 -0700 | [diff] [blame] | 936 | goto Error; |
| 937 | } |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 938 | if (verbose) { |
| 939 | const double time = StopwatchReadAndReset(&stop_watch); |
| 940 | fprintf(stderr, "Time to encode picture: %.3fs\n", time); |
| 941 | } |
Pascal Massimino | 0744e84 | 2011-02-25 12:03:27 -0800 | [diff] [blame] | 942 | |
| 943 | // Write info |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 944 | if (dump_file) { |
Pascal Massimino | dd10817 | 2012-07-18 21:58:53 +0000 | [diff] [blame] | 945 | if (picture.use_argb) { |
Pascal Massimino | 437999f | 2012-06-04 15:50:05 -0700 | [diff] [blame] | 946 | fprintf(stderr, "Warning: can't dump file (-d option) in lossless mode."); |
| 947 | } else if (!DumpPicture(&picture, dump_file)) { |
| 948 | fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file); |
| 949 | } |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 950 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 951 | |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 952 | if (!quiet) { |
Vikas Arora | c4ccab6 | 2012-05-09 11:27:46 +0530 | [diff] [blame] | 953 | if (config.lossless) { |
| 954 | PrintExtraInfoLossless(&picture, short_output, in_file); |
| 955 | } else { |
| 956 | PrintExtraInfoLossy(&picture, short_output, in_file); |
| 957 | } |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 958 | } |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 959 | if (!quiet && !short_output && print_distortion >= 0) { // print distortion |
| 960 | static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" }; |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 961 | float values[5]; |
| 962 | WebPPictureDistortion(&picture, &original_picture, |
Pascal Massimino | f86e6ab | 2012-10-18 08:26:40 -0700 | [diff] [blame] | 963 | print_distortion, values); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 964 | 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] | 965 | distortion_names[print_distortion], |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 966 | values[0], values[1], values[2], values[3], values[4]); |
Pascal Massimino | 38369c0 | 2011-05-04 22:59:51 -0700 | [diff] [blame] | 967 | } |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 968 | return_value = 0; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 969 | |
| 970 | Error: |
| 971 | free(picture.extra_info); |
James Zern | 63aba3a | 2012-12-03 18:20:00 -0800 | [diff] [blame] | 972 | MetadataFree(&metadata); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 973 | WebPPictureFree(&picture); |
Pascal Massimino | d61479f | 2012-01-20 07:20:56 -0800 | [diff] [blame] | 974 | WebPPictureFree(&original_picture); |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 975 | if (out != NULL) { |
| 976 | fclose(out); |
| 977 | } |
| 978 | |
Pascal Massimino | 4abe04a | 2012-05-09 00:32:20 -0700 | [diff] [blame] | 979 | return return_value; |
Pascal Massimino | f61d14a | 2011-02-18 23:33:46 -0800 | [diff] [blame] | 980 | } |
| 981 | |
James Zern | c7e86ab | 2011-08-25 14:22:32 -0700 | [diff] [blame] | 982 | //------------------------------------------------------------------------------ |