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.util.Arrays;
  32 import java.util.List;
  33 import java.util.function.Function;
  34 import java.util.stream.Collectors;
  35 
  36 public class MultiResolutionBufferedImage extends BufferedImage
  37         implements MultiResolutionImage {
  38 
  39     private final MultiResolutionImageMapper mapper;
  40     private final Dimension2D[] sizes;
  41 
  42     public MultiResolutionBufferedImage(Image baseImage,
  43             Dimension2D[] sizes, MultiResolutionImageMapper mapper) {
  44         super(baseImage.getWidth(null), baseImage.getHeight(null),
  45                 BufferedImage.TYPE_INT_ARGB_PRE);
  46         this.sizes = sizes;
  47         this.mapper = mapper;
  48         Graphics g = getGraphics();
  49         g.drawImage(baseImage, 0, 0, null);
  50         g.dispose();
  51     }
  52 
  53     @Override
  54     public Image getResolutionVariant(int width, int height) {
  55         int baseWidth = getWidth();
  56         int baseHeight = getHeight();
  57 
  58         if (baseWidth == width && baseHeight == height) {
  59             return this;
  60         }
  61 
  62         ImageCache cache = ImageCache.getInstance();
  63         ImageCacheKey key = new ImageCacheKey(this, width, height);
  64         Image resolutionVariant = cache.getImage(key);
  65         if (resolutionVariant == null) {
  66             resolutionVariant = mapper.apply(width, height);
  67             cache.setImage(key, resolutionVariant);
  68         }
  69 
  70         return resolutionVariant;
  71     }
  72 
  73     @Override
  74     public List<Image> getResolutionVariants() {
  75         return Arrays.stream(sizes).map((Function<Dimension2D, Image>) size
  76                 -> getResolutionVariant((int) size.getWidth(),
  77                         (int) size.getHeight())).collect(Collectors.toList());
  78     }
  79 
  80     public MultiResolutionBufferedImage map(Function<Image, Image> mapper) {
  81         return new MultiResolutionBufferedImage(mapper.apply(this), sizes,
  82                 (width, height) ->
  83                         mapper.apply(getResolutionVariant(width, height)));
  84     }
  85 
  86     public interface MultiResolutionImageMapper {
  87 
  88         Image apply(int width, int height);
  89     }
  90 
  91     private static class ImageCacheKey implements ImageCache.PixelsKey {
  92 
  93         private final int pixelCount;
  94         private final int hash;
  95 
  96         private final int w;
  97         private final int h;
  98         private final Image baseImage;
  99 
 100         ImageCacheKey(final Image baseImage,
 101                 final int w, final int h) {
 102             this.baseImage = baseImage;
 103             this.w = w;
 104             this.h = h;
 105             this.pixelCount = w * h;
 106             hash = hash();
 107         }
 108 
 109         @Override
 110         public int getPixelCount() {
 111             return pixelCount;
 112         }
 113 
 114         private int hash() {
 115             int hash = baseImage.hashCode();
 116             hash = 31 * hash + w;
 117             hash = 31 * hash + h;
 118             return hash;
 119         }
 120 
 121         @Override
 122         public int hashCode() {
 123             return hash;
 124         }
 125 
 126         @Override
 127         public boolean equals(Object obj) {
 128             if (obj instanceof ImageCacheKey) {
 129                 ImageCacheKey key = (ImageCacheKey) obj;
 130                 return baseImage == key.baseImage && w == key.w && h == key.h;
 131             }
 132             return false;
 133         }
 134     }
 135 }