Skip to content

Commit

Permalink
Reset frame pointer in setData in GifDecoder.
Browse files Browse the repository at this point in the history
Fixes #204.
  • Loading branch information
sjudd committed Oct 19, 2014
1 parent 159f8c3 commit 29fa7dd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,55 @@ public void testCanDecodeFramesFromTestGif() {
assertEquals(GifDecoder.STATUS_OK, decoder.getStatus());
}

@Test
public void testFrameIndexStartsAtNegativeOne() {
GifHeader gifheader = new GifHeader();
gifheader.frameCount = 4;
byte[] data = new byte[0];
GifDecoder decoder = new GifDecoder(provider);
decoder.setData(gifheader, data);
assertEquals(-1, decoder.getCurrentFrameIndex());
}

@Test
public void testAdvanceIncrementsFrameIndex() {
GifHeader gifheader = new GifHeader();
gifheader.frameCount = 4;
byte[] data = new byte[0];
GifDecoder decoder = new GifDecoder(provider);
decoder.setData(gifheader, data);
decoder.advance();
assertEquals(0, decoder.getCurrentFrameIndex());
}

@Test
public void testAdvanceWrapsIndexBackToZero() {
GifHeader gifheader = new GifHeader();
gifheader.frameCount = 2;
byte[] data = new byte[0];
GifDecoder decoder = new GifDecoder(provider);
decoder.setData(gifheader, data);
decoder.advance();
decoder.advance();
decoder.advance();
assertEquals(0, decoder.getCurrentFrameIndex());
}

@Test
public void testSettingDataResetsFramePointer() {
GifHeader gifheader = new GifHeader();
gifheader.frameCount = 4;
byte[] data = new byte[0];
GifDecoder decoder = new GifDecoder(provider);
decoder.setData(gifheader, data);
decoder.advance();
decoder.advance();
assertEquals(1, decoder.getCurrentFrameIndex());

decoder.setData(gifheader, data);
assertEquals(-1, decoder.getCurrentFrameIndex());
}

private static class MockProvider implements GifDecoder.BitmapProvider {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class GifDecoder {

private static final int NULL_CODE = -1;

private static final int INITIAL_FRAME_POINTER = -1;

// Global File Header values and parsing flags.
// Active color table.
private int[] act;
Expand All @@ -104,24 +106,26 @@ public class GifDecoder {
private ByteBuffer rawData;

// Raw data read working array.
private byte[] block = new byte[256];
private final byte[] block = new byte[256];

private GifHeaderParser parser;

// LZW decoder working arrays.
private short[] prefix;
private byte[] suffix;
private byte[] pixelStack;
private byte[] mainPixels;
private int[] mainScratch;

private int framePointer = -1;
private int framePointer;
private byte[] data;
private GifHeader header;
private String id;
private BitmapProvider bitmapProvider;
private GifHeaderParser parser = new GifHeaderParser();
private Bitmap previousImage;
private boolean savePrevious;
private Bitmap.Config config;
private int status = STATUS_OK;
private int status;

/**
* An interface that can be used to provide reused {@link android.graphics.Bitmap}s to avoid GCs from constantly
Expand Down Expand Up @@ -339,6 +343,7 @@ public void setData(String id, GifHeader header, byte[] data) {
this.header = header;
this.data = data;
this.status = STATUS_OK;
framePointer = INITIAL_FRAME_POINTER;
// Initialize the raw data buffer.
rawData = ByteBuffer.wrap(data);
rawData.rewind();
Expand All @@ -359,6 +364,13 @@ public void setData(String id, GifHeader header, byte[] data) {
mainScratch = new int[header.width * header.height];
}

private GifHeaderParser getHeaderParser() {
if (parser == null) {
parser = new GifHeaderParser();
}
return parser;
}

/**
* Reads GIF image from byte array.
*
Expand All @@ -367,7 +379,7 @@ public void setData(String id, GifHeader header, byte[] data) {
*/
public int read(byte[] data) {
this.data = data;
this.header = parser.setData(data).parseHeader();
this.header = getHeaderParser().setData(data).parseHeader();
if (data != null) {
// Initialize the raw data buffer.
rawData = ByteBuffer.wrap(data);
Expand Down

0 comments on commit 29fa7dd

Please sign in to comment.