From 8f80ee831ff7b42b72be5f8728a9486fe64c2fa6 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Sun, 12 Oct 2014 18:30:27 -0700 Subject: [PATCH] Skip AnimatedGifEncoder for original GIFs. Fixes #184. --- .../resource/gif/GifResourceEncoderTest.java | 20 ++++++++++++++++++ .../load/resource/gif/GifResourceEncoder.java | 21 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/library/src/androidTest/java/com/bumptech/glide/load/resource/gif/GifResourceEncoderTest.java b/library/src/androidTest/java/com/bumptech/glide/load/resource/gif/GifResourceEncoderTest.java index 9593d2b02..2d75d2cbb 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/resource/gif/GifResourceEncoderTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/resource/gif/GifResourceEncoderTest.java @@ -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.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()); diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceEncoder.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceEncoder.java index edccbecac..ab2e14c6d 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceEncoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceEncoder.java @@ -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 { long startTime = LogTime.getLogTime(); GifDrawable drawable = resource.get(); + Transformation 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 { return false; } - Transformation transformation = drawable.getFrameTransformation(); for (int i = 0; i < decoder.getFrameCount(); i++) { Bitmap currentFrame = decoder.getNextFrame(); Resource transformedResource = getTransformedFrame(currentFrame, transformation, drawable); @@ -77,6 +83,19 @@ public class GifResourceEncoder implements ResourceEncoder { 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); -- GitLab