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