Skip to content

8357034: GifImageDecoder can produce wrong transparent pixels #25264

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 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5dbf0a6
Merge pull request #1 from openjdk/master
mickleness May 22, 2022
333c87c
Merge pull request #2 from openjdk/master
mickleness May 24, 2022
57346a4
Merge pull request #3 from openjdk/master
mickleness May 29, 2022
69481fc
Merge pull request #4 from openjdk/master
mickleness Mar 11, 2023
81085d4
Merge pull request #5 from openjdk/master
mickleness Oct 13, 2024
93ef96a
Merge branch 'openjdk:master' into master
mickleness Dec 18, 2024
026c3db
Merge branch 'openjdk:master' into master
mickleness Mar 3, 2025
cc97808
Merge branch 'openjdk:master' into master
mickleness Mar 21, 2025
86b7944
Merge branch 'master' of https://github.com/mickleness/jdk
mickleness Apr 4, 2025
b9f767b
Merge branch 'master' of https://github.com/mickleness/jdk
mickleness Apr 20, 2025
15cef1c
Merge branch 'master' of https://github.com/mickleness/jdk
mickleness Apr 27, 2025
bf967a1
Merge branch 'master' of https://github.com/mickleness/jdk
mickleness May 15, 2025
89a54af
GifComparison: adding common helper class for gif bugs
mickleness May 14, 2025
865f094
GifComparison: code cleanup
mickleness May 14, 2025
05fc705
Incident 9078482: fix when new trans index is in saved images
mickleness May 14, 2025
2d6b612
GifComparison: fixing error message
mickleness May 14, 2025
5b982b1
8357034: fixing ticket number
mickleness May 16, 2025
80944a2
Merge branch 'master' of https://github.com/mickleness/jdk
mickleness May 26, 2025
b16c43d
Merge branch 'master' into JDK-8357034
mickleness May 26, 2025
3d0cd7e
8356320: trivial whitespace and comment changes
mickleness May 26, 2025
b1a1d7e
8357034: fixing classname
mickleness May 26, 2025
3d94c43
8356137: Adding copyright to GifComparison
mickleness May 26, 2025
4139334
8356137: only inspect last frame of gif
mickleness May 27, 2025
120a8f4
8356137: trivial javadoc update
mickleness May 27, 2025
e8a01c8
8356137: Adding GifBuilder to dynamically create test file
mickleness Jun 1, 2025
b75cda2
8356137: adding copyright
mickleness Jun 1, 2025
3750e58
8356320: Use new GifBuilder and remove ukraine-flag.gif
mickleness Jun 1, 2025
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
Expand Up @@ -395,7 +395,16 @@ private int sendPixels(int x, int y, int width, int height,
int off = y * global_width + x2;
boolean save = (curframe.disposal_method == GifFrame.DISPOSAL_SAVE);
if (trans_pixel >= 0 && !curframe.initialframe) {
if (saved_image != null && model.equals(saved_model)) {
boolean isSimple = saved_image != null && model.equals(saved_model);
if (isSimple) {
for (int a = 0; a < saved_image.length; a++) {
if ((saved_image[a] & 0xff) == trans_pixel) {
isSimple = false;
break;
}
}
}
if (isSimple) {
for (int i = rasbeg; i < rasend; i++, off++) {
byte pixel = rasline[i];
if ((pixel & 0xff) == trans_pixel) {
Expand Down
152 changes: 152 additions & 0 deletions test/jdk/sun/awt/image/gif/GifBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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.
*/

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
* This constructs sample gif files used to test different combinations
* of gif frame disposal methods and transparent pixel indices.
*/
public class GifBuilder {

/**
* Different disposal methods for gif frames. These names exactly
* correspond to the String identifier ImageIO uses.
*/
public enum Disposal {none, doNotDispose, restoreToBackgroundColor, restoreToPrevious};


/**
* @param disposal the frame disposal method
* @param isFirstTableIndexTransparent if true then the transparent pixel
* is set to 0. If false then the
* transparent pixel is set to the
* last index.
*/
public record FrameDescription(Disposal disposal, boolean isFirstTableIndexTransparent) {}

/**
* This creates a sample gif image based on a series of FrameDescriptions, and the
* calls {@link GifComparison#run(URL)}
*/
public static void test(FrameDescription... frameDescriptions) throws Throwable {
File file = createTestFile(frameDescriptions);
try {
GifComparison.run(file.toURI().toURL());
} finally {
file.delete();
}
}

private static File createTestFile(FrameDescription... frameDescriptions) throws IOException {
Color[] colors = new Color[] {
Color.red,
Color.yellow,
Color.green,
Color.cyan
};
File file = File.createTempFile("GifBuilder", ".gif");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
ImageWriter gifWriter = ImageIO.getImageWritersByFormatName("GIF").next();
gifWriter.setOutput(ios);
ImageWriteParam wparam = gifWriter.getDefaultWriteParam();
IIOMetadata streamMetadata = gifWriter.getDefaultStreamMetadata(wparam);
gifWriter.prepareWriteSequence(streamMetadata);

IndexColorModel icm = createIndexColorModel(colors, colors.length - 1);

ImageTypeSpecifier s = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_INDEXED);
IIOMetadata metadata = gifWriter.getDefaultImageMetadata(s, wparam);
String metaFormatName = metadata.getNativeMetadataFormatName();

for (FrameDescription frameDescription : frameDescriptions) {

// prepare the image:
int width = 100 + 50 * (icm.getMapSize() - 2);
BufferedImage bi = new BufferedImage(width, 100,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
g.setComposite(AlphaComposite.SrcOver);
int x = 0;
for (int a = 0; a < icm.getMapSize() - 1; a++) {
if (a != icm.getTransparentPixel()) {
Color color = new Color(icm.getRGB(a));
g.setColor(color);
g.fillOval(x, 0, 100, 100);
x += 50;
}
}
g.dispose();

// wrap attributes for gifWriter:
int transparentPixel = frameDescription.isFirstTableIndexTransparent ? 0 : icm.getMapSize() - 1;
IIOMetadata frameMetadata = gifWriter.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(bi), wparam);
IIOMetadataNode root = new IIOMetadataNode(metaFormatName);
IIOMetadataNode gce = new IIOMetadataNode("GraphicControlExtension");
gce.setAttribute("disposalMethod", frameDescription.disposal.name());
gce.setAttribute("userInputFlag", "FALSE");
gce.setAttribute("transparentColorFlag", "TRUE");
gce.setAttribute("delayTime", "0");
gce.setAttribute("transparentColorIndex", Integer.toString(transparentPixel));
root.appendChild(gce);
frameMetadata.mergeTree(metaFormatName, root);
IIOImage img = new IIOImage(bi, null, frameMetadata);
gifWriter.writeToSequence(img, wparam);
}
gifWriter.endWriteSequence();
ios.flush();
ios.close();

return file;
}

private static IndexColorModel createIndexColorModel(Color[] colors, int transparentIndex) {
byte[] r = new byte[colors.length];
byte[] g = new byte[colors.length];
byte[] b = new byte[colors.length];
for (int a = 0; a < colors.length; a++) {
r[a] = (byte) colors[a].getRed();
g[a] = (byte) colors[a].getGreen();
b[a] = (byte) colors[a].getBlue();
}
int bits = (int)(Math.log(colors.length) / Math.log(2) + .5);
return new IndexColorModel(bits, colors.length, r, g, b, transparentIndex);
}
}
Loading