Class GifToWebPConverter
This class provides methods to convert both static and animated GIF images to WebP format. It supports a dual-path approach:
- Primary: Native giflib decoder (fast, complete GIF support)
- Fallback: Java ImageIO decoder (slower, but works when native library unavailable)
- Author:
- MrNanko
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]convert(byte[] gifData) Converts a GIF image to WebP format with default lossy settings.static byte[]convert(byte[] gifData, GifToWebPConfig config) Converts a GIF image to WebP format.static byte[]convertLossless(byte[] gifData) Converts a GIF image to lossless WebP format.static byte[]convertUsingJavaImageIO(byte[] gifData, GifToWebPConfig config) Fallback implementation: Encodes GIF to WebP using Java ImageIO for GIF decoding.static AnimationInfogetInfo(byte[] gifData) Gets information about a GIF image without performing full conversion.
-
Method Details
-
getInfo
Gets information about a GIF image without performing full conversion.This is useful for determining whether a GIF is animated, its dimensions, and other properties before deciding how to convert it.
- Parameters:
gifData- Byte array containing the GIF image data- Returns:
- AnimationInfo containing frame count, dimensions, loop count, and transparency info
- Throws:
IOException- If reading GIF information failsIllegalArgumentException- If gifData is null or empty
-
convert
Converts a GIF image to WebP format with default lossy settings.This is a convenience method that uses quality factor 75 and lossy compression. For more control over the conversion, use
convert(byte[], GifToWebPConfig).- Parameters:
gifData- Byte array containing the GIF image data- Returns:
- Byte array containing the WebP encoded image
- Throws:
IOException- If conversion fails
-
convert
Converts a GIF image to WebP format.This method automatically detects whether the GIF is static or animated, and handles both cases appropriately.
- Parameters:
gifData- Byte array containing the GIF image dataconfig- Configuration for conversion (quality, lossless, compression, etc.)- Returns:
- Byte array containing the WebP encoded image
- Throws:
IOException- If conversion failsIllegalArgumentException- If gifData or config is null
-
convertLossless
Converts a GIF image to lossless WebP format.This is a convenience method that uses lossless compression to preserve the original GIF quality without any loss.
- Parameters:
gifData- Byte array containing the GIF image data- Returns:
- Byte array containing the lossless WebP encoded image
- Throws:
IOException- If conversion fails
-
convertUsingJavaImageIO
public static byte[] convertUsingJavaImageIO(byte[] gifData, GifToWebPConfig config) throws IOException Fallback implementation: Encodes GIF to WebP using Java ImageIO for GIF decoding.This method is used when native giflib is unavailable. It decodes the GIF using Java's ImageIO, then encodes to WebP using native WebPAnimEncoder.
Package-private for testing purposes.
- Parameters:
gifData- Byte array containing the GIF image dataconfig- Configuration for conversion- Returns:
- Byte array containing the WebP encoded image
- Throws:
IOException- If conversion fails
-