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