Example usage for java.awt.image WritableRaster getDataBuffer

List of usage examples for java.awt.image WritableRaster getDataBuffer

Introduction

In this page you can find the example usage for java.awt.image WritableRaster getDataBuffer.

Prototype

public DataBuffer getDataBuffer() 

Source Link

Document

Returns the DataBuffer associated with this Raster.

Usage

From source file:org.shaman.terrain.vegetation.ImpositorCreator.java

public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out) {
    WritableRaster wr = out.getRaster();
    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();

    byte[] cpuArray = db.getData();

    // copy native memory to java memory
    bgraBuf.clear();/*from   ww  w .ja  va 2s  . co m*/
    bgraBuf.get(cpuArray);
    bgraBuf.clear();

    int width = wr.getWidth();
    int height = wr.getHeight();

    // flip the components the way AWT likes them

    // calcuate half of height such that all rows of the array are written to
    // e.g. for odd heights, write 1 more scanline
    int heightdiv2ceil = height % 2 == 1 ? (height / 2) + 1 : height / 2;
    for (int y = 0; y < heightdiv2ceil; y++) {
        for (int x = 0; x < width; x++) {
            int inPtr = (y * width + x) * 4;
            int outPtr = ((height - y - 1) * width + x) * 4;

            byte b1 = cpuArray[inPtr + 0];
            byte g1 = cpuArray[inPtr + 1];
            byte r1 = cpuArray[inPtr + 2];
            byte a1 = cpuArray[inPtr + 3];

            byte b2 = cpuArray[outPtr + 0];
            byte g2 = cpuArray[outPtr + 1];
            byte r2 = cpuArray[outPtr + 2];
            byte a2 = cpuArray[outPtr + 3];

            cpuArray[outPtr + 0] = a1;
            cpuArray[outPtr + 1] = r1;//b1;
            cpuArray[outPtr + 2] = g1;
            cpuArray[outPtr + 3] = b1;//r1;

            cpuArray[inPtr + 0] = a2;
            cpuArray[inPtr + 1] = r2;//b2;
            cpuArray[inPtr + 2] = g2;
            cpuArray[inPtr + 3] = b2;//r2;
        }
    }
}