WebP API दस्तावेज़

इस सेक्शन में WebP लाइब्रेरी में शामिल एन्कोडर और डिकोडर के लिए एपीआई के बारे में बताया गया है. एपीआई का यह ब्यौरा, वर्शन 1.4.0 के बारे में है.

हेडर और लाइब्रेरी

libwebp को इंस्टॉल करने पर, आपके प्लैटफ़ॉर्म की सामान्य जगह पर webp/ नाम की डायरेक्ट्री इंस्टॉल हो जाएगी. उदाहरण के लिए, Unix प्लैटफ़ॉर्म पर, नीचे दी गई हेडर फ़ाइलों को /usr/local/include/webp/ में कॉपी किया जाएगा.

decode.h
encode.h
types.h

लाइब्रेरी, लाइब्रेरी की सामान्य डायरेक्ट्री में मौजूद होती हैं. स्टैटिक और डाइनैमिक लाइब्रेरी, Unix प्लैटफ़ॉर्म पर /usr/local/lib/ में हैं.

सिंपल डिकोडिंग एपीआई

डिकोडिंग एपीआई का इस्तेमाल शुरू करने के लिए, आपको यह पक्का करना होगा कि आपने ऊपर बताए गए तरीके से लाइब्रेरी और हेडर फ़ाइलें इंस्टॉल की हों.

अपने C/C++ कोड में, डिकोड करने वाले एपीआई हेडर को इस तरह शामिल करें:

#include "webp/decode.h"
int WebPGetInfo(const uint8_t* data, size_t data_size, int* width, int* height);

यह फ़ंक्शन WebP इमेज के हेडर की पुष्टि करेगा और इमेज की चौड़ाई और ऊंचाई हासिल करेगा. अगर हमें लगता है कि यह पॉइंटर *width और *height के काम का नहीं है, तो NULL को पास किया जा सकता है.

इनपुट विशेषताएं

डेटा
WebP इमेज के डेटा का पॉइंटर
data_size
यह उस मेमोरी ब्लॉक का साइज़ है जिसके बारे में data ने बताया है. इसमें इमेज का डेटा शामिल है.

लौटाए गए प्रॉडक्ट

false
(a) फ़ॉर्मैटिंग से जुड़ी गड़बड़ी(गड़बड़ियों) के मामले में गड़बड़ी कोड मिला.
सही
सफलता के बाद. *width और *height, सिर्फ़ सामान लौटाने पर ही मान्य होते हैं.
चौड़ाई
पूरी संख्या देने वाली वैल्यू. इसकी सीमा 1 से 16383 तक सीमित है.
ऊंचाई
पूरी संख्या देने वाली वैल्यू. सीमा 1 से 16383 तक सीमित है.
struct WebPBitstreamFeatures {
  int width;          // Width in pixels.
  int height;         // Height in pixels.
  int has_alpha;      // True if the bitstream contains an alpha channel.
  int has_animation;  // True if the bitstream is an animation.
  int format;         // 0 = undefined (/mixed), 1 = lossy, 2 = lossless
}

VP8StatusCode WebPGetFeatures(const uint8_t* data,
                              size_t data_size,
                              WebPBitstreamFeatures* features);

यह फ़ंक्शन, बिटस्ट्रीम की सुविधाएं फिर से हासिल करेगा. *features स्ट्रक्चर में, बिटस्ट्रीम से इकट्ठा की गई जानकारी भरी हुई है:

इनपुट विशेषताएं

डेटा
WebP इमेज के डेटा का पॉइंटर
data_size
यह उस मेमोरी ब्लॉक का साइज़ है जिसके बारे में data ने बताया है. इसमें इमेज का डेटा शामिल है.

लौटाए गए प्रॉडक्ट

VP8_STATUS_OK
जब सुविधाओं का ऐक्सेस वापस मिल जाए.
VP8_STATUS_NOT_ENOUGH_DATA
हेडर से सुविधाओं को वापस पाने के लिए ज़्यादा डेटा की ज़रूरत होने पर.

अन्य मामलों में, VP8StatusCode गड़बड़ी की अतिरिक्त वैल्यू.

सुविधाएँ
WebPBitstreamFeatures के स्ट्रक्चर की जानकारी देने वाला पॉइंट.
uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, int* width, int* height);
uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size, int* width, int* height);

ये फ़ंक्शन, data की ओर से बताने वाली WebP इमेज को डिकोड करते हैं.

  • WebPDecodeRGBA, [r0, g0, b0, a0, r1, g1, b1, a1, ...] के क्रम में RGBA इमेज के नमूने दिखाता है.
  • WebPDecodeARGB, [a0, r0, g0, b0, a1, r1, g1, b1, ...] क्रम में ARGB इमेज के सैंपल दिखाता है.
  • WebPDecodeBGRA, BGRA इमेज के सैंपल [b0, g0, r0, a0, b1, g1, r1, a1, ...] के क्रम में दिखाता है.
  • WebPDecodeRGB, [r0, g0, b0, r1, g1, b1, ...] के क्रम में आरजीबी इमेज के नमूने दिखाता है.
  • WebPDecodeBGR, [b0, g0, r0, b1, g1, r1, ...] के हिसाब से BGR इमेज के सैंपल दिखाता है.

इनमें से किसी भी फ़ंक्शन को कॉल करने वाले कोड को, WebPFree() का इस्तेमाल करके इन फ़ंक्शन से मिले डेटा बफ़र (uint8_t*) को मिटाना होगा.

इनपुट विशेषताएं

डेटा
WebP इमेज के डेटा का पॉइंटर
data_size
यह उस मेमोरी ब्लॉक का साइज़ है जिसके बारे में data ने बताया है. इसमें इमेज का डेटा शामिल है
चौड़ाई
पूरी संख्या देने वाली वैल्यू. फ़िलहाल, यह सीमा 1 से 16383 तक सीमित है.
ऊंचाई
पूरी संख्या देने वाली वैल्यू. फ़िलहाल, यह सीमा 1 से 16383 तक सीमित है.

लौटाए गए प्रॉडक्ट

uint8_t*
WebP इमेज के डिकोड किए गए सैंपल को लीनियर आरजीए/एआरजीबी/बीजीआरए/आरजीबी/बीजीआर ऑर्डर में देखने के लिए पॉइंटर.
uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
                            uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
                            uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
                            uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
                           uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
                           uint8_t* output_buffer, int output_buffer_size, int output_stride);

ये फ़ंक्शन ऊपर दिए गए फ़ंक्शन के वैरिएंट हैं. साथ ही, इमेज को सीधे पहले से तय किए गए बफ़र output_buffer में डिकोड कर देते हैं. इस बफ़र में उपलब्ध ज़्यादा से ज़्यादा स्टोरेज को output_buffer_size से दिखाया जाता है. अगर यह स्टोरेज ज़रूरत के मुताबिक नहीं है या कोई गड़बड़ी हुई है, तो NULL दिखाता है. अगर ऐसा नहीं है, तो सुविधा के लिए output_buffer वापस कर दिया जाएगा.

पैरामीटर output_stride, स्कैनलाइन के बीच की दूरी (बाइट में) बताता है. इसलिए, output_buffer_size की वैल्यू कम से कम output_stride * picture - height होनी चाहिए.

इनपुट विशेषताएं

डेटा
WebP इमेज के डेटा का पॉइंटर
data_size
यह उस मेमोरी ब्लॉक का साइज़ है जिसके बारे में data ने बताया है. इसमें इमेज का डेटा शामिल है
output_buffer_size
पूरी संख्या देने वाली वैल्यू. तय किए गए बफ़र का साइज़
output_stride
पूरी संख्या देने वाली वैल्यू. स्कैनलाइन के बीच की दूरी दिखाता है.

लौटाए गए प्रॉडक्ट

output_buffer
डीकोड की गई WebP इमेज पर कर्सर ले जाएं.
uint8_t*
output_buffer, अगर फ़ंक्शन सही से काम करता है, तो NULL नहीं.

बेहतर डिकोडिंग एपीआई

WebP डिकोड करने की सुविधा एक बेहतर एपीआई की मदद करती है. इससे, तुरंत काटने और साइज़ बढ़ाने की सुविधा मिलती है. यह मोबाइल फ़ोन जैसे सीमित मेमोरी वाले एनवायरमेंट में बहुत काम की चीज़ होती है. बुनियादी रूप से, मेमोरी का इस्तेमाल आउटपुट के साइज़ के हिसाब से तय होगा, न कि इनपुट के साइज़ पर. कुछ सीपीयू (CPU) को भी गलती से सेव किया जा सकता है.

WebP डिकोडिंग के दो वैरिएंट होते हैं. जैसे, पूरे इमेज को डिकोड करना और छोटे इनपुट बफ़र पर इंक्रीमेंटल डिकोडिंग. इमेज को डिकोड करने के लिए, उपयोगकर्ता एक बाहरी मेमोरी बफ़र दे सकते हैं. हालांकि, ऐसा करना ज़रूरी नहीं है. नीचे दिए गए कोड सैंपल में, ऐडवांस डिकोडिंग एपीआई इस्तेमाल करने का तरीका बताया जाएगा.

सबसे पहले हमें कॉन्फ़िगरेशन ऑब्जेक्ट को शुरू करना होगा:

#include "webp/decode.h"

WebPDecoderConfig config;
CHECK(WebPInitDecoderConfig(&config));

// One can adjust some additional decoding options:
config.options.no_fancy_upsampling = 1;
config.options.use_scaling = 1;
config.options.scaled_width = scaledWidth();
config.options.scaled_height = scaledHeight();
// etc.

डिकोड करने के विकल्प WebPDecoderConfig स्ट्रक्चर में इकट्ठा किए गए हैं:

struct WebPDecoderOptions {
  int bypass_filtering;             // if true, skip the in-loop filtering
  int no_fancy_upsampling;          // if true, use faster pointwise upsampler
  int use_cropping;                 // if true, cropping is applied first 
  int crop_left, crop_top;          // top-left position for cropping.
                                    // Will be snapped to even values.
  int crop_width, crop_height;      // dimension of the cropping area
  int use_scaling;                  // if true, scaling is applied afterward
  int scaled_width, scaled_height;  // final resolution
  int use_threads;                  // if true, use multi-threaded decoding
  int dithering_strength;           // dithering strength (0=Off, 100=full)
  int flip;                         // if true, flip output vertically
  int alpha_dithering_strength;     // alpha dithering strength in [0..100]
};

इसके अलावा, अगर हमें बिटस्ट्रीम सुविधाओं के बारे में पहले से जानना हो, तो उन्हें config.input पर पढ़ा जा सकता है. उदाहरण के लिए, इससे यह जानने में आसानी होती है कि इमेज में थोड़ी पारदर्शिता है या नहीं. ध्यान दें कि यह बिटस्ट्रीम के हेडर को भी पार्स करेगा. इसलिए, इससे यह जानने में मदद मिलती है कि बिटस्ट्रीम एक मान्य WebP फ़ॉर्मैट की तरह दिखती है या नहीं.

CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);

इसके बाद हमें डीकोडिंग मेमोरी बफ़र सेट अप करना होगा, ताकि शायद हम इसे सीधे तौर पर उपलब्ध कराना चाहें. इसके लिए, हमें डिकोडर पर निर्भर होने की ज़रूरत न हो. हमें सिर्फ़ मेमोरी के लिए पॉइंटर की सुविधा देने की ज़रूरत है. साथ ही, बफ़र के कुल साइज़ और लाइन स्ट्रेड (स्कैनलाइन के बीच बाइट में दूरी) की भी ज़रूरत है.

// Specify the desired output colorspace:
config.output.colorspace = MODE_BGRA;
// Have config.output point to an external buffer:
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;

यह इमेज, डिकोड की जा सकती है. इमेज को डिकोड करने के दो वैरिएंट हो सकते हैं. हम इनका इस्तेमाल करके एक बार में इमेज को डिकोड कर सकते हैं:

CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);

इसके अलावा, इंक्रीमेंटल तरीके का इस्तेमाल करके, हम इमेज को धीरे-धीरे डिकोड कर सकते हैं. ऐसा तब किया जा सकता है, जब नई बाइट डेटा उपलब्ध हो:

WebPIDecoder* idec = WebPINewDecoder(&config.output);
CHECK(idec != NULL);
while (additional_data_is_available) {
  // ... (get additional data in some new_data[] buffer)
  VP8StatusCode status = WebPIAppend(idec, new_data, new_data_size);
  if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
    break;
  }
  // The above call decodes the current available buffer.
  // Part of the image can now be refreshed by calling
  // WebPIDecGetRGB()/WebPIDecGetYUVA() etc.
}
WebPIDelete(idec);  // the object doesn't own the image memory, so it can
                    // now be deleted. config.output memory is preserved.

डिकोड की गई इमेज अब config.OUT (या इस मामले में, config.OUT.u.RGBA) में है, क्योंकि अनुरोध किया गया आउटपुट कलरस्पेस MODE_BGRA था. इमेज को सेव किया जा सकता है, दिखाया जा सकता है या प्रोसेस किया जा सकता है. इसके बाद, हमें सिर्फ़ कॉन्फ़िगरेशन के ऑब्जेक्ट में असाइन की गई मेमोरी पर फिर से दावा करना होगा. इस फ़ंक्शन को कॉल किया जा सकता है. भले ही, मेमोरी बाहरी हो और WebPDecode() से असाइन न की गई हो:

WebPFreeDecBuffer(&config.output);

इस एपीआई का इस्तेमाल करके MODE_YUV और MODE_YUVA का इस्तेमाल करके, इमेज को YUV और YUVA फ़ॉर्मैट में भी डिकोड किया जा सकता है. इस फ़ॉर्मैट को Y'CbCr भी कहा जाता है.

सिंपल एन्कोडिंग एपीआई

सबसे सामान्य लेआउट में आरजीबीए सैंपल के अरे को कोड में बदलने के लिए, कुछ बेहद आसान फ़ंक्शन दिए जाते हैं. 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);

क्वालिटी फ़ैक्टर quality_factor की रेंज 0 से 100 तक होती है. साथ ही, कंप्रेस करने के दौरान होने वाले नुकसान और क्वालिटी को कंट्रोल करता है. वैल्यू 0, हल्की क्वालिटी और कम आउटपुट साइज़ से जुड़ी होती है, जबकि 100 सबसे ज़्यादा क्वालिटी और सबसे बड़े आउटपुट साइज़ होती है. प्रोसेस हो जाने पर, कंप्रेस की गई बाइट को *output पॉइंटर में रखा जाता है और साइज़ बाइट में दिखाया जाता है. ऐसा न होने पर, 0 वैल्यू दिखती है. मेमोरी फिर से पाने के लिए, कॉलर को *output पॉइंटर पर WebPFree() को कॉल करना होगा.

इनपुट ऐरे, बाइट का पैक किया गया कलेक्शन होना चाहिए. यह हर चैनल के लिए, फ़ंक्शन के नाम के हिसाब से एक होगा. stride का मतलब है, एक लाइन से अगली लाइन में जाने के लिए ज़रूरी बाइट की संख्या. उदाहरण के लिए, BGRA लेआउट:

हस्ताक्षर के साथ, लॉसलेस एन्कोडिंग के लिए एक जैसे फ़ंक्शन होते हैं:

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);

ध्यान रखें कि नुकसान पहुंचाने वाले वर्शन की तरह ही, ये फ़ंक्शन लाइब्रेरी की डिफ़ॉल्ट सेटिंग का इस्तेमाल करते हैं. लॉसलेस के लिए इसका मतलब है कि 'एग्ज़ैक्ट' बंद है. कंप्रेशन को बेहतर बनाने के लिए, पारदर्शी हिस्सों में मौजूद आरजीबी वैल्यू में बदलाव किया जाएगा. इससे बचने के लिए, WebPEncode() का इस्तेमाल करें और WebPConfig::exact को 1 पर सेट करें.

बेहतर एन्कोडिंग एपीआई

हुड के तहत, एन्कोडर में कोड में बदलने के कई बेहतर पैरामीटर मौजूद होते हैं. ये कंप्रेशन परफ़ॉर्मेंस और प्रोसेस होने में लगने वाले समय के बीच ट्रेड-ऑफ़ को बेहतर तरीके से संतुलित करने में मददगार हो सकती हैं. ये पैरामीटर WebPConfig स्ट्रक्चर के अंदर इकट्ठा किए जाते हैं. इस स्ट्रक्चर के सबसे ज़्यादा इस्तेमाल किए जाने वाले फ़ील्ड हैं:

struct WebPConfig {
  int lossless;           // Lossless encoding (0=lossy(default), 1=lossless).
  float quality;          // between 0 and 100. For lossy, 0 gives the smallest
                          // size and 100 the largest. For lossless, this
                          // parameter is the amount of effort put into the
                          // compression: 0 is the fastest but gives larger
                          // files compared to the slowest, but best, 100.
  int method;             // quality/speed trade-off (0=fast, 6=slower-better)

  WebPImageHint image_hint;  // Hint for image type (lossless only for now).

  // Parameters related to lossy compression only:
  int target_size;        // if non-zero, set the desired target size in bytes.
                          // Takes precedence over the 'compression' parameter.
  float target_PSNR;      // if non-zero, specifies the minimal distortion to
                          // try to achieve. Takes precedence over target_size.
  int segments;           // maximum number of segments to use, in [1..4]
  int sns_strength;       // Spatial Noise Shaping. 0=off, 100=maximum.
  int filter_strength;    // range: [0 = off .. 100 = strongest]
  int filter_sharpness;   // range: [0 = off .. 7 = least sharp]
  int filter_type;        // filtering type: 0 = simple, 1 = strong (only used
                          // if filter_strength > 0 or autofilter > 0)
  int autofilter;         // Auto adjust filter's strength [0 = off, 1 = on]
  int alpha_compression;  // Algorithm for encoding the alpha plane (0 = none,
                          // 1 = compressed with WebP lossless). Default is 1.
  int alpha_filtering;    // Predictive filtering method for alpha plane.
                          //  0: none, 1: fast, 2: best. Default if 1.
  int alpha_quality;      // Between 0 (smallest size) and 100 (lossless).
                          // Default is 100.
  int pass;               // number of entropy-analysis passes (in [1..10]).

  int show_compressed;    // if true, export the compressed picture back.
                          // In-loop filtering is not applied.
  int preprocessing;      // preprocessing filter (0=none, 1=segment-smooth)
  int partitions;         // log2(number of token partitions) in [0..3]
                          // Default is set to 0 for easier progressive decoding.
  int partition_limit;    // quality degradation allowed to fit the 512k limit on
                          // prediction modes coding (0: no degradation,
                          // 100: maximum possible degradation).
  int use_sharp_yuv;      // if needed, use sharp (and slow) RGB->YUV conversion
};

ध्यान दें कि इनमें से ज़्यादातर पैरामीटर को cwebp कमांड लाइन टूल का इस्तेमाल करके, प्रयोग के लिए ऐक्सेस किया जा सकता है.

इनपुट सैंपल को WebPPicture स्ट्रक्चर में रैप किया जाना चाहिए. यह स्ट्रक्चर, use_argb फ़्लैग की वैल्यू के आधार पर इनपुट सैंपल को आरजीए या YUVA फ़ॉर्मैट में सेव कर सकता है.

स्ट्रक्चर इस तरह व्यवस्थित किया जाता है:

struct WebPPicture {
  int use_argb;              // To select between ARGB and YUVA input.

  // YUV input, recommended for lossy compression.
  // Used if use_argb = 0.
  WebPEncCSP colorspace;     // colorspace: should be YUVA420 or YUV420 for now (=Y'CbCr).
  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
  int y_stride, uv_stride;   // luma/chroma strides.
  uint8_t* a;                // pointer to the alpha plane
  int a_stride;              // stride of the alpha plane

  // Alternate ARGB input, recommended for lossless compression.
  // Used if use_argb = 1.
  uint32_t* argb;            // Pointer to argb (32 bit) plane.
  int argb_stride;           // This is stride in pixels units, not bytes.

  // Byte-emission hook, to store compressed bytes as they are ready.
  WebPWriterFunction writer;  // can be NULL
  void* custom_ptr;           // can be used by the writer.

  // Error code for the latest error encountered during encoding
  WebPEncodingError error_code;
};

इस स्ट्रक्चर में भी एक फ़ंक्शन है, जो कंप्रेस किए गए बाइट का उत्सर्जन करता है. इन-मेमोरी राइटर के साथ एक उदाहरण देखें. दूसरे लेखक सीधे फ़ाइल में डेटा सेव कर सकते हैं (उदाहरण के लिए, examples/cwebp.c देखें).

बेहतर एपीआई का इस्तेमाल करके कोड में बदलने का सामान्य फ़्लो ऐसा दिखता है:

सबसे पहले, हमें एक ऐसा एन्कोडिंग कॉन्फ़िगरेशन सेट अप करना होगा जिसमें कंप्रेशन पैरामीटर शामिल हों. ध्यान दें कि बाद में, कई अलग-अलग इमेज को कंप्रेस करने के लिए, उसी कॉन्फ़िगरेशन का इस्तेमाल किया जा सकता है.

#include "webp/encode.h"

WebPConfig config;
if (!WebPConfigPreset(&config, WEBP_PRESET_PHOTO, quality_factor)) return 0;   // version error

// Add additional tuning:
config.sns_strength = 90;
config.filter_sharpness = 6;
config.alpha_quality = 90;
config_error = WebPValidateConfig(&config);  // will verify parameter ranges (always a good habit)

इसके बाद, इनपुट सैंपल को WebPPicture में रेफ़रंस के तौर पर या कॉपी की मदद से रेफ़र किया जाना चाहिए. यहां एक उदाहरण दिया गया है, जिसमें नमूने रखने के लिए बफ़र ऐलोकेशन को शामिल किया गया है. हालांकि, पहले से तय किए गए सैंपल कलेक्शन में, "व्यू" को आसानी से सेट अप किया जा सकता है. WebPPictureView() फ़ंक्शन देखें.

// Setup the input data, allocating a picture of width x height dimension
WebPPicture pic;
if (!WebPPictureInit(&pic)) return 0;  // version error
pic.width = width;
pic.height = height;
if (!WebPPictureAlloc(&pic)) return 0;   // memory error

// At this point, 'pic' has been initialized as a container, and can receive the YUVA or RGBA samples.
// Alternatively, one could use ready-made import functions like WebPPictureImportRGBA(), which will take
// care of memory allocation. In any case, past this point, one will have to call WebPPictureFree(&pic)
// to reclaim allocated memory.

हर बार नई बाइट उपलब्ध होने पर, कंप्रेस की गई बाइट निकालने के लिए एक हुक कॉल किया जाता है. यहां एक उदाहरण दिया गया है, जिसमें मेमोरी-राइटर के बारे में webp/encode.h में बताया गया है. हर तस्वीर को कंप्रेस करने के लिए इस इनिशलाइज़ेशन की ज़रूरत पड़ सकती है:

// Set up a byte-writing method (write-to-memory, in this case):
WebPMemoryWriter writer;
WebPMemoryWriterInit(&writer);
pic.writer = WebPMemoryWrite;
pic.custom_ptr = &writer;

अब हम इनपुट सैंपल को कंप्रेस करने (और बाद में उनकी मेमोरी खाली करने) के लिए तैयार हैं:

int ok = WebPEncode(&config, &pic);
WebPPictureFree(&pic);   // Always free the memory associated with the input.
if (!ok) {
  printf("Encoding error: %d\n", pic.error_code);
} else {
  printf("Output size: %d\n", writer.size);
}

एपीआई और स्ट्रक्चर के बेहतर इस्तेमाल के लिए, हमारा सुझाव है कि आप webp/encode.h हेडर में उपलब्ध दस्तावेज़ देखें. उदाहरण कोड examples/cwebp.c को पढ़ना, कम इस्तेमाल किए गए पैरामीटर का पता लगाने में मददगार साबित हो सकता है.