121c2822985058df655ee4874798297f74c5e367eSam Juddpackage com.bumptech.glide.load.resource.gif;
221c2822985058df655ee4874798297f74c5e367eSam Judd
321c2822985058df655ee4874798297f74c5e367eSam Juddimport android.graphics.Bitmap;
4f7a6d65cf7c1a41908dd48e0dab68ee5b881387eSam Judd
521c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.Transformation;
621c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.engine.Resource;
721c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
821c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.resource.bitmap.BitmapResource;
921c2822985058df655ee4874798297f74c5e367eSam Judd
1021c2822985058df655ee4874798297f74c5e367eSam Judd/**
1121c2822985058df655ee4874798297f74c5e367eSam Judd * An {@link com.bumptech.glide.load.Transformation} that wraps a transformation for a {@link Bitmap}
1221c2822985058df655ee4874798297f74c5e367eSam Judd * and can apply it to every frame of any {@link com.bumptech.glide.load.resource.gif.GifDrawable}.
1321c2822985058df655ee4874798297f74c5e367eSam Judd */
1421c2822985058df655ee4874798297f74c5e367eSam Juddpublic class GifDrawableTransformation implements Transformation<GifDrawable> {
155ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd    private final Transformation<Bitmap> wrapped;
165ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd    private final BitmapPool bitmapPool;
1721c2822985058df655ee4874798297f74c5e367eSam Judd
1821c2822985058df655ee4874798297f74c5e367eSam Judd    public GifDrawableTransformation(Transformation<Bitmap> wrapped, BitmapPool bitmapPool) {
1921c2822985058df655ee4874798297f74c5e367eSam Judd        this.wrapped = wrapped;
2021c2822985058df655ee4874798297f74c5e367eSam Judd        this.bitmapPool = bitmapPool;
2121c2822985058df655ee4874798297f74c5e367eSam Judd    }
2221c2822985058df655ee4874798297f74c5e367eSam Judd
2321c2822985058df655ee4874798297f74c5e367eSam Judd    @Override
2421c2822985058df655ee4874798297f74c5e367eSam Judd    public Resource<GifDrawable> transform(Resource<GifDrawable> resource, int outWidth, int outHeight) {
2521c2822985058df655ee4874798297f74c5e367eSam Judd        GifDrawable drawable = resource.get();
2621c2822985058df655ee4874798297f74c5e367eSam Judd
2721c2822985058df655ee4874798297f74c5e367eSam Judd        // The drawable needs to be initialized with the correct width and height in order for a view displaying it
2821c2822985058df655ee4874798297f74c5e367eSam Judd        // to end up with the right dimensions. Since our transformations may arbitrarily modify the dimensions of
2921c2822985058df655ee4874798297f74c5e367eSam Judd        // our gif, here we create a stand in for a frame and pass it to the transformation to see what the final
304185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        // transformed dimensions will be so that our drawable can report the correct intrinsic width and height.
314185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Bitmap firstFrame = resource.get().getFirstFrame();
324185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool);
334185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Resource<Bitmap> transformed = wrapped.transform(bitmapResource, outWidth, outHeight);
345ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd        if (!bitmapResource.equals(transformed)) {
3521c2822985058df655ee4874798297f74c5e367eSam Judd            bitmapResource.recycle();
3621c2822985058df655ee4874798297f74c5e367eSam Judd        }
374185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Bitmap transformedFrame = transformed.get();
3821c2822985058df655ee4874798297f74c5e367eSam Judd
394185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        drawable.setFrameTransformation(wrapped, transformedFrame);
4021c2822985058df655ee4874798297f74c5e367eSam Judd        return resource;
4121c2822985058df655ee4874798297f74c5e367eSam Judd    }
4221c2822985058df655ee4874798297f74c5e367eSam Judd
4321c2822985058df655ee4874798297f74c5e367eSam Judd    @Override
4421c2822985058df655ee4874798297f74c5e367eSam Judd    public String getId() {
4521c2822985058df655ee4874798297f74c5e367eSam Judd        return wrapped.getId();
4621c2822985058df655ee4874798297f74c5e367eSam Judd    }
4721c2822985058df655ee4874798297f74c5e367eSam Judd}
48