Example usage for java.awt.image WritableRaster getNumBands

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

Introduction

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

Prototype

public final int getNumBands() 

Source Link

Document

Returns the number of bands (samples per pixel) in this Raster.

Usage

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Calculates and returns band histograms of a BufferedImage
 *
 * @param image//  w w w. j a  va  2 s.c o m
 * @return
 */
public static int[][] getChannelHistograms(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    int[][] histograms = new int[raster
            .getNumBands()][(int) (Math.pow(2, image.getColorModel().getPixelSize()) - 1)];

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

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                histograms[band][raster.getSample(col, row, band)]++;
            }
        }
    }

    return histograms;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

public static int[] getPercentileIntensity(BufferedImage image, double percentile) {
    WritableRaster raster = image.getRaster();
    int[] intensities = new int[raster.getNumBands()];

    DescriptiveStatistics[] ds = new DescriptiveStatistics[raster.getNumBands()];
    for (int i = 0; i < raster.getNumBands(); i++)
        ds[i] = new DescriptiveStatistics();

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

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                ds[band].addValue(raster.getSample(col, row, band));
            }/*from w  w  w . j  a  v  a2  s .  c o  m*/
        }
    }

    for (int i = 0; i < ds.length; i++) {
        if (percentile == 0d)
            intensities[i] = (int) ds[i].getMin();
        else if (percentile == 100d)
            intensities[i] = (int) ds[i].getMax();
        else
            intensities[i] = (int) ds[i].getPercentile(percentile);
    }
    return intensities;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Calculates and returns band histograms of a BufferedImage w/o a need to extract the raster
 *
 * @param raster//from   w  w w. j  av  a2 s  .c  o  m
 * @param width
 * @param height
 * @param iNumBins
 * @return
 */
public static int[][] getChannelHistograms(WritableRaster raster, int width, int height, int iNumBins) {

    int[][] histograms = new int[raster.getNumBands()][iNumBins];

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                histograms[band][raster.getSample(col, row, band)]++;
            }
        }
    }

    return histograms;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * scales bands to min = 0 & max = 2^8 or 2^16
 *
 * @param image//from   w  w  w . jav a 2s  . co m
 * @param min   int[] containing minima per band
 * @param max
 * @return
 */
public static BufferedImage scaleIntensities(BufferedImage image, int[] min, int[] max)
        throws IllegalArgumentException {
    WritableRaster raster = image.getRaster();

    if ((min.length != max.length) || min.length != raster.getNumBands())
        throw new IllegalArgumentException(
                "Please ensure consistency of min and max arrays and number of bands in image");

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

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                int pixel = raster.getSample(col, row, band);
                if (pixel < min[band])
                    pixel = min[band];
                if (pixel > max[band])
                    pixel = max[band];
                pixel = ((int) (((((double) pixel - min[band]) / (max[band] - min[band]))
                        * ((int) Math.pow(2, image.getColorModel().getPixelSize()) - 1)) + 0.5));
                raster.setSample(col, row, band, pixel);
            }
        }
    }

    return image;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Returns min and max intensities and percentiles 0.01, 0.05, 0.1, 0.9, 0.95, 0.99 of a BufferedImage.
 *
 * @param image/*  w  w w.  j a va  2s .co  m*/
 * @return
 */
public static int[][] getMinMaxIntensitiesOfBI(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    // per band: { min, 1%, 5%, 10%, 90%, 95%, 99%, max }
    int[][] intensities = new int[raster.getNumBands()][8];

    DescriptiveStatistics[] ds = new DescriptiveStatistics[raster.getNumBands()];
    for (int i = 0; i < raster.getNumBands(); i++)
        ds[i] = new DescriptiveStatistics();

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

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                ds[band].addValue(raster.getSample(col, row, band));
            }
        }
    }

    for (int i = 0; i < ds.length; i++) {
        intensities[i][0] = (int) ds[i].getMin();
        intensities[i][1] = (int) ds[i].getPercentile(1);
        intensities[i][2] = (int) ds[i].getPercentile(5);
        intensities[i][3] = (int) ds[i].getPercentile(10);
        intensities[i][4] = (int) ds[i].getPercentile(90);
        intensities[i][5] = (int) ds[i].getPercentile(95);
        intensities[i][6] = (int) ds[i].getPercentile(99);
        intensities[i][7] = (int) ds[i].getMax();
    }
    return intensities;
}

From source file:GraphicsUtil.java

public static void multiplyAlpha(WritableRaster wr) {
    if (is_BYTE_COMP_Data(wr.getSampleModel()))
        mult_BYTE_COMP_Data(wr);/*  w  w  w  .j  a  v a  2  s.  c om*/
    else if (is_INT_PACK_Data(wr.getSampleModel(), true))
        mult_INT_PACK_Data(wr);
    else {
        int[] pixel = null;
        int bands = wr.getNumBands();
        float norm = 1f / 255f;
        int x0, x1, y0, y1, a, b;
        float alpha;
        x0 = wr.getMinX();
        x1 = x0 + wr.getWidth();
        y0 = wr.getMinY();
        y1 = y0 + wr.getHeight();
        for (int y = y0; y < y1; y++)
            for (int x = x0; x < x1; x++) {
                pixel = wr.getPixel(x, y, pixel);
                a = pixel[bands - 1];
                if ((a >= 0) && (a < 255)) {
                    alpha = a * norm;
                    for (b = 0; b < bands - 1; b++)
                        pixel[b] = (int) (pixel[b] * alpha + 0.5f);
                    wr.setPixel(x, y, pixel);
                }
            }
    }
}

From source file:GraphicsUtil.java

public static void divideAlpha(WritableRaster wr) {
    if (is_BYTE_COMP_Data(wr.getSampleModel()))
        divide_BYTE_COMP_Data(wr);
    else if (is_INT_PACK_Data(wr.getSampleModel(), true))
        divide_INT_PACK_Data(wr);
    else {//  ww w .  ja  v  a  2s.c om
        int x0, x1, y0, y1, a, b;
        float ialpha;
        int bands = wr.getNumBands();
        int[] pixel = null;

        x0 = wr.getMinX();
        x1 = x0 + wr.getWidth();
        y0 = wr.getMinY();
        y1 = y0 + wr.getHeight();
        for (int y = y0; y < y1; y++)
            for (int x = x0; x < x1; x++) {
                pixel = wr.getPixel(x, y, pixel);
                a = pixel[bands - 1];
                if ((a > 0) && (a < 255)) {
                    ialpha = 255 / (float) a;
                    for (b = 0; b < bands - 1; b++)
                        pixel[b] = (int) (pixel[b] * ialpha + 0.5f);
                    wr.setPixel(x, y, pixel);
                }
            }
    }
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Can be used to verify impact of different encoding algorithms. Compares two BufferedImages and outputs differences between those.
 *
 * @param image/*ww  w. j a  v  a2s. c  om*/
 * @param image2
 * @return
 */
public static int[] calculateBufferedImageDifferencesPxByPx(BufferedImage image, BufferedImage image2) {
    int width = image.getWidth();
    int height = image.getHeight();

    int width2 = image2.getWidth();
    int height2 = image2.getHeight();

    WritableRaster raster1 = image.getRaster();
    WritableRaster raster2 = image2.getRaster();

    if (width != width2 || height != height2)
        throw new IllegalArgumentException(
                "Please insert two identical images that were treated with different image algorithms");

    int[] diff = new int[width * height];

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster1.getNumBands(); band++) {
                int p1 = raster1.getSample(col, row, band);
                int p2 = raster2.getSample(col, row, band);

                diff[((row * width) + col)] = p2 - p1;
            }
        }
    }

    return diff;
}

From source file:GraphicsUtil.java

/**
 * Copies data from one bufferedImage to another paying attention
 * to the state of AlphaPreMultiplied./*www.j a v a  2  s.  c  o  m*/
 *
 * @param src The source
 * @param srcRect The Rectangle of source data to be copied
 * @param dst The destination
 * @param destP The Place for the upper left corner of srcRect in dst.
 */
public static void copyData(BufferedImage src, Rectangle srcRect, BufferedImage dst, Point destP) {

    /*
     if (srcCS != dstCS)
    throw new IllegalArgumentException
        ("Images must be in the same ColorSpace in order "+
         "to copy Data between them");
     */
    boolean srcAlpha = src.getColorModel().hasAlpha();
    boolean dstAlpha = dst.getColorModel().hasAlpha();

    // System.out.println("Src has: " + srcAlpha +
    //                    " is: " + src.isAlphaPremultiplied());
    //
    // System.out.println("Dst has: " + dstAlpha +
    //                    " is: " + dst.isAlphaPremultiplied());

    if (srcAlpha == dstAlpha)
        if (!srcAlpha || src.isAlphaPremultiplied() == dst.isAlphaPremultiplied()) {
            // They match one another so just copy everything...
            copyData(src.getRaster(), dst.getRaster());
            return;
        }

    // System.out.println("Using Slow CopyData");

    int[] pixel = null;
    Raster srcR = src.getRaster();
    WritableRaster dstR = dst.getRaster();
    int bands = dstR.getNumBands();

    int dx = destP.x - srcRect.x;
    int dy = destP.y - srcRect.y;

    int w = srcRect.width;
    int x0 = srcRect.x;
    int y0 = srcRect.y;
    int y1 = y0 + srcRect.height - 1;

    if (!srcAlpha) {
        // Src has no alpha dest does so set alpha to 1.0 everywhere.
        // System.out.println("Add Alpha");
        int[] oPix = new int[bands * w];
        int out = (w * bands) - 1; // The 2 skips alpha channel
        while (out >= 0) {
            // Fill alpha channel with 255's
            oPix[out] = 255;
            out -= bands;
        }

        int b, in;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = w * (bands - 1) - 1;
            out = (w * bands) - 2; // The 2 skips alpha channel on last pix
            switch (bands) {
            case 4:
                while (in >= 0) {
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    out--;
                }
                break;
            default:
                while (in >= 0) {
                    for (b = 0; b < bands - 1; b++)
                        oPix[out--] = pixel[in--];
                    out--;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else if (dstAlpha && dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to multiply it for dst.
        // System.out.println("Mult Case");
        int a, b, alpha, in, fpNorm = (1 << 24) / 255, pt5 = 1 << 23;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = bands * w - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= 4;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= bands;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (dstAlpha && !dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to divide it out for dst.
        // System.out.println("Div Case");
        int a, b, ialpha, in, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands * w) - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= 4;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= bands;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (src.isAlphaPremultiplied()) {
        int[] oPix = new int[bands * w];
        // Src has alpha dest does not so unpremult and store...
        // System.out.println("Remove Alpha, Div Case");
        int a, b, ialpha, in, out, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands + 1) * w - 1;
            out = (bands * w) - 1;
            while (in >= 0) {
                a = pixel[in];
                in--;
                if (a > 0) {
                    if (a < 255) {
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands; b++)
                            oPix[out--] = (pixel[in--] * ialpha + pt5) >>> 16;
                    } else
                        for (b = 0; b < bands; b++)
                            oPix[out--] = pixel[in--];
                } else {
                    in -= bands;
                    for (b = 0; b < bands; b++)
                        oPix[out--] = 255;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else {
        // Src has unpremult alpha, dest does not have alpha,
        // just copy the color channels over.
        Rectangle dstRect = new Rectangle(destP.x, destP.y, srcRect.width, srcRect.height);
        for (int b = 0; b < bands; b++)
            copyBand(srcR, srcRect, b, dstR, dstRect, b);
    }
}

From source file:org.polymap.styler.helper.ImageHelper.java

public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[raster.getNumBands()];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }// www  .  j  av  a 2  s. co  m
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}