|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2024, 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 4243044 |
| 27 | + * @summary This test should show two windows filled with checker |
| 28 | + * board pattern. The transparency should runs from left to right from |
| 29 | + * total transparent to total opaque. |
| 30 | + * @library /java/awt/regtesthelpers |
| 31 | + * @build PassFailJFrame |
| 32 | + * @run main/manual GrayAlpha |
| 33 | + */ |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.color.ColorSpace; |
| 38 | +import java.awt.Dimension; |
| 39 | +import java.awt.Graphics; |
| 40 | +import java.awt.Graphics2D; |
| 41 | +import java.awt.geom.AffineTransform; |
| 42 | +import java.awt.image.BufferedImage; |
| 43 | +import java.awt.image.ColorModel; |
| 44 | +import java.awt.image.ComponentColorModel; |
| 45 | +import java.awt.image.DataBuffer; |
| 46 | +import java.awt.image.Raster; |
| 47 | +import java.awt.image.WritableRaster; |
| 48 | +import java.awt.Point; |
| 49 | +import java.awt.Panel; |
| 50 | +import java.awt.Transparency; |
| 51 | +import java.awt.Window; |
| 52 | + |
| 53 | +public class GrayAlpha extends Panel { |
| 54 | + |
| 55 | + private static final String INSTRUCTIONS = """ |
| 56 | + This test should show two windows filled with checker board |
| 57 | + vpattern. The transparency should runs from left to right from |
| 58 | + totally transparent to totally opaque. If either the pattern or |
| 59 | + the transparency is not shown correctly, click Fail, otherwise |
| 60 | + click Pass."""; |
| 61 | + |
| 62 | + BufferedImage bi; |
| 63 | + AffineTransform identityTransform = new AffineTransform(); |
| 64 | + |
| 65 | + public static void main(String[] args) throws Exception { |
| 66 | + PassFailJFrame.builder() |
| 67 | + .title("GrayAlpha Instructions") |
| 68 | + .instructions(INSTRUCTIONS) |
| 69 | + .rows((int)INSTRUCTIONS.lines().count() + 2) |
| 70 | + .columns(35) |
| 71 | + .testUI(GrayAlpha::createTestUI) |
| 72 | + .build() |
| 73 | + .awaitAndCheck(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + public GrayAlpha(int width, int height, |
| 78 | + boolean hasAlpha, boolean isAlphaPremultiplied, |
| 79 | + boolean useRGB) { |
| 80 | + boolean isAlphaPremuliplied = true; |
| 81 | + int bands = useRGB ? 3 : 1; |
| 82 | + bands = hasAlpha ? bands + 1 : bands; |
| 83 | + |
| 84 | + ColorSpace cs = useRGB ? |
| 85 | + ColorSpace.getInstance(ColorSpace.CS_sRGB) : |
| 86 | + ColorSpace.getInstance(ColorSpace.CS_GRAY); |
| 87 | + int transparency = hasAlpha ? |
| 88 | + Transparency.TRANSLUCENT : Transparency.OPAQUE; |
| 89 | + int[] bits = new int[bands]; |
| 90 | + for (int i = 0; i < bands; i++) { |
| 91 | + bits[i] = 8; |
| 92 | + } |
| 93 | + |
| 94 | + ColorModel cm = new ComponentColorModel(cs, |
| 95 | + bits, |
| 96 | + hasAlpha, |
| 97 | + isAlphaPremultiplied, |
| 98 | + transparency, |
| 99 | + DataBuffer.TYPE_BYTE); |
| 100 | + WritableRaster wr = |
| 101 | + Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, |
| 102 | + width, height, bands, |
| 103 | + new Point(0, 0)); |
| 104 | + |
| 105 | + for (int b = 0; b < bands; b++) { |
| 106 | + for (int y = 0; y < height; y++) { |
| 107 | + for (int x = 0; x < width; x++) { |
| 108 | + int s; |
| 109 | + |
| 110 | + if (b != bands - 1 || !hasAlpha) { |
| 111 | + // Gray band(s), fill with a checkerboard pattern |
| 112 | + if (((x / 10) % 2) == ((y / 10) % 2)) { |
| 113 | + s = 255; |
| 114 | + } else { |
| 115 | + s = 0; |
| 116 | + } |
| 117 | + if (isAlphaPremultiplied) { |
| 118 | + int alpha = (x*255)/(width - 1); |
| 119 | + s = (s*alpha)/255; |
| 120 | + } |
| 121 | + } else { |
| 122 | + // Alpha band, increase opacity left to right |
| 123 | + s = (x*255)/(width - 1); |
| 124 | + } |
| 125 | + |
| 126 | + wr.setSample(x, y, b, s); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + this.bi = new BufferedImage(cm, wr, isAlphaPremultiplied, null); |
| 132 | + } |
| 133 | + |
| 134 | + public Dimension getPreferredSize() { |
| 135 | + return new Dimension(bi.getWidth(), bi.getHeight()); |
| 136 | + } |
| 137 | + |
| 138 | + public void paint(Graphics g) { |
| 139 | + if (bi != null) { |
| 140 | + ((Graphics2D)g).drawImage(bi, 0, 0, null); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static Frame makeFrame(String title, |
| 145 | + int x, int y, int width, int height, |
| 146 | + boolean hasAlpha, |
| 147 | + boolean isAlphaPremultiplied, |
| 148 | + boolean useRGB) { |
| 149 | + Frame f = new Frame(title); |
| 150 | + f.add(new GrayAlpha(width, height, |
| 151 | + hasAlpha, isAlphaPremultiplied, useRGB)); |
| 152 | + f.pack(); |
| 153 | + f.setLocation(x, y); |
| 154 | + return f; |
| 155 | + } |
| 156 | + |
| 157 | + private static List<Window> createTestUI() { |
| 158 | + int width = 200; |
| 159 | + int height = 200; |
| 160 | + |
| 161 | + int x = 100; |
| 162 | + int y = 100; |
| 163 | + |
| 164 | + Frame f1 = makeFrame("Gray (non-premultiplied)", |
| 165 | + x, y, width, height, |
| 166 | + true, false, false); |
| 167 | + x += width + 20; |
| 168 | + |
| 169 | + Frame f2 = makeFrame("Gray (premultiplied)", |
| 170 | + x, y, width, height, |
| 171 | + true, true, false); |
| 172 | + |
| 173 | + return List.of(f1, f2); |
| 174 | + } |
| 175 | +} |
0 commit comments