Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/X11SurfaceData/DrawImageBgTest/DrawImageBgTest.java
38854 views
/*1* Copyright (c) 2007, 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* @bug 660388726* @summary Verifies that drawImage with bg color works correctly for ICM image27* @run main/othervm DrawImageBgTest28* @run main/othervm -Dsun.java2d.pmoffscreen=true DrawImageBgTest29*/30import java.awt.Color;31import java.awt.Graphics;32import java.awt.GraphicsConfiguration;33import java.awt.GraphicsEnvironment;34import java.awt.image.BufferedImage;35import java.awt.image.IndexColorModel;36import java.awt.image.VolatileImage;37import java.awt.image.WritableRaster;38import java.io.File;39import java.io.IOException;40import javax.imageio.ImageIO;4142public class DrawImageBgTest {43public static void main(String[] args) {44GraphicsConfiguration gc =45GraphicsEnvironment.getLocalGraphicsEnvironment().46getDefaultScreenDevice().getDefaultConfiguration();4748if (gc.getColorModel().getPixelSize() <= 8) {49System.out.println("8-bit color model, test considered passed");50return;51}5253/*54* Set up images:55* 1.) VolatileImge for rendering to,56* 2.) BufferedImage for reading back the contents of the VI57* 3.) The image triggering the problem58*/59VolatileImage vImg = null;60BufferedImage readBackBImg;6162// create a BITMASK ICM such that the transparent color is63// tr. black (and it's the first in the color map so a buffered image64// created with this ICM is transparent65byte r[] = { 0x00, (byte)0xff};66byte g[] = { 0x00, (byte)0xff};67byte b[] = { 0x00, (byte)0xff};68IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);69WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);70BufferedImage tImg = new BufferedImage(icm, wr, false, null);7172do {73if (vImg == null ||74vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)75{76vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),77tImg.getHeight());78}7980Graphics viG = vImg.getGraphics();81viG.setColor(Color.red);82viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());8384viG.drawImage(tImg, 0, 0, Color.green, null);85viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());86viG.drawImage(tImg, 0, 0, Color.white, null);8788readBackBImg = vImg.getSnapshot();89} while (vImg.contentsLost());9091for (int x = 0; x < readBackBImg.getWidth(); x++) {92for (int y = 0; y < readBackBImg.getHeight(); y++) {93int currPixel = readBackBImg.getRGB(x, y);94if (currPixel != Color.white.getRGB()) {95String fileName = "DrawImageBgTest.png";96try {97ImageIO.write(readBackBImg, "png", new File(fileName));98System.err.println("Dumped image to " + fileName);99} catch (IOException ex) {}100throw new101RuntimeException("Test Failed: found wrong color: 0x"+102Integer.toHexString(currPixel));103}104}105}106System.out.println("Test Passed.");107}108}109110111