1 __ __ ____ ____ ____ 2 / \\/ \/ _ \/ _ )/ _ \ 3 \ / __/ _ \ __/ 4 \__\__/\____/\_____/__/ ____ ___ 5 / _/ / \ \ / _ \/ _/ 6 / \_/ / / \ \ __/ \__ 7 \____/____/\_____/_____/____/v0.3.1 8 9Description: 10============ 11 12WebP codec: library to encode and decode images in WebP format. This package 13contains the library that can be used in other programs to add WebP support, 14as well as the command line tools 'cwebp' and 'dwebp'. 15 16See http://developers.google.com/speed/webp 17 18Latest sources are available from http://www.webmproject.org/code/ 19 20It is released under the same license as the WebM project. 21See http://www.webmproject.org/license/software/ or the 22file "COPYING" file for details. An additional intellectual 23property rights grant can be found in the file PATENTS. 24 25Building: 26========= 27 28Windows build: 29-------------- 30 31By running: 32 33 nmake /f Makefile.vc CFG=release-static RTLIBCFG=static OBJDIR=output 34 35the directory output\release-static\(x64|x86)\bin will contain the tools 36cwebp.exe and dwebp.exe. The directory output\release-static\(x64|x86)\lib will 37contain the libwebp static library. 38The target architecture (x86/x64) is detected by Makefile.vc from the Visual 39Studio compiler (cl.exe) available in the system path. 40 41Unix build using makefile.unix: 42------------------------------- 43 44On platforms with GNU tools installed (gcc and make), running 45 46 make -f makefile.unix 47 48will build the binaries examples/cwebp and examples/dwebp, along 49with the static library src/libwebp.a. No system-wide installation 50is supplied, as this is a simple alternative to the full installation 51system based on the autoconf tools (see below). 52Please refer to makefile.unix for additional details and customizations. 53 54Using autoconf tools: 55--------------------- 56When building from git sources, you will need to run autogen.sh to generate the 57configure script. 58 59./configure 60make 61make install 62 63should be all you need to have the following files 64 65/usr/local/include/webp/decode.h 66/usr/local/include/webp/encode.h 67/usr/local/include/webp/types.h 68/usr/local/lib/libwebp.* 69/usr/local/bin/cwebp 70/usr/local/bin/dwebp 71 72installed. 73 74Note: A decode-only library, libwebpdecoder, is available using the 75'--enable-libwebpdecoder' flag. The encode library is built separately and can 76be installed independently using a minor modification in the corresponding 77Makefile.am configure files (see comments there). See './configure --help' for 78more options. 79 80SWIG bindings: 81-------------- 82 83To generate language bindings from swig/libwebp.i at least swig-1.3 84(http://www.swig.org) is required. 85 86Currently the following functions are mapped: 87Decode: 88 WebPGetDecoderVersion 89 WebPGetInfo 90 WebPDecodeRGBA 91 WebPDecodeARGB 92 WebPDecodeBGRA 93 WebPDecodeBGR 94 WebPDecodeRGB 95 96Encode: 97 WebPGetEncoderVersion 98 WebPEncodeRGBA 99 WebPEncodeBGRA 100 WebPEncodeRGB 101 WebPEncodeBGR 102 WebPEncodeLosslessRGBA 103 WebPEncodeLosslessBGRA 104 WebPEncodeLosslessRGB 105 WebPEncodeLosslessBGR 106 107See swig/README for more detailed build instructions. 108 109Java bindings: 110 111To build the swig-generated JNI wrapper code at least JDK-1.5 (or equivalent) 112is necessary for enum support. The output is intended to be a shared object / 113DLL that can be loaded via System.loadLibrary("webp_jni"). 114 115Python bindings: 116 117To build the swig-generated Python extension code at least Python 2.6 is 118required. Python < 2.6 may build with some minor changes to libwebp.i or the 119generated code, but is untested. 120 121Encoding tool: 122============== 123 124The examples/ directory contains tools for encoding (cwebp) and 125decoding (dwebp) images. 126 127The easiest use should look like: 128 cwebp input.png -q 80 -o output.webp 129which will convert the input file to a WebP file using a quality factor of 80 130on a 0->100 scale (0 being the lowest quality, 100 being the best. Default 131value is 75). 132You might want to try the -lossless flag too, which will compress the source 133(in RGBA format) without any loss. The -q quality parameter will in this case 134control the amount of processing time spent trying to make the output file as 135small as possible. 136 137A longer list of options is available using the -longhelp command line flag: 138 139> cwebp -longhelp 140Usage: 141 cwebp [-preset <...>] [options] in_file [-o out_file] 142 143If input size (-s) for an image is not specified, it is assumed to be a PNG, 144JPEG or TIFF file. 145options: 146 -h / -help ............ short help 147 -H / -longhelp ........ long help 148 -q <float> ............. quality factor (0:small..100:big) 149 -alpha_q <int> ......... Transparency-compression quality (0..100). 150 -preset <string> ....... Preset setting, one of: 151 default, photo, picture, 152 drawing, icon, text 153 -preset must come first, as it overwrites other parameters. 154 -m <int> ............... compression method (0=fast, 6=slowest) 155 -segments <int> ........ number of segments to use (1..4) 156 -size <int> ............ Target size (in bytes) 157 -psnr <float> .......... Target PSNR (in dB. typically: 42) 158 159 -s <int> <int> ......... Input size (width x height) for YUV 160 -sns <int> ............. Spatial Noise Shaping (0:off, 100:max) 161 -f <int> ............... filter strength (0=off..100) 162 -sharpness <int> ....... filter sharpness (0:most .. 7:least sharp) 163 -strong ................ use strong filter instead of simple (default). 164 -nostrong .............. use simple filter instead of strong. 165 -partition_limit <int> . limit quality to fit the 512k limit on 166 the first partition (0=no degradation ... 100=full) 167 -pass <int> ............ analysis pass number (1..10) 168 -crop <x> <y> <w> <h> .. crop picture with the given rectangle 169 -resize <w> <h> ........ resize picture (after any cropping) 170 -mt .................... use multi-threading if available 171 -low_memory ............ reduce memory usage (slower encoding) 172 -map <int> ............. print map of extra info. 173 -print_psnr ............ prints averaged PSNR distortion. 174 -print_ssim ............ prints averaged SSIM distortion. 175 -print_lsim ............ prints local-similarity distortion. 176 -d <file.pgm> .......... dump the compressed output (PGM file). 177 -alpha_method <int> .... Transparency-compression method (0..1) 178 -alpha_filter <string> . predictive filtering for alpha plane. 179 One of: none, fast (default) or best. 180 -alpha_cleanup ......... Clean RGB values in transparent area. 181 -noalpha ............... discard any transparency information. 182 -lossless .............. Encode image losslessly. 183 -hint <string> ......... Specify image characteristics hint. 184 One of: photo, picture or graph 185 186 -metadata <string> ..... comma separated list of metadata to 187 copy from the input to the output if present. 188 Valid values: all, none (default), exif, icc, xmp 189 190 -short ................. condense printed message 191 -quiet ................. don't print anything. 192 -version ............... print version number and exit. 193 -noasm ................. disable all assembly optimizations. 194 -v ..................... verbose, e.g. print encoding/decoding times 195 -progress .............. report encoding progress 196 197Experimental Options: 198 -jpeg_like ............. Roughly match expected JPEG size. 199 -af .................... auto-adjust filter strength. 200 -pre <int> ............. pre-processing filter 201 202 203The main options you might want to try in order to further tune the 204visual quality are: 205 -preset 206 -sns 207 -f 208 -m 209 210Namely: 211 * 'preset' will set up a default encoding configuration targeting a 212 particular type of input. It should appear first in the list of options, 213 so that subsequent options can take effect on top of this preset. 214 Default value is 'default'. 215 * 'sns' will progressively turn on (when going from 0 to 100) some additional 216 visual optimizations (like: segmentation map re-enforcement). This option 217 will balance the bit allocation differently. It tries to take bits from the 218 "easy" parts of the picture and use them in the "difficult" ones instead. 219 Usually, raising the sns value (at fixed -q value) leads to larger files, 220 but with better quality. 221 Typical value is around '75'. 222 * 'f' option directly links to the filtering strength used by the codec's 223 in-loop processing. The higher the value, the smoother the 224 highly-compressed area will look. This is particularly useful when aiming 225 at very small files. Typical values are around 20-30. Note that using the 226 option -strong/-nostrong will change the type of filtering. Use "-f 0" to 227 turn filtering off. 228 * 'm' controls the trade-off between encoding speed and quality. Default is 4. 229 You can try -m 5 or -m 6 to explore more (time-consuming) encoding 230 possibilities. A lower value will result in faster encoding at the expense 231 of quality. 232 233Decoding tool: 234============== 235 236There is a decoding sample in examples/dwebp.c which will take 237a .webp file and decode it to a PNG image file (amongst other formats). 238This is simply to demonstrate the use of the API. You can verify the 239file test.webp decodes to exactly the same as test_ref.ppm by using: 240 241 cd examples 242 ./dwebp test.webp -ppm -o test.ppm 243 diff test.ppm test_ref.ppm 244 245The full list of options is available using -h: 246 247> dwebp -h 248Usage: dwebp in_file [options] [-o out_file] 249 250Decodes the WebP image file to PNG format [Default] 251Use following options to convert into alternate image formats: 252 -pam ......... save the raw RGBA samples as a color PAM 253 -ppm ......... save the raw RGB samples as a color PPM 254 -pgm ......... save the raw YUV samples as a grayscale PGM 255 file with IMC4 layout. 256 -yuv ......... save the raw YUV samples in flat layout. 257 258 Other options are: 259 -version .... print version number and exit. 260 -nofancy ..... don't use the fancy YUV420 upscaler. 261 -nofilter .... disable in-loop filtering. 262 -mt .......... use multi-threading 263 -crop <x> <y> <w> <h> ... crop output with the given rectangle 264 -scale <w> <h> .......... scale the output (*after* any cropping) 265 -alpha ....... only save the alpha plane. 266 -h ....... this help message. 267 -v ....... verbose (e.g. print encoding/decoding times) 268 -noasm ....... disable all assembly optimizations. 269 270Visualization tool: 271=================== 272 273There's a little self-serve visualization tool called 'vwebp' under the 274examples/ directory. It uses OpenGL to open a simple drawing window and show 275a decoded WebP file. It's not yet integrated in the automake build system, but 276you can try to manually compile it using the recommendations below. 277 278Usage: vwebp in_file [options] 279 280Decodes the WebP image file and visualize it using OpenGL 281Options are: 282 -version .... print version number and exit. 283 -noicc ....... don't use the icc profile if present. 284 -nofancy ..... don't use the fancy YUV420 upscaler. 285 -nofilter .... disable in-loop filtering. 286 -mt .......... use multi-threading. 287 -info ........ print info. 288 -h ....... this help message. 289 290Keyboard shortcuts: 291 'c' ................ toggle use of color profile. 292 'i' ................ overlay file information. 293 'q' / 'Q' / ESC .... quit. 294 295Building: 296--------- 297 298Prerequisites: 2991) OpenGL & OpenGL Utility Toolkit (GLUT) 300 Linux: 301 $ sudo apt-get install freeglut3-dev mesa-common-dev 302 Mac + XCode: 303 - These libraries should be available in the OpenGL / GLUT frameworks. 304 Windows: 305 http://freeglut.sourceforge.net/index.php#download 306 3072) (Optional) qcms (Quick Color Management System) 308 i. Download qcms from Mozilla / Chromium: 309 http://hg.mozilla.org/mozilla-central/file/0e7639e3bdfb/gfx/qcms 310 http://src.chromium.org/viewvc/chrome/trunk/src/third_party/qcms 311 ii. Build and archive the source files as libqcms.a / qcms.lib 312 iii. Update makefile.unix / Makefile.vc 313 a) Define WEBP_HAVE_QCMS 314 b) Update include / library paths to reference the qcms directory. 315 316Build using makefile.unix / Makefile.vc: 317$ make -f makefile.unix examples/vwebp 318> nmake /f Makefile.vc CFG=release-static \ 319 ../obj/x64/release-static/bin/vwebp.exe 320 321Encoding API: 322============= 323 324The main encoding functions are available in the header src/webp/encode.h 325The ready-to-use ones are: 326size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, 327 float quality_factor, uint8_t** output); 328size_t WebPEncodeBGR(const uint8_t* bgr, int width, int height, int stride, 329 float quality_factor, uint8_t** output); 330size_t WebPEncodeRGBA(const uint8_t* rgba, int width, int height, int stride, 331 float quality_factor, uint8_t** output); 332size_t WebPEncodeBGRA(const uint8_t* bgra, int width, int height, int stride, 333 float quality_factor, uint8_t** output); 334 335They will convert raw RGB samples to a WebP data. The only control supplied 336is the quality factor. 337 338There are some variants for using the lossless format: 339 340size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, 341 int stride, uint8_t** output); 342size_t WebPEncodeLosslessBGR(const uint8_t* bgr, int width, int height, 343 int stride, uint8_t** output); 344size_t WebPEncodeLosslessRGBA(const uint8_t* rgba, int width, int height, 345 int stride, uint8_t** output); 346size_t WebPEncodeLosslessBGRA(const uint8_t* bgra, int width, int height, 347 int stride, uint8_t** output); 348 349Of course in this case, no quality factor is needed since the compression 350occurs without loss of the input values, at the expense of larger output sizes. 351 352Advanced encoding API: 353---------------------- 354 355A more advanced API is based on the WebPConfig and WebPPicture structures. 356 357WebPConfig contains the encoding settings and is not tied to a particular 358picture. 359WebPPicture contains input data, on which some WebPConfig will be used for 360compression. 361The encoding flow looks like: 362 363-------------------------------------- BEGIN PSEUDO EXAMPLE 364 365#include <webp/encode.h> 366 367 // Setup a config, starting form a preset and tuning some additional 368 // parameters 369 WebPConfig config; 370 if (!WebPConfigPreset(&config, WEBP_PRESET_PHOTO, quality_factor)) 371 return 0; // version error 372 } 373 // ... additional tuning 374 config.sns_strength = 90; 375 config.filter_sharpness = 6; 376 config_error = WebPValidateConfig(&config); // not mandatory, but useful 377 378 // Setup the input data 379 WebPPicture pic; 380 if (!WebPPictureInit(&pic)) { 381 return 0; // version error 382 } 383 pic.width = width; 384 pic.height = height; 385 // allocated picture of dimension width x height 386 if (!WebPPictureAllocate(&pic)) { 387 return 0; // memory error 388 } 389 // at this point, 'pic' has been initialized as a container, 390 // and can receive the Y/U/V samples. 391 // Alternatively, one could use ready-made import functions like 392 // WebPPictureImportRGB(), which will take care of memory allocation. 393 // In any case, past this point, one will have to call 394 // WebPPictureFree(&pic) to reclaim memory. 395 396 // Set up a byte-output write method. WebPMemoryWriter, for instance. 397 WebPMemoryWriter wrt; 398 pic.writer = MyFileWriter; 399 pic.custom_ptr = my_opaque_structure_to_make_MyFileWriter_work; 400 // initialize 'wrt' here... 401 402 // Compress! 403 int ok = WebPEncode(&config, &pic); // ok = 0 => error occurred! 404 WebPPictureFree(&pic); // must be called independently of the 'ok' result. 405 406 // output data should have been handled by the writer at that point. 407 408-------------------------------------- END PSEUDO EXAMPLE 409 410Decoding API: 411============= 412 413This is mainly just one function to call: 414 415#include "webp/decode.h" 416uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, 417 int* width, int* height); 418 419Please have a look at the file src/webp/decode.h for the details. 420There are variants for decoding in BGR/RGBA/ARGB/BGRA order, along with 421decoding to raw Y'CbCr samples. One can also decode the image directly into a 422pre-allocated buffer. 423 424To detect a WebP file and gather the picture's dimensions, the function: 425 int WebPGetInfo(const uint8_t* data, size_t data_size, 426 int* width, int* height); 427is supplied. No decoding is involved when using it. 428 429Incremental decoding API: 430========================= 431 432In the case when data is being progressively transmitted, pictures can still 433be incrementally decoded using a slightly more complicated API. Decoder state 434is stored into an instance of the WebPIDecoder object. This object can be 435created with the purpose of decoding either RGB or Y'CbCr samples. 436For instance: 437 438 WebPDecBuffer buffer; 439 WebPInitDecBuffer(&buffer); 440 buffer.colorspace = MODE_BGR; 441 ... 442 WebPIDecoder* idec = WebPINewDecoder(&buffer); 443 444As data is made progressively available, this incremental-decoder object 445can be used to decode the picture further. There are two (mutually exclusive) 446ways to pass freshly arrived data: 447 448either by appending the fresh bytes: 449 450 WebPIAppend(idec, fresh_data, size_of_fresh_data); 451 452or by just mentioning the new size of the transmitted data: 453 454 WebPIUpdate(idec, buffer, size_of_transmitted_buffer); 455 456Note that 'buffer' can be modified between each call to WebPIUpdate, in 457particular when the buffer is resized to accommodate larger data. 458 459These functions will return the decoding status: either VP8_STATUS_SUSPENDED if 460decoding is not finished yet or VP8_STATUS_OK when decoding is done. Any other 461status is an error condition. 462 463The 'idec' object must always be released (even upon an error condition) by 464calling: WebPDelete(idec). 465 466To retrieve partially decoded picture samples, one must use the corresponding 467method: WebPIDecGetRGB or WebPIDecGetYUVA. 468It will return the last displayable pixel row. 469 470Lastly, note that decoding can also be performed into a pre-allocated pixel 471buffer. This buffer must be passed when creating a WebPIDecoder, calling 472WebPINewRGB() or WebPINewYUVA(). 473 474Please have a look at the src/webp/decode.h header for further details. 475 476Advanced Decoding API: 477====================== 478 479WebP decoding supports an advanced API which provides on-the-fly cropping and 480rescaling, something of great usefulness on memory-constrained environments like 481mobile phones. Basically, the memory usage will scale with the output's size, 482not the input's, when one only needs a quick preview or a zoomed in portion of 483an otherwise too-large picture. Some CPU can be saved too, incidentally. 484 485-------------------------------------- BEGIN PSEUDO EXAMPLE 486 // A) Init a configuration object 487 WebPDecoderConfig config; 488 CHECK(WebPInitDecoderConfig(&config)); 489 490 // B) optional: retrieve the bitstream's features. 491 CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK); 492 493 // C) Adjust 'config' options, if needed 494 config.options.no_fancy_upsampling = 1; 495 config.options.use_scaling = 1; 496 config.options.scaled_width = scaledWidth(); 497 config.options.scaled_height = scaledHeight(); 498 // etc. 499 500 // D) Specify 'config' output options for specifying output colorspace. 501 // Optionally the external image decode buffer can also be specified. 502 config.output.colorspace = MODE_BGRA; 503 // Optionally, the config.output can be pointed to an external buffer as 504 // well for decoding the image. This externally supplied memory buffer 505 // should be big enough to store the decoded picture. 506 config.output.u.RGBA.rgba = (uint8_t*) memory_buffer; 507 config.output.u.RGBA.stride = scanline_stride; 508 config.output.u.RGBA.size = total_size_of_the_memory_buffer; 509 config.output.is_external_memory = 1; 510 511 // E) Decode the WebP image. There are two variants w.r.t decoding image. 512 // The first one (E.1) decodes the full image and the second one (E.2) is 513 // used to incrementally decode the image using small input buffers. 514 // Any one of these steps can be used to decode the WebP image. 515 516 // E.1) Decode full image. 517 CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK); 518 519 // E.2) Decode image incrementally. 520 WebPIDecoder* const idec = WebPIDecode(NULL, NULL, &config); 521 CHECK(idec != NULL); 522 while (bytes_remaining > 0) { 523 VP8StatusCode status = WebPIAppend(idec, input, bytes_read); 524 if (status == VP8_STATUS_OK || status == VP8_STATUS_SUSPENDED) { 525 bytes_remaining -= bytes_read; 526 } else { 527 break; 528 } 529 } 530 WebPIDelete(idec); 531 532 // F) Decoded image is now in config.output (and config.output.u.RGBA). 533 // It can be saved, displayed or otherwise processed. 534 535 // G) Reclaim memory allocated in config's object. It's safe to call 536 // this function even if the memory is external and wasn't allocated 537 // by WebPDecode(). 538 WebPFreeDecBuffer(&config.output); 539 540-------------------------------------- END PSEUDO EXAMPLE 541 542Bugs: 543===== 544 545Please report all bugs to our issue tracker: 546 http://code.google.com/p/webp/issues 547Patches welcome! See this page to get started: 548 http://www.webmproject.org/code/contribute/submitting-patches/ 549 550Discuss: 551======== 552 553Email: webp-discuss@webmproject.org 554Web: http://groups.google.com/a/webmproject.org/group/webp-discuss 555