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

Print this page




  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




  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