List of usage examples for java.awt.image BufferedImage getRaster
public WritableRaster getRaster()
From source file:org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderImageIO.java
private BufferedImage getFallbackBufferedImage(ImageReader reader, int pageIndex, ImageReadParam param) throws IOException { //Work-around found at: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903 //There are some additional ideas there if someone wants to go further. // Try reading a Raster (no color conversion). Raster raster = reader.readRaster(pageIndex, param); // Arbitrarily select a BufferedImage type. int imageType; switch (raster.getNumBands()) { case 1://from ww w . ja va 2 s . c o m imageType = BufferedImage.TYPE_BYTE_GRAY; break; case 3: imageType = BufferedImage.TYPE_3BYTE_BGR; break; case 4: imageType = BufferedImage.TYPE_4BYTE_ABGR; break; default: throw new UnsupportedOperationException(); } // Create a BufferedImage. BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), imageType); // Set the image data. bi.getRaster().setRect(raster); return bi; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage copyImage(BufferedImage in) { BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType()); for (int i = 0; i < in.getRaster().getNumBands(); i++) { out.getRaster().setSamples(0, 0, out.getWidth(), out.getHeight(), i, in.getRaster().getSamples(0, 0, in.getWidth(), in.getHeight(), i, (int[]) null)); }//from w w w .ja v a 2s . c o m return out; }
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()); {/* ww w . ja v a 2 s . co 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:ImageOpByRomain.java
/** * <p>// ww w .ja v a 2 s . c o m * Returns an array of pixels, stored as integers, from a * <code>BufferedImage</code>. The pixels are grabbed from a rectangular * area defined by a location and two dimensions. Calling this method on an * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and * <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image. * </p> * * @param img * the source image * @param x * the x location at which to start grabbing pixels * @param y * the y location at which to start grabbing pixels * @param w * the width of the rectangle of pixels to grab * @param h * the height of the rectangle of pixels to grab * @param pixels * a pre-allocated array of pixels of size w*h; can be null * @return <code>pixels</code> if non-null, a new array of integers * otherwise * @throws IllegalArgumentException * is <code>pixels</code> is non-null and of length < w*h */ public static int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) { if (w == 0 || h == 0) { return new int[0]; } if (pixels == null) { pixels = new int[w * h]; } else if (pixels.length < w * h) { throw new IllegalArgumentException("pixels array must have a length" + " >= w*h"); } int imageType = img.getType(); if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB) { Raster raster = img.getRaster(); return (int[]) raster.getDataElements(x, y, w, h, pixels); } // Unmanages the image return img.getRGB(x, y, w, h, pixels, 0, w); }
From source file:org.psystems.dicomweb.Dcm2Dcm.java
/** * Recodes the images from the source transfer syntax, as read from the src * file, to the specified destination syntax. *//* w w w. j ava 2s . com*/ public void recodeImages(File src, File dest) throws IOException { ImageReader reader = new DicomImageReaderSpi().createReaderInstance(); ImageWriter writer = new DicomImageWriterSpi().createWriterInstance(); FileImageInputStream input = new FileImageInputStream(src); reader.setInput(input); if (dest.exists()) dest.delete(); FileImageOutputStream output = new FileImageOutputStream(dest); writer.setOutput(output); DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata(); DicomObject ds = streamMeta.getDicomObject(); DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null); DicomObject newDs = new BasicDicomObject(); ds.copyTo(newDs); writeMeta.setDicomObject(newDs); int frames = ds.getInt(Tag.NumberOfFrames, 1); LookupTable lut = prepareBitStrip(writeMeta, reader); newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid()); if (overwriteObject != null) { overwriteObject.copyTo(newDs); } writer.prepareWriteSequence(writeMeta); for (int i = 0; i < frames; i++) { WritableRaster r = (WritableRaster) reader.readRaster(i, null); ColorModel cm = ColorModelFactory.createColorModel(ds); BufferedImage bi = new BufferedImage(cm, r, false, null); if (lut != null) { lut.lookup(bi.getRaster(), bi.getRaster()); } IIOImage iioimage = new IIOImage(bi, null, null); writer.writeToSequence(iioimage, null); } writer.endWriteSequence(); output.close(); input.close(); }
From source file:org.psystems.dicomweb.Dcm2DcmCopy.java
/** * Recodes the images from the source transfer syntax, as read from the src * file, to the specified destination syntax. *//*from ww w . j av a2s.c om*/ public void recodeImages(File src, File dest) throws IOException { ImageReader reader = new DicomImageReaderSpi().createReaderInstance(); ImageWriter writer = new DicomImageWriterSpi().createWriterInstance(); FileImageInputStream input = new FileImageInputStream(src); reader.setInput(input); if (dest.exists()) dest.delete(); FileImageOutputStream output = new FileImageOutputStream(dest); writer.setOutput(output); DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata(); DicomObject ds = streamMeta.getDicomObject(); DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null); DicomObject newDs = new BasicDicomObject(); ds.copyTo(newDs); writeMeta.setDicomObject(newDs); int frames = ds.getInt(Tag.NumberOfFrames, 1); LookupTable lut = prepareBitStrip(writeMeta, reader); newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid()); if (overwriteObject != null) { overwriteObject.copyTo(newDs); } writer.prepareWriteSequence(writeMeta); for (int i = 0; i < frames; i++) { WritableRaster r = (WritableRaster) reader.readRaster(i, null); ColorModel cm = ColorModelFactory.createColorModel(ds); BufferedImage bi = new BufferedImage(cm, r, false, null); if (lut != null) { lut.lookup(bi.getRaster(), bi.getRaster()); } IIOImage iioimage = new IIOImage(bi, null, null); writer.writeToSequence(iioimage, null); } writer.endWriteSequence(); output.close(); input.close(); }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage transparentColor(BufferedImage src, Color trColor) { int w = src.getWidth(); int h = src.getHeight(); BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); if (src.getRaster().getNumBands() < 3) { for (int i = 0; i < 3; i++) { dst.getRaster().setSamples(0, 0, w, h, i, src.getRaster().getSamples(0, 0, w, h, 0, (int[]) null)); }//w w w.j ava 2 s.c o m } else if (src.getRaster().getNumBands() >= 3) { for (int i = 0; i < 3; i++) { dst.getRaster().setSamples(0, 0, w, h, i, src.getRaster().getSamples(0, 0, w, h, i, (int[]) null)); } } for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { if (dst.getRaster().getSample(x, y, 0) == trColor.getRed() && dst.getRaster().getSample(x, y, 1) == trColor.getGreen() && dst.getRaster().getSample(x, y, 2) == trColor.getBlue()) { dst.getRaster().setSample(x, y, 3, 0); } else { dst.getRaster().setSample(x, y, 3, 255); } } } return dst; }
From source file:haven.Utils.java
public static BufferedImage outline(BufferedImage img, Color col) { Coord sz = imgsz(img).add(2, 2);//from ww w.j a v a 2 s .co m BufferedImage ol = TexI.mkbuf(sz); Object fcol = ol.getColorModel().getDataElements(col.getRGB(), null); Raster src = img.getRaster(); WritableRaster dst = ol.getRaster(); for (int y = 0; y < sz.y; y++) { for (int x = 0; x < sz.x; x++) { boolean t; if ((y == 0) || (x == 0) || (y == sz.y - 1) || (x == sz.x - 1)) { t = true; } else { t = src.getSample(x - 1, y - 1, 3) < 250; } if (!t) continue; if (((x > 1) && (y > 0) && (y < sz.y - 1) && (src.getSample(x - 2, y - 1, 3) >= 250)) || ((x > 0) && (y > 1) && (x < sz.x - 1) && (src.getSample(x - 1, y - 2, 3) >= 250)) || ((x < sz.x - 2) && (y > 0) && (y < sz.y - 1) && (src.getSample(x, y - 1, 3) >= 250)) || ((x > 0) && (y < sz.y - 2) && (x < sz.x - 1) && (src.getSample(x - 1, y, 3) >= 250))) dst.setDataElements(x, y, fcol); } } return (ol); }
From source file:DataBufferGrabber.java
protected void paintComponent(Graphics g) { // create an image BufferedImage bImg = new BufferedImage(SWATCH_SIZE, SWATCH_SIZE, BufferedImage.TYPE_INT_RGB); Graphics gImage = bImg.getGraphics(); gImage.setColor(Color.WHITE); gImage.fillRect(0, 0, SWATCH_SIZE, SWATCH_SIZE); // Time how long it takes to copy the managed version long managedTime = copyImage(g, bImg, 0, 0); System.out.println("Managed: " + managedTime + " ms"); // Now grab the pixel array, change the colors, re-run the test Raster raster = bImg.getRaster(); DataBufferInt dataBuffer = (DataBufferInt) raster.getDataBuffer(); int pixels[] = dataBuffer.getData(); for (int i = 0; i < pixels.length; ++i) { // Make all pixels black pixels[i] = 0;/* w w w.java 2s . c o m*/ } // Time this un-managed copy long unmanagedTime = copyImage(g, bImg, SWATCH_SIZE, 0); System.out.println("Unmanaged: " + unmanagedTime + " ms"); }
From source file:Main.java
/** * Snapshots the specified {@link BufferedImage} and stores a copy of * its pixels into a JavaFX {@link Image} object, creating a new * object if needed./*from w w w . j a v a 2 s .c o m*/ * The returned {@code Image} will be a static snapshot of the state * of the pixels in the {@code BufferedImage} at the time the method * completes. Further changes to the {@code BufferedImage} will not * be reflected in the {@code Image}. * <p> * The optional JavaFX {@link WritableImage} parameter may be reused * to store the copy of the pixels. * A new {@code Image} will be created if the supplied object is null, * is too small or of a type which the image pixels cannot be easily * converted into. * * @param bimg the {@code BufferedImage} object to be converted * @param wimg an optional {@code WritableImage} object that can be * used to store the returned pixel data * @return an {@code Image} object representing a snapshot of the * current pixels in the {@code BufferedImage}. * @since JavaFX 2.2 */ public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); switch (bimg.getType()) { case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: break; default: BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = converted.createGraphics(); g2d.drawImage(bimg, 0, 0, null); g2d.dispose(); bimg = converted; break; } // assert(bimg.getType == TYPE_INT_ARGB[_PRE]); if (wimg != null) { int iw = (int) wimg.getWidth(); int ih = (int) wimg.getHeight(); if (iw < bw || ih < bh) { wimg = null; } else if (bw < iw || bh < ih) { int empty[] = new int[iw]; PixelWriter pw = wimg.getPixelWriter(); PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance(); if (bw < iw) { pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0); } if (bh < ih) { pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0); } } } if (wimg == null) { wimg = new WritableImage(bw, bh); } PixelWriter pw = wimg.getPixelWriter(); IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int data[] = icr.getDataStorage(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance() : PixelFormat.getIntArgbInstance()); pw.setPixels(0, 0, bw, bh, pf, data, offset, scan); return wimg; }