เอกสารประกอบของ WebP API

ส่วนนี้จะอธิบาย API สำหรับโปรแกรมเปลี่ยนไฟล์และตัวถอดรหัสที่รวมอยู่ในไลบรารี WebP คำอธิบาย API นี้เกี่ยวข้องกับเวอร์ชัน 1.3.2

ส่วนหัวและไลบรารี

เมื่อคุณติดตั้ง libwebp ระบบจะติดตั้งไดเรกทอรีที่ชื่อ webp/ ในตำแหน่งปกติของแพลตฟอร์ม เช่น ระบบจะคัดลอกไฟล์ส่วนหัวต่อไปนี้ไปยัง /usr/local/include/webp/ ในแพลตฟอร์ม Uniix

decode.h
encode.h
types.h

ไลบรารีจะอยู่ในไดเรกทอรีไลบรารีปกติ ไลบรารีแบบคงที่และแบบไดนามิกอยู่ใน /usr/local/lib/ บนแพลตฟอร์ม Unix

API การถอดรหัสอย่างง่าย

หากต้องการเริ่มต้นใช้งาน API การถอดรหัส คุณต้องตรวจสอบว่าได้ติดตั้งไฟล์ไลบรารีและไฟล์ส่วนหัวตามที่อธิบายไว้ด้านบน

ใส่ส่วนหัว API การถอดรหัสในโค้ด 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
แสดงผลรหัสข้อผิดพลาดในกรณีที่ (ก) เกิดข้อผิดพลาดในการจัดรูปแบบ
จริง
บนความสำเร็จ *width และ *height จะใช้ได้เมื่อคืนสินค้าเรียบร้อยแล้วเท่านั้น
ความกว้าง
ค่าจำนวนเต็ม โดยจำกัดช่วงนี้ไว้ตั้งแต่ 1 ถึง 16,383
ส่วนสูง
ค่าจำนวนเต็ม ช่วงนี้จะถูกจำกัดตั้งแต่ 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);

ฟังก์ชันเหล่านี้จะถอดรหัสอิมเมจ WebP ที่ data ชี้ไป

  • WebPDecodeRGBA แสดงผลตัวอย่างรูปภาพ RGBA ในลำดับ [r0, g0, b0, a0, r1, g1, b1, a1, ...]
  • WebPDecodeARGB แสดงผลตัวอย่างรูปภาพ ARGB ในคำสั่งซื้อ [a0, r0, g0, b0, a1, r1, g1, b1, ...] รายการ
  • WebPDecodeBGRA แสดงผลตัวอย่างรูปภาพ BGRA ในลำดับ [b0, g0, r0, a0, b1, g1, r1, a1, ...]
  • WebPDecodeRGB แสดงผลตัวอย่างรูปภาพ RGB ในลำดับ [r0, g0, b0, r1, g1, b1, ...]
  • WebPDecodeBGR แสดงผลตัวอย่างรูปภาพ BGR ในคำสั่งซื้อ [b0, g0, r0, b1, g1, r1, ...] รายการ

โค้ดที่เรียกใช้ฟังก์ชันเหล่านี้ต้องลบบัฟเฟอร์ข้อมูล (uint8_t*) ที่แสดงผลโดยฟังก์ชันเหล่านี้พร้อมกับ WebPFree()

แอตทริบิวต์อินพุต

ข้อมูล
ชี้ไปที่ข้อมูลรูปภาพของ WebP
data_size
นี่คือขนาดของบล็อกหน่วยความจำที่ data ชี้ไปที่มีข้อมูลรูปภาพ
ความกว้าง
ค่าจำนวนเต็ม ปัจจุบันมีการจำกัดช่วงตั้งแต่ 1 ถึง 16,383
ส่วนสูง
ค่าจำนวนเต็ม ขณะนี้จำกัดช่วงตั้งแต่ 1 ถึง 16383

การคืนสินค้า

uint8_t*
ตัวชี้ไปยังตัวอย่างรูปภาพ WebP ที่ถอดรหัสแล้วตามลำดับ RGBA/ARGB/BGRA/RGB/BGR เชิงเส้นตามลำดับ
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 ไม่เช่นนั้น

API การถอดรหัสขั้นสูง

การถอดรหัส WebP รองรับ API ขั้นสูงที่ช่วยให้ครอบตัดและปรับขนาดได้ทันที ซึ่งเป็นความสามารถที่มีประโยชน์อย่างยิ่งในสภาพแวดล้อมที่จำกัดหน่วยความจำ เช่น โทรศัพท์มือถือ โดยปกติแล้ว การใช้งานหน่วยความจำจะปรับขนาดตามขนาดของเอาต์พุต ไม่ใช่เมื่อผู้ใช้ต้องการเพียงแค่ดูตัวอย่างอย่างรวดเร็ว หรือซูมส่วนหนึ่งของรูปภาพที่มีขนาดใหญ่เกินไป อาจบันทึก CPU บางตัวไว้ด้วย โดยไม่ได้ตั้งใจ

การถอดรหัส WebP มี 2 รูปแบบ ได้แก่ การถอดรหัสรูปภาพแบบเต็มและการถอดรหัสที่เพิ่มขึ้นบนบัฟเฟอร์อินพุตขนาดเล็ก ผู้ใช้สามารถเลือกที่จะให้บัฟเฟอร์หน่วยความจำภายนอก สำหรับการถอดรหัสรูปภาพ ตัวอย่างโค้ดต่อไปนี้จะพูดถึงขั้นตอน การใช้ API การถอดรหัสขั้นสูง

ก่อนอื่น เราต้องเริ่มต้นออบเจ็กต์การกำหนดค่า ดังนี้

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

รูปภาพพร้อมสำหรับการถอดรหัสแล้ว การถอดรหัสรูปภาพมี 2 รูปแบบที่เป็นไปได้ เราสามารถถอดรหัสรูปภาพในครั้งเดียว โดยใช้:

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.output (หรือจะมากกว่าใน config.output.u.RGBA ในกรณีนี้ เนื่องจากพื้นที่สีเอาต์พุตที่ขอคือ MODE_BGRA) บันทึก แสดง หรือประมวลผลรูปภาพได้ หลังจากนั้น เราจะต้องเรียกคืนหน่วยความจำที่จัดสรรในออบเจ็กต์ของการกำหนดค่าเท่านั้น คุณสามารถเรียกใช้ฟังก์ชันนี้ได้อย่างปลอดภัยแม้ว่าหน่วยความจำจะเป็นข้อมูลภายนอกและไม่ได้จัดสรรโดย WebPDecode():

WebPFreeDecBuffer(&config.output);

เมื่อใช้ API นี้ คุณจะถอดรหัสรูปภาพเป็นรูปแบบ YUV และ YUVA ได้โดยใช้ MODE_YUV และ MODE_YUVA ตามลำดับ รูปแบบนี้เรียกอีกอย่างว่า Y'CbCr

API การเข้ารหัสอย่างง่าย

มีฟังก์ชันง่ายๆ บางอย่างให้ใช้สำหรับอาร์เรย์การเข้ารหัสของตัวอย่าง RGBA ในเลย์เอาต์ที่พบบ่อยที่สุด โดยจะมีการประกาศในส่วนหัว 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 ในกรณีที่ล้มเหลว) ผู้โทรจะต้องโทรหา WebPFree() ที่ตัวชี้ *output เพื่อเรียกคืนหน่วยความจำ

อาร์เรย์อินพุตควรเป็นอาร์เรย์แพ็กไบต์ (1 รายการต่อแชแนล ตามที่คาดหวังตามชื่อฟังก์ชัน) 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);

โปรดสังเกตฟังก์ชันเหล่านี้ เช่น เวอร์ชันแบบสูญเสียข้อมูล ให้ใช้การตั้งค่าเริ่มต้นของไลบรารี หากเป็นแบบไม่สูญเสียข้อมูล จะหมายถึงปิดใช้ "แบบตรงทั้งหมด" ระบบจะแก้ไขค่า RGB ในพื้นที่โปร่งใสเพื่อปรับปรุงการบีบอัด หากไม่ต้องการให้เป็นเช่นนั้น ให้ใช้ WebPEncode() และตั้งค่า WebPConfig::exact เป็น 1

API การเข้ารหัสขั้นสูง

ภายในโปรแกรมเปลี่ยนไฟล์ โปรแกรมเปลี่ยนไฟล์มาพร้อมกับพารามิเตอร์การเข้ารหัสขั้นสูงจำนวนมาก การทดสอบเหล่านี้อาจเป็นประโยชน์ในการสร้างความสมดุลระหว่างประสิทธิภาพในการบีบอัดกับเวลาในการประมวลผลข้อมูลให้ดียิ่งขึ้น พารามิเตอร์เหล่านี้รวบรวมไว้ในโครงสร้าง 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 โครงสร้างนี้จัดเก็บตัวอย่างอินพุตได้ในรูปแบบ RGBA หรือ YUVA โดยขึ้นอยู่กับค่าของแฟล็ก use_argb

โดยมีโครงสร้างดังนี้

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)

ขั้นตอนทั่วไปในการเข้ารหัสโดยใช้ API ขั้นสูงจะมีลักษณะดังนี้

ก่อนอื่น เราต้องกำหนดค่าการเข้ารหัสที่มีพารามิเตอร์การบีบอัด โปรดทราบว่าการกำหนดค่าเดียวกันสามารถใช้ในการบีบอัดรูปภาพต่างๆ ได้ภายหลัง

#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.

ในการปล่อยไบต์ที่บีบอัด ระบบจะเรียก Hook ทุกครั้งที่มีไบต์ใหม่ ต่อไปนี้เป็นตัวอย่างง่ายๆ ที่มีการประกาศผู้เขียนหน่วยความจำไว้ใน 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);
}

หากต้องการใช้งาน API และโครงสร้างขั้นสูง เราขอแนะนำให้ดูเอกสารที่มีอยู่ในส่วนหัว webp/encode.h การอ่านโค้ดตัวอย่าง examples/cwebp.c อาจมีประโยชน์ สำหรับการค้นพบพารามิเตอร์ที่ใช้น้อย