提交 8f80ee83 编写于 作者: S Sam Judd

Skip AnimatedGifEncoder for original GIFs.

Fixes #184.
上级 3c1ebe27
......@@ -8,12 +8,14 @@ import com.bumptech.glide.gifencoder.AnimatedGifEncoder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.UnitTransformation;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
import java.io.OutputStream;
import static org.junit.Assert.assertEquals;
......@@ -24,6 +26,7 @@ import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -236,6 +239,23 @@ public class GifResourceEncoderTest {
order.verify(frameResource).recycle();
}
@Test
public void testWritesBytesDirectlyToDiskIfTransformationIsUnitTransformation() throws IOException {
when(gifDrawable.getFrameTransformation()).thenReturn(UnitTransformation.<Bitmap>get());
byte[] expected = "expected".getBytes();
when(gifDrawable.getData()).thenReturn(expected);
OutputStream os = mock(OutputStream.class);
encoder.encode(resource, os);
verify(os).write(eq(expected));
verify(gifEncoder, never()).start(any(OutputStream.class));
verify(parser, never()).setData(any(byte[].class));
verify(parser, never()).parseHeader();
}
@Test
public void testHasValidId() {
assertEquals("", encoder.getId());
......
......@@ -10,9 +10,11 @@ import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.UnitTransformation;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.util.LogTime;
import java.io.IOException;
import java.io.OutputStream;
/**
......@@ -42,6 +44,11 @@ public class GifResourceEncoder implements ResourceEncoder<GifDrawable> {
long startTime = LogTime.getLogTime();
GifDrawable drawable = resource.get();
Transformation<Bitmap> transformation = drawable.getFrameTransformation();
if (transformation instanceof UnitTransformation) {
return writeDataDirect(drawable.getData(), os);
}
GifDecoder decoder = decodeHeaders(drawable.getData());
AnimatedGifEncoder encoder = factory.buildEncoder();
......@@ -49,7 +56,6 @@ public class GifResourceEncoder implements ResourceEncoder<GifDrawable> {
return false;
}
Transformation<Bitmap> transformation = drawable.getFrameTransformation();
for (int i = 0; i < decoder.getFrameCount(); i++) {
Bitmap currentFrame = decoder.getNextFrame();
Resource<Bitmap> transformedResource = getTransformedFrame(currentFrame, transformation, drawable);
......@@ -77,6 +83,19 @@ public class GifResourceEncoder implements ResourceEncoder<GifDrawable> {
return result;
}
private boolean writeDataDirect(byte[] data, OutputStream os) {
boolean success = true;
try {
os.write(data);
} catch (IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Failed to write data to output stream in GifResourceEncoder", e);
}
success = false;
}
return success;
}
private GifDecoder decodeHeaders(byte[] data) {
GifHeaderParser parser = factory.buildParser();
parser.setData(data);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册