Search in sources :

Example 1 with GifDecoder

use of com.bumptech.glide.gifdecoder.GifDecoder in project glide by bumptech.

the class ReEncodingGifResourceEncoder method encodeTransformedToStream.

private boolean encodeTransformedToStream(GifDrawable drawable, OutputStream os) {
    Transformation<Bitmap> transformation = drawable.getFrameTransformation();
    GifDecoder decoder = decodeHeaders(drawable.getBuffer());
    AnimatedGifEncoder encoder = factory.buildEncoder();
    if (!encoder.start(os)) {
        return false;
    }
    for (int i = 0; i < decoder.getFrameCount(); i++) {
        Bitmap currentFrame = decoder.getNextFrame();
        Resource<Bitmap> transformedResource = getTransformedFrame(currentFrame, transformation, drawable);
        try {
            if (!encoder.addFrame(transformedResource.get())) {
                return false;
            }
            int currentFrameIndex = decoder.getCurrentFrameIndex();
            int delay = decoder.getDelay(currentFrameIndex);
            encoder.setDelay(delay);
            decoder.advance();
        } finally {
            transformedResource.recycle();
        }
    }
    return encoder.finish();
}
Also used : GifDecoder(com.bumptech.glide.gifdecoder.GifDecoder) StandardGifDecoder(com.bumptech.glide.gifdecoder.StandardGifDecoder) Bitmap(android.graphics.Bitmap) AnimatedGifEncoder(com.bumptech.glide.gifencoder.AnimatedGifEncoder)

Example 2 with GifDecoder

use of com.bumptech.glide.gifdecoder.GifDecoder in project glide by bumptech.

the class ReEncodingGifResourceEncoder method decodeHeaders.

private GifDecoder decodeHeaders(ByteBuffer data) {
    GifHeaderParser parser = factory.buildParser();
    parser.setData(data);
    GifHeader header = parser.parseHeader();
    GifDecoder decoder = factory.buildDecoder(provider);
    decoder.setData(header, data);
    decoder.advance();
    return decoder;
}
Also used : GifDecoder(com.bumptech.glide.gifdecoder.GifDecoder) StandardGifDecoder(com.bumptech.glide.gifdecoder.StandardGifDecoder) GifHeaderParser(com.bumptech.glide.gifdecoder.GifHeaderParser) GifHeader(com.bumptech.glide.gifdecoder.GifHeader)

Example 3 with GifDecoder

use of com.bumptech.glide.gifdecoder.GifDecoder in project glide by bumptech.

the class ByteBufferGifDecoder method decode.

private GifDrawableResource decode(ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser) {
    long startTime = LogTime.getLogTime();
    final GifHeader header = parser.parseHeader();
    if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) {
        // If we couldn't decode the GIF, we will end up with a frame count of 0.
        return null;
    }
    int sampleSize = getSampleSize(header, width, height);
    GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize);
    gifDecoder.advance();
    Bitmap firstFrame = gifDecoder.getNextFrame();
    if (firstFrame == null) {
        return null;
    }
    Transformation<Bitmap> unitTransformation = UnitTransformation.get();
    GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, bitmapPool, unitTransformation, width, height, firstFrame);
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime));
    }
    return new GifDrawableResource(gifDrawable);
}
Also used : StandardGifDecoder(com.bumptech.glide.gifdecoder.StandardGifDecoder) GifDecoder(com.bumptech.glide.gifdecoder.GifDecoder) Bitmap(android.graphics.Bitmap) GifHeader(com.bumptech.glide.gifdecoder.GifHeader)

Example 4 with GifDecoder

use of com.bumptech.glide.gifdecoder.GifDecoder in project glide by bumptech.

the class ByteBufferGifDecoder method decode.

@Nullable
private GifDrawableResource decode(ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser, Options options) {
    long startTime = LogTime.getLogTime();
    try {
        final GifHeader header = parser.parseHeader();
        if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) {
            // If we couldn't decode the GIF, we will end up with a frame count of 0.
            return null;
        }
        Bitmap.Config config = options.get(GifOptions.DECODE_FORMAT) == DecodeFormat.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888;
        int sampleSize = getSampleSize(header, width, height);
        GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize);
        gifDecoder.setDefaultBitmapConfig(config);
        gifDecoder.advance();
        Bitmap firstFrame = gifDecoder.getNextFrame();
        if (firstFrame == null) {
            return null;
        }
        Transformation<Bitmap> unitTransformation = UnitTransformation.get();
        GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, unitTransformation, width, height, firstFrame);
        return new GifDrawableResource(gifDrawable);
    } finally {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime));
        }
    }
}
Also used : GifDecoder(com.bumptech.glide.gifdecoder.GifDecoder) StandardGifDecoder(com.bumptech.glide.gifdecoder.StandardGifDecoder) Bitmap(android.graphics.Bitmap) GifHeader(com.bumptech.glide.gifdecoder.GifHeader) Nullable(android.support.annotation.Nullable)

Example 5 with GifDecoder

use of com.bumptech.glide.gifdecoder.GifDecoder in project glide by bumptech.

the class GifFrameResourceDecoderTest method setUp.

@Before
public void setUp() {
    gifDecoder = mock(GifDecoder.class);
    resourceDecoder = new GifFrameResourceDecoder(mock(BitmapPool.class));
    options = new Options();
}
Also used : GifDecoder(com.bumptech.glide.gifdecoder.GifDecoder) Options(com.bumptech.glide.load.Options) Before(org.junit.Before)

Aggregations

GifDecoder (com.bumptech.glide.gifdecoder.GifDecoder)5 StandardGifDecoder (com.bumptech.glide.gifdecoder.StandardGifDecoder)4 Bitmap (android.graphics.Bitmap)3 GifHeader (com.bumptech.glide.gifdecoder.GifHeader)3 Nullable (android.support.annotation.Nullable)1 GifHeaderParser (com.bumptech.glide.gifdecoder.GifHeaderParser)1 AnimatedGifEncoder (com.bumptech.glide.gifencoder.AnimatedGifEncoder)1 Options (com.bumptech.glide.load.Options)1 Before (org.junit.Before)1