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  */
  23 import java.awt.Dimension;
  24 import java.awt.Graphics;
  25 import java.awt.Graphics2D;
  26 import java.awt.Image;
  27 import java.awt.image.BufferedImage;
  28 import javax.swing.JCheckBox;
  29 import javax.swing.JComponent;
  30 import javax.swing.SwingUtilities;
  31 
  32 import jdk.test.lib.Platform;
  33 
  34 /* @test
  35  * @bug 8032667
  36  * @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage
  37  * @library /test/lib
  38  * @build jdk.test.lib.Platform
  39  * @run main bug8032667_image_diff
  40  */
  41 public class bug8032667_image_diff {
  42 
  43     static final int IMAGE_WIDTH = 130;
  44     static final int IMAGE_HEIGHT = 50;
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         if (!Platform.isOSX()) {
  49             return;
  50         }
  51 
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             @Override
  54             public void run() {
  55 
  56                 JCheckBox checkBox = new JCheckBox();
  57                 checkBox.setSelected(true);
  58                 checkBox.setSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));
  59 
  60                 final BufferedImage image1 = getHiDPIImage(checkBox);
  61                 final BufferedImage image2 = getScaledImage(checkBox);
  62 
  63                 if(equal(image1, image2)){
  64                     throw new RuntimeException("2x image equals to non smooth image");
  65                 }
  66             }
  67         });
  68     }
  69 
  70     static boolean equal(BufferedImage image1, BufferedImage image2) {
  71 
  72         int w = image1.getWidth();
  73         int h = image1.getHeight();
  74 
  75         if (w != image2.getWidth() || h != image2.getHeight()) {
  76             return false;
  77         }
  78 
  79         for (int i = 0; i < w; i++) {
  80             for (int j = 0; j < h; j++) {
  81                 int color1 = image1.getRGB(i, j);
  82                 int color2 = image2.getRGB(i, j);
  83 
  84                 if (color1 != color2) {
  85                     return false;
  86                 }
  87             }
  88         }
  89         return true;
  90     }
  91 
  92     static BufferedImage getHiDPIImage(JComponent component) {
  93         return getImage(component, 2, IMAGE_WIDTH, IMAGE_HEIGHT);
  94     }
  95 
  96     static BufferedImage getScaledImage(JComponent component) {
  97         Image image1x = getImage(component, 1, IMAGE_WIDTH, IMAGE_HEIGHT);
  98         final BufferedImage image2x = new BufferedImage(
  99                 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
 100         final Graphics g = image2x.getGraphics();
 101         ((Graphics2D) g).scale(2, 2);
 102         g.drawImage(image1x, 0, 0, null);
 103         g.dispose();
 104         return image2x;
 105     }
 106 
 107     static BufferedImage getImage(JComponent component, int scale, int width, int height) {
 108         final BufferedImage image = new BufferedImage(
 109                 scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
 110         final Graphics g = image.getGraphics();
 111         ((Graphics2D) g).scale(scale, scale);
 112         component.paint(g);
 113         g.dispose();
 114         return image;
 115     }
 116 }