Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/SunGraphics2D/DrawImageBilinear.java
38840 views
/*1* Copyright (c) 2007, 2010, 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*/22/*23* @test24* @bug 5009033 6603000 666636225* @summary Verifies that images transformed with bilinear filtering do not26* leave artifacts at the edges.27* @run main/othervm DrawImageBilinear28* @run main/othervm -Dsun.java2d.opengl=True DrawImageBilinear29* @author campbelc30*/3132import java.awt.Canvas;33import java.awt.Color;34import java.awt.Component;35import java.awt.Dimension;36import java.awt.Frame;37import java.awt.Graphics;38import java.awt.Graphics2D;39import java.awt.GraphicsConfiguration;40import java.awt.Point;41import java.awt.Rectangle;42import java.awt.RenderingHints;43import java.awt.Robot;44import java.awt.Toolkit;45import java.awt.image.BufferedImage;46import java.awt.image.IndexColorModel;47import java.awt.image.VolatileImage;4849public class DrawImageBilinear extends Canvas {5051private static final int SIZE = 5;5253private static boolean done;54private BufferedImage bimg1, bimg2;55private VolatileImage vimg;56private static volatile BufferedImage capture;57private static void doCapture(Component test) {58try {59Thread.sleep(2000);60} catch (InterruptedException ex) {}61// Grab the screen region62try {63Robot robot = new Robot();64Point pt1 = test.getLocationOnScreen();65Rectangle rect =66new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());67capture = robot.createScreenCapture(rect);68} catch (Exception e) {69e.printStackTrace();70}71}7273private void renderPattern(Graphics g) {74g.setColor(Color.red);75g.fillRect(0, 0, SIZE, SIZE);76//g.setColor(Color.green);77//g.drawRect(0, 0, SIZE-1, SIZE-1);78g.dispose();79}8081public void paint(Graphics g) {82Graphics2D g2d = (Graphics2D)g;8384if (bimg1 == null) {85bimg1 = (BufferedImage)createImage(SIZE, SIZE);86bimg1.setAccelerationPriority(0.0f);87renderPattern(bimg1.createGraphics());8889bimg2 = (BufferedImage)createImage(SIZE, SIZE);90renderPattern(bimg2.createGraphics());9192vimg = createVolatileImage(SIZE, SIZE);93vimg.validate(getGraphicsConfiguration());94renderPattern(vimg.createGraphics());95}9697do {98g2d.setColor(Color.white);99g2d.fillRect(0, 0, getWidth(), getHeight());100101g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,102RenderingHints.VALUE_INTERPOLATION_BILINEAR);103104// first time will be a sw->surface blit105g2d.drawImage(bimg1, 10, 10, 40, 40, null);106107// second time will be a texture->surface blit108g2d.drawImage(bimg2, 80, 10, 40, 40, null);109if (!skipOglTextureTest) {110g2d.drawImage(bimg2, 80, 10, 40, 40, null);111}112113// third time will be a pbuffer->surface blit114if (vimg.validate(getGraphicsConfiguration()) != VolatileImage.IMAGE_OK) {115renderPattern(vimg.createGraphics());116}117g2d.drawImage(vimg, 150, 10, 40, 40, null);118119Toolkit.getDefaultToolkit().sync();120} while (vimg.contentsLost());121122synchronized (this) {123if (!done) {124doCapture(this);125done = true;126}127notifyAll();128}129}130131public Dimension getPreferredSize() {132return new Dimension(200, 100);133}134135private static void testRegion(BufferedImage bi,136Rectangle affectedRegion)137{138int x1 = affectedRegion.x;139int y1 = affectedRegion.y;140int x2 = x1 + affectedRegion.width;141int y2 = y1 + affectedRegion.height;142143for (int y = y1; y < y2; y++) {144for (int x = x1; x < x2; x++) {145int actual = bi.getRGB(x, y);146if ((actual != 0xfffe0000) && (actual != 0xffff0000)) {147throw new RuntimeException("Test failed at x="+x+" y="+y+148" (expected=0xffff0000"+149" actual=0x"+150Integer.toHexString(actual) +151")");152}153}154}155}156157private static boolean skipOglTextureTest = false;158159public static void main(String[] args) {160boolean show = false;161for (String arg : args) {162if ("-show".equals(arg)) {163show = true;164}165}166167String arch = System.getProperty("os.arch");168boolean isOglEnabled = Boolean.getBoolean("sun.java2d.opengl");169skipOglTextureTest = isOglEnabled && ("sparc".equals(arch));170System.out.println("Skip OpenGL texture test: " + skipOglTextureTest);171172DrawImageBilinear test = new DrawImageBilinear();173Frame frame = new Frame();174frame.add(test);175frame.pack();176frame.setVisible(true);177178// Wait until the component's been painted179synchronized (test) {180while (!done) {181try {182test.wait();183} catch (InterruptedException e) {184throw new RuntimeException("Failed: Interrupted");185}186}187}188189GraphicsConfiguration gc = frame.getGraphicsConfiguration();190if (gc.getColorModel() instanceof IndexColorModel) {191System.out.println("IndexColorModel detected: " +192"test considered PASSED");193frame.dispose();194return;195}196197if (!show) {198frame.dispose();199}200if (capture == null) {201throw new RuntimeException("Failed: capture is null");202}203204// Test background color205int pixel = capture.getRGB(5, 5);206if (pixel != 0xffffffff) {207throw new RuntimeException("Failed: Incorrect color for " +208"background");209}210211// Test pixels212testRegion(capture, new Rectangle(10, 10, 40, 40));213testRegion(capture, new Rectangle(80, 10, 40, 40));214testRegion(capture, new Rectangle(150, 10, 40, 40));215}216}217218219