001: /*
002: * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.X11;
026:
027: import java.awt.*;
028: import java.awt.color.*;
029: import java.awt.image.*;
030: import sun.awt.image.ToolkitImage;
031: import sun.awt.image.ImageRepresentation;
032: import java.util.Arrays;
033:
034: class XIconInfo {
035: /**
036: * Representation of image as an int array
037: * It's being used for _NET_WM_ICON hint
038: * with 32-bit X data model
039: */
040: private int[] intIconData;
041: /**
042: * Representation of image as an int array
043: * It's being used for _NET_WM_ICON hint
044: * with 64-bit X data model
045: */
046: private long[] longIconData;
047: /**
048: * Icon image.
049: */
050: private Image image;
051: /**
052: * Width of icon image. Being set in constructor.
053: */
054: private final int width;
055: /**
056: * Height of icon image. Being set in constructor.
057: */
058: private final int height;
059: /**
060: * Width of scaled icon image. Can be set in setScaledDimension.
061: */
062: private int scaledWidth;
063: /**
064: * Height of scaled icon image. Can be set in setScaledDimension.
065: */
066: private int scaledHeight;
067: /**
068: * Length of raw data. Being set in constructor / setScaledDimension.
069: */
070: private int rawLength;
071:
072: XIconInfo(int[] intIconData) {
073: this .intIconData = (null == intIconData) ? null : Arrays
074: .copyOf(intIconData, intIconData.length);
075: this .width = intIconData[0];
076: this .height = intIconData[1];
077: this .scaledWidth = width;
078: this .scaledHeight = height;
079: this .rawLength = width * height + 2;
080: }
081:
082: XIconInfo(long[] longIconData) {
083: this .longIconData = (null == longIconData) ? null : Arrays
084: .copyOf(longIconData, longIconData.length);
085: this .width = (int) longIconData[0];
086: this .height = (int) longIconData[1];
087: this .scaledWidth = width;
088: this .scaledHeight = height;
089: this .rawLength = width * height + 2;
090: }
091:
092: XIconInfo(Image image) {
093: this .image = image;
094: if (image instanceof ToolkitImage) {
095: ImageRepresentation ir = ((ToolkitImage) image)
096: .getImageRep();
097: ir.reconstruct(ImageObserver.ALLBITS);
098: this .width = ir.getWidth();
099: 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;
106: this .rawLength = width * height + 2;
107: }
108:
109: /*
110: * It sets size of scaled icon.
111: */
112: void setScaledSize(int width, int height) {
113: this .scaledWidth = width;
114: this .scaledHeight = height;
115: this .rawLength = width * height + 2;
116: }
117:
118: boolean isValid() {
119: return (width > 0 && height > 0);
120: }
121:
122: int getWidth() {
123: return width;
124: }
125:
126: int getHeight() {
127: return height;
128: }
129:
130: public String toString() {
131: return "XIconInfo[w=" + width + ",h=" + height + ",sw="
132: + scaledWidth + ",sh=" + scaledHeight + "]";
133: }
134:
135: int getRawLength() {
136: return rawLength;
137: }
138:
139: int[] getIntData() {
140: if (this .intIconData == null) {
141: if (this .longIconData != null) {
142: this .intIconData = longArrayToIntArray(longIconData);
143: } else if (this .image != null) {
144: this .intIconData = imageToIntArray(this .image,
145: scaledWidth, scaledHeight);
146: }
147: }
148: return this .intIconData;
149: }
150:
151: long[] getLongData() {
152: if (this .longIconData == null) {
153: if (this .intIconData != null) {
154: this .longIconData = intArrayToLongArray(this .intIconData);
155: } else if (this .image != null) {
156: int[] intIconData = imageToIntArray(this .image,
157: scaledWidth, scaledHeight);
158: this .longIconData = intArrayToLongArray(intIconData);
159: }
160: }
161: return this .longIconData;
162: }
163:
164: Image getImage() {
165: if (this .image == null) {
166: if (this .intIconData != null) {
167: this .image = intArrayToImage(this .intIconData);
168: } else if (this .longIconData != null) {
169: int[] intIconData = longArrayToIntArray(this .longIconData);
170: this .image = intArrayToImage(intIconData);
171: }
172: }
173: return this .image;
174: }
175:
176: private static int[] longArrayToIntArray(long[] longData) {
177: int[] intData = new int[longData.length];
178: for (int i = 0; i < longData.length; i++) {
179: // Such a conversion is valid since the
180: // original data (see
181: // make/sun/xawt/ToBin.java) were ints
182: intData[i] = (int) longData[i];
183: }
184: return intData;
185: }
186:
187: private static long[] intArrayToLongArray(int[] intData) {
188: long[] longData = new long[intData.length];
189: for (int i = 0; i < intData.length; i++) {
190: longData[i] = (int) intData[i];
191: }
192: return longData;
193: }
194:
195: static Image intArrayToImage(int[] raw) {
196: ColorModel cm = new DirectColorModel(ColorSpace
197: .getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000,
198: 0x0000ff00, 0x000000ff, 0xff000000, false,
199: DataBuffer.TYPE_INT);
200: DataBuffer buffer = new DataBufferInt(raw, raw.length - 2, 2);
201: WritableRaster raster = Raster.createPackedRaster(buffer,
202: raw[0], raw[1], raw[0], new int[] { 0x00ff0000,
203: 0x0000ff00, 0x000000ff, 0xff000000 }, null);
204: BufferedImage im = new BufferedImage(cm, raster, false, null);
205: return im;
206: }
207:
208: /*
209: * Returns array of integers which holds data for the image.
210: * It scales the image if necessary.
211: */
212: static int[] imageToIntArray(Image image, int width, int height) {
213: if (width <= 0 || height <= 0) {
214: return null;
215: }
216: ColorModel cm = new DirectColorModel(ColorSpace
217: .getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000,
218: 0x0000ff00, 0x000000ff, 0xff000000, false,
219: DataBuffer.TYPE_INT);
220: DataBufferInt buffer = new DataBufferInt(width * height);
221: WritableRaster raster = Raster.createPackedRaster(buffer,
222: width, height, width, new int[] { 0x00ff0000,
223: 0x0000ff00, 0x000000ff, 0xff000000 }, null);
224: BufferedImage im = new BufferedImage(cm, raster, false, null);
225: Graphics g = im.getGraphics();
226: g.drawImage(image, 0, 0, width, height, null);
227: g.dispose();
228: int[] data = buffer.getData();
229: int[] raw = new int[width * height + 2];
230: raw[0] = width;
231: raw[1] = height;
232: System.arraycopy(data, 0, raw, 2, width * height);
233: return raw;
234: }
235:
236: }
|