Search in sources :

Example 26 with Range

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

the class NetCDFImageReader method initialize.

/**
 * Initialize main properties for this reader.
 *
 * @throws exception
 *                 {@link InvalidRangeException}
 */
protected synchronized void initialize() {
    int numImages = 0;
    final Map<Range, NetCDFVariableWrapper> indexMap = new HashMap<Range, NetCDFVariableWrapper>();
    final NetcdfDataset dataset = reader.getDataset();
    try {
        if (dataset != null) {
            checkType = NetCDFUtilities.getCheckType(dataset);
            final List<Variable> variables = dataset.getVariables();
            if (variables != null) {
                for (final Variable variable : variables) {
                    if (variable != null && variable instanceof VariableDS) {
                        if (!NetCDFUtilities.isVariableAccepted(variable, checkType))
                            continue;
                        int[] shape = variable.getShape();
                        switch(shape.length) {
                            case 2:
                                indexMap.put(new Range(numImages, numImages + 1), new NetCDFVariableWrapper(variable));
                                numImages++;
                                break;
                            case 3:
                                indexMap.put(new Range(numImages, numImages + shape[0]), new NetCDFVariableWrapper(variable));
                                numImages += shape[0];
                                break;
                            case 4:
                                indexMap.put(new Range(numImages, numImages + shape[0] * shape[1]), new NetCDFVariableWrapper(variable));
                                numImages += shape[0] * shape[1];
                                break;
                        }
                    }
                }
            }
        } else
            throw new IllegalArgumentException("Not a valid dataset has been found");
    } catch (InvalidRangeException e) {
        throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
    }
    reader.setIndexMap(indexMap);
    setNumImages(numImages);
    reader.setNumImages(numImages);
    int numAttribs = 0;
    final List<Attribute> globalAttributes = dataset.getGlobalAttributes();
    if (globalAttributes != null && !globalAttributes.isEmpty())
        numAttribs = globalAttributes.size();
    reader.setNumGlobalAttributes(numAttribs);
}
Also used : Variable(ucar.nc2.Variable) HashMap(java.util.HashMap) Attribute(ucar.nc2.Attribute) InvalidRangeException(ucar.ma2.InvalidRangeException) VariableDS(ucar.nc2.dataset.VariableDS) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset) Range(ucar.ma2.Range) Point(java.awt.Point)

Example 27 with Range

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

the class NetCDFImageReader method read.

/**
 * @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam)
 */
@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    clearAbortRequest();
    Variable variable = null;
    Range indexRange = null;
    NetCDFVariableWrapper wrapper = null;
    Map<Range, ?> indexMap = reader.getIndexMap();
    for (Range range : indexMap.keySet()) {
        if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
            wrapper = (NetCDFVariableWrapper) 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) {
            throw netcdfFailure(e);
        }
    }
    final Section sections = new Section(ranges);
    /*
         * Setting SampleModel and ColorModel.
         */
    final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
    final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
    final WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
    final BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    /*
         * Reads the requested sub-region only.
         */
    processImageStarted(imageIndex);
    final int numDstBands = 1;
    final float toPercent = 100f / numDstBands;
    final int type = raster.getSampleModel().getDataType();
    final int xmin = destRegion.x;
    final int ymin = destRegion.y;
    final int xmax = destRegion.width + xmin;
    final int ymax = destRegion.height + ymin;
    for (int zi = 0; zi < numDstBands; zi++) {
        // final int srcBand = (srcBands == null) ? zi : srcBands[zi];
        final int dstBand = (dstBands == null) ? zi : dstBands[zi];
        final Array array;
        try {
            array = variable.read(sections);
        } catch (InvalidRangeException e) {
            throw netcdfFailure(e);
        }
        final IndexIterator it = array.getIndexIterator();
        // for (int y = ymax; --y >= ymin;) {
        for (int y = ymin; y < ymax; y++) {
            for (int x = xmin; x < xmax; x++) {
                switch(type) {
                    case DataBuffer.TYPE_DOUBLE:
                        {
                            raster.setSample(x, y, dstBand, it.getDoubleNext());
                            break;
                        }
                    case DataBuffer.TYPE_FLOAT:
                        {
                            raster.setSample(x, y, dstBand, it.getFloatNext());
                            break;
                        }
                    case DataBuffer.TYPE_BYTE:
                        {
                            byte b = it.getByteNext();
                            // int myByte = (0x000000FF & ((int) b));
                            // short anUnsignedByte = (short) myByte;
                            // raster.setSample(x, y, dstBand, anUnsignedByte);
                            raster.setSample(x, y, dstBand, b);
                            break;
                        }
                    default:
                        {
                            raster.setSample(x, y, dstBand, it.getIntNext());
                            break;
                        }
                }
            }
        }
        /*
             * Checks for abort requests after reading. It would be a waste of a
             * potentially good image (maybe the abort request occurred after we
             * just finished the reading) if we didn't implemented the
             * 'isCancel()' method. But because of the later, which is checked
             * by the NetCDF library, we can't assume that the image is
             * complete.
             */
        if (abortRequested()) {
            processReadAborted();
            return image;
        }
        /*
             * Reports progress here, not in the deeper loop, because the costly
             * part is the call to 'variable.read(...)' which can't report
             * progress. The loop that copy pixel values is fast, so reporting
             * progress there would be pointless.
             */
        processImageProgress(zi * toPercent);
    }
    if (lastError != null) {
        throw new IIOException(lastError);
    }
    processImageComplete();
    return image;
}
Also used : Variable(ucar.nc2.Variable) InvalidRangeException(ucar.ma2.InvalidRangeException) Rectangle(java.awt.Rectangle) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IndexIterator(ucar.ma2.IndexIterator) Range(ucar.ma2.Range) Section(ucar.ma2.Section) Point(java.awt.Point) LinkedList(java.util.LinkedList) BufferedImage(java.awt.image.BufferedImage) Array(ucar.ma2.Array) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster)

Example 28 with Range

use of ucar.ma2.Range 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

Range (org.apache.commons.lang3.Range)11 Date (java.util.Date)9 Test (org.junit.jupiter.api.Test)9 Variable (ucar.nc2.Variable)9 Array (ucar.ma2.Array)8 Range (ucar.ma2.Range)8 InvalidRangeException (ucar.ma2.InvalidRangeException)7 Attribute (ucar.nc2.Attribute)6 Point (java.awt.Point)5 HashMap (java.util.HashMap)5 IOException (java.io.IOException)4 ArrayFloat (ucar.ma2.ArrayFloat)4 ArrayShort (ucar.ma2.ArrayShort)4 DataType (ucar.ma2.DataType)4 NetcdfDataset (ucar.nc2.dataset.NetcdfDataset)4 Range (controlP5.Range)3 Slider (controlP5.Slider)3 Toggle (controlP5.Toggle)3 Rectangle (java.awt.Rectangle)3 BandedSampleModel (java.awt.image.BandedSampleModel)3