Skip to content

Commit

Permalink
Return null when gif frame cannot be decoded.
Browse files Browse the repository at this point in the history
More work toward bumptech#212.
  • Loading branch information
sjudd committed Oct 23, 2014
1 parent f4e8430 commit f0c8a95
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;

Expand All @@ -38,4 +39,11 @@ public void testReturnsFrameFromGifDecoder() throws IOException {

assertEquals(expected, resourceDecoder.decode(gifDecoder, 100, 100).get());
}

@Test
public void testReturnsNullIfGifDecoderReturnsNullFrame() {
when(gifDecoder.getNextFrame()).thenReturn(null);

assertNull(resourceDecoder.decode(gifDecoder, 100, 100));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public void testReturnsParserToPoolWhenParserThrows() {
verify(parserPool).release(eq(parser));
}


@Test
public void testSetsPreferredConfigOnDecoderBeforeDecoding() {
when(gifHeader.getNumFrames()).thenReturn(1);
Expand Down Expand Up @@ -148,6 +147,26 @@ public void testReturnsGifDecoderToPoolWhenDecoderThrows() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public GifFrameResourceDecoder(BitmapPool bitmapPool) {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ private GifDrawableResource decode(byte[] data, int width, int height, GifHeader
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,15 @@ public int getLoopCount() {
*/
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;
Expand All @@ -276,7 +282,9 @@ public Bitmap getNextFrame() {
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;
Expand Down

0 comments on commit f0c8a95

Please sign in to comment.