|
| 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