Skip to content

Commit a4f3064

Browse files
author
duke
committedMar 13, 2025
Automatic merge of jdk:master into master
2 parents 87169c5 + c18494d commit a4f3064

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed
 

‎src/java.desktop/share/classes/javax/imageio/ImageTypeSpecifier.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -919,7 +919,8 @@ ImageTypeSpecifier createFromRenderedImage(RenderedImage image) {
919919

920920
if (image instanceof BufferedImage) {
921921
int bufferedImageType = ((BufferedImage)image).getType();
922-
if (bufferedImageType != BufferedImage.TYPE_CUSTOM) {
922+
if (bufferedImageType != BufferedImage.TYPE_CUSTOM
923+
&& bufferedImageType != BufferedImage.TYPE_BYTE_INDEXED) {
923924
return getSpecifier(bufferedImageType);
924925
}
925926
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8351108
27+
* @summary This test verifies that attempting to write a JPEG using
28+
* ImageIO.write(..) fails by returning `false` for
29+
* translucent IndexColorModels.
30+
*/
31+
32+
import javax.imageio.ImageIO;
33+
import java.awt.Transparency;
34+
import java.awt.image.BufferedImage;
35+
import java.awt.image.IndexColorModel;
36+
import java.io.ByteArrayOutputStream;
37+
38+
public class JpegWriterWriteNonOpaqueIndexColorModelTest {
39+
public static void main(String[] args) {
40+
boolean b1 = testJpegWriter(Transparency.OPAQUE, "OPAQUE", true);
41+
boolean b2 = testJpegWriter(Transparency.BITMASK, "BITMASK", false);
42+
boolean b3 = testJpegWriter(Transparency.TRANSLUCENT, "TRANSLUCENT", false);
43+
if (!(b1 && b2 && b3)) {
44+
throw new Error("Test failed");
45+
}
46+
}
47+
48+
private static boolean testJpegWriter(int imageType, String name, boolean expectedWriteReturnValue) {
49+
System.out.println();
50+
System.out.println("TESTING " + name);
51+
try {
52+
byte[] gray = new byte[256];
53+
for (int a = 0; a < gray.length; a++) {
54+
gray[a] = (byte) a;
55+
}
56+
57+
byte[] alpha = new byte[256];
58+
if (imageType == Transparency.OPAQUE || imageType == Transparency.BITMASK) {
59+
for (int a = 0; a < alpha.length; a++) {
60+
alpha[a] = -1;
61+
}
62+
if (imageType == Transparency.BITMASK) {
63+
alpha[0] = 0;
64+
}
65+
} else if (imageType == Transparency.TRANSLUCENT) {
66+
for (int a = 0; a < alpha.length; a++) {
67+
alpha[a] = (byte) a;
68+
}
69+
}
70+
71+
IndexColorModel indexColorModel = new IndexColorModel(8, 256, gray, gray, gray, alpha);
72+
System.out.println("colorModel.getTransparency() = " + indexColorModel.getTransparency());
73+
BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_INDEXED, indexColorModel);
74+
boolean result = ImageIO.write(bi, "jpg", new ByteArrayOutputStream());
75+
if (result != expectedWriteReturnValue) {
76+
throw new Exception("ImageIO.write(..) returned " + result + " but we expected " + expectedWriteReturnValue);
77+
}
78+
System.out.println("Tested passed");
79+
return true;
80+
} catch (Exception e) {
81+
System.err.println(name + " test failed");
82+
e.printStackTrace();
83+
return false;
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)
Please sign in to comment.