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