Skip to content

Commit

Permalink
Pass Options into ResourceTranscoders.
Browse files Browse the repository at this point in the history
Fixes #2417.
  • Loading branch information
sjudd committed Sep 21, 2017
1 parent 0afc0e6 commit 134870e
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Resource<Transcode> decode(DataRewinder<DataType> rewinder, int width, in
Options options, DecodeCallback<ResourceType> callback) throws GlideException {
Resource<ResourceType> decoded = decodeResource(rewinder, width, height, options);
Resource<ResourceType> transformed = callback.onResourceDecoded(decoded);
return transcoder.transcode(transformed);
return transcoder.transcode(transformed, options);
}

private Resource<ResourceType> decodeResource(DataRewinder<DataType> rewinder, int width,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.resource.transcode;

import android.graphics.Bitmap;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.bytes.BytesResource;
import java.io.ByteArrayOutputStream;
Expand All @@ -25,7 +26,7 @@ public BitmapBytesTranscoder(Bitmap.CompressFormat compressFormat, int quality)
}

@Override
public Resource<byte[]> transcode(Resource<Bitmap> toTranscode) {
public Resource<byte[]> transcode(Resource<Bitmap> toTranscode, Options options) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
toTranscode.get().compress(compressFormat, quality, os);
toTranscode.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.LazyBitmapDrawableResource;
Expand All @@ -28,7 +29,7 @@ public BitmapDrawableTranscoder(Resources resources, BitmapPool bitmapPool) {
}

@Override
public Resource<BitmapDrawable> transcode(Resource<Bitmap> toTranscode) {
public Resource<BitmapDrawable> transcode(Resource<Bitmap> toTranscode, Options options) {
return LazyBitmapDrawableResource.obtain(resources, bitmapPool, toTranscode.get());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.resource.transcode;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.bytes.BytesResource;
import com.bumptech.glide.load.resource.gif.GifDrawable;
Expand All @@ -13,7 +14,7 @@
*/
public class GifDrawableBytesTranscoder implements ResourceTranscoder<GifDrawable, byte[]> {
@Override
public Resource<byte[]> transcode(Resource<GifDrawable> toTranscode) {
public Resource<byte[]> transcode(Resource<GifDrawable> toTranscode, Options options) {
GifDrawable gifData = toTranscode.get();
ByteBuffer byteBuffer = gifData.getBuffer();
return new BytesResource(ByteBufferUtil.toBytes(byteBuffer));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.resource.transcode;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;

/**
Expand All @@ -15,5 +16,5 @@ public interface ResourceTranscoder<Z, R> {
*
* @param toTranscode The resource to transcode.
*/
Resource<R> transcode(Resource<Z> toTranscode);
Resource<R> transcode(Resource<Z> toTranscode, Options options);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.resource.transcode;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;

/**
Expand All @@ -16,7 +17,7 @@ public static <Z> ResourceTranscoder<Z, Z> get() {
}

@Override
public Resource<Z> transcode(Resource<Z> toTranscode) {
public Resource<Z> transcode(Resource<Z> toTranscode, Options options) {
return toTranscode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.Mockito.when;

import android.graphics.Bitmap;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -59,6 +60,7 @@ private static class BitmapBytesTranscoderHarness {
final String description = "TestDescription";
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
Resource<Bitmap> bitmapResource = mockResource();
Options options = new Options();

public BitmapBytesTranscoderHarness() {
when(bitmapResource.get()).thenReturn(bitmap);
Expand All @@ -67,7 +69,7 @@ public BitmapBytesTranscoderHarness() {

public String getTranscodedDescription() {
BitmapBytesTranscoder transcoder = new BitmapBytesTranscoder(compressFormat, quality);
Resource<byte[]> bytesResource = transcoder.transcode(bitmapResource);
Resource<byte[]> bytesResource = transcoder.transcode(bitmapResource, options);

return new String(bytesResource.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import org.junit.Before;
Expand All @@ -33,7 +34,7 @@ public void testReturnsBitmapDrawableResourceContainingGivenBitmap() {
Resource<Bitmap> resource = mockResource();
when(resource.get()).thenReturn(expected);

Resource<BitmapDrawable> transcoded = transcoder.transcode(resource);
Resource<BitmapDrawable> transcoded = transcoder.transcode(resource, new Options());

assertEquals(expected, transcoded.get().getBitmap());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void testReturnsBytesOfGivenGifDrawable() {
ByteBuffer expected = ByteBuffer.wrap(fakeData.getBytes());
when(gifDrawable.getBuffer()).thenReturn(expected);

Resource<byte[]> transcoded = transcoder.transcode(resource);
Resource<byte[]> transcoded = transcoder.transcode(resource, new Options());

assertArrayEquals(expected.array(), transcoded.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.bumptech.glide.tests.Util.mockResource;
import static org.junit.Assert.assertEquals;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -16,6 +17,6 @@ public void testReturnsTheGivenResource() {
Resource<Object> resource = mockResource();
ResourceTranscoder<Object, Object> unitTranscoder = UnitTranscoder.get();

assertEquals(resource, unitTranscoder.transcode(resource));
assertEquals(resource, unitTranscoder.transcode(resource, new Options()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.graphics.Picture;
import android.graphics.drawable.PictureDrawable;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.SimpleResource;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
Expand All @@ -14,7 +14,7 @@
*/
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode, Options options) {
SVG svg = toTranscode.get();
Picture picture = svg.renderToPicture();
PictureDrawable drawable = new PictureDrawable(picture);
Expand Down

0 comments on commit 134870e

Please sign in to comment.