ส่วนนี้อธิบาย API สำหรับโปรแกรมเปลี่ยนไฟล์และตัวถอดรหัสที่รวมอยู่ในไลบรารี WebP คำอธิบาย API นี้เกี่ยวข้องกับเวอร์ชัน 1.4.0
ส่วนหัวและไลบรารี
เมื่อคุณติดตั้ง libwebp
ไดเรกทอรีชื่อ webp/
จะติดตั้งในตำแหน่งที่ปกติสำหรับแพลตฟอร์มของคุณ เช่น ในแพลตฟอร์ม Unix ระบบจะคัดลอกไฟล์ส่วนหัวต่อไปนี้ไปยัง /usr/local/include/webp/
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 ถึง 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);
ฟังก์ชันเหล่านี้จะถอดรหัสอิมเมจ 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 ถึง 16383
- ส่วนสูง
- ค่าจำนวนเต็ม ปัจจุบันช่วงดังกล่าวถูกจำกัดตั้งแต่ 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.เอาต์พุต (หรือในกรณีนี้คือ 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 ไบต์ต่อ 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.
หากต้องการปล่อยไบต์ที่บีบอัด ระบบจะเรียกเว็บฮุคทุกครั้งที่มีไบต์ใหม่ ต่อไปนี้เป็นตัวอย่างง่ายๆ ที่มีการประกาศผู้เขียนความทรงจำใน 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
อาจมีประโยชน์ในการค้นพบพารามิเตอร์ที่ใช้น้อย