Example usage for java.awt.image DataBufferByte DataBufferByte

List of usage examples for java.awt.image DataBufferByte DataBufferByte

Introduction

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

Prototype

public DataBufferByte(byte[][] dataArray, int size) 

Source Link

Document

Constructs a byte-based DataBuffer with the specified arrays.

Usage

From source file:org.shaman.terrain.polygonal.GraphToHeightmap.java

private void saveMatrix(float[][] matrix, String filename, float min, float max) {
    byte[] buffer = new byte[size * size];
    int i = 0;/*from ww w.  j a va 2s.c  om*/
    for (int x = 0; x < size; ++x) {
        for (int y = 0; y < size; ++y) {
            buffer[i] = (byte) ((matrix[x][y] - min) * 255 / (max - min));
            i++;
        }
    }
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    int[] nBits = { 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    SampleModel sm = cm.createCompatibleSampleModel(size, size);
    DataBufferByte db = new DataBufferByte(buffer, size * size);
    WritableRaster raster = Raster.createWritableRaster(sm, db, null);
    BufferedImage result = new BufferedImage(cm, raster, false, null);
    try {
        ImageIO.write(result, "png", new File(filename));
    } catch (IOException ex) {
        Logger.getLogger(SketchTerrain.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.shaman.terrain.polygonal.GraphToHeightmap.java

private void saveColorMatrix(float[][][] matrix, String filename, float min, float max) {
    byte[] buffer = new byte[size * size * 3];
    int i = 0;//from  w  w w.j a  v  a2 s  .c  o m
    for (int x = 0; x < size; ++x) {
        for (int y = 0; y < size; ++y) {
            buffer[i] = (byte) ((matrix[x][y][0] - min) * 255 / (max - min));
            i++;
            buffer[i] = (byte) ((matrix[x][y][1] - min) * 255 / (max - min));
            i++;
            buffer[i] = (byte) ((matrix[x][y][2] - min) * 255 / (max - min));
            i++;
        }
    }
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
    int[] nBits = { 8, 8, 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    SampleModel sm = cm.createCompatibleSampleModel(size, size);
    DataBufferByte db = new DataBufferByte(buffer, size * size * 3);
    WritableRaster raster = Raster.createWritableRaster(sm, db, null);
    BufferedImage result = new BufferedImage(cm, raster, false, null);
    try {
        ImageIO.write(result, "png", new File(filename));
    } catch (IOException ex) {
        Logger.getLogger(SketchTerrain.class.getName()).log(Level.SEVERE, null, ex);
    }
}