Example usage for java.awt.color ColorSpace CS_sRGB

List of usage examples for java.awt.color ColorSpace CS_sRGB

Introduction

In this page you can find the example usage for java.awt.color ColorSpace CS_sRGB.

Prototype

int CS_sRGB

To view the source code for java.awt.color ColorSpace CS_sRGB.

Click Source Link

Document

The sRGB color space defined at <a href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html"> http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html</a>.

Usage

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  a  v  a 2s.co  m

    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;
}