From 65e550618c0f991aafe7062d33e8fd443faa5cad Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 21 Nov 2017 19:40:54 -0800 Subject: [PATCH] Cache the actual color table index of transparent gif frames. Progress towards #2471. --- .../glide/gifdecoder/StandardGifDecoder.java | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java index 681c7adc58..d0356882bf 100644 --- a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java +++ b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java @@ -504,7 +504,7 @@ private void copyIntoScratchFast(GifFrame currentFrame) { int width = this.downsampledWidth; byte[] mainPixels = this.mainPixels; int[] act = this.act; - @Nullable Boolean isFirstFrameTransparent = this.isFirstFrameTransparent; + byte transparentColorIndex = -1; for (int i = 0; i < downsampledIH; i++) { int line = i + downsampledIY; int k = line * width; @@ -518,34 +518,25 @@ private void copyIntoScratchFast(GifFrame currentFrame) { } // Start of line in source. int sx = i * currentFrame.iw; - int averageColor; - if (isFirstFrameTransparent == null && isFirstFrame) { - while (dx < dlim) { - int currentColorIndex = ((int) mainPixels[sx]) & MASK_INT_LOWEST_BYTE; - averageColor = act[currentColorIndex]; - if (averageColor != COLOR_TRANSPARENT_BLACK) { - dest[dx] = averageColor; - } else if (isFirstFrameTransparent == null) { - isFirstFrameTransparent = true; - } - ++sx; - ++dx; - } - } else { - while (dx < dlim) { - int currentColorIndex = ((int) mainPixels[sx]) & MASK_INT_LOWEST_BYTE; - averageColor = act[currentColorIndex]; - if (averageColor != COLOR_TRANSPARENT_BLACK) { - dest[dx] = averageColor; + + while (dx < dlim) { + byte byteCurrentColorIndex = mainPixels[sx]; + int currentColorIndex = ((int) byteCurrentColorIndex) & MASK_INT_LOWEST_BYTE; + if (currentColorIndex != transparentColorIndex) { + int color = act[currentColorIndex]; + if (color != COLOR_TRANSPARENT_BLACK) { + dest[dx] = color; + } else { + transparentColorIndex = byteCurrentColorIndex; } - ++sx; - ++dx; } + ++sx; + ++dx; } } - this.isFirstFrameTransparent = isFirstFrameTransparent == null - ? false : isFirstFrameTransparent; + isFirstFrameTransparent = + isFirstFrameTransparent == null && isFirstFrame && transparentColorIndex != -1; } private void copyCopyIntoScratchRobust(GifFrame currentFrame) {