1 /*
2  * Copyright (c) 2013, 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     8005930
27  * @summary Thest verifies that color conversion does not distort
28  *          alpha channel in the destination image.
29  *
30  * @run     main AlphaTest
31  */
32 
33 import java.awt.AlphaComposite;
34 import java.awt.Color;
35 import java.awt.Graphics2D;
36 import java.awt.color.ColorSpace;
37 import java.awt.image.BufferedImage;
38 import java.awt.image.ColorConvertOp;
39 
40 public class AlphaTest {
main(String[] args)41     public static void main(String[] args) {
42         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
43 
44         ColorConvertOp op = new ColorConvertOp(cs, null);
45         // create source image filled with an opaque color
46         BufferedImage src = createSrc();
47         int srcAlpha = getAlpha(src);
48 
49         System.out.printf("Src alpha: 0x%02x\n", srcAlpha);
50 
51         // create clear (transparent black) destination image
52         BufferedImage dst = createDst();
53         int dstAlpha = getAlpha(dst);
54         System.out.printf("Dst alpha: 0x%02x\n", dstAlpha);
55 
56 
57         dst = op.filter(src, dst);
58         dstAlpha = getAlpha(dst);
59         // we expect that destination image is opaque
60         // i.e. alpha is transferred from source to
61         // the destination
62         System.out.printf("Result alpha: 0x%02x\n", dstAlpha);
63 
64         if (srcAlpha != dstAlpha) {
65             throw new RuntimeException("Test failed!");
66         }
67         System.out.println("Test passed");
68     }
69 
createSrc()70     private static BufferedImage createSrc() {
71         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
72 
73         Graphics2D g = img.createGraphics();
74         g.setColor(Color.red);
75         g.fillRect(0, 0, w, h);
76         g.dispose();
77 
78         return img;
79     }
80 
createDst()81     private static BufferedImage createDst() {
82         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
83 
84         Graphics2D g = img.createGraphics();
85         g.setComposite(AlphaComposite.Clear);
86         g.fillRect(0, 0, w, h);
87         g.dispose();
88 
89         return img;
90     }
91 
getAlpha(BufferedImage img)92     private static int getAlpha(BufferedImage img) {
93         int argb = img.getRGB(w / 2, h / 2);
94         return 0xff & (argb >> 24);
95     }
96 
97     private static final int w = 100;
98     private static final int h = 100;
99 }
100