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