/** * For BufferedImage stored as a byte array internally it extracts an * interlaced image. The input image and the returned image will both * share the same internal data array. Using this function allows unnecessary * memory copying to be avoided. * * @param img Image whose internal data is extracted and wrapped. * @return An image whose internal data is the same as the input image. */ public static ImageInterleavedInt8 extractInterlacedInt8(BufferedImage img) { if (img.getRaster() instanceof ByteInterleavedRaster && img.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ByteInterleavedRaster raster = (ByteInterleavedRaster) img.getRaster(); ImageInterleavedInt8 ret = new ImageInterleavedInt8(); ret.width = img.getWidth(); ret.height = img.getHeight(); ret.stride = ret.width; ret.numBands = raster.getNumBands(); ret.data = raster.getDataStorage(); return ret; } throw new IllegalArgumentException("Buffered image does not have an interlaced int raster"); }
/** * For BufferedImage stored as a byte array internally it extracts an * image. The input image and the returned image will both * share the same internal data array. Using this function allows unnecessary * memory copying to be avoided. * * @param img Image whose internal data is extracted and wrapped. * @return An image whose internal data is the same as the input image. */ public static ImageUInt8 extractImageInt8(BufferedImage img) { if (img.getRaster() instanceof ByteInterleavedRaster && img.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { if( isSubImage(img) ) throw new IllegalArgumentException("Sub-images of BufferedImages are not yet supported"); ByteInterleavedRaster raster = (ByteInterleavedRaster) img.getRaster(); if (raster.getNumBands() != 1) throw new IllegalArgumentException("Input image has more than one channel"); ImageUInt8 ret = new ImageUInt8(); ret.width = img.getWidth(); ret.height = img.getHeight(); ret.stride = ret.width; ret.data = raster.getDataStorage(); return ret; } throw new IllegalArgumentException("Buffered image does not have a gray scale byte raster"); }
/** * Converts the buffered image into an {@link boofcv.struct.image.ImageUInt8}. If the buffered image * has multiple channels the intensities of each channel are averaged together. * * @param src Input image. * @param dst Where the converted image is written to. If null a new unsigned image is created. * @return Converted image. */ public static ImageUInt8 convertFrom(BufferedImage src, ImageUInt8 dst) { if (dst != null) { if (src.getWidth() != dst.getWidth() || src.getHeight() != dst.getHeight()) { throw new IllegalArgumentException("image dimension are different"); } } else { dst = new ImageUInt8(src.getWidth(), src.getHeight()); } try { if (src.getRaster() instanceof ByteInterleavedRaster && src.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.bufferedToGray((ByteInterleavedRaster) src.getRaster(), dst); } else if (src.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.bufferedToGray((IntegerInterleavedRaster) src.getRaster(), dst); } else { ConvertRaster.bufferedToGray(src, dst); } } catch( java.security.AccessControlException e) { // Applets don't allow access to the raster() ConvertRaster.bufferedToGray(src, dst); } return dst; }
/** * Converts the buffered image into an {@link boofcv.struct.image.ImageFloat32}. If the buffered image * has multiple channels the intensities of each channel are averaged together. * * @param src Input image. * @param dst Where the converted image is written to. If null a new unsigned image is created. * @return Converted image. */ public static ImageFloat32 convertFrom(BufferedImage src, ImageFloat32 dst) { if (dst != null) { if (src.getWidth() != dst.getWidth() || src.getHeight() != dst.getHeight()) { throw new IllegalArgumentException("image dimension are different"); } } else { dst = new ImageFloat32(src.getWidth(), src.getHeight()); } try { if (src.getRaster() instanceof ByteInterleavedRaster && src.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.bufferedToGray((ByteInterleavedRaster) src.getRaster(), dst); } else if (src.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.bufferedToGray((IntegerInterleavedRaster) src.getRaster(), dst); } else { ConvertRaster.bufferedToGray(src, dst); } } catch( java.security.AccessControlException e) { // Applets don't allow access to the raster() ConvertRaster.bufferedToGray(src, dst); } return dst; }
/** * Converts a {@link boofcv.struct.image.ImageUInt8} into a BufferedImage. If the buffered image * has multiple channels the intensities of each channel are averaged together. * * @param src Input image. * @param dst Where the converted image is written to. If null a new image is created. * @return Converted image. */ public static BufferedImage convertTo(ImageUInt8 src, BufferedImage dst) { dst = checkInputs(src, dst); try { if (dst.getRaster() instanceof ByteInterleavedRaster && dst.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.grayToBuffered(src, (ByteInterleavedRaster) dst.getRaster()); } else if (dst.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.grayToBuffered(src, (IntegerInterleavedRaster) dst.getRaster()); } else { ConvertRaster.grayToBuffered(src, dst); } // hack so that it knows the buffer has been modified dst.setRGB(0,0,dst.getRGB(0,0)); } catch( java.security.AccessControlException e) { ConvertRaster.grayToBuffered(src, dst); } return dst; }
/** * Converts a {@link boofcv.struct.image.ImageInt16} into a BufferedImage. If the buffered image * has multiple channels the intensities of each channel are averaged together. * * @param src Input image. * @param dst Where the converted image is written to. If null a new image is created. * @return Converted image. */ public static BufferedImage convertTo(ImageInt16 src, BufferedImage dst) { dst = checkInputs(src, dst); try { if (dst.getRaster() instanceof ByteInterleavedRaster && dst.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.grayToBuffered(src, (ByteInterleavedRaster) dst.getRaster()); } else if (dst.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.grayToBuffered(src, (IntegerInterleavedRaster) dst.getRaster()); } else if( dst.getType() == BufferedImage.TYPE_USHORT_GRAY ) { ConvertRaster.grayToBuffered(src, (ShortInterleavedRaster) dst.getRaster()); } else { ConvertRaster.grayToBuffered(src, dst); } // hack so that it knows the buffer has been modified dst.setRGB(0,0,dst.getRGB(0,0)); } catch( java.security.AccessControlException e) { ConvertRaster.grayToBuffered(src, dst); } return dst; }
/** * Converts the buffered image into an {@link boofcv.struct.image.ImageFloat32}. If the buffered image * has multiple channels the intensities of each channel are averaged together. The floating * point image is assumed to be between 0 and 255. * * @param src Input image. * @param dst Where the converted image is written to. If null a new image is created. * @return Converted image. */ public static BufferedImage convertTo(ImageFloat32 src, BufferedImage dst) { dst = checkInputs(src, dst); try { if (dst.getRaster() instanceof ByteInterleavedRaster && dst.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.grayToBuffered(src, (ByteInterleavedRaster) dst.getRaster()); } else if (dst.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.grayToBuffered(src, (IntegerInterleavedRaster) dst.getRaster()); } else { ConvertRaster.grayToBuffered(src, dst); } // hack so that it knows the buffer has been modified dst.setRGB(0,0,dst.getRGB(0,0)); } catch( java.security.AccessControlException e) { ConvertRaster.grayToBuffered(src, dst); } return dst; }
/** * Converts a {@link boofcv.struct.image.ImageUInt8} into a BufferedImage. If the buffered image * has multiple channels the intensities of each channel are averaged together. * * @param src Input image. * @param dst Where the converted image is written to. If null a new image is created. * @return Converted image. */ public static BufferedImage convertTo_U8(MultiSpectral<ImageUInt8> src, BufferedImage dst) { dst = checkInputs(src, dst); try { if (dst.getRaster() instanceof ByteInterleavedRaster && dst.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.multToBuffered_U8(src, (ByteInterleavedRaster) dst.getRaster()); } else if (dst.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.multToBuffered_U8(src, (IntegerInterleavedRaster) dst.getRaster()); } else { ConvertRaster.multToBuffered_U8(src, dst); } // hack so that it knows the buffer has been modified dst.setRGB(0,0,dst.getRGB(0,0)); } catch( java.security.AccessControlException e) { ConvertRaster.multToBuffered_U8(src, dst); } return dst; }
/** * Converts a {@link boofcv.struct.image.ImageUInt8} into a BufferedImage. If the buffered image * has multiple channels the intensities of each channel are averaged together. * * @param src Input image. * @param dst Where the converted image is written to. If null a new image is created. * @return Converted image. */ public static BufferedImage convertTo_F32(MultiSpectral<ImageFloat32> src, BufferedImage dst) { dst = checkInputs(src, dst); try { if (dst.getRaster() instanceof ByteInterleavedRaster && dst.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ConvertRaster.multToBuffered_F32(src, (ByteInterleavedRaster) dst.getRaster()); } else if (dst.getRaster() instanceof IntegerInterleavedRaster) { ConvertRaster.multToBuffered_F32(src, (IntegerInterleavedRaster) dst.getRaster()); } else { ConvertRaster.multToBuffered_F32(src, dst); } // hack so that it knows the buffer has been modified dst.setRGB(0,0,dst.getRGB(0,0)); } catch( java.security.AccessControlException e) { ConvertRaster.multToBuffered_F32(src, dst); } return dst; }
/** * Creates a set of test BufferedImages with the appropriate rasters and number of bytes/channels. */ private BufferedImage[] createBufferedTestImages(Class<?> paramType) { BufferedImage[] input; if (paramType == ByteInterleavedRaster.class) { // the code is handled different when a different number of channels is used input = new BufferedImage[]{ createByteBuff(imgWidth, imgHeight, 4, rand), createByteBuff(imgWidth, imgHeight, 3, rand), createByteBuff(imgWidth, imgHeight, 1, rand)}; } else if (paramType == IntegerInterleavedRaster.class) { input = new BufferedImage[]{createIntBuff(imgWidth, imgHeight, rand)}; } else if( paramType == ShortInterleavedRaster.class ) { input = new BufferedImage[]{createShortBuff(imgWidth, imgHeight, rand)}; } else if (paramType == BufferedImage.class) { // just pick an arbitrary image type here input = new BufferedImage[]{createIntBuff(imgWidth, imgHeight, rand)}; } else { throw new RuntimeException("Unknown raster type: " + paramType.getSimpleName()); } return input; }
public static BufferedImage renderBinary( ImageUInt8 binaryImage , BufferedImage out ) { if( out == null ) { out = new BufferedImage(binaryImage.getWidth(),binaryImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY); } try { if( out.getRaster() instanceof ByteInterleavedRaster ) { renderBinary(binaryImage, (ByteInterleavedRaster)out.getRaster()); } else { _renderBinary(binaryImage, out); } } catch( SecurityException e ) { _renderBinary(binaryImage, out); } return out; }
public static PaintContext getContext(BufferedImage bufImg, AffineTransform xform, RenderingHints hints, Rectangle devBounds) { WritableRaster raster = bufImg.getRaster(); ColorModel cm = bufImg.getColorModel(); int maxw = devBounds.width; Object val = hints.get(RenderingHints.KEY_INTERPOLATION); boolean filter = (val == null ? (hints.get(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY) : (val != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)); if (raster instanceof IntegerInterleavedRaster && (!filter || isFilterableDCM(cm))) { IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster; if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) { return new Int(iir, cm, xform, maxw, filter); } } else if (raster instanceof ByteInterleavedRaster) { ByteInterleavedRaster bir = (ByteInterleavedRaster) raster; if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) { if (filter) { if (isFilterableICM(cm)) { return new ByteFilter(bir, cm, xform, maxw); } } else { return new Byte(bir, cm, xform, maxw); } } } return new Any(raster, cm, xform, maxw, filter); }
public Byte(ByteInterleavedRaster srcRas, ColorModel cm, AffineTransform xform, int maxw) { super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw); this.srcRas = srcRas; this.inData = srcRas.getDataStorage(); this.inSpan = srcRas.getScanlineStride(); this.inOff = srcRas.getDataOffset(0); }
public WritableRaster makeRaster(int w, int h) { WritableRaster ras = makeByteRaster(srcRas, w, h); ByteInterleavedRaster biRas = (ByteInterleavedRaster) ras; outData = biRas.getDataStorage(); outSpan = biRas.getScanlineStride(); outOff = biRas.getDataOffset(0); return ras; }
public ByteFilter(ByteInterleavedRaster srcRas, ColorModel cm, AffineTransform xform, int maxw) { super((cm.getTransparency() == Transparency.OPAQUE ? xrgbmodel : argbmodel), xform, srcRas.getWidth(), srcRas.getHeight(), maxw); this.inPalette = new int[256]; ((IndexColorModel) cm).getRGBs(this.inPalette); this.srcRas = srcRas; this.inData = srcRas.getDataStorage(); this.inSpan = srcRas.getScanlineStride(); this.inOff = srcRas.getDataOffset(0); }
/** * Creates a Raster based on a SinglePixelPackedSampleModel with * the specified DataBuffer, width, height, scanline stride, and * band masks. The number of bands is inferred from bandMasks.length. * The upper left corner of the Raster is given by * the location argument. If location is null, (0, 0) will be used. * @param dataBuffer the <code>DataBuffer</code> that contains the * image data * @param w the width in pixels of the image data * @param h the height in pixels of the image data * @param scanlineStride the line stride of the image data * @param bandMasks an array containing an entry for each band * @param location the upper-left corner of the <code>Raster</code> * @return a WritableRaster object with the specified * <code>DataBuffer</code>, width, height, scanline stride, * and band masks. * @throws RasterFormatException if <code>w</code> or <code>h</code> * is less than or equal to zero, or computing either * <code>location.x + w</code> or * <code>location.y + h</code> results in integer * overflow * @throws IllegalArgumentException if <code>dataType</code> is not * one of the supported data types, which are * <code>DataBuffer.TYPE_BYTE</code>, * <code>DataBuffer.TYPE_USHORT</code> * or <code>DataBuffer.TYPE_INT</code> * @throws RasterFormatException if <code>dataBuffer</code> has more * than one bank. * @throws NullPointerException if <code>dataBuffer</code> is null */ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, int w, int h, int scanlineStride, int bandMasks[], Point location) { if (dataBuffer == null) { throw new NullPointerException("DataBuffer cannot be null"); } if (location == null) { location = new Point(0,0); } int dataType = dataBuffer.getDataType(); SinglePixelPackedSampleModel sppsm = new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride, bandMasks); switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sppsm, dataBuffer, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sppsm, dataBuffer, location); case DataBuffer.TYPE_INT: return new IntegerInterleavedRaster(sppsm, dataBuffer, location); default: throw new IllegalArgumentException("Unsupported data type " + dataType); } }
public static void main(String[] args) throws Exception { String testImage = "D:\\pic\\Hamamatsu\\bloodsmear.ndpi"; RawDataFile rdf = DALConfig.getImageProvider().LoadRawDataFile(6184134); IOrbitImage oi = DALConfig.getImageProvider().createOrbitImage(rdf,0); //try (OrbitImageBioformats oi = new OrbitImageBioformats(testImage,2,0,null)) { System.out.println("tileSize before: " + oi.getTileWidth() + " x " + oi.getTileHeight()); TileSizeWrapper tw = new TileSizeWrapper(oi, 512, 512); OrbitTiledImage2 oit2 = new OrbitTiledImageIOrbitImage(tw,0); System.out.println("tileSize after: " + oit2.getTileWidth() + " x " + oit2.getTileHeight()); for (int tx=20; tx<23; tx++) for (int ty=20; ty<23; ty++) { WritableRaster raster = (WritableRaster)oit2.getTile(tx, ty); //59,10 System.out.println("tx x ty: "+tx+" x "+ty+" "+ "wxh: " + raster.getWidth() + " x " + raster.getHeight()+" scanlinestride: "+((ByteInterleavedRaster)raster).getScanlineStride()+" oit2: "+((PixelInterleavedSampleModel)oit2.getSampleModel()).getScanlineStride()); raster = (WritableRaster) raster.createTranslatedChild(0, 0); BufferedImage bi = new BufferedImage(oit2.getColorModel(), raster, false, null); ImageIO.write(bi, "png", new File("d:/NoBackup/a/tile"+ tx+"x"+ty+".png")); } } oi.close(); }