jdk/src/java.desktop/share/classes/java/awt/image/IndexColorModel.java
author avstepan
Mon, 11 Jan 2016 17:51:54 +0300
changeset 35667 ed476aba94de
parent 32865 f9cb6e427f9e
child 43827 a02bc4e52007
permissions -rw-r--r--
8138838: docs cleanup for java.desktop Summary: docs cleanup for java.desktop Reviewed-by: serb
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: 21278
diff changeset
     2
 * Copyright (c) 1995, 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    33
 * The {@code IndexColorModel} class is a {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * class that works with pixel values consisting of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * single sample that is an index into a fixed colormap in the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * sRGB color space.  The colormap specifies red, green, blue, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * optional alpha components corresponding to each index.  All components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * are represented in the colormap as 8-bit unsigned integral values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Some constructors allow the caller to specify "holes" in the colormap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * by indicating which colormap entries are valid and which represent
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    41
 * unusable colors via the bits set in a {@code BigInteger} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This color model is similar to an X11 PseudoColor visual.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Some constructors provide a means to specify an alpha component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for each pixel in the colormap, while others either provide no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * such means or, in some cases, a flag to indicate whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * colormap data contains alpha values.  If no alpha is supplied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the constructor, an opaque alpha component (alpha = 1.0) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * assumed for each entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * An optional transparent pixel value can be supplied that indicates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * pixel to be made completely transparent, regardless of any alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * component supplied or assumed for that pixel value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Note that the color components in the colormap of an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    54
 * {@code IndexColorModel} objects are never pre-multiplied with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the alpha components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a name="transparency">
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    58
 * The transparency of an {@code IndexColorModel} object is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * determined by examining the alpha components of the colors in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * colormap and choosing the most specific value after considering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * the optional alpha values and any transparent index specified.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    62
 * The transparency value is {@code Transparency.OPAQUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * only if all valid colors in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the colormap are opaque and there is no valid transparent pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * If all valid colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * in the colormap are either completely opaque (alpha = 1.0) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * completely transparent (alpha = 0.0), which typically occurs when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * a valid transparent pixel is specified,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    69
 * the value is {@code Transparency.BITMASK}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    70
 * Otherwise, the value is {@code Transparency.TRANSLUCENT}, indicating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * that some valid color has an alpha component that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * neither completely transparent nor completely opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * (0.0 &lt; alpha &lt; 1.0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    77
 * If an {@code IndexColorModel} object has
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    78
 * a transparency value of {@code Transparency.OPAQUE},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    79
 * then the {@code hasAlpha}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    80
 * and {@code getNumComponents} methods
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    81
 * (both inherited from {@code ColorModel})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * return false and 3, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * For any other transparency value,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    84
 * {@code hasAlpha} returns true
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    85
 * and {@code getNumComponents} returns 4.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <a name="index_values">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * The values used to index into the colormap are taken from the least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * significant <em>n</em> bits of pixel representations where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <em>n</em> is based on the pixel size specified in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * For pixel sizes smaller than 8 bits, <em>n</em> is rounded up to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * power of two (3 becomes 4 and 5,6,7 become 8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * For pixel sizes between 8 and 16 bits, <em>n</em> is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * pixel size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Pixel sizes larger than 16 bits are not supported by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Higher order bits beyond <em>n</em> are ignored in pixel representations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * Index values greater than or equal to the map size, but less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * 2<sup><em>n</em></sup>, are undefined and return 0 for all color and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * alpha components.
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 7762
diff changeset
   101
 * </a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * For those methods that use a primitive array pixel representation of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   104
 * type {@code transferType}, the array length is always one.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   105
 * The transfer types supported are {@code DataBuffer.TYPE_BYTE} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   106
 * {@code DataBuffer.TYPE_USHORT}.  A single int pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * representation is valid for all objects of this class, since it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * always possible to represent pixel values used with this class in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * single int.  Therefore, methods that use this representation do
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   110
 * not throw an {@code IllegalArgumentException} due to an invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * pixel value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Many of the methods in this class are final.  The reason for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * this is that the underlying native graphics code makes assumptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * about the layout and operation of this class and those assumptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * are reflected in the implementations of the methods here that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * marked final.  You can subclass this class for other reasons, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * you cannot override or modify the behaviour of those methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @see ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @see ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @see DataBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
public class IndexColorModel extends ColorModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private int rgb[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private int map_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private int pixel_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private int transparent_index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private boolean allgrayopaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private BigInteger validBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
6861
0c879c7c2ea2 6925710: IndexColorModel.finalize can be made to double free
bae
parents: 5506
diff changeset
   133
    private sun.awt.image.BufImgSurfaceData.ICMColorData colorData = null;
0c879c7c2ea2 6925710: IndexColorModel.finalize can be made to double free
bae
parents: 5506
diff changeset
   134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static int[] opaqueBits = {8, 8, 8};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static int[] alphaBits = {8, 8, 8, 8};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   138
    private static native void initIDs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        ColorModel.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   144
     * Constructs an {@code IndexColorModel} from the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * arrays of red, green, and blue components.  Pixels described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * by this color model all have alpha components of 255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * unnormalized (1.0&nbsp;normalized), which means they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * are fully opaque.  All of the arrays specifying the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * components must have at least the specified number of entries.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   150
     * The {@code ColorSpace} is the default sRGB space.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Since there is no alpha information in any of the arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * to this constructor, the transparency value is always
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   153
     * {@code Transparency.OPAQUE}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   154
     * The transfer type is the smallest of {@code DataBuffer.TYPE_BYTE}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   155
     * or {@code DataBuffer.TYPE_USHORT} that can hold a single pixel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param bits      the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param size      the size of the color component arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param r         the array of red color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param g         the array of green color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param b         the array of blue color components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   161
     * @throws IllegalArgumentException if {@code bits} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *         than 1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   163
     * @throws IllegalArgumentException if {@code size} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public IndexColorModel(int bits, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                           byte r[], byte g[], byte b[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        super(bits, opaqueBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
              ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
              false, false, OPAQUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
              ColorModel.getDefaultTransferType(bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        setRGBs(size, r, g, b, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        calculatePixelMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   181
     * Constructs an {@code IndexColorModel} from the given arrays
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * of red, green, and blue components.  Pixels described by this color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * model all have alpha components of 255 unnormalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * (1.0&nbsp;normalized), which means they are fully opaque, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * for the indicated pixel to be made transparent.  All of the arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * specifying the color components must have at least the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * number of entries.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   188
     * The {@code ColorSpace} is the default sRGB space.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   189
     * The transparency value may be {@code Transparency.OPAQUE} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   190
     * {@code Transparency.BITMASK} depending on the arguments, as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * specified in the <a href="#transparency">class description</a> above.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   192
     * The transfer type is the smallest of {@code DataBuffer.TYPE_BYTE}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   193
     * or {@code DataBuffer.TYPE_USHORT} that can hold a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * single pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param bits      the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param size      the size of the color component arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param r         the array of red color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param g         the array of green color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param b         the array of blue color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param trans     the index of the transparent pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   201
     * @throws IllegalArgumentException if {@code bits} is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *          1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   203
     * @throws IllegalArgumentException if {@code size} is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *          1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public IndexColorModel(int bits, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                           byte r[], byte g[], byte b[], int trans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        super(bits, opaqueBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
              ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
              false, false, OPAQUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
              ColorModel.getDefaultTransferType(bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        setRGBs(size, r, g, b, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        setTransparentPixel(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        calculatePixelMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   222
     * Constructs an {@code IndexColorModel} from the given
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * arrays of red, green, blue and alpha components.  All of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * arrays specifying the components must have at least the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * number of entries.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   226
     * The {@code ColorSpace} is the default sRGB space.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   227
     * The transparency value may be any of {@code Transparency.OPAQUE},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   228
     * {@code Transparency.BITMASK},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   229
     * or {@code Transparency.TRANSLUCENT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * depending on the arguments, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * in the <a href="#transparency">class description</a> above.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   232
     * The transfer type is the smallest of {@code DataBuffer.TYPE_BYTE}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   233
     * or {@code DataBuffer.TYPE_USHORT} that can hold a single pixel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param bits      the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param size      the size of the color component arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param r         the array of red color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param g         the array of green color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param b         the array of blue color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param a         the array of alpha value components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   240
     * @throws IllegalArgumentException if {@code bits} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *           than 1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   242
     * @throws IllegalArgumentException if {@code size} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *           than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public IndexColorModel(int bits, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                           byte r[], byte g[], byte b[], byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        super (bits, alphaBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
               ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
               true, false, TRANSLUCENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
               ColorModel.getDefaultTransferType(bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        setRGBs (size, r, g, b, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        calculatePixelMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   260
     * Constructs an {@code IndexColorModel} from a single
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * array of interleaved red, green, blue and optional alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * components.  The array must have enough values in it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * fill all of the needed component arrays of the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   264
     * size.  The {@code ColorSpace} is the default sRGB space.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   265
     * The transparency value may be any of {@code Transparency.OPAQUE},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   266
     * {@code Transparency.BITMASK},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   267
     * or {@code Transparency.TRANSLUCENT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * depending on the arguments, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * in the <a href="#transparency">class description</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * The transfer type is the smallest of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   271
     * {@code DataBuffer.TYPE_BYTE} or {@code DataBuffer.TYPE_USHORT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * that can hold a single pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param bits      the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param size      the size of the color component arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param cmap      the array of color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param start     the starting offset of the first color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param hasalpha  indicates whether alpha values are contained in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   279
     *                  the {@code cmap} array
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   280
     * @throws IllegalArgumentException if {@code bits} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *           than 1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   282
     * @throws IllegalArgumentException if {@code size} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *           than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public IndexColorModel(int bits, int size, byte cmap[], int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                           boolean hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this(bits, size, cmap, start, hasalpha, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   295
     * Constructs an {@code IndexColorModel} from a single array of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * interleaved red, green, blue and optional alpha components.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * specified transparent index represents a pixel that is made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * entirely transparent regardless of any alpha value specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * for it.  The array must have enough values in it to fill all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * of the needed component arrays of the specified size.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   301
     * The {@code ColorSpace} is the default sRGB space.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   302
     * The transparency value may be any of {@code Transparency.OPAQUE},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   303
     * {@code Transparency.BITMASK},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   304
     * or {@code Transparency.TRANSLUCENT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * depending on the arguments, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * in the <a href="#transparency">class description</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The transfer type is the smallest of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   308
     * {@code DataBuffer.TYPE_BYTE} or {@code DataBuffer.TYPE_USHORT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * that can hold a single pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param bits      the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param size      the size of the color component arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param cmap      the array of color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param start     the starting offset of the first color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param hasalpha  indicates whether alpha values are contained in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   315
     *                  the {@code cmap} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param trans     the index of the fully transparent pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   317
     * @throws IllegalArgumentException if {@code bits} is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *               1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   319
     * @throws IllegalArgumentException if {@code size} is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *               1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public IndexColorModel(int bits, int size, byte cmap[], int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                           boolean hasalpha, int trans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // REMIND: This assumes the ordering: RGB[A]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        super(bits, opaqueBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
              ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
              false, false, OPAQUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
              ColorModel.getDefaultTransferType(bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (size < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw new IllegalArgumentException("Map size ("+size+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                               ") must be >= 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        map_size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        rgb = new int[calcRealMapSize(bits, size)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        int j = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int alpha = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        boolean allgray = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int transparency = OPAQUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int r = cmap[j++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            int g = cmap[j++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            int b = cmap[j++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            allgray = allgray && (r == g) && (g == b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                alpha = cmap[j++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (alpha != 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (alpha == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        if (transparency == OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            transparency = BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        if (transparent_index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                            transparent_index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        transparency = TRANSLUCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    allgray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            rgb[i] = (alpha << 24) | (r << 16) | (g << 8) | b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        this.allgrayopaque = allgray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        setTransparency(transparency);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        setTransparentPixel(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        calculatePixelMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   374
     * Constructs an {@code IndexColorModel} from an array of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * ints where each int is comprised of red, green, blue, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * optional alpha components in the default RGB color model format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * The specified transparent index represents a pixel that is made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * entirely transparent regardless of any alpha value specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * for it.  The array must have enough values in it to fill all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * of the needed component arrays of the specified size.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   381
     * The {@code ColorSpace} is the default sRGB space.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   382
     * The transparency value may be any of {@code Transparency.OPAQUE},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   383
     * {@code Transparency.BITMASK},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   384
     * or {@code Transparency.TRANSLUCENT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * depending on the arguments, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * in the <a href="#transparency">class description</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param bits      the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param size      the size of the color component arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param cmap      the array of color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param start     the starting offset of the first color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param hasalpha  indicates whether alpha values are contained in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   392
     *                  the {@code cmap} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param trans     the index of the fully transparent pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param transferType the data type of the array used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *           pixel values.  The data type must be either
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   396
     *           {@code DataBuffer.TYPE_BYTE} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   397
     *           {@code DataBuffer.TYPE_USHORT}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   398
     * @throws IllegalArgumentException if {@code bits} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *           than 1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   400
     * @throws IllegalArgumentException if {@code size} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *           than 1
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   402
     * @throws IllegalArgumentException if {@code transferType} is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   403
     *           one of {@code DataBuffer.TYPE_BYTE} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   404
     *           {@code DataBuffer.TYPE_USHORT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public IndexColorModel(int bits, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                           int cmap[], int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                           boolean hasalpha, int trans, int transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // REMIND: This assumes the ordering: RGB[A]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        super(bits, opaqueBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
              ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
              false, false, OPAQUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
              transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (size < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throw new IllegalArgumentException("Map size ("+size+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                               ") must be >= 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if ((transferType != DataBuffer.TYPE_BYTE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            (transferType != DataBuffer.TYPE_USHORT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            throw new IllegalArgumentException("transferType must be either" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                "DataBuffer.TYPE_BYTE or DataBuffer.TYPE_USHORT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        setRGBs(size, cmap, start, hasalpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        setTransparentPixel(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        calculatePixelMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   435
     * Constructs an {@code IndexColorModel} from an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   436
     * {@code int} array where each {@code int} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * comprised of red, green, blue, and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * components in the default RGB color model format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * The array must have enough values in it to fill all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * of the needed component arrays of the specified size.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   441
     * The {@code ColorSpace} is the default sRGB space.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   442
     * The transparency value may be any of {@code Transparency.OPAQUE},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   443
     * {@code Transparency.BITMASK},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   444
     * or {@code Transparency.TRANSLUCENT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * depending on the arguments, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * in the <a href="#transparency">class description</a> above.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   447
     * The transfer type must be one of {@code DataBuffer.TYPE_BYTE}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   448
     * {@code DataBuffer.TYPE_USHORT}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   449
     * The {@code BigInteger} object specifies the valid/invalid pixels
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   450
     * in the {@code cmap} array.  A pixel is valid if the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   451
     * {@code BigInteger} value at that index is set, and is invalid
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   452
     * if the {@code BigInteger} bit  at that index is not set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param bits the number of bits each pixel occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param size the size of the color component array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param cmap the array of color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @param start the starting offset of the first color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param transferType the specified data type
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   458
     * @param validBits a {@code BigInteger} object.  If a bit is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *    set in the BigInteger, the pixel at that index is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *    If a bit is not set, the pixel at that index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *    is considered invalid.  If null, all pixels are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *    Only bits from 0 to the map size are considered.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   463
     * @throws IllegalArgumentException if {@code bits} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *           than 1 or greater than 16
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   465
     * @throws IllegalArgumentException if {@code size} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *           than 1
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   467
     * @throws IllegalArgumentException if {@code transferType} is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   468
     *           one of {@code DataBuffer.TYPE_BYTE} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   469
     *           {@code DataBuffer.TYPE_USHORT}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public IndexColorModel(int bits, int size, int cmap[], int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                           int transferType, BigInteger validBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        super (bits, alphaBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
               ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
               true, false, TRANSLUCENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
               transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (bits < 1 || bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                               +" 1 and 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (size < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throw new IllegalArgumentException("Map size ("+size+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                               ") must be >= 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if ((transferType != DataBuffer.TYPE_BYTE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            (transferType != DataBuffer.TYPE_USHORT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throw new IllegalArgumentException("transferType must be either" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                "DataBuffer.TYPE_BYTE or DataBuffer.TYPE_USHORT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (validBits != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            // Check to see if it is all valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            for (int i=0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (!validBits.testBit(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    this.validBits = validBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        setRGBs(size, cmap, start, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        calculatePixelMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    private void setRGBs(int size, byte r[], byte g[], byte b[], byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (size < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throw new IllegalArgumentException("Map size ("+size+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                               ") must be >= 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        map_size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        rgb = new int[calcRealMapSize(pixel_bits, size)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int alpha = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        int transparency = OPAQUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        boolean allgray = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            int rc = r[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            int gc = g[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            int bc = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            allgray = allgray && (rc == gc) && (gc == bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                alpha = a[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                if (alpha != 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    if (alpha == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        if (transparency == OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                            transparency = BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        if (transparent_index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            transparent_index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        transparency = TRANSLUCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    allgray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            rgb[i] = (alpha << 24) | (rc << 16) | (gc << 8) | bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        this.allgrayopaque = allgray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        setTransparency(transparency);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private void setRGBs(int size, int cmap[], int start, boolean hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        map_size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        rgb = new int[calcRealMapSize(pixel_bits, size)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        int j = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        int transparency = OPAQUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        boolean allgray = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        BigInteger validBits = this.validBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        for (int i = 0; i < size; i++, j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (validBits != null && !validBits.testBit(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            int cmaprgb = cmap[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            int r = (cmaprgb >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            int g = (cmaprgb >>  8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            int b = (cmaprgb      ) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            allgray = allgray && (r == g) && (g == b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if (hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                int alpha = cmaprgb >>> 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                if (alpha != 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    if (alpha == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        if (transparency == OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                            transparency = BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        if (transparent_index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                            transparent_index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        transparency = TRANSLUCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    allgray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                cmaprgb |= 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            rgb[i] = cmaprgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        this.allgrayopaque = allgray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        setTransparency(transparency);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    private int calcRealMapSize(int bits, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int newSize = Math.max(1 << bits, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return Math.max(newSize, 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private BigInteger getAllValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        int numbytes = (map_size+7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        byte[] valid = new byte[numbytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        java.util.Arrays.fill(valid, (byte)0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        valid[0] = (byte)(0xff >>> (numbytes*8 - map_size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return new BigInteger(1, valid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Returns the transparency.  Returns either OPAQUE, BITMASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * or TRANSLUCENT
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   602
     * @return the transparency of this {@code IndexColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see Transparency#OPAQUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see Transparency#BITMASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see Transparency#TRANSLUCENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public int getTransparency() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Returns an array of the number of bits for each color/alpha component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * The array contains the color components in the order red, green,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * blue, followed by the alpha component, if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @return an array containing the number of bits of each color
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   616
     *         and alpha component of this {@code IndexColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public int[] getComponentSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (nBits == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                nBits = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                nBits[3] = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                nBits = new int[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            nBits[0] = nBits[1] = nBits[2] = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
7747
44fa19e4dc49 7006948: FindBugs warning in IndexColorModel class
bae
parents: 6861
diff changeset
   629
        return nBits.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Returns the size of the color/alpha component arrays in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   634
     * {@code IndexColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @return the size of the color and alpha component arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   637
    public final int getMapSize() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return map_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Returns the index of a transparent pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   643
     * {@code IndexColorModel} or -1 if there is no pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * with an alpha value of 0.  If a transparent pixel was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * explicitly specified in one of the constructors by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * index, then that index will be preferred, otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * the index of any pixel which happens to be fully transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * may be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @return the index of a transparent pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   650
     *         {@code IndexColorModel} object, or -1 if there
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *         is no such pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   653
    public final int getTransparentPixel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return transparent_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Copies the array of red color components into the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Only the initial entries of the array as specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * {@link #getMapSize() getMapSize} are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param r the specified array into which the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *      array of red color components are copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   664
    public final void getReds(byte r[]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        for (int i = 0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            r[i] = (byte) (rgb[i] >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Copies the array of green color components into the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Only the initial entries of the array as specified by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   673
     * {@code getMapSize} are written.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param g the specified array into which the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *      array of green color components are copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   677
    public final void getGreens(byte g[]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        for (int i = 0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            g[i] = (byte) (rgb[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Copies the array of blue color components into the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Only the initial entries of the array as specified by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   686
     * {@code getMapSize} are written.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param b the specified array into which the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *      array of blue color components are copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   690
    public final void getBlues(byte b[]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        for (int i = 0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            b[i] = (byte) rgb[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Copies the array of alpha transparency components into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * specified array.  Only the initial entries of the array as specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   699
     * by {@code getMapSize} are written.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param a the specified array into which the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *      array of alpha components are copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   703
    public final void getAlphas(byte a[]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        for (int i = 0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            a[i] = (byte) (rgb[i] >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Converts data for each index from the color and alpha component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * arrays to an int in the default RGB ColorModel format and copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * the resulting 32-bit ARGB values into the specified array.  Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * the initial entries of the array as specified by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   714
     * {@code getMapSize} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @param rgb the specified array into which the converted ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *        values from this array of color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *        are copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   720
    public final void getRGBs(int rgb[]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        System.arraycopy(this.rgb, 0, rgb, 0, map_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private void setTransparentPixel(int trans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (trans >= 0 && trans < map_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            rgb[trans] &= 0x00ffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            transparent_index = trans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            allgrayopaque = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            if (this.transparency == OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                setTransparency(BITMASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private void setTransparency(int transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (this.transparency != transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            this.transparency = transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (transparency == OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                supportsAlpha = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                numComponents = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                nBits = opaqueBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                supportsAlpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                numComponents = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                nBits = alphaBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * This method is called from the constructors to set the pixel_mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * value, which is based on the value of pixel_bits.  The pixel_mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * value is used to mask off the pixel parameters for methods such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * as getRed(), getGreen(), getBlue(), getAlpha(), and getRGB().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    private final void calculatePixelMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        // Note that we adjust the mask so that our masking behavior here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        // is consistent with that of our native rendering loops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        int maskbits = pixel_bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (maskbits == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            maskbits = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        } else if (maskbits > 4 && maskbits < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            maskbits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        pixel_mask = (1 << maskbits) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * Returns the red color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  The pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * Only the lower <em>n</em> bits of the pixel value, as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * <a href="#index_values">class description</a> above, are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * calculate the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * The returned value is a non pre-multiplied value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @return the value of the red color component for the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   779
    public final int getRed(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return (rgb[pixel & pixel_mask] >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Returns the green color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  The pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Only the lower <em>n</em> bits of the pixel value, as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <a href="#index_values">class description</a> above, are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * calculate the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * The returned value is a non pre-multiplied value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @return the value of the green color component for the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   794
    public final int getGreen(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        return (rgb[pixel & pixel_mask] >> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Returns the blue color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  The pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * Only the lower <em>n</em> bits of the pixel value, as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <a href="#index_values">class description</a> above, are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * calculate the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * The returned value is a non pre-multiplied value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @return the value of the blue color component for the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   809
    public final int getBlue(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        return rgb[pixel & pixel_mask] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Returns the alpha component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * from 0 to 255.  The pixel value is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Only the lower <em>n</em> bits of the pixel value, as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <a href="#index_values">class description</a> above, are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * calculate the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @return the value of the alpha component for the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   822
    public final int getAlpha(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return (rgb[pixel & pixel_mask] >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * Returns the color/alpha components of the pixel in the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * RGB color model format.  The pixel value is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Only the lower <em>n</em> bits of the pixel value, as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * <a href="#index_values">class description</a> above, are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * calculate the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * The returned value is in a non pre-multiplied format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @return the color and alpha components of the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   837
    public final int getRGB(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return rgb[pixel & pixel_mask];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private static final int CACHESIZE = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    private int lookupcache[] = new int[CACHESIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Returns a data element array representation of a pixel in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * ColorModel, given an integer pixel representation in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * default RGB color model.  This array can then be passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * {@link WritableRaster#setDataElements(int, int, java.lang.Object) setDataElements}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * method of a {@link WritableRaster} object.  If the pixel variable is
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   850
     * {@code null}, a new array is allocated.  If {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   851
     * is not {@code null}, it must be
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   852
     * a primitive array of type {@code transferType}; otherwise, a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   853
     * {@code ClassCastException} is thrown.  An
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   854
     * {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   855
     * thrown if {@code pixel} is not large enough to hold a pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   856
     * value for this {@code ColorModel}.  The pixel array is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   858
     * Since {@code IndexColorModel} can be subclassed, subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * override it then they throw an exception if they use an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   861
     * unsupported {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @param rgb the integer pixel representation in the default RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @return an array representation of the specified pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   867
     *  {@code IndexColorModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   868
     * @throws ClassCastException if {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   869
     *  is not a primitive array of type {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @throws ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   871
     *  {@code pixel} is not large enough to hold a pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   872
     *  for this {@code ColorModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   873
     * @throws UnsupportedOperationException if {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *         is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public synchronized Object getDataElements(int rgb, Object pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        int red = (rgb>>16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        int green = (rgb>>8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int blue  = rgb & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int alpha = (rgb>>>24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int pix = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        // Note that pixels are stored at lookupcache[2*i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        // and the rgb that was searched is stored at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        // lookupcache[2*i+1].  Also, the pixel is first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        // inverted using the unary complement operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // before storing in the cache so it can never be 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        for (int i = CACHESIZE - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if ((pix = lookupcache[i]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            if (rgb == lookupcache[i+1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                return installpixel(pixel, ~pix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (allgrayopaque) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            // IndexColorModel objects are all tagged as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            // non-premultiplied so ignore the alpha value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            // of the incoming color, convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            // non-premultiplied color components to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            // grayscale value and search for the closest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            // gray value in the palette.  Since all colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            // in the palette are gray, we only need compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            // to one of the color components for a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            // using a simple linear distance formula.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            int minDist = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            int d;
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   912
            int gray = (red*77 + green*150 + blue*29 + 128)/256;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            for (int i = 0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                if (this.rgb[i] == 0x0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    // For allgrayopaque colormaps, entries are 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    // iff they are an invalid color and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    // ignored during color searches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                d = (this.rgb[i] & 0xff) - gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                if (d < 0) d = -d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                if (d < minDist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    pix = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    if (d == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    minDist = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        } else if (transparency == OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            // IndexColorModel objects are all tagged as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            // non-premultiplied so ignore the alpha value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            // of the incoming color and search for closest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            // color match independently using a 3 component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            // Euclidean distance formula.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            // For opaque colormaps, palette entries are 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            // iff they are an invalid color and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            // ignored during color searches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            // As an optimization, exact color searches are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            // likely to be fairly common in opaque colormaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            // so first we will do a quick search for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            // exact match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            int smallestError = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            int lut[] = this.rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            int lutrgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            for (int i=0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                lutrgb = lut[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                if (lutrgb == rgb && lutrgb != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    pix = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    smallestError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (smallestError != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                for (int i=0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    lutrgb = lut[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    if (lutrgb == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    int tmp = ((lutrgb >> 16) & 0xff) - red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    int currentError = tmp*tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    if (currentError < smallestError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        tmp = ((lutrgb >> 8) & 0xff) - green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        currentError += tmp * tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                        if (currentError < smallestError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                            tmp = (lutrgb & 0xff) - blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                            currentError += tmp * tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                            if (currentError < smallestError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                                pix = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                                smallestError = currentError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        } else if (alpha == 0 && transparent_index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            // Special case - transparent color maps to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            // specified transparent pixel, if there is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            pix = transparent_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            // IndexColorModel objects are all tagged as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            // non-premultiplied so use non-premultiplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            // color components in the distance calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            // Look for closest match using a 4 component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            // Euclidean distance formula.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            int smallestError = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            int lut[] = this.rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            for (int i=0; i < map_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                int lutrgb = lut[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                if (lutrgb == rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    if (validBits != null && !validBits.testBit(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    pix = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                int tmp = ((lutrgb >> 16) & 0xff) - red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                int currentError = tmp*tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                if (currentError < smallestError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    tmp = ((lutrgb >> 8) & 0xff) - green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    currentError += tmp * tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    if (currentError < smallestError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                        tmp = (lutrgb & 0xff) - blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                        currentError += tmp * tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        if (currentError < smallestError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                            tmp = (lutrgb >>> 24) - alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                            currentError += tmp * tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                            if (currentError < smallestError &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                                (validBits == null || validBits.testBit(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                pix = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                                smallestError = currentError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        System.arraycopy(lookupcache, 2, lookupcache, 0, CACHESIZE - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        lookupcache[CACHESIZE - 1] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        lookupcache[CACHESIZE - 2] = ~pix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        return installpixel(pixel, pix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    private Object installpixel(Object pixel, int pix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            int[] intObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                pixel = intObj = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                intObj = (int[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            intObj[0] = pix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            byte[] byteObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                pixel = byteObj = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                byteObj = (byte[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            byteObj[0] = (byte) pix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            short[] shortObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                pixel = shortObj = new short[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                shortObj = (short[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            shortObj[0] = (short) pix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                             "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        return pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Returns an array of unnormalized color/alpha components for a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1070
     * specified pixel in this {@code ColorModel}.  The pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1071
     * is specified as an int.  If the {@code components} array is {@code null},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * a new array is allocated that contains
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1073
     * {@code offset + getNumComponents()} elements.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1074
     * The {@code components} array is returned,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * with the alpha component included
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1076
     * only if {@code hasAlpha} returns true.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1077
     * Color/alpha components are stored in the {@code components} array starting
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1078
     * at {@code offset} even if the array is allocated by this method.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1079
     * An {@code ArrayIndexOutOfBoundsException}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1080
     * is thrown if  the {@code components} array is not {@code null} and is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * not large enough to hold all the color and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1082
     * starting at {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @param components the array to receive the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * components of the specified pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1086
     * @param offset the offset into the {@code components} array at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * which to start storing the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @return an array containing the color and alpha components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * specified pixel starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @see ColorModel#hasAlpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @see ColorModel#getNumComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    public int[] getComponents(int pixel, int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (components == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            components = new int[offset+numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        // REMIND: Needs to change if different color space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        components[offset+0] = getRed(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        components[offset+1] = getGreen(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        components[offset+2] = getBlue(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (supportsAlpha && (components.length-offset) > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            components[offset+3] = getAlpha(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        return components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * Returns an array of unnormalized color/alpha components for
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1111
     * a specified pixel in this {@code ColorModel}.  The pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * value is specified by an array of data elements of type
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1113
     * {@code transferType} passed in as an object reference.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1114
     * If {@code pixel} is not a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1115
     * {@code transferType}, a {@code ClassCastException}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1116
     * is thrown.  An {@code ArrayIndexOutOfBoundsException}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1117
     * is thrown if {@code pixel} is not large enough to hold
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1118
     * a pixel value for this {@code ColorModel}.  If the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1119
     * {@code components} array is {@code null}, a new array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * is allocated that contains
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1121
     * {@code offset + getNumComponents()} elements.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1122
     * The {@code components} array is returned,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * with the alpha component included
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1124
     * only if {@code hasAlpha} returns true.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1125
     * Color/alpha components are stored in the {@code components}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1126
     * array starting at {@code offset} even if the array is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * allocated by this method.  An
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1128
     * {@code ArrayIndexOutOfBoundsException} is also
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1129
     * thrown if  the {@code components} array is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1130
     * {@code null} and is not large enough to hold all the color
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1131
     * and alpha components starting at {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1133
     * Since {@code IndexColorModel} can be subclassed, subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * override it then they throw an exception if they use an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1136
     * unsupported {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @param components an array that receives the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * components of the specified pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1141
     * @param offset the index into the {@code components} array at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * which to begin storing the color and alpha components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @return an array containing the color and alpha components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * specified pixel starting at the specified offset.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1146
     * @throws ArrayIndexOutOfBoundsException if {@code pixel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *            is not large enough to hold a pixel value for this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1148
     *            {@code ColorModel} or if the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1149
     *            {@code components} array is not {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     *            and is not large enough to hold all the color
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1151
     *            and alpha components starting at {@code offset}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1152
     * @throws ClassCastException if {@code pixel} is not a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1153
     *            primitive array of type {@code transferType}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1154
     * @throws UnsupportedOperationException if {@code transferType}
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19169
diff changeset
  1155
     *         is not one of the supported transfer types
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * @see ColorModel#hasAlpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @see ColorModel#getNumComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    public int[] getComponents(Object pixel, int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        int intpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
               byte bdata[] = (byte[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
               intpixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
               short sdata[] = (short[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
               intpixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
               int idata[] = (int[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
               intpixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        return getComponents(intpixel, components, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Returns a pixel value represented as an int in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1183
     * {@code ColorModel} given an array of unnormalized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * color/alpha components.  An
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1185
     * {@code ArrayIndexOutOfBoundsException}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1186
     * is thrown if the {@code components} array is not large
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * enough to hold all of the color and alpha components starting
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1188
     * at {@code offset}.  Since
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1189
     * {@code ColorModel} can be subclassed, subclasses inherit the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * implementation of this method and if they don't override it then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * they throw an exception if they use an unsupported transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @param components an array of unnormalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1194
     * @param offset the index into {@code components} at which to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * begin retrieving the color and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1196
     * @return an {@code int} pixel value in this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1197
     * {@code ColorModel} corresponding to the specified components.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @throws ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1199
     *  the {@code components} array is not large enough to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *  hold all of the color and alpha components starting at
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1201
     *  {@code offset}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1202
     * @throws UnsupportedOperationException if {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *         is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    public int getDataElement(int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        int rgb = (components[offset+0]<<16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            | (components[offset+1]<<8) | (components[offset+2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            rgb |= (components[offset+3]<<24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            rgb |= 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        Object inData = getDataElements(rgb, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        int pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
               byte bdata[] = (byte[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
               pixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
               short sdata[] = (short[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
               pixel = sdata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
               int idata[] = (int[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
               pixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        return pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * Returns a data element array representation of a pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1238
     * {@code ColorModel} given an array of unnormalized color/alpha
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * components.  This array can then be passed to the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1240
     * {@code setDataElements} method of a {@code WritableRaster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1241
     * object.  An {@code ArrayIndexOutOfBoundsException} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * thrown if the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1243
     * {@code components} array is not large enough to hold all of the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1244
     * color and alpha components starting at {@code offset}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1245
     * If the pixel variable is {@code null}, a new array
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1246
     * is allocated.  If {@code pixel} is not {@code null},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1247
     * it must be a primitive array of type {@code transferType};
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1248
     * otherwise, a {@code ClassCastException} is thrown.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1249
     * An {@code ArrayIndexOutOfBoundsException} is thrown if pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * is not large enough to hold a pixel value for this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1251
     * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1253
     * Since {@code IndexColorModel} can be subclassed, subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * override it then they throw an exception if they use an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1256
     * unsupported {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @param components an array of unnormalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1260
     * @param offset the index into {@code components} at which to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * begin retrieving color and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1262
     * @param pixel the {@code Object} representing an array of color
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1264
     * @return an {@code Object} representing an array of color and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * alpha components.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1266
     * @throws ClassCastException if {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1267
     *  is not a primitive array of type {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @throws ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1269
     *  {@code pixel} is not large enough to hold a pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1270
     *  for this {@code ColorModel} or the {@code components}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *  array is not large enough to hold all of the color and alpha
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1272
     *  components starting at {@code offset}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1273
     * @throws UnsupportedOperationException if {@code transferType}
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19169
diff changeset
  1274
     *         is not one of the supported transfer types
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    public Object getDataElements(int[] components, int offset, Object pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        int rgb = (components[offset+0]<<16) | (components[offset+1]<<8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            | (components[offset+2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            rgb |= (components[offset+3]<<24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            rgb &= 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return getDataElements(rgb, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1291
     * Creates a {@code WritableRaster} with the specified width
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1292
     * and height that has a data layout ({@code SampleModel})
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1293
     * compatible with this {@code ColorModel}.  This method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * only works for color models with 16 or fewer bits per pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1296
     * Since {@code IndexColorModel} can be subclassed, any
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * subclass that supports greater than 16 bits per pixel must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1300
     * @param w the width to apply to the new {@code WritableRaster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1301
     * @param h the height to apply to the new {@code WritableRaster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1302
     * @return a {@code WritableRaster} object with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @throws UnsupportedOperationException if the number of bits in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *         pixel is greater than 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * @see WritableRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    public WritableRaster createCompatibleWritableRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        WritableRaster raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            // TYPE_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                               w, h, 1, pixel_bits, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        else if (pixel_bits <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                                                  w,h,1,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        else if (pixel_bits <= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                                                  w,h,1,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                UnsupportedOperationException("This method is not supported "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                                              " for pixel bits > 16.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        return raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1334
      * Returns {@code true} if {@code raster} is compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1335
      * with this {@code ColorModel} or {@code false} if it
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1336
      * is not compatible with this {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
      * @param raster the {@link Raster} object to test for compatibility
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1338
      * @return {@code true} if {@code raster} is compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1339
      * with this {@code ColorModel}; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    public boolean isCompatibleRaster(Raster raster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        int size = raster.getSampleModel().getSampleSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        return ((raster.getTransferType() == transferType) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                (raster.getNumBands() == 1) && ((1 << size) >= map_size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1350
     * Creates a {@code SampleModel} with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * width and height that has a data layout compatible with
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1352
     * this {@code ColorModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1353
     * @param w the width to apply to the new {@code SampleModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1354
     * @param h the height to apply to the new {@code SampleModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1355
     * @return a {@code SampleModel} object with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * width and height.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1357
     * @throws IllegalArgumentException if {@code w} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1358
     *         {@code h} is not greater than 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    public SampleModel createCompatibleSampleModel(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        int[] off = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        off[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            return new MultiPixelPackedSampleModel(transferType, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                                                   pixel_bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            return new ComponentSampleModel(transferType, w, h, 1, w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                                            off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1375
     * Checks if the specified {@code SampleModel} is compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1376
     * with this {@code ColorModel}.  If {@code sm} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1377
     * {@code null}, this method returns {@code false}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1378
     * @param sm the specified {@code SampleModel},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1379
     *           or {@code null}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1380
     * @return {@code true} if the specified {@code SampleModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1381
     * is compatible with this {@code ColorModel}; {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    public boolean isCompatibleSampleModel(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        // fix 4238629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        if (! (sm instanceof ComponentSampleModel) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            ! (sm instanceof MultiPixelPackedSampleModel)   ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        // Transfer type must be the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        if (sm.getTransferType() != transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        if (sm.getNumBands() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1405
     * Returns a new {@code BufferedImage} of TYPE_INT_ARGB or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1406
     * TYPE_INT_RGB that has a {@code Raster} with pixel data
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1407
     * computed by expanding the indices in the source {@code Raster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1408
     * using the color/alpha component arrays of this {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * Only the lower <em>n</em> bits of each index value in the source
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1410
     * {@code Raster}, as specified in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * <a href="#index_values">class description</a> above, are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * compute the color/alpha values in the returned image.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1413
     * If {@code forceARGB} is {@code true}, a TYPE_INT_ARGB image is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1414
     * returned regardless of whether or not this {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * has an alpha component array or a transparent pixel.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1416
     * @param raster the specified {@code Raster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1417
     * @param forceARGB if {@code true}, the returned
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1418
     *     {@code BufferedImage} is TYPE_INT_ARGB; otherwise it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *     TYPE_INT_RGB
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1420
     * @return a {@code BufferedImage} created with the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1421
     *     {@code Raster}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * @throws IllegalArgumentException if the raster argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *           compatible with this IndexColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    public BufferedImage convertToIntDiscrete(Raster raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                                              boolean forceARGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        ColorModel cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (!isCompatibleRaster(raster)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            throw new IllegalArgumentException("This raster is not compatible" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                 "with this IndexColorModel.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (forceARGB || transparency == TRANSLUCENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            cm = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        else if (transparency == BITMASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            cm = new DirectColorModel(25, 0xff0000, 0x00ff00, 0x0000ff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                                      0x1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            cm = new DirectColorModel(24, 0xff0000, 0x00ff00, 0x0000ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        int w = raster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        int h = raster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        WritableRaster discreteRaster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                  cm.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        Object obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        int[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        int rX = raster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        int rY = raster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        for (int y=0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            obj = raster.getDataElements(rX, rY, w, 1, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            if (obj instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                data = (int[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                data = DataBuffer.toIntArray(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            for (int x=0; x < w; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                data[x] = rgb[data[x] & pixel_mask];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            discreteRaster.setDataElements(0, y, w, 1, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        return new BufferedImage(cm, discreteRaster, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * Returns whether or not the pixel is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * @param pixel the specified pixel value
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1473
     * @return {@code true} if {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1474
     * is valid; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    public boolean isValid(int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        return ((pixel >= 0 && pixel < map_size) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                (validBits == null || validBits.testBit(pixel)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * Returns whether or not all of the pixels are valid.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1484
     * @return {@code true} if all pixels are valid;
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1485
     * {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        return (validBits == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1493
     * Returns a {@code BigInteger} that indicates the valid/invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * pixels in the colormap.  A bit is valid if the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1495
     * {@code BigInteger} value at that index is set, and is invalid
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1496
     * if the {@code BigInteger} value at that index is not set.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1497
     * The only valid ranges to query in the {@code BigInteger} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * between 0 and the map size.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1499
     * @return a {@code BigInteger} indicating the valid/invalid pixels.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    public BigInteger getValidPixels() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        if (validBits == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            return getAllValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            return validBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * Disposes of system resources associated with this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1513
     * {@code ColorModel} once this {@code ColorModel} is no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * longer referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    public void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1520
     * Returns the {@code String} representation of the contents of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1521
     * this {@code ColorModel} object.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1522
     * @return a {@code String} representing the contents of this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1523
     * {@code ColorModel} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
       return new String("IndexColorModel: #pixelBits = "+pixel_bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                         + " numComponents = "+numComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                         + " color space = "+colorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                         + " transparency = "+transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                         + " transIndex   = "+transparent_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                         + " has alpha = "+supportsAlpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                         + " isAlphaPre = "+isAlphaPremultiplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                         );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
}