bug8032667_image_diff.java revision 10730:07156012ab78
1/* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23import java.awt.Dimension; 24import java.awt.Graphics; 25import java.awt.Graphics2D; 26import java.awt.Image; 27import java.awt.image.BufferedImage; 28import javax.swing.JCheckBox; 29import javax.swing.JComponent; 30import javax.swing.SwingUtilities; 31import jdk.testlibrary.OSInfo; 32 33/* @test 34 * @bug 8032667 35 * @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage 36 * @library ../../../../lib/testlibrary 37 * @build jdk.testlibrary.OSInfo 38 * @run main bug8032667_image_diff 39 */ 40public class bug8032667_image_diff { 41 42 static final int IMAGE_WIDTH = 130; 43 static final int IMAGE_HEIGHT = 50; 44 45 public static void main(String[] args) throws Exception { 46 47 if(!OSInfo.OSType.MACOSX.equals(OSInfo.getOSType())){ 48 return; 49 } 50 51 SwingUtilities.invokeAndWait(new Runnable() { 52 @Override 53 public void run() { 54 55 JCheckBox checkBox = new JCheckBox(); 56 checkBox.setSelected(true); 57 checkBox.setSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT)); 58 59 final BufferedImage image1 = getHiDPIImage(checkBox); 60 final BufferedImage image2 = getScaledImage(checkBox); 61 62 if(equal(image1, image2)){ 63 throw new RuntimeException("2x image equals to non smooth image"); 64 } 65 } 66 }); 67 } 68 69 static boolean equal(BufferedImage image1, BufferedImage image2) { 70 71 int w = image1.getWidth(); 72 int h = image1.getHeight(); 73 74 if (w != image2.getWidth() || h != image2.getHeight()) { 75 return false; 76 } 77 78 for (int i = 0; i < w; i++) { 79 for (int j = 0; j < h; j++) { 80 int color1 = image1.getRGB(i, j); 81 int color2 = image2.getRGB(i, j); 82 83 if (color1 != color2) { 84 return false; 85 } 86 } 87 } 88 return true; 89 } 90 91 static BufferedImage getHiDPIImage(JComponent component) { 92 return getImage(component, 2, IMAGE_WIDTH, IMAGE_HEIGHT); 93 } 94 95 static BufferedImage getScaledImage(JComponent component) { 96 Image image1x = getImage(component, 1, IMAGE_WIDTH, IMAGE_HEIGHT); 97 final BufferedImage image2x = new BufferedImage( 98 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB); 99 final Graphics g = image2x.getGraphics(); 100 ((Graphics2D) g).scale(2, 2); 101 g.drawImage(image1x, 0, 0, null); 102 g.dispose(); 103 return image2x; 104 } 105 106 static BufferedImage getImage(JComponent component, int scale, int width, int height) { 107 final BufferedImage image = new BufferedImage( 108 scale * width, scale * height, BufferedImage.TYPE_INT_ARGB); 109 final Graphics g = image.getGraphics(); 110 ((Graphics2D) g).scale(scale, scale); 111 component.paint(g); 112 g.dispose(); 113 return image; 114 } 115} 116