Search in sources :

Example 6 with TIFFImageReaderSpi

use of it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi 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 7 with TIFFImageReaderSpi

use of it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi in project imageio-ext by geosolutions-it.

the class TIFFMetadataTest method testImageRead.

/**
 * This test class is used for testing the capability of the TIFFImageReader
 * class to read EXIF metadata. TIFFImageReader class presented a bug when
 * the EXIF IFD pointer tag type was not LONG (type = 4) but was the new
 * IFD_POINTER type (type = 13) defined in the Technical Note 1 of the TIFF
 * Specification Supplement documentation. This variation provoked a
 * ClassCastException when the reader tried to cast the EXIF IFD pointer
 * data to the TIFFIFD class. This bug has been fixed by adding the
 * possibility to contain the IFD_POINTER type to the EXIF IFD pointer tag.
 * The testImageRead() method reads an image with the new TIFF tag type and
 * checks if the EXIF tags has been read by the TIFFImageReader.
 *
 * @throws IOException
 */
@Test
public void testImageRead() throws IOException {
    // Selection of the input file from the TestData directory
    File inputFile = TestData.file(this, "test_IFD.tif");
    // Instantiation of the read-params
    final TIFFImageReadParam param = new TIFFImageReadParam();
    // Instantiation of the file-reader
    TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    // Creation of the file input stream associated to the selected file
    FileImageInputStream stream0 = new FileImageInputStream(inputFile);
    try {
        // Setting the inputstream to the reader
        reader.setInput(stream0);
        // Reading of the image
        RenderedImage img = reader.read(0, param);
        // Reading of the associated metadata
        TIFFImageMetadata metadata = (TIFFImageMetadata) reader.getImageMetadata(0);
        // Check if the Exif pointer metadata is present
        int tagPointer = 34665;
        boolean fieldPointer = metadata.getRootIFD().containsTIFFField(tagPointer);
        assertTrue(fieldPointer);
        // Selection of the subIFD associated to the exif pointer
        TIFFIFD subIFD = (TIFFIFD) metadata.getTIFFField(tagPointer).getData();
        // Selection of the tags associated to the EXIF pointer
        int tagNumberExifVersion = 36864;
        int tagNumberDateTime = 36868;
        int tagNumberCompConfig = 37121;
        int tagNumberFlashPix = 40960;
        int tagNumberColor = 40961;
        int tagNumberXpixelRes = 40962;
        int tagNumberYpixelRes = 40963;
        // Test Assertions
        assertTrue(subIFD.containsTIFFField(tagNumberExifVersion));
        assertTrue(subIFD.containsTIFFField(tagNumberDateTime));
        assertTrue(subIFD.containsTIFFField(tagNumberCompConfig));
        assertTrue(subIFD.containsTIFFField(tagNumberFlashPix));
        assertTrue(subIFD.containsTIFFField(tagNumberColor));
        assertTrue(subIFD.containsTIFFField(tagNumberXpixelRes));
        assertTrue(subIFD.containsTIFFField(tagNumberYpixelRes));
    } 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 (stream0 != null) {
            stream0.flush();
            stream0.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) TIFFImageMetadata(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageMetadata) TIFFIFD(it.geosolutions.imageioimpl.plugins.tiff.TIFFIFD) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) IOException(java.io.IOException) TIFFImageReadParam(it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam) Test(org.junit.Test)

Example 8 with TIFFImageReaderSpi

use of it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readFromFileDirect.

@Test
public void readFromFileDirect() throws IOException {
    final File file = TestData.file(this, "test.tif");
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 2, 2));
    // double sum=0;
    // final long num = 10000l;
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        reader.setInput(inputStream);
        // System.out.println(new IIOMetadataDumper(
        // reader.getImageMetadata(0),TIFFImageMetadata.nativeMetadataFormatName).getMetadata());
        // for(long i=0;i<num;i++){
        // final double time= System.nanoTime();
        // IMAGE 0
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(1, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(2, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(1, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(3, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
    // sum+=System.nanoTime()-time;
    // Assert.assertEquals(120, image.getWidth());
    // Assert.assertEquals(107, image.getHeight());
    // System.out.println("test "+i);
    // 
    // }
    // System.out.println(sum/num);
    } 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 : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) Rectangle(java.awt.Rectangle) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with TIFFImageReaderSpi

use of it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readWithEmptyTiles.

@Test
public void readWithEmptyTiles() throws IOException {
    // This input image is a 1440x720 image. However, the right half of the image
    // is made of empty tiles filled with nodata
    final File file = TestData.file(this, "emptyTiles.tif");
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        reader.setInput(inputStream);
        ImageReadParam param = new ImageReadParam();
        // Setting up a region to fall in the half of the image containing empty tiles
        param.setSourceRegion(new Rectangle(360, 0, 720, 720));
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(720, image.getWidth());
        Assert.assertEquals(720, image.getHeight());
        IIOMetadata metadata = reader.getImageMetadata(0);
        Node rootNode = metadata.getAsTree(metadata.getNativeMetadataFormatName());
        double noDataValue = getNoDataValue(rootNode);
        // get it from the core common metadata too
        CoreCommonImageMetadata ccm = (CoreCommonImageMetadata) metadata;
        double[] noDataArray = ccm.getNoData();
        assertNotNull(noDataArray);
        assertEquals(noDataArray[0], noDataValue, 0d);
        assertEquals(noDataArray[1], noDataValue, 0d);
        // Check that the value is noData (the empty Tiles are filled with NoData)
        double val = image.getData().getSampleDouble(719, 0, 0);
        assertEquals(Double.toString(noDataValue), Double.toString(val));
        image.flush();
        image = null;
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : CoreCommonImageMetadata(it.geosolutions.imageio.core.CoreCommonImageMetadata) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) 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) FileImageInputStream(javax.imageio.stream.FileImageInputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) File(java.io.File) Test(org.junit.Test)

Example 10 with TIFFImageReaderSpi

use of it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readDeflateWithHorizontalDifferencingPredictorOn16Bits.

@Test
public void readDeflateWithHorizontalDifferencingPredictorOn16Bits() throws IOException {
    // This image has been created from test.tif using the command:
    // gdal_translate -OT UInt16 -co COMPRESS=DEFLATE -co PREDICTOR=2 test.tif deflatetest.tif
    final File file = TestData.file(this, "deflatetest.tif");
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        reader.setInput(inputStream);
        BufferedImage image = reader.read(0);
        image.flush();
        image = null;
    } finally {
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Aggregations

TIFFImageReaderSpi (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi)11 File (java.io.File)11 Test (org.junit.Test)11 TIFFImageReader (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader)9 FileImageInputStream (javax.imageio.stream.FileImageInputStream)9 Rectangle (java.awt.Rectangle)8 BufferedImage (java.awt.image.BufferedImage)8 ImageReadParam (javax.imageio.ImageReadParam)8 IOException (java.io.IOException)6 MetadataNode (it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode)4 IIOMetadata (javax.imageio.metadata.IIOMetadata)4 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)4 Node (org.w3c.dom.Node)4 RenderedImage (java.awt.image.RenderedImage)3 NodeList (org.w3c.dom.NodeList)3 FileImageInputStreamExt (it.geosolutions.imageio.stream.input.FileImageInputStreamExt)2 FileImageInputStreamExtImpl (it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl)2 CoreCommonImageMetadata (it.geosolutions.imageio.core.CoreCommonImageMetadata)1 TIFFImageReadParam (it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam)1 TIFFImageWriteParam (it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam)1