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