Example usage for java.awt.image Raster createInterleavedRaster

List of usage examples for java.awt.image Raster createInterleavedRaster

Introduction

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

Prototype

public static WritableRaster createInterleavedRaster(int dataType, int w, int h, int bands, Point location) 

Source Link

Document

Creates a Raster based on a PixelInterleavedSampleModel with the specified data type, width, height, and number of bands.

Usage

From source file:Main.java

private static ByteBuffer convertImageData(BufferedImage bufferedImage) {
    ByteBuffer imageBuffer;/*from  www  .  j  a  v  a2s .c om*/
    WritableRaster raster;
    BufferedImage texImage;

    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
}

From source file:omr.jai.TestImage3.java

    public static PlanarImage decodeImage (String[] rows)
    {/* www.java2 s  .  c om*/
        // Create the DataBuffer to hold the pixel samples
        final int width = rows[0].length();
        final int height = rows.length;

        // Create Raster
        Raster raster;
        if (true) {
            raster = Raster.createPackedRaster
            (DataBuffer.TYPE_INT, width, height,
             new int[] {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000},// bandMasks RGBA
             null);
        } else {
            raster = Raster.createInterleavedRaster
                (DataBuffer.TYPE_BYTE, width, height,
                 4,// num of bands
                 null);
        }

        // Populate the data buffer
        DataBuffer dataBuffer = raster.getDataBuffer();
        int index = 0;
        for (String row : rows) {
            for (int x = 0; x < width; x++) {
                int argb = toARGB(row.charAt(x));
                dataBuffer.setElem(index, argb);
                index++;
            }
        }

        // Dump
//         final int size = width * height;
//         System.out.println("DataBuffer :");
//         for (int i = 0; i < size; i++) {
//             if (i % width == 0) {
//                 System.out.println();
//             }
//             System.out.print(String.format("%8x ", dataBuffer.getElem(i)));
//         }
//         System.out.println();

        // Create the image
        BufferedImage bufferedImage = new BufferedImage
                (width, height, BufferedImage.TYPE_INT_ARGB);
        bufferedImage.setData(raster);

        // Dump
//         System.out.println("BufferedImage :");
//         for (int y = 0; y < height; y++) {
//             System.out.println();
//             for (int x = 0; x < width; x++) {
//                 System.out.print(String.format("%8x ", bufferedImage.getRGB(x, y)));
//             }
//         }
//         System.out.println();

        return PlanarImage.wrapRenderedImage(bufferedImage);
    }