author | rchamyal |
Mon, 25 Jul 2016 15:29:24 +0530 | |
changeset 40141 | a7297a9dd5fa |
parent 25859 | 3317bb8137f4 |
child 41785 | 280cd31c3b20 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22584
eed64ee05369
8032733: Fix cast lint warnings in client libraries
darcy
parents:
18241
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
18241 | 25 |
package sun.awt; |
2 | 26 |
import java.awt.*; |
27 |
import java.awt.color.*; |
|
40141
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
28 |
import java.awt.geom.AffineTransform; |
2 | 29 |
import java.awt.image.*; |
30 |
import sun.awt.image.ToolkitImage; |
|
31 |
import sun.awt.image.ImageRepresentation; |
|
32 |
import java.util.Arrays; |
|
40141
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
33 |
import sun.java2d.pipe.Region; |
2 | 34 |
|
18241 | 35 |
public class IconInfo { |
2 | 36 |
/** |
18241 | 37 |
* Representation of image as an int array. |
38 |
* It's used on platforms where icon data |
|
39 |
* is expected to be in 32-bit format. |
|
2 | 40 |
*/ |
41 |
private int[] intIconData; |
|
42 |
/** |
|
18241 | 43 |
* Representation of image as an long array. |
44 |
* It's used on platforms where icon data |
|
45 |
* is expected to be in 64-bit format. |
|
2 | 46 |
*/ |
47 |
private long[] longIconData; |
|
48 |
/** |
|
49 |
* Icon image. |
|
50 |
*/ |
|
51 |
private Image image; |
|
52 |
/** |
|
53 |
* Width of icon image. Being set in constructor. |
|
54 |
*/ |
|
55 |
private final int width; |
|
56 |
/** |
|
57 |
* Height of icon image. Being set in constructor. |
|
58 |
*/ |
|
59 |
private final int height; |
|
60 |
/** |
|
61 |
* Width of scaled icon image. Can be set in setScaledDimension. |
|
62 |
*/ |
|
63 |
private int scaledWidth; |
|
64 |
/** |
|
65 |
* Height of scaled icon image. Can be set in setScaledDimension. |
|
66 |
*/ |
|
67 |
private int scaledHeight; |
|
68 |
/** |
|
69 |
* Length of raw data. Being set in constructor / setScaledDimension. |
|
70 |
*/ |
|
71 |
private int rawLength; |
|
72 |
||
18241 | 73 |
public IconInfo(int[] intIconData) { |
2 | 74 |
this.intIconData = |
75 |
(null == intIconData) ? null : Arrays.copyOf(intIconData, intIconData.length); |
|
76 |
this.width = intIconData[0]; |
|
77 |
this.height = intIconData[1]; |
|
78 |
this.scaledWidth = width; |
|
79 |
this.scaledHeight = height; |
|
80 |
this.rawLength = width * height + 2; |
|
81 |
} |
|
82 |
||
18241 | 83 |
public IconInfo(long[] longIconData) { |
2 | 84 |
this.longIconData = |
85 |
(null == longIconData) ? null : Arrays.copyOf(longIconData, longIconData.length); |
|
86 |
this.width = (int)longIconData[0]; |
|
87 |
this.height = (int)longIconData[1]; |
|
88 |
this.scaledWidth = width; |
|
89 |
this.scaledHeight = height; |
|
90 |
this.rawLength = width * height + 2; |
|
91 |
} |
|
92 |
||
18241 | 93 |
public IconInfo(Image image) { |
2 | 94 |
this.image = image; |
95 |
if (image instanceof ToolkitImage) { |
|
96 |
ImageRepresentation ir = ((ToolkitImage)image).getImageRep(); |
|
97 |
ir.reconstruct(ImageObserver.ALLBITS); |
|
98 |
this.width = ir.getWidth(); |
|
99 |
this.height = ir.getHeight(); |
|
100 |
} else { |
|
101 |
this.width = image.getWidth(null); |
|
102 |
this.height = image.getHeight(null); |
|
103 |
} |
|
104 |
this.scaledWidth = width; |
|
105 |
this.scaledHeight = height; |
|
40141
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
106 |
this.rawLength = getScaledRawLength(); |
2 | 107 |
} |
108 |
||
109 |
/* |
|
110 |
* It sets size of scaled icon. |
|
111 |
*/ |
|
18241 | 112 |
public void setScaledSize(int width, int height) { |
2 | 113 |
this.scaledWidth = width; |
114 |
this.scaledHeight = height; |
|
40141
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
115 |
this.rawLength = getScaledRawLength(); |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
116 |
} |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
117 |
|
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
118 |
/* |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
119 |
* returns scaled raw length. |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
120 |
*/ |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
121 |
private int getScaledRawLength() { |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
122 |
int scaledWidthAndHeight[] = getScaledWidthAndHeight(width, height); |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
123 |
return scaledWidthAndHeight[0] * scaledWidthAndHeight[1] + 2; |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
124 |
} |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
125 |
|
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
126 |
/* |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
127 |
* returns the scaled width and height. |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
128 |
*/ |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
129 |
private static int[] getScaledWidthAndHeight(int width, int height) { |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
130 |
AffineTransform tx = GraphicsEnvironment.getLocalGraphicsEnvironment(). |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
131 |
getDefaultScreenDevice().getDefaultConfiguration(). |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
132 |
getDefaultTransform(); |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
133 |
int w = Region.clipScale(width, tx.getScaleX()); |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
134 |
int h = Region.clipScale(height, tx.getScaleY()); |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
135 |
return new int[]{w, h}; |
2 | 136 |
} |
137 |
||
18241 | 138 |
public boolean isValid() { |
2 | 139 |
return (width > 0 && height > 0); |
140 |
} |
|
141 |
||
18241 | 142 |
public int getWidth() { |
2 | 143 |
return width; |
144 |
} |
|
145 |
||
18241 | 146 |
public int getHeight() { |
2 | 147 |
return height; |
148 |
} |
|
149 |
||
150 |
public String toString() { |
|
18241 | 151 |
return "IconInfo[w=" + width + ",h=" + height + ",sw=" + scaledWidth + ",sh=" + scaledHeight + "]"; |
2 | 152 |
} |
153 |
||
18241 | 154 |
public int getRawLength() { |
2 | 155 |
return rawLength; |
156 |
} |
|
157 |
||
18241 | 158 |
public int[] getIntData() { |
2 | 159 |
if (this.intIconData == null) { |
160 |
if (this.longIconData != null) { |
|
161 |
this.intIconData = longArrayToIntArray(longIconData); |
|
162 |
} else if (this.image != null) { |
|
163 |
this.intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight); |
|
164 |
} |
|
165 |
} |
|
166 |
return this.intIconData; |
|
167 |
} |
|
168 |
||
18241 | 169 |
public long[] getLongData() { |
2 | 170 |
if (this.longIconData == null) { |
171 |
if (this.intIconData != null) { |
|
172 |
this.longIconData = intArrayToLongArray(this.intIconData); |
|
173 |
} else if (this.image != null) { |
|
174 |
int[] intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight); |
|
175 |
this.longIconData = intArrayToLongArray(intIconData); |
|
176 |
} |
|
177 |
} |
|
178 |
return this.longIconData; |
|
179 |
} |
|
180 |
||
18241 | 181 |
public Image getImage() { |
2 | 182 |
if (this.image == null) { |
183 |
if (this.intIconData != null) { |
|
184 |
this.image = intArrayToImage(this.intIconData); |
|
185 |
} else if (this.longIconData != null) { |
|
186 |
int[] intIconData = longArrayToIntArray(this.longIconData); |
|
187 |
this.image = intArrayToImage(intIconData); |
|
188 |
} |
|
189 |
} |
|
190 |
return this.image; |
|
191 |
} |
|
192 |
||
193 |
private static int[] longArrayToIntArray(long[] longData) { |
|
194 |
int[] intData = new int[longData.length]; |
|
195 |
for (int i = 0; i < longData.length; i++) { |
|
196 |
// Such a conversion is valid since the |
|
197 |
// original data (see |
|
198 |
// make/sun/xawt/ToBin.java) were ints |
|
199 |
intData[i] = (int)longData[i]; |
|
200 |
} |
|
201 |
return intData; |
|
202 |
} |
|
203 |
||
204 |
private static long[] intArrayToLongArray(int[] intData) { |
|
205 |
long[] longData = new long[intData.length]; |
|
206 |
for (int i = 0; i < intData.length; i++) { |
|
22584
eed64ee05369
8032733: Fix cast lint warnings in client libraries
darcy
parents:
18241
diff
changeset
|
207 |
longData[i] = intData[i]; |
2 | 208 |
} |
209 |
return longData; |
|
210 |
} |
|
211 |
||
212 |
static Image intArrayToImage(int[] raw) { |
|
213 |
ColorModel cm = |
|
214 |
new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, |
|
215 |
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, |
|
216 |
false, DataBuffer.TYPE_INT); |
|
217 |
DataBuffer buffer = new DataBufferInt(raw, raw.length-2, 2); |
|
218 |
WritableRaster raster = |
|
219 |
Raster.createPackedRaster(buffer, raw[0], raw[1], |
|
220 |
raw[0], |
|
221 |
new int[] {0x00ff0000, 0x0000ff00, |
|
222 |
0x000000ff, 0xff000000}, |
|
223 |
null); |
|
224 |
BufferedImage im = new BufferedImage(cm, raster, false, null); |
|
225 |
return im; |
|
226 |
} |
|
227 |
||
228 |
/* |
|
229 |
* Returns array of integers which holds data for the image. |
|
230 |
* It scales the image if necessary. |
|
231 |
*/ |
|
232 |
static int[] imageToIntArray(Image image, int width, int height) { |
|
233 |
if (width <= 0 || height <= 0) { |
|
234 |
return null; |
|
235 |
} |
|
236 |
ColorModel cm = |
|
237 |
new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, |
|
238 |
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, |
|
239 |
false, DataBuffer.TYPE_INT); |
|
40141
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
240 |
int scaledWidthAndHeight[] = getScaledWidthAndHeight(width, height); |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
241 |
width = scaledWidthAndHeight[0]; |
a7297a9dd5fa
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel
rchamyal
parents:
25859
diff
changeset
|
242 |
height = scaledWidthAndHeight[1]; |
2 | 243 |
DataBufferInt buffer = new DataBufferInt(width * height); |
244 |
WritableRaster raster = |
|
245 |
Raster.createPackedRaster(buffer, width, height, |
|
246 |
width, |
|
247 |
new int[] {0x00ff0000, 0x0000ff00, |
|
248 |
0x000000ff, 0xff000000}, |
|
249 |
null); |
|
250 |
BufferedImage im = new BufferedImage(cm, raster, false, null); |
|
251 |
Graphics g = im.getGraphics(); |
|
252 |
g.drawImage(image, 0, 0, width, height, null); |
|
253 |
g.dispose(); |
|
254 |
int[] data = buffer.getData(); |
|
255 |
int[] raw = new int[width * height + 2]; |
|
256 |
raw[0] = width; |
|
257 |
raw[1] = height; |
|
258 |
System.arraycopy(data, 0, raw, 2, width * height); |
|
259 |
return raw; |
|
260 |
} |
|
261 |
||
262 |
} |