/** * Converts the buffered image into an {@link boofcv.struct.image.ImageInt16}. 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 <T extends ImageInt16>T convertFrom(BufferedImage src, T dst , Class<T> type ) { if (dst != null) { if (src.getWidth() != dst.getWidth() || src.getHeight() != dst.getHeight()) { throw new IllegalArgumentException("image dimension are different"); } } else { dst = GeneralizedImageOps.createSingleBand(type,src.getWidth(), src.getHeight()); } try { if (src.getRaster() instanceof ShortInterleavedRaster ) { ConvertRaster.bufferedToGray((ShortInterleavedRaster) src.getRaster(), dst); return dst; } } catch( java.security.AccessControlException e) {} // Applets don't allow access to the raster() or the image type wasn't supported ConvertRaster.bufferedToGray(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; }
/** * 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; }
/** * 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); } }
/** * A faster convert that works directly with a specific raster */ public static void bufferedToGray(ShortInterleavedRaster src, ImageInt16 dst) { short[] srcData = src.getDataStorage(); int numBands = src.getNumBands(); int size = dst.getWidth() * dst.getHeight(); int srcStride = src.getScanlineStride(); int srcOffset = src.getDataOffset(0)-src.getPixelStride()+1; int srcStrideDiff = srcStride-src.getPixelStride()*dst.width; if (numBands == 1) { if (dst.startIndex == 0 && dst.width == dst.stride && srcStrideDiff == 0 && srcOffset == 0 ) System.arraycopy(srcData, 0, dst.data, 0, size); else { for (int y = 0; y < dst.height; y++) { int indexDst = dst.startIndex + dst.stride * y; int indexSrc = srcOffset + srcStride * y; System.arraycopy(srcData, indexSrc, dst.data, indexDst, dst.width); } } } else { throw new RuntimeException("Only single band images are currently support for 16bit"); } }
/** * Creates a Raster based on a PixelInterleavedSampleModel with the * specified DataBuffer, width, height, scanline stride, pixel * stride, and band offsets. The number of bands is inferred from * bandOffsets.length. The upper left corner of the Raster * is given by the location argument. If location is null, (0, 0) * will be used. * <p> Note that interleaved {@code DataBuffer.TYPE_INT} * Rasters are not supported. To create a 1-band Raster of type * {@code DataBuffer.TYPE_INT}, use * Raster.createPackedRaster(). * @param dataBuffer the {@code DataBuffer} 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 pixelStride the pixel stride of the image data * @param bandOffsets the offsets of all bands * @param location the upper-left corner of the {@code Raster} * @return a WritableRaster object with the specified * {@code DataBuffer}, width, height, scanline stride, * pixel stride and band offsets. * @throws RasterFormatException if {@code w} or {@code h} * is less than or equal to zero, or computing either * {@code location.x + w} or * {@code location.y + h} results in integer * overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, * {@code DataBuffer.TYPE_USHORT} * @throws RasterFormatException if {@code dataBuffer} has more * than one bank. * @throws NullPointerException if {@code dataBuffer} is null */ public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer, int w, int h, int scanlineStride, int pixelStride, int bandOffsets[], Point location) { if (dataBuffer == null) { throw new NullPointerException("DataBuffer cannot be null"); } if (location == null) { location = new Point(0, 0); } int dataType = dataBuffer.getDataType(); PixelInterleavedSampleModel csm = new PixelInterleavedSampleModel(dataType, w, h, pixelStride, scanlineStride, bandOffsets); switch(dataType) { case DataBuffer.TYPE_BYTE: if (dataBuffer instanceof DataBufferByte) { return new ByteInterleavedRaster(csm, (DataBufferByte) dataBuffer, location); } break; case DataBuffer.TYPE_USHORT: if (dataBuffer instanceof DataBufferUShort) { return new ShortInterleavedRaster(csm, (DataBufferUShort) dataBuffer, location); } break; default: throw new IllegalArgumentException("Unsupported data type " + dataType); } // Create the generic raster return new SunWritableRaster(csm, dataBuffer, location); }
/** * 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} 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} * @return a WritableRaster object with the specified * {@code DataBuffer}, width, height, scanline stride, * and band masks. * @throws RasterFormatException if {@code w} or {@code h} * is less than or equal to zero, or computing either * {@code location.x + w} or * {@code location.y + h} results in integer * overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, * {@code DataBuffer.TYPE_USHORT} * or {@code DataBuffer.TYPE_INT} * @throws RasterFormatException if {@code dataBuffer} has more * than one bank. * @throws NullPointerException if {@code dataBuffer} 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: if (dataBuffer instanceof DataBufferByte) { return new ByteInterleavedRaster(sppsm, (DataBufferByte) dataBuffer, location); } break; case DataBuffer.TYPE_USHORT: if (dataBuffer instanceof DataBufferUShort) { return new ShortInterleavedRaster(sppsm, (DataBufferUShort) dataBuffer, location); } break; case DataBuffer.TYPE_INT: if (dataBuffer instanceof DataBufferInt) { return new IntegerInterleavedRaster(sppsm, (DataBufferInt) dataBuffer, location); } break; default: throw new IllegalArgumentException("Unsupported data type " + dataType); } // Create the generic raster return new SunWritableRaster(sppsm, dataBuffer, location); }
/** * Creates a Raster with the specified SampleModel and DataBuffer. * The upper left corner of the Raster is given by the location argument. * If location is null, (0, 0) will be used. * @param sm the specified <code>SampleModel</code> * @param db the specified <code>DataBuffer</code> * @param location the upper-left corner of the <code>Raster</code> * @return a <code>Raster</code> with the specified * <code>SampleModel</code>, <code>DataBuffer</code>, and * location. * @throws RasterFormatException if computing either * <code>location.x + sm.getWidth()</code> or * <code>location.y + sm.getHeight()</code> results in integer * overflow * @throws RasterFormatException if <code>db</code> has more * than one bank and <code>sm</code> is a * PixelInterleavedSampleModel, SinglePixelPackedSampleModel, * or MultiPixelPackedSampleModel. * @throws NullPointerException if either SampleModel or DataBuffer is * null */ public static Raster createRaster(SampleModel sm, DataBuffer db, Point location) { if ((sm == null) || (db == null)) { throw new NullPointerException("SampleModel and DataBuffer cannot be null"); } if (location == null) { location = new Point(0,0); } int dataType = sm.getDataType(); if (sm instanceof PixelInterleavedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); } } else if (sm instanceof SinglePixelPackedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); case DataBuffer.TYPE_INT: return new IntegerInterleavedRaster(sm, db, location); } } else if (sm instanceof MultiPixelPackedSampleModel && dataType == DataBuffer.TYPE_BYTE && sm.getSampleSize(0) < 8) { return new BytePackedRaster(sm, db, location); } // we couldn't do anything special - do the generic thing return new Raster(sm,db,location); }
/** * Creates a WritableRaster with the specified SampleModel and DataBuffer. * The upper left corner of the Raster is given by the location argument. * If location is null, (0, 0) will be used. * @param sm the specified <code>SampleModel</code> * @param db the specified <code>DataBuffer</code> * @param location the upper-left corner of the * <code>WritableRaster</code> * @return a <code>WritableRaster</code> with the specified * <code>SampleModel</code>, <code>DataBuffer</code>, and * location. * @throws RasterFormatException if computing either * <code>location.x + sm.getWidth()</code> or * <code>location.y + sm.getHeight()</code> results in integer * overflow * @throws RasterFormatException if <code>db</code> has more * than one bank and <code>sm</code> is a * PixelInterleavedSampleModel, SinglePixelPackedSampleModel, * or MultiPixelPackedSampleModel. * @throws NullPointerException if either SampleModel or DataBuffer is null */ public static WritableRaster createWritableRaster(SampleModel sm, DataBuffer db, Point location) { if ((sm == null) || (db == null)) { throw new NullPointerException("SampleModel and DataBuffer cannot be null"); } if (location == null) { location = new Point(0,0); } int dataType = sm.getDataType(); if (sm instanceof PixelInterleavedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); } } else if (sm instanceof SinglePixelPackedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); case DataBuffer.TYPE_INT: return new IntegerInterleavedRaster(sm, db, location); } } else if (sm instanceof MultiPixelPackedSampleModel && dataType == DataBuffer.TYPE_BYTE && sm.getSampleSize(0) < 8) { return new BytePackedRaster(sm, db, location); } // we couldn't do anything special - do the generic thing return new SunWritableRaster(sm,db,location); }
/** * Creates a Raster with the specified SampleModel and DataBuffer. * The upper left corner of the Raster is given by the location argument. * If location is null, (0, 0) will be used. * @param sm the specified <code>SampleModel</code> * @param db the specified <code>DataBuffer</code> * @param location the upper-left corner of the <code>Raster</code> * @return a <code>Raster</code> with the specified * <code>SampleModel</code>, <code>DataBuffer</code>, and * location. * @throws RasterFormatException if computing either * <code>location.x + sm.getWidth()</code> or * <code>location.y + sm.getHeight()</code> results in integer * overflow * @throws RasterFormatException if <code>dataBuffer</code> has more * than one bank and the <code>sampleModel</code> is * PixelInterleavedSampleModel, SinglePixelPackedSampleModel, * or MultiPixelPackedSampleModel. * @throws NullPointerException if either SampleModel or DataBuffer is * null */ public static Raster createRaster(SampleModel sm, DataBuffer db, Point location) { if ((sm == null) || (db == null)) { throw new NullPointerException("SampleModel and DataBuffer cannot be null"); } if (location == null) { location = new Point(0,0); } int dataType = sm.getDataType(); if (sm instanceof PixelInterleavedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); } } else if (sm instanceof SinglePixelPackedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); case DataBuffer.TYPE_INT: return new IntegerInterleavedRaster(sm, db, location); } } else if (sm instanceof MultiPixelPackedSampleModel && dataType == DataBuffer.TYPE_BYTE && sm.getSampleSize(0) < 8) { return new BytePackedRaster(sm, db, location); } // we couldn't do anything special - do the generic thing return new Raster(sm,db,location); }
/** * Creates a WritableRaster with the specified SampleModel and DataBuffer. * The upper left corner of the Raster is given by the location argument. * If location is null, (0, 0) will be used. * @param sm the specified <code>SampleModel</code> * @param db the specified <code>DataBuffer</code> * @param location the upper-left corner of the * <code>WritableRaster</code> * @return a <code>WritableRaster</code> with the specified * <code>SampleModel</code>, <code>DataBuffer</code>, and * location. * @throws RasterFormatException if computing either * <code>location.x + sm.getWidth()</code> or * <code>location.y + sm.getHeight()</code> results in integer * overflow * @throws RasterFormatException if <code>dataBuffer</code> has more * than one bank and the <code>sampleModel</code> is * PixelInterleavedSampleModel, SinglePixelPackedSampleModel, * or MultiPixelPackedSampleModel. * @throws NullPointerException if either SampleModel or DataBuffer is null */ public static WritableRaster createWritableRaster(SampleModel sm, DataBuffer db, Point location) { if ((sm == null) || (db == null)) { throw new NullPointerException("SampleModel and DataBuffer cannot be null"); } if (location == null) { location = new Point(0,0); } int dataType = sm.getDataType(); if (sm instanceof PixelInterleavedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); } } else if (sm instanceof SinglePixelPackedSampleModel) { switch(dataType) { case DataBuffer.TYPE_BYTE: return new ByteInterleavedRaster(sm, db, location); case DataBuffer.TYPE_USHORT: return new ShortInterleavedRaster(sm, db, location); case DataBuffer.TYPE_INT: return new IntegerInterleavedRaster(sm, db, location); } } else if (sm instanceof MultiPixelPackedSampleModel && dataType == DataBuffer.TYPE_BYTE && sm.getSampleSize(0) < 8) { return new BytePackedRaster(sm, db, location); } // we couldn't do anything special - do the generic thing return new SunWritableRaster(sm,db,location); }