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(DataBuffer dataBuffer, int w, int h, int scanlineStride,
        int pixelStride, int[] bandOffsets, Point location) 

Source Link

Document

Creates a Raster based on a PixelInterleavedSampleModel with the specified DataBuffer, width, height, scanline stride, pixel stride, and band offsets.

Usage

From source file:com.alvermont.terraj.planet.io.ImageBuilder.java

/**
 * Create and return a <code>BufferedImage</code> object from the
 * result of the projection. This image can then be further processed
 * or written out to a file using <code>ImageIO</code>.
 *
 * @param proj The projection that will provide the image
 * @return A <code>BufferedImage</code> object containing the results of
 * the projection./*from w ww  . j a  va2 s.  c o m*/
 */
public BufferedImage getImage(Projector proj) {
    // get the pixel data and store it into a data buffer
    final byte[] pixels = getPixels(proj);

    final DataBuffer db = new DataBufferByte(pixels, pixels.length);

    // set up offsets for the R,G,B elements
    final int[] offsets = new int[3];

    offsets[0] = 0;
    offsets[1] = 1;
    offsets[2] = 2;

    // create the raster from the pixel data
    final int height = proj.getParameters().getProjectionParameters().getHeight();
    final int width = proj.getParameters().getProjectionParameters().getWidth();

    final WritableRaster raster = Raster.createInterleavedRaster(db, width, height, width * 3, 3, offsets,
            null);

    // create a colour model that matches the raster we have
    final ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
            Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

    // combine raster and colour model into a buffered image
    final BufferedImage img = new BufferedImage(cm, raster, false, null);

    return img;
}

From source file:ImageComponentByReferenceTest.java

TiledImage() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8, 8 };
    int i, j, k, cc = 255;
    int[] bandOffset = new int[4];
    colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0);
    // Create 9 tiles
    bandOffset[0] = 3;//  w  w  w .  java 2 s . c om
    bandOffset[1] = 2;
    bandOffset[2] = 1;
    bandOffset[3] = 0;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            tile[i][j] = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 8, 8, 32, 4, bandOffset, null);
        }
    }

    // tile {-2, -1}
    byte[] byteData = ((DataBufferByte) tile[0][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4 + 4 * 4; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {-1, -1}
    byteData = ((DataBufferByte) tile[1][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4; i < 8; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {1, -1}
    byteData = ((DataBufferByte) tile[2][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
        for (j = 0; j < 4; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {-2, 0}
    byteData = ((DataBufferByte) tile[0][1].getDataBuffer()).getData();
    for (i = 0, k = 16; i < 4; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (i = 4, k = 8 * 4 * 4 + 16; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
    }
    // tile {-1, 0}
    byteData = ((DataBufferByte) tile[1][1].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 4; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (i = 0, k = 8 * 4 * 4; i < 4; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }

        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }

    }

    // tile {0, 0}
    byteData = ((DataBufferByte) tile[2][1].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 4; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {-2, 1}
    byteData = ((DataBufferByte) tile[0][2].getDataBuffer()).getData();
    for (i = 4, k = 16; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
    }

    // tile {-1, 1}
    byteData = ((DataBufferByte) tile[1][2].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 8; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {0, 1}
    byteData = ((DataBufferByte) tile[2][2].getDataBuffer()).getData();
    for (i = 4, k = 0; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }

    bigTile = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 16, 16, 64, 4, bandOffset, null);
    ;
    byteData = ((DataBufferByte) bigTile.getDataBuffer()).getData();
    for (i = 0, k = 0; i < 8; i++) {
        for (j = 0; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
        for (; j < 16; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (; i < 16; i++) {
        for (j = 0; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
        for (; j < 16; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }
    checkBoard = new BufferedImage(colorModel, bigTile, false, null);
}

From source file:TrackerPanel.java

private void drawUserDepths(Graphics2D g2d) {
    // define an 8-bit RGB channel color model
    ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
            new int[] { 8, 8, 8 }, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);

    // fill the raster with the depth image bytes
    DataBufferByte dataBuffer = new DataBufferByte(imgbytes, imWidth * imHeight * 3);

    WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, imWidth, imHeight, imWidth * 3, 3,
            new int[] { 0, 1, 2 }, null);

    // combine color model and raster to create a BufferedImage
    BufferedImage image = new BufferedImage(colorModel, raster, false, null);

    g2d.drawImage(image, 0, 0, null);/*  www . j a va 2 s.c om*/
}

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils.java

/**
 * Converts image raster data to a JPEG with RGB color space. Only images with color space CMYK and YCCK are
 * converted, other images are left untouched.
 *
 * Rationale: Java's ImageIO can't process 4-component images and Java2D can't apply AffineTransformOp either,
 * so we have to convert raster data to a JPG with RGB color space.
 *
 * The technique used in this method is due to Mark Stephens, and free for any use. See
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903 or
 * http://www.mail-archive.com/java2d-interest@capra.eng.sun.com/msg03247.html
 *
 * @param is the image data/*from  w w  w. j av  a  2 s  .  c  o  m*/
 * @param colorModel the color model of the image
 * @return the RGB version of the supplied image
 */
public static InputStream convertToRGB(InputStream is, ColorModel colorModel)
        throws IOException, UnsupportedImageException {
    if (colorModel != ColorModel.CMYK && colorModel != ColorModel.YCCK) {
        return is;
    }

    // Get an ImageReader.
    ImageInputStream input = ImageIO.createImageInputStream(is);

    try {
        Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
        if (readers == null || !readers.hasNext()) {
            throw new UnsupportedImageException("No ImageReaders found");
        }

        ImageReader reader = readers.next();
        reader.setInput(input);
        Raster raster = reader.readRaster(0, reader.getDefaultReadParam());

        int w = raster.getWidth();
        int h = raster.getHeight();
        byte[] rgb = new byte[w * h * 3];

        switch (colorModel) {
        case YCCK: {
            float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
            float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
            float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
            float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);

            for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
                float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];

                double val = y + 1.402 * (cr - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);

                val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);

                val = y + 1.772 * (cb - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
            }
            break;
        }
        case CMYK: {
            int[] C = raster.getSamples(0, 0, w, h, 0, (int[]) null);
            int[] M = raster.getSamples(0, 0, w, h, 1, (int[]) null);
            int[] Y = raster.getSamples(0, 0, w, h, 2, (int[]) null);
            int[] K = raster.getSamples(0, 0, w, h, 3, (int[]) null);

            for (int i = 0, imax = C.length, base = 0; i < imax; i++, base += 3) {
                int c = 255 - C[i];
                int m = 255 - M[i];
                int y = 255 - Y[i];
                int k = 255 - K[i];
                float kk = k / 255f;

                rgb[base] = (byte) (255 - Math.min(255f, c * kk + k));
                rgb[base + 1] = (byte) (255 - Math.min(255f, m * kk + k));
                rgb[base + 2] = (byte) (255 - Math.min(255f, y * kk + k));
            }
            break;
        }
        }

        // from other image types we know InterleavedRaster's can be
        // manipulated by AffineTransformOp, so create one of those.
        raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3,
                new int[] { 0, 1, 2 }, null);

        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        java.awt.image.ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE,
                DataBuffer.TYPE_BYTE);
        BufferedImage convertedImage = new BufferedImage(cm, (WritableRaster) raster, true, null);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(convertedImage, "jpg", os);

        return new ByteArrayInputStream(os.toByteArray());
    } finally {
        IOUtils.closeQuietly(is);
        if (input != null) {
            input.close();
        }
    }
}

From source file:TextureByReference.java

public static BufferedImage convertToCustomRGBA(BufferedImage bImage) {
    if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
        ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
    }/*from  w ww .j a  va  2s . c om*/

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

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8, 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0);
    int[] bandOffset = { 0, 1, 2, 3 };

    WritableRaster newRaster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4,
            bandOffset, null);
    byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer()).getData();
    Raster origRaster = bImage.getData();
    int[] pixel = new int[4];
    int k = 0;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            pixel = origRaster.getPixel(i, j, pixel);
            byteData[k++] = (byte) (pixel[0]);
            byteData[k++] = (byte) (pixel[1]);
            byteData[k++] = (byte) (pixel[2]);
            byteData[k++] = (byte) (pixel[3]);
        }
    }
    BufferedImage newImage = new BufferedImage(cm, newRaster, false, null);
    //  if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
    //    System.out.println("Type is custom");
    //  }
    return newImage;
}

From source file:TextureByReference.java

public static BufferedImage convertToCustomRGB(BufferedImage bImage) {
    if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
        ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
    }//from  w  w w  . j  av  a  2 s  .c om

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

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, 0);
    int[] bandOffset = { 0, 1, 2 };

    WritableRaster newRaster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 3, 3,
            bandOffset, null);
    byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer()).getData();
    Raster origRaster = bImage.getData();
    int[] pixel = new int[4];
    int k = 0;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            pixel = origRaster.getPixel(i, j, pixel);
            byteData[k++] = (byte) (pixel[0]);
            byteData[k++] = (byte) (pixel[1]);
            byteData[k++] = (byte) (pixel[2]);
        }
    }
    BufferedImage newImage = new BufferedImage(cm, newRaster, false, null);
    //  if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
    //    System.out.println("Type is custom");
    //  }
    return newImage;
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

private WritableRaster createRaster(final int width, final int height, final int bands,
        final int scanlineStride, final int bitDepth) {

    DataBuffer dataBuffer;/*ww  w  . ja  va  2  s . c  o  m*/
    WritableRaster ras = null;
    final Point origin = new Point(0, 0);
    if (bitDepth < 8 && bands == 1) {
        dataBuffer = new DataBufferByte(height * scanlineStride);
        ras = Raster.createPackedRaster(dataBuffer, width, height, bitDepth, origin);
    } else if (bitDepth <= 8) {
        dataBuffer = new DataBufferByte(height * scanlineStride);
        ras = Raster.createInterleavedRaster(dataBuffer, width, height, scanlineStride, bands,
                this.bandOffsets[bands], origin);
    } else {
        dataBuffer = new DataBufferUShort(height * scanlineStride);
        ras = Raster.createInterleavedRaster(dataBuffer, width, height, scanlineStride, bands,
                this.bandOffsets[bands], origin);
    }

    return ras;
}