Search in sources :

Example 1 with GifMetadataDecoder

use of com.facebook.animated.giflite.decoder.GifMetadataDecoder in project fresco by facebook.

the class GifDecoder method decode.

@Override
public CloseableImage decode(final EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) {
    InputStream is = encodedImage.getInputStream();
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GifMetadataDecoder decoder = GifMetadataDecoder.create(is, out);
        if (out.size() > 0) {
            // let's use the fixed gif version if exists
            is.close();
            is = new ByteArrayInputStream(out.toByteArray());
        }
        is.reset();
        Movie movie = Movie.decodeStream(is);
        MovieDrawer drawer = new MovieDrawer(movie);
        MovieFrame[] frames = new MovieFrame[decoder.getFrameCount()];
        int currTime = 0;
        for (int frameNumber = 0, N = frames.length; frameNumber < N; frameNumber++) {
            int frameDuration = decoder.getFrameDurationMs(frameNumber);
            currTime += frameDuration;
            frames[frameNumber] = new MovieFrame(drawer, currTime, frameDuration, movie.width(), movie.height(), translateFrameDisposal(decoder.getFrameDisposal(frameNumber)));
        }
        return new CloseableAnimatedImage(AnimatedImageResult.forAnimatedImage(new MovieAnimatedImage(frames, encodedImage.getSize(), movie.duration(), decoder.getLoopCount())), false);
    } catch (IOException e) {
        throw new RuntimeException("Error while decoding gif", e);
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : Movie(android.graphics.Movie) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) GifMetadataDecoder(com.facebook.animated.giflite.decoder.GifMetadataDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) MovieFrame(com.facebook.animated.giflite.draw.MovieFrame) MovieDrawer(com.facebook.animated.giflite.draw.MovieDrawer) MovieAnimatedImage(com.facebook.animated.giflite.draw.MovieAnimatedImage)

Example 2 with GifMetadataDecoder

use of com.facebook.animated.giflite.decoder.GifMetadataDecoder in project fresco by facebook.

the class GifAnimationBackend method create.

public static GifAnimationBackend create(String filePath) throws IOException {
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(filePath));
        is.mark(Integer.MAX_VALUE);
        GifMetadataDecoder decoder = GifMetadataDecoder.create(is, null);
        is.reset();
        Movie movie = Movie.decodeStream(is);
        return new GifAnimationBackend(decoder, movie);
    } finally {
        closeSilently(is);
    }
}
Also used : Movie(android.graphics.Movie) BufferedInputStream(java.io.BufferedInputStream) GifMetadataDecoder(com.facebook.animated.giflite.decoder.GifMetadataDecoder) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream)

Aggregations

Movie (android.graphics.Movie)2 GifMetadataDecoder (com.facebook.animated.giflite.decoder.GifMetadataDecoder)2 InputStream (java.io.InputStream)2 MovieAnimatedImage (com.facebook.animated.giflite.draw.MovieAnimatedImage)1 MovieDrawer (com.facebook.animated.giflite.draw.MovieDrawer)1 MovieFrame (com.facebook.animated.giflite.draw.MovieFrame)1 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1