Class GifToWebPConverter

java.lang.Object
dev.matrixlab.webp4j.gif.GifToWebPConverter

public final class GifToWebPConverter extends Object
Converter for transforming GIF images to WebP format.

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 Details

    • getInfo

      public static AnimationInfo getInfo(byte[] gifData) throws IOException
      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 fails
      IllegalArgumentException - If gifData is null or empty
    • convert

      public static byte[] convert(byte[] gifData) throws IOException
      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

      public static byte[] convert(byte[] gifData, GifToWebPConfig config) throws IOException
      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 data
      config - Configuration for conversion (quality, lossless, compression, etc.)
      Returns:
      Byte array containing the WebP encoded image
      Throws:
      IOException - If conversion fails
      IllegalArgumentException - If gifData or config is null
    • convertLossless

      public static byte[] convertLossless(byte[] gifData) throws IOException
      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 data
      config - Configuration for conversion
      Returns:
      Byte array containing the WebP encoded image
      Throws:
      IOException - If conversion fails