1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* Copyright (c) 2013, 2015, 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import sun.awt.SunHints;
import java.awt.geom.AffineTransform;
import java.util.Arrays;
import java.util.List;
import java.awt.image.MultiResolutionImage;
/**
* @test
* @bug 8011059
* @author Alexander Scherbatiy
* @summary Test MultiResolution image loading and painting with various scaling
* combinations
* @modules java.desktop/sun.awt
* java.desktop/sun.awt.image
*/
public class MultiResolutionImageCommonTest {
private static final int IMAGE_WIDTH = 300;
private static final int IMAGE_HEIGHT = 200;
private static final Color COLOR_1X = Color.GREEN;
private static final Color COLOR_2X = Color.BLUE;
public static void main(String[] args) throws Exception {
testCustomMultiResolutionImage();
System.out.println("Test passed.");
}
public static void testCustomMultiResolutionImage() {
testCustomMultiResolutionImage(false);
testCustomMultiResolutionImage(true);
}
public static void testCustomMultiResolutionImage(
boolean enableImageScaling) {
Image image = new MultiResolutionBufferedImage();
// Same image size
BufferedImage bufferedImage = new BufferedImage(
IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.drawImage(image, 0, 0, null);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false);
// Twice image size
bufferedImage = new BufferedImage(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.drawImage(image, 0, 0, 2 * IMAGE_WIDTH,
2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
checkColor(bufferedImage.getRGB(3 * IMAGE_WIDTH / 2,
3 * IMAGE_HEIGHT / 2), enableImageScaling);
// Scale 2x
bufferedImage = new BufferedImage(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.scale(2, 2);
g2d.drawImage(image, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling);
// Rotate
bufferedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.drawImage(image, 0, 0, null);
g2d.rotate(Math.PI / 4);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false);
// Scale 2x and Rotate
bufferedImage = new BufferedImage(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.scale(-2, 2);
g2d.rotate(-Math.PI / 10);
g2d.drawImage(image, -IMAGE_WIDTH, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling);
// General Transform
bufferedImage = new BufferedImage(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
float delta = 0.05f;
float cos = 1 - delta * delta / 2;
float sin = 1 + delta;
AffineTransform transform
= new AffineTransform(2 * cos, 0.1, 0.3, -2 * sin, 10, -5);
g2d.setTransform(transform);
g2d.drawImage(image, 0, -IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT, null);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling);
int D = 10;
// From Source to small Destination region
bufferedImage = new BufferedImage(
IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.drawImage(image, IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2,
IMAGE_WIDTH - D, IMAGE_HEIGHT - D,
D, D, IMAGE_WIDTH - D, IMAGE_HEIGHT - D, null);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false);
// From Source to large Destination region
bufferedImage = new BufferedImage(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) bufferedImage.getGraphics();
setImageScalingHint(g2d, enableImageScaling);
g2d.drawImage(image, D, D, 2 * IMAGE_WIDTH - D, 2 * IMAGE_HEIGHT - D,
IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2,
IMAGE_WIDTH - D, IMAGE_HEIGHT - D, null);
checkColor(bufferedImage.getRGB(
3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling);
}
static class MultiResolutionBufferedImage extends BufferedImage
implements MultiResolutionImage {
Image highResolutionImage;
public MultiResolutionBufferedImage() {
super(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
highResolutionImage = new BufferedImage(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
draw(getGraphics(), 1);
draw(highResolutionImage.getGraphics(), 2);
}
final void draw(Graphics graphics, float resolution) {
Graphics2D g2 = (Graphics2D) graphics;
g2.scale(resolution, resolution);
g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X);
g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
}
@Override
public Image getResolutionVariant(
double destImageWidth, double destImageHeight) {
return ((destImageWidth <= getWidth() && destImageHeight <= getHeight()))
? this : highResolutionImage;
}
@Override
public List<Image> getResolutionVariants() {
return Arrays.asList(this, highResolutionImage);
}
}
static void setImageScalingHint(
Graphics2D g2d, boolean enableImageScaling) {
g2d.setRenderingHint(SunHints.KEY_RESOLUTION_VARIANT, enableImageScaling
? RenderingHints.VALUE_RESOLUTION_VARIANT_DEFAULT
: RenderingHints.VALUE_RESOLUTION_VARIANT_BASE);
}
static void checkColor(int rgb, boolean isImageScaled) {
if (!isImageScaled && COLOR_1X.getRGB() != rgb) {
throw new RuntimeException("Wrong 1x color: " + new Color(rgb));
}
if (isImageScaled && COLOR_2X.getRGB() != rgb) {
throw new RuntimeException("Wrong 2x color" + new Color(rgb));
}
}
}
|