1 /*
   2  * Copyright (c) 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.Graphics;
  29 import java.awt.geom.Dimension2D;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.ImageObserver;
  32 import java.util.Arrays;
  33 import java.util.List;
  34 import java.util.function.Function;
  35 import java.util.function.BiFunction;
  36 import java.util.stream.Collectors;
  37 import java.awt.image.MultiResolutionImage;
  38 
  39 public class MultiResolutionBufferedImage extends BufferedImage
  40         implements MultiResolutionImage {
  41 
  42     private final BiFunction<Integer, Integer, Image> mapper;
  43     private final Dimension2D[] sizes;
  44     private final ImageResolutionHelper imageResolutionHelper;
  45     private int availableInfo;
  46 
  47     public MultiResolutionBufferedImage(Image baseImage,
  48             Dimension2D[] sizes, BiFunction<Integer, Integer, Image> mapper) {
  49         this(baseImage, sizes, mapper, ImageResolutionHelperType.getPlatformHelper());
  50     }
  51 
  52     public MultiResolutionBufferedImage(Image baseImage,
  53             Dimension2D[] sizes, BiFunction<Integer, Integer, Image> mapper,
  54             ImageResolutionHelper imageResolutionHelper) {
  55         super(baseImage.getWidth(null), baseImage.getHeight(null),
  56                 BufferedImage.TYPE_INT_ARGB_PRE);
  57         this.sizes = sizes;
  58         this.mapper = mapper;
  59         this.availableInfo = getInfo(baseImage);
  60         this.imageResolutionHelper = imageResolutionHelper;
  61         Graphics g = getGraphics();
  62         g.drawImage(baseImage, 0, 0, null);
  63         g.dispose();
  64     }
  65 
  66     @Override
  67     public Image getResolutionVariant(float logicalDPIX, float logicalDPIY,
  68             float baseWidth, float baseHeight, float destWidth, float destHeight) {
  69 
  70         float hiDPIWidth = imageResolutionHelper.getHiDPISize(logicalDPIX,
  71                 baseWidth, destWidth);
  72         float hiDPIHeight = imageResolutionHelper.getHiDPISize(logicalDPIY,
  73                 baseHeight, destHeight);
  74         return getResolutionVariant((int) hiDPIWidth, (int) hiDPIHeight);
  75     }
  76 
  77     private Image getResolutionVariant(int width, int height) {
  78         int baseWidth = getWidth();
  79         int baseHeight = getHeight();
  80 
  81         if (baseWidth == width && baseHeight == height) {
  82             return this;
  83         }
  84 
  85         ImageCache cache = ImageCache.getInstance();
  86         ImageCacheKey key = new ImageCacheKey(this, width, height);
  87         Image resolutionVariant = cache.getImage(key);
  88         if (resolutionVariant == null) {
  89             resolutionVariant = mapper.apply(width, height);
  90             cache.setImage(key, resolutionVariant);
  91             preload(resolutionVariant, availableInfo);
  92         }
  93 
  94         return resolutionVariant;
  95     }
  96 
  97     @Override
  98     public List<Image> getResolutionVariants() {
  99         return Arrays.stream(sizes).map((Function<Dimension2D, Image>) size
 100                 -> getResolutionVariant((int) size.getWidth(),
 101                         (int) size.getHeight())).collect(Collectors.toList());
 102     }
 103 
 104     public MultiResolutionBufferedImage map(Function<Image, Image> mapper) {
 105         return new MultiResolutionBufferedImage(mapper.apply(this), sizes,
 106                 (width, height) ->
 107                         mapper.apply(getResolutionVariant(width, height)));
 108     }
 109 
 110     @Override
 111     public int getWidth(ImageObserver observer) {
 112         availableInfo |= ImageObserver.WIDTH;
 113         return super.getWidth(observer);
 114     }
 115 
 116     @Override
 117     public int getHeight(ImageObserver observer) {
 118         availableInfo |= ImageObserver.HEIGHT;
 119         return super.getHeight(observer);
 120     }
 121 
 122     @Override
 123     public Object getProperty(String name, ImageObserver observer) {
 124         availableInfo |= ImageObserver.PROPERTIES;
 125         return super.getProperty(name, observer);
 126     }
 127 
 128     private static int getInfo(Image image) {
 129         if (image instanceof ToolkitImage) {
 130             return ((ToolkitImage) image).getImageRep().check(
 131                     (img, infoflags, x, y, w, h) -> false);
 132         }
 133         return 0;
 134     }
 135 
 136     private static void preload(Image image, int availableInfo) {
 137         if (image instanceof ToolkitImage) {
 138             ((ToolkitImage) image).preload(new ImageObserver() {
 139                 int flags = availableInfo;
 140 
 141                 @Override
 142                 public boolean imageUpdate(Image img, int infoflags,
 143                         int x, int y, int width, int height) {
 144                     flags &= ~infoflags;
 145                     return (flags != 0) && ((infoflags
 146                             & (ImageObserver.ERROR | ImageObserver.ABORT)) == 0);
 147                 }
 148             });
 149         }
 150     }
 151 
 152     private static class ImageCacheKey implements ImageCache.PixelsKey {
 153 
 154         private final int pixelCount;
 155         private final int hash;
 156 
 157         private final int w;
 158         private final int h;
 159         private final Image baseImage;
 160 
 161         ImageCacheKey(final Image baseImage,
 162                 final int w, final int h) {
 163             this.baseImage = baseImage;
 164             this.w = w;
 165             this.h = h;
 166             this.pixelCount = w * h;
 167             hash = hash();
 168         }
 169 
 170         @Override
 171         public int getPixelCount() {
 172             return pixelCount;
 173         }
 174 
 175         private int hash() {
 176             int hash = baseImage.hashCode();
 177             hash = 31 * hash + w;
 178             hash = 31 * hash + h;
 179             return hash;
 180         }
 181 
 182         @Override
 183         public int hashCode() {
 184             return hash;
 185         }
 186 
 187         @Override
 188         public boolean equals(Object obj) {
 189             if (obj instanceof ImageCacheKey) {
 190                 ImageCacheKey key = (ImageCacheKey) obj;
 191                 return baseImage == key.baseImage && w == key.w && h == key.h;
 192             }
 193             return false;
 194         }
 195     }
 196 }