Path: blob/master/test/jdk/sun/java2d/pipe/InterpolationQualityTest.java
41152 views
/*1* Copyright (c) 2012, 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*/2223/*24* @test25* @key headful26* @bug 7188093 8000176 819861327* @summary Tests each of the 3 possible methods for rendering an upscaled28* image via rendering hints for default, xrender and opengl pipelines.29*30* @run main/othervm -Dsun.java2d.uiScale=1 -Dsun.java2d.xrender=false InterpolationQualityTest31* @run main/othervm -Dsun.java2d.uiScale=1 -Dsun.java2d.xrender=True InterpolationQualityTest32* @run main/othervm -Dsun.java2d.uiScale=1 -Dsun.java2d.d3d=false InterpolationQualityTest33* @run main/othervm -Dsun.java2d.uiScale=1 -Dsun.java2d.d3d=True InterpolationQualityTest34* @run main/othervm -Dsun.java2d.uiScale=1 InterpolationQualityTest35*/3637import java.awt.*;38import java.awt.image.*;39import java.io.File;40import java.io.IOException;41import javax.imageio.ImageIO;4243public class InterpolationQualityTest {4445private static final int testSize = 4, scaleFactor = 20, tolerance = 3;46private static final int sw = testSize * scaleFactor;47private static final int sh = testSize * scaleFactor;4849private Image testImage;50private VolatileImage vImg;5152public InterpolationQualityTest() {53testImage = createTestImage();54}5556private Image createTestImage() {57BufferedImage bi = new BufferedImage(testSize, testSize, BufferedImage.TYPE_INT_ARGB);58Graphics2D g = bi.createGraphics();59g.setColor(Color.BLACK);60g.fillRect(0, 0, testSize, testSize);61for (int i = 0; i < testSize; i++) {62bi.setRGB(i, i, Color.WHITE.getRGB());63}64return bi;65}6667private BufferedImage createReferenceImage(Object hint) {68BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB);69Graphics2D g2d = bi.createGraphics();70drawImage(g2d, hint);71return bi;72}7374private void drawImage(Graphics2D g2d, Object hint) {75g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);76g2d.drawImage(testImage, 0, 0, sw, sh, null);77}7879private GraphicsConfiguration getDefaultGC() {80return GraphicsEnvironment.getLocalGraphicsEnvironment().81getDefaultScreenDevice().getDefaultConfiguration();82}8384private void createVImg() {85vImg = getDefaultGC().createCompatibleVolatileImage(sw, sh);86}8788private void renderOffscreen(Object hint) {89Graphics2D g = vImg.createGraphics();90drawImage(g, hint);91g.dispose();92}9394private BufferedImage renderImage(Object hint) {95BufferedImage snapshot;96createVImg();97renderOffscreen(hint);9899do {100int status = vImg.validate(getDefaultGC());101if (status != VolatileImage.IMAGE_OK) {102if (status == VolatileImage.IMAGE_INCOMPATIBLE) {103createVImg();104}105renderOffscreen(hint);106}107snapshot = vImg.getSnapshot();108} while (vImg.contentsLost());109vImg.flush();110return snapshot;111}112113private boolean compareComponent(int comp1, int comp2) {114return Math.abs(comp1 - comp2) <= tolerance;115}116117private boolean compareRGB(int rgb1, int rgb2) {118Color col1 = new Color(rgb1);119Color col2 = new Color(rgb2);120return compareComponent(col1.getRed(), col2.getRed()) &&121compareComponent(col1.getBlue(), col2.getBlue()) &&122compareComponent(col1.getGreen(), col2.getGreen()) &&123compareComponent(col1.getAlpha(), col2.getAlpha());124}125126private boolean compareImages(BufferedImage img, BufferedImage ref, String imgName) {127for (int y = 0; y < ref.getHeight(); y++) {128for (int x = 0; x < ref.getWidth(); x++) {129if (!compareRGB(ref.getRGB(x, y), img.getRGB(x, y))) {130System.out.println(imgName + ".getRGB(" + x + ", " + y + ") = "131+ new Color(img.getRGB(x, y)) + " != "132+ new Color(ref.getRGB(x, y)));133return false;134}135}136}137return true;138}139140private boolean test(Object hint) {141BufferedImage refImage = createReferenceImage(hint);142BufferedImage resImage = renderImage(hint);143144boolean passed = compareImages(resImage, refImage, "resImage");145System.out.println(getHintName(hint) + (passed ? " passed." : " failed."));146if (!passed) {147dumpImage(refImage, "out_" + getHintName(hint) + "_ref.png");148dumpImage(resImage, "out_" + getHintName(hint) + ".png");149}150return passed;151}152153public void test() {154boolean passed = true;155passed &= test(RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);156passed &= test(RenderingHints.VALUE_INTERPOLATION_BILINEAR);157passed &= test(RenderingHints.VALUE_INTERPOLATION_BICUBIC);158if (passed) {159System.out.println("Test PASSED.");160} else {161throw new RuntimeException("Test FAILED.");162}163}164165private String getHintName(Object hint) {166if (hint == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {167return "nearest";168}169else if (hint == RenderingHints.VALUE_INTERPOLATION_BILINEAR) {170return "bilinear";171}172else if (hint == RenderingHints.VALUE_INTERPOLATION_BICUBIC) {173return "bicubic";174}175else {176return "null";177}178}179180private void dumpImage(BufferedImage bi, String name) {181try {182ImageIO.write(bi, "PNG", new File(name));183} catch (IOException ex) {184}185}186187public static void main(String[] argv) {188InterpolationQualityTest test = new InterpolationQualityTest();189test.test();190}191}192193194