Example usage for javax.imageio.spi ImageReaderSpi canDecodeInput

List of usage examples for javax.imageio.spi ImageReaderSpi canDecodeInput

Introduction

In this page you can find the example usage for javax.imageio.spi ImageReaderSpi canDecodeInput.

Prototype

public abstract boolean canDecodeInput(Object source) throws IOException;

Source Link

Document

Returns true if the supplied source object appears to be of the format supported by this reader.

Usage

From source file:org.geotools.gce.imagemosaic.GranuleDescriptor.java

private void init(final BoundingBox granuleBBOX, final URL granuleUrl, final ImageReaderSpi suggestedSPI,
        final MultiLevelROI roiProvider, final boolean heterogeneousGranules,
        final boolean handleArtifactsFiltering, final Hints hints) {
    this.granuleBBOX = ReferencedEnvelope.reference(granuleBBOX);
    this.granuleUrl = granuleUrl;
    this.roiProvider = roiProvider;
    this.handleArtifactsFiltering = handleArtifactsFiltering;
    filterMe = handleArtifactsFiltering && roiProvider != null;

    // create the base grid to world transformation
    ImageInputStream inStream = null;
    ImageReader reader = null;/* ww  w .jav  a2 s  . co m*/
    try {
        //
        //get info about the raster we have to read
        //

        // get a stream
        if (cachedStreamSPI == null) {
            cachedStreamSPI = ImageIOExt.getImageInputStreamSPI(granuleUrl, true);
            if (cachedStreamSPI == null) {
                final File file = DataUtilities.urlToFile(granuleUrl);
                if (file != null) {
                    if (LOGGER.isLoggable(Level.WARNING)) {
                        LOGGER.log(Level.WARNING, Utils.getFileInfo(file));
                    }
                }
                throw new IllegalArgumentException(
                        "Unable to get an input stream for the provided granule " + granuleUrl.toString());
            }
        }
        assert cachedStreamSPI != null : "no cachedStreamSPI available!";
        inStream = cachedStreamSPI.createInputStreamInstance(granuleUrl, ImageIO.getUseCache(),
                ImageIO.getCacheDirectory());
        if (inStream == null) {
            final File file = DataUtilities.urlToFile(granuleUrl);
            if (file != null) {
                if (LOGGER.isLoggable(Level.WARNING)) {
                    LOGGER.log(Level.WARNING, Utils.getFileInfo(file));
                }
            }
            throw new IllegalArgumentException(
                    "Unable to get an input stream for the provided file " + granuleUrl.toString());
        }

        // get a reader and try to cache the suggested SPI first
        if (cachedReaderSPI == null) {
            inStream.mark();
            if (suggestedSPI != null && suggestedSPI.canDecodeInput(inStream)) {
                cachedReaderSPI = suggestedSPI;
                inStream.reset();
            } else {
                inStream.mark();
                reader = ImageIOExt.getImageioReader(inStream);
                if (reader != null)
                    cachedReaderSPI = reader.getOriginatingProvider();
                inStream.reset();
            }

        }
        if (reader == null) {
            if (cachedReaderSPI == null) {
                throw new IllegalArgumentException(
                        "Unable to get a ReaderSPI for the provided input: " + granuleUrl.toString());
            }
            reader = cachedReaderSPI.createReaderInstance();
        }

        if (reader == null)
            throw new IllegalArgumentException(
                    "Unable to get an ImageReader for the provided file " + granuleUrl.toString());
        boolean ignoreMetadata = customizeReaderInitialization(reader, hints);
        reader.setInput(inStream, false, ignoreMetadata);
        //get selected level and base level dimensions
        final Rectangle originalDimension = Utils.getDimension(0, reader);

        // build the g2W for this tile, in principle we should get it
        // somehow from the tile itself or from the index, but at the moment
        // we do not have such info, hence we assume that it is a simple
        // scale and translate
        this.geMapper = new GridToEnvelopeMapper(new GridEnvelope2D(originalDimension), granuleBBOX);
        geMapper.setPixelAnchor(PixelInCell.CELL_CENTER);//this is the default behavior but it is nice to write it down anyway
        this.baseGridToWorld = geMapper.createAffineTransform();

        // add the base level
        this.granuleLevels.put(Integer.valueOf(0),
                new GranuleOverviewLevelDescriptor(1, 1, originalDimension.width, originalDimension.height));

        ////////////////////// Setting overviewController ///////////////////////
        if (heterogeneousGranules) {
            // //
            //
            // Right now we are setting up overviewsController by assuming that
            // overviews are internal images as happens in TIFF images
            // We can improve this by leveraging on coverageReaders  
            //
            // //

            // Getting the first level descriptor
            final GranuleOverviewLevelDescriptor baseOverviewLevelDescriptor = granuleLevels.get(0);

            // Variables initialization
            final int numberOfOvervies = reader.getNumImages(true) - 1;
            final AffineTransform2D baseG2W = baseOverviewLevelDescriptor.getGridToWorldTransform();
            final int width = baseOverviewLevelDescriptor.getWidth();
            final int height = baseOverviewLevelDescriptor.getHeight();
            final double resX = AffineTransform2D.getScaleX0(baseG2W);
            final double resY = AffineTransform2D.getScaleY0(baseG2W);
            final double[] highestRes = new double[] { resX, resY };
            final double[][] overviewsResolution = new double[numberOfOvervies][2];

            // Populating overviews and initializing overviewsController
            for (int i = 0; i < numberOfOvervies; i++) {
                overviewsResolution[i][0] = (highestRes[0] * width) / reader.getWidth(i + 1);
                overviewsResolution[i][1] = (highestRes[1] * height) / reader.getHeight(i + 1);
            }
            overviewsController = new OverviewsController(highestRes, numberOfOvervies, overviewsResolution);
        }
        //////////////////////////////////////////////////////////////////////////

        if (hints != null && hints.containsKey(Utils.CHECK_AUXILIARY_METADATA)) {
            boolean checkAuxiliaryMetadata = (Boolean) hints.get(Utils.CHECK_AUXILIARY_METADATA);
            if (checkAuxiliaryMetadata) {
                checkPamDataset();
            }
        }

    } catch (IllegalStateException e) {
        throw new IllegalArgumentException(e);

    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        // close/dispose stream and readers
        try {
            if (inStream != null) {
                inStream.close();
            }
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        } finally {
            if (reader != null) {
                reader.dispose();
            }
        }
    }
}