Search in sources :

Example 1 with ArrayByte

use of ucar.ma2.ArrayByte in project imageio-ext by geosolutions-it.

the class GRIB1ImageReader method read.

/**
 * @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam)
 */
@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    BufferedImage image = null;
    Variable variable = null;
    Range indexRange = null;
    GribVariableWrapper wrapper = null;
    for (Range range : indexMap.keySet()) {
        if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
            wrapper = indexMap.get(range);
            indexRange = range;
            break;
        }
    }
    variable = wrapper.getVariable();
    /*
         * Fetches the parameters that are not already processed by utility
         * methods like 'getDestination' or 'computeRegions' (invoked below).
         */
    final int strideX, strideY;
    final int[] srcBands, dstBands;
    if (param != null) {
        strideX = param.getSourceXSubsampling();
        strideY = param.getSourceYSubsampling();
        srcBands = param.getSourceBands();
        dstBands = param.getDestinationBands();
    } else {
        strideX = 1;
        strideY = 1;
        srcBands = null;
        dstBands = null;
    }
    final int rank = wrapper.getRank();
    final int bandDimension = rank - NetCDFUtilities.Z_DIMENSION;
    /*
         * Gets the destination image of appropriate size. We create it now
         * since it is a convenient way to get the number of destination bands.
         */
    final int width = wrapper.getWidth();
    final int height = wrapper.getHeight();
    /*
         * Computes the source region (in the NetCDF file) and the destination
         * region (in the buffered image). Copies those informations into UCAR
         * Range structure.
         */
    final Rectangle srcRegion = new Rectangle();
    final Rectangle destRegion = new Rectangle();
    computeRegions(param, width, height, null, srcRegion, destRegion);
    // flipVertically(param, height, srcRegion);
    int destWidth = destRegion.x + destRegion.width;
    int destHeight = destRegion.y + destRegion.height;
    final List<Range> ranges = new LinkedList<Range>();
    for (int i = 0; i < rank; i++) {
        final int first, length, stride;
        switch(rank - i) {
            case NetCDFUtilities.X_DIMENSION:
                {
                    first = srcRegion.x;
                    length = srcRegion.width;
                    stride = strideX;
                    break;
                }
            case NetCDFUtilities.Y_DIMENSION:
                {
                    first = srcRegion.y;
                    length = srcRegion.height;
                    stride = strideY;
                    break;
                }
            default:
                {
                    if (i == bandDimension) {
                        first = NetCDFUtilities.getZIndex(variable, indexRange, imageIndex);
                    } else {
                        first = NetCDFUtilities.getTIndex(variable, indexRange, imageIndex);
                    }
                    length = 1;
                    stride = 1;
                    break;
                }
        }
        try {
            ranges.add(new Range(first, first + length - 1, stride));
        } catch (InvalidRangeException e) {
        }
    }
    final Section sections = new Section(ranges);
    /*
         * Setting SampleModel and ColorModel.
         */
    SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
    ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
    /*
         * Reads the requested sub-region only.
         */
    final int numDstBands = 1;
    final int size = destHeight * destWidth * numDstBands;
    for (int zi = 0; zi < numDstBands; zi++) {
        final int dstBand = (dstBands == null) ? zi : dstBands[zi];
        final Array array;
        try {
            array = variable.read(sections);
            DataBuffer dataBuffer = null;
            if (array instanceof ArrayByte) {
                dataBuffer = new DataBufferByte((byte[]) array.get1DJavaArray(byte.class), size);
            } else if (array instanceof ArrayShort) {
                dataBuffer = new DataBufferShort((short[]) array.get1DJavaArray(short.class), size);
            } else if (array instanceof ArrayInt) {
                dataBuffer = new DataBufferInt((int[]) array.get1DJavaArray(int.class), size);
            } else if (array instanceof ArrayFloat) {
                dataBuffer = new DataBufferFloat((float[]) array.get1DJavaArray(float.class), size);
            } else if (array instanceof ArrayDouble) {
                dataBuffer = new DataBufferDouble((double[]) array.get1DJavaArray(double.class), size);
            }
            WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
        } catch (InvalidRangeException e) {
        }
    }
    return image;
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) Variable(ucar.nc2.Variable) Rectangle(java.awt.Rectangle) ArrayFloat(ucar.ma2.ArrayFloat) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBufferShort(java.awt.image.DataBufferShort) ArrayDouble(ucar.ma2.ArrayDouble) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ArrayByte(ucar.ma2.ArrayByte) ArrayShort(ucar.ma2.ArrayShort) DataBuffer(java.awt.image.DataBuffer) InvalidRangeException(ucar.ma2.InvalidRangeException) Point(java.awt.Point) Range(ucar.ma2.Range) Section(ucar.ma2.Section) Point(java.awt.Point) LinkedList(java.util.LinkedList) Array(ucar.ma2.Array) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) ArrayInt(ucar.ma2.ArrayInt) DataBufferFloat(java.awt.image.DataBufferFloat)

Example 2 with ArrayByte

use of ucar.ma2.ArrayByte in project imageio-ext by geosolutions-it.

the class NetCDFConverterUtilities method getRangeArray.

public static Array getRangeArray(DataType varDataType) {
    int[] dim = new int[] { 2 };
    if (varDataType == DataType.FLOAT) {
        Array array = new ArrayFloat(dim);
        Index index = array.getIndex();
        array.setFloat(index.set(0), Float.MIN_VALUE);
        array.setFloat(index.set(1), Float.MAX_VALUE);
        return array;
    } else if (varDataType == DataType.DOUBLE) {
        Array array = new ArrayDouble(dim);
        Index index = array.getIndex();
        array.setDouble(index.set(0), Double.MIN_VALUE);
        array.setDouble(index.set(1), Double.MAX_VALUE);
        return array;
    } else if (varDataType == DataType.BYTE) {
        Array array = new ArrayByte(dim);
        Index index = array.getIndex();
        array.setByte(index.set(0), Byte.MIN_VALUE);
        array.setByte(index.set(1), Byte.MAX_VALUE);
        return array;
    } else if (varDataType == DataType.SHORT) {
        Array array = new ArrayShort(dim);
        Index index = array.getIndex();
        array.setShort(index.set(0), Short.MIN_VALUE);
        array.setShort(index.set(1), Short.MAX_VALUE);
        return array;
    } else if (varDataType == DataType.INT) {
        Array array = new ArrayInt(dim);
        Index index = array.getIndex();
        array.setInt(index.set(0), Integer.MIN_VALUE);
        array.setInt(index.set(1), Integer.MAX_VALUE);
        return array;
    }
    throw new IllegalArgumentException("Actually unsupported Datatype");
}
Also used : Array(ucar.ma2.Array) ArrayDouble(ucar.ma2.ArrayDouble) ArrayFloat(ucar.ma2.ArrayFloat) Index(ucar.ma2.Index) ArrayByte(ucar.ma2.ArrayByte) ArrayInt(ucar.ma2.ArrayInt) ArrayShort(ucar.ma2.ArrayShort)

Example 3 with ArrayByte

use of ucar.ma2.ArrayByte in project imageio-ext by geosolutions-it.

the class BaseHDF4ImageReader method read2DVariable.

protected BufferedImage read2DVariable(final int imageIndex, final ImageReadParam param) throws IOException {
    BufferedImage image = null;
    final HDF4DatasetWrapper wrapper = getDatasetWrapper(imageIndex);
    final Variable variable = wrapper.getVariable();
    /*
         * Fetches the parameters that are not already processed by utility
         * methods like 'getDestination' or 'computeRegions' (invoked below).
         */
    final int strideX, strideY;
    final int[] srcBands, dstBands;
    if (param != null) {
        strideX = param.getSourceXSubsampling();
        strideY = param.getSourceYSubsampling();
        srcBands = param.getSourceBands();
        dstBands = param.getDestinationBands();
    } else {
        strideX = 1;
        strideY = 1;
        srcBands = null;
        dstBands = null;
    }
    final int rank = variable.getRank();
    /*
         * Gets the destination image of appropriate size. We create it now
         * since it is a convenient way to get the number of destination bands.
         */
    final int width = wrapper.getWidth();
    final int height = wrapper.getHeight();
    final int numBands = wrapper.getNumBands();
    /*
         * Computes the source region (in the NetCDF file) and the destination
         * region (in the buffered image). Copies those informations into UCAR
         * Range structure.
         */
    final Rectangle srcRegion = new Rectangle();
    final Rectangle destRegion = new Rectangle();
    computeRegions(param, width, height, null, srcRegion, destRegion);
    // flipVertically(param, height, srcRegion);
    int destWidth = destRegion.x + destRegion.width;
    int destHeight = destRegion.y + destRegion.height;
    final List<Range> ranges = new LinkedList<Range>();
    for (int i = 0; i < rank; i++) {
        final int first, length, stride;
        switch(i) {
            case 1:
                {
                    first = srcRegion.x;
                    length = srcRegion.width;
                    stride = strideX;
                    break;
                }
            case 0:
                {
                    first = srcRegion.y;
                    length = srcRegion.height;
                    stride = strideY;
                    break;
                }
            default:
                {
                    first = 0;
                    length = numBands;
                    stride = 1;
                    break;
                }
        }
        try {
            ranges.add(new Range(first, first + length - 1, stride));
        } catch (InvalidRangeException e) {
        // TODO LOGME
        }
    }
    final Section sections = new Section(ranges);
    /*
         * Setting SampleModel and ColorModel.
         */
    final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
    final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
    /*
         * Reads the requested sub-region only.
         */
    final int size = destHeight * destWidth * numBands;
    Array array = null;
    try {
        array = variable.read(sections);
        DataBuffer dataBuffer = null;
        if (array instanceof ArrayByte) {
            dataBuffer = new DataBufferByte((byte[]) array.get1DJavaArray(byte.class), size);
        } else if (array instanceof ArrayShort) {
            dataBuffer = new DataBufferShort((short[]) array.get1DJavaArray(short.class), size);
        } else if (array instanceof ArrayInt) {
            dataBuffer = new DataBufferInt((int[]) array.get1DJavaArray(int.class), size);
        } else if (array instanceof ArrayFloat) {
            dataBuffer = new DataBufferFloat((float[]) array.get1DJavaArray(float.class), size);
        } else if (array instanceof ArrayDouble) {
            dataBuffer = new DataBufferDouble((double[]) array.get1DJavaArray(double.class), size);
        }
        WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
        image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    } catch (InvalidRangeException e) {
    // TODO LOGME
    }
    return image;
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) Variable(ucar.nc2.Variable) Rectangle(java.awt.Rectangle) ArrayFloat(ucar.ma2.ArrayFloat) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBufferShort(java.awt.image.DataBufferShort) ArrayDouble(ucar.ma2.ArrayDouble) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ArrayByte(ucar.ma2.ArrayByte) ArrayShort(ucar.ma2.ArrayShort) DataBuffer(java.awt.image.DataBuffer) InvalidRangeException(ucar.ma2.InvalidRangeException) Point(java.awt.Point) Range(ucar.ma2.Range) Section(ucar.ma2.Section) Point(java.awt.Point) LinkedList(java.util.LinkedList) Array(ucar.ma2.Array) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ArrayInt(ucar.ma2.ArrayInt) DataBufferFloat(java.awt.image.DataBufferFloat)

Aggregations

Array (ucar.ma2.Array)3 ArrayByte (ucar.ma2.ArrayByte)3 ArrayDouble (ucar.ma2.ArrayDouble)3 ArrayFloat (ucar.ma2.ArrayFloat)3 ArrayInt (ucar.ma2.ArrayInt)3 ArrayShort (ucar.ma2.ArrayShort)3 Point (java.awt.Point)2 Rectangle (java.awt.Rectangle)2 BandedSampleModel (java.awt.image.BandedSampleModel)2 BufferedImage (java.awt.image.BufferedImage)2 ColorModel (java.awt.image.ColorModel)2 DataBuffer (java.awt.image.DataBuffer)2 DataBufferByte (java.awt.image.DataBufferByte)2 DataBufferDouble (java.awt.image.DataBufferDouble)2 DataBufferFloat (java.awt.image.DataBufferFloat)2 DataBufferInt (java.awt.image.DataBufferInt)2 DataBufferShort (java.awt.image.DataBufferShort)2 SampleModel (java.awt.image.SampleModel)2 WritableRaster (java.awt.image.WritableRaster)2 LinkedList (java.util.LinkedList)2