Example usage for java.awt.image WritableRaster setSample

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

Introduction

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

Prototype

public void setSample(int x, int y, int b, double s) 

Source Link

Document

Sets a sample in the specified band for the pixel located at (x,y) in the DataBuffer using a double for input.

Usage

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage rgb2gray(BufferedImage img) {
    if (img.getType() == BufferedImage.TYPE_INT_ARGB || img.getType() == BufferedImage.TYPE_INT_RGB) {
        int w, h;
        w = img.getWidth();// w ww  .j  a  v  a 2s. c  o m
        h = img.getHeight();
        BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

        //----------------------------v3.0----------------------
        ColorModel cm = img.getColorModel();
        int pixel, gr;
        int r, g, b;
        WritableRaster raster = out.getRaster();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                gr = (int) Math.round(((double) r + (double) g + (double) b) / 3.0);
                raster.setSample(x, y, 0, gr);
            }
        }
        return out;
    }
    return img;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage combineRGBA(BufferedImage[] inImgs) {
    BufferedImage out = null;/*from ww  w .jav  a  2s.  c  o m*/
    int numBands = 0;
    int width = 0;
    int height = 0;
    if (inImgs == null) {
        return null;
    }

    if (inImgs.length != 4 && inImgs.length != 3) {
        return null;
    }

    if (inImgs.length == 3) {
        numBands = 3;
    } else if (inImgs.length == 4) {
        if (inImgs[3] == null) {
            numBands = 3;
        } else {
            numBands = 4;
        }
    }

    for (int i = 0; i < numBands; i++) {
        if (inImgs[i] == null) {
            return null;
        }
    }

    //check if all images are the same size
    width = inImgs[0].getWidth();
    height = inImgs[0].getHeight();
    for (int i = 1; i < numBands; i++) {
        if (inImgs[i].getWidth() != width || inImgs[i].getHeight() != height) {
            return null;
        }
    }

    //create output image
    if (numBands == 3) {
        out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    } else {
        out = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }

    //set pixels
    WritableRaster outRaster = out.getRaster();

    WritableRaster[] inRasters = new WritableRaster[numBands];
    for (int i = 0; i < numBands; i++) {
        inRasters[i] = inImgs[i].getRaster();
    }

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            for (int i = 0; i < numBands; i++) {
                outRaster.setSample(x, y, i, inRasters[i].getSample(x, y, 0));
            }
        }
    }
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage invert(BufferedImage inImg) {
    if (inImg == null) {
        return null;
    }/*from ww w .  jav a2s.  co m*/

    int width = inImg.getWidth();
    int height = inImg.getHeight();
    BufferedImage outImg = new BufferedImage(width, height, inImg.getType());
    WritableRaster outRaster = outImg.getRaster();
    WritableRaster inRaster = inImg.getRaster();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            for (int i = 0; i < outRaster.getNumBands(); i++) {
                outRaster.setSample(x, y, i, 255 - inRaster.getSample(x, y, i));
            }
        }
    }

    return outImg;
}