Class NativeWebP

java.lang.Object
dev.matrixlab.webp4j.internal.NativeWebP

public class NativeWebP extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    decodeAnimatedWebP(byte[] webPData, AnimatedWebPData result)
    Decodes an animated WebP image into individual frames.
    static boolean
    decodeRGBAInto(byte[] data, byte[] outputBuffer, int outputStride)
    uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
    static boolean
    decodeRGBInto(byte[] data, byte[] outputBuffer, int outputStride)
    uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
    static byte[]
    encodeAnimatedWebP(byte[][] frames, int[] delays, int width, int height, float quality, boolean lossless, int compressionMethod, int loopCount, int kmin, int kmax, boolean minimizeSize, boolean allowMixed)
    Encodes animated WebP from Java-decoded GIF frames.
    static byte[]
    encodeGifToWebP(byte[] gifData, float quality, boolean lossless, int compressionMethod, boolean extractFirstFrameOnly, int loopCount, int kmin, int kmax, boolean minimizeSize, boolean allowMixed)
    Converts GIF data to WebP format using native giflib decoder.
    static byte[]
    encodeLosslessRGB(byte[] image, int width, int height, int stride)
    size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, int stride, uint8_t** output);
    static byte[]
    encodeLosslessRGBA(byte[] image, int width, int height, int stride)
    size_t WebPEncodeLosslessRGBA(const uint8_t* rgba, int width, int height, int stride, uint8_t** output);
    static byte[]
    encodeRGB(byte[] image, int width, int height, int stride, float quality)
    size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output);
    static byte[]
    encodeRGBA(byte[] image, int width, int height, int stride, float quality)
    size_t WebPEncodeRGBA(const uint8_t* rgba, int width, int height, int stride, float quality_factor, uint8_t** output);
    static int
    getFeatures(byte[] data, int dataSize, WebPBitstreamFeatures features)
    VP8StatusCode WebPGetFeatures(const uint8_t* data, size_t data_size, WebPBitstreamFeatures* features);
    static boolean
    getGifInfo(byte[] gifData, AnimationInfo info)
    Gets information about a GIF file using native giflib.
    static boolean
    getInfo(byte[] data, int[] dimensions)
    int WebPGetInfo(const uint8_t* data, size_t data_size, int* width, int* height);
    static boolean
    Checks if the native library has been successfully loaded.
    static Throwable
    Returns the cause of unavailability if the native library failed to load.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getInfo

      public static boolean getInfo(byte[] data, int[] dimensions)
      int WebPGetInfo(const uint8_t* data, size_t data_size, int* width, int* height);
    • getFeatures

      public static int getFeatures(byte[] data, int dataSize, WebPBitstreamFeatures features)
      VP8StatusCode WebPGetFeatures(const uint8_t* data, size_t data_size, WebPBitstreamFeatures* features);
    • encodeRGB

      public static byte[] encodeRGB(byte[] image, int width, int height, int stride, float quality)
      size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output);
    • encodeRGBA

      public static byte[] encodeRGBA(byte[] image, int width, int height, int stride, float quality)
      size_t WebPEncodeRGBA(const uint8_t* rgba, int width, int height, int stride, float quality_factor, uint8_t** output);
    • encodeLosslessRGB

      public static byte[] encodeLosslessRGB(byte[] image, int width, int height, int stride)
      size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, int stride, uint8_t** output);
    • encodeLosslessRGBA

      public static byte[] encodeLosslessRGBA(byte[] image, int width, int height, int stride)
      size_t WebPEncodeLosslessRGBA(const uint8_t* rgba, int width, int height, int stride, uint8_t** output);
    • decodeRGBInto

      public static boolean decodeRGBInto(byte[] data, byte[] outputBuffer, int outputStride)
      uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
    • decodeRGBAInto

      public static boolean decodeRGBAInto(byte[] data, byte[] outputBuffer, int outputStride)
      uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
    • getGifInfo

      public static boolean getGifInfo(byte[] gifData, AnimationInfo info)
      Gets information about a GIF file using native giflib.
      Parameters:
      gifData - GIF image bytes
      info - AnimationInfo object to populate (via JNI field access)
      Returns:
      True on success, false on failure
    • encodeGifToWebP

      public static byte[] encodeGifToWebP(byte[] gifData, float quality, boolean lossless, int compressionMethod, boolean extractFirstFrameOnly, int loopCount, int kmin, int kmax, boolean minimizeSize, boolean allowMixed)
      Converts GIF data to WebP format using native giflib decoder. This is the primary (fast) path using native GIF decoding.
      Parameters:
      gifData - GIF image bytes
      quality - Quality factor (0-100)
      lossless - True for lossless encoding
      compressionMethod - Compression method (0-6)
      extractFirstFrameOnly - True to extract only first frame
      loopCount - Loop count (0=infinite, -1=use GIF's)
      kmin - Minimum key-frame distance
      kmax - Maximum key-frame distance
      minimizeSize - True to minimize output size
      allowMixed - True to allow mixed compression
      Returns:
      WebP encoded byte array, or null on failure
    • encodeAnimatedWebP

      public static byte[] encodeAnimatedWebP(byte[][] frames, int[] delays, int width, int height, float quality, boolean lossless, int compressionMethod, int loopCount, int kmin, int kmax, boolean minimizeSize, boolean allowMixed)
      Encodes animated WebP from Java-decoded GIF frames. This is used when GIF is decoded by Java ImageIO (fallback path). Uses WebPAnimEncoder API to create animated WebP.
      Parameters:
      frames - Array of RGBA frame data (each frame is width * height * 4 bytes)
      delays - Array of frame delays in milliseconds
      width - Canvas width
      height - Canvas height
      quality - Quality factor (0-100)
      lossless - True for lossless encoding
      compressionMethod - Compression method (0-6)
      loopCount - Loop count (0=infinite)
      kmin - Minimum key-frame distance
      kmax - Maximum key-frame distance
      minimizeSize - True to minimize output size
      allowMixed - True to allow mixed compression
      Returns:
      WebP encoded byte array, or null on failure
    • decodeAnimatedWebP

      public static boolean decodeAnimatedWebP(byte[] webPData, AnimatedWebPData result)
      Decodes an animated WebP image into individual frames.

      Uses the libwebp WebPAnimDecoder API to extract all frames as RGBA data along with their cumulative timestamps.

      The result object will have its fields populated: - canvasWidth, canvasHeight, loopCount, bgcolor, frameCount (metadata) - rawFrameData (byte[][] of RGBA frame data) - timestamps (int[] of cumulative timestamps in milliseconds)

      Parameters:
      webPData - The animated WebP image data
      result - AnimatedWebPData object to populate with decoded frames
      Returns:
      true on success, false on failure
    • isAvailable

      public static boolean isAvailable()
      Checks if the native library has been successfully loaded.

      This method can be used to determine if WebP operations are available on the current platform without actually attempting to encode/decode.

      Returns:
      true if the native library is loaded and available, false otherwise
    • unavailabilityCause

      public static Throwable unavailabilityCause()
      Returns the cause of unavailability if the native library failed to load.

      This is useful for debugging when WebP support is not available. The returned Throwable can indicate issues like:

      • Unsupported platform (no native library for this OS/architecture)
      • Missing dependencies
      • JNI signature mismatch
      Returns:
      The Throwable that caused loading to fail, or null if library loaded successfully