/* * 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.ImageConsumer; import java.awt.image.ImageObserver; import java.awt.image.ImageProducer; import java.awt.image.MultiResolutionImage; import java.awt.image.ResolutionVariantItem; import java.util.List; import java.util.stream.Collectors; import sun.misc.SoftCache; public class MultiResolutionToolkitImage extends ToolkitImage implements MultiResolutionImage { List> resolutionVariants; public MultiResolutionToolkitImage(ImageProducer mriProducer) { super(mriProducer); this.resolutionVariants = mriProducer.getResolutionVariantItems().stream() .map((item) -> new ResolutionVariantItem( new ToolkitImage(item.getValue()), item.getScaleX(), item.getScaleY())) .collect(Collectors.toList()); } public MultiResolutionToolkitImage( List> resolutionVariants) { super(resolutionVariants.get(0).getValue().getSource()); this.resolutionVariants = resolutionVariants; } @Override public Image getResolutionVariant(double destWidth, double destHeight) { return getResolutionVariantItem(destWidth, destHeight).getValue(); } 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); } 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.getValue()) .collect(Collectors.toList()); } public List> getResolutionVariantItems() { return resolutionVariants; } @Override public ImageProducer getSource() { return new MultiResolutionImageProducer(resolutionVariants.stream() .map(rvItem -> new ResolutionVariantItem<>( rvItem.getValue().getSource(), rvItem.getScaleX(), rvItem.getScaleY())) .collect(Collectors.toList())); } 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 ResolutionVariantItem<>(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.floor(width / scaleX); } if ((flags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) { height = (int) Math.floor(height / scaleY); } if ((flags & BITS_INFO) != 0) { x = (int) Math.ceil(x / scaleX); y = (int) Math.ceil(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 MultiResolutionImageProducer implements ImageProducer { private final ImageProducer baseImageProducer; private final List> rvProducers; public MultiResolutionImageProducer( List> rvProducers) { this.baseImageProducer = rvProducers.get(0).getValue(); this.rvProducers = rvProducers; } @Override public void addConsumer(ImageConsumer ic) { getBaseImageProducer().addConsumer(ic); } @Override public boolean isConsumer(ImageConsumer ic) { return getBaseImageProducer().isConsumer(ic); } @Override public void removeConsumer(ImageConsumer ic) { getBaseImageProducer().removeConsumer(ic); } @Override public void startProduction(ImageConsumer ic) { getBaseImageProducer().startProduction(ic); } @Override public void requestTopDownLeftRightResend(ImageConsumer ic) { getBaseImageProducer().requestTopDownLeftRightResend(ic); } private ImageProducer getBaseImageProducer() { return baseImageProducer; } @Override public boolean isMultiResolutionImageProducer() { return true; } @Override public List> getResolutionVariantItems() { return rvProducers; } } }