colormodel.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 577 行 · 第 1/2 页
JAVA
577 行
*
* <p>This method performs the inverse function of
* <code>getDataElements(int rgb, Object pixel)</code>.
* I.e. <code>(rgb == cm.getRGB(cm.getDataElements(rgb,
* null)))</code>.
*
* @param inData array of transferType containing a single pixel. The
* pixel should be encoded in the natural way of the color model.
*
* @return a pixel in sRGB color space, encoded in default
* 0xAARRGGBB format.
*
* @see #getDataElements(int, Object)
*/
public int getRGB(Object inData) {
return ((getAlpha(inData) & 0xff) << 24) | ((getRed(inData) & 0xff) << 16) | ((getGreen(inData) & 0xff) << 8) | ((getBlue(inData) & 0xff) << 0);
}
/**
* Converts an sRGB pixel int value to an array containing a
* single pixel of the color space of the color model.
*
* <p>This method performs the inverse function of
* <code>getRGB(Object inData)</code>.
*
* Outline of conversion process:
*
* <ol>
*
* <li>Convert rgb to normalized [0.0, 1.0] sRGB values.</li>
*
* <li>Convert to color space components using fromRGB in
* ColorSpace.</li>
*
* <li>If color model has alpha and should be premultiplied,
* multiply color space components with alpha value</li>
*
* <li>Scale the components to the correct number of bits.</li>
*
* <li>Arrange the components in the output array</li>
*
* </ol>
*
* @param rgb The color to be converted to dataElements. A pixel
* in sRGB color space, encoded in default 0xAARRGGBB format,
* assumed not alpha premultiplied.
*
* @param pixel to avoid needless creation of arrays, an array to
* use to return the pixel can be given. If null, a suitable array
* will be created.
*
* @return An array of transferType values representing the color,
* in the color model format. The color model defines whether the
*
* @see #getRGB(Object)
*/
public Object getDataElements(int rgb, Object pixel) {
// FIXME: implement
throw new UnsupportedOperationException();
}
/**
* Fills an array with the unnormalized component samples from a
* pixel value. I.e. decompose the pixel, but not perform any
* color conversion.
*
* This method is typically overriden in subclasses to provide a
* more efficient implementation.
*
* @param pixel pixel value encoded according to the color model.
*
* @return arrays of unnormalized component samples of single
* pixel. The scale and multiplication state of the samples are
* according to the color model. Each component sample is stored
* as a separate element in the array.
*/
public int[] getComponents(int pixel, int[] components, int offset) {
// FIXME: implement
throw new UnsupportedOperationException();
}
/**
* Fills an array with the unnormalized component samples from an
* array of transferType containing a single pixel. I.e. decompose
* the pixel, but not perform any color conversion.
*
* This method is typically overriden in subclasses to provide a
* more efficient implementation.
*
* @param array of transferType containing a single pixel. The
* pixel should be encoded in the natural way of the color model.
*
* @return arrays of unnormalized component samples of single
* pixel. The scale and multiplication state of the samples are
* according to the color model. Each component sample is stored
* as a separate element in the array.
*/
public int[] getComponents(Object pixel, int[] components, int offset) {
throw new UnsupportedOperationException();
}
/**
* Convert normalized components to unnormalized components.
*/
public int[] getUnnormalizedComponents(float[] normComponents, int normOffset, int[] components, int offset) {
int numComponents = getNumComponents();
if (components == null) {
components = new int[offset + numComponents];
}
for (int i = 0; i < numComponents; i++) {
float in = normComponents[normOffset++];
int out = (int) (in * ((1 << getComponentSize(i)) - 1));
components[offset++] = out;
}
return components;
}
/**
* Convert unnormalized components to normalized components.
*/
public float[] getNormalizedComponents(int[] components, int offset, float[] normComponents, int normOffset) {
int numComponents = getNumComponents();
if (normComponents == null) {
normComponents = new float[normOffset + numComponents];
}
for (int i = 0; i < numComponents; i++) {
float in = components[offset++];
float out = in / ((1 << getComponentSize(i)) - 1);
normComponents[normOffset++] = out;
}
return normComponents;
}
/**
* Converts the unnormalized component samples from an array to a
* pixel value. I.e. composes the pixel from component samples, but
* does not perform any color conversion or scaling of the samples.
*
* This method performs the inverse function of
* <code>getComponents(int pixel, int[] components,
* int offset)</code>. I.e.
*
* <code>(pixel == cm.getDataElement(cm.getComponents(pixel, null,
* 0), 0))</code>.
*
* This method is typically overriden in subclasses to provide a
* more efficient implementation.
*
* @param arrays of unnormalized component samples of single
* pixel. The scale and multiplication state of the samples are
* according to the color model. Each component sample is stored
* as a separate element in the array.
*
* @return pixel value encoded according to the color model.
*/
public int getDataElement(int[] components, int offset) {
throw new UnsupportedOperationException();
}
public Object getDataElements(int[] components, int offset, Object obj) {
throw new UnsupportedOperationException();
}
public boolean equals(Object obj) {
if (!(obj instanceof ColorModel))
return false;
ColorModel o = (ColorModel) obj;
return (pixel_bits == o.pixel_bits)
&& (transferType == o.transferType)
&& (transparency == o.transparency)
&& (hasAlpha == o.hasAlpha)
&& (isAlphaPremultiplied == isAlphaPremultiplied)
&& (bits.equals(o.bits))
&& (cspace.equals(o.cspace));
}
public final ColorSpace getColorSpace() {
return cspace;
}
// Typically overridden
public ColorModel coerceData(WritableRaster raster, boolean isAlphaPremultiplied) {
if (this.isAlphaPremultiplied == isAlphaPremultiplied)
return this;
int w = raster.getWidth();
int h = raster.getHeight();
int x = raster.getMinX();
int y = raster.getMinY();
int size = w * h;
int numColors = getNumColorComponents();
int numComponents = getNumComponents();
int alphaScale = (1 << getComponentSize(numColors)) - 1;
double[] pixels = raster.getPixels(x, y, w, h, (double[]) null);
for (int i = 0; i < size; i++) {
double alpha = pixels[i * numComponents + numColors] * alphaScale;
for (int c = 0; c < numColors; c++) {
int offset = i * numComponents + c;
if (isAlphaPremultiplied)
pixels[offset] = pixels[offset] / alpha;
else
pixels[offset] = pixels[offset] * alpha;
}
}
raster.setPixels(0, 0, w, h, pixels);
// FIXME: what can we return?
return null;
}
// Typically overridden
public boolean isCompatibleRaster(Raster raster) {
SampleModel sampleModel = raster.getSampleModel();
return isCompatibleSampleModel(sampleModel);
}
// Typically overridden
public WritableRaster createCompatibleWritableRaster(int w, int h) {
return new WritableRaster(createCompatibleSampleModel(w, h), new Point(0, 0));
}
// Typically overridden
public SampleModel createCompatibleSampleModel(int w, int h) {
throw new UnsupportedOperationException();
}
// Typically overridden
public boolean isCompatibleSampleModel(SampleModel sm) {
return sm.getTransferType() == transferType;
}
public void finalize() {
}
/**
* Subclasses must override this method if it is possible for the
* color model to have an alpha channel.
*
* @return null, as per JDK 1.3 doc. Subclasses will only return
* null if no alpha raster exists.
*/
public WritableRaster getAlphaRaster(WritableRaster raster) {
return null;
/* It is a mystery to me why we couldn't use the following code...
if (!hasAlpha()) return null;
SampleModel sm = raster.getSampleModel();
int[] alphaBand = { sm.getNumBands() - 1 };
SampleModel alphaModel = sm.createSubsetSampleModel(alphaBand);
DataBuffer buffer = raster.getDataBuffer();
Point origin = new Point(0, 0);
return Raster.createWritableRaster(alphaModel, buffer, origin);
...here, and avoided overriding the method in subclasses,
but the Sun docs state that this method always will return
null, and that overriding is required. Oh, well.
*/
}
String stringParam() {
return "pixel_bits="
+ pixel_bits
+ ", cspace="
+ cspace
+ ", transferType="
+ transferType
+ ", transparency="
+ transparency
+ ", hasAlpha="
+ hasAlpha
+ ", isAlphaPremultiplied="
+ isAlphaPremultiplied;
}
public String toString() {
return getClass().getName() + "[" + stringParam() + "]";
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?