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