Search in sources :

Example 1 with MovieAnimatedImage

use of com.facebook.animated.giflite.draw.MovieAnimatedImage 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)

Aggregations

Movie (android.graphics.Movie)1 GifMetadataDecoder (com.facebook.animated.giflite.decoder.GifMetadataDecoder)1 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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1