0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6603887
0N/A * @summary Verifies that drawImage with bg color works correctly for ICM image
0N/A * @run main/othervm DrawImageBgTest
0N/A * @run main/othervm -Dsun.java2d.pmoffscreen=true DrawImageBgTest
0N/A */
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.awt.image.IndexColorModel;
0N/Aimport java.awt.image.VolatileImage;
0N/Aimport java.awt.image.WritableRaster;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.ImageIO;
0N/A
0N/Apublic class DrawImageBgTest {
0N/A public static void main(String[] args) {
0N/A GraphicsConfiguration gc =
0N/A GraphicsEnvironment.getLocalGraphicsEnvironment().
0N/A getDefaultScreenDevice().getDefaultConfiguration();
0N/A
0N/A if (gc.getColorModel().getPixelSize() <= 8) {
0N/A System.out.println("8-bit color model, test considered passed");
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Set up images:
0N/A * 1.) VolatileImge for rendering to,
0N/A * 2.) BufferedImage for reading back the contents of the VI
0N/A * 3.) The image triggering the problem
0N/A */
0N/A VolatileImage vImg = null;
0N/A BufferedImage readBackBImg;
0N/A
0N/A // create a BITMASK ICM such that the transparent color is
0N/A // tr. black (and it's the first in the color map so a buffered image
0N/A // created with this ICM is transparent
0N/A byte r[] = { 0x00, (byte)0xff};
0N/A byte g[] = { 0x00, (byte)0xff};
0N/A byte b[] = { 0x00, (byte)0xff};
0N/A IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);
0N/A WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);
0N/A BufferedImage tImg = new BufferedImage(icm, wr, false, null);
0N/A
0N/A do {
0N/A if (vImg == null ||
0N/A vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)
0N/A {
0N/A vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),
0N/A tImg.getHeight());
0N/A }
0N/A
0N/A Graphics viG = vImg.getGraphics();
0N/A viG.setColor(Color.red);
0N/A viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
0N/A
0N/A viG.drawImage(tImg, 0, 0, Color.green, null);
0N/A viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
0N/A viG.drawImage(tImg, 0, 0, Color.white, null);
0N/A
0N/A readBackBImg = vImg.getSnapshot();
0N/A } while (vImg.contentsLost());
0N/A
0N/A for (int x = 0; x < readBackBImg.getWidth(); x++) {
0N/A for (int y = 0; y < readBackBImg.getHeight(); y++) {
0N/A int currPixel = readBackBImg.getRGB(x, y);
0N/A if (currPixel != Color.white.getRGB()) {
0N/A String fileName = "DrawImageBgTest.png";
0N/A try {
0N/A ImageIO.write(readBackBImg, "png", new File(fileName));
0N/A System.err.println("Dumped image to " + fileName);
0N/A } catch (IOException ex) {}
0N/A throw new
0N/A RuntimeException("Test Failed: found wrong color: 0x"+
0N/A Integer.toHexString(currPixel));
0N/A }
0N/A }
0N/A }
0N/A System.out.println("Test Passed.");
0N/A }
0N/A}