public static InterleavedU8 extractInterleavedU8 ( BufferedImage img ) { DataBuffer buffer = img . getRaster ( ) . getDataBuffer ( ) ; if ( buffer . getDataType ( ) == DataBuffer . TYPE_BYTE && isKnownByteFormat ( img ) ) { WritableRaster raster = img . getRaster ( ) ; InterleavedU8 ret = new InterleavedU8 ( ) ; ret . width = img . getWidth ( ) ; ret . height = img . getHeight ( ) ; ret . startIndex = ConvertRaster . getOffset ( raster ) ; ret . imageType . numBands = raster . getNumBands ( ) ; ret . numBands = raster . getNumBands ( ) ; ret . stride = ConvertRaster . stride ( raster ) ; ret . data = ( ( DataBufferByte ) buffer ) . getData ( ) ; ret . subImage = ret . startIndex != 0 ; return ret ; } throw new IllegalArgumentException ( "Buffered image does not have an interleaved byte raster" ) ; }
For BufferedImage stored as a byte array internally it extracts an interleaved 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 .