|
| 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.AlphaComposite; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.Graphics2D; |
| 27 | +import java.awt.Transparency; |
| 28 | +import java.awt.color.ColorSpace; |
| 29 | +import java.awt.image.BufferedImage; |
| 30 | +import java.awt.image.ColorConvertOp; |
| 31 | +import java.awt.image.ColorModel; |
| 32 | +import java.awt.image.ComponentColorModel; |
| 33 | +import java.awt.image.DataBuffer; |
| 34 | +import java.awt.image.DirectColorModel; |
| 35 | +import java.awt.image.Raster; |
| 36 | +import java.awt.image.WritableRaster; |
| 37 | +import java.util.Arrays; |
| 38 | + |
| 39 | +import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR_PRE; |
| 40 | +import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE; |
| 41 | + |
| 42 | +/** |
| 43 | + * @test |
| 44 | + * @bug 8279216 |
| 45 | + * @summary Verifies implementation of premultiplied alpha |
| 46 | + */ |
| 47 | +public final class PremultipliedAlpha { |
| 48 | + |
| 49 | + private static final int SIZE = 256; |
| 50 | + |
| 51 | + private static final int COLOR_TOLERANCE = 2; |
| 52 | + |
| 53 | + private enum Type { |
| 54 | + TYPE_ARGB_PRE, |
| 55 | + TYPE_4BYTE_ABGR_PRE, |
| 56 | + TYPE_CUSTOM_ARGB_PRE, |
| 57 | + TYPE_CUSTOM_GABR_PRE, |
| 58 | + TYPE_CUSTOM_4BYTE_ABGR_PRE, |
| 59 | + TYPE_CUSTOM_4BYTE_ARGB_PRE, |
| 60 | + TYPE_CUSTOM_4BYTE_RGBA_PRE, |
| 61 | + TYPE_CUSTOM_4BYTE_GABR_PRE, |
| 62 | + TYPE_CUSTOM_4USHORT_8bit_ARGB_PRE, |
| 63 | + TYPE_CUSTOM_4INT_8bit_ARGB_PRE, |
| 64 | + } |
| 65 | + |
| 66 | + private static final ColorSpace[] CSs = { |
| 67 | + ColorSpace.getInstance(ColorSpace.CS_CIEXYZ), |
| 68 | + ColorSpace.getInstance(ColorSpace.CS_GRAY), |
| 69 | + ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), |
| 70 | + ColorSpace.getInstance(ColorSpace.CS_PYCC), |
| 71 | + ColorSpace.getInstance(ColorSpace.CS_sRGB) |
| 72 | + }; |
| 73 | + |
| 74 | + public static void main(String[] args) { |
| 75 | + for (ColorSpace cs : CSs) { |
| 76 | + for (Type dst : Type.values()) { |
| 77 | + BufferedImage gold = null; |
| 78 | + for (Type src : Type.values()) { |
| 79 | + BufferedImage from = createSrc(src); |
| 80 | + BufferedImage to = createDst(dst); |
| 81 | + ColorConvertOp op = new ColorConvertOp(cs, null); |
| 82 | + op.filter(from, to); |
| 83 | + if (gold == null) { |
| 84 | + gold = to; |
| 85 | + } else { |
| 86 | + validate(gold, to); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void validate(BufferedImage img1, BufferedImage img2) { |
| 94 | + for (int a = 0; a < SIZE; a++) { |
| 95 | + for (int c = 0; c < SIZE; c++) { |
| 96 | + int[] pixel1 = img1.getRaster().getPixel(c, a, (int[]) null); |
| 97 | + int[] pixel2 = img2.getRaster().getPixel(c, a, (int[]) null); |
| 98 | + if (pixel1.length != pixel2.length) { |
| 99 | + throw new RuntimeException(); |
| 100 | + } |
| 101 | + for (int i = 0 ; i < pixel1.length; ++i) { |
| 102 | + if (Math.abs(pixel1[i] - pixel2[i]) >= COLOR_TOLERANCE) { |
| 103 | + System.out.println("c = " + c); |
| 104 | + System.out.println("a = " + a); |
| 105 | + System.err.println("rgb1 = " + Arrays.toString(pixel1)); |
| 106 | + System.err.println("rgb2 = " + Arrays.toString(pixel2)); |
| 107 | + throw new RuntimeException(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static BufferedImage createDst(Type type) { |
| 115 | + BufferedImage img = createSrc(type); |
| 116 | + Graphics2D g = img.createGraphics(); |
| 117 | + g.setComposite(AlphaComposite.Clear); |
| 118 | + g.fillRect(0, 0, SIZE, SIZE); |
| 119 | + g.dispose(); |
| 120 | + return img; |
| 121 | + } |
| 122 | + |
| 123 | + private static BufferedImage createSrc(Type type) { |
| 124 | + BufferedImage bi = switch (type) { |
| 125 | + case TYPE_ARGB_PRE -> new BufferedImage(SIZE, SIZE, TYPE_INT_ARGB_PRE); |
| 126 | + case TYPE_CUSTOM_ARGB_PRE -> TYPE_ARGB_PRE(); |
| 127 | + case TYPE_CUSTOM_GABR_PRE -> TYPE_GABR_PRE(); |
| 128 | + case TYPE_4BYTE_ABGR_PRE -> new BufferedImage(SIZE, SIZE, TYPE_4BYTE_ABGR_PRE); |
| 129 | + case TYPE_CUSTOM_4BYTE_ARGB_PRE -> TYPE_4BYTE_ARGB_PRE(); |
| 130 | + case TYPE_CUSTOM_4BYTE_ABGR_PRE -> TYPE_4BYTE_ABGR_PRE(); |
| 131 | + case TYPE_CUSTOM_4BYTE_RGBA_PRE -> TYPE_4BYTE_RGBA_PRE(); |
| 132 | + case TYPE_CUSTOM_4BYTE_GABR_PRE -> TYPE_4BYTE_GABR_PRE(); |
| 133 | + case TYPE_CUSTOM_4USHORT_8bit_ARGB_PRE -> TYPE_4USHORT_ARGB_8bit_PRE(); |
| 134 | + case TYPE_CUSTOM_4INT_8bit_ARGB_PRE -> TYPE_4INT_ARGB_8bit_PRE(); |
| 135 | + }; |
| 136 | + fill(bi); |
| 137 | + return bi; |
| 138 | + } |
| 139 | + |
| 140 | + private static BufferedImage TYPE_ARGB_PRE() { |
| 141 | + ColorModel colorModel = new DirectColorModel( |
| 142 | + ColorSpace.getInstance(ColorSpace.CS_sRGB), |
| 143 | + 32, |
| 144 | + 0x00ff0000, // Red |
| 145 | + 0x0000ff00, // Green |
| 146 | + 0x000000ff, // Blue |
| 147 | + 0xff000000, // Alpha |
| 148 | + true, // Alpha Premultiplied |
| 149 | + DataBuffer.TYPE_INT |
| 150 | + ); |
| 151 | + WritableRaster raster = colorModel.createCompatibleWritableRaster(SIZE, |
| 152 | + SIZE); |
| 153 | + return new BufferedImage(colorModel, raster, true, null); |
| 154 | + } |
| 155 | + |
| 156 | + private static BufferedImage TYPE_GABR_PRE() { |
| 157 | + ColorModel colorModel = new DirectColorModel( |
| 158 | + ColorSpace.getInstance(ColorSpace.CS_sRGB), |
| 159 | + 32, |
| 160 | + 0x000000ff, // Red |
| 161 | + 0xff000000, // Green |
| 162 | + 0x0000ff00, // Blue |
| 163 | + 0x00ff0000, // Alpha |
| 164 | + true, // Alpha Premultiplied |
| 165 | + DataBuffer.TYPE_INT |
| 166 | + ); |
| 167 | + WritableRaster raster = colorModel.createCompatibleWritableRaster(SIZE, |
| 168 | + SIZE); |
| 169 | + return new BufferedImage(colorModel, raster, true, null); |
| 170 | + } |
| 171 | + |
| 172 | + private static BufferedImage TYPE_4BYTE_RGBA_PRE() { |
| 173 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 174 | + int[] nBits = {8, 8, 8, 8}; |
| 175 | + int[] bOffs = {0, 1, 2, 3}; |
| 176 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true, |
| 177 | + Transparency.TRANSLUCENT, |
| 178 | + DataBuffer.TYPE_BYTE); |
| 179 | + WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, |
| 180 | + SIZE, SIZE, |
| 181 | + SIZE * 4, 4, |
| 182 | + bOffs, null); |
| 183 | + return new BufferedImage(colorModel, raster, true, null); |
| 184 | + } |
| 185 | + |
| 186 | + private static BufferedImage TYPE_4BYTE_ABGR_PRE() { |
| 187 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 188 | + int[] nBits = {8, 8, 8, 8}; |
| 189 | + int[] bOffs = {3, 2, 1, 0}; |
| 190 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true, |
| 191 | + Transparency.TRANSLUCENT, |
| 192 | + DataBuffer.TYPE_BYTE); |
| 193 | + WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, |
| 194 | + SIZE, SIZE, |
| 195 | + SIZE * 4, 4, |
| 196 | + bOffs, null); |
| 197 | + return new BufferedImage(colorModel, raster, true, null); |
| 198 | + } |
| 199 | + |
| 200 | + private static BufferedImage TYPE_4BYTE_ARGB_PRE() { |
| 201 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 202 | + int[] nBits = {8, 8, 8, 8}; |
| 203 | + int[] bOffs = {1, 2, 3, 0}; |
| 204 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true, |
| 205 | + Transparency.TRANSLUCENT, |
| 206 | + DataBuffer.TYPE_BYTE); |
| 207 | + WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, |
| 208 | + SIZE, SIZE, |
| 209 | + SIZE * 4, 4, |
| 210 | + bOffs, null); |
| 211 | + return new BufferedImage(colorModel, raster, true, null); |
| 212 | + } |
| 213 | + |
| 214 | + private static BufferedImage TYPE_4BYTE_GABR_PRE() { |
| 215 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 216 | + int[] nBits = {8, 8, 8, 8}; |
| 217 | + int[] bOffs = {3, 0, 2, 1}; |
| 218 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, true, false, |
| 219 | + Transparency.TRANSLUCENT, |
| 220 | + DataBuffer.TYPE_BYTE); |
| 221 | + WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, |
| 222 | + SIZE, SIZE, |
| 223 | + SIZE * 4, 4, |
| 224 | + bOffs, null); |
| 225 | + return new BufferedImage(colorModel, raster, true, null); |
| 226 | + } |
| 227 | + |
| 228 | + private static BufferedImage TYPE_4INT_ARGB_8bit_PRE() { |
| 229 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 230 | + int[] nBits = {8, 8, 8, 8}; |
| 231 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true, |
| 232 | + Transparency.TRANSLUCENT, |
| 233 | + DataBuffer.TYPE_INT); |
| 234 | + WritableRaster raster = colorModel.createCompatibleWritableRaster(SIZE, |
| 235 | + SIZE); |
| 236 | + return new BufferedImage(colorModel, raster, true, null); |
| 237 | + } |
| 238 | + |
| 239 | + private static BufferedImage TYPE_4USHORT_ARGB_8bit_PRE() { |
| 240 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 241 | + int[] nBits = {8, 8, 8, 8}; |
| 242 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true, |
| 243 | + Transparency.TRANSLUCENT, |
| 244 | + DataBuffer.TYPE_USHORT); |
| 245 | + WritableRaster raster = colorModel.createCompatibleWritableRaster(SIZE, |
| 246 | + SIZE); |
| 247 | + return new BufferedImage(colorModel, raster, true, null); |
| 248 | + } |
| 249 | + |
| 250 | + private static void fill(BufferedImage image) { |
| 251 | +// Graphics2D g = image.createGraphics(); |
| 252 | +// g.setComposite(AlphaComposite.Src); |
| 253 | + for (int a = 0; a < SIZE; ++a) { |
| 254 | + for (int c = 0; c < SIZE; ++c) { |
| 255 | + Color c1 = new Color(a, c, c, a); |
| 256 | +// g.setColor(c1); |
| 257 | +//TODO CANNOT USE fillrect, does not work for custom types! |
| 258 | +// g.fillRect(c, a, 1, 1); |
| 259 | + image.setRGB(c,a , c1.getRGB()); |
| 260 | + } |
| 261 | + } |
| 262 | +// g.dispose(); |
| 263 | + } |
| 264 | +} |
0 commit comments