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
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;
|
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
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;
|