use of java.awt.image.ComponentColorModel in project jdk8u_jdk by JetBrains.
the class RenderToCustomBufferTest method createCustomBuffer.
private static BufferedImage createCustomBuffer() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
return new BufferedImage(cm, wr, false, null);
}
use of java.awt.image.ComponentColorModel in project jdk8u_jdk by JetBrains.
the class ImageTypeSpecifier method createComponentCM.
static ColorModel createComponentCM(ColorSpace colorSpace, int numBands, int dataType, boolean hasAlpha, boolean isAlphaPremultiplied) {
int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace, numBits, hasAlpha, isAlphaPremultiplied, transparency, dataType);
}
use of java.awt.image.ComponentColorModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method createColorModel.
/* XXX testing only
public static void main(String[] args) {
ImageTypeSpecifier bilevel =
ImageTypeSpecifier.createIndexed(new byte[] {(byte)0, (byte)255},
new byte[] {(byte)0, (byte)255},
new byte[] {(byte)0, (byte)255},
null, 1,
DataBuffer.TYPE_BYTE);
ImageTypeSpecifier gray =
ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false);
ImageTypeSpecifier grayAlpha =
ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false,
false);
ImageTypeSpecifier rgb =
ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {0, 1, 2},
DataBuffer.TYPE_BYTE,
false,
false);
ImageTypeSpecifier rgba =
ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {0, 1, 2, 3},
DataBuffer.TYPE_BYTE,
true,
false);
ImageTypeSpecifier packed =
ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_sRGB),
0xff000000,
0x00ff0000,
0x0000ff00,
0x000000ff,
DataBuffer.TYPE_BYTE,
false);
SampleModel bandedSM =
new java.awt.image.BandedSampleModel(DataBuffer.TYPE_BYTE,
1, 1, 15);
System.out.println(createColorModel(bilevel.getSampleModel()));
System.out.println(createColorModel(gray.getSampleModel()));
System.out.println(createColorModel(grayAlpha.getSampleModel()));
System.out.println(createColorModel(rgb.getSampleModel()));
System.out.println(createColorModel(rgba.getSampleModel()));
System.out.println(createColorModel(packed.getSampleModel()));
System.out.println(createColorModel(bandedSM));
}
*/
/**
* Creates a <code>ColorModel</code> that may be used with the
* specified <code>SampleModel</code>. If a suitable
* <code>ColorModel</code> cannot be found, this method returns
* <code>null</code>.
*
* <p> Suitable <code>ColorModel</code>s are guaranteed to exist
* for all instances of <code>ComponentSampleModel</code>.
* For 1- and 3- banded <code>SampleModel</code>s, the returned
* <code>ColorModel</code> will be opaque. For 2- and 4-banded
* <code>SampleModel</code>s, the output will use alpha transparency
* which is not premultiplied. 1- and 2-banded data will use a
* grayscale <code>ColorSpace</code>, and 3- and 4-banded data a sRGB
* <code>ColorSpace</code>. Data with 5 or more bands will have a
* <code>BogusColorSpace</code>.</p>
*
* <p>An instance of <code>DirectColorModel</code> will be created for
* instances of <code>SinglePixelPackedSampleModel</code> with no more
* than 4 bands.</p>
*
* <p>An instance of <code>IndexColorModel</code> will be created for
* instances of <code>MultiPixelPackedSampleModel</code>. The colormap
* will be a grayscale ramp with <code>1 << numberOfBits</code>
* entries ranging from zero to at most 255.</p>
*
* @return An instance of <code>ColorModel</code> that is suitable for
* the supplied <code>SampleModel</code>, or <code>null</code>.
*
* @throws IllegalArgumentException If <code>sampleModel</code> is
* <code>null</code>.
*/
public static final ColorModel createColorModel(SampleModel sampleModel) {
// Check the parameter.
if (sampleModel == null) {
throw new IllegalArgumentException("sampleModel == null!");
}
// Get the data type.
int dataType = sampleModel.getDataType();
// Check the data type
switch(dataType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
case DataBuffer.TYPE_SHORT:
case DataBuffer.TYPE_INT:
case DataBuffer.TYPE_FLOAT:
case DataBuffer.TYPE_DOUBLE:
break;
default:
// Return null for other types.
return null;
}
// The return variable.
ColorModel colorModel = null;
// Get the sample size.
int[] sampleSize = sampleModel.getSampleSize();
// Create a Component ColorModel.
if (sampleModel instanceof ComponentSampleModel) {
// Get the number of bands.
int numBands = sampleModel.getNumBands();
// Determine the color space.
ColorSpace colorSpace = null;
if (numBands <= 2) {
colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
} else if (numBands <= 4) {
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
} else {
colorSpace = new BogusColorSpace(numBands);
}
boolean hasAlpha = (numBands == 2) || (numBands == 4);
boolean isAlphaPremultiplied = false;
int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
colorModel = new ComponentColorModel(colorSpace, sampleSize, hasAlpha, isAlphaPremultiplied, transparency, dataType);
} else if (sampleModel.getNumBands() <= 4 && sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
int[] bitMasks = sppsm.getBitMasks();
int rmask = 0;
int gmask = 0;
int bmask = 0;
int amask = 0;
int numBands = bitMasks.length;
if (numBands <= 2) {
rmask = gmask = bmask = bitMasks[0];
if (numBands == 2) {
amask = bitMasks[1];
}
} else {
rmask = bitMasks[0];
gmask = bitMasks[1];
bmask = bitMasks[2];
if (numBands == 4) {
amask = bitMasks[3];
}
}
int bits = 0;
for (int i = 0; i < sampleSize.length; i++) {
bits += sampleSize[i];
}
return new DirectColorModel(bits, rmask, gmask, bmask, amask);
} else if (sampleModel instanceof MultiPixelPackedSampleModel) {
// Load the colormap with a ramp.
int bitsPerSample = sampleSize[0];
int numEntries = 1 << bitsPerSample;
byte[] map = new byte[numEntries];
for (int i = 0; i < numEntries; i++) {
map[i] = (byte) (i * 255 / (numEntries - 1));
}
colorModel = new IndexColorModel(bitsPerSample, numEntries, map, map, map);
}
return colorModel;
}
use of java.awt.image.ComponentColorModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method createColorModel.
public static ColorModel createColorModel(ColorSpace colorSpace, SampleModel sampleModel) {
ColorModel colorModel = null;
if (sampleModel == null) {
throw new IllegalArgumentException(I18N.getString("ImageUtil1"));
}
int numBands = sampleModel.getNumBands();
if (numBands < 1 || numBands > 4) {
return null;
}
int dataType = sampleModel.getDataType();
if (sampleModel instanceof ComponentSampleModel) {
if (dataType < DataBuffer.TYPE_BYTE || //dataType == DataBuffer.TYPE_SHORT ||
dataType > DataBuffer.TYPE_DOUBLE) {
return null;
}
if (colorSpace == null)
colorSpace = numBands <= 2 ? ColorSpace.getInstance(ColorSpace.CS_GRAY) : ColorSpace.getInstance(ColorSpace.CS_sRGB);
boolean useAlpha = (numBands == 2) || (numBands == 4);
int transparency = useAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
boolean premultiplied = false;
int dataTypeSize = DataBuffer.getDataTypeSize(dataType);
int[] bits = new int[numBands];
for (int i = 0; i < numBands; i++) {
bits[i] = dataTypeSize;
}
colorModel = new ComponentColorModel(colorSpace, bits, useAlpha, premultiplied, transparency, dataType);
} else if (sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
int[] bitMasks = sppsm.getBitMasks();
int rmask = 0;
int gmask = 0;
int bmask = 0;
int amask = 0;
numBands = bitMasks.length;
if (numBands <= 2) {
rmask = gmask = bmask = bitMasks[0];
if (numBands == 2) {
amask = bitMasks[1];
}
} else {
rmask = bitMasks[0];
gmask = bitMasks[1];
bmask = bitMasks[2];
if (numBands == 4) {
amask = bitMasks[3];
}
}
int[] sampleSize = sppsm.getSampleSize();
int bits = 0;
for (int i = 0; i < sampleSize.length; i++) {
bits += sampleSize[i];
}
if (colorSpace == null)
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
colorModel = new DirectColorModel(colorSpace, bits, rmask, gmask, bmask, amask, false, sampleModel.getDataType());
} else if (sampleModel instanceof MultiPixelPackedSampleModel) {
int bits = ((MultiPixelPackedSampleModel) sampleModel).getPixelBitStride();
int size = 1 << bits;
byte[] comp = new byte[size];
for (int i = 0; i < size; i++) comp[i] = (byte) (255 * i / (size - 1));
colorModel = new IndexColorModel(bits, size, comp, comp, comp);
}
return colorModel;
}
use of java.awt.image.ComponentColorModel in project jdk8u_jdk by JetBrains.
the class X11SurfaceData method getSurfaceType.
public static SurfaceType getSurfaceType(X11GraphicsConfig gc, int transparency, boolean pixmapSurface) {
boolean transparent = (transparency == Transparency.BITMASK);
SurfaceType sType;
ColorModel cm = gc.getColorModel();
switch(cm.getPixelSize()) {
case 24:
if (gc.getBitsPerPixel() == 24) {
if (cm instanceof DirectColorModel) {
// 4517321: We will always use ThreeByteBgr for 24 bpp
// surfaces, regardless of the pixel masks reported by
// X11. Despite ambiguity in the X11 spec in how 24 bpp
// surfaces are treated, it appears that the best
// SurfaceType for these configurations (including
// some Matrox Millenium and ATI Radeon boards) is
// ThreeByteBgr.
sType = transparent ? X11SurfaceData.ThreeByteBgrX11_BM : X11SurfaceData.ThreeByteBgrX11;
} else {
throw new sun.java2d.InvalidPipeException("Unsupported bit " + "depth/cm combo: " + cm.getPixelSize() + ", " + cm);
}
break;
}
// Fall through for 32 bit case
case 32:
if (cm instanceof DirectColorModel) {
if (((SunToolkit) java.awt.Toolkit.getDefaultToolkit()).isTranslucencyCapable(gc) && !pixmapSurface) {
sType = X11SurfaceData.IntArgbPreX11;
} else {
if (((DirectColorModel) cm).getRedMask() == 0xff0000) {
sType = transparent ? X11SurfaceData.IntRgbX11_BM : X11SurfaceData.IntRgbX11;
} else {
sType = transparent ? X11SurfaceData.IntBgrX11_BM : X11SurfaceData.IntBgrX11;
}
}
} else if (cm instanceof ComponentColorModel) {
sType = X11SurfaceData.FourByteAbgrPreX11;
} else {
throw new sun.java2d.InvalidPipeException("Unsupported bit " + "depth/cm combo: " + cm.getPixelSize() + ", " + cm);
}
break;
case 15:
sType = transparent ? X11SurfaceData.UShort555RgbX11_BM : X11SurfaceData.UShort555RgbX11;
break;
case 16:
if ((cm instanceof DirectColorModel) && (((DirectColorModel) cm).getGreenMask() == 0x3e0)) {
// fix for 4352984: Riva128 on Linux
sType = transparent ? X11SurfaceData.UShort555RgbX11_BM : X11SurfaceData.UShort555RgbX11;
} else {
sType = transparent ? X11SurfaceData.UShort565RgbX11_BM : X11SurfaceData.UShort565RgbX11;
}
break;
case 12:
if (cm instanceof IndexColorModel) {
sType = transparent ? X11SurfaceData.UShortIndexedX11_BM : X11SurfaceData.UShortIndexedX11;
} else {
throw new sun.java2d.InvalidPipeException("Unsupported bit " + "depth: " + cm.getPixelSize() + " cm=" + cm);
}
break;
case 8:
if (cm.getColorSpace().getType() == ColorSpace.TYPE_GRAY && cm instanceof ComponentColorModel) {
sType = transparent ? X11SurfaceData.ByteGrayX11_BM : X11SurfaceData.ByteGrayX11;
} else if (cm instanceof IndexColorModel && isOpaqueGray((IndexColorModel) cm)) {
sType = transparent ? X11SurfaceData.Index8GrayX11_BM : X11SurfaceData.Index8GrayX11;
} else {
sType = transparent ? X11SurfaceData.ByteIndexedX11_BM : X11SurfaceData.ByteIndexedOpaqueX11;
}
break;
default:
throw new sun.java2d.InvalidPipeException("Unsupported bit " + "depth: " + cm.getPixelSize());
}
return sType;
}
Aggregations