Example usage for javax.imageio.spi ImageReaderSpi createReaderInstance

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

Introduction

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

Prototype

public ImageReader createReaderInstance() throws IOException 

Source Link

Document

Returns an instance of the ImageReader implementation associated with this service provider.

Usage

From source file:org.geotools.coverage.io.util.Utilities.java

/**
 * Returns a {@code PlanarImage} given a set of parameter specifying the type of read operation to be performed.
 * /*from   ww w . j  a v a  2 s. co  m*/
 * @param imageIndex
 * 
 * @param new FileImageInputStreamExtImplinput the input {@code ImageInputStream} to be used for reading the image.
 * @param useJAI {@code true} if we need to use a JAI ImageRead operation, {@code false} if we need a simple direct {@code ImageReader.read(...)}
 *        call.
 * @param imageReadParam an {@code ImageReadParam} specifying the read parameters
 * @param useMultithreading {@code true} if a JAI ImageRead operation is requested with support for multithreading. This parameter will be ignored
 *        if requesting a direct read operation.
 * @return the read {@code PlanarImage}
 * @throws IOException
 */
public static PlanarImage readImage(final ImageReaderSpi spi, final Object input, final int imageIndex,
        final boolean useJAI, final ImageReadParam imageReadParam, final boolean useMultithreading)
        throws IOException {
    ImageInputStream paramInput = null;
    if (input instanceof File) {
        paramInput = new FileImageInputStreamExtImpl((File) input);
    } else if (input instanceof FileImageInputStreamExt) {
        paramInput = (FileImageInputStreamExt) input;
    } else if (input instanceof URIImageInputStream) {
        paramInput = (URIImageInputStream) input;
    } else if (input instanceof URL) {
        final URL tempURL = (URL) input;
        String protocol = tempURL.getProtocol();
        if (protocol.equalsIgnoreCase("file")) {
            try {
                File file = it.geosolutions.imageio.utilities.Utilities.urlToFile(tempURL);
                paramInput = new FileImageInputStreamExtImpl(file);
            } catch (IOException e) {
                throw new RuntimeException("Failed to create a valid input stream ", e);
            }
        } else if (tempURL.getProtocol().toLowerCase().startsWith("http")
                || tempURL.getProtocol().equalsIgnoreCase("dods")) {
            try {
                paramInput = new URIImageInputStreamImpl(tempURL.toURI());
            } catch (URISyntaxException e) {
                throw new RuntimeException("Failed to create a valid input stream ", e);
            }
        }
    } else
        throw new IllegalArgumentException("Unsupported Input type:" + input);

    PlanarImage planarImage;
    ImageReader reader;
    ImageReaderSpi readerSpi = spi;
    if (useJAI) {
        final ParameterBlock pbjImageRead = new ParameterBlock();
        pbjImageRead.add(paramInput);
        pbjImageRead.add(imageIndex);
        pbjImageRead.add(Boolean.FALSE);
        pbjImageRead.add(Boolean.FALSE);
        pbjImageRead.add(Boolean.FALSE);
        pbjImageRead.add(null);
        pbjImageRead.add(null);
        pbjImageRead.add(imageReadParam);
        reader = readerSpi.createReaderInstance();
        pbjImageRead.add(reader);

        // Check if to use a simple JAI ImageRead operation or a
        // multithreaded one
        final String jaiOperation = useMultithreading ? "ImageReadMT" : "ImageRead";
        // final String jaiOperation = "ImageRead";
        /** TODO: SET HINTS */
        planarImage = JAI.create(jaiOperation, pbjImageRead, null);
    } else {
        reader = readerSpi.createReaderInstance();
        reader.setInput(paramInput, true, true);
        planarImage = PlanarImage.wrapRenderedImage(reader.read(imageIndex, imageReadParam));
        reader.dispose();
    }
    return planarImage;
}