Search in sources :

Example 31 with TIFFField

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

the class TIFFJPEGCompressor method setMetadata.

/**
 * Sets the value of the <code>metadata</code> field.
 *
 * <p>The implementation in this class also adds the TIFF fields
 * JPEGTables, YCbCrSubSampling, YCbCrPositioning, and
 * ReferenceBlackWhite superseding any prior settings of those
 * fields.</p>
 *
 * @param metadata the <code>IIOMetadata</code> object for the
 * image being written.
 *
 * @see #getMetadata()
 */
public void setMetadata(IIOMetadata metadata) {
    super.setMetadata(metadata);
    if (metadata instanceof TIFFImageMetadata) {
        TIFFImageMetadata tim = (TIFFImageMetadata) metadata;
        TIFFIFD rootIFD = tim.getRootIFD();
        BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
        TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_SAMPLES_PER_PIXEL);
        int numBands = f.getAsInt(0);
        if (numBands == 1) {
            // Remove YCbCr fields not relevant for grayscale.
            rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING);
            rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_POSITIONING);
            rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
        } else {
            // numBands == 3
            // Replace YCbCr fields.
            // YCbCrSubSampling
            TIFFField YCbCrSubSamplingField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING), TIFFTag.TIFF_SHORT, 2, new char[] { CHROMA_SUBSAMPLING, CHROMA_SUBSAMPLING });
            rootIFD.addTIFFField(YCbCrSubSamplingField);
            // YCbCrPositioning
            TIFFField YCbCrPositioningField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_POSITIONING), TIFFTag.TIFF_SHORT, 1, new char[] { BaselineTIFFTagSet.Y_CB_CR_POSITIONING_CENTERED });
            rootIFD.addTIFFField(YCbCrPositioningField);
            // ReferenceBlackWhite
            TIFFField referenceBlackWhiteField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE), TIFFTag.TIFF_RATIONAL, 6, new long[][] { // no headroon/footroom
            { 0, 1 }, { 255, 1 }, { 128, 1 }, { 255, 1 }, { 128, 1 }, { 255, 1 } });
            rootIFD.addTIFFField(referenceBlackWhiteField);
        }
        // JPEGTables field is written if and only if one is
        // already present in the metadata. If one is present
        // and has either zero length or does not represent a
        // valid tables-only stream, then a JPEGTables field
        // will be written initialized to the standard tables-
        // only stream written by the JPEG writer.
        // Retrieve the JPEGTables field.
        TIFFField JPEGTablesField = tim.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
        // Initialize JPEG writer to one supporting abbreviated streams.
        if (JPEGTablesField != null) {
            // Intialize the JPEG writer to one that supports stream
            // metadata, i.e., abbreviated streams, and may or may not
            // support image metadata.
            initJPEGWriter(true, false);
        }
        // streams was available.
        if (JPEGTablesField != null && JPEGWriter != null) {
            if (DEBUG)
                System.out.println("Has JPEGTables ...");
            // Set the abbreviated stream flag.
            this.writeAbbreviatedStream = true;
            // Branch based on field value count.
            if (JPEGTablesField.getCount() > 0) {
                if (DEBUG)
                    System.out.println("JPEGTables > 0");
                // Derive the stream metadata from the field.
                // Get the field values.
                byte[] tables = JPEGTablesField.getAsBytes();
                // Create an input stream for the tables.
                ByteArrayInputStream bais = new ByteArrayInputStream(tables);
                MemoryCacheImageInputStream iis = new MemoryCacheImageInputStream(bais);
                // Read the tables stream using the JPEG reader.
                ImageReader jpegReader = getJPEGTablesReader();
                jpegReader.setInput(iis);
                // Initialize the stream metadata object.
                try {
                    JPEGStreamMetadata = jpegReader.getStreamMetadata();
                } catch (Exception e) {
                    // Fall back to default tables.
                    JPEGStreamMetadata = null;
                } finally {
                    jpegReader.reset();
                }
                if (DEBUG)
                    System.out.println(JPEGStreamMetadata);
            }
            if (JPEGStreamMetadata == null) {
                if (DEBUG)
                    System.out.println("JPEGTables == 0");
                // Derive the field from default stream metadata.
                // Get default stream metadata.
                JPEGStreamMetadata = JPEGWriter.getDefaultStreamMetadata(JPEGParam);
                // Create an output stream for the tables.
                ByteArrayOutputStream tableByteStream = new ByteArrayOutputStream();
                MemoryCacheImageOutputStream tableStream = new MemoryCacheImageOutputStream(tableByteStream);
                // Write a tables-only stream.
                JPEGWriter.setOutput(tableStream);
                try {
                    JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
                    tableStream.flush();
                    JPEGWriter.endWriteSequence();
                    // Get the tables-only stream content.
                    byte[] tables = tableByteStream.toByteArray();
                    if (DEBUG)
                        System.out.println("tables.length = " + tables.length);
                    // Add the JPEGTables field.
                    JPEGTablesField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_JPEG_TABLES), TIFFTag.TIFF_UNDEFINED, tables.length, tables);
                    rootIFD.addTIFFField(JPEGTablesField);
                } catch (Exception e) {
                    // Do not write JPEGTables field.
                    rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
                    this.writeAbbreviatedStream = false;
                }
            }
        } else {
            // Do not write JPEGTables field.
            // Remove any field present.
            rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
            // Initialize the writer preferring codecLib.
            initJPEGWriter(false, false);
        }
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) BaselineTIFFTagSet(it.geosolutions.imageio.plugins.tiff.BaselineTIFFTagSet) ByteArrayInputStream(java.io.ByteArrayInputStream) TIFFField(it.geosolutions.imageio.plugins.tiff.TIFFField) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageReader(javax.imageio.ImageReader)

Example 32 with TIFFField

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

the class TIFFFaxCompressor method setMetadata.

/**
 * Sets the value of the <code>metadata</code> field.
 *
 * <p> The implementation in this class also sets local options
 * from the FILL_ORDER field if it exists.</p>
 *
 * @param metadata the <code>IIOMetadata</code> object for the
 * image being written.
 *
 * @see #getMetadata()
 */
public void setMetadata(IIOMetadata metadata) {
    super.setMetadata(metadata);
    if (metadata instanceof TIFFImageMetadata) {
        TIFFImageMetadata tim = (TIFFImageMetadata) metadata;
        TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
        inverseFill = (f != null && f.getAsInt(0) == 2);
    }
}
Also used : TIFFField(it.geosolutions.imageio.plugins.tiff.TIFFField)

Example 33 with TIFFField

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

the class TIFFFieldNode method initialize.

private synchronized void initialize() {
    if (isInitialized == Boolean.TRUE)
        return;
    if (isIFD) {
        TIFFDirectory dir = (TIFFDirectory) field.getData();
        TIFFField[] fields = dir.getTIFFFields();
        if (fields != null) {
            TIFFTagSet[] tagSets = dir.getTagSets();
            List tagSetList = Arrays.asList(tagSets);
            int numFields = fields.length;
            for (int i = 0; i < numFields; i++) {
                TIFFField f = fields[i];
                int tagNumber = f.getTagNumber();
                TIFFTag tag = TIFFIFD.getTag(tagNumber, tagSetList);
                Node node = f.getAsNativeNode();
                if (node != null) {
                    appendChild(node);
                }
            }
        }
    } else {
        IIOMetadataNode child;
        int count = field.getCount();
        if (field.getType() == TIFFTag.TIFF_UNDEFINED) {
            child = new IIOMetadataNode("TIFFUndefined");
            byte[] data = field.getAsBytes();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++) {
                sb.append(Integer.toString(data[i] & 0xff));
                if (i < count - 1) {
                    sb.append(",");
                }
            }
            child.setAttribute("value", sb.toString());
        } else {
            child = new IIOMetadataNode("TIFF" + field.getTypeName(field.getType()) + "s");
            TIFFTag tag = field.getTag();
            String tName = tag.getName();
            boolean proceed = true;
            if (EXCLUDE_ATTRIBUTES && tName != null && ATTRIBUTES_EXCLUSION_SET.contains(tName)) {
                proceed = false;
            }
            for (int i = 0; i < count && proceed; i++) {
                IIOMetadataNode cchild = new IIOMetadataNode("TIFF" + field.getTypeName(field.getType()));
                cchild.setAttribute("value", field.getValueAsString(i));
                if (tag.hasValueNames() && field.isIntegral()) {
                    int value = field.getAsInt(i);
                    String name = tag.getValueName(value);
                    if (name != null) {
                        cchild.setAttribute("description", name);
                    }
                }
                child.appendChild(cchild);
            }
        }
        appendChild(child);
    }
    isInitialized = Boolean.TRUE;
}
Also used : Node(org.w3c.dom.Node) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) TIFFDirectory(it.geosolutions.imageio.plugins.tiff.TIFFDirectory) TIFFField(it.geosolutions.imageio.plugins.tiff.TIFFField) List(java.util.List) TIFFTagSet(it.geosolutions.imageio.plugins.tiff.TIFFTagSet) TIFFTag(it.geosolutions.imageio.plugins.tiff.TIFFTag) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Example 34 with TIFFField

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

the class TIFFImageMetadata method mergeNativeTree.

private void mergeNativeTree(Node root) throws IIOInvalidTreeException {
    Node node = root;
    if (!node.getNodeName().equals(nativeMetadataFormatName)) {
        fatal(node, "Root must be " + nativeMetadataFormatName);
    }
    node = node.getFirstChild();
    if (node == null || !node.getNodeName().equals("TIFFIFD")) {
        fatal(root, "Root must have \"TIFFIFD\" child");
    }
    TIFFIFD ifd = parseIFD(node);
    List rootIFDTagSets = rootIFD.getTagSetList();
    Iterator tagSetIter = ifd.getTagSetList().iterator();
    while (tagSetIter.hasNext()) {
        Object o = tagSetIter.next();
        if (o instanceof TIFFTagSet && !rootIFDTagSets.contains(o)) {
            rootIFD.addTagSet((TIFFTagSet) o);
        }
    }
    Iterator ifdIter = ifd.iterator();
    while (ifdIter.hasNext()) {
        TIFFField field = (TIFFField) ifdIter.next();
        rootIFD.addTIFFField(field);
    }
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) TIFFField(it.geosolutions.imageio.plugins.tiff.TIFFField) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) BaselineTIFFTagSet(it.geosolutions.imageio.plugins.tiff.BaselineTIFFTagSet) TIFFTagSet(it.geosolutions.imageio.plugins.tiff.TIFFTagSet) EXIFParentTIFFTagSet(it.geosolutions.imageio.plugins.tiff.EXIFParentTIFFTagSet)

Example 35 with TIFFField

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

the class TIFFImageMetadata method getStandardTextNode.

public IIOMetadataNode getStandardTextNode() {
    IIOMetadataNode text_node = null;
    // scratch node
    IIOMetadataNode node = null;
    TIFFField f;
    int[] textFieldTagNumbers = new int[] { BaselineTIFFTagSet.TAG_DOCUMENT_NAME, BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION, BaselineTIFFTagSet.TAG_MAKE, BaselineTIFFTagSet.TAG_MODEL, BaselineTIFFTagSet.TAG_PAGE_NAME, BaselineTIFFTagSet.TAG_SOFTWARE, BaselineTIFFTagSet.TAG_ARTIST, BaselineTIFFTagSet.TAG_HOST_COMPUTER, BaselineTIFFTagSet.TAG_INK_NAMES, BaselineTIFFTagSet.TAG_COPYRIGHT };
    for (int i = 0; i < textFieldTagNumbers.length; i++) {
        f = getTIFFField(textFieldTagNumbers[i]);
        if (f != null) {
            String value = f.getAsString(0);
            if (text_node == null) {
                text_node = new IIOMetadataNode("Text");
            }
            node = new IIOMetadataNode("TextEntry");
            node.setAttribute("keyword", f.getTag().getName());
            node.setAttribute("value", value);
            text_node.appendChild(node);
        }
    }
    return text_node;
}
Also used : TIFFField(it.geosolutions.imageio.plugins.tiff.TIFFField) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Aggregations

TIFFField (it.geosolutions.imageio.plugins.tiff.TIFFField)35 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)11 BaselineTIFFTagSet (it.geosolutions.imageio.plugins.tiff.BaselineTIFFTagSet)8 TIFFTag (it.geosolutions.imageio.plugins.tiff.TIFFTag)7 Iterator (java.util.Iterator)7 List (java.util.List)7 TIFFTagSet (it.geosolutions.imageio.plugins.tiff.TIFFTagSet)6 ArrayList (java.util.ArrayList)6 Point (java.awt.Point)5 IIOException (javax.imageio.IIOException)5 EXIFParentTIFFTagSet (it.geosolutions.imageio.plugins.tiff.EXIFParentTIFFTagSet)4 IOException (java.io.IOException)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 TIFFImageWriteParam (it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam)2 Rectangle (java.awt.Rectangle)2 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)2 IndexColorModel (java.awt.image.IndexColorModel)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 EOFException (java.io.EOFException)2