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