/* * 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. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. */ package sun.awt.image; import java.awt.Image; import java.awt.image.ImageObserver; import java.awt.image.MultiResolutionImage; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; import sun.awt.SoftCache; public class MultiResolutionToolkitImage extends ToolkitImage implements MultiResolutionImage { final List resolutionVariants; public MultiResolutionToolkitImage(Image baseImage, List resolutionVariants) { super(baseImage.getSource()); this.resolutionVariants = resolutionVariants; } @Override public Image getResolutionVariant(double destWidth, double destHeight) { return getResolutionVariantItem(destWidth, destHeight).getImage(); } public ResolutionVariantItem getResolutionVariantItem( double destWidth, double destHeight) { checkSize(destWidth, destHeight); int baseWidth = getWidth(); int baseHeight = getHeight(); for (ResolutionVariantItem rvItem : resolutionVariants) { double sx = rvItem.getScaleX(); double sy = rvItem.getScaleY(); if (destWidth <= baseWidth * sx && destHeight <= baseHeight * sy) { return rvItem; } } return resolutionVariants.get(resolutionVariants.size() - 1); } public static Image map(MultiResolutionToolkitImage mrImage, Function mapper) { Image baseImage = mapper.apply(mrImage); List rvItems = mrImage.resolutionVariants.stream() .map(new RVItemMapper(mapper)) .collect(Collectors.toList()); return new MultiResolutionToolkitImage(baseImage, rvItems); } private static void checkSize(double width, double height) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException(String.format( "Width (%s) or height (%s) cannot be <= 0", width, height)); } if (!Double.isFinite(width) || !Double.isFinite(height)) { throw new IllegalArgumentException(String.format( "Width (%s) or height (%s) is not finite", width, height)); } } @Override public List getResolutionVariants() { return resolutionVariants.stream() .map(rvItem -> rvItem.getImage()) .collect(Collectors.toList()); } public List getResolutionVariantItems() { return resolutionVariants; } private static final int BITS_INFO = ImageObserver.SOMEBITS | ImageObserver.FRAMEBITS | ImageObserver.ALLBITS; private static class ObserverCache { @SuppressWarnings("deprecation") static final SoftCache INSTANCE = new SoftCache(); } public static ImageObserver getResolutionVariantObserver( final Image image, final ImageObserver observer, final double scaleX, final double scaleY, boolean concatenateInfo) { if (observer == null) { return null; } synchronized (ObserverCache.INSTANCE) { Object key = new ObserverKey(observer, scaleX, scaleY); ImageObserver o = (ImageObserver) ObserverCache.INSTANCE.get(key); if (o == null) { o = (Image resolutionVariant, int flags, int x, int y, int width, int height) -> { if ((flags & (ImageObserver.WIDTH | BITS_INFO)) != 0) { width = (int) Math.ceil(width / scaleX); } if ((flags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) { height = (int) Math.ceil(height / scaleY); } if ((flags & BITS_INFO) != 0) { x = (int) Math.floor(x / scaleX); y = (int) Math.floor(y / scaleY); } if(concatenateInfo){ flags &= ((ToolkitImage) image). getImageRep().check(null); } return observer.imageUpdate( image, flags, x, y, width, height); }; ObserverCache.INSTANCE.put(key, o); } return o; } } public static class ResolutionVariantItem { final Image image; final double scaleX; final double scaleY; public ResolutionVariantItem(Image image, double scaleX, double scaleY) { this.image = image; this.scaleX = scaleX; this.scaleY = scaleY; } public Image getImage() { return image; } public double getScaleX() { return scaleX; } public double getScaleY() { return scaleY; } public ResolutionVariantItem map(Function mapper) { return new ResolutionVariantItem(mapper.apply(image), scaleX, scaleY); } } static class RVItemMapper implements Function { final Function mapper; public RVItemMapper(Function mapper) { this.mapper = mapper; } @Override public ResolutionVariantItem apply(ResolutionVariantItem rvItem) { return new ResolutionVariantItem(mapper.apply(rvItem.getImage()), rvItem.getScaleX(), rvItem.getScaleY()); } } static class ObserverKey { final ImageObserver observer; final double scaleX; final double scaleY; public ObserverKey(ImageObserver observer, double scaleX, double scaleY) { this.observer = observer; this.scaleX = scaleX; this.scaleY = scaleY; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof ObserverKey)) { return false; } ObserverKey observerKey = (ObserverKey) obj; return scaleX == observerKey.scaleX && scaleY == observerKey.scaleY && observer.equals(observerKey.observer); } @Override public int hashCode() { int hash = observer.hashCode(); hash = 31 * hash + Double.hashCode(scaleX); hash = 31 * hash + Double.hashCode(scaleY); return hash; } } }