下面列出了java.awt.image.PixelInterleavedSampleModel#java.awt.color.ColorSpace 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the (A)RGB equivalent of the given raster, using the given AWT color space
* to perform the conversion.
* @param raster the source raster
* @param colorSpace the AWT
* @return an (A)RGB buffered image
*/
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace)
{
//
// WARNING: this method is performance sensitive, modify with care!
//
// ICC Profile color transforms are only fast when performed using ColorConvertOp
ColorModel colorModel = new ComponentColorModel(colorSpace,
false, false, Transparency.OPAQUE, raster.getDataBuffer().getDataType());
BufferedImage src = new BufferedImage(colorModel, raster, false, null);
BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(),
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dest);
return dest;
}
/**
* Returns a <code>float</code> array containing only the color
* components of the <code>Color</code> in the
* <code>ColorSpace</code> specified by the <code>cspace</code>
* parameter. If <code>compArray</code> is <code>null</code>, an array
* with length equal to the number of components in
* <code>cspace</code> is created for the return value. Otherwise,
* <code>compArray</code> must have at least this length, and it is
* filled in with the components and returned.
* @param cspace a specified <code>ColorSpace</code>
* @param compArray an array that this method fills with the color
* components of this <code>Color</code> in the specified
* <code>ColorSpace</code>
* @return the color components in a <code>float</code> array.
*/
public float[] getColorComponents(ColorSpace cspace, float[] compArray) {
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
float f[];
if (fvalue == null) {
f = new float[3];
f[0] = ((float)getRed())/255f;
f[1] = ((float)getGreen())/255f;
f[2] = ((float)getBlue())/255f;
} else {
f = fvalue;
}
float tmp[] = cs.toCIEXYZ(f);
float tmpout[] = cspace.fromCIEXYZ(tmp);
if (compArray == null) {
return tmpout;
}
for (int i = 0 ; i < tmpout.length ; i++) {
compArray[i] = tmpout[i];
}
return compArray;
}
private static int[] bitsArrayHelper(int[] origBits,
int transferType,
ColorSpace colorSpace,
boolean hasAlpha) {
switch(transferType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
case DataBuffer.TYPE_INT:
if (origBits != null) {
return origBits;
}
break;
default:
break;
}
int numBits = DataBuffer.getDataTypeSize(transferType);
int numComponents = colorSpace.getNumComponents();
if (hasAlpha) {
++numComponents;
}
int[] bits = new int[numComponents];
for (int i = 0; i < numComponents; i++) {
bits[i] = numBits;
}
return bits;
}
JFIFExtensionMarkerSegment(BufferedImage thumbnail)
throws IllegalThumbException {
super(JPEG.APP0);
ColorModel cm = thumbnail.getColorModel();
int csType = cm.getColorSpace().getType();
if (cm.hasAlpha()) {
throw new IllegalThumbException();
}
if (cm instanceof IndexColorModel) {
code = THUMB_PALETTE;
thumb = new JFIFThumbPalette(thumbnail);
} else if (csType == ColorSpace.TYPE_RGB) {
code = THUMB_RGB;
thumb = new JFIFThumbRGB(thumbnail);
} else if (csType == ColorSpace.TYPE_GRAY) {
code = THUMB_JPEG;
thumb = new JFIFThumbJPEG(thumbnail);
} else {
throw new IllegalThumbException();
}
}
@Override
protected Icon getIcon ()
{
// TODO Use that to get the state (-> highlight or not)
TransitionAwareUI transitionAwareUI = (TransitionAwareUI) slider.getUI();
StateTransitionTracker stateTransitionTracker = transitionAwareUI.getTransitionTracker();
// stateTransitionTracker.getModelStateInfo().getCurrModelState();
final Icon icon = super.getIcon();
final BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics iconGraphics = image.createGraphics();
icon.paintIcon(slider, iconGraphics, 0, 0);
// Make it brighter (very simple approach)
final RescaleOp rescaleOp = new RescaleOp(2.0f, 50, null);
rescaleOp.filter(image, image);
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(image, image);
return new ImageIcon(image);
}
private static int[] bitsArrayHelper(int[] origBits,
int transferType,
ColorSpace colorSpace,
boolean hasAlpha) {
switch(transferType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
case DataBuffer.TYPE_INT:
if (origBits != null) {
return origBits;
}
break;
default:
break;
}
int numBits = DataBuffer.getDataTypeSize(transferType);
int numComponents = colorSpace.getNumComponents();
if (hasAlpha) {
++numComponents;
}
int[] bits = new int[numComponents];
for (int i = 0; i < numComponents; i++) {
bits[i] = numBits;
}
return bits;
}
/**
* Returns <code>true</code> if the given <code>ColorSpace</code> object is
* an instance of <code>ICC_ColorSpace</code> but is not one of the standard
* <code>ColorSpace</code>s returned by <code>ColorSpace.getInstance()</code>.
*
* @param cs The <code>ColorSpace</code> to test.
*/
public static boolean isNonStandardICCColorSpace(ColorSpace cs) {
boolean retval = false;
try {
// Check the standard ColorSpaces in decreasing order of
// likelihood except check CS_PYCC last as in some JREs
// PYCC.pf used not to be installed.
retval =
(cs instanceof ICC_ColorSpace) &&
!(cs.isCS_sRGB() ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)) ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)) ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)));
} catch(IllegalArgumentException e) {
// PYCC.pf not installed: ignore it - 'retval' is still 'false'.
}
return retval;
}
@Override
public ColorModel getColorModel(int transparency) {
switch (transparency) {
case Transparency.OPAQUE:
// REMIND: once the ColorModel spec is changed, this should be
// an opaque premultiplied DCM...
return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
case Transparency.BITMASK:
return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
case Transparency.TRANSLUCENT:
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
return new DirectColorModel(cs, 32,
0xff0000, 0xff00, 0xff, 0xff000000,
true, DataBuffer.TYPE_INT);
default:
return null;
}
}
public Context(TestEnvironment env, Result result, ColorSpace cs) {
this.cs = cs;
this.env = env;
this.res = result;
numComponents = cs.getNumComponents();
val = new float[numComponents];
for (int i = 0; i < numComponents; i++) {
float min = cs.getMinValue(i);
float max = cs.getMaxValue(i);
val[i] = 0.5f * (max - min);
}
rgb = new float[]{0.5f, 0.5f, 0.5f};
cie = new float[]{0.5f, 0.5f, 0.5f};
}
/**
* @param buf
* @return DataImage
*/
protected DataImage get18PlaneImage(MemBuffer buf) {
// create the color model
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8 };
int[] bOffs = { 2, 1, 0 };
ColorModel colorModel =
new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
int w = getWidth();
int h = getHeight();
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w * 3, 3, bOffs, null);
// create the image
BufferedImage image =
new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
byte[] dbuf = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
getPixelData(buf, dbuf);
return new BitmapDataImage(image);
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
private BufferedImage create2ByteGrayAlphaImage(int width, int height)
{
/**
* gray + alpha
*/
int[] bandOffsets = new int[] { 1, 0 };
int bands = bandOffsets.length;
/**
* Color Model usesd for raw GRAY + ALPHA
*/
final ColorModel CM_GRAY_ALPHA = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_GRAY), true, false,
Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
// Init data buffer of type byte
DataBuffer buffer = new DataBufferByte(width * height * bands);
// Wrap the data buffer in a raster
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height,
width * bands, bands, bandOffsets, new Point(0, 0));
// Create a custom BufferedImage with the raster and a suitable color model
return new BufferedImage(CM_GRAY_ALPHA, raster, false, null);
}
/**
* Returns <code>true</code> if the given <code>ColorSpace</code>
* object is an instance of ICC_ColorSpace but is not one of the
* standard <code>ColorSpaces</code> returned by
* <code>ColorSpace.getInstance()</code>.
*/
static boolean isNonStandardICC(ColorSpace cs) {
boolean retval = false;
if ((cs instanceof ICC_ColorSpace)
&& (!cs.isCS_sRGB())
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
) {
retval = true;
}
return retval;
}
private int getSrcCSType(ColorModel cm) {
int retval = JPEG.JCS_UNKNOWN;
if (cm != null) {
boolean alpha = cm.hasAlpha();
ColorSpace cs = cm.getColorSpace();
switch (cs.getType()) {
case ColorSpace.TYPE_GRAY:
retval = JPEG.JCS_GRAYSCALE;
break;
case ColorSpace.TYPE_RGB:
if (alpha) {
retval = JPEG.JCS_RGBA;
} else {
retval = JPEG.JCS_RGB;
}
break;
case ColorSpace.TYPE_YCbCr:
if (alpha) {
retval = JPEG.JCS_YCbCrA;
} else {
retval = JPEG.JCS_YCbCr;
}
break;
case ColorSpace.TYPE_3CLR:
if (cs == JPEG.JCS.getYCC()) {
if (alpha) {
retval = JPEG.JCS_YCCA;
} else {
retval = JPEG.JCS_YCC;
}
}
case ColorSpace.TYPE_CMYK:
retval = JPEG.JCS_CMYK;
break;
}
}
return retval;
}
public static ComponentColorModel createABGRCCM() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
int[] bOffs = {3, 2, 1, 0};
return new ComponentColorModel(cs, nBits, true, true,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
}
/**
* Returns the <code>ColorSpace</code> of this <code>Color</code>.
* @return this <code>Color</code> object's <code>ColorSpace</code>.
*/
public ColorSpace getColorSpace() {
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
return cs;
}
/**
* Returns <code>true</code> if the given imageType can be used
* in a JFIF file. If <code>input</code> is true, then the
* image type is considered before colorspace conversion.
*/
static boolean isJFIFcompliant(ImageTypeSpecifier imageType,
boolean input) {
ColorModel cm = imageType.getColorModel();
// Can't have alpha
if (cm.hasAlpha()) {
return false;
}
// Gray is OK, always
int numComponents = imageType.getNumComponents();
if (numComponents == 1) {
return true;
}
// If it isn't gray, it must have 3 channels
if (numComponents != 3) {
return false;
}
if (input) {
// Must be RGB
if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
return true;
}
} else {
// Must be YCbCr
if (cm.getColorSpace().getType() == ColorSpace.TYPE_YCbCr) {
return true;
}
}
return false;
}
/**
* Returns <code>true</code> if the given <code>ColorSpace</code>
* object is an instance of ICC_ColorSpace but is not one of the
* standard <code>ColorSpaces</code> returned by
* <code>ColorSpace.getInstance()</code>.
*/
static boolean isNonStandardICC(ColorSpace cs) {
boolean retval = false;
if ((cs instanceof ICC_ColorSpace)
&& (!cs.isCS_sRGB())
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
) {
retval = true;
}
return retval;
}
private int getDefaultDestCSType(ColorModel cm) {
int retval = JPEG.JCS_UNKNOWN;
if (cm != null) {
boolean alpha = cm.hasAlpha();
ColorSpace cs = cm.getColorSpace();
switch (cs.getType()) {
case ColorSpace.TYPE_GRAY:
retval = JPEG.JCS_GRAYSCALE;
break;
case ColorSpace.TYPE_RGB:
if (alpha) {
retval = JPEG.JCS_YCbCrA;
} else {
retval = JPEG.JCS_YCbCr;
}
break;
case ColorSpace.TYPE_YCbCr:
if (alpha) {
retval = JPEG.JCS_YCbCrA;
} else {
retval = JPEG.JCS_YCbCr;
}
break;
case ColorSpace.TYPE_3CLR:
if (cs == JPEG.JCS.getYCC()) {
if (alpha) {
retval = JPEG.JCS_YCCA;
} else {
retval = JPEG.JCS_YCC;
}
}
case ColorSpace.TYPE_CMYK:
retval = JPEG.JCS_YCCK;
break;
}
}
return retval;
}
private static int bitsHelper(int transferType,
ColorSpace colorSpace,
boolean hasAlpha) {
int numBits = DataBuffer.getDataTypeSize(transferType);
int numComponents = colorSpace.getNumComponents();
if (hasAlpha) {
++numComponents;
}
return numBits * numComponents;
}
public void runTest(Object ctx, int numReps) {
final Context ictx = (Context) ctx;
final ColorSpace cs = ictx.cs;
final float[] val = ictx.val;
do {
try {
cs.toCIEXYZ(val);
} catch (Exception e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
public TiledImageWriter(int width, int height, int imageType, int tileWidth, int tileHeight) {
/*
DiskMemImage.getCommonTileCache().setMemoryCapacity(AparUtils.DISK_IMAGE_CACHE);
DiskMemImage.getCommonTileCache().setAutoFlushMemoryEnabled(AparUtils.DISK_IMAGE_AUTO_FLUSH);
DiskMemImage.getCommonTileCache().setAutoFlushMemoryInterval(AparUtils.DISK_IMAGE_AUTO_FLUSH_TIME);
DiskMemImage.getCommonTileCache().flush();
*/
this.width = width;
this.height = height;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
if (imageType == TiledImageWriter.IMAGE_RGBA) {
colorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8, 8}, true, false,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
} else {
colorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8}, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
}
//BufferedImage imgCol = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
//colorModel = imgCol.getColorModel();
sampleModel = colorModel.createCompatibleSampleModel(tileWidth, tileHeight);
DiskMemImageOrbit dmi = new DiskMemImageOrbit(0, 0, width, height, 0, 0, sampleModel, colorModel);
dmi.setUseCommonCache(true);
setImage(dmi);
// System.out.println("TiledImageWriter Info: ");
// System.out.println(getTileInfo(image));
}
@Test
public void testDumpColorSpace() {
final ImageDump imageDump = new ImageDump();
final ColorSpace colorSpace = ColorSpace.getInstance(1004);
imageDump.dumpColorSpace("Ku&]N>!4'C#Jzn+", colorSpace);
assertEquals(3, colorSpace.getNumComponents());
}
public void runTest(Object ctx, int numReps) {
final Context ictx = (Context) ctx;
final ColorSpace cs = ictx.cs;
final float[] rgb = ictx.rgb;
do {
try {
cs.fromRGB(rgb);
} catch (Exception e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
public static ColorSpace getYCC() {
if (!yccInited) {
try {
YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);
} catch (IllegalArgumentException e) {
// PYCC.pf may not always be installed
} finally {
yccInited = true;
}
}
return YCC;
}
public void runTest(Object ctx, int numReps) {
final Context ictx = (Context) ctx;
final ColorSpace cs = ictx.cs;
final float[] val = ictx.cie;
do {
try {
cs.fromCIEXYZ(val);
} catch (Exception e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
protected static ColorSpace getColorSpace(TestEnvironment env) {
ColorSpace cs;
Boolean usePlatfrom = true; //(Boolean)env.getModifier(usePlatfromProfiles);
int cs_code = env.getIntValue(csList);
if (usePlatfrom) {
cs = ColorSpace.getInstance(cs_code);
} else {
String resource = "profiles/";
switch (cs_code) {
case ColorSpace.CS_CIEXYZ:
resource += "CIEXYZ.pf";
break;
case ColorSpace.CS_GRAY:
resource += "GRAY.pf";
break;
case ColorSpace.CS_LINEAR_RGB:
resource += "LINEAR_RGB.pf";
break;
case ColorSpace.CS_PYCC:
resource += "PYCC.pf";
break;
case ColorSpace.CS_sRGB:
resource += "sRGB.pf";
break;
default:
throw new RuntimeException("Unknown color space: " + cs_code);
}
try {
InputStream is = CMMTests.class.getResourceAsStream(resource);
ICC_Profile p = ICC_Profile.getInstance(is);
cs = new ICC_ColorSpace(p);
} catch (IOException e) {
throw new RuntimeException("Unable load profile from resource " + resource, e);
}
}
return cs;
}
static String getCSName(int cs) {
switch(cs) {
case ColorSpace.CS_GRAY:
return "CS_GRAY";
case ColorSpace.CS_CIEXYZ:
return "CS_CIEXYZ";
case ColorSpace.CS_LINEAR_RGB:
return "CS_LINEAR_RGB";
case ColorSpace.CS_PYCC:
return "CS_PYCC";
case ColorSpace.CS_sRGB:
return "CS_sRGB";
}
return "UNKNOWN";
}
/**
* Returns a <code>float</code> array containing the color and alpha
* components of the <code>Color</code>, in the
* <code>ColorSpace</code> specified by the <code>cspace</code>
* parameter. If <code>compArray</code> is <code>null</code>, an
* array with length equal to the number of components in
* <code>cspace</code> plus one is created for the return value.
* Otherwise, <code>compArray</code> must have at least this
* length, and it is filled in with the components and returned.
* @param cspace a specified <code>ColorSpace</code>
* @param compArray an array that this method fills with the
* color and alpha components of this <code>Color</code> in
* the specified <code>ColorSpace</code> and returns
* @return the color and alpha components in a <code>float</code>
* array.
*/
public float[] getComponents(ColorSpace cspace, float[] compArray) {
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
float f[];
if (fvalue == null) {
f = new float[3];
f[0] = ((float)getRed())/255f;
f[1] = ((float)getGreen())/255f;
f[2] = ((float)getBlue())/255f;
} else {
f = fvalue;
}
float tmp[] = cs.toCIEXYZ(f);
float tmpout[] = cspace.fromCIEXYZ(tmp);
if (compArray == null) {
compArray = new float[tmpout.length + 1];
}
for (int i = 0 ; i < tmpout.length ; i++) {
compArray[i] = tmpout[i];
}
if (fvalue == null) {
compArray[tmpout.length] = ((float)getAlpha())/255f;
} else {
compArray[tmpout.length] = falpha;
}
return compArray;
}
/**
* Creates a color in the specified <code>ColorSpace</code>
* with the color components specified in the <code>float</code>
* array and the specified alpha. The number of components is
* determined by the type of the <code>ColorSpace</code>. For
* example, RGB requires 3 components, but CMYK requires 4
* components.
* @param cspace the <code>ColorSpace</code> to be used to
* interpret the components
* @param components an arbitrary number of color components
* that is compatible with the <code>ColorSpace</code>
* @param alpha alpha value
* @throws IllegalArgumentException if any of the values in the
* <code>components</code> array or <code>alpha</code> is
* outside of the range 0.0 to 1.0
* @see #getComponents
* @see #getColorComponents
*/
public Color(ColorSpace cspace, float components[], float alpha) {
boolean rangeError = false;
String badComponentString = "";
int n = cspace.getNumComponents();
fvalue = new float[n];
for (int i = 0; i < n; i++) {
if (components[i] < 0.0 || components[i] > 1.0) {
rangeError = true;
badComponentString = badComponentString + "Component " + i
+ " ";
} else {
fvalue[i] = components[i];
}
}
if (alpha < 0.0 || alpha > 1.0) {
rangeError = true;
badComponentString = badComponentString + "Alpha";
} else {
falpha = alpha;
}
if (rangeError) {
throw new IllegalArgumentException(
"Color parameter outside of expected range: " +
badComponentString);
}
frgbvalue = cspace.toRGB(fvalue);
cs = cspace;
value = ((((int)(falpha*255)) & 0xFF) << 24) |
((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
((((int)(frgbvalue[1]*255)) & 0xFF) << 8) |
((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
}