List of usage examples for java.awt.image RenderedImage getData
Raster getData();
From source file:GraphicsUtil.java
/** * Extracts an alpha raster from a RenderedImage. The method tries to avoid copying data * unnecessarily by checking if the RenderedImage is a BufferedImage which offers suitable * direct methods./* w w w .j ava2s . c o m*/ * @param image the image * @return the alpha raster */ public static Raster getAlphaRaster(RenderedImage image) { ColorModel cm = image.getColorModel(); if (!cm.hasAlpha() || cm.getTransparency() != ColorModel.TRANSLUCENT) { throw new IllegalStateException("Image doesn't have an alpha channel"); } Raster alpha; if (image instanceof BufferedImage) { //Optimization possible with BufferedImage (No copying) alpha = ((BufferedImage) image).getAlphaRaster(); } else { WritableRaster wraster = GraphicsUtil.makeRasterWritable(image.getData()); alpha = image.getColorModel().getAlphaRaster(wraster); } return alpha; }
From source file:com.alibaba.simpleimage.util.ImageLog.java
protected String formatMsg(Object action, RenderedImage img) { String msgFmt = action + ": {0}={1} DataType={2} Width={3} Height={4}"; String clazz = img.getColorModel().getColorSpace().getClass().getSimpleName(); int type = img.getColorModel().getColorSpace().getType(); int dataType = img.getData().getDataBuffer().getDataType(); int w = img.getWidth(); int h = img.getHeight(); String msg = MessageFormat.format(msgFmt, clazz, getColorSpaceName(type), getDataTypeName(dataType), w, h); return msg;//from w w w .ja v a 2 s . c o m }
From source file:org.geomajas.plugin.rasterizing.layer.RasterDirectLayer.java
/** * Converts an image to a RGBA direct color model using a workaround via buffered image directly calling the * ColorConvert operation fails for unknown reasons ?! * //w w w. j a v a2 s .c om * @param img image to convert * @return converted image */ public PlanarImage toDirectColorModel(RenderedImage img) { BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); BufferedImage source = new BufferedImage(img.getColorModel(), (WritableRaster) img.getData(), img.getColorModel().isAlphaPremultiplied(), null); ColorConvertOp op = new ColorConvertOp(null); op.filter(source, dest); return PlanarImage.wrapRenderedImage(dest); }
From source file:it.geosolutions.imageio.plugins.nitronitf.NITFImageWriter.java
/** * Do the real write operation (writing images, texts, ...) * /* w w w . ja v a 2s. c o m*/ * @param record * @param images * @param shp * @param fis * @param text * @return * @throws NITFException * @throws IOException */ private boolean writeNITF(final Record record, final List<ImageWrapper> images, final ShapeFileWrapper shp, final FileImageInputStreamExt fis, final List<TextWrapper> texts) throws NITFException, IOException { final int numImages = images.size(); ImageWrapper image = images.get(0); RenderedImage ri = image.getImage(); WriteCompression compression = image.getCompression(); int nBands = ri.getSampleModel().getNumBands(); boolean written = false; Writer writer = new Writer(); IOHandle handle = new IOHandle(outputFile.getCanonicalPath(), IOHandle.NITF_ACCESS_WRITEONLY, IOHandle.NITF_CREATE); byte[] shapeFileData = null; final boolean isJP2 = !(compression == WriteCompression.UNCOMPRESSED); if (shp != null) { shapeFileData = getShapeData(record, shp); } boolean prepared = false; if (isJP2) { // // // // get the JP2 Codestream previously written with Kakadu and transfer its content within // the NITF imageSegment // // // WriteHandler codeStream = null; IOInterface io; final int size = (int) fis.length(); io = new IOFileInputStream(fis); writer.prepare(record, handle); if (shapeFileData != null) { writeData(shapeFileData, writer); } codeStream = new StreamIOWriteHandler(io, 0, size); writer.setImageWriteHandler(0, codeStream); prepared = true; } if (!isJP2 || numImages > 1) { if (!prepared) { writer.prepare(record, handle); } if (numImages == 1) { // setup a Writer if (shapeFileData != null) { writeData(shapeFileData, writer); } ImageSource imageSource = new ImageSource(); nitf.ImageWriter imageWriter = writer.getNewImageWriter(0); boolean[] successes = new boolean[nBands]; final boolean isMono = images.get(0).getImage().getSampleModel().getNumBands() == 1; if (isMono) { DataBufferByte dbb = (DataBufferByte) ri.getData().getDataBuffer(); BandSource bs = new MemorySource(dbb.getData(), dbb.getSize(), 0, 0, 0); successes[0] = imageSource.addBand(bs); } else { for (int i = 0; i < nBands; i++) { RenderedImage band = BandSelectDescriptor.create(ri, new int[] { i }, null); DataBufferByte dbb = (DataBufferByte) band.getData().getDataBuffer(); BandSource bs = new MemorySource(dbb.getData(), dbb.getSize(), 0, 0, 0); successes[i] = imageSource.addBand(bs); } } imageWriter.attachSource(imageSource); } else { ImageWrapper img = images.get(1); ri = img.getImage(); nBands = ri.getSampleModel().getNumBands(); ImageSource imageSource = new ImageSource(); nitf.ImageWriter imageWriter2 = writer.getNewImageWriter(1); boolean[] successes = new boolean[nBands]; DataBufferByte dbb = (DataBufferByte) ri.getData().getDataBuffer(); BandSource bs = new MemorySource(dbb.getData(), dbb.getSize(), 0, 0, 0); successes[0] = imageSource.addBand(bs); imageWriter2.attachSource(imageSource); } } // Adding text if (texts != null && !texts.isEmpty()) { int i = 0; for (TextWrapper text : texts) { byte[] textContent = text.getTextContent(); if (textContent != null) { SegmentWriter textWriter = writer.getNewTextWriter(i++); SegmentSource source = SegmentSource.makeSegmentMemorySource(textContent, textContent.length, 0, 0); textWriter.attachSource(source); } } } written = writer.write(); if (handle != null) { handle.close(); } return written; }
From source file:org.apache.fop.render.pcl.PCLGenerator.java
private RenderedImage getMask(RenderedImage img, Dimension targetDim) { ColorModel cm = img.getColorModel(); if (cm.hasAlpha()) { BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Raster raster = img.getData(); GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0); BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null); BufferedImage alphat = op1.filter(alpha, null); BufferedImage mask;//from w w w . ja va 2s . c o m if (true) { mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY); } else { byte[] arr = { (byte) 0, (byte) 0xff }; ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr); WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width, targetDim.height, 1, 1, null); mask = new BufferedImage(colorModel, wraster, false, null); } Graphics2D g2d = mask.createGraphics(); try { AffineTransform at = new AffineTransform(); double sx = targetDim.getWidth() / img.getWidth(); double sy = targetDim.getHeight() / img.getHeight(); at.scale(sx, sy); g2d.drawRenderedImage(alphat, at); } finally { g2d.dispose(); } /* try { BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png")); BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png")); } catch (IOException e) { e.printStackTrace(); }*/ return mask; } else { return null; } }
From source file:org.apache.fop.render.pcl.PCLGenerator.java
/** * Paint a bitmap at the current cursor position. The bitmap must be a monochrome * (1-bit) bitmap image./*from ww w. java2 s .c o m*/ * @param img the bitmap image (must be 1-bit b/w) * @param resolution the resolution of the image (must be a PCL resolution) * @throws IOException In case of an I/O error */ public void paintMonochromeBitmap(RenderedImage img, int resolution) throws IOException { if (!isValidPCLResolution(resolution)) { throw new IllegalArgumentException("Invalid PCL resolution: " + resolution); } boolean monochrome = isMonochromeImage(img); if (!monochrome) { throw new IllegalArgumentException("img must be a monochrome image"); } setRasterGraphicsResolution(resolution); writeCommand("*r0f" + img.getHeight() + "t" + img.getWidth() + "s1A"); Raster raster = img.getData(); Encoder encoder = new Encoder(img); // Transfer graphics data int imgw = img.getWidth(); IndexColorModel cm = (IndexColorModel) img.getColorModel(); if (cm.getTransferType() == DataBuffer.TYPE_BYTE) { DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); MultiPixelPackedSampleModel packedSampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 1); if (img.getSampleModel().equals(packedSampleModel) && dataBuffer.getNumBanks() == 1) { //Optimized packed encoding byte[] buf = dataBuffer.getData(); int scanlineStride = packedSampleModel.getScanlineStride(); int idx = 0; int c0 = toGray(cm.getRGB(0)); int c1 = toGray(cm.getRGB(1)); boolean zeroIsWhite = c0 > c1; for (int y = 0, maxy = img.getHeight(); y < maxy; y++) { for (int x = 0, maxx = scanlineStride; x < maxx; x++) { if (zeroIsWhite) { encoder.add8Bits(buf[idx]); } else { encoder.add8Bits((byte) ~buf[idx]); } idx++; } encoder.endLine(); } } else { //Optimized non-packed encoding for (int y = 0, maxy = img.getHeight(); y < maxy; y++) { byte[] line = (byte[]) raster.getDataElements(0, y, imgw, 1, null); for (int x = 0, maxx = imgw; x < maxx; x++) { encoder.addBit(line[x] == 0); } encoder.endLine(); } } } else { //Safe but slow fallback for (int y = 0, maxy = img.getHeight(); y < maxy; y++) { for (int x = 0, maxx = imgw; x < maxx; x++) { int sample = raster.getSample(x, y, 0); encoder.addBit(sample == 0); } encoder.endLine(); } } // End raster graphics writeCommand("*rB"); }
From source file:org.apache.fop.util.BitmapImageUtilTestCase.java
private void assertPixels(String expected, RenderedImage img, int x, int y, int w) throws IOException { if (TEST_PIXELS) { byte[] byteArray = (byte[]) img.getData().getDataElements(x, y, w, 1, new byte[w]); assertEquals(expected, toHex(byteArray)); }// w w w. j a va 2s . c o m }
From source file:org.apache.xmlgraphics.ps.PSImageUtils.java
/** * Extracts a packed RGB integer array of a RenderedImage. * @param img the image/*from w w w . j av a2 s.c om*/ * @param startX the starting X coordinate * @param startY the starting Y coordinate * @param w the width of the cropped image * @param h the height of the cropped image * @param rgbArray the prepared integer array to write to * @param offset offset in the target array * @param scansize width of a row in the target array * @return the populated integer array previously passed in as rgbArray parameter */ public static int[] getRGB(RenderedImage img, int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) { Raster raster = img.getData(); int yoff = offset; int off; Object data; int nbands = raster.getNumBands(); int dataType = raster.getDataBuffer().getDataType(); switch (dataType) { case DataBuffer.TYPE_BYTE: data = new byte[nbands]; break; case DataBuffer.TYPE_USHORT: data = new short[nbands]; break; case DataBuffer.TYPE_INT: data = new int[nbands]; break; case DataBuffer.TYPE_FLOAT: data = new float[nbands]; break; case DataBuffer.TYPE_DOUBLE: data = new double[nbands]; break; default: throw new IllegalArgumentException("Unknown data buffer type: " + dataType); } if (rgbArray == null) { rgbArray = new int[offset + h * scansize]; } ColorModel colorModel = img.getColorModel(); for (int y = startY; y < startY + h; y++, yoff += scansize) { off = yoff; for (int x = startX; x < startX + w; x++) { rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data)); } } return rgbArray; }
From source file:org.esa.nest.gpf.ERSCalibrator.java
private double[][] computeADCPowerLossValue(final RenderedImage squaredImage, final TileDescriptionFlags tileDescriptionFlags) { final int delH = (windowHeight / 2) / blockHeight; final int delW = (windowWidth / 2) / blockWidth; int x0 = 0;//from ww w. j a v a 2s.c om int y0 = 0; int w = squaredImage.getWidth(); int h = squaredImage.getHeight(); if (tileDescriptionFlags.adcSourceTileTopExtFlag) { y0 = delH; h -= delH; } if (tileDescriptionFlags.adcSourceTileBottomExtFlag) { h -= delH; } if (h <= 0) h = 1; if (tileDescriptionFlags.adcSourceTileLeftExtFlag) { x0 = delW; w -= delW; } if (tileDescriptionFlags.adcSourceTileRightExtFlag) { w -= delW; } if (w <= 0) w = 1; double[][] adcPowerLoss = new double[h][w]; Raster data = squaredImage.getData(); double dn; for (int y = y0; y < y0 + h; y++) { for (int x = x0; x < x0 + w; x++) { dn = data.getSampleDouble(x, y, 0); if (isERS1Mission) { adcPowerLoss[y - y0][x - x0] = getPowerLossValue(dn, appendixF1); } else { adcPowerLoss[y - y0][x - x0] = getPowerLossValue(dn, appendixF2); } } } /* for (int x = 0; x < w; x++) { System.out.print(adcPowerLoss[25][x] + ","); } System.out.println(); */ return adcPowerLoss; }
From source file:org.esa.nest.gpf.ERSCalibrator.java
private static void outputRealImage(final RenderedImage I, final int startIdx, final int endIdx) { final Raster data = I.getData(); final double[] real = data.getSamples(0, 0, I.getWidth(), I.getHeight(), 0, (double[]) null); for (int i = startIdx; i <= endIdx; i++) { System.out.print(real[i] + ","); }//from w w w . j a v a 2 s. c o m System.out.println(); }