|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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 | +import java.awt.Color; |
| 25 | +import java.awt.Graphics2D; |
| 26 | +import java.awt.GraphicsConfiguration; |
| 27 | +import java.awt.GraphicsEnvironment; |
| 28 | +import java.awt.HeadlessException; |
| 29 | +import java.awt.Image; |
| 30 | +import java.awt.Paint; |
| 31 | +import java.awt.PaintContext; |
| 32 | +import java.awt.Rectangle; |
| 33 | +import java.awt.RenderingHints; |
| 34 | +import java.awt.geom.AffineTransform; |
| 35 | +import java.awt.geom.Rectangle2D; |
| 36 | +import java.awt.image.BufferedImage; |
| 37 | +import java.awt.image.ColorModel; |
| 38 | +import java.awt.image.VolatileImage; |
| 39 | +import java.util.function.Consumer; |
| 40 | + |
| 41 | +/** |
| 42 | + * @test |
| 43 | + * @bug 8355078 |
| 44 | + * @summary Checks if different image types (BufferedImage and VolatileImage) |
| 45 | + * produce the same results when using different ways to fill the image |
| 46 | + * (setColor, setPaint, and custom Paint) |
| 47 | + */ |
| 48 | +public final class ColorPaintContextBasicTest { |
| 49 | + |
| 50 | + private static final int SIZE = 100; |
| 51 | + |
| 52 | + private static final int[] TYPES = new int[]{ |
| 53 | + BufferedImage.TYPE_INT_RGB, |
| 54 | + BufferedImage.TYPE_INT_ARGB, |
| 55 | + BufferedImage.TYPE_INT_ARGB_PRE, |
| 56 | + BufferedImage.TYPE_INT_BGR, |
| 57 | + BufferedImage.TYPE_3BYTE_BGR, |
| 58 | + BufferedImage.TYPE_4BYTE_ABGR, |
| 59 | + BufferedImage.TYPE_4BYTE_ABGR_PRE, |
| 60 | + }; |
| 61 | + |
| 62 | + private static final Color[] COLORS = { |
| 63 | + Color.RED, |
| 64 | + Color.GREEN, |
| 65 | + Color.BLUE, |
| 66 | + Color.YELLOW, |
| 67 | + Color.MAGENTA, |
| 68 | + Color.CYAN, |
| 69 | + Color.BLACK, |
| 70 | + Color.WHITE, |
| 71 | + new Color(255, 165, 0), |
| 72 | + new Color(128, 0, 128), |
| 73 | + new Color(255, 0, 0, 128) |
| 74 | + }; |
| 75 | + |
| 76 | + /** |
| 77 | + * Custom implementation of Paint that wraps a Color but is intentionally |
| 78 | + * not a Color. This is used to bypass the "paint instanceof Color" |
| 79 | + * optimization in Graphics2D#setPaint(). |
| 80 | + */ |
| 81 | + private static final class CustomPaint implements Paint { |
| 82 | + |
| 83 | + private final Color color; |
| 84 | + |
| 85 | + private CustomPaint(Color color) { |
| 86 | + this.color = color; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public PaintContext createContext(ColorModel cm, |
| 91 | + Rectangle deviceBounds, |
| 92 | + Rectangle2D userBounds, |
| 93 | + AffineTransform xform, |
| 94 | + RenderingHints hints) |
| 95 | + { |
| 96 | + return color.createContext(cm, deviceBounds, userBounds, xform, |
| 97 | + hints); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int getTransparency() { |
| 102 | + return color.getTransparency(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static void main(String[] args) { |
| 107 | + GraphicsConfiguration gc = null; |
| 108 | + try { |
| 109 | + gc = GraphicsEnvironment.getLocalGraphicsEnvironment() |
| 110 | + .getDefaultScreenDevice() |
| 111 | + .getDefaultConfiguration(); |
| 112 | + } catch (HeadlessException ignore) { |
| 113 | + // skip VolatileImage validation |
| 114 | + } |
| 115 | + |
| 116 | + for (Color color : COLORS) { |
| 117 | + int rgb = color.getRGB(); |
| 118 | + System.err.println("Test color: " + Integer.toHexString(rgb)); |
| 119 | + for (int type : TYPES) { |
| 120 | + var goldBI = new BufferedImage(SIZE, SIZE, type); |
| 121 | + var paintBI = new BufferedImage(SIZE, SIZE, type); |
| 122 | + var customBI = new BufferedImage(SIZE, SIZE, type); |
| 123 | + |
| 124 | + fill(goldBI, g -> g.setColor(color)); |
| 125 | + fill(paintBI, g -> g.setPaint(color)); |
| 126 | + fill(customBI, g -> g.setPaint(new CustomPaint(color))); |
| 127 | + |
| 128 | + if (!verify(paintBI, goldBI)) { |
| 129 | + throw new RuntimeException("paintBI != goldBI"); |
| 130 | + } |
| 131 | + |
| 132 | + if (!verify(customBI, goldBI)) { |
| 133 | + throw new RuntimeException("customBI != goldBI"); |
| 134 | + } |
| 135 | + |
| 136 | + if (gc == null) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + int transparency = goldBI.getTransparency(); |
| 141 | + var goldVI = fillVI(gc, transparency, g -> g.setColor(color)); |
| 142 | + var paintVI = fillVI(gc, transparency, g -> g.setPaint(color)); |
| 143 | + var customVI = fillVI(gc, transparency, |
| 144 | + g -> g.setPaint(new CustomPaint(color))); |
| 145 | + |
| 146 | + if (gc.getColorModel().getPixelSize() >= 24) { |
| 147 | + if (color.getAlpha() == 255 && !verify(goldBI, goldVI)) { |
| 148 | + throw new RuntimeException("goldBI != goldVI"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (!verify(paintVI, goldVI)) { |
| 153 | + throw new RuntimeException("paintVI != goldVI"); |
| 154 | + } |
| 155 | + |
| 156 | + if (!verify(customVI, goldVI)) { |
| 157 | + throw new RuntimeException("customVI != goldVI"); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static void fill(Image img, Consumer<Graphics2D> action) { |
| 164 | + Graphics2D g2d = (Graphics2D) img.getGraphics(); |
| 165 | + action.accept(g2d); |
| 166 | + g2d.fillRect(0, 0, SIZE, SIZE); |
| 167 | + g2d.dispose(); |
| 168 | + } |
| 169 | + |
| 170 | + private static BufferedImage fillVI(GraphicsConfiguration gc, |
| 171 | + int transparency, |
| 172 | + Consumer<Graphics2D> action) |
| 173 | + { |
| 174 | + var vi = gc.createCompatibleVolatileImage(SIZE, SIZE, transparency); |
| 175 | + int attempt = 0; |
| 176 | + while (true) { |
| 177 | + if (++attempt > 10) { |
| 178 | + throw new RuntimeException("Too many attempts: " + attempt); |
| 179 | + } |
| 180 | + |
| 181 | + int status = vi.validate(gc); |
| 182 | + if (status == VolatileImage.IMAGE_INCOMPATIBLE) { |
| 183 | + vi = gc.createCompatibleVolatileImage(SIZE, SIZE, transparency); |
| 184 | + } |
| 185 | + |
| 186 | + fill(vi, action); |
| 187 | + |
| 188 | + BufferedImage snapshot = vi.getSnapshot(); |
| 189 | + if (vi.contentsLost()) { |
| 190 | + continue; |
| 191 | + } |
| 192 | + return snapshot; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private static boolean verify(BufferedImage img1, BufferedImage img2) { |
| 197 | + for (int y = 0; y < SIZE; y++) { |
| 198 | + for (int x = 0; x < SIZE; x++) { |
| 199 | + int rgb1 = img1.getRGB(x, y); |
| 200 | + int rgb2 = img2.getRGB(x, y); |
| 201 | + if (rgb1 != rgb2) { |
| 202 | + System.err.println("rgb1: " + Integer.toHexString(rgb1)); |
| 203 | + System.err.println("rgb2: " + Integer.toHexString(rgb2)); |
| 204 | + return false; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + return true; |
| 209 | + } |
| 210 | +} |
0 commit comments