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