Path: blob/master/test/jdk/sun/java2d/X11SurfaceData/DrawImageBgTest/DrawImageBgTest.java
41153 views
/*1* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 660388727* @summary Verifies that drawImage with bg color works correctly for ICM image28* @run main/othervm DrawImageBgTest29* @run main/othervm -Dsun.java2d.pmoffscreen=true DrawImageBgTest30*/31import java.awt.Color;32import java.awt.Graphics;33import java.awt.GraphicsConfiguration;34import java.awt.GraphicsEnvironment;35import java.awt.image.BufferedImage;36import java.awt.image.IndexColorModel;37import java.awt.image.VolatileImage;38import java.awt.image.WritableRaster;39import java.io.File;40import java.io.IOException;41import javax.imageio.ImageIO;4243public class DrawImageBgTest {44public static void main(String[] args) {45GraphicsConfiguration gc =46GraphicsEnvironment.getLocalGraphicsEnvironment().47getDefaultScreenDevice().getDefaultConfiguration();4849if (gc.getColorModel().getPixelSize() <= 8) {50System.out.println("8-bit color model, test considered passed");51return;52}5354/*55* Set up images:56* 1.) VolatileImge for rendering to,57* 2.) BufferedImage for reading back the contents of the VI58* 3.) The image triggering the problem59*/60VolatileImage vImg = null;61BufferedImage readBackBImg;6263// create a BITMASK ICM such that the transparent color is64// tr. black (and it's the first in the color map so a buffered image65// created with this ICM is transparent66byte r[] = { 0x00, (byte)0xff};67byte g[] = { 0x00, (byte)0xff};68byte b[] = { 0x00, (byte)0xff};69IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);70WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);71BufferedImage tImg = new BufferedImage(icm, wr, false, null);7273do {74if (vImg == null ||75vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)76{77vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),78tImg.getHeight());79}8081Graphics viG = vImg.getGraphics();82viG.setColor(Color.red);83viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());8485viG.drawImage(tImg, 0, 0, Color.green, null);86viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());87viG.drawImage(tImg, 0, 0, Color.white, null);8889readBackBImg = vImg.getSnapshot();90} while (vImg.contentsLost());9192for (int x = 0; x < readBackBImg.getWidth(); x++) {93for (int y = 0; y < readBackBImg.getHeight(); y++) {94int currPixel = readBackBImg.getRGB(x, y);95if (currPixel != Color.white.getRGB()) {96String fileName = "DrawImageBgTest.png";97try {98ImageIO.write(readBackBImg, "png", new File(fileName));99System.err.println("Dumped image to " + fileName);100} catch (IOException ex) {}101throw new102RuntimeException("Test Failed: found wrong color: 0x"+103Integer.toHexString(currPixel));104}105}106}107System.out.println("Test Passed.");108}109}110111112