EABlitTest.java revision 8729:0242fce0f717
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
24import java.awt.*;
25import java.awt.image.*;
26import java.util.*;
27import javax.swing.*;
28
29/**
30 * @test
31 * @bug 8024895
32 * @summary tests if changing extra alpha values are honored for transformed blits
33 * @author ceisserer
34 */
35public class EABlitTest extends Frame {
36    protected void test() {
37        BufferedImage srcImg = createSrcImage();
38        Image dstImg = getGraphicsConfiguration().createCompatibleVolatileImage(20, 50);
39
40        //be over-cautious and render twice to avoid BI caching issues
41        renderToVI(srcImg, dstImg);
42        renderToVI(srcImg, dstImg);
43
44        BufferedImage validationImg = new BufferedImage(20, 50, BufferedImage.TYPE_INT_RGB);
45        Graphics2D valG = (Graphics2D) validationImg.getGraphics();
46        valG.drawImage(dstImg, 0, 0, null);
47
48        //Loop over all pixel, and count the different pixel values encountered.
49        TreeSet<Integer> colorCntSet = new TreeSet<>();
50        for (int x=0; x < validationImg.getWidth(); x++) {
51            for (int y=0; y < validationImg.getHeight(); y++) {
52                int rgb = validationImg.getRGB(x, y);
53                colorCntSet.add(rgb);
54            }
55        }
56
57        //Check if we encountered 3 different color values in total
58        if (colorCntSet.size() == 3) {
59            System.out.println("Passed!");
60        } else {
61            throw new RuntimeException("Test FAILED!");
62        }
63    }
64
65    protected void renderToVI(BufferedImage src, Image dst) {
66        Graphics2D g = (Graphics2D) dst.getGraphics();
67
68        g.setColor(Color.WHITE);
69        g.fillRect(0, 0, 50, 50);
70        g.rotate(0.5f);
71        g.setRenderingHint(RenderingHints.KEY_RENDERING,
72                           RenderingHints.VALUE_RENDER_QUALITY);
73
74        g.setComposite(AlphaComposite.SrcOver.derive(1f));
75        g.drawImage(src, 10, 10, null);
76
77        g.setComposite(AlphaComposite.SrcOver.derive(0.5f));
78        g.drawImage(src, 20, 20, null);
79    }
80
81    protected BufferedImage createSrcImage() {
82        BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
83        Graphics2D g = (Graphics2D) bi.getGraphics();
84        g.setColor(Color.YELLOW);
85        g.fillRect(0, 0, 10, 10);
86        return bi;
87    }
88
89    public static void main(String[] args) {
90         new EABlitTest().test();
91    }
92}
93