List of usage examples for java.awt.image BufferedImage getWritableTile
public WritableRaster getWritableTile(int tileX, int tileY)
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 a v a2s.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:lucee.runtime.img.Image.java
private static BufferedImage addAlpha(BufferedImage src, BufferedImage alpha, int x, int y) { int w = src.getWidth(); int h = src.getHeight(); BufferedImage bi = new BufferedImage(w, h, 2); WritableRaster wr = bi.getWritableTile(0, 0); WritableRaster wr3 = wr.createWritableChild(0, 0, w, h, 0, 0, new int[] { 0, 1, 2 }); WritableRaster wr1 = wr.createWritableChild(0, 0, w, h, 0, 0, new int[] { 3 }); wr3.setRect(src.getData());/*from ww w .ja va2 s . co m*/ wr1.setRect(alpha.getData()); bi.releaseWritableTile(0, 0); return bi; }
From source file:ch5ImageReader.java
/** * read in the input image specified by index imageIndex using the * parameters specified by the ImageReadParam object param *///www . jav a2s . c o m public BufferedImage read(int imageIndex, ImageReadParam param) { checkIndex(imageIndex); if (isSeekForwardOnly()) minIndex = imageIndex; else minIndex = 0; BufferedImage bimage = null; WritableRaster raster = null; /* * this method sets the image metadata so that we can use the getWidth * and getHeight methods */ setImageMetadata(iis, imageIndex); int srcWidth = getWidth(imageIndex); int srcHeight = getHeight(imageIndex); // initialize values to -1 int dstWidth = -1; int dstHeight = -1; int srcRegionWidth = -1; int srcRegionHeight = -1; int srcRegionXOffset = -1; int srcRegionYOffset = -1; int xSubsamplingFactor = -1; int ySubsamplingFactor = -1; if (param == null) param = getDefaultReadParam(); Iterator imageTypes = getImageTypes(imageIndex); try { /* * get the destination BufferedImage which will be filled using the * input image's pixel data */ bimage = getDestination(param, imageTypes, srcWidth, srcHeight); /* * get Rectangle object which will be used to clip the source * image's dimensions. */ Rectangle srcRegion = param.getSourceRegion(); if (srcRegion != null) { srcRegionWidth = (int) srcRegion.getWidth(); srcRegionHeight = (int) srcRegion.getHeight(); srcRegionXOffset = (int) srcRegion.getX(); srcRegionYOffset = (int) srcRegion.getY(); /* * correct for overextended source regions */ if (srcRegionXOffset + srcRegionWidth > srcWidth) dstWidth = srcWidth - srcRegionXOffset; else dstWidth = srcRegionWidth; if (srcRegionYOffset + srcRegionHeight > srcHeight) dstHeight = srcHeight - srcRegionYOffset; else dstHeight = srcRegionHeight; } else { dstWidth = srcWidth; dstHeight = srcHeight; srcRegionXOffset = srcRegionYOffset = 0; } /* * get subsampling factors */ xSubsamplingFactor = param.getSourceXSubsampling(); ySubsamplingFactor = param.getSourceYSubsampling(); /** * dstWidth and dstHeight should be equal to bimage.getWidth() and * bimage.getHeight() after these next two instructions */ dstWidth = (dstWidth - 1) / xSubsamplingFactor + 1; dstHeight = (dstHeight - 1) / ySubsamplingFactor + 1; } catch (IIOException e) { System.err.println("Can't create destination BufferedImage"); } raster = bimage.getWritableTile(0, 0); /* * using the parameters specified by the ImageReadParam object, read the * image image data into the destination BufferedImage */ byte[] srcBuffer = new byte[srcWidth]; byte[] dstBuffer = new byte[dstWidth]; int jj; int index; try { for (int j = 0; j < srcHeight; j++) { iis.readFully(srcBuffer, 0, srcWidth); jj = j - srcRegionYOffset; if (jj % ySubsamplingFactor == 0) { jj /= ySubsamplingFactor; if ((jj >= 0) && (jj < dstHeight)) { for (int i = 0; i < dstWidth; i++) { index = srcRegionXOffset + i * xSubsamplingFactor; dstBuffer[i] = srcBuffer[index]; } raster.setDataElements(0, jj, dstWidth, 1, dstBuffer); } } } } catch (IOException e) { bimage = null; } return bimage; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage addAlpha(BufferedImage src, BufferedImage alpha) { int w = src.getWidth(); int h = src.getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); WritableRaster wr = bi.getWritableTile(0, 0); WritableRaster wr3 = wr.createWritableChild(0, 0, w, h, 0, 0, new int[] { 0, 1, 2 }); WritableRaster wr1 = wr.createWritableChild(0, 0, w, h, 0, 0, new int[] { 3 }); wr3.setRect(src.getData());/* w w w.j a v a 2 s . c om*/ wr1.setRect(alpha.getData()); bi.releaseWritableTile(0, 0); return bi; }