提交 f0c8a95c 编写于 作者: S Sam Judd

Return null when gif frame cannot be decoded.

More work toward #212.
上级 f4e84300
......@@ -12,6 +12,7 @@ import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -38,4 +39,11 @@ public class GifFrameResourceDecoderTest {
assertEquals(expected, resourceDecoder.decode(gifDecoder, 100, 100).get());
}
@Test
public void testReturnsNullIfGifDecoderReturnsNullFrame() {
when(gifDecoder.getNextFrame()).thenReturn(null);
assertNull(resourceDecoder.decode(gifDecoder, 100, 100));
}
}
......@@ -99,7 +99,6 @@ public class GifResourceDecoderTest {
verify(parserPool).release(eq(parser));
}
@Test
public void testSetsPreferredConfigOnDecoderBeforeDecoding() {
when(gifHeader.getNumFrames()).thenReturn(1);
......@@ -148,6 +147,26 @@ public class GifResourceDecoderTest {
verify(decoderPool).release(eq(gifDecoder));
}
@Test
public void testReturnsNullIfGifDecoderFailsToDecodeFirstFrame() {
when(gifHeader.getNumFrames()).thenReturn(1);
when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_OK);
when(gifDecoder.getNextFrame()).thenReturn(null);
assertNull(decoder.decode(new ByteArrayInputStream(new byte[0]), 100, 100));
}
@Test
public void testReturnsGifDecoderToPoolWhenGifDecoderReturnsNullFirstFrame() {
when(gifHeader.getNumFrames()).thenReturn(1);
when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_OK);
when(gifDecoder.getNextFrame()).thenReturn(null);
decoder.decode(new ByteArrayInputStream(new byte[0]), 100, 100);
verify(decoderPool).release(eq(gifDecoder));
}
@Test
public void testCanObtainNonNullDecoderFromPool() {
GifDecoder.BitmapProvider provider = mock(GifDecoder.BitmapProvider.class);
......
......@@ -17,7 +17,11 @@ class GifFrameResourceDecoder implements ResourceDecoder<GifDecoder, Bitmap> {
@Override
public Resource<Bitmap> decode(GifDecoder source, int width, int height) {
Bitmap bitmap = source.getNextFrame();
return new BitmapResource(bitmap, bitmapPool);
if (bitmap == null) {
return null;
} else {
return new BitmapResource(bitmap, bitmapPool);
}
}
@Override
......
......@@ -85,6 +85,10 @@ public class GifResourceDecoder implements ResourceDecoder<InputStream, GifDrawa
}
Bitmap firstFrame = decodeFirstFrame(decoder, header, data);
if (firstFrame == null) {
return null;
}
Transformation<Bitmap> unitTransformation = UnitTransformation.get();
GifDrawable gifDrawable = new GifDrawable(context, provider, bitmapPool, unitTransformation, width, height,
......
......@@ -250,9 +250,15 @@ public class GifDecoder {
*/
public Bitmap getNextFrame() {
if (header.frameCount <= 0 || framePointer < 0) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "unable to decode frame, frameCount=" + header.frameCount + " framePointer=" + framePointer);
}
status = STATUS_FORMAT_ERROR;
}
if (status == STATUS_FORMAT_ERROR || status == STATUS_OPEN_ERROR) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unable to decode frame, status=" + status);
}
return null;
}
status = STATUS_OK;
......@@ -276,7 +282,9 @@ public class GifDecoder {
act[frame.transIndex] = 0;
}
if (act == null) {
Log.w(TAG, "No Valid Color Table");
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "No Valid Color Table");
}
// No color table defined.
status = STATUS_FORMAT_ERROR;
return null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册