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) {
}
}
}
Aggregations