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