jdk/src/java.desktop/share/classes/java/awt/image/ComponentColorModel.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.color.ICC_ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A <CODE>ColorModel</CODE> class that works with pixel values that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * represent color and alpha information as separate samples and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * store each sample in a separate data element.  This class can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * used with an arbitrary <CODE>ColorSpace</CODE>.  The number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * color samples in the pixel values must be same as the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * color components in the <CODE>ColorSpace</CODE>. There may be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * single alpha sample.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * For those methods that use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a primitive array pixel representation of type <CODE>transferType</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the array length is the same as the number of color and alpha samples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Color samples are stored first in the array followed by the alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * sample, if present.  The order of the color samples is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * by the <CODE>ColorSpace</CODE>.  Typically, this order reflects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * name of the color space type. For example, for <CODE>TYPE_RGB</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * index 0 corresponds to red, index 1 to green, and index 2 to blue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The translation from pixel sample values to color/alpha components for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * display or processing purposes is based on a one-to-one correspondence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * samples to components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Depending on the transfer type used to create an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>ComponentColorModel</code>, the pixel sample values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * represented by that instance may be signed or unsigned and may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * be of integral type or float or double (see below for details).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The translation from sample values to normalized color/alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * must follow certain rules.  For float and double samples, the translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * is an identity, i.e. normalized component values are equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * corresponding sample values.  For integral samples, the translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * should be only a simple scale and offset, where the scale and offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * constants may be different for each component.  The result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * applying the scale and offset constants is a set of color/alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * component values, which are guaranteed to fall within a certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * range.  Typically, the range for a color component will be the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * defined by the <code>getMinValue</code> and <code>getMaxValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * methods of the <code>ColorSpace</code> class.  The range for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * alpha component should be 0.0 to 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Instances of <code>ComponentColorModel</code> created with transfer types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * and <CODE>DataBuffer.TYPE_INT</CODE> have pixel sample values which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * are treated as unsigned integral values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The number of bits in a color or alpha sample of a pixel value might not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * be the same as the number of bits for the corresponding color or alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * sample passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <code>ComponentColorModel(ColorSpace, int[], boolean, boolean, int, int)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * constructor.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * that case, this class assumes that the least significant n bits of a sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * value hold the component value, where n is the number of significant bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * for the component passed to the constructor.  It also assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * any higher-order bits in a sample value are zero.  Thus, sample values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * range from 0 to 2<sup>n</sup> - 1.  This class maps these sample values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * to normalized color component values such that 0 maps to the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * obtained from the <code>ColorSpace's</code> <code>getMinValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * method for each component and 2<sup>n</sup> - 1 maps to the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * obtained from <code>getMaxValue</code>.  To create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>ComponentColorModel</code> with a different color sample mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * requires subclassing this class and overriding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <code>getNormalizedComponents(Object, float[], int)</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The mapping for an alpha sample always maps 0 to 0.0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * 2<sup>n</sup> - 1 to 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * For instances with unsigned sample values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * the unnormalized color/alpha component representation is only
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    95
 * supported if two conditions hold.  First, sample value 0 must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * map to normalized component value 0.0 and sample value 2<sup>n</sup> - 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * to 1.0.  Second the min/max range of all color components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <code>ColorSpace</code> must be 0.0 to 1.0.  In this case, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * component representation is the n least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * significant bits of the corresponding sample.  Thus each component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * an unsigned integral value between 0 and 2<sup>n</sup> - 1, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * n is the number of significant bits for a particular component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * If these conditions are not met, any method taking an unnormalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * component argument will throw an <code>IllegalArgumentException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Instances of <code>ComponentColorModel</code> created with transfer types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <CODE>DataBuffer.TYPE_DOUBLE</CODE> have pixel sample values which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * are treated as signed short, float, or double values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Such instances do not support the unnormalized color/alpha component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * representation, so any methods taking such a representation as an argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * will throw an <code>IllegalArgumentException</code> when called on one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * of these instances.  The normalized component values of instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * of this class have a range which depends on the transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * type as follows: for float samples, the full range of the float data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * type; for double samples, the full range of the float data type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * (resulting from casting double to float); for short samples,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * from approximately -maxVal to +maxVal, where maxVal is the per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * component maximum value for the <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * (-32767 maps to -maxVal, 0 maps to 0.0, and 32767 maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * to +maxVal).  A subclass may override the scaling for short sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * values to normalized component values by overriding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <code>getNormalizedComponents(Object, float[], int)</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * For float and double samples, the normalized component values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * taken to be equal to the corresponding sample values, and subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * should not attempt to add any non-identity scaling for these transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * Instances of <code>ComponentColorModel</code> created with transfer types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <CODE>DataBuffer.TYPE_DOUBLE</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * use all the bits of all sample values.  Thus all color/alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * have 16 bits when using <CODE>DataBuffer.TYPE_SHORT</CODE>, 32 bits when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * using <CODE>DataBuffer.TYPE_FLOAT</CODE>, and 64 bits when using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <CODE>DataBuffer.TYPE_DOUBLE</CODE>.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code>ComponentColorModel(ColorSpace, int[], boolean, boolean, int, int)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * form of constructor is used with one of these transfer types, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * bits array argument is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * It is possible to have color/alpha sample values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * which cannot be reasonably interpreted as component values for rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * This can happen when <code>ComponentColorModel</code> is subclassed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * override the mapping of unsigned sample values to normalized color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * component values or when signed sample values outside a certain range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * are used.  (As an example, specifying an alpha component as a signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * short value outside the range 0 to 32767, normalized range 0.0 to 1.0, can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * lead to unexpected results.) It is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * responsibility of applications to appropriately scale pixel data before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * rendering such that color components fall within the normalized range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * of the <code>ColorSpace</code> (obtained using the <code>getMinValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * and <code>getMaxValue</code> methods of the <code>ColorSpace</code> class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * and the alpha component is between 0.0 and 1.0.  If color or alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * component values fall outside these ranges, rendering results are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * indeterminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * Methods that use a single int pixel representation throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * an <CODE>IllegalArgumentException</CODE>, unless the number of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * for the <CODE>ComponentColorModel</CODE> is one and the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * value is unsigned -- in other words,  a single color component using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * a transfer type of <CODE>DataBuffer.TYPE_BYTE</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <CODE>DataBuffer.TYPE_USHORT</CODE>, or <CODE>DataBuffer.TYPE_INT</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * and no alpha.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * A <CODE>ComponentColorModel</CODE> can be used in conjunction with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <CODE>ComponentSampleModel</CODE>, a <CODE>BandedSampleModel</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * or a <CODE>PixelInterleavedSampleModel</CODE> to construct a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <CODE>BufferedImage</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * @see ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * @see ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * @see ComponentSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * @see BandedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * @see PixelInterleavedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * @see BufferedImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
public class ComponentColorModel extends ColorModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>signed</code>  is <code>true</code> for <code>short</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>float</code>, and <code>double</code> transfer types; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * is <code>false</code> for <code>byte</code>, <code>ushort</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * and <code>int</code> transfer types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private boolean signed; // true for transfer types short, float, double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            // false for byte, ushort, int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private boolean is_sRGB_stdScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private boolean is_LinearRGB_stdScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private boolean is_LinearGray_stdScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private boolean is_ICCGray_stdScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private byte[] tosRGB8LUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private byte[] fromsRGB8LUT8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private short[] fromsRGB8LUT16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private byte[] fromLinearGray16ToOtherGray8LUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private short[] fromLinearGray16ToOtherGray16LUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private boolean needScaleInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private boolean noUnnorm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private boolean nonStdScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private float[] min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private float[] diffMinMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private float[] compOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private float[] compScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Constructs a <CODE>ComponentColorModel</CODE> from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * parameters. Color components will be in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <CODE>ColorSpace</CODE>.  The supported transfer types are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <CODE>DataBuffer.TYPE_INT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * and <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * If not null, the <CODE>bits</CODE> array specifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * number of significant bits per color and alpha component and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * length should be at least the number of components in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <CODE>ColorSpace</CODE> if there is no alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * information in the pixel values, or one more than this number if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * there is alpha information.  When the <CODE>transferType</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * or <CODE>DataBuffer.TYPE_DOUBLE</CODE> the <CODE>bits</CODE> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * argument is ignored.  <CODE>hasAlpha</CODE> indicates whether alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * information is present.  If <CODE>hasAlpha</CODE> is true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * the boolean <CODE>isAlphaPremultiplied</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * specifies how to interpret color and alpha samples in pixel values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * If the boolean is true, color samples are assumed to have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * multiplied by the alpha sample. The <CODE>transparency</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * specifies what alpha values can be represented by this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The acceptable <code>transparency</code> values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <CODE>OPAQUE</CODE>, <CODE>BITMASK</CODE> or <CODE>TRANSLUCENT</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The <CODE>transferType</CODE> is the type of primitive array used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * to represent pixel values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param colorSpace       The <CODE>ColorSpace</CODE> associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *                         with this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param bits             The number of significant bits per component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *                         May be null, in which case all bits of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *                         component samples will be significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *                         Ignored if transferType is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *                         <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *                         <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *                         <CODE>DataBuffer.TYPE_DOUBLE</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *                         in which case all bits of all component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *                         samples will be significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param hasAlpha         If true, this color model supports alpha.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param isAlphaPremultiplied If true, alpha is premultiplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param transparency     Specifies what alpha values can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *                         by this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param transferType     Specifies the type of primitive array used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *                         represent pixel values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws IllegalArgumentException If the <CODE>bits</CODE> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *         argument is not null, its length is less than the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         color and alpha components, and transferType is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *         <CODE>DataBuffer.TYPE_BYTE</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *         <CODE>DataBuffer.TYPE_USHORT</CODE>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         <CODE>DataBuffer.TYPE_INT</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws IllegalArgumentException If transferType is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *         <CODE>DataBuffer.TYPE_BYTE</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *         <CODE>DataBuffer.TYPE_INT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *         <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *         <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *         <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @see java.awt.Transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public ComponentColorModel (ColorSpace colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                int[] bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                boolean hasAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                boolean isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                int transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                int transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        super (bitsHelper(transferType, colorSpace, hasAlpha),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
               bitsArrayHelper(bits, transferType, colorSpace, hasAlpha),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
               colorSpace, hasAlpha, isAlphaPremultiplied, transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
               transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        switch(transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                signed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                needScaleInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                signed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                needScaleInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            case DataBuffer.TYPE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            case DataBuffer.TYPE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                signed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                needScaleInit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                noUnnorm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                nonStdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                throw new IllegalArgumentException("This constructor is not "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                         "compatible with transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        setupLUTs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Constructs a <CODE>ComponentColorModel</CODE> from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * parameters. Color components will be in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <CODE>ColorSpace</CODE>.  The supported transfer types are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <CODE>DataBuffer.TYPE_INT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * and <CODE>DataBuffer.TYPE_DOUBLE</CODE>.  The number of significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * bits per color and alpha component will be 8, 16, 32, 16, 32,  or 64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * respectively.  The number of color components will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * number of components in the <CODE>ColorSpace</CODE>.  There will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * an alpha component if <CODE>hasAlpha</CODE> is <CODE>true</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * If <CODE>hasAlpha</CODE> is true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * the boolean <CODE>isAlphaPremultiplied</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * specifies how to interpret color and alpha samples in pixel values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * If the boolean is true, color samples are assumed to have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * multiplied by the alpha sample. The <CODE>transparency</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * specifies what alpha values can be represented by this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * The acceptable <code>transparency</code> values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <CODE>OPAQUE</CODE>, <CODE>BITMASK</CODE> or <CODE>TRANSLUCENT</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * The <CODE>transferType</CODE> is the type of primitive array used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * to represent pixel values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param colorSpace       The <CODE>ColorSpace</CODE> associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *                         with this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param hasAlpha         If true, this color model supports alpha.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param isAlphaPremultiplied If true, alpha is premultiplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param transparency     Specifies what alpha values can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *                         by this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param transferType     Specifies the type of primitive array used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *                         represent pixel values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws IllegalArgumentException If transferType is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *         <CODE>DataBuffer.TYPE_BYTE</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *         <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *         <CODE>DataBuffer.TYPE_INT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *         <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *         <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *         <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @see ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @see java.awt.Transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public ComponentColorModel (ColorSpace colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                boolean hasAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                boolean isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                int transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                int transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        this(colorSpace, null, hasAlpha, isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
             transparency, transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private static int bitsHelper(int transferType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                  ColorSpace colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                  boolean hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        int numBits = DataBuffer.getDataTypeSize(transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int numComponents = colorSpace.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            ++numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return numBits * numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private static int[] bitsArrayHelper(int[] origBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                         int transferType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                         ColorSpace colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                         boolean hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        switch(transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (origBits != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    return origBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int numBits = DataBuffer.getDataTypeSize(transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int numComponents = colorSpace.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            ++numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        int[] bits = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        for (int i = 0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            bits[i] = numBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    private void setupLUTs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // REMIND: there is potential to accelerate sRGB, LinearRGB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // LinearGray, ICCGray, and non-ICC Gray spaces with non-standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // scaling, if that becomes important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // NOTE: The is_xxx_stdScale and nonStdScale booleans are provisionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // set here when this method is called at construction time.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // variables may be set again when initScale is called later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // When setupLUTs returns, nonStdScale is true if (the transferType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // is not float or double) AND (some minimum ColorSpace component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // value is not 0.0 OR some maximum ColorSpace component value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // is not 1.0).  This is correct for the calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // getNormalizedComponents(Object, float[], int) from initScale().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // initScale() may change the value nonStdScale based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // return value of getNormalizedComponents() - this will only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // happen if getNormalizedComponents() has been overridden by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // subclass to make the mapping of min/max pixel sample values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // something different from min/max color component values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (is_sRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            is_sRGB_stdScale = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            nonStdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } else if (ColorModel.isLinearRGBspace(colorSpace)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // Note that the built-in Linear RGB space has a normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // range of 0.0 - 1.0 for each coordinate.  Usage of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // LUTs makes that assumption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            is_LinearRGB_stdScale = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            nonStdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (transferType == DataBuffer.TYPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } else if ((colorSpaceType == ColorSpace.TYPE_GRAY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                   (colorSpace instanceof ICC_ColorSpace) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                   (colorSpace.getMinValue(0) == 0.0f) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                   (colorSpace.getMaxValue(0) == 1.0f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // Note that a normalized range of 0.0 - 1.0 for the gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            // component is required, because usage of these LUTs makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            // that assumption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            ICC_ColorSpace ics = (ICC_ColorSpace) colorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            is_ICCGray_stdScale = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            nonStdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (ColorModel.isLinearGRAYspace(ics)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                is_LinearGray_stdScale = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                if (transferType == DataBuffer.TYPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (transferType == DataBuffer.TYPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    fromLinearGray16ToOtherGray8LUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        ColorModel.getLinearGray16ToOtherGray8LUT(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    fromLinearGray16ToOtherGray16LUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        ColorModel.getLinearGray16ToOtherGray16LUT(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        } else if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // if transferType is byte, ushort, int, or short and we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            // don't already know the ColorSpace has minVlaue == 0.0f and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // maxValue == 1.0f for all components, we need to check that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            // now and setup the min[] and diffMinMax[] arrays if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            nonStdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if ((colorSpace.getMinValue(i) != 0.0f) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    (colorSpace.getMaxValue(i) != 1.0f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    nonStdScale = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (nonStdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                min = new float[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                diffMinMax = new float[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    min[i] = colorSpace.getMinValue(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    diffMinMax[i] = colorSpace.getMaxValue(i) - min[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private void initScale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // This method is called the first time any method which uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        // pixel sample value to color component value scaling information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // is called if the transferType supports non-standard scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // as defined above (byte, ushort, int, and short), unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // method is getNormalizedComponents(Object, float[], int) (that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // method must be overridden to use non-standard scaling).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // method also sets up the noUnnorm boolean variable for these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        // transferTypes.  After this method is called, the nonStdScale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // variable will be true if getNormalizedComponents() maps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // sample value of 0 to anything other than 0.0f OR maps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // sample value of 2^^n - 1 (2^^15 - 1 for short transferType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        // to anything other than 1.0f.  Note that this can be independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // of the colorSpace min/max component values, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // getNormalizedComponents() method has been overridden for some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // reason, e.g. to provide greater dynamic range in the sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // values than in the color component values.  Unfortunately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        // this method can't be called at construction time, since a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // subclass may still have uninitialized state that would cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // getNormalizedComponents() to return an incorrect result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        needScaleInit = false; // only needs to called once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (nonStdScale || signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            // The unnormalized form is only supported for unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            // transferTypes and when the ColorSpace min/max values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // are 0.0/1.0.  When this method is called nonStdScale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            // true if the latter condition does not hold.  In addition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // the unnormalized form requires that the full range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // the pixel sample values map to the full 0.0 - 1.0 range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // of color component values.  That condition is checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            // later in this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            noUnnorm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            noUnnorm = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        float[] lowVal, highVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                byte[] bpixel = new byte[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    bpixel[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    bpixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        (byte) ((1 << nBits[numColorComponents]) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                lowVal = getNormalizedComponents(bpixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    bpixel[i] = (byte) ((1 << nBits[i]) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                highVal = getNormalizedComponents(bpixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                short[] uspixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    uspixel[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    uspixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        (short) ((1 << nBits[numColorComponents]) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                lowVal = getNormalizedComponents(uspixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    uspixel[i] = (short) ((1 << nBits[i]) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                highVal = getNormalizedComponents(uspixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                int[] ipixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    ipixel[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    ipixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        ((1 << nBits[numColorComponents]) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                lowVal = getNormalizedComponents(ipixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    ipixel[i] = ((1 << nBits[i]) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                highVal = getNormalizedComponents(ipixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                short[] spixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    spixel[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    spixel[numColorComponents] = 32767;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                lowVal = getNormalizedComponents(spixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    spixel[i] = 32767;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                highVal = getNormalizedComponents(spixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            lowVal = highVal = null;  // to keep the compiler from complaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        nonStdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if ((lowVal[i] != 0.0f) || (highVal[i] != 1.0f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                nonStdScale = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (nonStdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            noUnnorm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            is_sRGB_stdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            is_LinearRGB_stdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            is_LinearGray_stdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            is_ICCGray_stdScale = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            compOffset = new float[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            compScale = new float[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                compOffset[i] = lowVal[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                compScale[i] = 1.0f / (highVal[i] - lowVal[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private int getRGBComponent(int pixel, int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (numComponents > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                IllegalArgumentException("More than one component per pixel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                IllegalArgumentException("Component value is signed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        // Since there is only 1 component, there is no alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        // Normalize the pixel in order to convert it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        Object opixel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                byte[] bpixel = { (byte) pixel };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                opixel = bpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                short[] spixel = { (short) pixel };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                opixel = spixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                int[] ipixel = { pixel };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                opixel = ipixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        float[] norm = getNormalizedComponents(opixel, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        float[] rgb = colorSpace.toRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        return (int) (rgb[idx] * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Returns the red color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * is done if necessary.  The pixel value is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * The returned value will be a non pre-multiplied value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * If the alpha is premultiplied, this method divides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * it out before returning the value (if the alpha value is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * the red value will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param pixel The pixel from which you want to get the red color component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return The red color component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @throws IllegalArgumentException If there is more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * one component in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @throws IllegalArgumentException If the component value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * <CODE>ColorModel</CODE> is signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public int getRed(int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return getRGBComponent(pixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Returns the green color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * is done if necessary.  The pixel value is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * The returned value will be a non
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * pre-multiplied value. If the alpha is premultiplied, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * divides it out before returning the value (if the alpha value is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * the green value will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param pixel The pixel from which you want to get the green color component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return The green color component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @throws IllegalArgumentException If there is more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * one component in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @throws IllegalArgumentException If the component value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <CODE>ColorModel</CODE> is signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public int getGreen(int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return getRGBComponent(pixel, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * Returns the blue color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * is done if necessary.  The pixel value is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * The returned value will be a non
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * pre-multiplied value. If the alpha is premultiplied, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * divides it out before returning the value (if the alpha value is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * the blue value will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @param pixel The pixel from which you want to get the blue color component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return The blue color component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @throws IllegalArgumentException If there is more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * one component in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @throws IllegalArgumentException If the component value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <CODE>ColorModel</CODE> is signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    public int getBlue(int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        return getRGBComponent(pixel, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Returns the alpha component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * from 0 to 255.   The pixel value is specified as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param pixel The pixel from which you want to get the alpha component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @return The alpha component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @throws IllegalArgumentException If there is more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * one component in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @throws IllegalArgumentException If the component value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * <CODE>ColorModel</CODE> is signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public int getAlpha(int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (supportsAlpha == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if (numComponents > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                IllegalArgumentException("More than one component per pixel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                IllegalArgumentException("Component value is signed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return (int) ((((float) pixel) / ((1<<nBits[0])-1)) * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Returns the color/alpha components of the pixel in the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * RGB color model format.  A color conversion is done if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * The returned value will be in a non pre-multiplied format. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * the alpha is premultiplied, this method divides it out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * color components (if the alpha value is 0, the color values will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @param pixel The pixel from which you want to get the color/alpha components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @return The color/alpha components for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @throws IllegalArgumentException If there is more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * one component in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws IllegalArgumentException If the component value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <CODE>ColorModel</CODE> is signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public int getRGB(int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (numComponents > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                IllegalArgumentException("More than one component per pixel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                IllegalArgumentException("Component value is signed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return (getAlpha(pixel) << 24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            | (getRed(pixel) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            | (getGreen(pixel) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            | (getBlue(pixel) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    private int extractComponent(Object inData, int idx, int precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // Extract component idx from inData.  The precision argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // should be either 8 or 16.  If it's 8, this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        // an 8-bit value.  If it's 16, this method will return a 16-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        // value for transferTypes other than TYPE_BYTE.  For TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // an 8-bit value will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        // This method maps the input value corresponding to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        // normalized ColorSpace component value of 0.0 to 0, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        // input value corresponding to a normalized ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        // component value of 1.0 to 2^n - 1 (where n is 8 or 16), so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        // it is appropriate only for ColorSpaces with min/max component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        // values of 0.0/1.0.  This will be true for sRGB, the built-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        // Linear RGB and Linear Gray spaces, and any other ICC grayscale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        // spaces for which we have precomputed LUTs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        boolean needAlpha = (supportsAlpha && isAlphaPremultiplied);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        int alp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        int comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        int mask = (1 << nBits[idx]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // Note: we do no clamping of the pixel data here - we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            // assume that the data is scaled properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            case DataBuffer.TYPE_SHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                short sdata[] = (short[]) inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                float scalefactor = (float) ((1 << precision) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    short s = sdata[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    if (s != (short) 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                        return (int) ((((float) sdata[idx]) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                       ((float) s)) * scalefactor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    return (int) ((sdata[idx] / 32767.0f) * scalefactor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            case DataBuffer.TYPE_FLOAT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                float fdata[] = (float[]) inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                float scalefactor = (float) ((1 << precision) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    float f = fdata[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    if (f != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                        return (int) (((fdata[idx] / f) * scalefactor) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    return (int) (fdata[idx] * scalefactor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            case DataBuffer.TYPE_DOUBLE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                double ddata[] = (double[]) inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                double scalefactor = (double) ((1 << precision) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    double d = ddata[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    if (d != 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        return (int) (((ddata[idx] / d) * scalefactor) + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    return (int) (ddata[idx] * scalefactor + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
               byte bdata[] = (byte[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
               comp = bdata[idx] & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
               precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
               if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                   alp = bdata[numColorComponents] & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
               short usdata[] = (short[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
               comp = usdata[idx] & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
               if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                   alp = usdata[numColorComponents] & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
               int idata[] = (int[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
               comp = idata[idx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
               if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                   alp = idata[numColorComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
               throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                   UnsupportedOperationException("This method has not "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                   "been implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            if (alp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                float scalefactor = (float) ((1 << precision) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                float fcomp = ((float) comp) / ((float)mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                float invalp = ((float) ((1<<nBits[numColorComponents]) - 1)) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                               ((float) alp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                return (int) (fcomp * invalp * scalefactor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (nBits[idx] != precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                float scalefactor = (float) ((1 << precision) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                float fcomp = ((float) comp) / ((float)mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                return (int) (fcomp * scalefactor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    private int getRGBComponent(Object inData, int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        if (is_sRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return extractComponent(inData, idx, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        } else if (is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            int lutidx = extractComponent(inData, idx, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            return tosRGB8LUT[lutidx] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        } else if (is_ICCGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            int lutidx = extractComponent(inData, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            return tosRGB8LUT[lutidx] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        // Not CS_sRGB, CS_LINEAR_RGB, or any TYPE_GRAY ICC_ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        float[] norm = getNormalizedComponents(inData, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // Note that getNormalizedComponents returns non-premultiplied values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        float[] rgb = colorSpace.toRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        return (int) (rgb[idx] * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Returns the red color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * is done if necessary.  The <CODE>pixel</CODE> value is specified by an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * of data elements of type <CODE>transferType</CODE> passed in as an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * reference. The returned value will be a non pre-multiplied value. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * alpha is premultiplied, this method divides it out before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * the value (if the alpha value is 0, the red value will be 0). Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <code>ComponentColorModel</code> can be subclassed, subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * inherit the implementation of this method and if they don't override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * it then they throw an exception if they use an unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @param inData The pixel from which you want to get the red color component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * specified by an array of data elements of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @return The red color component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    public int getRed(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        return getRGBComponent(inData, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * Returns the green color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * A color conversion is done if necessary.  The <CODE>pixel</CODE> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * is specified by an array of data elements of type <CODE>transferType</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * passed in as an object reference. The returned value is a non pre-multiplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * value. If the alpha is premultiplied, this method divides it out before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * returning the value (if the alpha value is 0, the green value will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Since <code>ComponentColorModel</code> can be subclassed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * subclasses inherit the implementation of this method and if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * don't override it then they throw an exception if they use an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * unsupported <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @param inData The pixel from which you want to get the green color component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * specified by an array of data elements of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @return The green color component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public int getGreen(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        return getRGBComponent(inData, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Returns the blue color component for the specified pixel, scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * A color conversion is done if necessary.  The <CODE>pixel</CODE> value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * specified by an array of data elements of type <CODE>transferType</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * passed in as an object reference. The returned value is a non pre-multiplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * value. If the alpha is premultiplied, this method divides it out before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * returning the value (if the alpha value is 0, the blue value will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Since <code>ComponentColorModel</code> can be subclassed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * subclasses inherit the implementation of this method and if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * don't override it then they throw an exception if they use an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * unsupported <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @param inData The pixel from which you want to get the blue color component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * specified by an array of data elements of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @return The blue color component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    public int getBlue(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        return getRGBComponent(inData, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Returns the alpha component for the specified pixel, scaled from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * 0 to 255.  The pixel value is specified by an array of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * elements of type <CODE>transferType</CODE> passed in as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * object reference.  Since <code>ComponentColorModel</code> can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * subclassed, subclasses inherit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * implementation of this method and if they don't override it then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * they throw an exception if they use an unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @param inData The pixel from which you want to get the alpha component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * specified by an array of data elements of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @return The alpha component for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    public int getAlpha(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        if (supportsAlpha == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            return 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        int alpha = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        int aIdx = numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        int mask = (1 << nBits[aIdx]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                short sdata[] = (short[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                alpha = (int) ((sdata[aIdx] / 32767.0f) * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                return alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            case DataBuffer.TYPE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                float fdata[] = (float[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                alpha = (int) (fdata[aIdx] * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                return alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            case DataBuffer.TYPE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                double ddata[] = (double[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                alpha = (int) (ddata[aIdx] * 255.0 + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                return alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
               byte bdata[] = (byte[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
               alpha = bdata[aIdx] & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
               short usdata[] = (short[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
               alpha = usdata[aIdx] & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
               int idata[] = (int[])inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
               alpha = idata[aIdx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
               throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                   UnsupportedOperationException("This method has not "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                   "been implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (nBits[aIdx] == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            return (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                ((((float) alpha) / ((float) ((1 << nBits[aIdx]) - 1))) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * Returns the color/alpha components for the specified pixel in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * default RGB color model format.  A color conversion is done if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * necessary.  The pixel value is specified by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * array of data elements of type <CODE>transferType</CODE> passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * in as an object reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * The returned value is in a non pre-multiplied format. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * the alpha is premultiplied, this method divides it out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * color components (if the alpha value is 0, the color values will be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * Since <code>ComponentColorModel</code> can be subclassed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * subclasses inherit the implementation of this method and if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * don't override it then they throw an exception if they use an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * unsupported <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @param inData The pixel from which you want to get the color/alpha components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * specified by an array of data elements of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @return The color/alpha components for the specified pixel, as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    public int getRGB(Object inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            return (getAlpha(inData) << 24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                | (getRed(inData) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                | (getGreen(inData) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                | (getBlue(inData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        } else if (colorSpaceType == ColorSpace.TYPE_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            int gray = getRed(inData); // Red sRGB component should equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                                       // green and blue components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            return (getAlpha(inData) << 24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                | (gray << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                | (gray <<  8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                | gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        float[] norm = getNormalizedComponents(inData, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // Note that getNormalizedComponents returns non-premult values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        float[] rgb = colorSpace.toRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        return (getAlpha(inData) << 24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            | (((int) (rgb[0] * 255.0f + 0.5f)) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            | (((int) (rgb[1] * 255.0f + 0.5f)) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            | (((int) (rgb[2] * 255.0f + 0.5f)) << 0);
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
     * Returns a data element array representation of a pixel in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <CODE>ColorModel</CODE>, given an integer pixel representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * in the default RGB color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * This array can then be passed to the <CODE>setDataElements</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * method of a <CODE>WritableRaster</CODE> object.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <CODE>pixel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * parameter is null, a new array is allocated.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <code>ComponentColorModel</code> can be subclassed, subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * override it then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * they throw an exception if they use an unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @param rgb the integer representation of the pixel in the RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *            color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @return The data element array representation of a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @throws ClassCastException If <CODE>pixel</CODE> is not null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * is not a primitive array of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @throws ArrayIndexOutOfBoundsException If <CODE>pixel</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * not large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    public Object getDataElements(int rgb, Object pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // REMIND: Use rendering hints?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        int red, grn, blu, alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        red = (rgb>>16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        grn = (rgb>>8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        blu = rgb & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            // Handle SHORT, FLOAT, & DOUBLE here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            switch(transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    short sdata[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                    if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                        sdata = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                        sdata = (short[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                        factor = 32767.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                        if (is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                            red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                            grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                            blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                            factor = 32767.0f / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                            sdata[3] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                                (short) (alp * (32767.0f / 255.0f) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                                factor = alp * factor * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        sdata[0] = (short) (red * factor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                        sdata[1] = (short) (grn * factor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                        sdata[2] = (short) (blu * factor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    } else if (is_LinearGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                        red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                        grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                        blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                        float gray = ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                                      (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                                      (0.0721f * blu)) / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                        factor = 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                            sdata[1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                                (short) (alp * (32767.0f / 255.0f) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                                factor = alp * factor * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                        sdata[0] = (short) (gray * factor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    } else if (is_ICCGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                        red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                        grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                        blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        int gray = (int) ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                                          (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                                          (0.0721f * blu) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                        gray = fromLinearGray16ToOtherGray16LUT[gray] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                        factor = 32767.0f / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                            sdata[1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                                (short) (alp * (32767.0f / 255.0f) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                factor = alp * factor * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                        sdata[0] = (short) (gray * factor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                        factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                        float norm[] = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                        norm[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        norm[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                        norm[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                        norm = colorSpace.fromRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                        if (nonStdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                            for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                                norm[i] = (norm[i] - compOffset[i]) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                                          compScale[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                                // REMIND: need to analyze whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                                // clamping is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                                if (norm[i] < 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                    norm[i] = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                                if (norm[i] > 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                                    norm[i] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                        factor = 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                            sdata[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                (short) (alp * (32767.0f / 255.0f) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                                factor *= alp * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                        for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                            sdata[i] = (short) (norm[i] * factor + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    return sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            case DataBuffer.TYPE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                    float fdata[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                    if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                        fdata = new float[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                        fdata = (float[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                        if (is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                            red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                            grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                            blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                            factor = 1.0f / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                            factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                            fdata[3] = alp * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                                factor *= fdata[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                        fdata[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                        fdata[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                        fdata[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                    } else if (is_LinearGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                        red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                        grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                        blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                        fdata[0] = ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                    (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                                    (0.0721f * blu)) / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                            fdata[1] = alp * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                                fdata[0] *= fdata[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    } else if (is_ICCGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                        red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                        grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                        blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                        int gray = (int) ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                                          (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                                          (0.0721f * blu) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                        fdata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                                    0xffff) / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                            fdata[1] = alp * (1.0f / 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                                fdata[0] *= fdata[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                        float norm[] = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                        factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                        norm[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                        norm[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        norm[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                        norm = colorSpace.fromRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                            fdata[numColorComponents] = alp * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                                factor *= alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                                    norm[i] *= factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                        for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                            fdata[i] = norm[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                    return fdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            case DataBuffer.TYPE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    double ddata[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                    if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                        ddata = new double[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                        ddata = (double[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                        double factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                        if (is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                            red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                            grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                            blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                            factor = 1.0 / 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                            factor = 1.0 / 255.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                            ddata[3] = alp * (1.0 / 255.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                                factor *= ddata[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                        ddata[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                        ddata[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                        ddata[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    } else if (is_LinearGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                        red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                        grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                        blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                        ddata[0] = ((0.2125 * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                                    (0.7154 * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                                    (0.0721 * blu)) / 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                            ddata[1] = alp * (1.0 / 255.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                                ddata[0] *= ddata[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                    } else if (is_ICCGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                        red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                        grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                        blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                        int gray = (int) ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                                          (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                                          (0.0721f * blu) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                        ddata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                                    0xffff) / 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                            ddata[1] = alp * (1.0 / 255.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                                ddata[0] *= ddata[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                        float factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                        float norm[] = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                        norm[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                        norm[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                        norm[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                        norm = colorSpace.fromRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                            alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                            ddata[numColorComponents] = alp * (1.0 / 255.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                            if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                                factor *= alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                                    norm[i] *= factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                        for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                            ddata[i] = norm[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                    return ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        // Handle BYTE, USHORT, & INT here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        //REMIND: maybe more efficient not to use int array for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        //DataBuffer.TYPE_USHORT and DataBuffer.TYPE_INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        int intpixel[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        if (transferType == DataBuffer.TYPE_INT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            pixel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
           intpixel = (int[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            intpixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            int precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            if (is_LinearRGB_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                if (transferType == DataBuffer.TYPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    red = fromsRGB8LUT8[red] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                    grn = fromsRGB8LUT8[grn] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    blu = fromsRGB8LUT8[blu] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                    precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                    factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                    red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                    grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    precision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                    factor = 1.0f / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                alp = (rgb>>24)&0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                if (nBits[3] == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                    intpixel[3] = alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                    intpixel[3] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                        (alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                    factor *= (alp * (1.0f / 255.0f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                    precision = -1;  // force component calculations below
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            if (nBits[0] == precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                intpixel[0] = red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                intpixel[0] = (int) (red * factor * ((1<<nBits[0]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (nBits[1] == precision) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
  1517
                intpixel[1] = grn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                intpixel[1] = (int) (grn * factor * ((1<<nBits[1]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            if (nBits[2] == precision) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
  1523
                intpixel[2] = blu;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                intpixel[2] = (int) (blu * factor * ((1<<nBits[2]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        } else if (is_LinearGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            float gray = ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                          (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                          (0.0721f * blu)) / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                if (nBits[1] == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    intpixel[1] = alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    intpixel[1] = (int) (alp * (1.0f / 255.0f) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                         ((1 << nBits[1]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    gray *= (alp * (1.0f / 255.0f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        } else if (is_ICCGray_stdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            red = fromsRGB8LUT16[red] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            grn = fromsRGB8LUT16[grn] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            blu = fromsRGB8LUT16[blu] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            int gray16 = (int) ((0.2125f * red) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                                (0.7154f * grn) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                                (0.0721f * blu) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            float gray = (fromLinearGray16ToOtherGray16LUT[gray16] &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                          0xffff) / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                if (nBits[1] == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    intpixel[1] = alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                    intpixel[1] = (int) (alp * (1.0f / 255.0f) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                                         ((1 << nBits[1]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                    gray *= (alp * (1.0f / 255.0f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            // Need to convert the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            float[] norm = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            float factor = 1.0f / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            norm[0] = red * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            norm[1] = grn * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            norm[2] = blu * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            norm = colorSpace.fromRGB(norm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            if (nonStdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                    norm[i] = (norm[i] - compOffset[i]) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                              compScale[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                    // REMIND: need to analyze whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                    // clamping is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    if (norm[i] < 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                        norm[i] = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                    if (norm[i] > 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                        norm[i] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                alp = (rgb>>24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                if (nBits[numColorComponents] == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                    intpixel[numColorComponents] = alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    intpixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                        (int) (alp * factor *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                               ((1<<nBits[numColorComponents]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                    factor *= alp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                    for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                        norm[i] *= factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            for (int i = 0; i < numColorComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                intpixel[i] = (int) (norm[i] * ((1<<nBits[i]) - 1) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            case DataBuffer.TYPE_BYTE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
               byte bdata[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
               if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                   bdata = new byte[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                   bdata = (byte[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
               for (int i = 0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                   bdata[i] = (byte)(0xff&intpixel[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
               return bdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            case DataBuffer.TYPE_USHORT:{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
               short sdata[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
               if (pixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                   sdata = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                   sdata = (short[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
               for (int i = 0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                   sdata[i] = (short)(intpixel[i]&0xffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
               return sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                if (maxBits > 23) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    // fix 4412670 - for components of 24 or more bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    // some calculations done above with float precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    // may lose enough precision that the integer result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    // overflows nBits, so we need to clamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    for (int i = 0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                        if (intpixel[i] > ((1<<nBits[i]) - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                            intpixel[i] = (1<<nBits[i]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                return intpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        throw new IllegalArgumentException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                 "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
   /** Returns an array of unnormalized color/alpha components given a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * An IllegalArgumentException is thrown if the component value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * <CODE>ColorModel</CODE> is not conveniently representable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * unnormalized form.  Color/alpha components are stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * in the <CODE>components</CODE> array starting at <CODE>offset</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * (even if the array is allocated by this method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * @param pixel The pixel value specified as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * @param components An integer array in which to store the unnormalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * color/alpha components. If the <CODE>components</CODE> array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * a new array is allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * @param offset An offset into the <CODE>components</CODE> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * @return The components array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * @throws IllegalArgumentException If there is more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * component in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * @throws IllegalArgumentException If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * <CODE>ColorModel</CODE> does not support the unnormalized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @throws ArrayIndexOutOfBoundsException If the <CODE>components</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * array is not null and is not large enough to hold all the color and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * alpha components (starting at offset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    public int[] getComponents(int pixel, int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        if (numComponents > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                IllegalArgumentException("More than one component per pixel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        if (noUnnorm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    "This ColorModel does not support the unnormalized form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        if (components == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            components = new int[offset+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        components[offset+0] = (pixel & ((1<<nBits[0]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        return components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * Returns an array of unnormalized color/alpha components given a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * in this <CODE>ColorModel</CODE>.  The pixel value is specified by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * array of data elements of type <CODE>transferType</CODE> passed in as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * an object reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * An IllegalArgumentException is thrown if the component values for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * <CODE>ColorModel</CODE> are not conveniently representable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * unnormalized form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * Color/alpha components are stored in the <CODE>components</CODE> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * starting at  <CODE>offset</CODE> (even if the array is allocated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * this method).  Since <code>ComponentColorModel</code> can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * subclassed, subclasses inherit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * implementation of this method and if they don't override it then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * this method might throw an exception if they use an unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * @param pixel A pixel value specified by an array of data elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * @param components An integer array in which to store the unnormalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * color/alpha components. If the <CODE>components</CODE> array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * a new array is allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * @param offset An offset into the <CODE>components</CODE> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * @return The <CODE>components</CODE> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * @throws IllegalArgumentException If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * @throws UnsupportedOperationException in some cases iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * transfer type of this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * is not one of the following transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * or <CODE>DataBuffer.TYPE_INT</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * @throws ClassCastException If <CODE>pixel</CODE> is not a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * array of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * @throws IllegalArgumentException If the <CODE>components</CODE> array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * not null and is not large enough to hold all the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * components (starting at offset), or if <CODE>pixel</CODE> is not large
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * enough to hold a pixel value for this ColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    public int[] getComponents(Object pixel, int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        int intpixel[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        if (noUnnorm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                    "This ColorModel does not support the unnormalized form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        if (pixel instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            intpixel = (int[])pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            intpixel = DataBuffer.toIntArray(pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            if (intpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
               throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                   "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        if (intpixel.length < numComponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                ("Length of pixel array < number of components in model");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        if (components == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            components = new int[offset+numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        else if ((components.length-offset) < numComponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                ("Length of components array < number of components in model");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        System.arraycopy(intpixel, 0, components, offset, numComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        return components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * Returns an array of all of the color/alpha components in unnormalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * form, given a normalized component array.  Unnormalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * n is the number of bits for a particular component.  Normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * components are float values between a per component minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * maximum specified by the <code>ColorSpace</code> object for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * <code>ColorModel</code>.  An <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * will be thrown if color component values for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * <code>ColorModel</code> are not conveniently representable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * unnormalized form.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * <code>components</code> array is <code>null</code>, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * will be allocated.  The <code>components</code> array will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * be returned.  Color/alpha components are stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * <code>components</code> array starting at <code>offset</code> (even
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * if the array is allocated by this method). An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * <code>components</code> array is not <code>null</code> and is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * large enough to hold all the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * components (starting at <code>offset</code>).  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * <code>IllegalArgumentException</code> is thrown if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * <code>normComponents</code> array is not large enough to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * all the color and alpha components starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * <code>normOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @param normComponents an array containing normalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * @param normOffset the offset into the <code>normComponents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * array at which to start retrieving normalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * @param components an array that receives the components from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * <code>normComponents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * @param offset the index into <code>components</code> at which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * begin storing normalized components from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * <code>normComponents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * @return an array containing unnormalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * @throws IllegalArgumentException If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * @throws IllegalArgumentException if the length of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *          <code>normComponents</code> minus <code>normOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *          is less than <code>numComponents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    public int[] getUnnormalizedComponents(float[] normComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                                           int normOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                                           int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        if (noUnnorm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    "This ColorModel does not support the unnormalized form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        return super.getUnnormalizedComponents(normComponents, normOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                                               components, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * Returns an array of all of the color/alpha components in normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * form, given an unnormalized component array.  Unnormalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * n is the number of bits for a particular component.  Normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * components are float values between a per component minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * maximum specified by the <code>ColorSpace</code> object for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * <code>ColorModel</code>.  An <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * will be thrown if color component values for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * <code>ColorModel</code> are not conveniently representable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * unnormalized form.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * <code>normComponents</code> array is <code>null</code>, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * will be allocated.  The <code>normComponents</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * will be returned.  Color/alpha components are stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * <code>normComponents</code> array starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * <code>normOffset</code> (even if the array is allocated by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * method).  An <code>ArrayIndexOutOfBoundsException</code> is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * if the <code>normComponents</code> array is not <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * and is not large enough to hold all the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * (starting at <code>normOffset</code>).  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * <code>IllegalArgumentException</code> is thrown if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * <code>components</code> array is not large enough to hold all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * color and alpha components starting at <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * @param components an array containing unnormalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * @param offset the offset into the <code>components</code> array at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * which to start retrieving unnormalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * @param normComponents an array that receives the normalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * @param normOffset the index into <code>normComponents</code> at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * which to begin storing normalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * @return an array containing normalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * @throws IllegalArgumentException If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    public float[] getNormalizedComponents(int[] components, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                                           float[] normComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                                           int normOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        if (noUnnorm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    "This ColorModel does not support the unnormalized form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        return super.getNormalizedComponents(components, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                                             normComponents, normOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * Returns a pixel value represented as an int in this <CODE>ColorModel</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * given an array of unnormalized color/alpha components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * @param components An array of unnormalized color/alpha components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * @param offset An offset into the <CODE>components</CODE> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * @return A pixel value represented as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * @throws IllegalArgumentException If there is more than one component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @throws IllegalArgumentException If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    public int getDataElement(int[] components, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        if (numComponents == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            if (noUnnorm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                    IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                    "This ColorModel does not support the unnormalized form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            return components[offset+0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        throw new IllegalArgumentException("This model returns "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                                           numComponents+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                                           " elements in the pixel array.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * Returns a data element array representation of a pixel in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * <CODE>ColorModel</CODE>, given an array of unnormalized color/alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * components. This array can then be passed to the <CODE>setDataElements</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * method of a <CODE>WritableRaster</CODE> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @param components An array of unnormalized color/alpha components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * @param offset The integer offset into the <CODE>components</CODE> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * @param obj The object in which to store the data element array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * representation of the pixel. If <CODE>obj</CODE> variable is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * a new array is allocated.  If <CODE>obj</CODE> is not null, it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * be a primitive array of type <CODE>transferType</CODE>. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * <CODE>ArrayIndexOutOfBoundsException</CODE> is thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * <CODE>obj</CODE> is not large enough to hold a pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * for this <CODE>ColorModel</CODE>.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * <code>ComponentColorModel</code> can be subclassed, subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * inherit the implementation of this method and if they don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * override it then they throw an exception if they use an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * unsupported <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * @return The data element array representation of a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * in this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * @throws IllegalArgumentException If the components array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * is not large enough to hold all the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * (starting at offset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @throws ClassCastException If <CODE>obj</CODE> is not null and is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * primitive  array of type <CODE>transferType</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * @throws ArrayIndexOutOfBoundsException If <CODE>obj</CODE> is not large
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * enough to hold a pixel value for this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * @throws IllegalArgumentException If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * @throws UnsupportedOperationException If the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * is not one of the following transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * or <CODE>DataBuffer.TYPE_INT</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    public Object getDataElements(int[] components, int offset, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        if (noUnnorm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                    "This ColorModel does not support the unnormalized form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        if ((components.length-offset) < numComponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            throw new IllegalArgumentException("Component array too small"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                                               " (should be "+numComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        switch(transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                int[] pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    pixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    pixel = (int[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                System.arraycopy(components, offset, pixel, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                                 numComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                return pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                byte[] pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    pixel = new byte[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    pixel = (byte[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                    pixel[i] = (byte) (components[offset+i]&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                return pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                short[] pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    pixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                    pixel = (short[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    pixel[i] = (short) (components[offset+i]&0xffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                return pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                                        "implemented for transferType " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                                        transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * Returns a pixel value represented as an <code>int</code> in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * <code>ColorModel</code>, given an array of normalized color/alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * components.  This method will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * <code>IllegalArgumentException</code> if pixel values for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * <code>ColorModel</code> are not conveniently representable as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * single <code>int</code>.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * <code>ArrayIndexOutOfBoundsException</code> is thrown if  the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * <code>normComponents</code> array is not large enough to hold all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * color and alpha components (starting at <code>normOffset</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * @param normComponents an array of normalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * @param normOffset the index into <code>normComponents</code> at which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * begin retrieving the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * @return an <code>int</code> pixel value in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * <code>ColorModel</code> corresponding to the specified components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * @throws IllegalArgumentException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     *  pixel values for this <code>ColorModel</code> are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     *  conveniently representable as a single <code>int</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * @throws ArrayIndexOutOfBoundsException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     *  the <code>normComponents</code> array is not large enough to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     *  hold all of the color and alpha components starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     *  <code>normOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    public int getDataElement(float[] normComponents, int normOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        if (numComponents > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                IllegalArgumentException("More than one component per pixel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        if (signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                IllegalArgumentException("Component value is signed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        Object pixel = getDataElements(normComponents, normOffset, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                byte bpixel[] = (byte[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                return bpixel[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                short[] uspixel = (short[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                return uspixel[0] & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                int[] ipixel = (int[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                return ipixel[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            throw new UnsupportedOperationException("This method has not been "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                + "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * Returns a data element array representation of a pixel in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * <code>ColorModel</code>, given an array of normalized color/alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * components.  This array can then be passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * <code>setDataElements</code> method of a <code>WritableRaster</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * object.  An <code>ArrayIndexOutOfBoundsException</code> is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * if the <code>normComponents</code> array is not large enough to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * all the color and alpha components (starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * <code>normOffset</code>).  If the <code>obj</code> variable is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * <code>null</code>, a new array will be allocated.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * <code>obj</code> is not <code>null</code>, it must be a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * array of type transferType; otherwise, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * <code>ClassCastException</code> is thrown.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * <code>ArrayIndexOutOfBoundsException</code> is thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * <code>obj</code> is not large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * <code>ColorModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * @param normComponents an array of normalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * @param normOffset the index into <code>normComponents</code> at which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * begin retrieving color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * @param obj a primitive data array to hold the returned pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * @return an <code>Object</code> which is a primitive data array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * representation of a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * @throws ClassCastException if <code>obj</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     *  is not a primitive array of type <code>transferType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * @throws ArrayIndexOutOfBoundsException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *  <code>obj</code> is not large enough to hold a pixel value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     *  for this <code>ColorModel</code> or the <code>normComponents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *  array is not large enough to hold all of the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *  components starting at <code>normOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * @see WritableRaster#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * @see SampleModel#setDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    public Object getDataElements(float[] normComponents, int normOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                                  Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        boolean needAlpha = supportsAlpha && isAlphaPremultiplied;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        float[] stdNormComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        if (needScaleInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            initScale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        if (nonStdScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            stdNormComponents = new float[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                 c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                stdNormComponents[c] = (normComponents[nc] - compOffset[c]) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                                       compScale[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                // REMIND: need to analyze whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                // clamping is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                if (stdNormComponents[c] < 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    stdNormComponents[c] = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                if (stdNormComponents[c] > 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                    stdNormComponents[c] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                stdNormComponents[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                    normComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            normOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            stdNormComponents = normComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            byte[] bpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                bpixel = new byte[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                bpixel = (byte[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                float alpha =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                    stdNormComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                    bpixel[c] = (byte) ((stdNormComponents[nc] * alpha) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                                        ((float) ((1 << nBits[c]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                bpixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                    (byte) (alpha *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                            ((float) ((1 << nBits[numColorComponents]) - 1)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                            0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                for (int c = 0, nc = normOffset; c < numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    bpixel[c] = (byte) (stdNormComponents[nc] *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                                        ((float) ((1 << nBits[c]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            return bpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            short[] uspixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                uspixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                uspixel = (short[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                float alpha =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    stdNormComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    uspixel[c] = (short) ((stdNormComponents[nc] * alpha) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                                          ((float) ((1 << nBits[c]) - 1)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                                          0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                uspixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    (short) (alpha *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                             ((float) ((1 << nBits[numColorComponents]) - 1)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                             0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                for (int c = 0, nc = normOffset; c < numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    uspixel[c] = (short) (stdNormComponents[nc] *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                                          ((float) ((1 << nBits[c]) - 1)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                                          0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            return uspixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            int[] ipixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                ipixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                ipixel = (int[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                float alpha =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    stdNormComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    ipixel[c] = (int) ((stdNormComponents[nc] * alpha) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                                       ((float) ((1 << nBits[c]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                ipixel[numColorComponents] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                    (int) (alpha *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                           ((float) ((1 << nBits[numColorComponents]) - 1)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                           0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                for (int c = 0, nc = normOffset; c < numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                    ipixel[c] = (int) (stdNormComponents[nc] *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                                       ((float) ((1 << nBits[c]) - 1)) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            return ipixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            short[] spixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                spixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                spixel = (short[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                float alpha =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                    stdNormComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                    spixel[c] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                        (stdNormComponents[nc] * alpha * 32767.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                spixel[numColorComponents] = (short) (alpha * 32767.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                for (int c = 0, nc = normOffset; c < numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                    spixel[c] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                        (stdNormComponents[nc] * 32767.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            return spixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        case DataBuffer.TYPE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            float[] fpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                fpixel = new float[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                fpixel = (float[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                float alpha = normComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                    fpixel[c] = normComponents[nc] * alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                fpixel[numColorComponents] = alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                for (int c = 0, nc = normOffset; c < numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                    fpixel[c] = normComponents[nc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            return fpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        case DataBuffer.TYPE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            double[] dpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                dpixel = new double[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                dpixel = (double[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            if (needAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                double alpha =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                    (double) (normComponents[numColorComponents + normOffset]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                for (int c = 0, nc = normOffset; c < numColorComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    dpixel[c] = normComponents[nc] * alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                dpixel[numColorComponents] = alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                for (int c = 0, nc = normOffset; c < numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                     c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                    dpixel[c] = (double) normComponents[nc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            return dpixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                                        "implemented for transferType " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                                        transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * Returns an array of all of the color/alpha components in normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * form, given a pixel in this <code>ColorModel</code>.  The pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * value is specified by an array of data elements of type transferType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * passed in as an object reference.  If pixel is not a primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * of type transferType, a <code>ClassCastException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     * An <code>ArrayIndexOutOfBoundsException</code> is thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     * <code>pixel</code> is not large enough to hold a pixel value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * <code>ColorModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * Normalized components are float values between a per component minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * and maximum specified by the <code>ColorSpace</code> object for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * <code>ColorModel</code>.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * <code>normComponents</code> array is <code>null</code>, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * will be allocated.  The <code>normComponents</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * will be returned.  Color/alpha components are stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * <code>normComponents</code> array starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * <code>normOffset</code> (even if the array is allocated by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * method).  An <code>ArrayIndexOutOfBoundsException</code> is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * if the <code>normComponents</code> array is not <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * and is not large enough to hold all the color and alpha components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * (starting at <code>normOffset</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
  2321
     * This method must be overridden by a subclass if that subclass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * is designed to translate pixel sample values to color component values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * in a non-default way.  The default translations implemented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * class is described in the class comments.  Any subclass implementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * a non-default translation must follow the constraints on allowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * translations defined there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * @param pixel the specified pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * @param normComponents an array to receive the normalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * @param normOffset the offset into the <code>normComponents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * array at which to start storing normalized components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * @return an array containing normalized color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @throws ClassCastException if <code>pixel</code> is not a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     *          array of type transferType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     * @throws ArrayIndexOutOfBoundsException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     *          <code>normComponents</code> is not large enough to hold all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     *          color and alpha components starting at <code>normOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     * @throws ArrayIndexOutOfBoundsException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     *          <code>pixel</code> is not large enough to hold a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     *          value for this <code>ColorModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    public float[] getNormalizedComponents(Object pixel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                                           float[] normComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                                           int normOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        if (normComponents == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            normComponents = new float[numComponents+normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            byte[] bpixel = (byte[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                normComponents[nc] = ((float) (bpixel[c] & 0xff)) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                                     ((float) ((1 << nBits[c]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            short[] uspixel = (short[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                normComponents[nc] = ((float) (uspixel[c] & 0xffff)) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                                     ((float) ((1 << nBits[c]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            int[] ipixel = (int[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                normComponents[nc] = ((float) ipixel[c]) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                                     ((float) ((1 << nBits[c]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            short[] spixel = (short[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                normComponents[nc] = ((float) spixel[c]) / 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        case DataBuffer.TYPE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            float[] fpixel = (float[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                normComponents[nc] = fpixel[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        case DataBuffer.TYPE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            double[] dpixel = (double[]) pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                normComponents[nc] = (float) dpixel[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                                        "implemented for transferType " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                                        transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        if (supportsAlpha && isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            float alpha = normComponents[numColorComponents + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            if (alpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                float invAlpha = 1.0f / alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                for (int c = normOffset; c < numColorComponents + normOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                     c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                    normComponents[c] *= invAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        if (min != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            // Normally (i.e. when this class is not subclassed to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            // this method), the test (min != null) will be equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            // the test (nonStdScale).  However, there is an unlikely, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            // possible case, in which this method is overridden, nonStdScale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            // is set true by initScale(), the subclass method for some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            // reason calls this superclass method, but the min and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            // diffMinMax arrays were never initialized by setupLUTs().  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            // that case, the right thing to do is follow the intended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            // semantics of this method, and rescale the color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            // only if the ColorSpace min/max were detected to be other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            // than 0.0/1.0 by setupLUTs().  Note that this implies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            // transferType is byte, ushort, int, or short - i.e. components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            // derived from float and double pixel data are never rescaled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            for (int c = 0; c < numColorComponents; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                normComponents[c + normOffset] = min[c] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                    diffMinMax[c] * normComponents[c + normOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        return normComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * Forces the raster data to match the state specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * <CODE>isAlphaPremultiplied</CODE> variable, assuming the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * is currently correctly described by this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * It may multiply or divide the color raster data by alpha, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * do nothing if the data is in the correct state.  If the data needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * to be coerced, this method also returns an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * this <CODE>ColorModel</CODE> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * the <CODE>isAlphaPremultiplied</CODE> flag set appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * Since <code>ColorModel</code> can be subclassed, subclasses inherit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * the implementation of this method and if they don't override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * then they throw an exception if they use an unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * <code>transferType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
     * @throws NullPointerException if <code>raster</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     * <code>null</code> and data coercion is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     * @throws UnsupportedOperationException if the transfer type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     * this <CODE>ComponentColorModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
     * is not one of the supported transfer types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    public ColorModel coerceData (WritableRaster raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                                  boolean isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        if ((supportsAlpha == false) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
            (this.isAlphaPremultiplied == isAlphaPremultiplied))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            // Nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        int w = raster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        int h = raster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        int aIdx = raster.getNumBands() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        float normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        int rminX = raster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        int rY = raster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        int rX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
        if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                case DataBuffer.TYPE_BYTE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                    byte pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                    byte zpixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                    float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                            pixel = (byte[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                                                                   pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                            normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                                    pixel[c] = (byte)((pixel[c] & 0xff) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                                                      normAlpha + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                                    zpixel = new byte[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                                    java.util.Arrays.fill(zpixel, (byte) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                                raster.setDataElements(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                case DataBuffer.TYPE_USHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                    short pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                    short zpixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                    float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                            pixel = (short[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                                                                    pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                            normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                                    pixel[c] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                                        ((pixel[c] & 0xffff) * normAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                                         0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                                    zpixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                                    java.util.Arrays.fill(zpixel, (short) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                                raster.setDataElements(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                case DataBuffer.TYPE_INT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                    int pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                    int zpixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                    float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                            pixel = (int[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                                                                  pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                                    pixel[c] = (int) (pixel[c] * normAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                                                      0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                                    zpixel = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                                    java.util.Arrays.fill(zpixel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                                raster.setDataElements(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                case DataBuffer.TYPE_SHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                    short pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                    short zpixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                    float alphaScale = 1.0f / 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                            pixel = (short[]) raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                                                                     pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                                    pixel[c] = (short) (pixel[c] * normAlpha +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                                                        0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                                    zpixel = new short[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                                    java.util.Arrays.fill(zpixel, (short) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                                raster.setDataElements(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                case DataBuffer.TYPE_FLOAT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                    float pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                    float zpixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                            pixel = (float[]) raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                                                                     pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                            normAlpha = pixel[aIdx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                                    pixel[c] *= normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                                    zpixel = new float[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                                    java.util.Arrays.fill(zpixel, 0.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                                raster.setDataElements(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                case DataBuffer.TYPE_DOUBLE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                    double pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                    double zpixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                            pixel = (double[]) raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                                                                      pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                            double dnormAlpha = pixel[aIdx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                            if (dnormAlpha != 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                                    pixel[c] *= dnormAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                                if (zpixel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                                    zpixel = new double[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                                    java.util.Arrays.fill(zpixel, 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
                                raster.setDataElements(rX, rY, zpixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                    throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                         "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            // We are premultiplied and want to divide it out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                case DataBuffer.TYPE_BYTE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                    byte pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                    float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                            pixel = (byte[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                                                                   pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                            normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                                    pixel[c] = (byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                                        ((pixel[c] & 0xff) * invAlpha + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                case DataBuffer.TYPE_USHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                    short pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                    float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                            pixel = (short[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                                                                    pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                            normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                                    pixel[c] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                                        ((pixel[c] & 0xffff) * invAlpha + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                case DataBuffer.TYPE_INT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                    int pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                    float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                            pixel = (int[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
                                                                  pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                                    pixel[c] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                                        (pixel[c] * invAlpha + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                case DataBuffer.TYPE_SHORT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                    short pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                    float alphaScale = 1.0f / 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                            pixel = (short[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                                                                    pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                            normAlpha = pixel[aIdx] * alphaScale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
                                    pixel[c] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                                        (pixel[c] * invAlpha + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                case DataBuffer.TYPE_FLOAT: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                    float pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                            pixel = (float[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                                                                    pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                            normAlpha = pixel[aIdx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                            if (normAlpha != 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                                float invAlpha = 1.0f / normAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                                    pixel[c] *= invAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                case DataBuffer.TYPE_DOUBLE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                    double pixel[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                    for (int y = 0; y < h; y++, rY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                        rX = rminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                        for (int x = 0; x < w; x++, rX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                            pixel = (double[])raster.getDataElements(rX, rY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                                                                     pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
                            double dnormAlpha = pixel[aIdx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                            if (dnormAlpha != 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                                double invAlpha = 1.0 / dnormAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                                for (int c=0; c < aIdx; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                                    pixel[c] *= invAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                                raster.setDataElements(rX, rY, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                    throw new UnsupportedOperationException("This method has not been "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                         "implemented for transferType " + transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
        // Return a new color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        if (!signed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
            return new ComponentColorModel(colorSpace, nBits, supportsAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                                           isAlphaPremultiplied, transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                                           transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
            return new ComponentColorModel(colorSpace, supportsAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                                           isAlphaPremultiplied, transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                                           transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
      * Returns true if <CODE>raster</CODE> is compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
      * <CODE>ColorModel</CODE>; false if it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
      * @param raster The <CODE>Raster</CODE> object to test for compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
      * @return <CODE>true</CODE> if <CODE>raster</CODE> is compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
      * <CODE>ColorModel</CODE>, <CODE>false</CODE> if it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
    public boolean isCompatibleRaster(Raster raster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
        SampleModel sm = raster.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        if (sm instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
            if (sm.getNumBands() != getNumComponents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            for (int i=0; i<nBits.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                if (sm.getSampleSize(i) < nBits[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
            return (raster.getTransferType() == transferType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * Creates a <CODE>WritableRaster</CODE> with the specified width and height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * that  has a data layout (<CODE>SampleModel</CODE>) compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * @param w The width of the <CODE>WritableRaster</CODE> you want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * @param h The height of the <CODE>WritableRaster</CODE> you want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * @return A <CODE>WritableRaster</CODE> that is compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
     * this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
     * @see WritableRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
    public WritableRaster createCompatibleWritableRaster (int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
        int dataSize = w*h*numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        WritableRaster raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
            raster = Raster.createInterleavedRaster(transferType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                                                    w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                                                    numComponents, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            SampleModel sm = createCompatibleSampleModel(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
            DataBuffer db = sm.createDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
            raster = Raster.createWritableRaster(sm, db, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
        return raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * Creates a <CODE>SampleModel</CODE> with the specified width and height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * that  has a data layout compatible with this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     * @param w The width of the <CODE>SampleModel</CODE> you want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * @param h The height of the <CODE>SampleModel</CODE> you want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * @return A <CODE>SampleModel</CODE> that is compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
    public SampleModel createCompatibleSampleModel(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
        int[] bandOffsets = new int[numComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            bandOffsets[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        switch (transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            return new PixelInterleavedSampleModel(transferType, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                                                   numComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                                                   w*numComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                                                   bandOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
            return new ComponentSampleModel(transferType, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                                            numComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                                            w*numComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                                            bandOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
     * Checks whether or not the specified <CODE>SampleModel</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * is compatible with this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     * @param sm The <CODE>SampleModel</CODE> to test for compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * @return <CODE>true</CODE> if the <CODE>SampleModel</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * compatible with this <CODE>ColorModel</CODE>, <CODE>false</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     * if it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     * @see SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
    public boolean isCompatibleSampleModel(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        if (!(sm instanceof ComponentSampleModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        // Must have the same number of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        if (numComponents != sm.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        if (sm.getTransferType() != transferType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
     * Returns a <CODE>Raster</CODE> representing the alpha channel of an image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
     * extracted from the input <CODE>Raster</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
     * This method assumes that <CODE>Raster</CODE> objects associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
     * this <CODE>ColorModel</CODE> store the alpha band, if present, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
     * the last band of image data. Returns null if there is no separate spatial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
     * alpha channel associated with this <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
     * This method creates a new <CODE>Raster</CODE>, but will share the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     * @param raster The <CODE>WritableRaster</CODE> from which to extract the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * alpha  channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     * @return A <CODE>WritableRaster</CODE> containing the image's alpha channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
    public WritableRaster getAlphaRaster(WritableRaster raster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        if (hasAlpha() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        int x = raster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        int y = raster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        int[] band = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        band[0] = raster.getNumBands() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
        return raster.createWritableChild(x, y, raster.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                                          raster.getHeight(), x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                                          band);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     * Compares this color model with another for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     * @param obj The object to compare with this color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
     * @return <CODE>true</CODE> if the color model objects are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
     * <CODE>false</CODE> if they are not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        if (!super.equals(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        if (obj.getClass() !=  getClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
}