下面列出了怎么用java.awt.image.DataBufferInt的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public BufferedImage scale(BufferedImage in, int w, int h) {
BufferedImage out = new BufferedImage(w, h, in.getType());
int[] pixels = ((DataBufferInt) in.getRaster().getDataBuffer()).getData();
int[] newpixels = ((DataBufferInt) out.getRaster().getDataBuffer()).getData();
int n = 0;
double k = 0d;
int ow = in.getWidth();
double xr = in.getWidth() / (double) w;
double yr = in.getHeight() / (double) h;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
newpixels[n] = pixels[(int) k];
k = k + xr;
n = n + 1;
}
k = ow * (int) (y * yr);
}
return out;
}
public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {
WritableRaster wr;
DataBuffer db;
BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.drawImage(bufferedImage, null, null);
bufferedImage = bi;
wr = bi.getRaster();
db = wr.getDataBuffer();
DataBufferInt dbi = (DataBufferInt) db;
int[] data = dbi.getData();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.asIntBuffer().put(data);
byteBuffer.flip();
return byteBuffer;
}
public Image makeImage(TestEnvironment env, int w, int h) {
BufferedImage img = new BufferedImage(w, h, type);
if (unmanaged) {
DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt)db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort)db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte)db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (Throwable e) {}
}
}
return img;
}
private static BufferedImage makeUnmanagedBI(final int type) {
final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
private static BufferedImage getBufferedImage(int sw) {
final BufferedImage bi = new BufferedImage(sw, sw, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, sw, sw);
g2d.dispose();
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
private static BufferedImage makeUnmanagedBI(final int type) {
final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
public static void RGB2CMYK(ICC_ColorSpace cmykColorSpace, int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) {
DataBuffer db = new DataBufferInt(rgb, rgb.length);
WritableRaster raster = Raster.createPackedRaster(db, imageWidth, imageHeight, imageWidth, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff}, null);
ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorConvertOp cco = new ColorConvertOp(sRGB, cmykColorSpace, null);
BufferedImage rgbImage = new BufferedImage(new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff), raster, false, null);
BufferedImage cmykImage = cco.filter(rgbImage, null);
WritableRaster cmykRaster = cmykImage.getRaster();
byte[] cmyk = (byte[])cmykRaster.getDataElements(0, 0, imageWidth, imageHeight, null);
for(int i = 0, index = 0; i < imageHeight; i++) {
for(int j = 0; j < imageWidth; j++) {
C[i][j] = (cmyk[index++]&0xff) - 128.0f;
M[i][j] = (cmyk[index++]&0xff) - 128.0f;
Y[i][j] = (cmyk[index++]&0xff) - 128.0f;
K[i][j] = (cmyk[index++]&0xff) - 128.0f;
}
}
}
@Test
public void testWriteImageRaw_happyCase() throws ImageWriteException,
ImageReadException, IOException {
final BufferedImage srcImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
final Map<String, Object> params = new HashMap<>();
params.put(PnmImageParser.PARAM_KEY_PNM_RAWBITS, PnmImageParser.PARAM_VALUE_PNM_RAWBITS_YES);
final byte[] dstBytes = Imaging.writeImageToBytes(srcImage, ImageFormats.PNM, params);
final BufferedImage dstImage = Imaging.getBufferedImage(dstBytes);
assertTrue(srcImage.getWidth() == dstImage.getWidth());
assertTrue(srcImage.getHeight() == dstImage.getHeight());
final DataBufferInt srcData = (DataBufferInt) srcImage.getRaster().getDataBuffer();
final DataBufferInt dstData = (DataBufferInt) dstImage.getRaster().getDataBuffer();
for (int bank = 0; bank < srcData.getNumBanks(); bank++) {
final int[] actual = srcData.getData(bank);
final int[] expected = dstData.getData(bank);
assertArrayEquals(actual, expected);
}
}
/**
* Renders an image with all possible colors. The image can be used to
* display the color look-up table.
*
* @param width Width of the image
* @param height Height of the image
* @return The new image.
*/
@Override
public BufferedImage getDiagramImage(int width, int height) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] imageBuffer = ((DataBufferInt) (img.getRaster().getDataBuffer())).getData();
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
double x = c / (width - 1d);
double y = 1d - r / (height - 1d);
int lutCol = (int) Math.round(x * (ColorLUT.LUT_SIZE - 1));
int lutRow = (int) Math.round(y * (ColorLUT.LUT_SIZE - 1));
imageBuffer[r * width + c] = lut[lutRow][lutCol];
}
}
return img;
}
public Image makeImage(TestEnvironment env, int w, int h) {
BufferedImage img = new BufferedImage(w, h, type);
if (unmanaged) {
DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt)db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort)db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte)db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (Throwable e) {}
}
}
return img;
}
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
int type) {
BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
Graphics2D g2d = img.createGraphics();
g2d.setColor(RGB);
g2d.fillRect(0, 0, SIZE, SIZE);
g2d.dispose();
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
private static BufferedImage makeUnmanagedBI(final int type) {
final BufferedImage bi = new BufferedImage(511, 255, type);
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
private static BufferedImage makeUnmanagedBI(final int type) {
final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
private static BufferedImage makeUnmanagedBI() {
final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
static void initScreen() {
if(!HAS_GUI) return;
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
try {
// This sets up the screens, and loads the different spritesheets.
initSpriteSheets();
} catch (IOException e) {
e.printStackTrace();
}
screen.pixels = pixels;
if(HAS_GUI) {
canvas.createBufferStrategy(3);
canvas.requestFocus();
}
}
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
int type) {
BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
Graphics2D g2d = img.createGraphics();
g2d.setColor(RGB);
g2d.fillRect(0, 0, SIZE, SIZE);
g2d.dispose();
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
private static BufferedImage makeUnmanagedBI() {
final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
private static BufferedImage makeUnmanagedBI() {
final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
/**
* Creates an RGB image from the given raw byte array, performing any
* necessary type conversions.
*
* @param data Array containing image data.
* @param c Nuber of channels. NB: Channels over 4 will be discarded.
* @param h Height of image plane.
* @param w Width of image plane.
* @param interleaved If set, the channels are assumed to be interleaved;
* otherwise they are assumed to be sequential. For example, for RGB
* data, the pattern "RGBRGBRGB..." is interleaved, while
* "RRR...GGG...BBB..." is sequential.
*/
public static BufferedImage makeRGBImage(final byte[] data, final int c,
final int w, final int h, final boolean interleaved)
{
final int cc = Math.min(c, 4); // throw away channels beyond 4
final int[] buf = new int[data.length / c];
final int nBits = (cc - 1) * 8;
for (int i = 0; i < buf.length; i++) {
for (int q = 0; q < cc; q++) {
if (interleaved) {
buf[i] |= ((data[i * c + q] & 0xff) << (nBits - q * 8));
}
else {
buf[i] |= ((data[q * buf.length + i] & 0xff) << (nBits - q * 8));
}
}
}
final DataBuffer buffer = new DataBufferInt(buf, buf.length);
return constructImage(cc, DataBuffer.TYPE_INT, w, h, false, false, buffer);
}
private static BufferedImage makeUnmanagedBI() {
final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
private void syncCopyBuffer(boolean reset, int x, int y, int w, int h, int scale) {
content.paintLock();
try {
int[] srcBuffer = ((DataBufferInt)bbImage.getRaster().getDataBuffer()).getData();
if (reset) {
copyBuffer = new int[srcBuffer.length];
}
int linestride = bbImage.getWidth();
x *= scale;
y *= scale;
w *= scale;
h *= scale;
for (int i=0; i<h; i++) {
int from = (y + i) * linestride + x;
System.arraycopy(srcBuffer, from, copyBuffer, from, w);
}
} finally {
content.paintUnlock();
}
}
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
int type) {
BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
Graphics2D g2d = img.createGraphics();
g2d.setColor(RGB);
g2d.fillRect(0, 0, SIZE, SIZE);
g2d.dispose();
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
private static BufferedImage makeUnmanagedBI(final int type) {
final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
@Override
public BufferedImage getImage (int maxDistance)
{
final BufferedImage img = new BufferedImage(
getWidth(),
getHeight(),
BufferedImage.TYPE_INT_ARGB);
// Built a LUT (biased one cell to make room for VALUE_UNKNOWN)
final int rawDistMax = maxDistance * normalizer;
final int[] lut = getLut(rawDistMax);
// Process image data, pixel by pixel
final int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
Arrays.fill(data, Color.WHITE.getRGB()); // All white by default
for (int i = (getWidth() * getHeight()) - 1; i >= 0; i--) {
final int val = getValue(i);
if (val < rawDistMax) {
data[i] = lut[1 + val]; // LUT is biased
}
}
return img;
}
@Override
public void grab(Rectangle rect) throws Exception {
// ADD UPDATE CHECK, IT MAY FAIL SOMETIMES
if (screenImage == null) {
grab();
return;
}
screenImage = robot.createScreenCapture(rect);
final int pixels[] = ((DataBufferInt) screenImage.getData().getDataBuffer()).getData();
long[] buffer_origin = new long[] { rect.x * Sizeof.cl_int, rect.y, 0 }; // setup initial offsets
long[] host_origin = new long[] { 0, 0, 0 }; // screenshot data has no offsets
long[] region = new long[] { rect.width * Sizeof.cl_int, rect.height, 1 }; // set rectangle bounds
int result = clEnqueueWriteBufferRect(commandQueue, memObjects[0], CL_TRUE, buffer_origin, host_origin, region,
(long) (screenRect.width * Sizeof.cl_int), (long) 0, (long) (rect.width * Sizeof.cl_int), (long) 0,
Pointer.to(pixels), 0, null, null);
if (result != CL_SUCCESS)
throw new Exception(String.valueOf(result).concat(" screenshot update error occured"));
}
public static void RGB2YCCK_Inverted(ICC_ColorSpace cmykColorSpace, int[] rgb, float[][] Y, float[][] Cb, float[][] Cr, float[][] K, int imageWidth, int imageHeight) {
DataBuffer db = new DataBufferInt(rgb, rgb.length);
WritableRaster raster = Raster.createPackedRaster(db, imageWidth, imageHeight, imageWidth, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff}, null);
ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorConvertOp cco = new ColorConvertOp(sRGB, cmykColorSpace, null);
BufferedImage rgbImage = new BufferedImage(new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff), raster, false, null);
BufferedImage cmykImage = cco.filter(rgbImage, null);
WritableRaster cmykRaster = cmykImage.getRaster();
byte[] cmyk = (byte[])cmykRaster.getDataElements(0, 0, imageWidth, imageHeight, null);
float c, m, y;
for(int i = 0, index = 0; i < imageHeight; i++) {
for(int j = 0; j < imageWidth; j++) {
c = 255.0f - (cmyk[index++]&0xff); // Red
m = 255.0f - (cmyk[index++]&0xff); // Green
y = 255.0f - (cmyk[index++]&0xff); // Blue
Y[i][j] = 128.0f - (c*0.299f + m*0.587f + y*0.114f);
Cb[i][j] = 0.16874f*c + 0.33126f*m - 0.5f*y;
Cr[i][j] = - 0.5f*c + 0.41869f*m + 0.08131f*y;
K[i][j] = 128.0f - (cmyk[index++]&0xff);
}
}
}
/**
* Convert the front framebuffer to an Image
*
* @return
*/
private Image screenshot()
{
int width = client.getCanvasWidth();
int height = client.getCanvasHeight();
if (client.isStretchedEnabled())
{
Dimension dim = client.getStretchedDimensions();
width = dim.width;
height = dim.height;
}
ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4)
.order(ByteOrder.nativeOrder());
gl.glReadBuffer(gl.GL_FRONT);
gl.glReadPixels(0, 0, width, height, GL.GL_RGBA, gl.GL_UNSIGNED_BYTE, buffer);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
int r = buffer.get() & 0xff;
int g = buffer.get() & 0xff;
int b = buffer.get() & 0xff;
buffer.get(); // alpha
pixels[(height - y - 1) * width + x] = (r << 16) | (g << 8) | b;
}
}
return image;
}
/**
* {@inheritDoc}
*/
public final Raster getRaster(int x, int y, int w, int h) {
// If working raster is big enough, reuse it. Otherwise,
// build a large enough new one.
Raster raster = saved;
if (raster == null ||
raster.getWidth() < w || raster.getHeight() < h)
{
raster = getCachedRaster(model, w, h);
saved = raster;
}
// Access raster internal int array. Because we use a DirectColorModel,
// we know the DataBuffer is of type DataBufferInt and the SampleModel
// is SinglePixelPackedSampleModel.
// Adjust for initial offset in DataBuffer and also for the scanline
// stride.
// These calls make the DataBuffer non-acceleratable, but the
// Raster is never Stable long enough to accelerate anyway...
DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
int[] pixels = rasterDB.getData(0);
int off = rasterDB.getOffset();
int scanlineStride = ((SinglePixelPackedSampleModel)
raster.getSampleModel()).getScanlineStride();
int adjust = scanlineStride - w;
fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass
return raster;
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
vi.validate(gc);
BufferedImage bi =
new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
data[0] = 0x0000007f;
data[1] = 0x0000007f;
data[2] = 0xff00007f;
data[3] = 0xff00007f;
Graphics2D g = vi.createGraphics();
g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
g.drawImage(bi, 0, 0, null);
bi = vi.getSnapshot();
if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
throw new RuntimeException("Test FAILED: color at 0x0 ="+
Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
Integer.toHexString(bi.getRGB(1,1)));
}
System.out.println("Test PASSED.");
}
/**
* {@inheritDoc}
*/
public final Raster getRaster(int x, int y, int w, int h) {
// If working raster is big enough, reuse it. Otherwise,
// build a large enough new one.
Raster raster = saved;
if (raster == null ||
raster.getWidth() < w || raster.getHeight() < h)
{
raster = getCachedRaster(model, w, h);
saved = raster;
}
// Access raster internal int array. Because we use a DirectColorModel,
// we know the DataBuffer is of type DataBufferInt and the SampleModel
// is SinglePixelPackedSampleModel.
// Adjust for initial offset in DataBuffer and also for the scanline
// stride.
// These calls make the DataBuffer non-acceleratable, but the
// Raster is never Stable long enough to accelerate anyway...
DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
int[] pixels = rasterDB.getData(0);
int off = rasterDB.getOffset();
int scanlineStride = ((SinglePixelPackedSampleModel)
raster.getSampleModel()).getScanlineStride();
int adjust = scanlineStride - w;
fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass
return raster;
}