下面列出了java.awt.image.DataBuffer#TYPE_INT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
}
@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;
}
}
@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;
}
}
static String getDTName(int dType) {
switch(dType) {
case DataBuffer.TYPE_BYTE:
return "TYPE_BYTE";
case DataBuffer.TYPE_DOUBLE:
return "TYPE_DOUBLE";
case DataBuffer.TYPE_FLOAT:
return "TYPE_FLOAT";
case DataBuffer.TYPE_INT:
return "TYPE_INT";
case DataBuffer.TYPE_SHORT:
return "TYPE_SHORT";
case DataBuffer.TYPE_USHORT:
return "TYPE_USHORT";
case DataBuffer.TYPE_UNDEFINED:
return "TYPE_UNDEFINED";
}
return "UNKNOWN";
}
@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 static void main(String[] args) {
int[] dataTypes = new int[] {
DataBuffer.TYPE_BYTE,
DataBuffer.TYPE_USHORT,
DataBuffer.TYPE_INT };
for (int type : dataTypes) {
doTest(type);
}
}
private static boolean checkComponentsOrder(ColorModel cm) {
if (cm instanceof DirectColorModel && cm.getTransferType() == DataBuffer.TYPE_INT) {
DirectColorModel directCM = (DirectColorModel) cm;
return directCM.getRedMask() == 0x00FF0000 && directCM.getGreenMask() == 0x0000FF00 && directCM.getBlueMask() == 0x000000FF
&& (directCM.getNumComponents() != 4 || directCM.getAlphaMask() == 0xFF000000);
}
return false;
}
static PixelWriter createXorPixelWriter(SunGraphics2D sg2d,
SurfaceData sData)
{
ColorModel dstCM = sData.getColorModel();
Object srcPixel = dstCM.getDataElements(sg2d.eargb, null);
XORComposite comp = (XORComposite)sg2d.getComposite();
int xorrgb = comp.getXorColor().getRGB();
Object xorPixel = dstCM.getDataElements(xorrgb, null);
switch (dstCM.getTransferType()) {
case DataBuffer.TYPE_BYTE:
return new XorPixelWriter.ByteData(srcPixel, xorPixel);
case DataBuffer.TYPE_SHORT:
case DataBuffer.TYPE_USHORT:
return new XorPixelWriter.ShortData(srcPixel, xorPixel);
case DataBuffer.TYPE_INT:
return new XorPixelWriter.IntData(srcPixel, xorPixel);
case DataBuffer.TYPE_FLOAT:
return new XorPixelWriter.FloatData(srcPixel, xorPixel);
case DataBuffer.TYPE_DOUBLE:
return new XorPixelWriter.DoubleData(srcPixel, xorPixel);
default:
throw new InternalError("Unsupported XOR pixel type");
}
}
public static void main(String[] args) {
int[] dataTypes = new int[] {
DataBuffer.TYPE_BYTE,
DataBuffer.TYPE_USHORT,
DataBuffer.TYPE_INT };
for (int type : dataTypes) {
doTest(type);
}
}
@Override
public void paint(final BufferedImage image, final Graphics2D graphics) {
final DataBuffer dataBuf = image.getRaster().getDataBuffer();
switch (dataBuf.getDataType()) {
case DataBuffer.TYPE_INT: {
// We chose RGB888 model, so Raster will use DataBufferInt type
final DataBufferInt dataBuffer = (DataBufferInt) dataBuf;
final int imageWidth = image.getWidth();
final int imageHeight = image.getHeight();
// Paint rectangle directly on buffer, line by line
final int[] imageBuffer = dataBuffer.getData();
for (int srcLine = 0, dstLine = y; srcLine < height && dstLine < imageHeight; srcLine++, dstLine++) {
try {
System.arraycopy(buf, srcLine * width, imageBuffer, x + dstLine * imageWidth, width);
} catch (final IndexOutOfBoundsException e) {
s_logger.info("[ignored] buffer overflow!?!", e);
}
}
break;
}
default:
throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: " +
dataBuf.getClass().getSimpleName());
}
}
public Interleaved(ColorSpace colorSpace,
int[] bandOffsets,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
if (colorSpace == null) {
throw new IllegalArgumentException("colorSpace == null!");
}
if (bandOffsets == null) {
throw new IllegalArgumentException("bandOffsets == null!");
}
int numBands = colorSpace.getNumComponents() +
(hasAlpha ? 1 : 0);
if (bandOffsets.length != numBands) {
throw new IllegalArgumentException
("bandOffsets.length is wrong!");
}
if (dataType != DataBuffer.TYPE_BYTE &&
dataType != DataBuffer.TYPE_SHORT &&
dataType != DataBuffer.TYPE_USHORT &&
dataType != DataBuffer.TYPE_INT &&
dataType != DataBuffer.TYPE_FLOAT &&
dataType != DataBuffer.TYPE_DOUBLE) {
throw new IllegalArgumentException
("Bad value for dataType!");
}
this.colorSpace = colorSpace;
this.bandOffsets = (int[])bandOffsets.clone();
this.dataType = dataType;
this.hasAlpha = hasAlpha;
this.isAlphaPremultiplied = isAlphaPremultiplied;
this.colorModel =
ImageTypeSpecifier.createComponentCM(colorSpace,
bandOffsets.length,
dataType,
hasAlpha,
isAlphaPremultiplied);
int minBandOffset = bandOffsets[0];
int maxBandOffset = minBandOffset;
for (int i = 0; i < bandOffsets.length; i++) {
int offset = bandOffsets[i];
minBandOffset = Math.min(offset, minBandOffset);
maxBandOffset = Math.max(offset, maxBandOffset);
}
int pixelStride = maxBandOffset - minBandOffset + 1;
int w = 1;
int h = 1;
this.sampleModel =
new PixelInterleavedSampleModel(dataType,
w, h,
pixelStride,
w*pixelStride,
bandOffsets);
}
public Packed(ColorSpace colorSpace,
int redMask,
int greenMask,
int blueMask,
int alphaMask, // 0 if no alpha
int transferType,
boolean isAlphaPremultiplied) {
if (colorSpace == null) {
throw new IllegalArgumentException("colorSpace == null!");
}
if (colorSpace.getType() != ColorSpace.TYPE_RGB) {
throw new IllegalArgumentException
("colorSpace is not of type TYPE_RGB!");
}
if (transferType != DataBuffer.TYPE_BYTE &&
transferType != DataBuffer.TYPE_USHORT &&
transferType != DataBuffer.TYPE_INT) {
throw new IllegalArgumentException
("Bad value for transferType!");
}
if (redMask == 0 && greenMask == 0 &&
blueMask == 0 && alphaMask == 0) {
throw new IllegalArgumentException
("No mask has at least 1 bit set!");
}
this.colorSpace = colorSpace;
this.redMask = redMask;
this.greenMask = greenMask;
this.blueMask = blueMask;
this.alphaMask = alphaMask;
this.transferType = transferType;
this.isAlphaPremultiplied = isAlphaPremultiplied;
int bits = 32;
this.colorModel =
new DirectColorModel(colorSpace,
bits,
redMask, greenMask, blueMask,
alphaMask, isAlphaPremultiplied,
transferType);
this.sampleModel = colorModel.createCompatibleSampleModel(1, 1);
}
public Interleaved(ColorSpace colorSpace,
int[] bandOffsets,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
if (colorSpace == null) {
throw new IllegalArgumentException("colorSpace == null!");
}
if (bandOffsets == null) {
throw new IllegalArgumentException("bandOffsets == null!");
}
int numBands = colorSpace.getNumComponents() +
(hasAlpha ? 1 : 0);
if (bandOffsets.length != numBands) {
throw new IllegalArgumentException
("bandOffsets.length is wrong!");
}
if (dataType != DataBuffer.TYPE_BYTE &&
dataType != DataBuffer.TYPE_SHORT &&
dataType != DataBuffer.TYPE_USHORT &&
dataType != DataBuffer.TYPE_INT &&
dataType != DataBuffer.TYPE_FLOAT &&
dataType != DataBuffer.TYPE_DOUBLE) {
throw new IllegalArgumentException
("Bad value for dataType!");
}
this.colorSpace = colorSpace;
this.bandOffsets = (int[])bandOffsets.clone();
this.dataType = dataType;
this.hasAlpha = hasAlpha;
this.isAlphaPremultiplied = isAlphaPremultiplied;
this.colorModel =
ImageTypeSpecifier.createComponentCM(colorSpace,
bandOffsets.length,
dataType,
hasAlpha,
isAlphaPremultiplied);
int minBandOffset = bandOffsets[0];
int maxBandOffset = minBandOffset;
for (int i = 0; i < bandOffsets.length; i++) {
int offset = bandOffsets[i];
minBandOffset = Math.min(offset, minBandOffset);
maxBandOffset = Math.max(offset, maxBandOffset);
}
int pixelStride = maxBandOffset - minBandOffset + 1;
int w = 1;
int h = 1;
this.sampleModel =
new PixelInterleavedSampleModel(dataType,
w, h,
pixelStride,
w*pixelStride,
bandOffsets);
}
public Packed(ColorSpace colorSpace,
int redMask,
int greenMask,
int blueMask,
int alphaMask, // 0 if no alpha
int transferType,
boolean isAlphaPremultiplied) {
if (colorSpace == null) {
throw new IllegalArgumentException("colorSpace == null!");
}
if (colorSpace.getType() != ColorSpace.TYPE_RGB) {
throw new IllegalArgumentException
("colorSpace is not of type TYPE_RGB!");
}
if (transferType != DataBuffer.TYPE_BYTE &&
transferType != DataBuffer.TYPE_USHORT &&
transferType != DataBuffer.TYPE_INT) {
throw new IllegalArgumentException
("Bad value for transferType!");
}
if (redMask == 0 && greenMask == 0 &&
blueMask == 0 && alphaMask == 0) {
throw new IllegalArgumentException
("No mask has at least 1 bit set!");
}
this.colorSpace = colorSpace;
this.redMask = redMask;
this.greenMask = greenMask;
this.blueMask = blueMask;
this.alphaMask = alphaMask;
this.transferType = transferType;
this.isAlphaPremultiplied = isAlphaPremultiplied;
int bits = 32;
this.colorModel =
new DirectColorModel(colorSpace,
bits,
redMask, greenMask, blueMask,
alphaMask, isAlphaPremultiplied,
transferType);
this.sampleModel = colorModel.createCompatibleSampleModel(1, 1);
}
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
if (src.getSampleModel().getDataType() != DataBuffer.TYPE_INT ||
dstIn.getSampleModel().getDataType() != DataBuffer.TYPE_INT ||
dstOut.getSampleModel().getDataType() != DataBuffer.TYPE_INT) {
throw new IllegalStateException(
"Source and destination must store pixels as INT.");
}
int width = Math.min(src.getWidth(), dstIn.getWidth());
int height = Math.min(src.getHeight(), dstIn.getHeight());
float alpha = composite.getAlpha();
int[] srcPixel = new int[4];
int[] dstPixel = new int[4];
int[] srcPixels = new int[width];
int[] dstPixels = new int[width];
for (int y = 0; y < height; y++) {
src.getDataElements(0, y, width, 1, srcPixels);
dstIn.getDataElements(0, y, width, 1, dstPixels);
for (int x = 0; x < width; x++) {
// pixels are stored as INT_ARGB
// our arrays are [R, G, B, A]
int pixel = srcPixels[x];
srcPixel[0] = (pixel >> 16) & 0xFF;
srcPixel[1] = (pixel >> 8) & 0xFF;
srcPixel[2] = (pixel ) & 0xFF;
srcPixel[3] = (pixel >> 24) & 0xFF;
pixel = dstPixels[x];
dstPixel[0] = (pixel >> 16) & 0xFF;
dstPixel[1] = (pixel >> 8) & 0xFF;
dstPixel[2] = (pixel ) & 0xFF;
dstPixel[3] = (pixel >> 24) & 0xFF;
int[] result = blender.blend(srcPixel, dstPixel);
// mixes the result with the opacity
dstPixels[x] = ((int) (dstPixel[3] + (result[3] - dstPixel[3]) * alpha) & 0xFF) << 24 |
((int) (dstPixel[0] + (result[0] - dstPixel[0]) * alpha) & 0xFF) << 16 |
((int) (dstPixel[1] + (result[1] - dstPixel[1]) * alpha) & 0xFF) << 8 |
(int) (dstPixel[2] + (result[2] - dstPixel[2]) * alpha) & 0xFF;
}
dstOut.setDataElements(0, y, width, 1, dstPixels);
}
}
private void checkSampleModel(SampleModel sm) {
int type = sm.getDataType();
if (type < DataBuffer.TYPE_BYTE || type > DataBuffer.TYPE_INT
|| sm.getNumBands() != 1 || sm.getSampleSize(0) != 1)
throw new IllegalArgumentException(I18N.getString("WBMPImageWriter2"));
}
public Indexed(byte[] redLUT,
byte[] greenLUT,
byte[] blueLUT,
byte[] alphaLUT,
int bits,
int dataType) {
if (redLUT == null || greenLUT == null || blueLUT == null) {
throw new IllegalArgumentException("LUT is null!");
}
if (bits != 1 && bits != 2 && bits != 4 &&
bits != 8 && bits != 16) {
throw new IllegalArgumentException("Bad value for bits!");
}
if (dataType != DataBuffer.TYPE_BYTE &&
dataType != DataBuffer.TYPE_SHORT &&
dataType != DataBuffer.TYPE_USHORT &&
dataType != DataBuffer.TYPE_INT) {
throw new IllegalArgumentException
("Bad value for dataType!");
}
if ((bits > 8 && dataType == DataBuffer.TYPE_BYTE) ||
(bits > 16 && dataType != DataBuffer.TYPE_INT)) {
throw new IllegalArgumentException
("Too many bits for dataType!");
}
int len = 1 << bits;
if (redLUT.length != len ||
greenLUT.length != len ||
blueLUT.length != len ||
(alphaLUT != null && alphaLUT.length != len)) {
throw new IllegalArgumentException("LUT has improper length!");
}
this.redLUT = (byte[])redLUT.clone();
this.greenLUT = (byte[])greenLUT.clone();
this.blueLUT = (byte[])blueLUT.clone();
if (alphaLUT != null) {
this.alphaLUT = (byte[])alphaLUT.clone();
}
this.bits = bits;
this.dataType = dataType;
if (alphaLUT == null) {
this.colorModel = new IndexColorModel(bits,
redLUT.length,
redLUT,
greenLUT,
blueLUT);
} else {
this.colorModel = new IndexColorModel(bits,
redLUT.length,
redLUT,
greenLUT,
blueLUT,
alphaLUT);
}
if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) ||
(bits == 16 &&
(dataType == DataBuffer.TYPE_SHORT ||
dataType == DataBuffer.TYPE_USHORT))) {
int[] bandOffsets = { 0 };
this.sampleModel =
new PixelInterleavedSampleModel(dataType,
1, 1, 1, 1,
bandOffsets);
} else {
this.sampleModel =
new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
}
}
/**
* Warps a rectangle. If ROI is present, the intersection between ROI and tile bounds is
* calculated; The result ROI will be used for calculations inside the computeRect() method.
*/
@Override
protected void computeRect(
final PlanarImage[] sources,
final WritableRaster dest,
final Rectangle destRect) {
// Retrieve format tags.
final RasterFormatTag[] formatTags = getFormatTags();
final RasterAccessor dst = new RasterAccessor(dest, destRect, formatTags[1], getColorModel());
RandomIter roiIter = null;
boolean roiContainsTile = false;
boolean roiDisjointTile = false;
// If a ROI is present, then only the part contained inside the current
// tile bounds is taken.
if (hasROI) {
final Rectangle srcRectExpanded = mapDestRect(destRect, 0);
// The tile dimension is extended for avoiding border errors
srcRectExpanded.setRect(
srcRectExpanded.getMinX() - leftPad,
srcRectExpanded.getMinY() - topPad,
srcRectExpanded.getWidth() + rightPad + leftPad,
srcRectExpanded.getHeight() + bottomPad + topPad);
if (!roiBounds.intersects(srcRectExpanded)) {
roiDisjointTile = true;
} else {
roiContainsTile = roi.contains(srcRectExpanded);
if (!roiContainsTile) {
if (!roi.intersects(srcRectExpanded)) {
roiDisjointTile = true;
} else {
final PlanarImage roiIMG = getImage();
roiIter = RandomIterFactory.create(roiIMG, null, TILE_CACHED, ARRAY_CALC);
}
}
}
}
if (!hasROI || !roiDisjointTile) {
switch (dst.getDataType()) {
case DataBuffer.TYPE_BYTE:
computeRectByte(sources[0], dst, roiIter, roiContainsTile);
break;
case DataBuffer.TYPE_USHORT:
computeRectUShort(sources[0], dst, roiIter, roiContainsTile);
break;
case DataBuffer.TYPE_SHORT:
computeRectShort(sources[0], dst, roiIter, roiContainsTile);
break;
case DataBuffer.TYPE_INT:
computeRectInt(sources[0], dst, roiIter, roiContainsTile);
break;
case DataBuffer.TYPE_FLOAT:
computeRectFloat(sources[0], dst, roiIter, roiContainsTile);
break;
case DataBuffer.TYPE_DOUBLE:
computeRectDouble(sources[0], dst, roiIter, roiContainsTile);
break;
}
// After the calculations, the output data are copied into the
// WritableRaster
if (dst.isDataCopy()) {
dst.clampDataArrays();
dst.copyDataToRaster();
}
} else {
// If the tile is outside the ROI, then the destination Raster is
// set to backgroundValues
if (setBackground) {
ImageUtil.fillBackground(dest, destRect, backgroundValues);
}
}
}
public static DirectColorModel createDCM32(int rMask, int gMask, int bMask,
int aMask, boolean aPre) {
return new DirectColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
32, rMask, gMask, bMask, aMask, aPre, DataBuffer.TYPE_INT);
}
public static DirectColorModel createDCM32(int rMask, int gMask, int bMask,
int aMask, boolean aPre) {
return new DirectColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
32, rMask, gMask, bMask, aMask, aPre, DataBuffer.TYPE_INT);
}