DrawImageBgTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2007, 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
24/**
25 * @test
26 * @key headful
27 * @bug 6603887
28 * @summary Verifies that drawImage with bg color works correctly for ICM image
29 * @run main/othervm DrawImageBgTest
30 * @run main/othervm -Dsun.java2d.pmoffscreen=true DrawImageBgTest
31 */
32import java.awt.Color;
33import java.awt.Graphics;
34import java.awt.GraphicsConfiguration;
35import java.awt.GraphicsEnvironment;
36import java.awt.image.BufferedImage;
37import java.awt.image.IndexColorModel;
38import java.awt.image.VolatileImage;
39import java.awt.image.WritableRaster;
40import java.io.File;
41import java.io.IOException;
42import javax.imageio.ImageIO;
43
44public class DrawImageBgTest {
45    public static void main(String[] args) {
46        GraphicsConfiguration gc =
47            GraphicsEnvironment.getLocalGraphicsEnvironment().
48                getDefaultScreenDevice().getDefaultConfiguration();
49
50        if (gc.getColorModel().getPixelSize() <= 8) {
51            System.out.println("8-bit color model, test considered passed");
52            return;
53        }
54
55        /*
56         * Set up images:
57         * 1.) VolatileImge for rendering to,
58         * 2.) BufferedImage for reading back the contents of the VI
59         * 3.) The image triggering the problem
60         */
61        VolatileImage vImg = null;
62        BufferedImage readBackBImg;
63
64        // create a BITMASK ICM such that the transparent color is
65        // tr. black (and it's the first in the color map so a buffered image
66        // created with this ICM is transparent
67        byte r[] = { 0x00, (byte)0xff};
68        byte g[] = { 0x00, (byte)0xff};
69        byte b[] = { 0x00, (byte)0xff};
70        IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);
71        WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);
72        BufferedImage tImg = new BufferedImage(icm, wr, false, null);
73
74        do {
75            if (vImg == null ||
76                vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)
77            {
78                vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),
79                                                        tImg.getHeight());
80            }
81
82            Graphics viG = vImg.getGraphics();
83            viG.setColor(Color.red);
84            viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
85
86            viG.drawImage(tImg, 0, 0, Color.green, null);
87            viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
88            viG.drawImage(tImg, 0, 0, Color.white, null);
89
90            readBackBImg = vImg.getSnapshot();
91        } while (vImg.contentsLost());
92
93        for (int x = 0; x < readBackBImg.getWidth(); x++) {
94            for (int y = 0; y < readBackBImg.getHeight(); y++) {
95                int currPixel = readBackBImg.getRGB(x, y);
96                if (currPixel != Color.white.getRGB()) {
97                    String fileName = "DrawImageBgTest.png";
98                    try {
99                        ImageIO.write(readBackBImg, "png", new File(fileName));
100                        System.err.println("Dumped image to " + fileName);
101                    } catch (IOException ex) {}
102                    throw new
103                        RuntimeException("Test Failed: found wrong color: 0x"+
104                                         Integer.toHexString(currPixel));
105                }
106            }
107        }
108        System.out.println("Test Passed.");
109    }
110}
111