README 30 KB

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