test/java/awt/image/MultiResolutionImageTest.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 30,52 **** import java.io.File; import java.lang.reflect.Method; import java.net.URL; import javax.imageio.ImageIO; import sun.awt.OSInfo; - import sun.awt.SunHints; import java.awt.MediaTracker; import java.awt.geom.AffineTransform; import java.awt.image.ImageObserver; import java.util.Arrays; import java.util.List; import javax.swing.JPanel; import sun.awt.SunToolkit; ! import sun.awt.image.MultiResolutionImage; /** * @test ! * @bug 8011059 * @author Alexander Scherbatiy * @summary [macosx] Make JDK demos look perfect on retina displays * @run main MultiResolutionImageTest CUSTOM * @run main MultiResolutionImageTest TOOLKIT_PREPARE * @run main MultiResolutionImageTest TOOLKIT_LOAD --- 30,52 ---- import java.io.File; import java.lang.reflect.Method; import java.net.URL; import javax.imageio.ImageIO; import sun.awt.OSInfo; import java.awt.MediaTracker; + import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.ImageObserver; import java.util.Arrays; import java.util.List; import javax.swing.JPanel; import sun.awt.SunToolkit; ! import java.awt.image.MultiResolutionImage; /** * @test ! * @bug 8011059 8029339 * @author Alexander Scherbatiy * @summary [macosx] Make JDK demos look perfect on retina displays * @run main MultiResolutionImageTest CUSTOM * @run main MultiResolutionImageTest TOOLKIT_PREPARE * @run main MultiResolutionImageTest TOOLKIT_LOAD
*** 58,67 **** --- 58,69 ---- private static final int IMAGE_HEIGHT = 200; private static final Color COLOR_1X = Color.GREEN; private static final Color COLOR_2X = Color.BLUE; private static final String IMAGE_NAME_1X = "image.png"; private static final String IMAGE_NAME_2X = "image@2x.png"; + private static final AffineTransform IDENTITY + = AffineTransform.getScaleInstance(1, 1); public static void main(String[] args) throws Exception { System.out.println("args: " + args.length);
*** 200,218 **** g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X); g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); } @Override ! public Image getResolutionVariant(int width, int height) { return ((width <= getWidth() && height <= getHeight())) ? this : highResolutionImage; } @Override public List<Image> getResolutionVariants() { return Arrays.asList(this, highResolutionImage); } } static void testToolkitMultiResolutionImagePrepare() throws Exception { generateImages(); --- 202,233 ---- g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X); g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); } @Override ! public Image getResolutionVariant(float logicalDPIX, float logicalDPIY, ! float baseWidth, float baseHeight, float destWidth, float destHeight) { ! return getResolutionVariant(destWidth, destHeight); ! } ! ! public Image getResolutionVariant(float width, float height) { return ((width <= getWidth() && height <= getHeight())) ? this : highResolutionImage; } @Override public List<Image> getResolutionVariants() { return Arrays.asList(this, highResolutionImage); } + + static float scaleWidth(double size, AffineTransform t) { + return (float) (size * Math.hypot(t.getScaleX(), t.getShearY())); + } + + static float scaleHeight(double size, AffineTransform t) { + return (float) (size * Math.hypot(t.getShearX(), t.getScaleY())); + } } static void testToolkitMultiResolutionImagePrepare() throws Exception { generateImages();
*** 251,270 **** testImageLoaded(image); int w = image.getWidth(null); int h = image.getHeight(null); ! Image resolutionVariant = ((MultiResolutionImage) image) ! .getResolutionVariant(2 * w, 2 * h); if (image == resolutionVariant) { throw new RuntimeException("Resolution variant is not loaded"); } testImageLoaded(resolutionVariant); } static void testImageLoaded(Image image) { SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); int flags = toolkit.checkImage(image, IMAGE_WIDTH, IMAGE_WIDTH, new SilentImageObserver()); --- 266,293 ---- testImageLoaded(image); int w = image.getWidth(null); int h = image.getHeight(null); ! Image resolutionVariant = getOSDependentToolkitResolutionVariant( ! (MultiResolutionImage) image, w, h); if (image == resolutionVariant) { throw new RuntimeException("Resolution variant is not loaded"); } testImageLoaded(resolutionVariant); } + static Image getOSDependentToolkitResolutionVariant( + MultiResolutionImage mrImage, int w, int h) { + if (OSInfo.OSType.WINDOWS.equals(OSInfo.getOSType())) { + return mrImage.getResolutionVariant(2, 2, w, h, 2 * w, 2 * h); + } + return mrImage.getResolutionVariant(1, 1, w, h, 2 * w, 2 * h); + } + static void testImageLoaded(Image image) { SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); int flags = toolkit.checkImage(image, IMAGE_WIDTH, IMAGE_WIDTH, new SilentImageObserver());
*** 371,382 **** Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics(); setImageScalingHint(g1x, false); g1x.drawImage(image, 0, 0, null); checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false); ! Image resolutionVariant = ((MultiResolutionImage) image). ! getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT); if (resolutionVariant == null) { throw new RuntimeException("Resolution variant is null"); } --- 394,406 ---- Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics(); setImageScalingHint(g1x, false); g1x.drawImage(image, 0, 0, null); checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false); ! Image resolutionVariant = ((MultiResolutionImage) image) ! .getResolutionVariant(2, 2, IMAGE_WIDTH, IMAGE_HEIGHT, ! 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT); if (resolutionVariant == null) { throw new RuntimeException("Resolution variant is null"); }
*** 398,409 **** throw new RuntimeException("Not a MultiResolutionImage"); } MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image; ! Image image1x = multiResolutionImage.getResolutionVariant(IMAGE_WIDTH, IMAGE_HEIGHT); ! Image image2x = multiResolutionImage.getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT); if (image1x.getWidth(null) * 2 != image2x.getWidth(null) || image1x.getHeight(null) * 2 != image2x.getHeight(null)) { throw new RuntimeException("Wrong resolution variant size"); } --- 422,436 ---- throw new RuntimeException("Not a MultiResolutionImage"); } MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image; ! Image image1x = multiResolutionImage ! .getResolutionVariant(1, 1, IMAGE_WIDTH, IMAGE_HEIGHT, ! IMAGE_WIDTH, IMAGE_HEIGHT); ! Image image2x = getOSDependentToolkitResolutionVariant( ! multiResolutionImage, IMAGE_WIDTH, IMAGE_WIDTH); if (image1x.getWidth(null) * 2 != image2x.getWidth(null) || image1x.getHeight(null) * 2 != image2x.getHeight(null)) { throw new RuntimeException("Wrong resolution variant size"); }
*** 437,449 **** g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, observer); } static void setImageScalingHint(Graphics2D g2d, boolean enableImageScaling) { ! g2d.setRenderingHint(SunHints.KEY_RESOLUTION_VARIANT, enableImageScaling ! ? SunHints.VALUE_RESOLUTION_VARIANT_ON ! : SunHints.VALUE_RESOLUTION_VARIANT_OFF); } static void checkColor(int rgb, boolean isImageScaled) { if (!isImageScaled && COLOR_1X.getRGB() != rgb) { --- 464,476 ---- g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, observer); } static void setImageScalingHint(Graphics2D g2d, boolean enableImageScaling) { ! g2d.setRenderingHint(RenderingHints.KEY_RESOLUTION_VARIANT, enableImageScaling ! ? RenderingHints.VALUE_RESOLUTION_VARIANT_ON ! : RenderingHints.VALUE_RESOLUTION_VARIANT_OFF); } static void checkColor(int rgb, boolean isImageScaled) { if (!isImageScaled && COLOR_1X.getRGB() != rgb) {