Search in sources :

Example 1 with FileImageInputStreamExt

use of it.geosolutions.imageio.stream.input.FileImageInputStreamExt in project imageio-ext by geosolutions-it.

the class JPEGWriterTest method testExifReplace.

@Test
@Ignore
public void testExifReplace() throws IOException {
    EXIFMetadata exif = initExif();
    FileImageInputStreamExt inStream = new FileImageInputStreamExtImpl(new File("/media/bigdisk/data/turbojpeg/lastExif.jpeg"));
    EXIFUtilities.replaceEXIFs(inStream, exif);
}
Also used : EXIFMetadata(it.geosolutions.imageio.plugins.exif.EXIFMetadata) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) FileImageInputStreamExtImpl(it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with FileImageInputStreamExt

use of it.geosolutions.imageio.stream.input.FileImageInputStreamExt in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readExternalMasksOvr.

@Test
public void readExternalMasksOvr() throws IOException {
    // Reading file with external mask and external mask overviews
    final File file = TestData.file(this, "external2.tif");
    // Read parameters
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 2, 2));
    // Creating a new Reader
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    // Using FileImageInputStreamExt for being able to locate the file path
    FileImageInputStreamExt inputStream = new FileImageInputStreamExtImpl(file);
    try {
        // Reading
        reader.setInput(inputStream);
        // IMAGE 0
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        // Getting Stream Metadata
        IIOMetadata metadata = reader.getStreamMetadata();
        Node tree = metadata.getAsTree("com_sun_media_imageio_plugins_tiff_stream_1.0");
        // Ensuring not null
        Assert.assertNotNull(tree);
        // Checking Childs
        NodeList list = tree.getChildNodes();
        int len = list.getLength();
        // Loop on the Node list
        for (int i = 0; i < len; i++) {
            Node node = list.item(i);
            // Ensuring not null
            Assert.assertNotNull(node);
            // Getting the name
            String nodeName = node.getNodeName();
            // Checking attributes
            Assert.assertTrue(node.hasAttributes());
            // Getting Attribute Value
            String value = node.getAttributes().item(0).getNodeValue();
            // Getting related enum
            MetadataNode mnode = MetadataNode.getFromName(nodeName);
            // Checking Attribute value
            switch(mnode) {
                case B_ORDER:
                    Assert.assertTrue(value.equalsIgnoreCase(ByteOrder.LITTLE_ENDIAN.toString()));
                    break;
                case N_INT_MASK:
                    Assert.assertEquals(0, Integer.parseInt(value));
                    break;
                case N_EXT_MASK:
                    Assert.assertEquals(1, Integer.parseInt(value));
                    break;
                case N_INT_OVR:
                    Assert.assertEquals(0, Integer.parseInt(value));
                    break;
                case N_EXT_OVR:
                    Assert.assertEquals(0, Integer.parseInt(value));
                    break;
                case N_EXT_OVR_MASK:
                    Assert.assertEquals(4, Integer.parseInt(value));
                    break;
                case EXT_MASK_FILE:
                    Assert.assertTrue(value.contains("external2.tif.msk"));
                    break;
                case EXT_OVR_FILE:
                    Assert.assertTrue(value.isEmpty());
                    break;
                case EXT_OVR_MASK_FILE:
                    Assert.assertTrue(value.contains("external2.tif.msk.ovr"));
                    break;
                default:
                    // Wrong element
                    Assert.assertTrue(false);
            }
        }
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        // and the input stream are closed
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) NodeList(org.w3c.dom.NodeList) Rectangle(java.awt.Rectangle) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) FileImageInputStreamExtImpl(it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl) File(java.io.File) Test(org.junit.Test)

Example 3 with FileImageInputStreamExt

use of it.geosolutions.imageio.stream.input.FileImageInputStreamExt in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readExternalMasks.

@Test
public void readExternalMasks() throws IOException {
    // Reading file with external masks
    final File file = TestData.file(this, "external.tif");
    // Setting read parameters
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 2, 2));
    // Creating the reader
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    // Using FileImageInputStreamExt for being able to locate the file path
    FileImageInputStreamExt inputStream = new FileImageInputStreamExtImpl(file);
    try {
        // reading phase
        reader.setInput(inputStream);
        // IMAGE 0
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        // Getting Stream Metadata
        IIOMetadata metadata = reader.getStreamMetadata();
        Node tree = metadata.getAsTree("com_sun_media_imageio_plugins_tiff_stream_1.0");
        // Ensuring not null
        Assert.assertNotNull(tree);
        // Checking Childs
        NodeList list = tree.getChildNodes();
        int len = list.getLength();
        // Loop the nodes
        for (int i = 0; i < len; i++) {
            // Node i-th
            Node node = list.item(i);
            // Ensuring not null
            Assert.assertNotNull(node);
            // Getting the name
            String nodeName = node.getNodeName();
            // Checking attributes
            Assert.assertTrue(node.hasAttributes());
            // Getting Attribute Value
            String value = node.getAttributes().item(0).getNodeValue();
            // Getting related enum
            MetadataNode mnode = MetadataNode.getFromName(nodeName);
            // Checking Attribute value
            switch(mnode) {
                case B_ORDER:
                    Assert.assertTrue(value.equalsIgnoreCase(ByteOrder.LITTLE_ENDIAN.toString()));
                    break;
                case N_INT_MASK:
                    Assert.assertEquals(0, Integer.parseInt(value));
                    break;
                case N_EXT_MASK:
                    Assert.assertEquals(5, Integer.parseInt(value));
                    break;
                case N_INT_OVR:
                    Assert.assertEquals(4, Integer.parseInt(value));
                    break;
                case N_EXT_OVR:
                    Assert.assertEquals(0, Integer.parseInt(value));
                    break;
                case N_EXT_OVR_MASK:
                    Assert.assertEquals(0, Integer.parseInt(value));
                    break;
                case EXT_MASK_FILE:
                    Assert.assertTrue(value.contains("external.tif.msk"));
                    break;
                case EXT_OVR_FILE:
                    Assert.assertTrue(value.isEmpty());
                    break;
                case EXT_OVR_MASK_FILE:
                    Assert.assertTrue(value.isEmpty());
                    break;
                default:
                    // Wrong element
                    Assert.assertTrue(false);
            }
        }
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
        Assert.assertTrue(false);
    } finally {
        // and the input stream are closed
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) NodeList(org.w3c.dom.NodeList) Rectangle(java.awt.Rectangle) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) FileImageInputStreamExtImpl(it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl) File(java.io.File) Test(org.junit.Test)

Example 4 with FileImageInputStreamExt

use of it.geosolutions.imageio.stream.input.FileImageInputStreamExt in project imageio-ext by geosolutions-it.

the class GDALImageReaderSpi method canDecodeInput.

/**
 * Checks if the provided input can be decoded by the specific SPI. When
 * building a new plugin, remember to implement the
 * <code>getSupportedFormat</coded> abstract method.
 *
 * @return
 * 		<code>true</code> if the input can be successfully decoded.
 */
public boolean canDecodeInput(Object input) throws IOException {
    if (input == null)
        return false;
    if (LOGGER.isLoggable(Level.FINE))
        LOGGER.fine("Can Decode Input called with object " + input != null ? input.toString() : "null");
    File sourceFile = null;
    // convert input from String to File then try URL
    if (input instanceof String) {
        final File file = new File((String) input);
        if (!file.exists() || !file.canRead()) {
            // /check for URL
            input = new URL((String) input);
        } else
            input = file;
    }
    // if input source is an URL, open an InputStream
    if (input instanceof URL) {
        input = ImageIOUtilities.urlToFile((URL) input);
    }
    // convert input from File to FileInputStream
    if (input instanceof File)
        sourceFile = (File) input;
    // at this point it must be an instance of FileImageInputStreamExt to be able to proceed
    if (input instanceof FileImageInputStreamExt)
        sourceFile = ((FileImageInputStreamExt) input).getFile();
    if (sourceFile == null || !sourceFile.exists() || !sourceFile.canRead()) {
        return false;
    }
    boolean isInputDecodable = false;
    // Checking if this specific SPI can decode the provided input
    Dataset ds = null;
    try {
        ds = GDALUtilities.acquireDataSet(sourceFile.getAbsolutePath(), gdalconst.GA_ReadOnly);
        isInputDecodable = isDecodable(ds);
    } catch (Throwable e) {
        if (LOGGER.isLoggable(Level.FINE))
            LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
    } finally {
        if (ds != null)
            try {
                // Closing the dataset
                GDALUtilities.closeDataSet(ds);
            } catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINEST))
                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
            }
    }
    return isInputDecodable;
}
Also used : Dataset(org.gdal.gdal.Dataset) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) File(java.io.File) URL(java.net.URL)

Example 5 with FileImageInputStreamExt

use of it.geosolutions.imageio.stream.input.FileImageInputStreamExt in project imageio-ext by geosolutions-it.

the class NITFImageWriter method write.

@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    // Headers and segments initialization
    NITFImageWriteParam nitfParam = null;
    HeaderWrapper header = null;
    Map<String, Map<String, String>> extensionsMap = null;
    ShapeFileWrapper shape = null;
    List<TextWrapper> texts = null;
    WriteCompression compression = null;
    List<ImageWrapper> inputImages = null;
    if (param != null && param instanceof NITFImageWriteParam) {
        nitfParam = (NITFImageWriteParam) param;
        NITFProperties nitfMetadata = nitfParam.getNitfProperties();
        if (nitfMetadata != null) {
            header = nitfMetadata.getHeader();
            shape = nitfMetadata.getShape();
            texts = nitfMetadata.getTextsWrapper();
            inputImages = nitfMetadata.getImagesWrapper();
        }
        compression = nitfParam.getWriteCompression();
    }
    ImageWrapper imageW = inputImages.get(0);
    RenderedImage ri = imageW.getImage();
    final boolean isJP2 = (compression != null && compression != WriteCompression.UNCOMPRESSED);
    FileImageInputStreamExt jp2Stream = null;
    File tempFile = null;
    try {
        Record record = new Record(Version.NITF_21);
        if (isJP2) {
            // Proceeding with jp2 compression
            if (JP2_TEMP_FOLDER != null) {
                tempFile = File.createTempFile("jp2compressed", ".jpc", new File(JP2_TEMP_FOLDER));
            }
            String parentPath = outputFile.getParent();
            String name = FilenameUtils.getBaseName(outputFile.getCanonicalPath());
            tempFile = new File(parentPath + File.separatorChar + name + ".j2c");
            prepareJP2Image(ri, tempFile, compression);
            jp2Stream = new FileImageInputStreamExtImpl(tempFile);
        }
        // populating the file header
        initFileHeader(record, header);
        // adding an image segment to the record
        addImageSegment(record, inputImages, jp2Stream, compression);
        if (texts != null && !texts.isEmpty()) {
            // adding a text segment if present
            addTextSegment(record, texts);
        }
        if (!writeNITF(record, inputImages, shape, jp2Stream, texts)) {
            throw new IOException("Unable to successfully write");
        }
    } catch (Throwable t) {
        IOException ioe = new IOException();
        ioe.initCause(t);
        throw ioe;
    } finally {
        // Releasing resources
        if (jp2Stream != null) {
            try {
                jp2Stream.close();
            } catch (Throwable thr) {
            // Eat exception
            }
        }
        if (tempFile != null) {
            try {
                tempFile.delete();
            } catch (Throwable thr) {
            }
        }
    }
    // record.destruct();
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Successfully wrote NITF: " + outputFile);
    }
}
Also used : NITFProperties(it.geosolutions.imageio.plugins.nitronitf.wrapper.NITFProperties) ShapeFileWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.ShapeFileWrapper) IOException(java.io.IOException) TextWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.TextWrapper) ImageWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.ImageWrapper) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) HeaderWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.HeaderWrapper) Record(nitf.Record) FileImageInputStreamExtImpl(it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl) RenderedImage(java.awt.image.RenderedImage) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) File(java.io.File) WriteCompression(it.geosolutions.imageio.plugins.nitronitf.NITFUtilities.WriteCompression)

Aggregations

FileImageInputStreamExt (it.geosolutions.imageio.stream.input.FileImageInputStreamExt)8 File (java.io.File)8 IOException (java.io.IOException)5 FileImageInputStreamExtImpl (it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl)4 URL (java.net.URL)4 Test (org.junit.Test)3 TIFFImageReader (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader)2 TIFFImageReaderSpi (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi)2 MetadataNode (it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode)2 Rectangle (java.awt.Rectangle)2 BufferedImage (java.awt.image.BufferedImage)2 FileInputStream (java.io.FileInputStream)2 ImageReadParam (javax.imageio.ImageReadParam)2 IIOMetadata (javax.imageio.metadata.IIOMetadata)2 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 Jp2_family_src (kdu_jni.Jp2_family_src)2 Jpx_source (kdu_jni.Jpx_source)2 KduException (kdu_jni.KduException)2 Kdu_simple_file_source (kdu_jni.Kdu_simple_file_source)2