Skip to content

Commit cb640b2

Browse files
committed
Merge pull request #620 from kojilin/master
Let gif's first frame using last frame's dispose
2 parents f10b029 + c2ac07b commit cb640b2

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,12 @@ public synchronized Bitmap getNextFrame() {
318318
status = STATUS_OK;
319319

320320
GifFrame currentFrame = header.frames.get(framePointer);
321-
GifFrame previousFrame = null;
321+
GifFrame previousFrame;
322322
int previousIndex = framePointer - 1;
323323
if (previousIndex >= 0) {
324324
previousFrame = header.frames.get(previousIndex);
325+
} else {
326+
previousFrame = header.frames.get(getFrameCount() - 1);
325327
}
326328

327329
final int savedBgColor = header.bgColor;

third_party/gif_decoder/src/test/java/com/bumptech/glide/gifdecoder/GifDecoderTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.robolectric.Shadows.shadowOf;
57

68
import android.graphics.Bitmap;
79
import android.support.annotation.NonNull;
@@ -14,8 +16,12 @@
1416
import org.robolectric.RobolectricTestRunner;
1517
import org.robolectric.Shadows;
1618
import org.robolectric.annotation.Config;
19+
import org.robolectric.annotation.Implementation;
20+
import org.robolectric.annotation.Implements;
21+
import org.robolectric.shadows.ShadowBitmap;
1722

1823
import java.io.IOException;
24+
import java.util.Arrays;
1925

2026
/**
2127
* Tests for {@link com.bumptech.glide.gifdecoder.GifDecoder}.
@@ -94,6 +100,45 @@ public void testSettingDataResetsFramePointer() {
94100
assertEquals(-1, decoder.getCurrentFrameIndex());
95101
}
96102

103+
@Test
104+
@Config(shadows = { CustomShadowBitmap.class })
105+
public void testFirstFrameMustUsingLastFrameDispose() throws IOException {
106+
byte[] data = TestUtil.resourceToBytes(getClass(), "transparent_dispose.gif");
107+
GifHeaderParser headerParser = new GifHeaderParser();
108+
headerParser.setData(data);
109+
GifHeader header = headerParser.parseHeader();
110+
GifDecoder decoder = new GifDecoder(provider);
111+
decoder.setData(header, data);
112+
decoder.advance();
113+
Bitmap firstFrame = decoder.getNextFrame();
114+
decoder.advance();
115+
decoder.getNextFrame();
116+
decoder.advance();
117+
Bitmap firstFrameTwice = decoder.getNextFrame();
118+
assertTrue(Arrays.equals((((CustomShadowBitmap) shadowOf(firstFrame))).getPixels(),
119+
(((CustomShadowBitmap) shadowOf(firstFrameTwice))).getPixels()));
120+
}
121+
122+
/**
123+
* Preserve generated bitmap data for checking.
124+
*/
125+
@Implements(Bitmap.class)
126+
public static class CustomShadowBitmap extends ShadowBitmap {
127+
128+
private int[] pixels;
129+
130+
@Implementation
131+
public void setPixels(int[] pixels, int offset, int stride,
132+
int x, int y, int width, int height) {
133+
this.pixels = new int[pixels.length];
134+
System.arraycopy(pixels, 0, this.pixels, 0, this.pixels.length);
135+
}
136+
137+
public int[] getPixels() {
138+
return pixels;
139+
}
140+
}
141+
97142
private static class MockProvider implements GifDecoder.BitmapProvider {
98143

99144
@NonNull
1.12 KB
Loading

0 commit comments

Comments
 (0)