1 /* 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.awt.image; 26 27 import java.awt.Image; 28 import java.awt.image.ImageConsumer; 29 import java.awt.image.ImageObserver; 30 import java.awt.image.ImageProducer; 31 import java.awt.image.MultiResolutionImage; 32 import java.awt.image.ResolutionVariantItem; 33 import java.util.List; 34 import java.util.stream.Collectors; 35 import sun.misc.SoftCache; 36 37 public class MultiResolutionToolkitImage extends ToolkitImage implements MultiResolutionImage { 38 39 List<ResolutionVariantItem<Image>> resolutionVariants; 40 41 public MultiResolutionToolkitImage(ImageProducer mriProducer) { 42 super(mriProducer); 43 this.resolutionVariants = mriProducer.getResolutionVariantItems().stream() 44 .map((item) -> new ResolutionVariantItem<Image>( 45 new ToolkitImage(item.getValue()), 46 item.getScaleX(), 47 item.getScaleY())) 48 .collect(Collectors.toList()); 49 } 50 51 public MultiResolutionToolkitImage( 52 List<ResolutionVariantItem<Image>> resolutionVariants) { 53 super(resolutionVariants.get(0).getValue().getSource()); 54 this.resolutionVariants = resolutionVariants; 55 } 56 57 58 @Override 59 public Image getResolutionVariant(double destWidth, double destHeight) { 60 return getResolutionVariantItem(destWidth, destHeight).getValue(); 61 } 62 63 public ResolutionVariantItem<Image> getResolutionVariantItem( 64 double destWidth, double destHeight) { 65 checkSize(destWidth, destHeight); 66 int baseWidth = getWidth(); 67 int baseHeight = getHeight(); 68 69 for (ResolutionVariantItem<Image> rvItem : resolutionVariants) { 70 double sx = rvItem.getScaleX(); 71 double sy = rvItem.getScaleY(); 72 if (destWidth <= baseWidth * sx && destHeight <= baseHeight * sy) { 73 return rvItem; 74 } 75 } 76 77 return resolutionVariants.get(resolutionVariants.size() - 1); 78 } 79 80 81 private static void checkSize(double width, double height) { 82 if (width <= 0 || height <= 0) { 83 throw new IllegalArgumentException(String.format( 84 "Width (%s) or height (%s) cannot be <= 0", width, height)); 85 } 86 87 if (!Double.isFinite(width) || !Double.isFinite(height)) { 88 throw new IllegalArgumentException(String.format( 89 "Width (%s) or height (%s) is not finite", width, height)); 90 } 91 } 92 93 @Override 94 public List<Image> getResolutionVariants() { 95 return resolutionVariants.stream() 96 .map(rvItem -> rvItem.getValue()) 97 .collect(Collectors.toList()); 98 } 99 100 public List<ResolutionVariantItem<Image>> getResolutionVariantItems() { 101 return resolutionVariants; 102 } 103 104 @Override 105 public ImageProducer getSource() { 106 return new MultiResolutionImageProducer(resolutionVariants.stream() 107 .map(rvItem -> new ResolutionVariantItem<>( 108 rvItem.getValue().getSource(), 109 rvItem.getScaleX(), 110 rvItem.getScaleY())) 111 .collect(Collectors.toList())); 112 } 113 114 private static final int BITS_INFO = ImageObserver.SOMEBITS 115 | ImageObserver.FRAMEBITS | ImageObserver.ALLBITS; 116 117 private static class ObserverCache { 118 119 @SuppressWarnings("deprecation") 120 static final SoftCache INSTANCE = new SoftCache(); 121 } 122 123 public static ImageObserver getResolutionVariantObserver( 124 final Image image, final ImageObserver observer, 125 final double scaleX, final double scaleY, 126 boolean concatenateInfo) { 127 128 if (observer == null) { 129 return null; 130 } 131 132 synchronized (ObserverCache.INSTANCE) { 133 134 Object key = new ResolutionVariantItem<>(observer, scaleX, scaleY); 135 ImageObserver o = (ImageObserver) ObserverCache.INSTANCE.get(key); 136 137 if (o == null) { 138 139 o = (Image resolutionVariant, int flags, 140 int x, int y, int width, int height) -> { 141 142 if ((flags & (ImageObserver.WIDTH | BITS_INFO)) != 0) { 143 width = (int) Math.floor(width / scaleX); 144 } 145 146 if ((flags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) { 147 height = (int) Math.floor(height / scaleY); 148 } 149 150 if ((flags & BITS_INFO) != 0) { 151 x = (int) Math.ceil(x / scaleX); 152 y = (int) Math.ceil(y / scaleY); 153 } 154 155 if(concatenateInfo){ 156 flags &= ((ToolkitImage) image). 157 getImageRep().check(null); 158 } 159 160 return observer.imageUpdate( 161 image, flags, x, y, width, height); 162 }; 163 164 ObserverCache.INSTANCE.put(key, o); 165 } 166 return o; 167 } 168 } 169 170 public static class MultiResolutionImageProducer implements ImageProducer { 171 172 private final ImageProducer baseImageProducer; 173 private final List<ResolutionVariantItem<ImageProducer>> rvProducers; 174 175 public MultiResolutionImageProducer( 176 List<ResolutionVariantItem<ImageProducer>> rvProducers) 177 { 178 this.baseImageProducer = rvProducers.get(0).getValue(); 179 this.rvProducers = rvProducers; 180 } 181 182 @Override 183 public void addConsumer(ImageConsumer ic) { 184 getBaseImageProducer().addConsumer(ic); 185 } 186 187 @Override 188 public boolean isConsumer(ImageConsumer ic) { 189 return getBaseImageProducer().isConsumer(ic); 190 } 191 192 @Override 193 public void removeConsumer(ImageConsumer ic) { 194 getBaseImageProducer().removeConsumer(ic); 195 } 196 197 @Override 198 public void startProduction(ImageConsumer ic) { 199 getBaseImageProducer().startProduction(ic); 200 } 201 202 @Override 203 public void requestTopDownLeftRightResend(ImageConsumer ic) { 204 getBaseImageProducer().requestTopDownLeftRightResend(ic); 205 } 206 207 private ImageProducer getBaseImageProducer() { 208 return baseImageProducer; 209 } 210 211 @Override 212 public boolean isMultiResolutionImageProducer() { 213 return true; 214 } 215 216 @Override 217 public List<ResolutionVariantItem<ImageProducer>> getResolutionVariantItems() { 218 return rvProducers; 219 } 220 } 221 }