Search in sources :

Example 1 with DataBufferFloat

use of java.awt.image.DataBufferFloat in project bioformats by openmicroscopy.

the class AWTImageTools method makeImage.

/**
 * Creates an image from the given float data.
 *
 * @param data Array containing image data.
 * @param w Width of image plane.
 * @param h Height of image plane.
 * @param c Number of channels.
 * @param interleaved If set, the channels are assumed to be interleaved;
 *   otherwise they are assumed to be sequential.
 *   For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
 *   while "RRR...GGG...BBB..." is sequential.
 */
public static BufferedImage makeImage(float[] data, int w, int h, int c, boolean interleaved) {
    if (c == 1)
        return makeImage(data, w, h);
    int dataType = DataBuffer.TYPE_FLOAT;
    DataBuffer buffer = new DataBufferFloat(data, c * w * h);
    return constructImage(c, dataType, w, h, interleaved, false, buffer);
}
Also used : DataBufferFloat(java.awt.image.DataBufferFloat) DataBuffer(java.awt.image.DataBuffer)

Example 2 with DataBufferFloat

use of java.awt.image.DataBufferFloat in project bioformats by openmicroscopy.

the class AWTImageTools method makeImage.

/**
 * Creates an image from the given single-precision floating point data.
 *
 * @param data Array containing image data.
 *   It is assumed that each channel corresponds to one element of the array.
 *   For example, for RGB data, data[0] is R, data[1] is G, and data[2] is B.
 * @param w Width of image plane.
 * @param h Height of image plane.
 */
public static BufferedImage makeImage(float[][] data, int w, int h) {
    int dataType = DataBuffer.TYPE_FLOAT;
    DataBuffer buffer = new DataBufferFloat(data, data[0].length);
    return constructImage(data.length, dataType, w, h, false, true, buffer);
}
Also used : DataBufferFloat(java.awt.image.DataBufferFloat) DataBuffer(java.awt.image.DataBuffer)

Example 3 with DataBufferFloat

use of java.awt.image.DataBufferFloat in project imageio-ext by geosolutions-it.

the class ImageIOUtils method makeGenericBandedWritableRaster.

/**
 * Returns a generic banded WritableRaster
 *
 * @param numElems
 * @param numLines
 * @param bandOffsets
 * @param dataType
 * @return
 */
public static WritableRaster makeGenericBandedWritableRaster(int numElems, int numLines, int numBands, int dataType) {
    int[] bandOffsets = new int[numBands];
    for (int i = 0; i < numBands; ++i) bandOffsets[i] = i;
    DataBuffer d = null;
    if (dataType == DataBuffer.TYPE_BYTE)
        d = new DataBufferByte(numElems * numLines * numBands);
    else if (dataType == DataBuffer.TYPE_FLOAT)
        d = new DataBufferFloat(numElems * numLines * numBands);
    else
        throw new IllegalArgumentException("Invalid datatype: " + dataType);
    BandedSampleModel bsm = new BandedSampleModel(dataType, numElems, numLines, bandOffsets.length, bandOffsets, bandOffsets);
    SunWritableRaster ras = new SunWritableRaster(bsm, d, new Point(0, 0));
    return ras;
}
Also used : BandedSampleModel(java.awt.image.BandedSampleModel) SunWritableRaster(sun.awt.image.SunWritableRaster) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) DataBufferFloat(java.awt.image.DataBufferFloat) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 4 with DataBufferFloat

use of java.awt.image.DataBufferFloat in project imageio-ext by geosolutions-it.

the class ImageIOUtils method makeGenericPixelInterleavedWritableRaster.

/**
 * Returns a generic pixel interleaved WritableRaster
 *
 * @param numElems
 * @param numLines
 * @param bandOffsets
 * @param dataType
 * @return
 */
public static WritableRaster makeGenericPixelInterleavedWritableRaster(int numElems, int numLines, int numBands, int dataType) {
    int[] bandOffsets = new int[numBands];
    for (int i = 0; i < numBands; ++i) bandOffsets[i] = i;
    DataBuffer d = null;
    if (dataType == DataBuffer.TYPE_BYTE)
        d = new DataBufferByte(numElems * numLines * numBands);
    else if (dataType == DataBuffer.TYPE_SHORT)
        d = new DataBufferShort(numElems * numLines * numBands);
    else if (dataType == DataBuffer.TYPE_USHORT)
        d = new DataBufferUShort(numElems * numLines * numBands);
    else if (dataType == DataBuffer.TYPE_FLOAT)
        d = new DataBufferFloat(numElems * numLines * numBands);
    else if (dataType == DataBuffer.TYPE_DOUBLE)
        d = new DataBufferDouble(numElems * numLines * numBands);
    else
        throw new IllegalArgumentException("Invalid datatype: " + dataType);
    PixelInterleavedSampleModel pism = new PixelInterleavedSampleModel(dataType, numElems, numLines, bandOffsets.length, numElems * bandOffsets.length, bandOffsets);
    SunWritableRaster ras = new SunWritableRaster(pism, d, new Point(0, 0));
    return ras;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) DataBufferDouble(java.awt.image.DataBufferDouble) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SunWritableRaster(sun.awt.image.SunWritableRaster) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) DataBufferFloat(java.awt.image.DataBufferFloat) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 5 with DataBufferFloat

use of java.awt.image.DataBufferFloat in project imageio-ext by geosolutions-it.

the class GDALImageReader method readDatasetRaster.

/**
 * Read data from the required region of the raster.
 *
 * @param destSM
 *                sample model for the image
 * @param dataset
 *                GDAL <code>Dataset</code> to read
 * @param srcRegion
 *                the source Region to be read
 * @param dstRegion
 *                the destination Region of the image read
 * @param selectedBands
 *                an array specifying the requested bands
 * @return the read <code>Raster</code>
 */
private Raster readDatasetRaster(SampleModel destSm, Dataset dataset, Rectangle srcRegion, Rectangle dstRegion, int[] selectedBands) throws IOException {
    SampleModel sampleModel = null;
    DataBuffer imgBuffer = null;
    Band pBand = null;
    try {
        int dstWidth = dstRegion.width;
        int dstHeight = dstRegion.height;
        int srcRegionXOffset = srcRegion.x;
        int srcRegionYOffset = srcRegion.y;
        int srcRegionWidth = srcRegion.width;
        int srcRegionHeight = srcRegion.height;
        if (LOGGER.isLoggable(Level.FINE))
            LOGGER.fine("SourceRegion = " + srcRegion.toString());
        // Getting number of bands
        final int nBands = selectedBands != null ? selectedBands.length : destSm.getNumBands();
        int[] banks = new int[nBands];
        int[] offsets = new int[nBands];
        // setting the number of pixels to read
        final int pixels = dstWidth * dstHeight;
        int bufferType = 0, bufferSize = 0;
        int typeSizeInBytes = 0;
        // ////////////////////////////////////////////////////////////////////
        // 
        // -------------------------------------------------------------------
        // Raster Creation >>> Step 2: Data Read
        // -------------------------------------------------------------------
        // 
        // ////////////////////////////////////////////////////////////////////
        // NOTE: Bands are not 0-base indexed, so we must add 1
        pBand = dataset.GetRasterBand(1);
        // setting buffer properties
        bufferType = pBand.getDataType();
        typeSizeInBytes = gdal.GetDataTypeSize(bufferType) / 8;
        bufferSize = nBands * pixels * typeSizeInBytes;
        // splitBands = false -> I read n Bands at once.
        // splitBands = false -> I need to read 1 Band at a time.
        boolean splitBands = false;
        if (bufferSize < 0 || destSm instanceof BandedSampleModel) {
            // The number resulting from the product
            // "numBands*pixels*gdal.GetDataTypeSize(buf_type) / 8"
            // may be negative (A very high number which is not
            // "int representable")
            // In such a case, we will read 1 band at a time.
            bufferSize = pixels * typeSizeInBytes;
            splitBands = true;
        }
        int dataBufferType = -1;
        byte[][] byteBands = new byte[nBands][];
        for (int k = 0; k < nBands; k++) {
            // I quit the loop
            if (k > 0 && !splitBands)
                break;
            final byte[] dataBuffer = new byte[bufferSize];
            final int returnVal;
            if (!splitBands) {
                // I can read nBands at once.
                final int[] bandsMap = new int[nBands];
                if (selectedBands != null) {
                    for (int i = 0; i < nBands; i++) bandsMap[i] = selectedBands[i] + 1;
                } else {
                    for (int i = 0; i < nBands; i++) bandsMap[i] = i + 1;
                }
                returnVal = dataset.ReadRaster(srcRegionXOffset, srcRegionYOffset, srcRegionWidth, srcRegionHeight, dstWidth, dstHeight, bufferType, dataBuffer, bandsMap, nBands * typeSizeInBytes, dstWidth * nBands * typeSizeInBytes, typeSizeInBytes);
                byteBands[k] = dataBuffer;
            } else {
                // I need to read 1 band at a time.
                Band rBand = null;
                try {
                    rBand = dataset.GetRasterBand(k + 1);
                    returnVal = rBand.ReadRaster(srcRegionXOffset, srcRegionYOffset, srcRegionWidth, srcRegionHeight, dstWidth, dstHeight, bufferType, dataBuffer);
                    byteBands[k] = dataBuffer;
                } finally {
                    if (rBand != null) {
                        try {
                            // Closing the band
                            rBand.delete();
                        } catch (Throwable e) {
                            if (LOGGER.isLoggable(Level.FINEST))
                                LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
                        }
                    }
                }
            }
            if (returnVal == gdalconstConstants.CE_None) {
                if (!splitBands)
                    for (int band = 0; band < nBands; band++) {
                        banks[band] = band;
                        offsets[band] = band;
                    }
                else {
                    banks[k] = k;
                    offsets[k] = 0;
                }
            } else {
                // The read operation was not successfully computed.
                // Showing error messages.
                LOGGER.info(new StringBuilder("Last error: ").append(gdal.GetLastErrorMsg()).toString());
                LOGGER.info(new StringBuilder("Last error number: ").append(gdal.GetLastErrorNo()).toString());
                LOGGER.info(new StringBuilder("Last error type: ").append(gdal.GetLastErrorType()).toString());
                throw new RuntimeException(gdal.GetLastErrorMsg());
            }
        }
        // /////////////////////////////////////////////////////////////////////
        if (bufferType == gdalconstConstants.GDT_Byte) {
            if (!splitBands) {
                // final byte[] bytes = new byte[nBands * pixels];
                // bands[0].get(bytes, 0, nBands * pixels);
                imgBuffer = new DataBufferByte(byteBands[0], nBands * pixels);
            } else {
                // final byte[][] bytes = new byte[nBands][];
                // for (int i = 0; i < nBands; i++) {
                // //                    bytes[i] = new byte[pixels];
                // bands[i].get(bytes[i], 0, pixels);
                // }
                imgBuffer = new DataBufferByte(byteBands, pixels);
            }
            dataBufferType = DataBuffer.TYPE_BYTE;
        } else {
            ByteBuffer[] bands = new ByteBuffer[nBands];
            for (int k = 0; (splitBands && k < nBands) || (k < 1 && !splitBands); k++) {
                bands[k] = ByteBuffer.wrap(byteBands[k], 0, byteBands[k].length);
            }
            if (bufferType == gdalconstConstants.GDT_Int16 || bufferType == gdalconstConstants.GDT_UInt16) {
                if (!splitBands) {
                    // I get short values from the ByteBuffer using a view
                    // of the ByteBuffer as a ShortBuffer
                    // It is worth to create the view outside the loop.
                    short[] shorts = new short[nBands * pixels];
                    bands[0].order(ByteOrder.nativeOrder());
                    final ShortBuffer buff = bands[0].asShortBuffer();
                    buff.get(shorts, 0, nBands * pixels);
                    if (bufferType == gdalconstConstants.GDT_Int16)
                        imgBuffer = new DataBufferShort(shorts, nBands * pixels);
                    else
                        imgBuffer = new DataBufferUShort(shorts, nBands * pixels);
                } else {
                    short[][] shorts = new short[nBands][];
                    for (int i = 0; i < nBands; i++) {
                        shorts[i] = new short[pixels];
                        bands[i].order(ByteOrder.nativeOrder());
                        bands[i].asShortBuffer().get(shorts[i], 0, pixels);
                    }
                    if (bufferType == gdalconstConstants.GDT_Int16)
                        imgBuffer = new DataBufferShort(shorts, pixels);
                    else
                        imgBuffer = new DataBufferUShort(shorts, pixels);
                }
                if (bufferType == gdalconstConstants.GDT_UInt16)
                    dataBufferType = DataBuffer.TYPE_USHORT;
                else
                    dataBufferType = DataBuffer.TYPE_SHORT;
            } else if (bufferType == gdalconstConstants.GDT_Int32 || bufferType == gdalconstConstants.GDT_UInt32) {
                if (!splitBands) {
                    // I get int values from the ByteBuffer using a view
                    // of the ByteBuffer as an IntBuffer
                    // It is worth to create the view outside the loop.
                    int[] ints = new int[nBands * pixels];
                    bands[0].order(ByteOrder.nativeOrder());
                    final IntBuffer buff = bands[0].asIntBuffer();
                    buff.get(ints, 0, nBands * pixels);
                    imgBuffer = new DataBufferInt(ints, nBands * pixels);
                } else {
                    int[][] ints = new int[nBands][];
                    for (int i = 0; i < nBands; i++) {
                        ints[i] = new int[pixels];
                        bands[i].order(ByteOrder.nativeOrder());
                        bands[i].asIntBuffer().get(ints[i], 0, pixels);
                    }
                    imgBuffer = new DataBufferInt(ints, pixels);
                }
                dataBufferType = DataBuffer.TYPE_INT;
            } else if (bufferType == gdalconstConstants.GDT_Float32) {
                if (!splitBands) {
                    // I get float values from the ByteBuffer using a view
                    // of the ByteBuffer as a FloatBuffer
                    // It is worth to create the view outside the loop.
                    float[] floats = new float[nBands * pixels];
                    bands[0].order(ByteOrder.nativeOrder());
                    final FloatBuffer buff = bands[0].asFloatBuffer();
                    buff.get(floats, 0, nBands * pixels);
                    imgBuffer = new DataBufferFloat(floats, nBands * pixels);
                } else {
                    float[][] floats = new float[nBands][];
                    for (int i = 0; i < nBands; i++) {
                        floats[i] = new float[pixels];
                        bands[i].order(ByteOrder.nativeOrder());
                        bands[i].asFloatBuffer().get(floats[i], 0, pixels);
                    }
                    imgBuffer = new DataBufferFloat(floats, pixels);
                }
                dataBufferType = DataBuffer.TYPE_FLOAT;
            } else if (bufferType == gdalconstConstants.GDT_Float64) {
                if (!splitBands) {
                    // I get double values from the ByteBuffer using a view
                    // of the ByteBuffer as a DoubleBuffer
                    // It is worth to create the view outside the loop.
                    double[] doubles = new double[nBands * pixels];
                    bands[0].order(ByteOrder.nativeOrder());
                    final DoubleBuffer buff = bands[0].asDoubleBuffer();
                    buff.get(doubles, 0, nBands * pixels);
                    imgBuffer = new DataBufferDouble(doubles, nBands * pixels);
                } else {
                    double[][] doubles = new double[nBands][];
                    for (int i = 0; i < nBands; i++) {
                        doubles[i] = new double[pixels];
                        bands[i].order(ByteOrder.nativeOrder());
                        bands[i].asDoubleBuffer().get(doubles[i], 0, pixels);
                    }
                    imgBuffer = new DataBufferDouble(doubles, pixels);
                }
                dataBufferType = DataBuffer.TYPE_DOUBLE;
            } else {
                // TODO: Handle more cases if needed. Show the name of the type
                // instead of the numeric value.
                LOGGER.info("The specified data type is actually unsupported: " + bufferType);
            }
        }
        // TODO: Fix this in compliance with the specified destSampleModel
        if (splitBands)
            sampleModel = new BandedSampleModel(dataBufferType, dstWidth, dstHeight, dstWidth, banks, offsets);
        else
            sampleModel = new PixelInterleavedSampleModel(dataBufferType, dstWidth, dstHeight, nBands, dstWidth * nBands, offsets);
    } finally {
        if (pBand != null) {
            try {
                // Closing the band
                pBand.delete();
            } catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINE))
                    LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
            }
        }
    }
    // dstRegion.x, dstRegion.y));
    return Raster.createWritableRaster(sampleModel, imgBuffer, null);
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) Band(org.gdal.gdal.Band) FloatBuffer(java.nio.FloatBuffer) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferShort(java.awt.image.DataBufferShort) BandedSampleModel(java.awt.image.BandedSampleModel) DataBufferUShort(java.awt.image.DataBufferUShort) DataBuffer(java.awt.image.DataBuffer) DoubleBuffer(java.nio.DoubleBuffer) ByteBuffer(java.nio.ByteBuffer) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) IntBuffer(java.nio.IntBuffer) DataBufferFloat(java.awt.image.DataBufferFloat) ShortBuffer(java.nio.ShortBuffer)

Aggregations

DataBuffer (java.awt.image.DataBuffer)8 DataBufferFloat (java.awt.image.DataBufferFloat)8 DataBufferByte (java.awt.image.DataBufferByte)6 DataBufferDouble (java.awt.image.DataBufferDouble)5 DataBufferShort (java.awt.image.DataBufferShort)5 DataBufferUShort (java.awt.image.DataBufferUShort)5 DataBufferInt (java.awt.image.DataBufferInt)4 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 SampleModel (java.awt.image.SampleModel)3 Point (java.awt.Point)2 BandedSampleModel (java.awt.image.BandedSampleModel)2 ComponentSampleModel (java.awt.image.ComponentSampleModel)2 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)2 Raster (java.awt.image.Raster)2 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)2 WritableRaster (java.awt.image.WritableRaster)2 ByteBuffer (java.nio.ByteBuffer)2 DoubleBuffer (java.nio.DoubleBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2