GifInfoHandle.java 源代码


package pl.droidsonroids.gif;

import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.net.Uri;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public final class GifInfoHandle {

    private volatile long f25608a;

    static {
        i.a(null);
    }

    public GifInfoHandle() {
    }

    private static native void free(long j6);

    private static native long getAllocationByteCount(long j6);

    private static native int getCurrentFrameIndex(long j6);

    private static native int getCurrentLoop(long j6);

    private static native int getCurrentPosition(long j6);

    private static native int getDuration(long j6);

    private static native int getHeight(long j6);

    private static native int getLoopCount(long j6);

    private static native long getMetadataByteCount(long j6);

    private static native int getNativeErrorCode(long j6);

    private static native int getNumberOfFrames(long j6);

    private static native long[] getSavedState(long j6);

    private static native int getWidth(long j6);

    private static native boolean isOpaque(long j6);

    public static GifInfoHandle o(ContentResolver contentResolver, Uri uri) throws IOException {
        if ("file".equals(uri.getScheme())) {
            return new GifInfoHandle(uri.getPath());
        }
        return new GifInfoHandle(contentResolver.openAssetFileDescriptor(uri, "r"));
    }

    static native long openByteArray(byte[] bArr) throws GifIOException;

    static native long openDirectByteBuffer(ByteBuffer byteBuffer) throws GifIOException;

    static native long openFd(FileDescriptor fileDescriptor, long j6) throws GifIOException;

    static native long openFile(String str) throws GifIOException;

    static native long openStream(InputStream inputStream) throws GifIOException;

    private static native void postUnbindSurface(long j6);

    private static native long renderFrame(long j6, Bitmap bitmap);

    private static native boolean reset(long j6);

    private static native long restoreRemainder(long j6);

    private static native int restoreSavedState(long j6, long[] jArr, Bitmap bitmap);

    private static native void saveRemainder(long j6);

    private static native void seekToTime(long j6, int i6, Bitmap bitmap);

    private static native void setSpeedFactor(long j6, float f6);

    public synchronized long a() {
        return getAllocationByteCount(this.f25608a);
    }

    public synchronized int b() {
        return getCurrentFrameIndex(this.f25608a);
    }

    public synchronized int c() {
        return getCurrentLoop(this.f25608a);
    }

    public synchronized int d() {
        return getCurrentPosition(this.f25608a);
    }

    public synchronized int e() {
        return getDuration(this.f25608a);
    }

    public synchronized int f() {
        return getHeight(this.f25608a);
    }

    protected void finalize() throws Throwable {
        try {
            q();
        } finally {
            super.finalize();
        }
    }

    public synchronized int g() {
        return getLoopCount(this.f25608a);
    }

    public synchronized long h() {
        return getMetadataByteCount(this.f25608a);
    }

    public synchronized int i() {
        return getNativeErrorCode(this.f25608a);
    }

    public synchronized int j() {
        return getNumberOfFrames(this.f25608a);
    }

    public synchronized long[] k() {
        return getSavedState(this.f25608a);
    }

    public synchronized int l() {
        return getWidth(this.f25608a);
    }

    public synchronized boolean m() {
        return isOpaque(this.f25608a);
    }

    public synchronized boolean n() {
        boolean z6;
        if (this.f25608a == 0) {
            z6 = true;
        } else {
            z6 = false;
        }
        return z6;
    }

    public synchronized void p() {
        postUnbindSurface(this.f25608a);
    }

    public synchronized void q() {
        free(this.f25608a);
        this.f25608a = 0L;
    }

    public synchronized long r(Bitmap bitmap) {
        return renderFrame(this.f25608a, bitmap);
    }

    public synchronized boolean s() {
        return reset(this.f25608a);
    }

    public synchronized long t() {
        return restoreRemainder(this.f25608a);
    }

    public synchronized int u(long[] jArr, Bitmap bitmap) {
        return restoreSavedState(this.f25608a, jArr, bitmap);
    }

    public synchronized void v() {
        saveRemainder(this.f25608a);
    }

    public synchronized void w(@IntRange(from = 0, to = 2147483647L) int i6, Bitmap bitmap) {
        seekToTime(this.f25608a, i6, bitmap);
    }

    public void x(@FloatRange(from = 0.0d, fromInclusive = false) float f6) {
        if (f6 > 0.0f && !Float.isNaN(f6)) {
            if (f6 < 4.656613E-10f) {
                f6 = 4.656613E-10f;
            }
            synchronized (this) {
                setSpeedFactor(this.f25608a, f6);
            }
            return;
        }
        throw new IllegalArgumentException("Speed factor is not positive");
    }

    public GifInfoHandle(FileDescriptor fileDescriptor) throws GifIOException {
        this.f25608a = openFd(fileDescriptor, 0L);
    }

    public GifInfoHandle(byte[] bArr) throws GifIOException {
        this.f25608a = openByteArray(bArr);
    }

    public GifInfoHandle(ByteBuffer byteBuffer) throws GifIOException {
        this.f25608a = openDirectByteBuffer(byteBuffer);
    }

    public GifInfoHandle(String str) throws GifIOException {
        this.f25608a = openFile(str);
    }

    public GifInfoHandle(InputStream inputStream) throws GifIOException {
        if (inputStream.markSupported()) {
            this.f25608a = openStream(inputStream);
            return;
        }
        throw new IllegalArgumentException("InputStream does not support marking");
    }

    public GifInfoHandle(AssetFileDescriptor assetFileDescriptor) throws IOException {
        try {
            this.f25608a = openFd(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset());
        } finally {
            try {
                assetFileDescriptor.close();
            } catch (IOException unused) {
            }
        }
    }
}