Search in sources :

Example 1 with TIFFImageMetadata

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

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

the class TIFFDirectory method createFromMetadata.

/**
 * Creates a <code>TIFFDirectory</code> instance from the contents of
 * an image metadata object. The supplied object must support an image
 * metadata format supported by the TIFF {@link javax.imageio.ImageWriter}
 * plug-in. This will usually be either the TIFF native image metadata
 * format <tt>com_sun_media_imageio_plugins_tiff_1.0</tt> or the Java
 * Image I/O standard metadata format <tt>javax_imageio_1.0</tt>.
 *
 * @param tiffImageMetadata A metadata object which supports a compatible
 * image metadata format.
 *
 * @return A <code>TIFFDirectory</code> populated from the contents of
 * the supplied metadata object.
 *
 * @throws IllegalArgumentException if <code>tiffImageMetadata</code>
 * is <code>null</code>.
 * @throws IllegalArgumentException if <code>tiffImageMetadata</code>
 * does not support a compatible image metadata format.
 * @throws IIOInvalidTreeException if the supplied metadata object
 * cannot be parsed.
 */
public static TIFFDirectory createFromMetadata(IIOMetadata tiffImageMetadata) throws IIOInvalidTreeException {
    if (tiffImageMetadata == null) {
        throw new IllegalArgumentException("tiffImageMetadata == null");
    }
    TIFFImageMetadata tim;
    if (tiffImageMetadata instanceof TIFFImageMetadata) {
        tim = (TIFFImageMetadata) tiffImageMetadata;
    } else {
        // Create a native metadata object.
        ArrayList l = new ArrayList(1);
        l.add(BaselineTIFFTagSet.getInstance());
        tim = new TIFFImageMetadata(l);
        // Determine the format name to use.
        String formatName = null;
        if (TIFFImageMetadata.nativeMetadataFormatName.equals(tiffImageMetadata.getNativeMetadataFormatName())) {
            formatName = TIFFImageMetadata.nativeMetadataFormatName;
        } else {
            String[] extraNames = tiffImageMetadata.getExtraMetadataFormatNames();
            if (extraNames != null) {
                for (int i = 0; i < extraNames.length; i++) {
                    if (TIFFImageMetadata.nativeMetadataFormatName.equals(extraNames[i])) {
                        formatName = extraNames[i];
                        break;
                    }
                }
            }
            if (formatName == null) {
                if (tiffImageMetadata.isStandardMetadataFormatSupported()) {
                    formatName = IIOMetadataFormatImpl.standardMetadataFormatName;
                } else {
                    throw new IllegalArgumentException("Parameter does not support required metadata format!");
                }
            }
        }
        // Set the native metadata object from the tree.
        tim.setFromTree(formatName, tiffImageMetadata.getAsTree(formatName));
    }
    return tim.getRootIFD();
}
Also used : TIFFImageMetadata(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageMetadata) ArrayList(java.util.ArrayList)

Aggregations

TIFFImageMetadata (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageMetadata)2 TIFFImageReadParam (it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam)1 TIFFIFD (it.geosolutions.imageioimpl.plugins.tiff.TIFFIFD)1 TIFFImageReader (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader)1 TIFFImageReaderSpi (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FileImageInputStream (javax.imageio.stream.FileImageInputStream)1 Test (org.junit.Test)1