Search in sources :

Example 21 with IIOInvalidTreeException

use of javax.imageio.metadata.IIOInvalidTreeException in project imageio-ext by geosolutions-it.

the class TIFFBaseJPEGCompressor method getImageMetadata.

/**
 * Retrieves image metadata with non-core nodes removed.
 */
private IIOMetadata getImageMetadata(boolean pruneTables) throws IIOException {
    if (DEBUG) {
        System.out.println("getImageMetadata(" + pruneTables + ")");
    }
    if (JPEGImageMetadata == null && IMAGE_METADATA_NAME.equals(JPEGWriter.getOriginatingProvider().getNativeImageMetadataFormatName())) {
        TIFFImageWriter tiffWriter = (TIFFImageWriter) this.writer;
        // Get default image metadata.
        JPEGImageMetadata = JPEGWriter.getDefaultImageMetadata(tiffWriter.imageType, JPEGParam);
        // Get the DOM tree.
        Node tree = JPEGImageMetadata.getAsTree(IMAGE_METADATA_NAME);
        // Remove unwanted marker segments.
        try {
            pruneNodes(tree, pruneTables);
        } catch (IllegalArgumentException e) {
            throw new IIOException("Error pruning unwanted nodes", e);
        }
        // Set the DOM back into the metadata.
        try {
            JPEGImageMetadata.setFromTree(IMAGE_METADATA_NAME, tree);
        } catch (IIOInvalidTreeException e) {
            // present of JPEGTables field.
            throw new IIOException("Cannot set pruned image metadata!", e);
        }
    }
    return JPEGImageMetadata;
}
Also used : IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) IIOException(javax.imageio.IIOException)

Example 22 with IIOInvalidTreeException

use of javax.imageio.metadata.IIOInvalidTreeException in project quick-media by liuyueyi.

the class ImageIOJPEGImageWriter method updateMetadata.

/**
 * {@inheritDoc}
 */
@Override
protected IIOMetadata updateMetadata(IIOMetadata meta, ImageWriterParams params) {
    // ImageIODebugUtil.dumpMetadata(meta);
    if (JPEG_NATIVE_FORMAT.equals(meta.getNativeMetadataFormatName())) {
        meta = addAdobeTransform(meta);
        IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(JPEG_NATIVE_FORMAT);
        IIOMetadataNode jv = getChildNode(root, "JPEGvariety");
        if (jv == null) {
            jv = new IIOMetadataNode("JPEGvariety");
            root.appendChild(jv);
        }
        IIOMetadataNode child;
        if (params.getResolution() != null) {
            child = getChildNode(jv, "app0JFIF");
            if (child == null) {
                child = new IIOMetadataNode("app0JFIF");
                jv.appendChild(child);
            }
            // JPEG gets special treatment because there seems to be a bug in
            // the JPEG codec in ImageIO converting the pixel size incorrectly
            // (or not at all) when using standard metadata format.
            child.setAttribute("majorVersion", null);
            child.setAttribute("minorVersion", null);
            // dots per inch
            child.setAttribute("resUnits", "1");
            child.setAttribute("Xdensity", params.getResolution().toString());
            child.setAttribute("Ydensity", params.getResolution().toString());
            child.setAttribute("thumbWidth", null);
            child.setAttribute("thumbHeight", null);
        }
        try {
            meta.setFromTree(JPEG_NATIVE_FORMAT, root);
        } catch (IIOInvalidTreeException e) {
            throw new RuntimeException("Cannot update image metadata: " + e.getMessage(), e);
        }
    // ImageIODebugUtil.dumpMetadata(meta);
    }
    return meta;
}
Also used : IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Example 23 with IIOInvalidTreeException

use of javax.imageio.metadata.IIOInvalidTreeException in project quick-media by liuyueyi.

the class ImageIOJPEGImageWriter method addAdobeTransform.

private static IIOMetadata addAdobeTransform(IIOMetadata meta) {
    // add the adobe transformation (transform 1 -> to YCbCr)
    IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(JPEG_NATIVE_FORMAT);
    IIOMetadataNode markerSequence = getChildNode(root, "markerSequence");
    if (markerSequence == null) {
        throw new RuntimeException("Invalid metadata!");
    }
    IIOMetadataNode adobeTransform = getChildNode(markerSequence, "app14Adobe");
    if (adobeTransform == null) {
        adobeTransform = new IIOMetadataNode("app14Adobe");
        // convert RGB to YCbCr
        adobeTransform.setAttribute("transform", "1");
        adobeTransform.setAttribute("version", "101");
        adobeTransform.setAttribute("flags0", "0");
        adobeTransform.setAttribute("flags1", "0");
        markerSequence.appendChild(adobeTransform);
    } else {
        adobeTransform.setAttribute("transform", "1");
    }
    try {
        meta.setFromTree(JPEG_NATIVE_FORMAT, root);
    } catch (IIOInvalidTreeException e) {
        throw new RuntimeException("Cannot update image metadata: " + e.getMessage(), e);
    }
    return meta;
}
Also used : IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Aggregations

IIOInvalidTreeException (javax.imageio.metadata.IIOInvalidTreeException)23 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)15 Node (org.w3c.dom.Node)12 NodeList (org.w3c.dom.NodeList)9 Point (java.awt.Point)5 ArrayList (java.util.ArrayList)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)4 SampleModel (java.awt.image.SampleModel)3 List (java.util.List)3 IIOException (javax.imageio.IIOException)3 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)3 IIOMetadata (javax.imageio.metadata.IIOMetadata)3 ColorModel (java.awt.image.ColorModel)2 ComponentSampleModel (java.awt.image.ComponentSampleModel)2 Iterator (java.util.Iterator)2 TIFFImageWriteParam (it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam)1 Rectangle (java.awt.Rectangle)1 ComponentColorModel (java.awt.image.ComponentColorModel)1 IndexColorModel (java.awt.image.IndexColorModel)1 IOException (java.io.IOException)1