All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luciad.imageio.webp.WebP Maven / Gradle / Ivy

There is a newer version: 2.5.8
Show newest version
package com.luciad.imageio.webp;

import org.scijava.nativelib.NativeLibraryUtil;

import java.io.IOException;
import java.nio.ByteOrder;

final class WebP {

    private static boolean NATIVE_LIBRARY_LOADED = false;

    static synchronized void loadNativeLibrary() {
        if (!NATIVE_LIBRARY_LOADED) {
            NATIVE_LIBRARY_LOADED = true;
            NativeLibraryUtil.loadNativeLibrary(WebP.class, "webp-imageio");
        }
    }

    static {
        loadNativeLibrary();
    }

    private WebP() {
    }

    public static int[] decode(WebPDecoderOptions aOptions, byte[] aData, int aOffset, int aLength, int[] aOut) throws IOException {
        if (aOptions == null) {
            throw new NullPointerException("Decoder options may not be null");
        }

        if (aData == null) {
            throw new NullPointerException("Input data may not be null");
        }

        if (aOffset + aLength > aData.length) {
            throw new IllegalArgumentException("Offset/length exceeds array size");
        }

        int[] pixels = decode(aOptions.fPointer, aData, aOffset, aLength, aOut, ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN));
        VP8StatusCode status = VP8StatusCode.getStatusCode(aOut[0]);
        switch (status) {
            case VP8_STATUS_OK:
                break;
            case VP8_STATUS_OUT_OF_MEMORY:
                throw new OutOfMemoryError();
            default:
                throw new IOException("Decode returned code " + status);
        }

        return pixels;
    }

    private static native int[] decode(long aDecoderOptionsPointer, byte[] aData, int aOffset, int aLength, int[] aFlags, boolean aBigEndian);

    public static int[] getInfo(byte[] aData, int aOffset, int aLength) throws IOException {
        int[] out = new int[2];
        int result = getInfo(aData, aOffset, aLength, out);
        if (result == 0) {
            throw new IOException("Invalid WebP data");
        }

        return out;
    }

    private static native int getInfo(byte[] aData, int aOffset, int aLength, int[] aOut);

    public static byte[] encodeRGBA(WebPEncoderOptions aOptions, byte[] aRgbaData, int aWidth, int aHeight, int aStride) {
        return encodeRGBA(aOptions.fPointer, aRgbaData, aWidth, aHeight, aStride);
    }

    private static native byte[] encodeRGBA(long aConfig, byte[] aRgbaData, int aWidth, int aHeight, int aStride);

    public static byte[] encodeRGB(WebPEncoderOptions aOptions, byte[] aRgbaData, int aWidth, int aHeight, int aStride) {
        return encodeRGB(aOptions.fPointer, aRgbaData, aWidth, aHeight, aStride);
    }

    private static native byte[] encodeRGB(long aConfig, byte[] aRgbaData, int aWidth, int aHeight, int aStride);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy