jdk/src/java.desktop/share/classes/sun/awt/IconInfo.java
author rchamyal
Mon, 25 Jul 2016 15:29:24 +0530
changeset 40141 a7297a9dd5fa
parent 25859 3317bb8137f4
child 41785 280cd31c3b20
permissions -rw-r--r--
8147648: [hidpi] multiresolution image: wrong resolution variant is used as icon in the Unity panel Reviewed-by: alexsch, ssadetsky Contributed-by: rajeev.chamyal@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    25
package sun.awt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.awt.image.ToolkitImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.image.ImageRepresentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    35
public class IconInfo {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    /**
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    37
     * Representation of image as an int array.
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    38
     * It's used on platforms where icon data
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    39
     * is expected to be in 32-bit format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private int[] intIconData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    43
     * Representation of image as an long array.
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    44
     * It's used on platforms where icon data
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    45
     * is expected to be in 64-bit format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private long[] longIconData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Icon image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Width of icon image. Being set in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Height of icon image. Being set in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Width of scaled icon image. Can be set in setScaledDimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int scaledWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Height of scaled icon image. Can be set in setScaledDimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int scaledHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Length of raw data. Being set in constructor / setScaledDimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int rawLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    73
    public IconInfo(int[] intIconData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.intIconData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            (null == intIconData) ? null : Arrays.copyOf(intIconData, intIconData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.width = intIconData[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.height = intIconData[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.scaledWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.scaledHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.rawLength = width * height + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    83
    public IconInfo(long[] longIconData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.longIconData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        (null == longIconData) ? null : Arrays.copyOf(longIconData, longIconData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.width = (int)longIconData[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.height = (int)longIconData[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.scaledWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.scaledHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.rawLength = width * height + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
    93
    public IconInfo(Image image) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (image instanceof ToolkitImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ir.reconstruct(ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.width = ir.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            this.height = ir.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            this.width = image.getWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            this.height = image.getHeight(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.scaledWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * It sets size of scaled icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   112
    public void setScaledSize(int width, int height) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.scaledWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   138
    public boolean isValid() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return (width > 0 && height > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   142
    public int getWidth() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   146
    public int getHeight() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public String toString() {
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   151
        return "IconInfo[w=" + width + ",h=" + height + ",sw=" + scaledWidth + ",sh=" + scaledHeight + "]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   154
    public int getRawLength() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return rawLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   158
    public int[] getIntData() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (this.intIconData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (this.longIconData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                this.intIconData = longArrayToIntArray(longIconData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            } else if (this.image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                this.intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return this.intIconData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   169
    public long[] getLongData() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (this.longIconData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (this.intIconData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                this.longIconData = intArrayToLongArray(this.intIconData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else if (this.image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                int[] intIconData = imageToIntArray(this.image, scaledWidth, scaledHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                this.longIconData = intArrayToLongArray(intIconData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return this.longIconData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
18241
3e009b5be123 8003559: Update display of applet windows
leonidr
parents: 5506
diff changeset
   181
    public Image getImage() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (this.image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (this.intIconData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                this.image = intArrayToImage(this.intIconData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else if (this.longIconData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                int[] intIconData = longArrayToIntArray(this.longIconData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                this.image = intArrayToImage(intIconData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return this.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static int[] longArrayToIntArray(long[] longData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int[] intData = new int[longData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        for (int i = 0; i < longData.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // Such a conversion is valid since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // original data (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // make/sun/xawt/ToBin.java) were ints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            intData[i] = (int)longData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return intData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static long[] intArrayToLongArray(int[] intData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        long[] longData = new long[intData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return longData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    static Image intArrayToImage(int[] raw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        ColorModel cm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                 false, DataBuffer.TYPE_INT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        DataBuffer buffer = new DataBufferInt(raw, raw.length-2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            Raster.createPackedRaster(buffer, raw[0], raw[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                      raw[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                      new int[] {0x00ff0000, 0x0000ff00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                                 0x000000ff, 0xff000000},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                      null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        BufferedImage im = new BufferedImage(cm, raster, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return im;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Returns array of integers which holds data for the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * It scales the image if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static int[] imageToIntArray(Image image, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (width <= 0 || height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        ColorModel cm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        DataBufferInt buffer = new DataBufferInt(width * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            Raster.createPackedRaster(buffer, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                      width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                      new int[] {0x00ff0000, 0x0000ff00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                 0x000000ff, 0xff000000},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                      null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        BufferedImage im = new BufferedImage(cm, raster, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Graphics g = im.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        g.drawImage(image, 0, 0, width, height, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int[] data = buffer.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int[] raw = new int[width * height + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        raw[0] = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        raw[1] = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        System.arraycopy(data, 0, raw, 2, width * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}