/* * Copyright (c) 2016, 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.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BaseMultiResolutionImage; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.awt.image.ImageFilter; import java.awt.image.ImageProducer; import java.awt.image.MultiResolutionImage; import java.awt.image.ReplicateScaleFilter; import java.util.HashSet; import java.util.Set; import java.util.List; /* * @test * @bug 8152309 * @summary Seamless way of using image filters with multi-resolution images */ public class ScaledFilterTest { private final static int IMAGE_SIZE = 500; private final static int X = 100; private final static int Y = 200; private final static int W = 300; private final static int H = 200; private final static int B = 10; private final static Color COLOR_BACKGROUND = Color.PINK; private final static Color COLOR_BORDER = Color.ORANGE; private final static Color COLOR_1X = Color.GREEN; private final static Color COLOR_2X = Color.BLUE; public static void main(String[] args) throws Exception { new CropFilterTest().test(); new ReplicateScaleFilterTest().test(); } static abstract class FilterTest { abstract ImageFilter createFilter(); abstract void checkImage(Image image); void test() { Image image = getFilteredImage(getMRImage()); checkImage(image); } Image getFilteredImage(Image image) { Toolkit toolkit = Toolkit.getDefaultToolkit(); ImageProducer filteredImageSource = new FilteredImageSource( image.getSource(), createFilter()); return toolkit.createImage(filteredImageSource); } BufferedImage drawToBufferedImage(Image image, int scale) { int w = scale * IMAGE_SIZE; int h = scale * IMAGE_SIZE; BufferedImage buffImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = buffImage.createGraphics(); g2d.scale(scale, scale); g2d.drawImage(image, 0, 0, null); g2d.dispose(); return buffImage; } Image getMRImage() { return new BaseMultiResolutionImage( new BufferedImage[]{ generateImage(1, COLOR_1X), generateImage(2, COLOR_2X)} ); } BufferedImage generateImage(int scale, Color color) { int size = IMAGE_SIZE * scale; BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); Graphics2D g = img.createGraphics(); g.scale(scale, scale); g.setColor(COLOR_BACKGROUND); g.fillRect(0, 0, IMAGE_SIZE, IMAGE_SIZE); g.setColor(COLOR_BORDER); g.fillRect(X, Y, W, H); g.setColor(color); g.fillRect(X + B, Y + B, W - 2 * B, H - 2 * B); g.dispose(); return img; } } static class CropFilterTest extends FilterTest { @Override ImageFilter createFilter() { return new CropImageFilter(X, Y, W, H); } @Override void checkImage(Image image) { checkImage(drawToBufferedImage(image, 1), 1); checkImage(drawToBufferedImage(image, 2), 2); } void checkImage(BufferedImage buffImage, int scale) { int fw = scale * W; int fh = scale * H; Set colors = new HashSet<>(); for (int i = 0; i < fw; i++) { colors.add(buffImage.getRGB(i, fh / 2)); } checkColor(colors, scale); colors.clear(); for (int i = 0; i < fh; i++) { colors.add(buffImage.getRGB(fw / 2, i)); } checkColor(colors, scale); } void checkColor(Set colors, int scale) { if (colors.size() != 2) { throw new RuntimeException("Wrong number of colors!"); } if (!colors.contains(COLOR_BORDER.getRGB())) { throw new RuntimeException("Color is not painted!"); } Color testColor = scale == 1 ? COLOR_1X : COLOR_2X; if (!colors.contains(testColor.getRGB())) { throw new RuntimeException("Color is not painted!"); } } } static class ReplicateScaleFilterTest extends FilterTest { final int DST_WIDTH = IMAGE_SIZE / 2; final int DST_HEIGHT = IMAGE_SIZE / 3; @Override ImageFilter createFilter() { return new ReplicateScaleFilter(DST_WIDTH, DST_HEIGHT); } @Override void checkImage(Image image) { if (!(image instanceof MultiResolutionImage)) { throw new RuntimeException("Not multi-resoltion image"); } MultiResolutionImage mrImage = (MultiResolutionImage) image; List resoltionVariants = mrImage.getResolutionVariants(); Image lowResolution = resoltionVariants.get(0); int w = lowResolution.getWidth(null); int h = lowResolution.getHeight(null); if (w != DST_WIDTH || h != DST_HEIGHT) { throw new RuntimeException("Wrong low-resolution size!"); } Image highResolution = resoltionVariants.get(1); w = highResolution.getWidth(null); h = highResolution.getHeight(null); if (w != 2 * DST_WIDTH || h != 2 * DST_HEIGHT) { throw new RuntimeException("Wrong high-resolution size!"); } BufferedImage buffImage = drawToBufferedImage(image, 1); int x = (X + W / 2) * DST_WIDTH / IMAGE_SIZE; int y = (Y + H / 2) * DST_HEIGHT / IMAGE_SIZE; if (buffImage.getRGB(x, y) != COLOR_1X.getRGB()) { throw new RuntimeException("Wrong low-resolution color!"); } buffImage = drawToBufferedImage(image, 2); if (buffImage.getRGB(2 * x, 2 * y) != COLOR_2X.getRGB()) { throw new RuntimeException("Wrong high-resolution color!"); } } } }