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