Skip to content

8356320: GifImageDecoder can produce wrong image when GCE changes transparent pixel index #25076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -730,6 +730,7 @@ public boolean dispose() {
// clear saved_image using transparent pixels
// this will be used as the background in the next display
if( decoder.saved_image != null ) {
tpix = (byte) decoder.saved_model.getTransparentPixel();
for( int i = 0; i < global_width * global_height; i ++ )
decoder.saved_image[i] = tpix;
}
Expand Down
139 changes: 139 additions & 0 deletions test/jdk/sun/awt/image/gif/bug8356320/GifErasureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8356320
* @summary This test verifies that we enforce the transparent background
* when the disposal code is DISPOSAL_BGCOLOR and the transparent pixel
* index changes.
*/

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.IndexColorModel;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.concurrent.Semaphore;

public class GifErasureTest {
public static void main(String[] args) throws Exception {
URL srcURL = GifErasureTest.class.getResource("leo.gif");
BufferedImage[] frames = getFrames(srcURL, 3);

if (new Color(frames[2].getRGB(20, 20), true).getAlpha() != 0) {
throw new Error("The pixel at (20, 20) should be transparent.");
}
}

private static BufferedImage[] getFrames(URL gifURL, int numberOfFrames) {
Image image = Toolkit.getDefaultToolkit().createImage(gifURL);
ArrayList<BufferedImage> returnValue = new ArrayList<>(numberOfFrames);

Semaphore semaphore = new Semaphore(1);
semaphore.acquireUninterruptibly();
image.getSource().startProduction(new ImageConsumer() {
BufferedImage bi;
int frameCtr = 0;

@Override
public void setDimensions(int width, int height) {
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

@Override
public void setProperties(Hashtable<?, ?> props) {}

@Override
public void setColorModel(ColorModel model) {}

@Override
public void setHints(int hintflags) {}

@Override
public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
try {
final int yMax = y + h;
final int xMax = x + w;

IndexColorModel icm = (IndexColorModel) model;
int[] colorModelRGBs = new int[icm.getMapSize()];
icm.getRGBs(colorModelRGBs);
int[] argbRow = new int[bi.getWidth()];

for (int y_ = y; y_ < yMax; y_++) {
int i = y_ * scansize + off;
for (int x_ = x; x_ < xMax; x_++, i++) {
int pixel = pixels[i] & 0xff;
argbRow[x_ - x] = colorModelRGBs[pixel];
}
bi.getRaster().setDataElements(x, y_, w, 1, argbRow);
}
} catch (RuntimeException e) {
// we don't expect this to happen, but if something goes
// wrong nobody else will print our stacktrace for us:
e.printStackTrace();
throw e;
}
}

@Override
public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {}

@Override
public void imageComplete(int status) {
try {
frameCtr++;

BufferedImage copy = new BufferedImage(bi.getWidth(),
bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = copy.createGraphics();
g.drawImage(bi, 0, 0, null);
g.dispose();
returnValue.add(copy);

if (frameCtr == numberOfFrames) {
semaphore.release();
// if we don't detach this consumer the producer will
// loop forever
image.getSource().removeConsumer(this);
image.flush();
}
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
});

semaphore.acquireUninterruptibly();

return returnValue.toArray(new BufferedImage[0]);
}
}
Binary file added test/jdk/sun/awt/image/gif/bug8356320/leo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.