Path: blob/master/src/java.desktop/share/classes/sun/awt/IconInfo.java
41152 views
/*1* Copyright (c) 2006, 2018, 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.geom.AffineTransform;28import java.awt.image.*;29import sun.awt.image.ToolkitImage;30import sun.awt.image.ImageRepresentation;31import java.util.Arrays;32import sun.java2d.pipe.Region;3334public class IconInfo {35/**36* Representation of image as an int array.37* It's used on platforms where icon data38* is expected to be in 32-bit format.39*/40private int[] intIconData;41/**42* Representation of image as an long array.43* It's used on platforms where icon data44* is expected to be in 64-bit format.45*/46private long[] longIconData;47/**48* Icon image.49*/50private Image image;51/**52* Width of icon image. Being set in constructor.53*/54private final int width;55/**56* Height of icon image. Being set in constructor.57*/58private final int height;59/**60* Width of scaled icon image. Can be set in setScaledDimension.61*/62private int scaledWidth;63/**64* Height of scaled icon image. Can be set in setScaledDimension.65*/66private int scaledHeight;67/**68* Length of raw data. Being set in constructor / setScaledDimension.69*/70private int rawLength;7172public IconInfo(int[] intIconData) {73this.intIconData =74(null == intIconData) ? null : Arrays.copyOf(intIconData, intIconData.length);75this.width = intIconData[0];76this.height = intIconData[1];77this.scaledWidth = width;78this.scaledHeight = height;79this.rawLength = width * height + 2;80}8182public IconInfo(long[] longIconData) {83this.longIconData =84(null == longIconData) ? null : Arrays.copyOf(longIconData, longIconData.length);85this.width = (int)longIconData[0];86this.height = (int)longIconData[1];87this.scaledWidth = width;88this.scaledHeight = height;89this.rawLength = width * height + 2;90}9192public IconInfo(Image image) {93this.image = image;94if (image instanceof ToolkitImage) {95ImageRepresentation ir = ((ToolkitImage)image).getImageRep();96ir.reconstruct(ImageObserver.ALLBITS);97this.width = ir.getWidth();98this.height = ir.getHeight();99} else {100this.width = image.getWidth(null);101this.height = image.getHeight(null);102}103this.scaledWidth = width;104this.scaledHeight = height;105this.rawLength = getScaledRawLength(width, height);106}107108/*109* It sets size of scaled icon.110*/111public void setScaledSize(int width, int height) {112this.scaledWidth = width;113this.scaledHeight = height;114this.rawLength = getScaledRawLength(width, height);115}116117/*118* returns scaled raw length.119*/120private int getScaledRawLength(int w, int h) {121int[] scaledWidthAndHeight = getScaledWidthAndHeight(w, h);122return scaledWidthAndHeight[0] * scaledWidthAndHeight[1] + 2;123}124125/*126* returns the scaled width and height.127*/128private static int[] getScaledWidthAndHeight(int width, int height) {129AffineTransform tx = GraphicsEnvironment.getLocalGraphicsEnvironment().130getDefaultScreenDevice().getDefaultConfiguration().131getDefaultTransform();132int w = Region.clipScale(width, tx.getScaleX());133int h = Region.clipScale(height, tx.getScaleY());134return new int[]{w, h};135}136137public boolean isValid() {138return (width > 0 && height > 0);139}140141public int getWidth() {142return width;143}144145public int getHeight() {146return height;147}148149public String toString() {150return "IconInfo[w=" + width + ",h=" + height + ",sw=" + scaledWidth + ",sh=" + scaledHeight + "]";151}152153public int getRawLength() {154return rawLength;155}156157public int[] getIntData() {158if (this.intIconData == null) {159if (this.longIconData != null) {160this.intIconData = longArrayToIntArray(longIconData);161} else if (this.image != null) {162this.intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);163}164}165return this.intIconData;166}167168public long[] getLongData() {169if (this.longIconData == null) {170if (this.intIconData != null) {171this.longIconData = intArrayToLongArray(this.intIconData);172} else if (this.image != null) {173int[] intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);174this.longIconData = intArrayToLongArray(intIconData);175}176}177return this.longIconData;178}179180public Image getImage() {181if (this.image == null) {182if (this.intIconData != null) {183this.image = intArrayToImage(this.intIconData);184} else if (this.longIconData != null) {185int[] intIconData = longArrayToIntArray(this.longIconData);186this.image = intArrayToImage(intIconData);187}188}189return this.image;190}191192private static int[] longArrayToIntArray(long[] longData) {193int[] intData = new int[longData.length];194for (int i = 0; i < longData.length; i++) {195// Such a conversion is valid since the196// original data (see197// make/sun/xawt/ToBin.java) were ints198intData[i] = (int)longData[i];199}200return intData;201}202203private static long[] intArrayToLongArray(int[] intData) {204long[] longData = new long[intData.length];205for (int i = 0; i < intData.length; i++) {206longData[i] = intData[i];207}208return longData;209}210211static Image intArrayToImage(int[] raw) {212ColorModel cm =213new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,2140x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,215false, DataBuffer.TYPE_INT);216DataBuffer buffer = new DataBufferInt(raw, raw.length-2, 2);217WritableRaster raster =218Raster.createPackedRaster(buffer, raw[0], raw[1],219raw[0],220new int[] {0x00ff0000, 0x0000ff00,2210x000000ff, 0xff000000},222null);223BufferedImage im = new BufferedImage(cm, raster, false, null);224return im;225}226227/*228* Returns array of integers which holds data for the image.229* It scales the image if necessary.230*/231static int[] imageToIntArray(Image image, int width, int height) {232if (width <= 0 || height <= 0) {233return null;234}235ColorModel cm =236new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,2370x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,238false, DataBuffer.TYPE_INT);239int[] scaledWidthAndHeight = getScaledWidthAndHeight(width, height);240width = scaledWidthAndHeight[0];241height = scaledWidthAndHeight[1];242DataBufferInt buffer = new DataBufferInt(width * height);243WritableRaster raster =244Raster.createPackedRaster(buffer, width, height,245width,246new int[] {0x00ff0000, 0x0000ff00,2470x000000ff, 0xff000000},248null);249BufferedImage im = new BufferedImage(cm, raster, false, null);250Graphics g = im.getGraphics();251g.drawImage(image, 0, 0, width, height, null);252g.dispose();253int[] data = buffer.getData();254int[] raw = new int[width * height + 2];255raw[0] = width;256raw[1] = height;257System.arraycopy(data, 0, raw, 2, width * height);258return raw;259}260261}262263264