libwebp
Mirror only. Please do not send pull requests. See https://chromium.googlesource.com/webm/libwebp/+/HEAD/CONTRIBUTING.md.
Top Related Projects
Perceptual JPEG encoder
Improved JPEG encoder.
Main libjpeg-turbo repository
libavif - Library for encoding and decoding .avif files
libheif is an HEIF and AVIF file format decoder and encoder.
Lossy PNG compressor — pngquant command based on libimagequant library
Quick Overview
libwebp is an open-source library developed by Google for encoding and decoding WebP images. WebP is a modern image format that provides superior lossless and lossy compression for images on the web, resulting in smaller file sizes while maintaining high visual quality.
Pros
- Significantly smaller file sizes compared to traditional formats like JPEG and PNG
- Supports both lossless and lossy compression
- Offers alpha channel transparency with lower file sizes than PNG
- Wide browser support, including all major modern browsers
Cons
- Not universally supported by all image editing software
- Slightly higher encoding time compared to JPEG
- Limited support on older systems and browsers
- Some quality loss in lossy compression mode, especially at very high compression levels
Code Examples
- Encoding a WebP image:
#include <webp/encode.h>
int EncodeWebP(const uint8_t* rgb, int width, int height, float quality, uint8_t** output) {
size_t size;
*output = WebPEncodeRGB(rgb, width, height, width * 3, quality, &size);
return (*output != NULL) ? size : 0;
}
- Decoding a WebP image:
#include <webp/decode.h>
uint8_t* DecodeWebP(const uint8_t* webp_data, size_t data_size, int* width, int* height) {
return WebPDecodeRGB(webp_data, data_size, width, height);
}
- Converting RGBA buffer to WebP:
#include <webp/encode.h>
uint8_t* ConvertRGBAToWebP(const uint8_t* rgba, int width, int height, float quality, size_t* output_size) {
return WebPEncodeRGBA(rgba, width, height, width * 4, quality, output_size);
}
Getting Started
To use libwebp in your project, follow these steps:
-
Install libwebp using your package manager or build from source:
sudo apt-get install libwebp-dev # For Ubuntu/Debian
-
Include the necessary headers in your C/C++ code:
#include <webp/encode.h> #include <webp/decode.h>
-
Link against the libwebp library when compiling:
gcc -o your_program your_program.c -lwebp
-
Use the WebP encoding and decoding functions in your code as shown in the examples above.
Competitor Comparisons
Perceptual JPEG encoder
Pros of Guetzli
- Produces significantly smaller JPEG files (20-30% reduction) while maintaining visual quality
- Optimized for perceptual quality, resulting in visually pleasing images
- Suitable for high-quality image compression in web applications
Cons of Guetzli
- Slower compression speed compared to libwebp
- Limited to JPEG format, while libwebp supports multiple formats
- Higher memory usage during compression process
Code Comparison
Guetzli (C++):
guetzli::Params params;
params.butteraugli_target = 0.5;
guetzli::ProcessJpegData(params, jpg_data, jpg_data_len, &output_data);
libwebp (C):
WebPConfig config;
WebPConfigInit(&config);
config.quality = 75;
WebPEncode(&config, &picture, &output);
Both libraries offer simple APIs for image compression, but Guetzli focuses on JPEG optimization while libwebp provides broader format support. Guetzli's approach emphasizes perceptual quality and file size reduction, whereas libwebp offers faster compression and more versatile format options. The choice between the two depends on specific project requirements, such as compression speed, supported formats, and desired output quality.
Improved JPEG encoder.
Pros of mozjpeg
- Better compression for JPEG images, resulting in smaller file sizes
- Maintains high image quality while reducing file size
- Optimized for web use, improving page load times
Cons of mozjpeg
- Limited to JPEG format, while libwebp supports multiple formats
- Slower encoding process compared to libwebp
- Less widespread adoption and browser support than WebP format
Code Comparison
mozjpeg:
cinfo.optimize_coding = TRUE;
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_simple_progression(&cinfo);
libwebp:
WebPConfig config;
WebPConfigInit(&config);
config.quality = quality;
config.method = 6; // Slowest but best compression
Both libraries offer simple APIs for configuring compression settings. mozjpeg focuses on optimizing JPEG encoding, while libwebp provides options for WebP format compression.
mozjpeg is ideal for projects requiring high-quality JPEG compression, especially for web use. libwebp offers a more versatile solution with support for multiple formats and potentially faster encoding, but may have slightly lower compression ratios for JPEG images compared to mozjpeg.
Main libjpeg-turbo repository
Pros of libjpeg-turbo
- Faster JPEG compression and decompression compared to libwebp
- Wider compatibility with existing JPEG-based systems and software
- Smaller file sizes for photographic images in some cases
Cons of libjpeg-turbo
- Limited to JPEG format, lacking support for modern image formats
- Lower compression efficiency for non-photographic images compared to WebP
Code Comparison
libjpeg-turbo:
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
libwebp:
WebPConfig config;
WebPConfigInit(&config);
config.quality = quality;
WebPEncode(&config, &picture);
The code snippets demonstrate the basic setup for compression in both libraries. libjpeg-turbo uses a more verbose approach with multiple function calls, while libwebp offers a more streamlined API with fewer function calls required for basic encoding.
Both libraries are widely used and have their strengths. libjpeg-turbo excels in JPEG processing and is ideal for systems that primarily work with JPEG images. libwebp, on the other hand, offers a more modern image format with better compression for certain types of images and support for features like animation and alpha transparency.
libavif - Library for encoding and decoding .avif files
Pros of libavif
- Better compression efficiency, resulting in smaller file sizes for similar quality
- Support for HDR and wide color gamut images
- More advanced features like multi-layer images and animations
Cons of libavif
- Slower encoding and decoding times compared to libwebp
- Less widespread browser support, especially for older versions
- Larger library size and more complex implementation
Code Comparison
libavif encoding example:
avifEncoder * encoder = avifEncoderCreate();
avifEncoderSetCodecChoice(encoder, AVIF_CODEC_CHOICE_AOM);
avifEncoderWrite(encoder, image, &raw);
avifEncoderDestroy(encoder);
libwebp encoding example:
WebPConfig config;
WebPConfigInit(&config);
WebPPicture picture;
WebPPictureInit(&picture);
WebPEncode(&config, &picture);
Both libraries offer C APIs for encoding and decoding images. libavif's API is more object-oriented, while libwebp's API is more procedural. libavif provides more configuration options for advanced use cases, but libwebp's API is generally simpler for basic encoding tasks.
libheif is an HEIF and AVIF file format decoder and encoder.
Pros of libheif
- Supports HEIF format, which offers better compression and quality than WebP
- Provides both encoding and decoding capabilities for HEIF/HEIC images
- Offers more advanced features like HDR support and multi-image containers
Cons of libheif
- Less widespread browser support compared to WebP
- Potentially higher computational requirements for encoding/decoding
- Smaller community and ecosystem compared to WebP
Code Comparison
libheif example:
heif_context* ctx = heif_context_alloc();
heif_context_read_from_file(ctx, input_filename, NULL);
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);
heif_image* img;
heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGB, NULL);
libwebp example:
WebPDecoderConfig config;
WebPInitDecoderConfig(&config);
WebPDecode(data, data_size, &config);
uint8_t* output = config.output.u.RGBA.rgba;
int width = config.output.width;
int height = config.output.height;
Both libraries provide C APIs for decoding images, but libheif's API is more verbose due to its support for more complex image formats and features. libwebp's API is simpler for basic decoding tasks.
Lossy PNG compressor — pngquant command based on libimagequant library
Pros of pngquant
- Specializes in PNG optimization, offering excellent compression for PNG files
- Supports alpha channel preservation, crucial for transparent images
- Faster processing times for PNG files compared to libwebp
Cons of pngquant
- Limited to PNG format, while libwebp supports multiple image formats
- May result in some quality loss due to lossy compression, whereas libwebp offers lossless options
- Less flexibility in terms of output formats compared to libwebp
Code Comparison
pngquant:
void pngquant_image(png24_image *input_image, png8_image *output_image,
const quant_options *options) {
// Quantization logic here
}
libwebp:
int WebPEncode(const WebPConfig* config, WebPPicture* picture) {
// Encoding logic here
}
The code snippets show the core functions for image processing in each library. pngquant focuses on quantization for PNG images, while libwebp provides a more general encoding function for WebP format.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
WebP Codec
__ __ ____ ____ ____
/ \\/ \/ _ \/ _ )/ _ \
\ / __/ _ \ __/
\__\__/\____/\_____/__/ ____ ___
/ _/ / \ \ / _ \/ _/
/ \_/ / / \ \ __/ \__
\____/____/\_____/_____/____/v1.4.0
WebP codec is a library to encode and decode images in WebP format. This package contains the library that can be used in other programs to add WebP support, as well as the command line tools 'cwebp' and 'dwebp' to compress and decompress images respectively.
See https://developers.google.com/speed/webp for details on the image format.
The latest source tree is available at https://chromium.googlesource.com/webm/libwebp
It is released under the same license as the WebM project. See https://www.webmproject.org/license/software/ or the "COPYING" file for details. An additional intellectual property rights grant can be found in the file PATENTS.
Building
See the building documentation.
Encoding and Decoding Tools
The examples/ directory contains tools to encode and decode images and animations, view information about WebP images, and more. See the tools documentation.
APIs
See the APIs documentation, and API usage examples in the
examples/
directory.
Bugs
Please report all bugs to the issue tracker. For security reports, select 'Security report' from the Template dropdown.
Patches welcome! See how to contribute.
Discuss
Email: webp-discuss@webmproject.org
Web: https://groups.google.com/a/webmproject.org/group/webp-discuss
Top Related Projects
Perceptual JPEG encoder
Improved JPEG encoder.
Main libjpeg-turbo repository
libavif - Library for encoding and decoding .avif files
libheif is an HEIF and AVIF file format decoder and encoder.
Lossy PNG compressor — pngquant command based on libimagequant library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot