Path: blob/master/test/jdk/sun/java2d/SunGraphics2D/DrawImageBilinear.java
41152 views
/*1* Copyright (c) 2007, 2020, 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* @key headful25* @bug 5009033 6603000 6666362 8159142 819861326* @summary Verifies that images transformed with bilinear filtering do not27* leave artifacts at the edges.28* @run main/othervm -Dsun.java2d.uiScale=2.5 DrawImageBilinear29* @run main/othervm -Dsun.java2d.uiScale=2.5 -Dsun.java2d.d3d=false DrawImageBilinear30* @author campbelc31*/3233import java.awt.Canvas;34import java.awt.Color;35import java.awt.Component;36import java.awt.Dimension;37import java.awt.Frame;38import java.awt.Graphics;39import java.awt.Graphics2D;40import java.awt.GraphicsConfiguration;41import java.awt.Point;42import java.awt.Rectangle;43import java.awt.RenderingHints;44import java.awt.Robot;45import java.awt.Toolkit;46import java.awt.image.BufferedImage;47import java.awt.image.IndexColorModel;48import java.awt.image.VolatileImage;4950public class DrawImageBilinear extends Canvas {5152private static final int SIZE = 5;5354private static boolean done;55private BufferedImage bimg1, bimg2;56private VolatileImage vimg;57private static volatile BufferedImage capture;58private static void doCapture(Component test) {59try {60Thread.sleep(2000);61} catch (InterruptedException ex) {}62// Grab the screen region63try {64Robot robot = new Robot();65Point pt1 = test.getLocationOnScreen();66Rectangle rect =67new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());68capture = robot.createScreenCapture(rect);69} catch (Exception e) {70e.printStackTrace();71}72}7374private void renderPattern(Graphics g) {75g.setColor(Color.red);76g.fillRect(0, 0, SIZE, SIZE);77//g.setColor(Color.green);78//g.drawRect(0, 0, SIZE-1, SIZE-1);79g.dispose();80}8182public void paint(Graphics g) {83Graphics2D g2d = (Graphics2D)g;8485if (bimg1 == null) {86bimg1 = (BufferedImage)createImage(SIZE, SIZE);87bimg1.setAccelerationPriority(0.0f);88renderPattern(bimg1.createGraphics());8990bimg2 = (BufferedImage)createImage(SIZE, SIZE);91renderPattern(bimg2.createGraphics());9293vimg = createVolatileImage(SIZE, SIZE);94vimg.validate(getGraphicsConfiguration());95renderPattern(vimg.createGraphics());96}9798do {99g2d.setColor(Color.white);100g2d.fillRect(0, 0, getWidth(), getHeight());101102g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,103RenderingHints.VALUE_INTERPOLATION_BILINEAR);104105// first time will be a sw->surface blit106g2d.drawImage(bimg1, 10, 10, 40, 40, null);107108// second time will be a texture->surface blit109g2d.drawImage(bimg2, 80, 10, 40, 40, null);110if (!skipOglTextureTest) {111g2d.drawImage(bimg2, 80, 10, 40, 40, null);112}113114// third time will be a pbuffer->surface blit115if (vimg.validate(getGraphicsConfiguration()) != VolatileImage.IMAGE_OK) {116renderPattern(vimg.createGraphics());117}118g2d.drawImage(vimg, 150, 10, 40, 40, null);119120Toolkit.getDefaultToolkit().sync();121} while (vimg.contentsLost());122123synchronized (this) {124if (!done) {125doCapture(this);126done = true;127}128notifyAll();129}130}131132public Dimension getPreferredSize() {133return new Dimension(200, 100);134}135136private static void testRegion(BufferedImage bi,137Rectangle affectedRegion)138{139int x1 = affectedRegion.x;140int y1 = affectedRegion.y;141int x2 = x1 + affectedRegion.width;142int y2 = y1 + affectedRegion.height;143144for (int y = y1; y < y2; y++) {145for (int x = x1; x < x2; x++) {146int actual = bi.getRGB(x, y);147if ((actual != 0xfffe0000) && (actual != 0xffff0000)) {148throw new RuntimeException("Test failed at x="+x+" y="+y+149" (expected=0xffff0000"+150" actual=0x"+151Integer.toHexString(actual) +152")");153}154}155}156}157158private static boolean skipOglTextureTest = false;159160public static void main(String[] args) {161boolean show = false;162for (String arg : args) {163if ("-show".equals(arg)) {164show = true;165}166}167168String arch = System.getProperty("os.arch");169boolean isOglEnabled = Boolean.getBoolean("sun.java2d.opengl");170skipOglTextureTest = false;171System.out.println("Skip OpenGL texture test: " + skipOglTextureTest);172173DrawImageBilinear test = new DrawImageBilinear();174Frame frame = new Frame();175frame.add(test);176frame.pack();177frame.setVisible(true);178179// Wait until the component's been painted180synchronized (test) {181while (!done) {182try {183test.wait();184} catch (InterruptedException e) {185throw new RuntimeException("Failed: Interrupted");186}187}188}189190GraphicsConfiguration gc = frame.getGraphicsConfiguration();191if (gc.getColorModel() instanceof IndexColorModel) {192System.out.println("IndexColorModel detected: " +193"test considered PASSED");194frame.dispose();195return;196}197198if (!show) {199frame.dispose();200}201if (capture == null) {202throw new RuntimeException("Failed: capture is null");203}204205// Test background color206int pixel = capture.getRGB(5, 5);207if (pixel != 0xffffffff) {208throw new RuntimeException("Failed: Incorrect color for " +209"background");210}211212// Test pixels213testRegion(capture, new Rectangle(10, 10, 40, 40));214testRegion(capture, new Rectangle(80, 10, 40, 40));215testRegion(capture, new Rectangle(150, 10, 40, 40));216}217}218219220