README 28 KB

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