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