class that works with pixel values that
represent color and alpha information as separate samples and that
store each sample in a separate data element. This class can be
used with an arbitrary
. The number of
color samples in the pixel values must be same as the number of
color components in the
. There may be a
single alpha sample.
The translation from pixel sample values to color/alpha components for
display or processing purposes is based on a one-to-one correspondence of
samples to components.
Depending on the transfer type used to create an instance of
ComponentColorModel
, the pixel sample values
represented by that instance may be signed or unsigned and may
be of integral type or float or double (see below for details).
The translation from sample values to normalized color/alpha components
must follow certain rules. For float and double samples, the translation
is an identity, i.e. normalized component values are equal to the
corresponding sample values. For integral samples, the translation
should be only a simple scale and offset, where the scale and offset
constants may be different for each component. The result of
applying the scale and offset constants is a set of color/alpha
component values, which are guaranteed to fall within a certain
range. Typically, the range for a color component will be the range
defined by the getMinValue
and getMaxValue
methods of the ColorSpace
class. The range for an
alpha component should be 0.0 to 1.0.
For instances with unsigned sample values,
the unnormalized color/alpha component representation is only
supported if two conditions hold. First, sample value value 0 must
map to normalized component value 0.0 and sample value 2n - 1
to 1.0. Second the min/max range of all color components of the
ColorSpace
must be 0.0 to 1.0. In this case, the
component representation is the n least
significant bits of the corresponding sample. Thus each component is
an unsigned integral value between 0 and 2n - 1, where
n is the number of significant bits for a particular component.
If these conditions are not met, any method taking an unnormalized
component argument will throw an IllegalArgumentException
.
It is possible to have color/alpha sample values
which cannot be reasonably interpreted as component values for rendering.
This can happen when ComponentColorModel
is subclassed to
override the mapping of unsigned sample values to normalized color
component values or when signed sample values outside a certain range
are used. (As an example, specifying an alpha component as a signed
short value outside the range 0 to 32767, normalized range 0.0 to 1.0, can
lead to unexpected results.) It is the
responsibility of applications to appropriately scale pixel data before
rendering such that color components fall within the normalized range
of the ColorSpace
(obtained using the getMinValue
and getMaxValue
methods of the ColorSpace
class)
and the alpha component is between 0.0 and 1.0. If color or alpha
component values fall outside these ranges, rendering results are
indeterminate.
Method from java.awt.image.ComponentColorModel Detail: |
public ColorModel coerceData(WritableRaster raster,
boolean isAlphaPremultiplied) {
if ((supportsAlpha == false) ||
(this.isAlphaPremultiplied == isAlphaPremultiplied))
{
// Nothing to do
return this;
}
int w = raster.getWidth();
int h = raster.getHeight();
int aIdx = raster.getNumBands() - 1;
float normAlpha;
int rminX = raster.getMinX();
int rY = raster.getMinY();
int rX;
if (isAlphaPremultiplied) {
switch (transferType) {
case DataBuffer.TYPE_BYTE: {
byte pixel[] = null;
byte zpixel[] = null;
float alphaScale = 1.0f / ((float) ((1< < nBits[aIdx]) - 1));
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (byte[])raster.getDataElements(rX, rY,
pixel);
normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
if (normAlpha != 0.0f) {
for (int c=0; c < aIdx; c++) {
pixel[c] = (byte)((pixel[c] & 0xff) *
normAlpha + 0.5f);
}
raster.setDataElements(rX, rY, pixel);
} else {
if (zpixel == null) {
zpixel = new byte[numComponents];
java.util.Arrays.fill(zpixel, (byte) 0);
}
raster.setDataElements(rX, rY, zpixel);
}
}
}
}
break;
case DataBuffer.TYPE_USHORT: {
short pixel[] = null;
short zpixel[] = null;
float alphaScale = 1.0f / ((float) ((1< < nBits[aIdx]) - 1));
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (short[])raster.getDataElements(rX, rY,
pixel);
normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
if (normAlpha != 0.0f) {
for (int c=0; c < aIdx; c++) {
pixel[c] = (short)
((pixel[c] & 0xffff) * normAlpha +
0.5f);
}
raster.setDataElements(rX, rY, pixel);
} else {
if (zpixel == null) {
zpixel = new short[numComponents];
java.util.Arrays.fill(zpixel, (short) 0);
}
raster.setDataElements(rX, rY, zpixel);
}
}
}
}
break;
case DataBuffer.TYPE_INT: {
int pixel[] = null;
int zpixel[] = null;
float alphaScale = 1.0f / ((float) ((1< < nBits[aIdx]) - 1));
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (int[])raster.getDataElements(rX, rY,
pixel);
normAlpha = pixel[aIdx] * alphaScale;
if (normAlpha != 0.0f) {
for (int c=0; c < aIdx; c++) {
pixel[c] = (int) (pixel[c] * normAlpha +
0.5f);
}
raster.setDataElements(rX, rY, pixel);
} else {
if (zpixel == null) {
zpixel = new int[numComponents];
java.util.Arrays.fill(zpixel, 0);
}
raster.setDataElements(rX, rY, zpixel);
}
}
}
}
break;
case DataBuffer.TYPE_SHORT: {
short pixel[] = null;
short zpixel[] = null;
float alphaScale = 1.0f / 32767.0f;
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (short[]) raster.getDataElements(rX, rY,
pixel);
normAlpha = pixel[aIdx] * alphaScale;
if (normAlpha != 0.0f) {
for (int c=0; c < aIdx; c++) {
pixel[c] = (short) (pixel[c] * normAlpha +
0.5f);
}
raster.setDataElements(rX, rY, pixel);
} else {
if (zpixel == null) {
zpixel = new short[numComponents];
java.util.Arrays.fill(zpixel, (short) 0);
}
raster.setDataElements(rX, rY, zpixel);
}
}
}
}
break;
case DataBuffer.TYPE_FLOAT: {
float pixel[] = null;
float zpixel[] = null;
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (float[]) raster.getDataElements(rX, rY,
pixel);
normAlpha = pixel[aIdx];
if (normAlpha != 0.0f) {
for (int c=0; c < aIdx; c++) {
pixel[c] *= normAlpha;
}
raster.setDataElements(rX, rY, pixel);
} else {
if (zpixel == null) {
zpixel = new float[numComponents];
java.util.Arrays.fill(zpixel, 0.0f);
}
raster.setDataElements(rX, rY, zpixel);
}
}
}
}
break;
case DataBuffer.TYPE_DOUBLE: {
double pixel[] = null;
double zpixel[] = null;
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (double[]) raster.getDataElements(rX, rY,
pixel);
double dnormAlpha = pixel[aIdx];
if (dnormAlpha != 0.0) {
for (int c=0; c < aIdx; c++) {
pixel[c] *= dnormAlpha;
}
raster.setDataElements(rX, rY, pixel);
} else {
if (zpixel == null) {
zpixel = new double[numComponents];
java.util.Arrays.fill(zpixel, 0.0);
}
raster.setDataElements(rX, rY, zpixel);
}
}
}
}
break;
default:
throw new UnsupportedOperationException("This method has not been "+
"implemented for transferType " + transferType);
}
}
else {
// We are premultiplied and want to divide it out
switch (transferType) {
case DataBuffer.TYPE_BYTE: {
byte pixel[] = null;
float alphaScale = 1.0f / ((float) ((1< < nBits[aIdx]) - 1));
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (byte[])raster.getDataElements(rX, rY,
pixel);
normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
if (normAlpha != 0.0f) {
float invAlpha = 1.0f / normAlpha;
for (int c=0; c < aIdx; c++) {
pixel[c] = (byte)
((pixel[c] & 0xff) * invAlpha + 0.5f);
}
raster.setDataElements(rX, rY, pixel);
}
}
}
}
break;
case DataBuffer.TYPE_USHORT: {
short pixel[] = null;
float alphaScale = 1.0f / ((float) ((1< < nBits[aIdx]) - 1));
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (short[])raster.getDataElements(rX, rY,
pixel);
normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
if (normAlpha != 0.0f) {
float invAlpha = 1.0f / normAlpha;
for (int c=0; c < aIdx; c++) {
pixel[c] = (short)
((pixel[c] & 0xffff) * invAlpha + 0.5f);
}
raster.setDataElements(rX, rY, pixel);
}
}
}
}
break;
case DataBuffer.TYPE_INT: {
int pixel[] = null;
float alphaScale = 1.0f / ((float) ((1< < nBits[aIdx]) - 1));
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (int[])raster.getDataElements(rX, rY,
pixel);
normAlpha = pixel[aIdx] * alphaScale;
if (normAlpha != 0.0f) {
float invAlpha = 1.0f / normAlpha;
for (int c=0; c < aIdx; c++) {
pixel[c] = (int)
(pixel[c] * invAlpha + 0.5f);
}
raster.setDataElements(rX, rY, pixel);
}
}
}
}
break;
case DataBuffer.TYPE_SHORT: {
short pixel[] = null;
float alphaScale = 1.0f / 32767.0f;
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (short[])raster.getDataElements(rX, rY,
pixel);
normAlpha = pixel[aIdx] * alphaScale;
if (normAlpha != 0.0f) {
float invAlpha = 1.0f / normAlpha;
for (int c=0; c < aIdx; c++) {
pixel[c] = (short)
(pixel[c] * invAlpha + 0.5f);
}
raster.setDataElements(rX, rY, pixel);
}
}
}
}
break;
case DataBuffer.TYPE_FLOAT: {
float pixel[] = null;
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (float[])raster.getDataElements(rX, rY,
pixel);
normAlpha = pixel[aIdx];
if (normAlpha != 0.0f) {
float invAlpha = 1.0f / normAlpha;
for (int c=0; c < aIdx; c++) {
pixel[c] *= invAlpha;
}
raster.setDataElements(rX, rY, pixel);
}
}
}
}
break;
case DataBuffer.TYPE_DOUBLE: {
double pixel[] = null;
for (int y = 0; y < h; y++, rY++) {
rX = rminX;
for (int x = 0; x < w; x++, rX++) {
pixel = (double[])raster.getDataElements(rX, rY,
pixel);
double dnormAlpha = pixel[aIdx];
if (dnormAlpha != 0.0) {
double invAlpha = 1.0 / dnormAlpha;
for (int c=0; c < aIdx; c++) {
pixel[c] *= invAlpha;
}
raster.setDataElements(rX, rY, pixel);
}
}
}
}
break;
default:
throw new UnsupportedOperationException("This method has not been "+
"implemented for transferType " + transferType);
}
}
// Return a new color model
if (!signed) {
return new ComponentColorModel(colorSpace, nBits, supportsAlpha,
isAlphaPremultiplied, transparency,
transferType);
} else {
return new ComponentColorModel(colorSpace, supportsAlpha,
isAlphaPremultiplied, transparency,
transferType);
}
}
Forces the raster data to match the state specified in the
isAlphaPremultiplied variable, assuming the data
is currently correctly described by this ColorModel .
It may multiply or divide the color raster data by alpha, or
do nothing if the data is in the correct state. If the data needs
to be coerced, this method also returns an instance of
this ColorModel with
the isAlphaPremultiplied flag set appropriately.
Since ColorModel can be subclassed, subclasses inherit
the implementation of this method and if they don't override it
then they throw an exception if they use an unsupported
transferType . |
public SampleModel createCompatibleSampleModel(int w,
int h) {
int[] bandOffsets = new int[numComponents];
for (int i=0; i < numComponents; i++) {
bandOffsets[i] = i;
}
switch (transferType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
return new PixelInterleavedSampleModel(transferType, w, h,
numComponents,
w*numComponents,
bandOffsets);
default:
return new ComponentSampleModel(transferType, w, h,
numComponents,
w*numComponents,
bandOffsets);
}
}
Creates a SampleModel with the specified width and height,
that has a data layout compatible with this ColorModel . |
public WritableRaster createCompatibleWritableRaster(int w,
int h) {
int dataSize = w*h*numComponents;
WritableRaster raster = null;
switch (transferType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
raster = Raster.createInterleavedRaster(transferType,
w, h,
numComponents, null);
break;
default:
SampleModel sm = createCompatibleSampleModel(w, h);
DataBuffer db = sm.createDataBuffer();
raster = Raster.createWritableRaster(sm, db, null);
}
return raster;
}
Creates a WritableRaster with the specified width and height,
that has a data layout (SampleModel ) compatible with
this ColorModel . |
public boolean equals(Object obj) {
if (!super.equals(obj)) {
return false;
}
if (obj.getClass() != getClass()) {
return false;
}
return true;
}
Compares this color model with another for equality. |
public int getAlpha(int pixel) {
if (supportsAlpha == false) {
return 255;
}
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (signed) {
throw new
IllegalArgumentException("Component value is signed");
}
return (int) ((((float) pixel) / ((1< < nBits[0])-1)) * 255.0f + 0.5f);
}
Returns the alpha component for the specified pixel, scaled
from 0 to 255. The pixel value is specified as an int. |
public int getAlpha(Object inData) {
if (supportsAlpha == false) {
return 255;
}
int alpha = 0;
int aIdx = numColorComponents;
int mask = (1 < < nBits[aIdx]) - 1;
switch (transferType) {
case DataBuffer.TYPE_SHORT:
short sdata[] = (short[])inData;
alpha = (int) ((sdata[aIdx] / 32767.0f) * 255.0f + 0.5f);
return alpha;
case DataBuffer.TYPE_FLOAT:
float fdata[] = (float[])inData;
alpha = (int) (fdata[aIdx] * 255.0f + 0.5f);
return alpha;
case DataBuffer.TYPE_DOUBLE:
double ddata[] = (double[])inData;
alpha = (int) (ddata[aIdx] * 255.0 + 0.5);
return alpha;
case DataBuffer.TYPE_BYTE:
byte bdata[] = (byte[])inData;
alpha = bdata[aIdx] & mask;
break;
case DataBuffer.TYPE_USHORT:
short usdata[] = (short[])inData;
alpha = usdata[aIdx] & mask;
break;
case DataBuffer.TYPE_INT:
int idata[] = (int[])inData;
alpha = idata[aIdx];
break;
default:
throw new
UnsupportedOperationException("This method has not "+
"been implemented for transferType " + transferType);
}
if (nBits[aIdx] == 8) {
return alpha;
} else {
return (int)
((((float) alpha) / ((float) ((1 < < nBits[aIdx]) - 1))) *
255.0f + 0.5f);
}
}
Returns the alpha component for the specified pixel, scaled from
0 to 255. The pixel value is specified by an array of data
elements of type transferType passed in as an
object reference. Since ComponentColorModel can be
subclassed, subclasses inherit the
implementation of this method and if they don't override it then
they throw an exception if they use an unsupported
transferType . |
public WritableRaster getAlphaRaster(WritableRaster raster) {
if (hasAlpha() == false) {
return null;
}
int x = raster.getMinX();
int y = raster.getMinY();
int[] band = new int[1];
band[0] = raster.getNumBands() - 1;
return raster.createWritableChild(x, y, raster.getWidth(),
raster.getHeight(), x, y,
band);
}
Returns a Raster representing the alpha channel of an image,
extracted from the input Raster .
This method assumes that Raster objects associated with
this ColorModel store the alpha band, if present, as
the last band of image data. Returns null if there is no separate spatial
alpha channel associated with this ColorModel .
This method creates a new Raster , but will share the data
array. |
public int getBlue(int pixel) {
return getRGBComponent(pixel, 2);
}
Returns the blue color component for the specified pixel, scaled
from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
is done if necessary. The pixel value is specified as an int.
The returned value will be a non
pre-multiplied value. If the alpha is premultiplied, this method
divides it out before returning the value (if the alpha value is 0,
the blue value will be 0). |
public int getBlue(Object inData) {
return getRGBComponent(inData, 2);
}
Returns the blue color component for the specified pixel, scaled
from 0 to 255 in the default RGB ColorSpace , sRGB.
A color conversion is done if necessary. The pixel value is
specified by an array of data elements of type transferType
passed in as an object reference. The returned value is a non pre-multiplied
value. If the alpha is premultiplied, this method divides it out before
returning the value (if the alpha value is 0, the blue value will be 0).
Since ComponentColorModel can be subclassed,
subclasses inherit the implementation of this method and if they
don't override it then they throw an exception if they use an
unsupported transferType . |
public int[] getComponents(int pixel,
int[] components,
int offset) {
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (needScaleInit) {
initScale();
}
if (noUnnorm) {
throw new
IllegalArgumentException(
"This ColorModel does not support the unnormalized form");
}
if (components == null) {
components = new int[offset+1];
}
components[offset+0] = (pixel & ((1< < nBits[0]) - 1));
return components;
}
Returns an array of unnormalized color/alpha components given a pixel
in this ColorModel .
An IllegalArgumentException is thrown if the component value for this
ColorModel is not conveniently representable in the
unnormalized form. Color/alpha components are stored
in the components array starting at offset
(even if the array is allocated by this method). |
public int[] getComponents(Object pixel,
int[] components,
int offset) {
int intpixel[];
if (needScaleInit) {
initScale();
}
if (noUnnorm) {
throw new
IllegalArgumentException(
"This ColorModel does not support the unnormalized form");
}
if (pixel instanceof int[]) {
intpixel = (int[])pixel;
} else {
intpixel = DataBuffer.toIntArray(pixel);
if (intpixel == null) {
throw new UnsupportedOperationException("This method has not been "+
"implemented for transferType " + transferType);
}
}
if (intpixel.length < numComponents) {
throw new IllegalArgumentException
("Length of pixel array < number of components in model");
}
if (components == null) {
components = new int[offset+numComponents];
}
else if ((components.length-offset) < numComponents) {
throw new IllegalArgumentException
("Length of components array < number of components in model");
}
System.arraycopy(intpixel, 0, components, offset, numComponents);
return components;
}
Returns an array of unnormalized color/alpha components given a pixel
in this ColorModel . The pixel value is specified by an
array of data elements of type transferType passed in as
an object reference.
An IllegalArgumentException is thrown if the component values for this
ColorModel are not conveniently representable in the
unnormalized form.
Color/alpha components are stored in the components array
starting at offset (even if the array is allocated by
this method). Since ComponentColorModel can be
subclassed, subclasses inherit the
implementation of this method and if they don't override it then
this method might throw an exception if they use an unsupported
transferType . |
public int getDataElement(int[] components,
int offset) {
if (needScaleInit) {
initScale();
}
if (numComponents == 1) {
if (noUnnorm) {
throw new
IllegalArgumentException(
"This ColorModel does not support the unnormalized form");
}
return components[offset+0];
}
throw new IllegalArgumentException("This model returns "+
numComponents+
" elements in the pixel array.");
}
Returns a pixel value represented as an int in this ColorModel ,
given an array of unnormalized color/alpha components. |
public int getDataElement(float[] normComponents,
int normOffset) {
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (signed) {
throw new
IllegalArgumentException("Component value is signed");
}
if (needScaleInit) {
initScale();
}
Object pixel = getDataElements(normComponents, normOffset, null);
switch (transferType) {
case DataBuffer.TYPE_BYTE:
{
byte bpixel[] = (byte[]) pixel;
return bpixel[0] & 0xff;
}
case DataBuffer.TYPE_USHORT:
{
short[] uspixel = (short[]) pixel;
return uspixel[0] & 0xffff;
}
case DataBuffer.TYPE_INT:
{
int[] ipixel = (int[]) pixel;
return ipixel[0];
}
default:
throw new UnsupportedOperationException("This method has not been "
+ "implemented for transferType " + transferType);
}
}
Returns a pixel value represented as an int in this
ColorModel , given an array of normalized color/alpha
components. This method will throw an
IllegalArgumentException if pixel values for this
ColorModel are not conveniently representable as a
single int . An
ArrayIndexOutOfBoundsException is thrown if the
normComponents array is not large enough to hold all the
color and alpha components (starting at normOffset ). |
public Object getDataElements(int rgb,
Object pixel) {
// REMIND: Use rendering hints?
int red, grn, blu, alp;
red = (rgb > >16) & 0xff;
grn = (rgb > >8) & 0xff;
blu = rgb & 0xff;
if (needScaleInit) {
initScale();
}
if (signed) {
// Handle SHORT, FLOAT, & DOUBLE here
switch(transferType) {
case DataBuffer.TYPE_SHORT:
{
short sdata[];
if (pixel == null) {
sdata = new short[numComponents];
} else {
sdata = (short[])pixel;
}
float factor;
if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
factor = 32767.0f / 255.0f;
if (is_LinearRGB_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
factor = 32767.0f / 65535.0f;
}
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
sdata[3] =
(short) (alp * (32767.0f / 255.0f) + 0.5f);
if (isAlphaPremultiplied) {
factor = alp * factor * (1.0f / 255.0f);
}
}
sdata[0] = (short) (red * factor + 0.5f);
sdata[1] = (short) (grn * factor + 0.5f);
sdata[2] = (short) (blu * factor + 0.5f);
} else if (is_LinearGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
float gray = ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu)) / 65535.0f;
factor = 32767.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
sdata[1] =
(short) (alp * (32767.0f / 255.0f) + 0.5f);
if (isAlphaPremultiplied) {
factor = alp * factor * (1.0f / 255.0f);
}
}
sdata[0] = (short) (gray * factor + 0.5f);
} else if (is_ICCGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
int gray = (int) ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu) + 0.5f);
gray = fromLinearGray16ToOtherGray16LUT[gray] & 0xffff;
factor = 32767.0f / 65535.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
sdata[1] =
(short) (alp * (32767.0f / 255.0f) + 0.5f);
if (isAlphaPremultiplied) {
factor = alp * factor * (1.0f / 255.0f);
}
}
sdata[0] = (short) (gray * factor + 0.5f);
} else {
factor = 1.0f / 255.0f;
float norm[] = new float[3];
norm[0] = red * factor;
norm[1] = grn * factor;
norm[2] = blu * factor;
norm = colorSpace.fromRGB(norm);
if (nonStdScale) {
for (int i = 0; i < numColorComponents; i++) {
norm[i] = (norm[i] - compOffset[i]) *
compScale[i];
// REMIND: need to analyze whether this
// clamping is necessary
if (norm[i] < 0.0f) {
norm[i] = 0.0f;
}
if (norm[i] > 1.0f) {
norm[i] = 1.0f;
}
}
}
factor = 32767.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
sdata[numColorComponents] =
(short) (alp * (32767.0f / 255.0f) + 0.5f);
if (isAlphaPremultiplied) {
factor *= alp * (1.0f / 255.0f);
}
}
for (int i = 0; i < numColorComponents; i++) {
sdata[i] = (short) (norm[i] * factor + 0.5f);
}
}
return sdata;
}
case DataBuffer.TYPE_FLOAT:
{
float fdata[];
if (pixel == null) {
fdata = new float[numComponents];
} else {
fdata = (float[])pixel;
}
float factor;
if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
if (is_LinearRGB_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
factor = 1.0f / 65535.0f;
} else {
factor = 1.0f / 255.0f;
}
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
fdata[3] = alp * (1.0f / 255.0f);
if (isAlphaPremultiplied) {
factor *= fdata[3];
}
}
fdata[0] = red * factor;
fdata[1] = grn * factor;
fdata[2] = blu * factor;
} else if (is_LinearGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
fdata[0] = ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu)) / 65535.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
fdata[1] = alp * (1.0f / 255.0f);
if (isAlphaPremultiplied) {
fdata[0] *= fdata[1];
}
}
} else if (is_ICCGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
int gray = (int) ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu) + 0.5f);
fdata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
0xffff) / 65535.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
fdata[1] = alp * (1.0f / 255.0f);
if (isAlphaPremultiplied) {
fdata[0] *= fdata[1];
}
}
} else {
float norm[] = new float[3];
factor = 1.0f / 255.0f;
norm[0] = red * factor;
norm[1] = grn * factor;
norm[2] = blu * factor;
norm = colorSpace.fromRGB(norm);
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
fdata[numColorComponents] = alp * factor;
if (isAlphaPremultiplied) {
factor *= alp;
for (int i = 0; i < numColorComponents; i++) {
norm[i] *= factor;
}
}
}
for (int i = 0; i < numColorComponents; i++) {
fdata[i] = norm[i];
}
}
return fdata;
}
case DataBuffer.TYPE_DOUBLE:
{
double ddata[];
if (pixel == null) {
ddata = new double[numComponents];
} else {
ddata = (double[])pixel;
}
if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
double factor;
if (is_LinearRGB_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
factor = 1.0 / 65535.0;
} else {
factor = 1.0 / 255.0;
}
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
ddata[3] = alp * (1.0 / 255.0);
if (isAlphaPremultiplied) {
factor *= ddata[3];
}
}
ddata[0] = red * factor;
ddata[1] = grn * factor;
ddata[2] = blu * factor;
} else if (is_LinearGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
ddata[0] = ((0.2125 * red) +
(0.7154 * grn) +
(0.0721 * blu)) / 65535.0;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
ddata[1] = alp * (1.0 / 255.0);
if (isAlphaPremultiplied) {
ddata[0] *= ddata[1];
}
}
} else if (is_ICCGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
int gray = (int) ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu) + 0.5f);
ddata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
0xffff) / 65535.0;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
ddata[1] = alp * (1.0 / 255.0);
if (isAlphaPremultiplied) {
ddata[0] *= ddata[1];
}
}
} else {
float factor = 1.0f / 255.0f;
float norm[] = new float[3];
norm[0] = red * factor;
norm[1] = grn * factor;
norm[2] = blu * factor;
norm = colorSpace.fromRGB(norm);
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
ddata[numColorComponents] = alp * (1.0 / 255.0);
if (isAlphaPremultiplied) {
factor *= alp;
for (int i = 0; i < numColorComponents; i++) {
norm[i] *= factor;
}
}
}
for (int i = 0; i < numColorComponents; i++) {
ddata[i] = norm[i];
}
}
return ddata;
}
}
}
// Handle BYTE, USHORT, & INT here
//REMIND: maybe more efficient not to use int array for
//DataBuffer.TYPE_USHORT and DataBuffer.TYPE_INT
int intpixel[];
if (transferType == DataBuffer.TYPE_INT &&
pixel != null) {
intpixel = (int[])pixel;
} else {
intpixel = new int[numComponents];
}
if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
int precision;
float factor;
if (is_LinearRGB_stdScale) {
if (transferType == DataBuffer.TYPE_BYTE) {
red = fromsRGB8LUT8[red] & 0xff;
grn = fromsRGB8LUT8[grn] & 0xff;
blu = fromsRGB8LUT8[blu] & 0xff;
precision = 8;
factor = 1.0f / 255.0f;
} else {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
precision = 16;
factor = 1.0f / 65535.0f;
}
} else {
precision = 8;
factor = 1.0f / 255.0f;
}
if (supportsAlpha) {
alp = (rgb > >24)&0xff;
if (nBits[3] == 8) {
intpixel[3] = alp;
}
else {
intpixel[3] = (int)
(alp * (1.0f / 255.0f) * ((1< < nBits[3]) - 1) + 0.5f);
}
if (isAlphaPremultiplied) {
factor *= (alp * (1.0f / 255.0f));
precision = -1; // force component calculations below
}
}
if (nBits[0] == precision) {
intpixel[0] = red;
}
else {
intpixel[0] = (int) (red * factor * ((1< < nBits[0]) - 1) + 0.5f);
}
if (nBits[1] == precision) {
intpixel[1] = (int)(grn);
}
else {
intpixel[1] = (int) (grn * factor * ((1< < nBits[1]) - 1) + 0.5f);
}
if (nBits[2] == precision) {
intpixel[2] = (int)(blu);
}
else {
intpixel[2] = (int) (blu * factor * ((1< < nBits[2]) - 1) + 0.5f);
}
} else if (is_LinearGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
float gray = ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu)) / 65535.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
if (nBits[1] == 8) {
intpixel[1] = alp;
} else {
intpixel[1] = (int) (alp * (1.0f / 255.0f) *
((1 < < nBits[1]) - 1) + 0.5f);
}
if (isAlphaPremultiplied) {
gray *= (alp * (1.0f / 255.0f));
}
}
intpixel[0] = (int) (gray * ((1 < < nBits[0]) - 1) + 0.5f);
} else if (is_ICCGray_stdScale) {
red = fromsRGB8LUT16[red] & 0xffff;
grn = fromsRGB8LUT16[grn] & 0xffff;
blu = fromsRGB8LUT16[blu] & 0xffff;
int gray16 = (int) ((0.2125f * red) +
(0.7154f * grn) +
(0.0721f * blu) + 0.5f);
float gray = (fromLinearGray16ToOtherGray16LUT[gray16] &
0xffff) / 65535.0f;
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
if (nBits[1] == 8) {
intpixel[1] = alp;
} else {
intpixel[1] = (int) (alp * (1.0f / 255.0f) *
((1 < < nBits[1]) - 1) + 0.5f);
}
if (isAlphaPremultiplied) {
gray *= (alp * (1.0f / 255.0f));
}
}
intpixel[0] = (int) (gray * ((1 < < nBits[0]) - 1) + 0.5f);
} else {
// Need to convert the color
float[] norm = new float[3];
float factor = 1.0f / 255.0f;
norm[0] = red * factor;
norm[1] = grn * factor;
norm[2] = blu * factor;
norm = colorSpace.fromRGB(norm);
if (nonStdScale) {
for (int i = 0; i < numColorComponents; i++) {
norm[i] = (norm[i] - compOffset[i]) *
compScale[i];
// REMIND: need to analyze whether this
// clamping is necessary
if (norm[i] < 0.0f) {
norm[i] = 0.0f;
}
if (norm[i] > 1.0f) {
norm[i] = 1.0f;
}
}
}
if (supportsAlpha) {
alp = (rgb > >24) & 0xff;
if (nBits[numColorComponents] == 8) {
intpixel[numColorComponents] = alp;
}
else {
intpixel[numColorComponents] =
(int) (alp * factor *
((1< < nBits[numColorComponents]) - 1) + 0.5f);
}
if (isAlphaPremultiplied) {
factor *= alp;
for (int i = 0; i < numColorComponents; i++) {
norm[i] *= factor;
}
}
}
for (int i = 0; i < numColorComponents; i++) {
intpixel[i] = (int) (norm[i] * ((1< < nBits[i]) - 1) + 0.5f);
}
}
switch (transferType) {
case DataBuffer.TYPE_BYTE: {
byte bdata[];
if (pixel == null) {
bdata = new byte[numComponents];
} else {
bdata = (byte[])pixel;
}
for (int i = 0; i < numComponents; i++) {
bdata[i] = (byte)(0xff&intpixel[i]);
}
return bdata;
}
case DataBuffer.TYPE_USHORT:{
short sdata[];
if (pixel == null) {
sdata = new short[numComponents];
} else {
sdata = (short[])pixel;
}
for (int i = 0; i < numComponents; i++) {
sdata[i] = (short)(intpixel[i]&0xffff);
}
return sdata;
}
case DataBuffer.TYPE_INT:
if (maxBits > 23) {
// fix 4412670 - for components of 24 or more bits
// some calculations done above with float precision
// may lose enough precision that the integer result
// overflows nBits, so we need to clamp.
for (int i = 0; i < numComponents; i++) {
if (intpixel[i] > ((1< < nBits[i]) - 1)) {
intpixel[i] = (1< < nBits[i]) - 1;
}
}
}
return intpixel;
}
throw new IllegalArgumentException("This method has not been "+
"implemented for transferType " + transferType);
}
Returns a data element array representation of a pixel in this
ColorModel , given an integer pixel representation
in the default RGB color model.
This array can then be passed to the setDataElements
method of a WritableRaster object. If the
pixel
parameter is null, a new array is allocated. Since
ComponentColorModel can be subclassed, subclasses
inherit the implementation of this method and if they don't
override it then
they throw an exception if they use an unsupported
transferType . |
public Object getDataElements(int[] components,
int offset,
Object obj) {
if (needScaleInit) {
initScale();
}
if (noUnnorm) {
throw new
IllegalArgumentException(
"This ColorModel does not support the unnormalized form");
}
if ((components.length-offset) < numComponents) {
throw new IllegalArgumentException("Component array too small"+
" (should be "+numComponents);
}
switch(transferType) {
case DataBuffer.TYPE_INT:
{
int[] pixel;
if (obj == null) {
pixel = new int[numComponents];
}
else {
pixel = (int[]) obj;
}
System.arraycopy(components, offset, pixel, 0,
numComponents);
return pixel;
}
case DataBuffer.TYPE_BYTE:
{
byte[] pixel;
if (obj == null) {
pixel = new byte[numComponents];
}
else {
pixel = (byte[]) obj;
}
for (int i=0; i < numComponents; i++) {
pixel[i] = (byte) (components[offset+i]&0xff);
}
return pixel;
}
case DataBuffer.TYPE_USHORT:
{
short[] pixel;
if (obj == null) {
pixel = new short[numComponents];
}
else {
pixel = (short[]) obj;
}
for (int i=0; i < numComponents; i++) {
pixel[i] = (short) (components[offset+i]&0xffff);
}
return pixel;
}
default:
throw new UnsupportedOperationException("This method has not been "+
"implemented for transferType " +
transferType);
}
}
Returns a data element array representation of a pixel in this
ColorModel , given an array of unnormalized color/alpha
components. This array can then be passed to the setDataElements
method of a WritableRaster object. |
public Object getDataElements(float[] normComponents,
int normOffset,
Object obj) {
boolean needAlpha = supportsAlpha && isAlphaPremultiplied;
float[] stdNormComponents;
if (needScaleInit) {
initScale();
}
if (nonStdScale) {
stdNormComponents = new float[numComponents];
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
stdNormComponents[c] = (normComponents[nc] - compOffset[c]) *
compScale[c];
// REMIND: need to analyze whether this
// clamping is necessary
if (stdNormComponents[c] < 0.0f) {
stdNormComponents[c] = 0.0f;
}
if (stdNormComponents[c] > 1.0f) {
stdNormComponents[c] = 1.0f;
}
}
if (supportsAlpha) {
stdNormComponents[numColorComponents] =
normComponents[numColorComponents + normOffset];
}
normOffset = 0;
} else {
stdNormComponents = normComponents;
}
switch (transferType) {
case DataBuffer.TYPE_BYTE:
byte[] bpixel;
if (obj == null) {
bpixel = new byte[numComponents];
} else {
bpixel = (byte[]) obj;
}
if (needAlpha) {
float alpha =
stdNormComponents[numColorComponents + normOffset];
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
bpixel[c] = (byte) ((stdNormComponents[nc] * alpha) *
((float) ((1 < < nBits[c]) - 1)) + 0.5f);
}
bpixel[numColorComponents] =
(byte) (alpha *
((float) ((1 < < nBits[numColorComponents]) - 1)) +
0.5f);
} else {
for (int c = 0, nc = normOffset; c < numComponents;
c++, nc++) {
bpixel[c] = (byte) (stdNormComponents[nc] *
((float) ((1 < < nBits[c]) - 1)) + 0.5f);
}
}
return bpixel;
case DataBuffer.TYPE_USHORT:
short[] uspixel;
if (obj == null) {
uspixel = new short[numComponents];
} else {
uspixel = (short[]) obj;
}
if (needAlpha) {
float alpha =
stdNormComponents[numColorComponents + normOffset];
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
uspixel[c] = (short) ((stdNormComponents[nc] * alpha) *
((float) ((1 < < nBits[c]) - 1)) +
0.5f);
}
uspixel[numColorComponents] =
(short) (alpha *
((float) ((1 < < nBits[numColorComponents]) - 1)) +
0.5f);
} else {
for (int c = 0, nc = normOffset; c < numComponents;
c++, nc++) {
uspixel[c] = (short) (stdNormComponents[nc] *
((float) ((1 < < nBits[c]) - 1)) +
0.5f);
}
}
return uspixel;
case DataBuffer.TYPE_INT:
int[] ipixel;
if (obj == null) {
ipixel = new int[numComponents];
} else {
ipixel = (int[]) obj;
}
if (needAlpha) {
float alpha =
stdNormComponents[numColorComponents + normOffset];
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
ipixel[c] = (int) ((stdNormComponents[nc] * alpha) *
((float) ((1 < < nBits[c]) - 1)) + 0.5f);
}
ipixel[numColorComponents] =
(int) (alpha *
((float) ((1 < < nBits[numColorComponents]) - 1)) +
0.5f);
} else {
for (int c = 0, nc = normOffset; c < numComponents;
c++, nc++) {
ipixel[c] = (int) (stdNormComponents[nc] *
((float) ((1 < < nBits[c]) - 1)) + 0.5f);
}
}
return ipixel;
case DataBuffer.TYPE_SHORT:
short[] spixel;
if (obj == null) {
spixel = new short[numComponents];
} else {
spixel = (short[]) obj;
}
if (needAlpha) {
float alpha =
stdNormComponents[numColorComponents + normOffset];
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
spixel[c] = (short)
(stdNormComponents[nc] * alpha * 32767.0f + 0.5f);
}
spixel[numColorComponents] = (short) (alpha * 32767.0f + 0.5f);
} else {
for (int c = 0, nc = normOffset; c < numComponents;
c++, nc++) {
spixel[c] = (short)
(stdNormComponents[nc] * 32767.0f + 0.5f);
}
}
return spixel;
case DataBuffer.TYPE_FLOAT:
float[] fpixel;
if (obj == null) {
fpixel = new float[numComponents];
} else {
fpixel = (float[]) obj;
}
if (needAlpha) {
float alpha = normComponents[numColorComponents + normOffset];
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
fpixel[c] = normComponents[nc] * alpha;
}
fpixel[numColorComponents] = alpha;
} else {
for (int c = 0, nc = normOffset; c < numComponents;
c++, nc++) {
fpixel[c] = normComponents[nc];
}
}
return fpixel;
case DataBuffer.TYPE_DOUBLE:
double[] dpixel;
if (obj == null) {
dpixel = new double[numComponents];
} else {
dpixel = (double[]) obj;
}
if (needAlpha) {
double alpha =
(double) (normComponents[numColorComponents + normOffset]);
for (int c = 0, nc = normOffset; c < numColorComponents;
c++, nc++) {
dpixel[c] = normComponents[nc] * alpha;
}
dpixel[numColorComponents] = alpha;
} else {
for (int c = 0, nc = normOffset; c < numComponents;
c++, nc++) {
dpixel[c] = (double) normComponents[nc];
}
}
return dpixel;
default:
throw new UnsupportedOperationException("This method has not been "+
"implemented for transferType " +
transferType);
}
}
Returns a data element array representation of a pixel in this
ColorModel , given an array of normalized color/alpha
components. This array can then be passed to the
setDataElements method of a WritableRaster
object. An ArrayIndexOutOfBoundsException is thrown
if the normComponents array is not large enough to hold
all the color and alpha components (starting at
normOffset ). If the obj variable is
null , a new array will be allocated. If
obj is not null , it must be a primitive
array of type transferType; otherwise, a
ClassCastException is thrown. An
ArrayIndexOutOfBoundsException is thrown if
obj is not large enough to hold a pixel value for this
ColorModel . |
public int getGreen(int pixel) {
return getRGBComponent(pixel, 1);
}
Returns the green color component for the specified pixel, scaled
from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
is done if necessary. The pixel value is specified as an int.
The returned value will be a non
pre-multiplied value. If the alpha is premultiplied, this method
divides it out before returning the value (if the alpha value is 0,
the green value will be 0). |
public int getGreen(Object inData) {
return getRGBComponent(inData, 1);
}
Returns the green color component for the specified pixel, scaled
from 0 to 255 in the default RGB ColorSpace , sRGB.
A color conversion is done if necessary. The pixel value
is specified by an array of data elements of type transferType
passed in as an object reference. The returned value is a non pre-multiplied
value. If the alpha is premultiplied, this method divides it out before
returning the value (if the alpha value is 0, the green value will be 0).
Since ComponentColorModel can be subclassed,
subclasses inherit the implementation of this method and if they
don't override it then they throw an exception if they use an
unsupported transferType . |
public float[] getNormalizedComponents(Object pixel,
float[] normComponents,
int normOffset) {
if (normComponents == null) {
normComponents = new float[numComponents+normOffset];
}
switch (transferType) {
case DataBuffer.TYPE_BYTE:
byte[] bpixel = (byte[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = ((float) (bpixel[c] & 0xff)) /
((float) ((1 < < nBits[c]) - 1));
}
break;
case DataBuffer.TYPE_USHORT:
short[] uspixel = (short[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = ((float) (uspixel[c] & 0xffff)) /
((float) ((1 < < nBits[c]) - 1));
}
break;
case DataBuffer.TYPE_INT:
int[] ipixel = (int[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = ((float) ipixel[c]) /
((float) ((1 < < nBits[c]) - 1));
}
break;
case DataBuffer.TYPE_SHORT:
short[] spixel = (short[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = ((float) spixel[c]) / 32767.0f;
}
break;
case DataBuffer.TYPE_FLOAT:
float[] fpixel = (float[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = fpixel[c];
}
break;
case DataBuffer.TYPE_DOUBLE:
double[] dpixel = (double[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = (float) dpixel[c];
}
break;
default:
throw new UnsupportedOperationException("This method has not been "+
"implemented for transferType " +
transferType);
}
if (supportsAlpha && isAlphaPremultiplied) {
float alpha = normComponents[numColorComponents + normOffset];
if (alpha != 0.0f) {
float invAlpha = 1.0f / alpha;
for (int c = normOffset; c < numColorComponents + normOffset;
c++) {
normComponents[c] *= invAlpha;
}
}
}
if (min != null) {
// Normally (i.e. when this class is not subclassed to override
// this method), the test (min != null) will be equivalent to
// the test (nonStdScale). However, there is an unlikely, but
// possible case, in which this method is overridden, nonStdScale
// is set true by initScale(), the subclass method for some
// reason calls this superclass method, but the min and
// diffMinMax arrays were never initialized by setupLUTs(). In
// that case, the right thing to do is follow the intended
// semantics of this method, and rescale the color components
// only if the ColorSpace min/max were detected to be other
// than 0.0/1.0 by setupLUTs(). Note that this implies the
// transferType is byte, ushort, int, or short - i.e. components
// derived from float and double pixel data are never rescaled.
for (int c = 0; c < numColorComponents; c++) {
normComponents[c + normOffset] = min[c] +
diffMinMax[c] * normComponents[c + normOffset];
}
}
return normComponents;
}
Returns an array of all of the color/alpha components in normalized
form, given a pixel in this ColorModel . The pixel
value is specified by an array of data elements of type transferType
passed in as an object reference. If pixel is not a primitive array
of type transferType, a ClassCastException is thrown.
An ArrayIndexOutOfBoundsException is thrown if
pixel is not large enough to hold a pixel value for this
ColorModel .
Normalized components are float values between a per component minimum
and maximum specified by the ColorSpace object for this
ColorModel . If the
normComponents array is null , a new array
will be allocated. The normComponents array
will be returned. Color/alpha components are stored in the
normComponents array starting at
normOffset (even if the array is allocated by this
method). An ArrayIndexOutOfBoundsException is thrown
if the normComponents array is not null
and is not large enough to hold all the color and alpha components
(starting at normOffset ).
This method must be overrridden by a subclass if that subclass
is designed to translate pixel sample values to color component values
in a non-default way. The default translations implemented by this
class is described in the class comments. Any subclass implementing
a non-default translation must follow the constraints on allowable
translations defined there. |
public float[] getNormalizedComponents(int[] components,
int offset,
float[] normComponents,
int normOffset) {
if (needScaleInit) {
initScale();
}
if (noUnnorm) {
throw new
IllegalArgumentException(
"This ColorModel does not support the unnormalized form");
}
return super.getNormalizedComponents(components, offset,
normComponents, normOffset);
}
Returns an array of all of the color/alpha components in normalized
form, given an unnormalized component array. Unnormalized components
are unsigned integral values between 0 and 2n - 1, where
n is the number of bits for a particular component. Normalized
components are float values between a per component minimum and
maximum specified by the ColorSpace object for this
ColorModel . An IllegalArgumentException
will be thrown if color component values for this
ColorModel are not conveniently representable in the
unnormalized form. If the
normComponents array is null , a new array
will be allocated. The normComponents array
will be returned. Color/alpha components are stored in the
normComponents array starting at
normOffset (even if the array is allocated by this
method). An ArrayIndexOutOfBoundsException is thrown
if the normComponents array is not null
and is not large enough to hold all the color and alpha components
(starting at normOffset ). An
IllegalArgumentException is thrown if the
components array is not large enough to hold all the
color and alpha components starting at offset . |
public int getRGB(int pixel) {
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (signed) {
throw new
IllegalArgumentException("Component value is signed");
}
return (getAlpha(pixel) < < 24)
| (getRed(pixel) < < 16)
| (getGreen(pixel) < < 8)
| (getBlue(pixel) < < 0);
}
Returns the color/alpha components of the pixel in the default
RGB color model format. A color conversion is done if necessary.
The returned value will be in a non pre-multiplied format. If
the alpha is premultiplied, this method divides it out of the
color components (if the alpha value is 0, the color values will be 0). |
public int getRGB(Object inData) {
if (needScaleInit) {
initScale();
}
if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
return (getAlpha(inData) < < 24)
| (getRed(inData) < < 16)
| (getGreen(inData) < < 8)
| (getBlue(inData));
} else if (colorSpaceType == ColorSpace.TYPE_GRAY) {
int gray = getRed(inData); // Red sRGB component should equal
// green and blue components
return (getAlpha(inData) < < 24)
| (gray < < 16)
| (gray < < 8)
| gray;
}
float[] norm = getNormalizedComponents(inData, null, 0);
// Note that getNormalizedComponents returns non-premult values
float[] rgb = colorSpace.toRGB(norm);
return (getAlpha(inData) < < 24)
| (((int) (rgb[0] * 255.0f + 0.5f)) < < 16)
| (((int) (rgb[1] * 255.0f + 0.5f)) < < 8)
| (((int) (rgb[2] * 255.0f + 0.5f)) < < 0);
}
Returns the color/alpha components for the specified pixel in the
default RGB color model format. A color conversion is done if
necessary. The pixel value is specified by an
array of data elements of type transferType passed
in as an object reference.
The returned value is in a non pre-multiplied format. If
the alpha is premultiplied, this method divides it out of the
color components (if the alpha value is 0, the color values will be 0).
Since ComponentColorModel can be subclassed,
subclasses inherit the implementation of this method and if they
don't override it then they throw an exception if they use an
unsupported transferType . |
public int getRed(int pixel) {
return getRGBComponent(pixel, 0);
}
Returns the red color component for the specified pixel, scaled
from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
is done if necessary. The pixel value is specified as an int.
The returned value will be a non pre-multiplied value.
If the alpha is premultiplied, this method divides
it out before returning the value (if the alpha value is 0,
the red value will be 0). |
public int getRed(Object inData) {
return getRGBComponent(inData, 0);
}
Returns the red color component for the specified pixel, scaled
from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
is done if necessary. The pixel value is specified by an array
of data elements of type transferType passed in as an object
reference. The returned value will be a non pre-multiplied value. If the
alpha is premultiplied, this method divides it out before returning
the value (if the alpha value is 0, the red value will be 0). Since
ComponentColorModel can be subclassed, subclasses
inherit the implementation of this method and if they don't override
it then they throw an exception if they use an unsupported
transferType . |
public int[] getUnnormalizedComponents(float[] normComponents,
int normOffset,
int[] components,
int offset) {
if (needScaleInit) {
initScale();
}
if (noUnnorm) {
throw new
IllegalArgumentException(
"This ColorModel does not support the unnormalized form");
}
return super.getUnnormalizedComponents(normComponents, normOffset,
components, offset);
}
Returns an array of all of the color/alpha components in unnormalized
form, given a normalized component array. Unnormalized components
are unsigned integral values between 0 and 2n - 1, where
n is the number of bits for a particular component. Normalized
components are float values between a per component minimum and
maximum specified by the ColorSpace object for this
ColorModel . An IllegalArgumentException
will be thrown if color component values for this
ColorModel are not conveniently representable in the
unnormalized form. If the
components array is null , a new array
will be allocated. The components array will
be returned. Color/alpha components are stored in the
components array starting at offset (even
if the array is allocated by this method). An
ArrayIndexOutOfBoundsException is thrown if the
components array is not null and is not
large enough to hold all the color and alpha
components (starting at offset ). An
IllegalArgumentException is thrown if the
normComponents array is not large enough to hold
all the color and alpha components starting at
normOffset . |
public boolean isCompatibleRaster(Raster raster) {
SampleModel sm = raster.getSampleModel();
if (sm instanceof ComponentSampleModel) {
if (sm.getNumBands() != getNumComponents()) {
return false;
}
for (int i=0; i< nBits.length; i++) {
if (sm.getSampleSize(i) < nBits[i]) {
return false;
}
}
return (raster.getTransferType() == transferType);
}
else {
return false;
}
}
Returns true if raster is compatible with this
ColorModel ; false if it is not. |
public boolean isCompatibleSampleModel(SampleModel sm) {
if (!(sm instanceof ComponentSampleModel)) {
return false;
}
// Must have the same number of components
if (numComponents != sm.getNumBands()) {
return false;
}
if (sm.getTransferType() != transferType) {
return false;
}
return true;
}
Checks whether or not the specified SampleModel
is compatible with this ColorModel . |