List of usage examples for java.awt.image WritableRaster getSample
public int getSample(int x, int y, int b)
From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java
public static BufferedImage makeTransparent(BufferedImage image, int x, int y) { ColorModel cm = image.getColorModel(); if (!(cm instanceof IndexColorModel)) return image; // sorry... IndexColorModel icm = (IndexColorModel) cm; WritableRaster raster = image.getRaster(); int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's // palette/* w ww .java 2 s . c o m*/ int size = icm.getMapSize(); byte[] reds = new byte[size]; byte[] greens = new byte[size]; byte[] blues = new byte[size]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel); return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null); }
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/*w w w .jav 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
/** * Calculates and returns band histograms of a BufferedImage * * @param image//from w w w . j a v a 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
/** * scales bands to min = 0 & max = 2^8 or 2^16 * * @param image/*from ww w . jav a 2 s . 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
/** * Can be used to verify impact of different encoding algorithms. Compares two BufferedImages and outputs differences between those. * * @param image//from w ww . j ava 2 s . co m * @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: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)); }// www.ja va 2s. 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:net.d53dev.dslfy.web.service.GifService.java
public BufferedImage convertRGBAToGIF(BufferedImage src, int transColor) { BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); Graphics g = dst.getGraphics(); g.setColor(new Color(transColor)); g.fillRect(0, 0, dst.getWidth(), dst.getHeight()); {// w w w. j a va 2 s . c o m IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel(); WritableRaster raster = dst.getRaster(); int sample = raster.getSample(0, 0, 0); int size = indexedModel.getMapSize(); byte[] rr = new byte[size]; byte[] gg = new byte[size]; byte[] bb = new byte[size]; indexedModel.getReds(rr); indexedModel.getGreens(gg); indexedModel.getBlues(bb); IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample); dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null); } dst.createGraphics().drawImage(src, 0, 0, null); return dst; }
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 v a 2 s . 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:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static RegularField bufferedImage2RegularField(BufferedImage inImage, boolean vFlip) { if (inImage == null) { return null; }/* ww w . j a v a 2 s . c om*/ int[] dims = new int[2]; dims[0] = inImage.getWidth(); dims[1] = inImage.getHeight(); RegularField field = new RegularField(dims); WritableRaster raster = inImage.getRaster(); byte[][] samples = null; int[][] samples32 = null; int i = 0; switch (inImage.getType()) { case BufferedImage.TYPE_BYTE_GRAY: samples = new byte[1][]; samples[0] = new byte[dims[0] * dims[1]]; if (vFlip) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i++] = (byte) raster.getSample(x, dims[1] - y - 1, 0); } } } else { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i++] = (byte) raster.getSample(x, y, 0); } } } field.addData(DataArray.create(samples[0], 1, "grayscaleData")); break; case BufferedImage.TYPE_USHORT_GRAY: samples32 = new int[1][]; samples32[0] = new int[dims[0] * dims[1]]; if (vFlip) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples32[0][i++] = (int) raster.getSample(x, dims[1] - y - 1, 0); } } } else { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples32[0][i++] = (int) raster.getSample(x, y, 0); } } } field.addData(DataArray.create(samples32[0], 1, "grayscaleData")); break; case BufferedImage.TYPE_INT_RGB: samples = new byte[3][]; samples[0] = new byte[dims[0] * dims[1]]; samples[1] = new byte[dims[0] * dims[1]]; samples[2] = new byte[dims[0] * dims[1]]; if (vFlip) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0); samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1); samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2); i++; } } } else { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, y, 0); samples[1][i] = (byte) raster.getSample(x, y, 1); samples[2][i] = (byte) raster.getSample(x, y, 2); i++; } } } field.addData(DataArray.create(samples[0], 1, "redData")); field.addData(DataArray.create(samples[1], 1, "greenData")); field.addData(DataArray.create(samples[2], 1, "blueData")); break; case BufferedImage.TYPE_3BYTE_BGR: case BufferedImage.TYPE_INT_BGR: samples = new byte[3][]; samples[0] = new byte[dims[0] * dims[1]]; samples[1] = new byte[dims[0] * dims[1]]; samples[2] = new byte[dims[0] * dims[1]]; if (vFlip) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0); samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1); samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2); i++; } } } else { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, y, 0); samples[1][i] = (byte) raster.getSample(x, y, 1); samples[2][i] = (byte) raster.getSample(x, y, 2); i++; } } } field.addData(DataArray.create(samples[0], 1, "blueData")); field.addData(DataArray.create(samples[1], 1, "greenData")); field.addData(DataArray.create(samples[2], 1, "redData")); break; case BufferedImage.TYPE_INT_ARGB: samples = new byte[4][]; samples[0] = new byte[dims[0] * dims[1]]; samples[1] = new byte[dims[0] * dims[1]]; samples[2] = new byte[dims[0] * dims[1]]; samples[3] = new byte[dims[0] * dims[1]]; for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0); samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1); samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2); samples[3][i] = (byte) raster.getSample(x, dims[1] - y - 1, 3); i++; } } field.addData(DataArray.create(samples[0], 1, "redData")); field.addData(DataArray.create(samples[1], 1, "greenData")); field.addData(DataArray.create(samples[2], 1, "blueData")); field.addData(DataArray.create(samples[3], 1, "alphaData")); break; case BufferedImage.TYPE_4BYTE_ABGR: samples = new byte[4][]; samples[0] = new byte[dims[0] * dims[1]]; samples[1] = new byte[dims[0] * dims[1]]; samples[2] = new byte[dims[0] * dims[1]]; samples[3] = new byte[dims[0] * dims[1]]; for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0); samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1); samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2); samples[3][i] = (byte) raster.getSample(x, dims[1] - y - 1, 3); i++; } } field.addData(DataArray.create(samples[0], 1, "alphaData")); field.addData(DataArray.create(samples[1], 1, "redData")); field.addData(DataArray.create(samples[2], 1, "greenData")); field.addData(DataArray.create(samples[3], 1, "blueData")); break; default: BufferedImage newImg = new BufferedImage(inImage.getWidth(), inImage.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = newImg.createGraphics(); g2d.drawImage(inImage, null, 0, 0); g2d.dispose(); raster = newImg.getRaster(); samples = new byte[3][]; samples[0] = new byte[dims[0] * dims[1]]; samples[1] = new byte[dims[0] * dims[1]]; samples[2] = new byte[dims[0] * dims[1]]; if (vFlip) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0); samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1); samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2); i++; } } } else { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { samples[0][i] = (byte) raster.getSample(x, y, 0); samples[1][i] = (byte) raster.getSample(x, y, 1); samples[2][i] = (byte) raster.getSample(x, y, 2); i++; } } } field.addData(DataArray.create(samples[0], 1, "redData")); field.addData(DataArray.create(samples[1], 1, "greenData")); field.addData(DataArray.create(samples[2], 1, "blueData")); } float[][] affine = new float[4][3]; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { affine[j][k] = 0.0f; if (j == k) affine[j][k] = 1.0f; } } affine[3][0] = -(float) dims[0] / 2.0f; affine[3][1] = -(float) dims[1] / 2.0f; affine[3][2] = 0.0f; field.setAffine(affine); return field; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage invert(BufferedImage inImg) { if (inImg == null) { return null; }/* w ww. j av a2 s . c om*/ 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; }