Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/awt/IconInfo.java
38827 views
/*1* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.awt;25import java.awt.*;26import java.awt.color.*;27import java.awt.image.*;28import sun.awt.image.ToolkitImage;29import sun.awt.image.ImageRepresentation;30import java.util.Arrays;3132public class IconInfo {33/**34* Representation of image as an int array.35* It's used on platforms where icon data36* is expected to be in 32-bit format.37*/38private int[] intIconData;39/**40* Representation of image as an long array.41* It's used on platforms where icon data42* is expected to be in 64-bit format.43*/44private long[] longIconData;45/**46* Icon image.47*/48private Image image;49/**50* Width of icon image. Being set in constructor.51*/52private final int width;53/**54* Height of icon image. Being set in constructor.55*/56private final int height;57/**58* Width of scaled icon image. Can be set in setScaledDimension.59*/60private int scaledWidth;61/**62* Height of scaled icon image. Can be set in setScaledDimension.63*/64private int scaledHeight;65/**66* Length of raw data. Being set in constructor / setScaledDimension.67*/68private int rawLength;6970public IconInfo(int[] intIconData) {71this.intIconData =72(null == intIconData) ? null : Arrays.copyOf(intIconData, intIconData.length);73this.width = intIconData[0];74this.height = intIconData[1];75this.scaledWidth = width;76this.scaledHeight = height;77this.rawLength = width * height + 2;78}7980public IconInfo(long[] longIconData) {81this.longIconData =82(null == longIconData) ? null : Arrays.copyOf(longIconData, longIconData.length);83this.width = (int)longIconData[0];84this.height = (int)longIconData[1];85this.scaledWidth = width;86this.scaledHeight = height;87this.rawLength = width * height + 2;88}8990public IconInfo(Image image) {91this.image = image;92if (image instanceof ToolkitImage) {93ImageRepresentation ir = ((ToolkitImage)image).getImageRep();94ir.reconstruct(ImageObserver.ALLBITS);95this.width = ir.getWidth();96this.height = ir.getHeight();97} else {98this.width = image.getWidth(null);99this.height = image.getHeight(null);100}101this.scaledWidth = width;102this.scaledHeight = height;103this.rawLength = width * height + 2;104}105106/*107* It sets size of scaled icon.108*/109public void setScaledSize(int width, int height) {110this.scaledWidth = width;111this.scaledHeight = height;112this.rawLength = width * height + 2;113}114115public boolean isValid() {116return (width > 0 && height > 0);117}118119public int getWidth() {120return width;121}122123public int getHeight() {124return height;125}126127public String toString() {128return "IconInfo[w=" + width + ",h=" + height + ",sw=" + scaledWidth + ",sh=" + scaledHeight + "]";129}130131public int getRawLength() {132return rawLength;133}134135public int[] getIntData() {136if (this.intIconData == null) {137if (this.longIconData != null) {138this.intIconData = longArrayToIntArray(longIconData);139} else if (this.image != null) {140this.intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);141}142}143return this.intIconData;144}145146public long[] getLongData() {147if (this.longIconData == null) {148if (this.intIconData != null) {149this.longIconData = intArrayToLongArray(this.intIconData);150} else if (this.image != null) {151int[] intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);152this.longIconData = intArrayToLongArray(intIconData);153}154}155return this.longIconData;156}157158public Image getImage() {159if (this.image == null) {160if (this.intIconData != null) {161this.image = intArrayToImage(this.intIconData);162} else if (this.longIconData != null) {163int[] intIconData = longArrayToIntArray(this.longIconData);164this.image = intArrayToImage(intIconData);165}166}167return this.image;168}169170private static int[] longArrayToIntArray(long[] longData) {171int[] intData = new int[longData.length];172for (int i = 0; i < longData.length; i++) {173// Such a conversion is valid since the174// original data (see175// make/sun/xawt/ToBin.java) were ints176intData[i] = (int)longData[i];177}178return intData;179}180181private static long[] intArrayToLongArray(int[] intData) {182long[] longData = new long[intData.length];183for (int i = 0; i < intData.length; i++) {184longData[i] = (int)intData[i];185}186return longData;187}188189static Image intArrayToImage(int[] raw) {190ColorModel cm =191new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,1920x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,193false, DataBuffer.TYPE_INT);194DataBuffer buffer = new DataBufferInt(raw, raw.length-2, 2);195WritableRaster raster =196Raster.createPackedRaster(buffer, raw[0], raw[1],197raw[0],198new int[] {0x00ff0000, 0x0000ff00,1990x000000ff, 0xff000000},200null);201BufferedImage im = new BufferedImage(cm, raster, false, null);202return im;203}204205/*206* Returns array of integers which holds data for the image.207* It scales the image if necessary.208*/209static int[] imageToIntArray(Image image, int width, int height) {210if (width <= 0 || height <= 0) {211return null;212}213ColorModel cm =214new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,2150x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,216false, DataBuffer.TYPE_INT);217DataBufferInt buffer = new DataBufferInt(width * height);218WritableRaster raster =219Raster.createPackedRaster(buffer, width, height,220width,221new int[] {0x00ff0000, 0x0000ff00,2220x000000ff, 0xff000000},223null);224BufferedImage im = new BufferedImage(cm, raster, false, null);225Graphics g = im.getGraphics();226g.drawImage(image, 0, 0, width, height, null);227g.dispose();228int[] data = buffer.getData();229int[] raw = new int[width * height + 2];230raw[0] = width;231raw[1] = height;232System.arraycopy(data, 0, raw, 2, width * height);233return raw;234}235236}237238239