src/java.desktop/share/classes/java/awt/image/DirectColorModel.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
/*
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 2018, 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.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    32
 * The {@code DirectColorModel} class is a {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * class that works with pixel values that represent RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * color and alpha information as separate samples and that pack all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * samples for a single pixel into a single int, short, or byte quantity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class can be used only with ColorSpaces of type ColorSpace.TYPE_RGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * In addition, for each component of the ColorSpace, the minimum
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    38
 * normalized component value obtained via the {@code getMinValue()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * method of ColorSpace must be 0.0, and the maximum value obtained via
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    40
 * the {@code getMaxValue()} method must be 1.0 (these min/max
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * values are typical for RGB spaces).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * There must be three color samples in the pixel values and there can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * be a single alpha sample.  For those methods that use a primitive array
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    44
 * pixel representation of type {@code transferType}, the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * length is always one.  The transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * types supported are DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Color and alpha samples are stored in the single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * element of the array in bits indicated by bit masks.  Each bit mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * must be contiguous and masks must not overlap.  The same masks apply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the single int pixel representation used by other methods.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * correspondence of masks and color/alpha samples is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li> Masks are identified by indices running from 0 through 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * if no alpha is present, or 3 if an alpha is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <li> The first three indices refer to color samples;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * index 0 corresponds to red, index 1 to green, and index 2 to blue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <li> Index 3 corresponds to the alpha sample, if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The translation from pixel values to color/alpha components for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * display or processing purposes is a one-to-one correspondence of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    63
 * samples to components.  A {@code DirectColorModel} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * typically used with image data which uses masks to define packed
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    65
 * samples.  For example, a {@code DirectColorModel} can be used in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    66
 * conjunction with a {@code SinglePixelPackedSampleModel} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * construct a {@link BufferedImage}.  Normally the masks used by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    68
 * {@link SampleModel} and the {@code ColorModel} would be the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * same.  However, if they are different, the color interpretation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * of pixel data will be done according to the masks of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    71
 * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * A single int pixel representation is valid for all objects of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * class, since it is always possible to represent pixel values used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * this class in a single int.  Therefore, methods which use this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    76
 * representation will not throw an {@code IllegalArgumentException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * due to an invalid pixel value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * This color model is similar to an X11 TrueColor visual.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * The default RGB ColorModel specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * {@link ColorModel#getRGBdefault() getRGBdefault} method is a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
    82
 * {@code DirectColorModel} with the following parameters:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Number of bits:        32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Red mask:              0x00ff0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Green mask:            0x0000ff00
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Blue mask:             0x000000ff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * Alpha mask:            0xff000000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Color space:           sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * isAlphaPremultiplied:  False
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Transparency:          Transparency.TRANSLUCENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * transferType:          DataBuffer.TYPE_INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Many of the methods in this class are final. This is because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * underlying native graphics code makes assumptions about the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * and operation of this class and those assumptions are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * the implementations of the methods here that are marked final.  You
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * can subclass this class for other reasons, but you cannot override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * or modify the behavior of those methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @see ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @see ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @see SinglePixelPackedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @see BufferedImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
public class DirectColorModel extends PackedColorModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private int red_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private int green_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private int blue_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private int alpha_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int red_offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private int green_offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int blue_offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int alpha_offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int red_scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private int green_scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private int blue_scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private int alpha_scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private boolean is_LinearRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private int lRGBprecision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private byte[] tosRGB8LUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private byte[] fromsRGB8LUT8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private short[] fromsRGB8LUT16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   129
     * Constructs a {@code DirectColorModel} from the specified masks
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   130
     * that indicate which bits in an {@code int} pixel representation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * contain the red, green and blue color samples.  As pixel values do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * contain alpha information, all pixels are treated as opaque, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * means that alpha&nbsp;=&nbsp;1.0.  All of the bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * in each mask must be contiguous and fit in the specified number
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   135
     * of least significant bits of an {@code int} pixel representation.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   136
     *  The {@code ColorSpace} is the default sRGB space. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * transparency value is Transparency.OPAQUE.  The transfer type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * is the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * or DataBuffer.TYPE_INT that can hold a single pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param bits the number of bits in the pixel values; for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         the sum of the number of bits in the masks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param rmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *         integer pixel contain the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param gmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *         integer pixel contain the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param bmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *         integer pixel contain the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public DirectColorModel(int bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            int rmask, int gmask, int bmask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this(bits, rmask, gmask, bmask, 0);
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 DirectColorModel} from the specified masks
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   157
     * that indicate which bits in an {@code int} pixel representation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * contain the red, green and blue color samples and the alpha sample,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   159
     * if present.  If {@code amask} is 0, pixel values do not contain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * alpha information and all pixels are treated as opaque, which means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * that alpha&nbsp;=&nbsp;1.0.  All of the bits in each mask must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * be contiguous and fit in the specified number of least significant bits
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   163
     * of an {@code int} pixel representation.  Alpha, if present, is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   164
     * premultiplied.  The {@code ColorSpace} is the default sRGB space.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * The transparency value is Transparency.OPAQUE if no alpha is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * present, or Transparency.TRANSLUCENT otherwise.  The transfer type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * is the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * or DataBuffer.TYPE_INT that can hold a single pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param bits the number of bits in the pixel values; for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         the sum of the number of bits in the masks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param rmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *         integer pixel contain the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param gmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         integer pixel contain the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param bmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         integer pixel contain the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param amask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *         integer pixel contain the alpha component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public DirectColorModel(int bits, int rmask, int gmask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            int bmask, int amask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super (ColorSpace.getInstance(ColorSpace.CS_sRGB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
               bits, rmask, gmask, bmask, amask, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
               amask == 0 ? Transparency.OPAQUE : Transparency.TRANSLUCENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
               ColorModel.getDefaultTransferType(bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        setFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   190
     * Constructs a {@code DirectColorModel} from the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * parameters.  Color components are in the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   192
     * {@code ColorSpace}, which must be of type ColorSpace.TYPE_RGB
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and have minimum normalized component values which are all 0.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * and maximum values which are all 1.0.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   195
     * The masks specify which bits in an {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * representation contain the red, green and blue color samples and
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   197
     * the alpha sample, if present.  If {@code amask} is 0, pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * values do not contain alpha information and all pixels are treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * as opaque, which means that alpha&nbsp;=&nbsp;1.0.  All of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * bits in each mask must be contiguous and fit in the specified number
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   201
     * of least significant bits of an {@code int} pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   202
     * representation.  If there is alpha, the {@code boolean}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   203
     * {@code isAlphaPremultiplied} specifies how to interpret
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   204
     * color and alpha samples in pixel values.  If the {@code boolean}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   205
     * is {@code true}, color samples are assumed to have been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * multiplied by the alpha sample.  The transparency value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Transparency.OPAQUE, if no alpha is present, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Transparency.TRANSLUCENT otherwise.  The transfer type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * is the type of primitive array used to represent pixel values and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * must be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * DataBuffer.TYPE_INT.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   212
     * @param space the specified {@code ColorSpace}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param bits the number of bits in the pixel values; for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *         the sum of the number of bits in the masks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param rmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *         integer pixel contain the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param gmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *         integer pixel contain the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param bmask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *         integer pixel contain the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param amask specifies a mask indicating which bits in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *         integer pixel contain the alpha component
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   223
     * @param isAlphaPremultiplied {@code true} if color samples are
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   224
     *        premultiplied by the alpha sample; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param transferType the type of array used to represent pixel values
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   226
     * @throws IllegalArgumentException if {@code space} is not a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         TYPE_RGB space or if the min/max normalized component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         values are not 0.0/1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public DirectColorModel(ColorSpace space, int bits, int rmask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            int gmask, int bmask, int amask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            boolean isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            int transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        super (space, bits, rmask, gmask, bmask, amask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
               isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
               amask == 0 ? Transparency.OPAQUE : Transparency.TRANSLUCENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
               transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (ColorModel.isLinearRGBspace(colorSpace)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            is_LinearRGB = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (maxBits <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                lRGBprecision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                lRGBprecision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else if (!is_sRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // super constructor checks that space is TYPE_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // check here that min/max are all 0.0/1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if ((space.getMinValue(i) != 0.0f) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    (space.getMaxValue(i) != 1.0f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        "Illegal min/max RGB component value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        setFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   264
     * Returns the mask indicating which bits in an {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * representation contain the red color component.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   266
     * @return the mask, which indicates which bits of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         pixel representation contain the red color sample.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   269
    public final int getRedMask() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return maskArray[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   274
     * Returns the mask indicating which bits in an {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * representation contain the green color component.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   276
     * @return the mask, which indicates which bits of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *         pixel representation contain the green color sample.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   279
    public final int getGreenMask() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return maskArray[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   284
     * Returns the mask indicating which bits in an {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * representation contain the blue color component.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   286
     * @return the mask, which indicates which bits of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         pixel representation contain the blue color sample.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   289
    public final int getBlueMask() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return maskArray[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   294
     * Returns the mask indicating which bits in an {@code int} pixel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * representation contain the alpha component.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   296
     * @return the mask, which indicates which bits of the {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *         pixel representation contain the alpha sample.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   299
    public final int getAlphaMask() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return maskArray[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Given an int pixel in this ColorModel's ColorSpace, converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * it to the default sRGB ColorSpace and returns the R, G, and B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * components as float values between 0.0 and 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private float[] getDefaultRGBComponents(int pixel) {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   314
        int[] components = getComponents(pixel, null, 0);
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   315
        float[] norm = getNormalizedComponents(components, 0, null, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Note that getNormalizedComponents returns non-premultiplied values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return colorSpace.toRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private int getsRGBComponentFromsRGB(int pixel, int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int c = ((pixel & maskArray[idx]) >>> maskOffsets[idx]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            int a = ((pixel & maskArray[3]) >>> maskOffsets[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            c = (a == 0) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                         (int) (((c * scaleFactors[idx]) * 255.0f /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                 (a * scaleFactors[3])) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else if (scaleFactors[idx] != 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            c = (int) ((c * scaleFactors[idx]) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private int getsRGBComponentFromLinearRGB(int pixel, int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int c = ((pixel & maskArray[idx]) >>> maskOffsets[idx]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            float factor = (float) ((1 << lRGBprecision) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            int a = ((pixel & maskArray[3]) >>> maskOffsets[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            c = (a == 0) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                         (int) (((c * scaleFactors[idx]) * factor /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                 (a * scaleFactors[3])) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else if (nBits[idx] != lRGBprecision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (lRGBprecision == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                c = (int) ((c * scaleFactors[idx] * 257.0f) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                c = (int) ((c * scaleFactors[idx]) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // now range of c is 0-255 or 0-65535, depending on lRGBprecision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return tosRGB8LUT[c] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns the red color component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   357
     * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * color conversion is done if necessary.  The pixel value is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   359
     * as an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * The returned value is a non pre-multiplied value.  Thus, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * the value.  If the alpha value is 0, for example, the red value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return the red color component for the specified pixel, from
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   366
     *         0 to 255 in the sRGB {@code ColorSpace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   368
    public final int getRed(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (is_sRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return getsRGBComponentFromsRGB(pixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } else if (is_LinearRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return getsRGBComponentFromLinearRGB(pixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   374
        float[] rgb = getDefaultRGBComponents(pixel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return (int) (rgb[0] * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Returns the green color component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   380
     * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * color conversion is done if necessary.  The pixel value is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   382
     * as an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * The returned value is a non pre-multiplied value.  Thus, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * the value.  If the alpha value is 0, for example, the green value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @return the green color component for the specified pixel, from
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   389
     *         0 to 255 in the sRGB {@code ColorSpace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   391
    public final int getGreen(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (is_sRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return getsRGBComponentFromsRGB(pixel, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } else if (is_LinearRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return getsRGBComponentFromLinearRGB(pixel, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   397
        float[] rgb = getDefaultRGBComponents(pixel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return (int) (rgb[1] * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns the blue color component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   403
     * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * color conversion is done if necessary.  The pixel value is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   405
     * as an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * The returned value is a non pre-multiplied value.  Thus, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * the value.  If the alpha value is 0, for example, the blue value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return the blue color component for the specified pixel, from
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   412
     *         0 to 255 in the sRGB {@code ColorSpace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   414
    public final int getBlue(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (is_sRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return getsRGBComponentFromsRGB(pixel, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } else if (is_LinearRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return getsRGBComponentFromLinearRGB(pixel, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   420
        float[] rgb = getDefaultRGBComponents(pixel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return (int) (rgb[2] * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Returns the alpha component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   426
     * from 0 to 255.  The pixel value is specified as an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param pixel the specified pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   428
     * @return the value of the alpha component of {@code pixel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         from 0 to 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   431
    public final int getAlpha(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (!supportsAlpha) return 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int a = ((pixel & maskArray[3]) >>> maskOffsets[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (scaleFactors[3] != 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            a = (int)(a * scaleFactors[3] + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Returns the color/alpha components of the pixel in the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * RGB color model format.  A color conversion is done if necessary.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   443
     * The pixel value is specified as an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * The returned value is in a non pre-multiplied format.  Thus, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * the alpha is premultiplied, this method divides it out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * color components.  If the alpha value is 0, for example, the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * values are each 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return the RGB value of the color/alpha components of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *         pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   453
    public final int getRGB(int pixel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (is_sRGB || is_LinearRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return (getAlpha(pixel) << 24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                | (getRed(pixel) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                | (getGreen(pixel) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                | (getBlue(pixel) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   460
        float[] rgb = getDefaultRGBComponents(pixel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return (getAlpha(pixel) << 24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            | (((int) (rgb[0] * 255.0f + 0.5f)) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            | (((int) (rgb[1] * 255.0f + 0.5f)) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            | (((int) (rgb[2] * 255.0f + 0.5f)) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Returns the red color component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   469
     * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * color conversion is done if necessary.  The pixel value is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   471
     * by an array of data elements of type {@code transferType} passed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * in as an object reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * The returned value is a non pre-multiplied value.  Thus, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * the value.  If the alpha value is 0, for example, the red value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * is 0.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   477
     * If {@code inData} is not a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   478
     * {@code transferType}, a {@code ClassCastException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   479
     * thrown.  An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   480
     * thrown if {@code inData} is not large enough to hold a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   481
     * pixel value for this {@code ColorModel}.  Since
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   482
     * {@code DirectColorModel} can be subclassed, subclasses inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * the implementation of this method and if they don't override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   485
     * {@code transferType}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   486
     * An {@code UnsupportedOperationException} is thrown if this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   487
     * {@code transferType} is not supported by this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   488
     * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param inData the array containing the pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return the value of the red component of the specified pixel.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   491
     * @throws ArrayIndexOutOfBoundsException if {@code inData} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *         large enough to hold a pixel value for this color model
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   493
     * @throws ClassCastException if {@code inData} is not a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   494
     *         primitive array of type {@code transferType}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   495
     * @throws UnsupportedOperationException if this {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *         is not supported by this color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public int getRed(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int pixel=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            case DataBuffer.TYPE_BYTE:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   502
               byte[] bdata = (byte[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
               pixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            case DataBuffer.TYPE_USHORT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   506
               short[] sdata = (short[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
               pixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            case DataBuffer.TYPE_INT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   510
               int[] idata = (int[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
               pixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return getRed(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Returns the green color component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   523
     * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * color conversion is done if necessary.  The pixel value is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   525
     * by an array of data elements of type {@code transferType} passed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * in as an object reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * The returned value is a non pre-multiplied value.  Thus, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * the value.  If the alpha value is 0, for example, the green value
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   530
     * is 0.  If {@code inData} is not a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   531
     * {@code transferType}, a {@code ClassCastException} is thrown.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   532
     *  An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   533
     * thrown if {@code inData} is not large enough to hold a pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   534
     * value for this {@code ColorModel}.  Since
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   535
     * {@code DirectColorModel} can be subclassed, subclasses inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the implementation of this method and if they don't override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   538
     * {@code transferType}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   539
     * An {@code UnsupportedOperationException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   540
     * thrown if this {@code transferType} is not supported by this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   541
     * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param inData the array containing the pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return the value of the green component of the specified pixel.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   544
     * @throws ArrayIndexOutOfBoundsException if {@code inData} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *         large enough to hold a pixel value for this color model
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   546
     * @throws ClassCastException if {@code inData} is not a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   547
     *         primitive array of type {@code transferType}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   548
     * @throws UnsupportedOperationException if this {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *         is not supported by this color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public int getGreen(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        int pixel=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            case DataBuffer.TYPE_BYTE:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   555
               byte[] bdata = (byte[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
               pixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            case DataBuffer.TYPE_USHORT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   559
               short[] sdata = (short[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
               pixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            case DataBuffer.TYPE_INT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   563
               int[] idata = (int[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
               pixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return getGreen(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Returns the blue color component for the specified pixel, scaled
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   576
     * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * color conversion is done if necessary.  The pixel value is specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   578
     * by an array of data elements of type {@code transferType} passed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * in as an object reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * The returned value is a non pre-multiplied value.  Thus, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * the value.  If the alpha value is 0, for example, the blue value
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   583
     * is 0.  If {@code inData} is not a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   584
     * {@code transferType}, a {@code ClassCastException} is thrown.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   585
     *  An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   586
     * thrown if {@code inData} is not large enough to hold a pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   587
     * value for this {@code ColorModel}.  Since
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   588
     * {@code DirectColorModel} can be subclassed, subclasses inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * the implementation of this method and if they don't override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   591
     * {@code transferType}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   592
     * An {@code UnsupportedOperationException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   593
     * thrown if this {@code transferType} is not supported by this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   594
     * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param inData the array containing the pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @return the value of the blue component of the specified pixel.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   597
     * @throws ArrayIndexOutOfBoundsException if {@code inData} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *         large enough to hold a pixel value for this color model
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   599
     * @throws ClassCastException if {@code inData} is not a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   600
     *         primitive array of type {@code transferType}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   601
     * @throws UnsupportedOperationException if this {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *         is not supported by this color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public int getBlue(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        int pixel=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            case DataBuffer.TYPE_BYTE:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   608
               byte[] bdata = (byte[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
               pixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            case DataBuffer.TYPE_USHORT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   612
               short[] sdata = (short[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
               pixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            case DataBuffer.TYPE_INT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   616
               int[] idata = (int[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
               pixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return getBlue(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns the alpha component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * from 0 to 255.  The pixel value is specified by an array of data
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   629
     * elements of type {@code transferType} passed in as an object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * reference.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   631
     * If {@code inData} is not a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   632
     * {@code transferType}, a {@code ClassCastException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   633
     * thrown.  An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   634
     * thrown if {@code inData} is not large enough to hold a pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   635
     * value for this {@code ColorModel}.  Since
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   636
     * {@code DirectColorModel} can be subclassed, subclasses inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * the implementation of this method and if they don't override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   639
     * {@code transferType}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   640
     * If this {@code transferType} is not supported, an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   641
     * {@code UnsupportedOperationException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param inData the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return the alpha component of the specified pixel, scaled from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *         0 to 255.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   645
     * @exception ClassCastException if {@code inData}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   646
     *  is not a primitive array of type {@code transferType}
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 13165
diff changeset
   647
     * @exception ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   648
     *  {@code inData} is not large enough to hold a pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   649
     *  for this {@code ColorModel}
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 13165
diff changeset
   650
     * @exception UnsupportedOperationException if this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   651
     *  {@code tranferType} is not supported by this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   652
     *  {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public int getAlpha(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        int pixel=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            case DataBuffer.TYPE_BYTE:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   658
               byte[] bdata = (byte[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
               pixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            case DataBuffer.TYPE_USHORT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   662
               short[] sdata = (short[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
               pixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            case DataBuffer.TYPE_INT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   666
               int[] idata = (int[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
               pixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return getAlpha(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Returns the color/alpha components for the specified pixel in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * default RGB color model format.  A color conversion is done if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * necessary.  The pixel value is specified by an array of data
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   680
     * elements of type {@code transferType} passed in as an object
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   681
     * reference.  If {@code inData} is not a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   682
     * {@code transferType}, a {@code ClassCastException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   683
     * thrown.  An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   684
     * thrown if {@code inData} is not large enough to hold a pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   685
     * value for this {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * The returned value is in a non pre-multiplied format.  Thus, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * the alpha is premultiplied, this method divides it out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * color components.  If the alpha value is 0, for example, the color
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   689
     * values is 0.  Since {@code DirectColorModel} can be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * subclassed, subclasses inherit the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * and if they don't override it then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   693
     * {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param inData the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @return the color and alpha components of the specified pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @exception UnsupportedOperationException if this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   698
     *            {@code transferType} is not supported by this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   699
     *            {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public int getRGB(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int pixel=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            case DataBuffer.TYPE_BYTE:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   706
               byte[] bdata = (byte[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
               pixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            case DataBuffer.TYPE_USHORT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   710
               short[] sdata = (short[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
               pixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            case DataBuffer.TYPE_INT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   714
               int[] idata = (int[])inData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
               pixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        return getRGB(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Returns a data element array representation of a pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   726
     * {@code ColorModel}, given an integer pixel representation in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * default RGB color model.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   728
     * This array can then be passed to the {@code setDataElements}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   729
     * method of a {@code WritableRaster} object.  If the pixel variable
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   730
     * is {@code null}, a new array is allocated.  If {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   731
     * is not {@code null}, it must be a primitive array of type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   732
     * {@code transferType}; otherwise, a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   733
     * {@code ClassCastException} is thrown.  An
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   734
     * {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   735
     * thrown if {@code pixel} is not large enough to hold a pixel
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   736
     * value for this {@code ColorModel}.  The pixel array is returned.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   737
     * Since {@code DirectColorModel} can be subclassed, subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * override it then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   740
     * {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param rgb the integer pixel representation in the default RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *            color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @return an array representation of the specified pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   746
     *         {@code ColorModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   747
     * @exception ClassCastException if {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   748
     *  is not a primitive array of type {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @exception ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   750
     *  {@code pixel} is not large enough to hold a pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   751
     *  for this {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @exception UnsupportedOperationException if this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   753
     *  {@code transferType} is not supported by this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   754
     *  {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public Object getDataElements(int rgb, Object pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        //REMIND: maybe more efficient not to use int array for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        //DataBuffer.TYPE_USHORT and DataBuffer.TYPE_INT
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   761
        int[] intpixel = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (transferType == DataBuffer.TYPE_INT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            pixel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            intpixel = (int[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            intpixel[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            intpixel = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        ColorModel defaultCM = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (this == defaultCM || equals(defaultCM)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            intpixel[0] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            return intpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        int red, grn, blu, alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        red = (rgb>>16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        grn = (rgb>>8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        blu = rgb & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (is_sRGB || is_LinearRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            int precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (is_LinearRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (lRGBprecision == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    red = fromsRGB8LUT8[red] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    grn = fromsRGB8LUT8[grn] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    blu = fromsRGB8LUT8[blu] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    precision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    factor = 1.0f / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    factor *= (alp * (1.0f / 255.0f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    precision = -1;  // force component calculations below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                if (nBits[3] != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    alp = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        ((alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    if (alp > ((1<<nBits[3]) - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        // fix 4412670 - see comment below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                        alp = (1<<nBits[3]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                intpixel[0] = alp << maskOffsets[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (nBits[0] != precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                red = (int) ((red * factor * ((1<<nBits[0]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (nBits[1] != precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                grn = (int) ((grn * factor * ((1<<nBits[1]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            if (nBits[2] != precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                blu = (int) ((blu * factor * ((1<<nBits[2]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            // Need to convert the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            float[] norm = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            float factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            norm[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            norm[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            norm[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            norm = colorSpace.fromRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    factor *= alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        norm[i] *= factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                if (nBits[3] != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    alp = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        ((alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    if (alp > ((1<<nBits[3]) - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        // fix 4412670 - see comment below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                        alp = (1<<nBits[3]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                intpixel[0] = alp << maskOffsets[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            red = (int) ((norm[0] * ((1<<nBits[0]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            grn = (int) ((norm[1] * ((1<<nBits[1]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            blu = (int) ((norm[2] * ((1<<nBits[2]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (maxBits > 23) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            // fix 4412670 - for components of 24 or more bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            // some calculations done above with float precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            // may lose enough precision that the integer result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            // overflows nBits, so we need to clamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            if (red > ((1<<nBits[0]) - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                red = (1<<nBits[0]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (grn > ((1<<nBits[1]) - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                grn = (1<<nBits[1]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            if (blu > ((1<<nBits[2]) - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                blu = (1<<nBits[2]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        intpixel[0] |= (red << maskOffsets[0]) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                       (grn << maskOffsets[1]) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                       (blu << maskOffsets[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            case DataBuffer.TYPE_BYTE: {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   879
               byte[] bdata;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
               if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                   bdata = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                   bdata = (byte[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
               bdata[0] = (byte)(0xff&intpixel[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
               return bdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            case DataBuffer.TYPE_USHORT:{
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   889
               short[] sdata;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
               if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                   sdata = new short[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                   sdata = (short[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
               sdata[0] = (short)(intpixel[0]&0xffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
               return sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
               return intpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                 "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Returns an array of unnormalized color/alpha components given a pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   908
     * in this {@code ColorModel}.  The pixel value is specified as an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   909
     * {@code int}.  If the {@code components} array is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   910
     * {@code null}, a new array is allocated.  The
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   911
     * {@code components} array is returned.  Color/alpha components are
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   912
     * stored in the {@code components} array starting at
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   913
     * {@code offset}, even if the array is allocated by this method.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   914
     * An {@code ArrayIndexOutOfBoundsException} is thrown if the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   915
     * {@code components} array is not {@code null} and is not large
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * enough to hold all the color and alpha components, starting at
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   917
     * {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @param components the array to receive the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * components of the specified pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   921
     * @param offset the offset into the {@code components} array at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * which to start storing the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @return an array containing the color and alpha components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * specified pixel starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   926
    public final int[] getComponents(int pixel, int[] components, int offset) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (components == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            components = new int[offset+numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            components[offset+i] = (pixel & maskArray[i]) >>> maskOffsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        return components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns an array of unnormalized color/alpha components given a pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   940
     * in this {@code ColorModel}.  The pixel value is specified by an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   941
     * array of data elements of type {@code transferType} passed in as
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   942
     * an object reference.  If {@code pixel} is not a primitive array
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   943
     * of type {@code transferType}, a {@code ClassCastException}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   944
     * is thrown.  An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   945
     * thrown if {@code pixel} is not large enough to hold a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   946
     * pixel value for this {@code ColorModel}.  If the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   947
     * {@code components} array is {@code null}, a new
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   948
     * array is allocated.  The {@code components} array is returned.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   949
     * Color/alpha components are stored in the {@code components} array
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   950
     * starting at {@code offset}, even if the array is allocated by
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   951
     * this method.  An {@code ArrayIndexOutOfBoundsException}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   952
     * is thrown if the {@code components} array is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   953
     * {@code null} and is not large enough to hold all the color and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   954
     * alpha components, starting at {@code offset}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   955
     * Since {@code DirectColorModel} can be subclassed, subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * override it then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   958
     * {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @param components the array to receive the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *        components of the specified pixel
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   962
     * @param offset the offset into the {@code components} array at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *        which to start storing the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @return an array containing the color and alpha components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * specified pixel starting at the specified offset.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   966
     * @exception ClassCastException if {@code pixel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   967
     *  is not a primitive array of type {@code transferType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @exception ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   969
     *  {@code pixel} is not large enough to hold a pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   970
     *  for this {@code ColorModel}, or if {@code components}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   971
     *  is not {@code null} and is not large enough to hold all the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   972
     *  color and alpha components, starting at {@code offset}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @exception UnsupportedOperationException if this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   974
     *            {@code transferType} is not supported by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *            color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   977
    public final int[] getComponents(Object pixel, int[] components,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                                     int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        int intpixel=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            case DataBuffer.TYPE_BYTE:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   982
               byte[] bdata = (byte[])pixel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
               intpixel = bdata[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            case DataBuffer.TYPE_USHORT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   986
               short[] sdata = (short[])pixel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
               intpixel = sdata[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            case DataBuffer.TYPE_INT:
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   990
               int[] idata = (int[])pixel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
               intpixel = idata[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        return getComponents(intpixel, components, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1001
     * Creates a {@code WritableRaster} with the specified width and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1002
     * height that has a data layout ({@code SampleModel}) compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1003
     * with this {@code ColorModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1004
     * @param w the width to apply to the new {@code WritableRaster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1005
     * @param h the height to apply to the new {@code WritableRaster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1006
     * @return a {@code WritableRaster} object with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * width and height.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1008
     * @throws IllegalArgumentException if {@code w} or {@code h}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *         is less than or equal to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @see WritableRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
  1013
    public final WritableRaster createCompatibleWritableRaster (int w,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                                                                int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        if ((w <= 0) || (h <= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            throw new IllegalArgumentException("Width (" + w + ") and height (" + h +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                                               ") cannot be <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        int[] bandmasks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            bandmasks = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            bandmasks[3] = alpha_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            bandmasks = new int[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        bandmasks[0] = red_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        bandmasks[1] = green_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        bandmasks[2] = blue_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (pixel_bits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return Raster.createPackedRaster(DataBuffer.TYPE_INT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                                             w,h,bandmasks,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        else if (pixel_bits > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            return Raster.createPackedRaster(DataBuffer.TYPE_USHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                                             w,h,bandmasks,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            return Raster.createPackedRaster(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                                             w,h,bandmasks,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1046
     * Returns a pixel value represented as an {@code int} in this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1047
     * {@code ColorModel}, given an array of unnormalized color/alpha
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1048
     * components.   An {@code ArrayIndexOutOfBoundsException} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1049
     * thrown if the {@code components} array is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * not large enough to hold all the color and alpha components, starting
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1051
     * at {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @param components an array of unnormalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1054
     * @param offset the index into {@code components} at which to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * begin retrieving the color and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1056
     * @return an {@code int} pixel value in this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1057
     * {@code ColorModel} corresponding to the specified components.
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 13165
diff changeset
  1058
     * @exception ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1059
     *  the {@code components} array is not large enough to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *  hold all of the color and alpha components starting at
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1061
     *  {@code offset}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    public int getDataElement(int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        int pixel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            pixel |= ((components[offset+i]<<maskOffsets[i])&maskArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        return pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Returns a data element array representation of a pixel in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1073
     * {@code ColorModel}, given an array of unnormalized color/alpha
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * components.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1075
     * This array can then be passed to the {@code setDataElements}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1076
     * method of a {@code WritableRaster} object.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1077
     * An {@code ArrayIndexOutOfBoundsException} is thrown if the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1078
     * {@code components} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * is not large enough to hold all the color and alpha components,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1080
     * starting at offset.  If the {@code obj} variable is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1081
     * {@code null}, a new array is allocated.  If {@code obj} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1082
     * not {@code null}, it must be a primitive array
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1083
     * of type {@code transferType}; otherwise, a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1084
     * {@code ClassCastException} is thrown.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1085
     * An {@code ArrayIndexOutOfBoundsException} is thrown if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1086
     * {@code obj} is not large enough to hold a pixel value for this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1087
     * {@code ColorModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1088
     * Since {@code DirectColorModel} can be subclassed, subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * override it then they throw an exception if they use an unsupported
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1091
     * {@code transferType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @param components an array of unnormalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1094
     * @param offset the index into {@code components} at which to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * begin retrieving color and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1096
     * @param obj the {@code Object} representing an array of color
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * and alpha components
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1098
     * @return an {@code Object} representing an array of color and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * alpha components.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1100
     * @exception ClassCastException if {@code obj}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1101
     *  is not a primitive array of type {@code transferType}
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 13165
diff changeset
  1102
     * @exception ArrayIndexOutOfBoundsException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1103
     *  {@code obj} is not large enough to hold a pixel value
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1104
     *  for this {@code ColorModel} or the {@code components}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *  array is not large enough to hold all of the color and alpha
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1106
     *  components starting at {@code offset}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @exception UnsupportedOperationException if this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1108
     *            {@code transferType} is not supported by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *            color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    public Object getDataElements(int[] components, int offset, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        int pixel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            pixel |= ((components[offset+i]<<maskOffsets[i])&maskArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
               if (obj instanceof byte[]) {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1121
                   byte[] bdata = (byte[])obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                   bdata[0] = (byte)(pixel&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                   return bdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
               } else {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1125
                   byte[] bdata = {(byte)(pixel&0xff)};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                   return bdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
               if (obj instanceof short[]) {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1130
                   short[] sdata = (short[])obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                   sdata[0] = (short)(pixel&0xffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                   return sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
               } else {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1134
                   short[] sdata = {(short)(pixel&0xffff)};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                   return sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
               if (obj instanceof int[]) {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1139
                   int[] idata = (int[])obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                   idata[0] = pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                   return idata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
               } else {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1143
                   int[] idata = {pixel};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                   return idata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
               throw new ClassCastException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * Forces the raster data to match the state specified in the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1154
     * {@code isAlphaPremultiplied} variable, assuming the data is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1155
     * currently correctly described by this {@code ColorModel}.  It
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * may multiply or divide the color raster data by alpha, or do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * nothing if the data is in the correct state.  If the data needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * be coerced, this method will also return an instance of this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1159
     * {@code ColorModel} with the {@code isAlphaPremultiplied}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * flag set appropriately.  This method will throw a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1161
     * {@code UnsupportedOperationException} if this transferType is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1162
     * not supported by this {@code ColorModel}.  Since
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1163
     * {@code ColorModel} can be subclassed, subclasses inherit the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * implementation of this method and if they don't override it then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * they throw an exception if they use an unsupported transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1167
     * @param raster the {@code WritableRaster} data
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1168
     * @param isAlphaPremultiplied {@code true} if the alpha is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1169
     * premultiplied; {@code false} otherwise
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1170
     * @return a {@code ColorModel} object that represents the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * coerced data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @exception UnsupportedOperationException if this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1173
     *            {@code transferType} is not supported by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *            color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
  1176
    public final ColorModel coerceData (WritableRaster raster,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                                        boolean isAlphaPremultiplied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (!supportsAlpha ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            this.isAlphaPremultiplied() == isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        int w = raster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        int h = raster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        int aIdx = numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        float normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        float alphaScale = 1.0f / ((float) ((1 << nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        int rminX = raster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        int rY = raster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        int rX;
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1193
        int[] pixel = null;
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
  1194
        int[] zpixel = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            // Must mean that we are currently not premultiplied so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            // multiply by alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                case DataBuffer.TYPE_BYTE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                            pixel = raster.getPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                            if (normAlpha != 0.f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                                    pixel[c] = (int) (pixel[c] * normAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                raster.setPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                    zpixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                                    java.util.Arrays.fill(zpixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                                raster.setPixel(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                case DataBuffer.TYPE_USHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                            pixel = raster.getPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                            if (normAlpha != 0.f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                    pixel[c] = (int) (pixel[c] * normAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                                raster.setPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                                    zpixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                                    java.util.Arrays.fill(zpixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                                raster.setPixel(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                case DataBuffer.TYPE_INT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                            pixel = raster.getPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                            if (normAlpha != 0.f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                                    pixel[c] = (int) (pixel[c] * normAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                                raster.setPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                                    zpixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                                    java.util.Arrays.fill(zpixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                                raster.setPixel(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                         "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            // We are premultiplied and want to divide it out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                case DataBuffer.TYPE_BYTE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                            pixel = raster.getPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                                    pixel[c] = (int) (pixel[c] * invAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                raster.setPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                case DataBuffer.TYPE_USHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                            pixel = raster.getPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                            if (normAlpha != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                    pixel[c] = (int) (pixel[c] * invAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                                raster.setPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                case DataBuffer.TYPE_INT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                            pixel = raster.getPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                            if (normAlpha != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                                    pixel[c] = (int) (pixel[c] * invAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                raster.setPixel(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                         "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        // Return a new color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        return new DirectColorModel(colorSpace, pixel_bits, maskArray[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                                    maskArray[1], maskArray[2], maskArray[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                                    isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                                    transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1346
      * Returns {@code true} if {@code raster} is compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1347
      * with this {@code ColorModel} and {@code false} if it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
      * not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
      * @param raster the {@link Raster} object to test for compatibility
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1350
      * @return {@code true} if {@code raster} is compatible
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1351
      * with this {@code ColorModel}; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    public boolean isCompatibleRaster(Raster raster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        SampleModel sm = raster.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        SinglePixelPackedSampleModel spsm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        if (sm instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            spsm = (SinglePixelPackedSampleModel) sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if (spsm.getNumBands() != getNumComponents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        int[] bitMasks = spsm.getBitMasks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        for (int i=0; i<numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            if (bitMasks[i] != maskArray[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        return (raster.getTransferType() == transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    private void setFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        // Set the private fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        // REMIND: Get rid of these from the native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        red_mask     = maskArray[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        red_offset   = maskOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        green_mask   = maskArray[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        green_offset = maskOffsets[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        blue_mask    = maskArray[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        blue_offset  = maskOffsets[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        if (nBits[0] < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            red_scale = (1 << nBits[0]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        if (nBits[1] < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            green_scale = (1 << nBits[1]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        if (nBits[2] < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            blue_scale = (1 << nBits[2]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            alpha_mask   = maskArray[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            alpha_offset = maskOffsets[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            if (nBits[3] < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                alpha_scale = (1 << nBits[3]) - 1;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1404
     * Returns a {@code String} that represents this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1405
     * {@code DirectColorModel}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1406
     * @return a {@code String} representing this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1407
     * {@code DirectColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        return new String("DirectColorModel: rmask="
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                          +Integer.toHexString(red_mask)+" gmask="
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                          +Integer.toHexString(green_mask)+" bmask="
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                          +Integer.toHexString(blue_mask)+" amask="
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                          +Integer.toHexString(alpha_mask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
}