src/share/classes/sun/awt/image/MultiResolutionBufferedImage.java

Print this page




   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.image.BufferedImage;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import java.util.function.Function;

  33 
  34 public class MultiResolutionBufferedImage extends BufferedImage
  35         implements MultiResolutionImage {
  36 
  37     Image[] resolutionVariants;
  38     int baseIndex;
  39 
  40     public MultiResolutionBufferedImage(int imageType, int baseIndex, Image... images) {
  41         super(images[baseIndex].getWidth(null), images[baseIndex].getHeight(null),
  42                 imageType);
  43         this.baseIndex = baseIndex;
  44         this.resolutionVariants = images;

  45         Graphics g = getGraphics();
  46         g.drawImage(images[baseIndex], 0, 0, null);
  47         g.dispose();
  48         images[baseIndex] = this;
  49     }
  50 
  51     @Override
  52     public Image getResolutionVariant(int width, int height) {
  53         for (Image image : resolutionVariants) {
  54             if (width <= image.getWidth(null) && height <= image.getHeight(null)) {
  55                 return image;


  56             }







  57         }
  58         return this;

  59     }
  60 
  61     @Override
  62     public List<Image> getResolutionVariants() {
  63         return Arrays.asList(resolutionVariants);


  64     }
  65 
  66     public MultiResolutionBufferedImage map(Function<Image, Image> mapper) {
  67         return new MultiResolutionBufferedImage(getType(), baseIndex,
  68                 Arrays.stream(resolutionVariants).map(mapper)
  69                         .toArray(length -> new Image[length]));


















































  70     }
  71 }


   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 }