WZWEBP SDK Documentation
Introduction
WZWEBP is a powerful implementation for WebP image compression and encoding. It provides robust functionality for handling WebP images with various optimization features and quality settings, supporting both lossy and lossless compression modes.
Key Options
- Quality Control: Adjustable compression quality
[0, 100] - Method: Compression method (0-6), balancing encoding speed and compression ratio
0: Fastest compression4: Default - balanced speed/compression6: Slowest, best compression
- Multiple Compression Methods: Support for both lossy and lossless compression modes
- Lossy: Default mode, best for photographic images
- Lossless: Perfect reconstruction, best for graphics with text
- Advanced Features: Support for metadata (EXIF, XMP, ICC), alpha channel, and various preprocessing options
API Definitions
Key functions:
/* Initialize encoding */
WebPConfig config; // Configuration object
WebPPicture pic; // Picture object
WebPConfigInit(&config); // Initialize config with default values
WebPPictureInit(&pic); // Initialize picture object
/* Set parameters */
WebPConfigPreset(&config, preset, quality_factor); // Set encoding preset and quality
WebPValidateConfig(&config); // Validate configuration
WebPPictureAlloc(&pic); // Allocate picture memory
/* Encoding process */
WebPEncode(&config, &pic); // Start encoding
WebPPictureFree(&pic); // Clean up
API Parameters
Basic Quality Setting
- Function:
WebPConfigPreset(&config, preset, quality_factor) - Quality Range: [0, 100] where higher numbers indicate better video quality.
- Example:
WebPConfigPreset(&config, WEBP_PRESET_PHOTO, 90)for high-quality photo compression
Compression Presets
Available presets:
WEBP_PRESET_DEFAULT: Default presetWEBP_PRESET_PHOTO: High-quality photo compressionWEBP_PRESET_PICTURE: Digital picture, like portrait, inner shotWEBP_PRESET_DRAWING: Hand or line drawing with high contrastWEBP_PRESET_ICON: Small-sized colorful imagesWEBP_PRESET_TEXT: Text-like images
Direct Parameter Settings
Many parameters can be set directly through the config structure made available to the user:
WebPConfig config;
// 1. First initialize with default values
if (!WebPConfigInit(&config)) {
return 0; // version error
}
// 2. Apply any preset (optional)
if (!WebPConfigPreset(&config, WEBP_PRESET_PHOTO, quality_factor)) {
return 0;
}
// 3. Set other parameters directly
config.method = 6;
config.quality = 90;
config.alpha_quality = 100;
config.thread_level = 1;
// 4. Validate configuration
if (!WebPValidateConfig(&config)) {
return 0;
}
Key direct parameters:
- Lossless:
config.lossless = 0/1(0= lossy;1= lossless mode) - Method:
config.method = 0-6(compression speed vs quality tradeoff) - Thread-level:
config.thread_level(multi-threading support:0: use single thread,1: use multi-threading) - Low memory:
config.low_memory = 1(reduces memory usage at cost of speed.0: disablelow_memorymode,1: uselow_memorymode) - Alpha quality:
config.alpha_quality. Specify quality for alpha channel, range[0, 100], where100means lossless. The default is100.
API Usage
Key functions for encoding:
// Initialize configuration with default values
WebPConfig config;
WebPPicture pic;
WebPConfigInit(&config);
WebPPictureInit(&pic);
// Set encoding parameters
config.quality = 90.0f; // 0-100
config.method = 6; // 0-6
config.lossless = 0; // 0=lossy, 1=lossless
config.image_hint = WEBP_HINT_PHOTO; // Photo/Picture/Graph
// Allocate and import picture data
pic.width = width;
pic.height = height;
pic.use_argb = 1; // Use ARGB format
WebPPictureAlloc(&pic);
WebPPictureImportRGBA(&pic, rgba_data, rgba_stride);
// Encode
WebPEncode(&config, &pic);
// Cleanup
WebPPictureFree(&pic);
Sample Encoder: cwebp
Introduction to cwebp
The WZWebP Library provides a command-line encoder tool called cwebp that demonstrates how to use the WZWebP Library for image compression. It accepts various image formats (PNG, JPEG, TIFF, WebP) as input and produces WebP files as output. The encoder showcases key features of the WZWebP Library including:
- Quality control (0-100 scale)
- Multiple compression methods (0-6)
- Lossy and lossless compression modes
- Metadata handling (EXIF, XMP, ICC)
- Multi-threading support
- Memory optimization options
- Advanced features like alpha channel handling and preprocessing
Building the cwebp
To build the cwebp encoder, please follow these steps:
- Ensure you have the
WZWebPLibrary and its dependencies installed - Compile using the following command line:
gcc -o cwebp examples/cwebp.c \
-I/usr/local/include \
-L/usr/local/lib \
-lwebp -lwebpmux -lsharpyuv
Notes:
- Adjust the include path (-I) and library path (-L) according to your
WZWebPLibrary installation location - Required libraries:
libwebp: Core WZWebP Librarylibwebpmux: WebP muxing librarylibsharpyuv: YUV conversion library
- For better performance, add optimization flags:
gcc -O3 -o cwebp ...
Running cwebp
Basic usage:
cwebp [options] input_file -o output_file.webp
Key Parameters:
-q <float> # Quality factor (0:smallest..100:biggest)
-m <int> # Compression method (0=fast, 6=slowest)
-lossless # Enable lossless compression
-preset <string> # Default/photo/picture/drawing/icon/text
-metadata <string> # Comma separated list of metadata to copy
-mt # Use multi-threading if available
Example usage:
# High-quality lossy compression
cwebp -q 90 -m 6 input.png -o output.webp
# Lossless compression
cwebp -lossless -m 6 input.png -o output.webp
# Fast compression with reduced memory usage
cwebp -q 75 -m 0 -low_memory input.png -o output.webp
Additional Resources
For more detailed information about specific functions and parameters, please refer to:
- Header files (
webp/encode.h,webp/decode.h) - Source code documentation
- WebP container specification (
doc/webp-container-spec.txt)
License and Attribution
- This library is developed and maintained by Google Inc.
- Licensed under a BSD-style license that can be found in the COPYING file.
- Additional intellectual property rights can be found in the PATENTS file.