List of usage examples for java.awt.image BufferedImage getRaster
public WritableRaster getRaster()
From source file:net.bioclipse.chart.ChartUtils.java
/** * Utility method for converting JFreeChart to an image * @param parent used for color correction * @param chart the chart to be made into an image * @param width image width/*from ww w . j a v a2s .co m*/ * @param height image height * @return SWT Image of the chart */ public static Image createChartImage(Composite parent, JFreeChart chart, int width, int height) { // Color adjustment Color swtBackground = parent.getBackground(); java.awt.Color awtBackground = new java.awt.Color(swtBackground.getRed(), swtBackground.getGreen(), swtBackground.getRed()); chart.setBackgroundPaint(awtBackground); // Draw the chart in an AWT buffered image BufferedImage bufferedImage = chart.createBufferedImage(width, height, null); // Get the data buffer of the image DataBuffer buffer = bufferedImage.getRaster().getDataBuffer(); DataBufferInt intBuffer = (DataBufferInt) buffer; // Copy the data from the AWT buffer to a SWT buffer PaletteData paletteData = new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF); ImageData imageData = new ImageData(width, height, 32, paletteData); for (int bank = 0; bank < intBuffer.getNumBanks(); bank++) { int[] bankData = intBuffer.getData(bank); imageData.setPixels(0, bank, bankData.length, bankData, 0); } // Create an SWT image return new Image(parent.getDisplay(), imageData); }
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 ava2 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
/** * 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 av 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:VASSAL.tools.image.tilecache.TileUtils.java
/** * Reads an image tile./*from ww w .j a va 2 s . c om*/ * * @param in a stream containing the tile data * @return the tile image * * @throws IOException if the read fails */ public static BufferedImage read(InputStream in) throws IOException { ByteBuffer bb; // read the header final byte[] header = readHeader(in); bb = ByteBuffer.wrap(header); // validate the signature final byte[] sig = new byte[6]; bb.get(sig); checkSignature(sig); // get the dimensions and type final int w = bb.getInt(); final int h = bb.getInt(); final int type = bb.getInt(); // read the image data final byte[] cdata = IOUtils.toByteArray(in); // decompress the image data InputStream zin = null; try { zin = new GZIPInputStream(new ByteArrayInputStream(cdata)); bb = ByteBuffer.wrap(IOUtils.toByteArray(zin)); zin.close(); } finally { IOUtils.closeQuietly(zin); } // build the image final BufferedImage img = new BufferedImage(w, h, type); // FIXME: This might decelerate the image? If so, then we should // make a copy. final DataBufferInt db = (DataBufferInt) img.getRaster().getDataBuffer(); final int[] data = db.getData(); final IntBuffer ib = bb.asIntBuffer(); ib.get(data); /* if (ib.hasRemaining()) { // buffer contains garbage at the end! throw new IOException("found " + (4*ib.remaining()) + " more bytes!"); } */ return img; }
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)); }/*w ww .j ava2s.c om*/ } } 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:app.utils.ImageUtilities.java
public static Image readImageFromFile(File imgFile) throws IOException { String imgName = imgFile.getName(); BufferedImage img = ImageIO.read(imgFile); Pixel[][] pixels = new Pixel[img.getWidth()][img.getHeight()]; for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { int redValue = new Color(img.getRGB(x, y)).getRed(); int greenValue = new Color(img.getRGB(x, y)).getGreen(); int blueValue = new Color(img.getRGB(x, y)).getBlue(); pixels[x][y] = new Pixel(redValue, greenValue, blueValue); }/*from w w w. java 2s.c o m*/ } return new Image(imgName, pixels, img.getRaster().getNumDataElements()); }
From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java
/** * Utility method for creating a BufferedImage from a source raster Currently only Float->Byte and Byte->Byte are supported. Will throw an * {@link UnsupportedOperationException} if the conversion is not supported. * /*from w w w . j av a2 s. c o m*/ * @param raster * @param imageType * @return */ public static BufferedImage rasterToBufferedImage(Raster raster, ImageTypeSpecifier imageType) { if (imageType == null) { if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) imageType = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false); else throw new IllegalArgumentException("unable to dynamically determine the imageType"); } // create a new buffered image, for display BufferedImage bufImage = imageType.createBufferedImage(raster.getWidth(), raster.getHeight()); if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) { // convert short pixels to bytes short[] shortData = ((DataBufferUShort) raster.getDataBuffer()).getData(); byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData(); ImageIOUtils.shortToByteBuffer(shortData, byteData, 1, raster.getNumBands()); } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_FLOAT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) { // convert float pixels to bytes float[] floatData = ((DataBufferFloat) raster.getDataBuffer()).getData(); byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData(); ImageIOUtils.floatToByteBuffer(floatData, byteData, 1, raster.getNumBands()); } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_DOUBLE && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) { // convert double pixels to bytes double[] doubleData = ((DataBufferDouble) raster.getDataBuffer()).getData(); byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData(); ImageIOUtils.doubleToByteBuffer(doubleData, byteData, 1, raster.getNumBands()); } else if ((raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT) || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT)) { bufImage.setData(raster); } else { throw new UnsupportedOperationException( "Unable to convert raster type to bufferedImage type: " + raster.getDataBuffer().getDataType() + " ==> " + bufImage.getRaster().getDataBuffer().getDataType()); } return bufImage; }
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); }/*w ww .j av a2 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; }
From source file:org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java
private static BufferedImage applyColorKeyMask(BufferedImage image, BufferedImage mask) throws IOException { int width = image.getWidth(); int height = image.getHeight(); // compose to ARGB BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); WritableRaster src = image.getRaster(); WritableRaster dest = masked.getRaster(); WritableRaster alpha = mask.getRaster(); float[] rgb = new float[3]; float[] rgba = new float[4]; float[] alphaPixel = null; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { src.getPixel(x, y, rgb);/* ww w .j a va2 s.c om*/ rgba[0] = rgb[0]; rgba[1] = rgb[1]; rgba[2] = rgb[2]; alphaPixel = alpha.getPixel(x, y, alphaPixel); rgba[3] = 255 - alphaPixel[0]; dest.setPixel(x, y, rgba); } } return masked; }
From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java
/** * scales bands to min = 0 & max = 2^8 or 2^16 * * @param image/* 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; }