List of usage examples for java.awt.image BufferedImage setData
public void setData(Raster r)
From source file:Main.java
/** * Quickly copies an image.//from w w w . jav a2 s . c o m * @param src The source image. * @return The replicated image. */ public static BufferedImage imgUtilFastCopy(BufferedImage src) { if (src == null) return null; BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType()); b.setData(src.getRaster()); return b; }
From source file:TestImage2.java
public static RenderedImage toRenderedImage(String[] rows) { // Create the DataBuffer to hold the pixel samples final int width = rows[0].length(); final int height = rows.length; final int size = width * height; byte[] pixels = new byte[size]; int index = 0; for (String row : rows) { for (int x = 0; x < width; x++) { pixels[index++] = (byte) decodeGray(row.charAt(x)); }/* w ww .j ava 2s. co m*/ } DataBuffer dataBuffer = new DataBufferByte(pixels, size); // Create Raster WritableRaster writableRaster = Raster.createBandedRaster(dataBuffer, width, height, width, // scanlineStride new int[] { 0 }, // bankIndices, new int[] { 0 }, // bandOffsets, null); // location // Create the image BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); bufferedImage.setData(writableRaster); return bufferedImage; }
From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java
private static BufferedImage getAlphaImage(BufferedImage image) { if (!image.getColorModel().hasAlpha()) { return null; }/*from w ww . j a va 2s .co m*/ if (image.getTransparency() == Transparency.BITMASK) { throw new UnsupportedOperationException( "BITMASK Transparency JPEG compression is not" + " useful, use LosslessImageFactory instead"); } WritableRaster alphaRaster = image.getAlphaRaster(); if (alphaRaster == null) { // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha return null; } BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); alphaImage.setData(alphaRaster); return alphaImage; }
From source file:omr.jai.TestImage3.java
public static PlanarImage decodeImage (String[] rows) {/*w ww. j a va2s . co m*/ // Create the DataBuffer to hold the pixel samples final int width = rows[0].length(); final int height = rows.length; // Create Raster Raster raster; if (true) { raster = Raster.createPackedRaster (DataBuffer.TYPE_INT, width, height, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000},// bandMasks RGBA null); } else { raster = Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE, width, height, 4,// num of bands null); } // Populate the data buffer DataBuffer dataBuffer = raster.getDataBuffer(); int index = 0; for (String row : rows) { for (int x = 0; x < width; x++) { int argb = toARGB(row.charAt(x)); dataBuffer.setElem(index, argb); index++; } } // Dump // final int size = width * height; // System.out.println("DataBuffer :"); // for (int i = 0; i < size; i++) { // if (i % width == 0) { // System.out.println(); // } // System.out.print(String.format("%8x ", dataBuffer.getElem(i))); // } // System.out.println(); // Create the image BufferedImage bufferedImage = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); bufferedImage.setData(raster); // Dump // System.out.println("BufferedImage :"); // for (int y = 0; y < height; y++) { // System.out.println(); // for (int x = 0; x < width; x++) { // System.out.print(String.format("%8x ", bufferedImage.getRGB(x, y))); // } // } // System.out.println(); return PlanarImage.wrapRenderedImage(bufferedImage); }
From source file:Main.java
static BufferedImage average(BufferedImage[] images) { BufferedImage average = new BufferedImage(images[0].getWidth(), images[0].getHeight(), BufferedImage.TYPE_BYTE_GRAY); WritableRaster raster = average.getRaster().createCompatibleWritableRaster(); for (int k = 0; k < images[0].getHeight(); ++k) { for (int j = 0; j < images[0].getWidth(); ++j) { float sum = 0.0f; for (int i = 0; i < images.length; ++i) { sum = sum + images[i].getRaster().getSample(j, k, 0); }// w w w. jav a 2 s .c om raster.setSample(j, k, 0, Math.round(sum / images.length)); } } average.setData(raster); return average; }
From source file:net.mindengine.oculus.frontend.web.controllers.project.ProjectEditController.java
private String saveProjectIconFromBufferedImage(BufferedImage image, Long id) throws IOException { Date date = new Date(); String path = config.getDataFolder() + File.separator + "projects" + File.separator + id; new File(path).mkdirs(); File file = new File(path + File.separator + "icon_" + date.getTime() + ".png"); file.createNewFile();// w w w . j a v a 2s . c om BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); imageRGB.setData(image.getData()); ImageIO.write(imageRGB, "png", file); return Long.toString(date.getTime()); }
From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDInlinedImage.java
/** * This will take the inlined image information and create a java.awt.Image from * it.//w w w . j a va 2s .c om * * @param colorSpaces The ColorSpace dictionary from the current resources, if any. * * @return The image that this object represents. * * @throws IOException If there is an error creating the image. */ public BufferedImage createImage(Map colorSpaces) throws IOException { /* * This was the previous implementation, not sure which is better right now. * byte[] transparentColors = new byte[]{(byte)0xFF,(byte)0xFF}; byte[] colors=new byte[]{0, (byte)0xFF}; IndexColorModel colorModel = new IndexColorModel( 1, 2, colors, colors, colors, transparentColors ); BufferedImage image = new BufferedImage( params.getWidth(), params.getHeight(), BufferedImage.TYPE_BYTE_BINARY, colorModel ); DataBufferByte buffer = new DataBufferByte( getImageData(), 1 ); WritableRaster raster = Raster.createPackedRaster( buffer, params.getWidth(), params.getHeight(), params.getBitsPerComponent(), new Point(0,0) ); image.setData( raster ); return image; */ //verify again pci32.pdf before changing below PDColorSpace pcs = params.getColorSpace(colorSpaces); ColorModel colorModel; if (pcs != null) { colorModel = pcs.createColorModel(params.getBitsPerComponent()); } else { byte[] transparentColors = new byte[] { (byte) 0xFF, (byte) 0xFF }; byte[] colors = new byte[] { 0, (byte) 0xFF }; colorModel = new IndexColorModel(1, 2, colors, colors, colors, transparentColors); } boolean invert = false; // maybe a decode array is defined COSBase dictObj = params.getDictionary().getDictionaryObject(COSName.DECODE, COSName.D); if (dictObj != null && dictObj instanceof COSArray) { COSArray decode = (COSArray) dictObj; if (decode.getInt(0) == 1) { if (params.getBitsPerComponent() == 1) { // [1.0, 0.0] -> invert the "color" values invert = true; } else { //TODO implement decode array for BPC > 1 LOG.warn("decode array is not implemented for BPC > 1"); } } } List filters = params.getFilters(); byte[] finalData; if (filters == null || filters.isEmpty()) { finalData = getImageData(); } else { ByteArrayInputStream in = new ByteArrayInputStream(getImageData()); ByteArrayOutputStream out = new ByteArrayOutputStream(getImageData().length); FilterManager filterManager = new FilterManager(); for (int i = 0; i < filters.size(); i++) { out.reset(); Filter filter = filterManager.getFilter((String) filters.get(i)); filter.decode(in, out, params.getDictionary(), i); in = new ByteArrayInputStream(out.toByteArray()); } finalData = out.toByteArray(); } WritableRaster raster = colorModel.createCompatibleWritableRaster(params.getWidth(), params.getHeight()); /* Raster.createPackedRaster( buffer, params.getWidth(), params.getHeight(), params.getBitsPerComponent(), new Point(0,0) ); */ DataBuffer rasterBuffer = raster.getDataBuffer(); if (rasterBuffer instanceof DataBufferByte) { DataBufferByte byteBuffer = (DataBufferByte) rasterBuffer; byte[] data = byteBuffer.getData(); System.arraycopy(finalData, 0, data, 0, data.length); if (invert) { invertBitmap(data); } } else if (rasterBuffer instanceof DataBufferInt) { DataBufferInt byteBuffer = (DataBufferInt) rasterBuffer; int[] data = byteBuffer.getData(); for (int i = 0; i < finalData.length; i++) { data[i] = (finalData[i] + 256) % 256; if (invert) { data[i] = (~data[i] & 0xFF); } } } BufferedImage image = new BufferedImage(colorModel, raster, false, null); image.setData(raster); return image; }
From source file:nl.ru.ai.projects.parrot.tools.TwitterAccess.java
/** * Returns a RenderedImage object from a byte array * @param cameraOutput The camera output to transform into a RenderedImage * @return The RenderedImage that resulted from the camera output *//*from w w w .j av a 2s .c om*/ private RenderedImage getImageFromCamera(byte[] cameraOutput) { BufferedImage image = new BufferedImage(VideoPollInterface.FRONT_VIDEO_FRAME_WIDTH, VideoPollInterface.FRONT_VIDEO_FRAME_HEIGHT, BufferedImage.TYPE_3BYTE_BGR); if (cameraOutput != null) { WritableRaster raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, VideoPollInterface.FRONT_VIDEO_FRAME_WIDTH, VideoPollInterface.FRONT_VIDEO_FRAME_HEIGHT, 3, new Point(0, 0)); raster.setDataElements(0, 0, VideoPollInterface.FRONT_VIDEO_FRAME_WIDTH, VideoPollInterface.FRONT_VIDEO_FRAME_HEIGHT, cameraOutput); image.setData(raster); } return image; }
From source file:org.kuali.kra.printing.service.impl.PersonSignatureServiceImpl.java
/** * This method is to get buffered image based on predefined parameters * @param params/*from ww w.ja v a 2 s .c om*/ * @param imageData * @return */ protected BufferedImage getBufferedImage(ImageParameters params, byte[] imageData) { byte[] transparentColors = new byte[] { (byte) 0xFF, (byte) 0xFF }; byte[] colors = new byte[] { 0, (byte) 0xFF }; IndexColorModel colorModel = new IndexColorModel(1, 2, colors, colors, colors, transparentColors); BufferedImage image = new BufferedImage(params.getWidth(), params.getHeight(), BufferedImage.TYPE_BYTE_BINARY, colorModel); DataBufferByte buffer = new DataBufferByte(imageData, 1); WritableRaster raster = Raster.createPackedRaster(buffer, params.getWidth(), params.getHeight(), params.getBitsPerComponent(), new Point(0, 0)); image.setData(raster); return image; }
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. * //www . java2s .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; }