Java Code Examples for javax.imageio.metadata.IIOMetadata#mergeTree()

The following examples show how to use javax.imageio.metadata.IIOMetadata#mergeTree() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MergeStdCommentTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String format = "javax_imageio_1.0";
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    IIOMetadata meta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, text, entry;
    root = doc.getDocumentElement();
    root.appendChild(text = doc.createElement("Text"));
    text.appendChild(entry = doc.createElement("TextEntry"));
    // keyword isn't #REQUIRED by the standard metadata format.
    // However, it is required by the PNG format, so we include it here.
    entry.setAttribute("keyword", "Comment");
    entry.setAttribute("value", "Some demo comment");
    meta.mergeTree(format, root);
}
 
Example 2
Source File: MergeStdCommentTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String format = "javax_imageio_1.0";
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    IIOMetadata meta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, text, entry;
    root = doc.getDocumentElement();
    root.appendChild(text = doc.createElement("Text"));
    text.appendChild(entry = doc.createElement("TextEntry"));
    // keyword isn't #REQUIRED by the standard metadata format.
    // However, it is required by the PNG format, so we include it here.
    entry.setAttribute("keyword", "Comment");
    entry.setAttribute("value", "Some demo comment");
    meta.mergeTree(format, root);
}
 
Example 3
Source File: ShortHistogramTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) {
    String format = src.getNativeMetadataFormatName();
    System.out.println("Native format: " + format);
    Node root = src.getAsTree(format);

    // add hIST node
    Node n = lookupChildNode(root, "hIST");
    if (n == null) {
        System.out.println("Appending new hIST node...");
        Node hIST = gethISTNode(bi);
        root.appendChild(hIST);
    }

    System.out.println("Upgraded metadata tree:");
    dump(root, "");

    System.out.println("Merging metadata...");
    try {
        src.mergeTree(format, root);
    } catch (IIOInvalidTreeException e) {
        throw new RuntimeException("Test FAILED!", e);
    }
    return src;
}
 
Example 4
Source File: ShortHistogramTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) {
    String format = src.getNativeMetadataFormatName();
    System.out.println("Native format: " + format);
    Node root = src.getAsTree(format);

    // add hIST node
    Node n = lookupChildNode(root, "hIST");
    if (n == null) {
        System.out.println("Appending new hIST node...");
        Node hIST = gethISTNode(bi);
        root.appendChild(hIST);
    }

    System.out.println("Upgraded metadata tree:");
    dump(root, "");

    System.out.println("Merging metadata...");
    try {
        src.mergeTree(format, root);
    } catch (IIOInvalidTreeException e) {
        throw new RuntimeException("Test FAILED!", e);
    }
    return src;
}
 
Example 5
Source File: MergeStdCommentTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String format = "javax_imageio_1.0";
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    IIOMetadata meta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, text, entry;
    root = doc.getDocumentElement();
    root.appendChild(text = doc.createElement("Text"));
    text.appendChild(entry = doc.createElement("TextEntry"));
    // keyword isn't #REQUIRED by the standard metadata format.
    // However, it is required by the PNG format, so we include it here.
    entry.setAttribute("keyword", "Comment");
    entry.setAttribute("value", "Some demo comment");
    meta.mergeTree(format, root);
}
 
Example 6
Source File: ShortHistogramTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) {
    String format = src.getNativeMetadataFormatName();
    System.out.println("Native format: " + format);
    Node root = src.getAsTree(format);

    // add hIST node
    Node n = lookupChildNode(root, "hIST");
    if (n == null) {
        System.out.println("Appending new hIST node...");
        Node hIST = gethISTNode(bi);
        root.appendChild(hIST);
    }

    System.out.println("Upgraded metadata tree:");
    dump(root, "");

    System.out.println("Merging metadata...");
    try {
        src.mergeTree(format, root);
    } catch (IIOInvalidTreeException e) {
        throw new RuntimeException("Test FAILED!", e);
    }
    return src;
}
 
Example 7
Source File: MergeTreeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ImageWriter iw =
        (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    ImageOutputStream ios =
        ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
    iw.setOutput(ios);

    IIOMetadata meta = iw.getDefaultImageMetadata(type, null);

    boolean isFailed = false;

    String[] fmts = meta.getMetadataFormatNames();
    for (int i=0; i<fmts.length; i++) {
        System.out.print("Format: " + fmts[i] + " ... ");
        Node root = meta.getAsTree(fmts[i]);
        try {
            meta.mergeTree(fmts[i], root);
        } catch (NullPointerException e) {
            throw new RuntimeException("Test failed for format " + fmts[i], e);
        }
        System.out.println("PASSED");
    }
}
 
Example 8
Source File: GIFImageWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 9
Source File: MergeTreeTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ImageWriter iw =
        (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    ImageOutputStream ios =
        ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
    iw.setOutput(ios);

    IIOMetadata meta = iw.getDefaultImageMetadata(type, null);

    boolean isFailed = false;

    String[] fmts = meta.getMetadataFormatNames();
    for (int i=0; i<fmts.length; i++) {
        System.out.print("Format: " + fmts[i] + " ... ");
        Node root = meta.getAsTree(fmts[i]);
        try {
            meta.mergeTree(fmts[i], root);
        } catch (NullPointerException e) {
            throw new RuntimeException("Test failed for format " + fmts[i], e);
        }
        System.out.println("PASSED");
    }
}
 
Example 10
Source File: GIFImageWriter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 11
Source File: AnnotateScreenCapture.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public File saveToFile(File captureFile) throws FileNotFoundException, IOException {
    writePNG(captureFile);

    ImageReader reader = getPNGImageReader();
    ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream(captureFile));
    reader.setInput(iis);
    BufferedImage pngImage = reader.read(0);
    IIOMetadata imageMetadata = reader.getImageMetadata(0);

    Node root = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName());
    IIOMetadataNode textNode = new IIOMetadataNode("tEXt");
    ArrayList<Annotation> annotations = imagePanel.getAnnotations();
    for (int i = 0; i < annotations.size(); i++) {
        textNode.appendChild(getAnnotationNode((Annotation) annotations.get(i), i));
    }
    root.appendChild(textNode);

    imageMetadata.mergeTree(imageMetadata.getNativeMetadataFormatName(), root);

    IIOImage imageWrite = new IIOImage(pngImage, new ArrayList<BufferedImage>(), imageMetadata);

    ImageWriter writer = getPNGImageWriter();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new FileOutputStream(captureFile));
    writer.setOutput(ios);
    writer.write(imageWrite);
    writer.dispose();

    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    ImagePanel finalImage = new ImagePanel(new FileInputStream(captureFile), false);
    frame.add(finalImage, BorderLayout.CENTER);
    frame.pack();
    File savedFile = new File(captureFile.getParentFile(), "ext-" + captureFile.getName());
    getScreenShot(frame.getContentPane(), savedFile);
    frame.dispose();

    return savedFile;
}
 
Example 12
Source File: GIFImageWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 13
Source File: GIFImageWriter.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 14
Source File: GIFImageWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 15
Source File: GIFImageWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 16
Source File: PNGCanvas.java    From SwissQRBill with MIT License 5 votes vote down vote up
private static void addTextMetadata(IIOMetadata metadata) throws IIOInvalidTreeException {
    IIOMetadataNode textEntry = new IIOMetadataNode("tEXtEntry");
    textEntry.setAttribute(METADATA_KEY_KEYWORD, "Title");
    textEntry.setAttribute(METADATA_KEY_VALUE, "Swiss QR Bill");
    IIOMetadataNode text = new IIOMetadataNode("tEXt");
    text.appendChild(textEntry);
    IIOMetadataNode commentMetadata = new IIOMetadataNode(metadata.getNativeMetadataFormatName());
    commentMetadata.appendChild(text);
    metadata.mergeTree(metadata.getNativeMetadataFormatName(), commentMetadata);
}
 
Example 17
Source File: GIFImageWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 18
Source File: BooleanAttributes.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
 
Example 19
Source File: BooleanAttributes.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
 
Example 20
Source File: BooleanAttributes.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}