Search in sources :

Example 6 with DataBufferShort

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

the class SignedColorModel method createCompatibleWritableRaster.

/* @see java.awt.image.ColorModel#createCompatibleWritableRaster(int, int) */
@Override
public WritableRaster createCompatibleWritableRaster(int w, int h) {
    if (pixelBits == 16) {
        int[] bandOffsets = new int[nChannels];
        for (int i = 0; i < nChannels; i++) bandOffsets[i] = i;
        SampleModel m = new ComponentSampleModel(DataBuffer.TYPE_SHORT, w, h, nChannels, w * nChannels, bandOffsets);
        DataBuffer db = new DataBufferShort(w * h, nChannels);
        return Raster.createWritableRaster(m, db, null);
    }
    return helper.createCompatibleWritableRaster(w, h);
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBuffer(java.awt.image.DataBuffer)

Example 7 with DataBufferShort

use of java.awt.image.DataBufferShort in project javacv by bytedeco.

the class GLCanvasFrame method showImage.

public void showImage(BufferedImage image) {
    if (image == null) {
        return;
    }
    this.color = null;
    this.width = image.getWidth();
    this.height = image.getHeight();
    DataBuffer buffer = image.getRaster().getDataBuffer();
    if (buffer instanceof DataBufferByte) {
        this.buffer = ByteBuffer.wrap(((DataBufferByte) buffer).getData());
        this.type = GL2.GL_UNSIGNED_BYTE;
    } else if (buffer instanceof DataBufferDouble) {
        this.buffer = DoubleBuffer.wrap(((DataBufferDouble) buffer).getData());
        this.type = GL2.GL_DOUBLE;
    } else if (buffer instanceof DataBufferFloat) {
        this.buffer = FloatBuffer.wrap(((DataBufferFloat) buffer).getData());
        this.type = GL2.GL_FLOAT;
    } else if (buffer instanceof DataBufferInt) {
        this.buffer = IntBuffer.wrap(((DataBufferInt) buffer).getData());
        this.type = GL2.GL_INT;
    } else if (buffer instanceof DataBufferShort) {
        this.buffer = ShortBuffer.wrap(((DataBufferShort) buffer).getData());
        this.type = GL2.GL_SHORT;
    } else if (buffer instanceof DataBufferUShort) {
        this.buffer = ShortBuffer.wrap(((DataBufferUShort) buffer).getData());
        this.type = GL2.GL_UNSIGNED_SHORT;
    } else {
        assert false;
    }
    switch(image.getSampleModel().getNumBands()) {
        case 1:
            this.format = GL2.GL_LUMINANCE;
            break;
        case 2:
            this.format = GL2.GL_RG;
            break;
        case 3:
            this.format = GL2.GL_RGB;
            break;
        case 4:
            this.format = GL2.GL_RGBA;
            break;
        default:
            assert false;
    }
    getGLCanvas().display();
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) DataBufferShort(java.awt.image.DataBufferShort) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferFloat(java.awt.image.DataBufferFloat) DataBufferUShort(java.awt.image.DataBufferUShort) DataBuffer(java.awt.image.DataBuffer)

Example 8 with DataBufferShort

use of java.awt.image.DataBufferShort 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 9 with DataBufferShort

use of java.awt.image.DataBufferShort 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)

Example 10 with DataBufferShort

use of java.awt.image.DataBufferShort in project jdk8u_jdk by JetBrains.

the class IncorrectClipXorModeSW2Surface method getBufferedImage.

private static BufferedImage getBufferedImage(int sw) {
    final BufferedImage bi = new BufferedImage(sw, sw, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, sw, sw);
    g2d.dispose();
    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBufferShort (java.awt.image.DataBufferShort)17 DataBuffer (java.awt.image.DataBuffer)16 DataBufferByte (java.awt.image.DataBufferByte)16 DataBufferInt (java.awt.image.DataBufferInt)15 DataBufferUShort (java.awt.image.DataBufferUShort)10 SampleModel (java.awt.image.SampleModel)10 ComponentSampleModel (java.awt.image.ComponentSampleModel)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)7 BufferedImage (java.awt.image.BufferedImage)6 Point (java.awt.Point)5 DataBufferDouble (java.awt.image.DataBufferDouble)5 DataBufferFloat (java.awt.image.DataBufferFloat)5 BandedSampleModel (java.awt.image.BandedSampleModel)3 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 Raster (java.awt.image.Raster)3 WritableRaster (java.awt.image.WritableRaster)3 ByteBuffer (java.nio.ByteBuffer)3 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2