src/java.desktop/share/classes/java/awt/image/PackedColorModel.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211300: Convert C-style array declarations in JDK client code Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
     2
 * Copyright (c) 1997, 2017, 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;
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
    30
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    33
 * The {@code PackedColorModel} class is an abstract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * {@link ColorModel} class that works with pixel values which represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * color and alpha information as separate samples and which pack all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * samples for a single pixel into a single int, short, or byte quantity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class can be used with an arbitrary {@link ColorSpace}.  The number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * color samples in the pixel values must be the same as the number of color
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    39
 * components in the {@code ColorSpace}.  There can be a single alpha
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * sample.  The array length is always 1 for those methods that use a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    41
 * primitive array pixel representation of type {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The transfer types supported are DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Color and alpha samples are stored in the single element of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * in bits indicated by bit masks.  Each bit mask must be contiguous and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * masks must not overlap.  The same masks apply to the single int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * pixel representation used by other methods.  The correspondence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * masks and color/alpha samples is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li> Masks are identified by indices running from 0 through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link ColorModel#getNumComponents() getNumComponents}&nbsp;-&nbsp;1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li> The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link ColorModel#getNumColorComponents() getNumColorComponents}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * indices refer to color samples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li> If an alpha sample is present, it corresponds the last index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <li> The order of the color indices is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    57
 * by the {@code ColorSpace}.  Typically, this reflects the name of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the color space type (for example, TYPE_RGB), index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * corresponds to red, index 1 to green, and index 2 to blue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The translation from pixel values to color/alpha components for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * display or processing purposes is a one-to-one correspondence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * samples to components.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    65
 * A {@code PackedColorModel} is typically used with image data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * that uses masks to define packed samples.  For example, a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    67
 * {@code PackedColorModel} can be used in conjunction with a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * {@link SinglePixelPackedSampleModel} to construct a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * {@link BufferedImage}.  Normally the masks used by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    70
 * {@link SampleModel} and the {@code ColorModel} would be the same.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * However, if they are different, the color interpretation of pixel data is
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    72
 * done according to the masks of the {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    74
 * A single {@code int} pixel representation is valid for all objects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * of this class since it is always possible to represent pixel values
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    76
 * used with this class in a single {@code int}.  Therefore, methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * that use this representation do not throw an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    78
 * {@code IllegalArgumentException} due to an invalid pixel value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    80
 * A subclass of {@code PackedColorModel} is {@link DirectColorModel},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * which is similar to an X11 TrueColor visual.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see DirectColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see SinglePixelPackedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see BufferedImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
public abstract class PackedColorModel extends ColorModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    int[] maskArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    int[] maskOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    float[] scaleFactors;
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
    92
    private volatile int hashCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    95
     * Constructs a {@code PackedColorModel} from a color mask array,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    96
     * which specifies which bits in an {@code int} pixel representation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * contain each of the color samples, and an alpha mask.  Color
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    98
     * components are in the specified {@code ColorSpace}.  The length of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    99
     * {@code colorMaskArray} should be the number of components in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   100
     * the {@code ColorSpace}.  All of the bits in each mask
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * must be contiguous and fit in the specified number of least significant
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   102
     * bits of an {@code int} pixel representation.  If the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   103
     * {@code alphaMask} is 0, there is no alpha.  If there is alpha,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   104
     * the {@code boolean isAlphaPremultiplied} specifies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * how to interpret color and alpha samples in pixel values.  If the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   106
     * {@code boolean} is {@code true}, color samples are assumed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * to have been multiplied by the alpha sample.  The transparency,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   108
     * {@code trans}, specifies what alpha values can be represented
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * by this color model.  The transfer type is the type of primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * array used to represent pixel values.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   111
     * @param space the specified {@code ColorSpace}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param bits the number of bits in the pixel values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param colorMaskArray array that specifies the masks representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *         the bits of the pixel values that represent the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *         components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param alphaMask specifies the mask representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *         the bits of the pixel values that represent the alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *         component
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   119
     * @param isAlphaPremultiplied {@code true} if color samples are
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   120
     *        premultiplied by the alpha sample; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param trans specifies the alpha value that can be represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *        this color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param transferType the type of array used to represent pixel values
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   124
     * @throws IllegalArgumentException if {@code bits} is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *         1 or greater than 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public PackedColorModel (ColorSpace space, int bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                             int[] colorMaskArray, int alphaMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                             boolean isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                             int trans, int transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        super(bits, PackedColorModel.createBitsArray(colorMaskArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                                     alphaMask),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
              space, (alphaMask == 0 ? false : true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
              isAlphaPremultiplied, trans, transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (bits < 1 || bits > 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalArgumentException("Number of bits must be between"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                               +" 1 and 32.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        maskArray   = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        maskOffsets = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        scaleFactors = new float[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for (int i=0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // Get the mask offset and #bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            DecomposeMask(colorMaskArray[i], i, space.getName(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (alphaMask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            DecomposeMask(alphaMask, numColorComponents, "alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (nBits[numComponents-1] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                transparency = Transparency.BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   156
     * Constructs a {@code PackedColorModel} from the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   157
     * masks which indicate which bits in an {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * representation contain the alpha, red, green and blue color samples.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   159
     * Color components are in the specified {@code ColorSpace}, which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * must be of type ColorSpace.TYPE_RGB.  All of the bits in each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * mask must be contiguous and fit in the specified number of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   162
     * least significant bits of an {@code int} pixel representation.  If
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   163
     * {@code amask} is 0, there is no alpha.  If there is alpha,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   164
     * the {@code boolean isAlphaPremultiplied}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * specifies how to interpret color and alpha samples
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   166
     * in pixel values.  If the {@code boolean} is {@code true},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * color samples are assumed to have been multiplied by the alpha sample.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   168
     * The transparency, {@code trans}, specifies what alpha values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * can be represented by this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The transfer type is the type of primitive array used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * pixel values.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   172
     * @param space the specified {@code ColorSpace}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param bits the number of bits in the pixel values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param rmask specifies the mask representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *         the bits of the pixel values that represent the red
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param gmask specifies the mask representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *         the bits of the pixel values that represent the green
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *         color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param bmask specifies the mask representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *         the bits of the pixel values that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         the blue color component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param amask specifies the mask representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         the bits of the pixel values that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *         the alpha component
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   186
     * @param isAlphaPremultiplied {@code true} if color samples are
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   187
     *        premultiplied by the alpha sample; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param trans specifies the alpha value that can be represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *        this color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param transferType the type of array used to represent pixel values
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   191
     * @throws IllegalArgumentException if {@code space} is not a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *         TYPE_RGB space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public PackedColorModel(ColorSpace space, int bits, int rmask, int gmask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            int bmask, int amask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                            boolean isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            int trans, int transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        super (bits, PackedColorModel.createBitsArray(rmask, gmask, bmask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                                      amask),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
               space, (amask == 0 ? false : true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
               isAlphaPremultiplied, trans, transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (space.getType() != ColorSpace.TYPE_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new IllegalArgumentException("ColorSpace must be TYPE_RGB.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        maskArray = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        maskOffsets = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        scaleFactors = new float[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        DecomposeMask(rmask, 0, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        DecomposeMask(gmask, 1, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        DecomposeMask(bmask, 2, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (amask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            DecomposeMask(amask, 3, "alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (nBits[3] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                transparency = Transparency.BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns the mask indicating which bits in a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * contain the specified color/alpha sample.  For color
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   228
     * samples, {@code index} corresponds to the placement of color
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   229
     * sample names in the color space.  Thus, an {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * equal to 0 for a CMYK ColorSpace would correspond to
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   231
     * Cyan and an {@code index} equal to 1 would correspond to
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   232
     * Magenta.  If there is alpha, the alpha {@code index} would be:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *      alphaIndex = numComponents() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param index the specified color or alpha sample
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   237
     * @return the mask, which indicates which bits of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         pixel representation contain the color or alpha sample specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   239
     *         by {@code index}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   240
     * @throws ArrayIndexOutOfBoundsException if {@code index} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         greater than the number of components minus 1 in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   242
     *         {@code PackedColorModel} or if {@code index} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *         less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   245
    public final int getMask(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return maskArray[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Returns a mask array indicating which bits in a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * contain the color and alpha samples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return the mask array , which indicates which bits of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   253
     *         {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *         representation contain the color or alpha samples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   256
    public final int[] getMasks() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 9035
diff changeset
   257
        return maskArray.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * A utility function to compute the mask offset and scalefactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * store these and the mask in instance arrays, and verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the mask fits in the specified pixel size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private void DecomposeMask(int mask,  int idx, String componentName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int count = nBits[idx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Store the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        maskArray[idx]   = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // Now find the shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (mask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            while ((mask & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                mask >>>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                off++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (off + count > pixel_bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new IllegalArgumentException(componentName + " mask "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                        Integer.toHexString(maskArray[idx])+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                               " overflows pixel (expecting "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                               pixel_bits+" bits");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        maskOffsets[idx] = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // High enough to scale any 0-ff value down to 0.0, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // high enough to get Infinity when scaling back to pixel bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            scaleFactors[idx] = 256.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            scaleFactors[idx] = 255.0f / ((1 << count) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   299
     * Creates a {@code SampleModel} with the specified width and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * height that has a data layout compatible with this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   301
     * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param w the width (in pixels) of the region of the image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *          described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param h the height (in pixels) of the region of the image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *          described
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   306
     * @return the newly created {@code SampleModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   307
     * @throws IllegalArgumentException if {@code w} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   308
     *         {@code h} is not greater than 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public SampleModel createCompatibleSampleModel(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return new SinglePixelPackedSampleModel(transferType, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                                maskArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   317
     * Checks if the specified {@code SampleModel} is compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   318
     * with this {@code ColorModel}.  If {@code sm} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   319
     * {@code null}, this method returns {@code false}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   320
     * @param sm the specified {@code SampleModel},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   321
     * or {@code null}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   322
     * @return {@code true} if the specified {@code SampleModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   323
     *         is compatible with this {@code ColorModel};
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   324
     *         {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public boolean isCompatibleSampleModel(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (! (sm instanceof SinglePixelPackedSampleModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // Must have the same number of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (numComponents != sm.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // Transfer type must be the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (sm.getTransferType() != transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Now compare the specific masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int[] bitMasks = sppsm.getBitMasks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (bitMasks.length != maskArray.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
8750
50fec4bad1f7 7014528: ColorModel and SampleModel gotten from the same ImageTypeSpecifier instance can be not compatible
bae
parents: 5506
diff changeset
   348
50fec4bad1f7 7014528: ColorModel and SampleModel gotten from the same ImageTypeSpecifier instance can be not compatible
bae
parents: 5506
diff changeset
   349
        /* compare 'effective' masks only, i.e. only part of the mask
50fec4bad1f7 7014528: ColorModel and SampleModel gotten from the same ImageTypeSpecifier instance can be not compatible
bae
parents: 5506
diff changeset
   350
         * which fits the capacity of the transfer type.
50fec4bad1f7 7014528: ColorModel and SampleModel gotten from the same ImageTypeSpecifier instance can be not compatible
bae
parents: 5506
diff changeset
   351
         */
50fec4bad1f7 7014528: ColorModel and SampleModel gotten from the same ImageTypeSpecifier instance can be not compatible
bae
parents: 5506
diff changeset
   352
        int maxMask = (int)((1L << DataBuffer.getDataTypeSize(transferType)) - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        for (int i=0; i < bitMasks.length; i++) {
8750
50fec4bad1f7 7014528: ColorModel and SampleModel gotten from the same ImageTypeSpecifier instance can be not compatible
bae
parents: 5506
diff changeset
   354
            if ((maxMask & bitMasks[i]) != (maxMask & maskArray[i])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Returns a {@link WritableRaster} representing the alpha channel of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   364
     * an image, extracted from the input {@code WritableRaster}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   365
     * This method assumes that {@code WritableRaster} objects
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   366
     * associated with this {@code ColorModel} store the alpha band,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   367
     * if present, as the last band of image data.  Returns {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * if there is no separate spatial alpha channel associated with this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   369
     * {@code ColorModel}.  This method creates a new
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   370
     * {@code WritableRaster}, but shares the data array.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   371
     * @param raster a {@code WritableRaster} containing an image
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   372
     * @return a {@code WritableRaster} that represents the alpha
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   373
     *         channel of the image contained in {@code raster}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public WritableRaster getAlphaRaster(WritableRaster raster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (hasAlpha() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int x = raster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int y = raster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int[] band = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        band[0] = raster.getNumBands() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return raster.createWritableChild(x, y, raster.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                          raster.getHeight(), x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                          band);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   390
     * Tests if the specified {@code Object} is an instance
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   391
     * of {@code PackedColorModel} and equals this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   392
     * {@code PackedColorModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   393
     * @param obj the {@code Object} to test for equality
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   394
     * @return {@code true} if the specified {@code Object}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   395
     * is an instance of {@code PackedColorModel} and equals this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   396
     * {@code PackedColorModel}; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   398
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (!(obj instanceof PackedColorModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   404
        PackedColorModel cm = (PackedColorModel) obj;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   405
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   406
        if (supportsAlpha != cm.hasAlpha() ||
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   407
            isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   408
            pixel_bits != cm.getPixelSize() ||
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   409
            transparency != cm.getTransparency() ||
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   410
            numComponents != cm.getNumComponents() ||
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   411
            (!(colorSpace.equals(cm.colorSpace))) ||
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   412
            transferType != cm.transferType)
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   413
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int numC = cm.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        for(int i=0; i < numC; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (maskArray[i] != cm.getMask(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   423
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   424
        if (!(Arrays.equals(nBits, cm.getComponentSize()))) {
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   425
            return false;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   426
        }
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   427
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   431
    /**
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   432
     * Returns the hash code for this PackedColorModel.
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   433
     *
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   434
     * @return    a hash code for this PackedColorModel.
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   435
     */
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   436
    @Override
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   437
    public int hashCode() {
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   438
        int result = hashCode;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   439
        if (result == 0) {
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   440
            result = 7;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   441
            result = 89 * result + this.pixel_bits;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   442
            result = 89 * result + Arrays.hashCode(this.nBits);
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   443
            result = 89 * result + this.transparency;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   444
            result = 89 * result + (this.supportsAlpha ? 1 : 0);
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   445
            result = 89 * result + (this.isAlphaPremultiplied ? 1 : 0);
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   446
            result = 89 * result + this.numComponents;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   447
            result = 89 * result + this.colorSpace.hashCode();
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   448
            result = 89 * result + this.transferType;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   449
            result = 89 * result + Arrays.hashCode(this.maskArray);
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   450
            hashCode = result;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   451
        }
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   452
        return result;
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   453
    }
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   454
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   455
    private static final int[] createBitsArray(int[]colorMaskArray,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                               int alphaMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int numColors = colorMaskArray.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        int numAlpha = (alphaMask == 0 ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int[] arr = new int[numColors+numAlpha];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        for (int i=0; i < numColors; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            arr[i] = countBits(colorMaskArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (arr[i] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                throw new IllegalArgumentException("Noncontiguous color mask ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                     + Integer.toHexString(colorMaskArray[i])+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                     "at index "+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (alphaMask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            arr[numColors] = countBits(alphaMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (arr[numColors] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                throw new IllegalArgumentException("Noncontiguous alpha mask ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                     + Integer.toHexString(alphaMask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   478
    private static final int[] createBitsArray(int rmask, int gmask, int bmask,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                         int amask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        int[] arr = new int[3 + (amask == 0 ? 0 : 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        arr[0] = countBits(rmask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        arr[1] = countBits(gmask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        arr[2] = countBits(bmask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (arr[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throw new IllegalArgumentException("Noncontiguous red mask ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                     + Integer.toHexString(rmask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        else if (arr[1] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throw new IllegalArgumentException("Noncontiguous green mask ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                     + Integer.toHexString(gmask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        else if (arr[2] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throw new IllegalArgumentException("Noncontiguous blue mask ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                     + Integer.toHexString(bmask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (amask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            arr[3] = countBits(amask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (arr[3] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                throw new IllegalArgumentException("Noncontiguous alpha mask ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                     + Integer.toHexString(amask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   506
    private static final int countBits(int mask) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (mask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            while ((mask & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                mask >>>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            while ((mask & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                mask >>>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (mask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
43827
a02bc4e52007 7107905: ColorModel subclasses are missing hashCode() or equals() or both methods
jdv
parents: 37554
diff changeset
   523
}