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