0N/A/*
6323N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
6323N/Apackage sun.awt;
0N/Aimport java.awt.*;
0N/Aimport java.awt.color.*;
0N/Aimport java.awt.image.*;
0N/Aimport sun.awt.image.ToolkitImage;
0N/Aimport sun.awt.image.ImageRepresentation;
0N/Aimport java.util.Arrays;
0N/A
6323N/Apublic class IconInfo {
0N/A /**
6323N/A * Representation of image as an int array.
6323N/A * It's used on platforms where icon data
6323N/A * is expected to be in 32-bit format.
0N/A */
0N/A private int[] intIconData;
0N/A /**
6323N/A * Representation of image as an long array.
6323N/A * It's used on platforms where icon data
6323N/A * is expected to be in 64-bit format.
0N/A */
0N/A private long[] longIconData;
0N/A /**
0N/A * Icon image.
0N/A */
0N/A private Image image;
0N/A /**
0N/A * Width of icon image. Being set in constructor.
0N/A */
0N/A private final int width;
0N/A /**
0N/A * Height of icon image. Being set in constructor.
0N/A */
0N/A private final int height;
0N/A /**
0N/A * Width of scaled icon image. Can be set in setScaledDimension.
0N/A */
0N/A private int scaledWidth;
0N/A /**
0N/A * Height of scaled icon image. Can be set in setScaledDimension.
0N/A */
0N/A private int scaledHeight;
0N/A /**
0N/A * Length of raw data. Being set in constructor / setScaledDimension.
0N/A */
0N/A private int rawLength;
0N/A
6323N/A public IconInfo(int[] intIconData) {
0N/A this.intIconData =
0N/A (null == intIconData) ? null : Arrays.copyOf(intIconData, intIconData.length);
0N/A this.width = intIconData[0];
0N/A this.height = intIconData[1];
0N/A this.scaledWidth = width;
0N/A this.scaledHeight = height;
0N/A this.rawLength = width * height + 2;
0N/A }
0N/A
6323N/A public IconInfo(long[] longIconData) {
0N/A this.longIconData =
0N/A (null == longIconData) ? null : Arrays.copyOf(longIconData, longIconData.length);
0N/A this.width = (int)longIconData[0];
0N/A this.height = (int)longIconData[1];
0N/A this.scaledWidth = width;
0N/A this.scaledHeight = height;
0N/A this.rawLength = width * height + 2;
0N/A }
0N/A
6323N/A public IconInfo(Image image) {
0N/A this.image = image;
0N/A if (image instanceof ToolkitImage) {
0N/A ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
0N/A ir.reconstruct(ImageObserver.ALLBITS);
0N/A this.width = ir.getWidth();
0N/A this.height = ir.getHeight();
0N/A } else {
0N/A this.width = image.getWidth(null);
0N/A this.height = image.getHeight(null);
0N/A }
0N/A this.scaledWidth = width;
0N/A this.scaledHeight = height;
0N/A this.rawLength = width * height + 2;
0N/A }
0N/A
0N/A /*
0N/A * It sets size of scaled icon.
0N/A */
6323N/A public void setScaledSize(int width, int height) {
0N/A this.scaledWidth = width;
0N/A this.scaledHeight = height;
0N/A this.rawLength = width * height + 2;
0N/A }
0N/A
6323N/A public boolean isValid() {
0N/A return (width > 0 && height > 0);
0N/A }
0N/A
6323N/A public int getWidth() {
0N/A return width;
0N/A }
0N/A
6323N/A public int getHeight() {
0N/A return height;
0N/A }
0N/A
0N/A public String toString() {
6323N/A return "IconInfo[w=" + width + ",h=" + height + ",sw=" + scaledWidth + ",sh=" + scaledHeight + "]";
0N/A }
0N/A
6323N/A public int getRawLength() {
0N/A return rawLength;
0N/A }
0N/A
6323N/A public int[] getIntData() {
0N/A if (this.intIconData == null) {
0N/A if (this.longIconData != null) {
0N/A this.intIconData = longArrayToIntArray(longIconData);
0N/A } else if (this.image != null) {
0N/A this.intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);
0N/A }
0N/A }
0N/A return this.intIconData;
0N/A }
0N/A
6323N/A public long[] getLongData() {
0N/A if (this.longIconData == null) {
0N/A if (this.intIconData != null) {
0N/A this.longIconData = intArrayToLongArray(this.intIconData);
0N/A } else if (this.image != null) {
0N/A int[] intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);
0N/A this.longIconData = intArrayToLongArray(intIconData);
0N/A }
0N/A }
0N/A return this.longIconData;
0N/A }
0N/A
6323N/A public Image getImage() {
0N/A if (this.image == null) {
0N/A if (this.intIconData != null) {
0N/A this.image = intArrayToImage(this.intIconData);
0N/A } else if (this.longIconData != null) {
0N/A int[] intIconData = longArrayToIntArray(this.longIconData);
0N/A this.image = intArrayToImage(intIconData);
0N/A }
0N/A }
0N/A return this.image;
0N/A }
0N/A
0N/A private static int[] longArrayToIntArray(long[] longData) {
0N/A int[] intData = new int[longData.length];
0N/A for (int i = 0; i < longData.length; i++) {
0N/A // Such a conversion is valid since the
0N/A // original data (see
0N/A // make/sun/xawt/ToBin.java) were ints
0N/A intData[i] = (int)longData[i];
0N/A }
0N/A return intData;
0N/A }
0N/A
0N/A private static long[] intArrayToLongArray(int[] intData) {
0N/A long[] longData = new long[intData.length];
0N/A for (int i = 0; i < intData.length; i++) {
0N/A longData[i] = (int)intData[i];
0N/A }
0N/A return longData;
0N/A }
0N/A
0N/A static Image intArrayToImage(int[] raw) {
0N/A ColorModel cm =
0N/A new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,
0N/A 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,
0N/A false, DataBuffer.TYPE_INT);
0N/A DataBuffer buffer = new DataBufferInt(raw, raw.length-2, 2);
0N/A WritableRaster raster =
0N/A Raster.createPackedRaster(buffer, raw[0], raw[1],
0N/A raw[0],
0N/A new int[] {0x00ff0000, 0x0000ff00,
0N/A 0x000000ff, 0xff000000},
0N/A null);
0N/A BufferedImage im = new BufferedImage(cm, raster, false, null);
0N/A return im;
0N/A }
0N/A
0N/A /*
0N/A * Returns array of integers which holds data for the image.
0N/A * It scales the image if necessary.
0N/A */
0N/A static int[] imageToIntArray(Image image, int width, int height) {
0N/A if (width <= 0 || height <= 0) {
0N/A return null;
0N/A }
0N/A ColorModel cm =
0N/A new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,
0N/A 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,
0N/A false, DataBuffer.TYPE_INT);
0N/A DataBufferInt buffer = new DataBufferInt(width * height);
0N/A WritableRaster raster =
0N/A Raster.createPackedRaster(buffer, width, height,
0N/A width,
0N/A new int[] {0x00ff0000, 0x0000ff00,
0N/A 0x000000ff, 0xff000000},
0N/A null);
0N/A BufferedImage im = new BufferedImage(cm, raster, false, null);
0N/A Graphics g = im.getGraphics();
0N/A g.drawImage(image, 0, 0, width, height, null);
0N/A g.dispose();
0N/A int[] data = buffer.getData();
0N/A int[] raw = new int[width * height + 2];
0N/A raw[0] = width;
0N/A raw[1] = height;
0N/A System.arraycopy(data, 0, raw, 2, width * height);
0N/A return raw;
0N/A }
0N/A
0N/A}