
Android 10 (r33) पर WebP कोडेक लाइब्रेरी को लक्षित करने वाले CVE-2023-1999 के लिए प्रूफ-ऑफ-कॉन्सेप्ट शोषण। libwebp लाइब्रेरी में हीप बफर ओवरफ्लो भेद्यता का प्रदर्शन, क्राफ्टेड WebP छवि के माध्यम से रिमोट कोड निष्पादन को सक्षम करना।
/ \\/ \/ _ \/ _ )/ _ \
\ / __/ _ \ __/
\__\__/\____/\_____/__/ ____ ___
/ _/ / \ \ / _ \/ _/
/ \_/ / / \ \ __/ \__
\____/____/\_____/_____/____/v1.0.2
WebP कोडेक: WebP प्रारूप में छवियों को एन्कोड और डिकोड करने के लिए लाइब्रेरी। इस पैकेज में वह लाइब्रेरी शामिल है जिसका उपयोग अन्य प्रोग्रामों में WebP समर्थन जोड़ने के लिए किया जा सकता है, साथ ही कमांड लाइन टूल 'cwebp' और 'dwebp' भी शामिल हैं।
देखें http://developers.google.com/speed/webp
नवीनतम सोर्स ट्री यहाँ उपलब्ध है: https://chromium.googlesource.com/webm/libwebp
यह WebM प्रोजेक्ट के समान लाइसेंस के अंतर्गत जारी किया गया है। विवरण के लिए http://www.webmproject.org/license/software/ या "COPYING" फ़ाइल देखें। अतिरिक्त बौद्धिक संपदा अधिकार अनुदान PATENTS फ़ाइल में पाया जा सकता है।
निम्न चलाकर:
nmake /f Makefile.vc CFG=release-static RTLIBCFG=static OBJDIR=output
निर्देशिका output\release-static(x64|x86)\bin में cwebp.exe और dwebp.exe टूल होंगे। निर्देशिका output\release-static(x64|x86)\lib में libwebp स्टैटिक लाइब्रेरी होगी। लक्ष्य आर्किटेक्चर (x86/x64) का पता Makefile.vc सिस्टम पथ में उपलब्ध Visual Studio कंपाइलर (cl.exe) से लगाता है।
GNU टूल्स (gcc और make) इंस्टॉल वाले प्लेटफ़ॉर्म पर, निम्न चलाने पर
make -f makefile.unix
इससे बाइनरी examples/cwebp और examples/dwebp, साथ ही स्टैटिक लाइब्रेरी src/libwebp.a बनेगी। कोई सिस्टम-व्यापी इंस्टॉलेशन प्रदान नहीं किया गया है, क्योंकि यह autoconf टूल पर आधारित पूर्ण इंस्टॉलेशन प्रणाली का एक सरल विकल्प है (नीचे देखें)। अतिरिक्त विवरण और अनुकूलन के लिए कृपया makefile.unix देखें।
पूर्वापेक्षाएँ: एक कंपाइलर (जैसे, gcc), make, autoconf, automake, libtool। Debian-जैसी प्रणाली पर निम्नलिखित कमांड न्यूनतम बिल्ड के लिए आवश्यक सब कुछ इंस्टॉल कर देगी: $ sudo apt-get install gcc make autoconf automake libtool
git स्रोतों से बिल्ड करते समय, configure स्क्रिप्ट उत्पन्न करने के लिए आपको autogen.sh चलाना होगा।
./configure make make install
आपके पास निम्नलिखित फ़ाइलें हों, इसके लिए बस इतना ही चाहिए:
/usr/local/include/webp/decode.h /usr/local/include/webp/encode.h /usr/local/include/webp/types.h /usr/local/lib/libwebp.* /usr/local/bin/cwebp /usr/local/bin/dwebp
इंस्टॉल हो जाएँ।
नोट: '--enable-libwebpdecoder' फ़्लैग का उपयोग करके केवल-डिकोड लाइब्रेरी, libwebpdecoder, उपलब्ध है। एन्कोड लाइब्रेरी अलग से बनती है और संबंधित Makefile.am configure फ़ाइलों में छोटे बदलाव के साथ स्वतंत्र रूप से इंस्टॉल की जा सकती है (वहाँ टिप्पणियाँ देखें)। अधिक विकल्पों के लिए './configure --help' देखें।
MIPS Linux टूलचेन की स्थिर उपलब्ध रिलीज़ यहाँ मिल सकती हैं: https://community.imgtec.com/developers/mips/tools/codescape-mips-sdk/available-releases/
export PATH=$PATH:/path/to/toolchain/bin
HOST=mips-mti-linux-gnu
MIPS_CFLAGS="-O3 -mips32r5 -mabi=32 -mtune=p5600 -mmsa -mfp64
-msched-weight -mload-store-pairs -fPIE"
MIPS_LDFLAGS="-mips32r5 -mabi=32 -mmsa -mfp64 -pie"
HOST=mips-img-linux-gnu
MIPS_CFLAGS="-O3 -mips64r6 -mabi=64 -mtune=i6400 -mmsa -mfp64
-msched-weight -mload-store-pairs -fPIE"
MIPS_LDFLAGS="-mips64r6 -mabi=64 -mmsa -mfp64 -pie"
./configure --host=${HOST} --build=config.guess
CC="${HOST}-gcc -EL"
CFLAGS="$MIPS_CFLAGS"
LDFLAGS="$MIPS_LDFLAGS"
make
make install
CMake के साथ, आप libwebp, cwebp, dwebp, gif2web, img2webp, webpinfo और JS बाइंडिंग्स संकलित कर सकते हैं।
पूर्वापेक्षाएँ: एक कंपाइलर (जैसे, autotools के साथ gcc) और CMake। Debian-जैसी प्रणाली पर निम्नलिखित कमांड न्यूनतम बिल्ड के लिए आवश्यक सब कुछ इंस्टॉल कर देगी: $ sudo apt-get install build-essential cmake
git स्रोतों से बिल्ड करते समय, makefiles उत्पन्न करने के लिए आपको cmake चलाना होगा।
mkdir build && cd build && cmake ../ make make install
यदि आप इनमें से किसी भी एक्ज़ीक्यूटेबल को भी चाहते हैं, तो आपको उन्हें CMake के माध्यम से सक्षम करना होगा, उदाहरण के लिए:
cmake -DWEBP_BUILD_CWEBP=ON -DWEBP_BUILD_DWEBP=ON ../
या अपने पसंदीदा इंटरफ़ेस (जैसे ccmake या cmake-qt-gui) के माध्यम से।
विंडोज़ पर यूनिकोड समर्थन के लिए -DWEBP_UNICODE=ON विकल्प का उपयोग करें (chcp 65001 के साथ)।
अंत में, इंस्टॉल होने के बाद, आप अपने CMake प्रोजेक्ट में निम्न करके WebP का उपयोग कर सकते हैं:
find_package(WebP)
जो CMake वेरिएबल WebP_INCLUDE_DIRS और WebP_LIBRARIES को परिभाषित करेगा।
Gradle के लिए समर्थन न्यूनतम है: यह केवल libwebp, cwebp, dwebp और webpmux_example संकलित करने में मदद करता है।
पूर्वापेक्षाएँ: एक कंपाइलर (जैसे, autotools के साथ gcc) और gradle। Debian-जैसी प्रणाली पर निम्नलिखित कमांड न्यूनतम बिल्ड के लिए आवश्यक सब कुछ इंस्टॉल कर देगी: $ sudo apt-get install build-essential gradle
git स्रोतों से बिल्ड करते समय, आपको Gradle रैपर को उपयुक्त टार्गेट के साथ चलाना होगा, उदाहरण के लिए:
./gradlew buildAllExecutables
swig/libwebp.swig से भाषा बाइंडिंग्स उत्पन्न करने के लिए कम से कम swig-1.3 (http://www.swig.org) आवश्यक है।
वर्तमान में निम्नलिखित फ़ंक्शन मैप किए गए हैं: डिकोड: WebPGetDecoderVersion WebPGetInfo WebPDecodeRGBA WebPDecodeARGB WebPDecodeBGRA WebPDecodeBGR WebPDecodeRGB
एन्कोड: WebPGetEncoderVersion WebPEncodeRGBA WebPEncodeBGRA WebPEncodeRGB WebPEncodeBGR WebPEncodeLosslessRGBA WebPEncodeLosslessBGRA WebPEncodeLosslessRGB WebPEncodeLosslessBGR
अधिक विस्तृत बिल्ड निर्देशों के लिए swig/README देखें।
swig-जनित JNI रैपर कोड बनाने के लिए enum समर्थन हेतु कम से कम JDK-1.5 (या समकक्ष) आवश्यक है। आउटपुट एक shared object / DLL होना चाहिए जिसे System.loadLibrary("webp_jni") के माध्यम से लोड किया जा सके।
swig-जनित Python एक्सटेंशन कोड बनाने के लिए कम से कम Python 2.6 आवश्यक है। Python < 2.6 libwebp.swig या जनित कोड में कुछ छोटे बदलावों के साथ बिल्ड हो सकता है, लेकिन यह परीक्षण नहीं किया गया है।
examples/ निर्देशिका में छवियों को एन्कोड (cwebp) और डिकोड (dwebp) करने के लिए टूल हैं।
सबसे आसान उपयोग इस प्रकार होगा: cwebp input.png -q 80 -o output.webp
यह इनपुट फ़ाइल को 0->100 पैमाने पर गुणवत्ता कारक 80 का उपयोग करके WebP फ़ाइल में परिवर्तित करेगा (0 सबसे कम गुणवत्ता, 100 सबसे अच्छी। डिफ़ॉल्ट मान 75 है)। आप -lossless फ़्लैग भी आज़माना चाह सकते हैं, जो स्रोत (RGBA प्रारूप में) को बिना किसी हानि के संपीड़ित करेगा। इस स्थिति में -q गुणवत्ता पैरामीटर आउटपुट फ़ाइल को यथासंभव छोटा बनाने में खर्च किए गए प्रोसेसिंग समय की मात्रा को नियंत्रित करेगा।
-longhelp कमांड लाइन फ़्लैग का उपयोग करके विकल्पों की एक लंबी सूची उपलब्ध है:
cwebp -longhelp Usage: cwebp [-preset <...>] [options] in_file [-o out_file]
If input size (-s) for an image is not specified, it is assumed to be a PNG, JPEG, TIFF or WebP file.
Options: -h / -help ............. short help -H / -longhelp ......... long help -q ............. quality factor (0:small..100:big), default=75 -alpha_q ......... transparency-compression quality (0..100), default=100 -preset ....... preset setting, one of: default, photo, picture, drawing, icon, text -preset must come first, as it overwrites other parameters -z ............... activates lossless preset with given level in [0:fast, ..., 9:slowest]
-m ............... compression method (0=fast, 6=slowest), default=4 -segments ........ number of segments to use (1..4), default=4 -size ............ target size (in bytes) -psnr .......... target PSNR (in dB. typically: 42)
-s ......... input size (width x height) for YUV -sns ............. spatial noise shaping (0:off, 100:max), default=50 -f ............... filter strength (0=off..100), default=60 -sharpness ....... filter sharpness (0:most .. 7:least sharp), default=0 -strong ................ use strong filter instead of simple (default) -nostrong .............. use simple filter instead of strong -sharp_yuv ............. use sharper (and slower) RGB->YUV conversion -partition_limit . limit quality to fit the 512k limit on the first partition (0=no degradation ... 100=full) -pass ............ analysis pass number (1..10) -crop .. crop picture with the given rectangle -resize ........ resize picture (after any cropping) -mt .................... use multi-threading if available -low_memory ............ reduce memory usage (slower encoding) -map ............. print map of extra info -print_psnr ............ prints averaged PSNR distortion -print_ssim ............ prints averaged SSIM distortion -print_lsim ............ prints local-similarity distortion -d <file.pgm> .......... dump the compressed output (PGM file) -alpha_method .... transparency-compression method (0..1), default=1 -alpha_filter . predictive filtering for alpha plane, one of: none, fast (default) or best -exact ................. preserve RGB values in transparent area, default=off -blend_alpha ..... blend colors against background color expressed as RGB values written in hexadecimal, e.g. 0xc0e0d0 for red=0xc0 green=0xe0 and blue=0xd0 -noalpha ............... discard any transparency information -lossless .............. encode image losslessly, default=off -near_lossless ... use near-lossless image preprocessing (0..100=off), default=100 -hint ......... specify image characteristics hint, one of: photo, picture or graph
-metadata ..... comma separated list of metadata to copy from the input to the output if present. Valid values: all, none (default), exif, icc, xmp
-short ................. condense printed message -quiet ................. don't print anything -version ............... print version number and exit -noasm ................. disable all assembly optimizations -v ..................... verbose, e.g. print encoding/decoding times -progress .............. report encoding progress
Experimental Options: -jpeg_like ............. roughly match expected JPEG size -af .................... auto-adjust filter strength -pre ............. pre-processing filter
दृश्य गुणवत्ता को और बेहतर बनाने के लिए आप ये मुख्य विकल्प आज़मा सकते हैं: -preset -sns -f -m
अर्थात:
examples/dwebp.c में एक डिकोडिंग नमूना है जो एक .webp फ़ाइल लेकर उसे (अन्य प्रारूपों के अलावा) PNG छवि फ़ाइल में डिकोड करेगा। यह केवल API के उपयोग को प्रदर्शित करने के लिए है। आप यह सत्यापित कर सकते हैं कि test.webp निम्न का उपयोग करके test_ref.ppm के बिल्कुल समान डिकोड होती है:
cd examples ./dwebp test.webp -ppm -o test.ppm diff test.ppm test_ref.ppm
विकल्पों की पूरी सूची -h का उपयोग करके उपलब्ध है:
dwebp -h Usage: dwebp in_file [options] [-o out_file]
Decodes the WebP image file to PNG format [Default] Use following options to convert into alternate image formats: -pam ......... save the raw RGBA samples as a color PAM -ppm ......... save the raw RGB samples as a color PPM -bmp ......... save as uncompressed BMP format -tiff ........ save as uncompressed TIFF format -pgm ......... save the raw YUV samples as a grayscale PGM file with IMC4 layout -yuv ......... save the raw YUV samples in flat layout
Other options are: -version ..... print version number and exit -nofancy ..... don't use the fancy YUV420 upscaler -nofilter .... disable in-loop filtering -nodither .... disable dithering -dither .. dithering strength (in 0..100) -alpha_dither use alpha-plane dithering if needed -mt .......... use multi-threading -crop ... crop output with the given rectangle -resize ......... scale the output (after any cropping) -flip ........ flip the output vertically -alpha ....... only save the alpha plane -incremental . use incremental decoding (useful for tests) -h ........... this help message -v ........... verbose (e.g. print encoding/decoding times) -quiet ....... quiet mode, don't print anything -noasm ....... disable all assembly optimizations
'webpinfo' का उपयोग WebP फ़ाइलों की chunk स्तरीय संरचना और bitstream हैडर जानकारी प्रिंट करने के लिए किया जा सकता है। यह यह भी जांच सकता है कि फ़ाइलें मान्य WebP प्रारूप की हैं या नहीं।
Usage: webpinfo [options] in_files Note: there could be multiple input files; options must come before input files. Options: -version ........... Print version number and exit. -quiet ............. Do not show chunk parsing information. -diag .............. Show parsing error diagnosis. -summary ........... Show chunk stats summary. -bitstream_info .... Parse bitstream header.
examples/ निर्देशिका के अंतर्गत 'vwebp' नामक एक छोटा स्व-सेवा विज़ुअलाइज़ेशन टूल है। यह एक सरल ड्रॉइंग विंडो खोलने और डिकोड की गई WebP फ़ाइल दिखाने के लिए OpenGL का उपयोग करता है। यह अभी तक automake बिल्ड प्रणाली में एकीकृत नहीं है, लेकिन आप नीचे दी गई अनुशंसाओं का उपयोग करके इसे मैन्युअल रूप से संकलित करने का प्रयास कर सकते हैं।
Usage: vwebp in_file [options]
Decodes the WebP image file and visualize it using OpenGL Options are: -version ..... print version number and exit -noicc ....... don't use the icc profile if present -nofancy ..... don't use the fancy YUV420 upscaler -nofilter .... disable in-loop filtering -dither dithering strength (0..100), default=50 -noalphadither disable alpha plane dithering -usebgcolor .. display background color -mt .......... use multi-threading -info ........ print info -h ........... this help message
कीबोर्ड शॉर्टकट: 'c' ................ रंग प्रोफ़ाइल का उपयोग टॉगल करें 'b' ................ पृष्ठभूमि रंग प्रदर्शन टॉगल करें 'i' ................ फ़ाइल जानकारी ओवरले करें 'd' ................ ब्लेंडिंग और डिस्पोज़ल अक्षम करें (डीबग) 'q' / 'Q' / ESC .... बाहर निकलें
पूर्वापेक्षाएँ:
OpenGL और OpenGL यूटिलिटी टूलकिट (GLUT) Linux: $ sudo apt-get install freeglut3-dev mesa-common-dev Mac + XCode:
(वैकल्पिक) qcms (Quick Color Management System) i. qcms को Mozilla / Chromium से डाउनलोड करें: http://hg.mozilla.org/mozilla-central/file/0e7639e3bdfb/gfx/qcms http://src.chromium.org/viewvc/chrome/trunk/src/third_party/qcms ii. स्रोत फ़ाइलों को libqcms.a / qcms.lib के रूप में बनाएं और संग्रहित करें iii. makefile.unix / Makefile.vc को अपडेट करें a) WEBP_HAVE_QCMS को परिभाषित करें b) qcms निर्देशिका को संदर्भित करने के लिए include / library पथ अपडेट करें।
makefile.unix / Makefile.vc का उपयोग करके बिल्ड करें: $ make -f makefile.unix examples/vwebp
nmake /f Makefile.vc CFG=release-static
../obj/x64/release-static/bin/vwebp.exe
'img2webp' यूटिलिटी इनपुट छवियों के अनुक्रम (PNG, JPEG, ...) को एक एनिमेटेड WebP फ़ाइल में बदल सकती है। यह अवधि, एन्कोडिंग मोड आदि पर सूक्ष्म नियंत्रण प्रदान करती है।
Usage:
img2webp [file-level options] [image files...] [per-frame options...]
File-level options (only used at the start of compression): -min_size ............ minimize size -loop .......... loop count (default: 0, = infinite loop) -kmax .......... maximum number of frame between key-frames (0=only keyframes) -kmin .......... minimum number of frame between key-frames (0=disable key-frames altogether) -mixed ............... use mixed lossy/lossless automatic mode -v ................... verbose mode -h ................... this help -version ............. print version number and exit
Per-frame options (only used for subsequent images input): -d ............. frame duration in ms (default: 100) -lossless ........... use lossless mode (default) -lossy ... ........... use lossy mode -q ........... quality -m ............. method to use
example: img2webp -loop 2 in0.png -lossy in1.jpg -d 80 in2.tiff -o out.webp
नोट: यदि एकल फ़ाइल नाम तर्क के रूप में पारित किया जाता है, तो तर्क इस फ़ाइल से टोकनाइज़ किए जाएंगे। फ़ाइल नाम वर्ण '-' से शुरू नहीं होना चाहिए।
एनिमेटेड GIF फ़ाइलों को examples/ के अंतर्गत उपलब्ध gif2webp यूटिलिटी का उपयोग करके एनीमेशन के साथ WebP फ़ाइलों में परिवर्तित किया जा सकता है। फिर इन फ़ाइलों को vwebp का उपयोग करके देखा जा सकता है।
Usage: gif2webp [options] gif_file -o webp_file Options: -h / -help ............. this help -lossy ................. encode image using lossy compression -mixed ................. for each frame in the image, pick lossy or lossless compression heuristically -q ............. quality factor (0:small..100:big) -m ............... compression method (0=fast, 6=slowest) -min_size .............. minimize output size (default:off) lossless compression by default; can be combined with -q, -m, -lossy or -mixed options -kmin ............ min distance between key frames -kmax ............ max distance between key frames -f ............... filter strength (0=off..100) -metadata ..... comma separated list of metadata to copy from the input to the output if present Valid values: all, none, icc, xmp (default) -loop_compatibility .... use compatibility mode for Chrome version prior to M62 (inclusive) -mt .................... use multi-threading if available
-version ............... print version number and exit -v ..................... verbose -quiet ................. don't print anything
libgif विकास फ़ाइलें इंस्टॉल होने पर, gif2webp को makefile.unix का उपयोग करके बनाया जा सकता है: $ make -f makefile.unix examples/gif2webp
या autoconf का उपयोग करके: $ ./configure --enable-everything $ make
examples/ के अंतर्गत परीक्षण यूटिलिटी anim_diff का उपयोग दो एनिमेटेड छवियों (प्रत्येक GIF या WebP हो सकती है) की तुलना करने के लिए किया जा सकता है।उपयोग: anim_diff [options]
विकल्प: -dump_frames डिकोड किए गए फ़्रेम्स को PAM प्रारूप में डंप करें -min_psnr ... न्यूनतम प्रति-फ़्रेम PSNR -raw_comparison ..... यदि यह फ़्लैग उपयोग नहीं किया गया है, तो RGB को तुलना से पहले प्रीमल्टीप्लाई किया जाता है -max_diff ..... प्रति चैनल अधिकतम अनुमत अंतर बाद के फ़्रेमों में संबंधित पिक्सेल के बीच -h .................. यह सहायता -version ............ संस्करण संख्या प्रिंट करें और बाहर निकलें
libgif विकास फ़ाइलों और एक C++ कंपाइलर स्थापित होने पर, anim_diff को makefile.unix का उपयोग करके बनाया जा सकता है: $ make -f makefile.unix examples/anim_diff
या autoconf का उपयोग करके: $ ./configure --enable-everything $ make
मुख्य एन्कोडिंग फ़ंक्शन हेडर src/webp/encode.h में उपलब्ध हैं। सीधे उपयोग होने वाले फ़ंक्शन निम्नलिखित हैं: size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output); size_t WebPEncodeBGR(const uint8_t* bgr, int width, int height, int stride, float quality_factor, uint8_t** output); size_t WebPEncodeRGBA(const uint8_t* rgba, int width, int height, int stride, float quality_factor, uint8_t** output); size_t WebPEncodeBGRA(const uint8_t* bgra, int width, int height, int stride, float quality_factor, uint8_t** output);
ये कच्चे RGB सैंपल्स को WebP डेटा में परिवर्तित करते हैं। केवल प्रदान किया गया नियंत्रण क्वालिटी फैक्टर है।
लॉसलेस प्रारूप का उपयोग करने के लिए कुछ वेरिएंट हैं:
size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, int stride, uint8_t** output); size_t WebPEncodeLosslessBGR(const uint8_t* bgr, int width, int height, int stride, uint8_t** output); size_t WebPEncodeLosslessRGBA(const uint8_t* rgba, int width, int height, int stride, uint8_t** output); size_t WebPEncodeLosslessBGRA(const uint8_t* bgra, int width, int height, int stride, uint8_t** output);
बेशक इस मामले में, किसी क्वालिटी फैक्टर की आवश्यकता नहीं है क्योंकि कंप्रेशन इनपुट मानों के बिना किसी नुकसान के होता है, जिसके परिणामस्वरूप आउटपुट आकार बड़े होते हैं।
एक अधिक उन्नत API WebPConfig और WebPPicture संरचनाओं पर आधारित है।
WebPConfig एन्कोडिंग सेटिंग्स रखता है और किसी विशेष चित्र से जुड़ा नहीं होता है। WebPPicture इनपुट डेटा रखता है, जिस पर कुछ WebPConfig कंप्रेशन के लिए उपयोग किया जाएगा। एन्कोडिंग प्रवाह इस प्रकार है:
-------------------------------------- BEGIN PSEUDO EXAMPLE
#include <webp/encode.h>
// Setup a config, starting form a preset and tuning some additional // parameters WebPConfig config; if (!WebPConfigPreset(&config, WEBP_PRESET_PHOTO, quality_factor)) return 0; // version error } // ... additional tuning config.sns_strength = 90; config.filter_sharpness = 6; config_error = WebPValidateConfig(&config); // not mandatory, but useful
// Setup the input data WebPPicture pic; if (!WebPPictureInit(&pic)) { return 0; // version error } pic.width = width; pic.height = height; // allocated picture of dimension width x height if (!WebPPictureAllocate(&pic)) { return 0; // memory error } // at this point, 'pic' has been initialized as a container, // and can receive the Y/U/V samples. // Alternatively, one could use ready-made import functions like // WebPPictureImportRGB(), which will take care of memory allocation. // In any case, past this point, one will have to call // WebPPictureFree(&pic) to reclaim memory.
// Set up a byte-output write method. WebPMemoryWriter, for instance. WebPMemoryWriter wrt; WebPMemoryWriterInit(&wrt); // initialize 'wrt'
pic.writer = MyFileWriter; pic.custom_ptr = my_opaque_structure_to_make_MyFileWriter_work;
// Compress! int ok = WebPEncode(&config, &pic); // ok = 0 => error occurred! WebPPictureFree(&pic); // must be called independently of the 'ok' result.
// output data should have been handled by the writer at that point. // -> compressed data is the memory buffer described by wrt.mem / wrt.size
// deallocate the memory used by compressed data WebPMemoryWriterClear(&wrt);
-------------------------------------- END PSEUDO EXAMPLE
यह मुख्य रूप से केवल एक फ़ंक्शन है जिसे कॉल करना होता है:
#include "webp/decode.h" uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, int* width, int* height);
विवरण के लिए कृपया src/webp/decode.h फ़ाइल देखें। BGR/RGBA/ARGB/BGRA क्रम में डिकोड करने के साथ-साथ कच्चे Y'CbCr सैंपल्स में डिकोड करने के भी वेरिएंट हैं। कोई भी छवि को सीधे एक पूर्व-आवंटित बफर में डिकोड कर सकता है।
WebP फ़ाइल का पता लगाने और चित्र के आयाम प्राप्त करने के लिए, निम्न फ़ंक्शन: int WebPGetInfo(const uint8_t* data, size_t data_size, int* width, int* height); प्रदान किया गया है। इसका उपयोग करने पर कोई डिकोडिंग शामिल नहीं होती है।
जब डेटा क्रमिक रूप से प्रसारित किया जा रहा हो, तो चित्रों को अभी भी थोड़े अधिक जटिल API का उपयोग करके वृद्धिशील रूप से डिकोड किया जा सकता है। डिकोडर स्थिति WebPIDecoder ऑब्जेक्ट के एक इंस्टेंस में संग्रहीत की जाती है। यह ऑब्जेक्ट RGB या Y'CbCr सैंपल्स को डिकोड करने के उद्देश्य से बनाया जा सकता है। उदाहरण के लिए:
WebPDecBuffer buffer; WebPInitDecBuffer(&buffer); buffer.colorspace = MODE_BGR; ... WebPIDecoder* idec = WebPINewDecoder(&buffer);
जैसे-जैसे डेटा क्रमिक रूप से उपलब्ध होता है, यह वृद्धिशील-डिकोडर ऑब्जेक्ट चित्र को आगे डिकोड करने के लिए उपयोग किया जा सकता है। ताज़ा प्राप्त डेटा पास करने के दो (परस्पर अनन्य) तरीके हैं:
या तो नए बाइट्स जोड़कर:
WebPIAppend(idec, fresh_data, size_of_fresh_data);
या केवल प्रसारित डेटा का नया आकार बताकर:
WebPIUpdate(idec, buffer, size_of_transmitted_buffer);
ध्यान दें कि WebPIUpdate के प्रत्येक कॉल के बीच 'buffer' को संशोधित किया जा सकता है, विशेष रूप से जब बड़े डेटा को समायोजित करने के लिए बफर का आकार बदला जाता है।
ये फ़ंक्शन डिकोडिंग स्थिति लौटाएंगे: या तो VP8_STATUS_SUSPENDED यदि डिकोडिंग अभी समाप्त नहीं हुई है, या VP8_STATUS_OK जब डिकोडिंग पूरी हो जाती है। कोई अन्य स्थिति एक त्रुटि स्थिति है।
'idec' ऑब्जेक्ट को हमेशा जारी किया जाना चाहिए (त्रुटि स्थिति पर भी) इस प्रकार कॉल करके: WebPDelete(idec)।
आंशिक रूप से डिकोड किए गए चित्र सैंपल प्राप्त करने के लिए, किसी को संबंधित विधि का उपयोग करना चाहिए: WebPIDecGetRGB या WebPIDecGetYUVA। यह अंतिम प्रदर्शन योग्य पिक्सेल पंक्ति लौटाएगा।
अंत में, ध्यान दें कि डिकोडिंग एक पूर्व-आवंटित पिक्सेल बफर में भी की जा सकती है। यह बफर WebPIDecoder बनाते समय पास किया जाना चाहिए, WebPINewRGB() या WebPINewYUVA() को कॉल करके।
अधिक विवरण के लिए कृपया src/webp/decode.h हेडर देखें।
WebP डिकोडिंग एक उन्नत API का समर्थन करता है जो तुरंत क्रॉपिंग और रीस्केलिंग प्रदान करता है, जो मोबाइल फोन जैसे सीमित मेमोरी वाले वातावरण के लिए अत्यंत उपयोगी है। मूल रूप से, मेमोरी उपयोग आउटपुट के आकार के अनुसार बढ़ेगा, न कि इनपुट के, जब किसी को केवल एक त्वरित पूर्वावलोकन या अन्यथा बहुत बड़े चित्र का ज़ूम किया हुआ भाग चाहिए होता है। वैसे, कुछ CPU भी बचाया जा सकता है।
-------------------------------------- BEGIN PSEUDO EXAMPLE // A) Init a configuration object WebPDecoderConfig config; CHECK(WebPInitDecoderConfig(&config));
// B) optional: retrieve the bitstream's features.
CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);
// C) Adjust 'config' options, if needed
config.options.no_fancy_upsampling = 1;
config.options.use_scaling = 1;
config.options.scaled_width = scaledWidth();
config.options.scaled_height = scaledHeight();
// etc.
// D) Specify 'config' output options for specifying output colorspace.
// Optionally the external image decode buffer can also be specified.
config.output.colorspace = MODE_BGRA;
// Optionally, the config.output can be pointed to an external buffer as
// well for decoding the image. This externally supplied memory buffer
// should be big enough to store the decoded picture.
config.output.u.RGBA.rgba = (uint8_t*) memory_buffer;
config.output.u.RGBA.stride = scanline_stride;
config.output.u.RGBA.size = total_size_of_the_memory_buffer;
config.output.is_external_memory = 1;
// E) Decode the WebP image. There are two variants w.r.t decoding image.
// The first one (E.1) decodes the full image and the second one (E.2) is
// used to incrementally decode the image using small input buffers.
// Any one of these steps can be used to decode the WebP image.
// E.1) Decode full image.
CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);
// E.2) Decode image incrementally.
WebPIDecoder* const idec = WebPIDecode(NULL, NULL, &config);
CHECK(idec != NULL);
while (bytes_remaining > 0) {
VP8StatusCode status = WebPIAppend(idec, input, bytes_read);
if (status == VP8_STATUS_OK || status == VP8_STATUS_SUSPENDED) {
bytes_remaining -= bytes_read;
} else {
break;
}
}
WebPIDelete(idec);
// F) Decoded image is now in config.output (and config.output.u.RGBA).
// It can be saved, displayed or otherwise processed.
// G) Reclaim memory allocated in config's object. It's safe to call
// this function even if the memory is external and wasn't allocated
// by WebPDecode().
WebPFreeDecBuffer(&config.output);
-------------------------------------- END PSEUDO EXAMPLE
कृपया सभी बग्स की रिपोर्ट इश्यू ट्रैकर पर करें: https://bugs.chromium.org/p/webp पैच का स्वागत है! शुरू करने के लिए यह पेज देखें: http://www.webmproject.org/code/contribute/submitting-patches/