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