770N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
770N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
770N/A *
770N/A * This code is free software; you can redistribute it and/or modify it
770N/A * under the terms of the GNU General Public License version 2 only, as
770N/A * published by the Free Software Foundation.
770N/A *
770N/A * This code is distributed in the hope that it will be useful, but WITHOUT
770N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
770N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
770N/A * version 2 for more details (a copy is included in the LICENSE file that
770N/A * accompanied this code).
770N/A *
770N/A * You should have received a copy of the GNU General Public License version
770N/A * 2 along with this work; if not, write to the Free Software Foundation,
770N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
770N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
770N/A */
770N/A
770N/A/**
770N/A * @test
770N/A * @bug 6541476
770N/A * @summary Test verifies that ImageIO PNG plugin correcly handles the
770N/A * iTxt chunk (International textual data).
770N/A *
770N/A * @run main ITXtTest
770N/A */
770N/A
770N/A
770N/Aimport java.awt.Color;
770N/Aimport java.awt.Graphics2D;
770N/Aimport java.awt.image.BufferedImage;
770N/Aimport java.io.File;
770N/A
770N/Aimport javax.imageio.ImageIO;
770N/Aimport javax.imageio.ImageReader;
770N/Aimport javax.imageio.IIOImage;
770N/Aimport javax.imageio.ImageTypeSpecifier;
770N/Aimport javax.imageio.ImageWriter;
770N/Aimport javax.imageio.metadata.IIOMetadata;
770N/Aimport javax.imageio.metadata.IIOMetadataNode;
770N/Aimport javax.imageio.stream.ImageOutputStream;
770N/Aimport javax.imageio.stream.ImageInputStream;
770N/A
770N/Aimport org.w3c.dom.Node;
770N/A
770N/Apublic class ITXtTest {
770N/A static public void main(String args[]) {
770N/A ITXtTest t_en = new ITXtTest();
770N/A t_en.description = "xml - en";
770N/A t_en.keyword = "XML:com.adobe.xmp";
770N/A t_en.isCompressed = false;
770N/A t_en.compression = 0;
770N/A t_en.language = "en";
770N/A t_en.trasKeyword = "XML:com.adobe.xmp";
770N/A t_en.text = "<xml>Something</xml>";
770N/A
770N/A doTest(t_en);
770N/A
770N/A // check compression case
770N/A t_en.isCompressed = true;
770N/A t_en.description = "xml - en - compressed";
770N/A
770N/A doTest(t_en);
770N/A
770N/A ITXtTest t_ru = new ITXtTest();
770N/A t_ru.description = "xml - ru";
770N/A t_ru.keyword = "XML:com.adobe.xmp";
770N/A t_ru.isCompressed = false;
770N/A t_ru.compression = 0;
770N/A t_ru.language = "ru";
770N/A t_ru.trasKeyword = "\u0410\u0410\u0410\u0410\u0410 XML";
770N/A t_ru.text = "<xml>\u042A\u042F\u042F\u042F\u042F\u042F\u042F</xml>";
770N/A
770N/A doTest(t_ru);
770N/A
770N/A t_ru.isCompressed = true;
770N/A t_ru.description = "xml - ru - compressed";
770N/A
770N/A doTest(t_ru);
770N/A }
770N/A
770N/A
770N/A String description;
770N/A
770N/A String keyword;
770N/A boolean isCompressed;
770N/A int compression;
770N/A String language;
770N/A String trasKeyword;
770N/A String text;
770N/A
770N/A
770N/A public IIOMetadataNode getNode() {
770N/A IIOMetadataNode iTXt = new IIOMetadataNode("iTXt");
770N/A IIOMetadataNode iTXtEntry = new IIOMetadataNode("iTXtEntry");
770N/A iTXtEntry.setAttribute("keyword", keyword);
770N/A iTXtEntry.setAttribute("compressionFlag",
770N/A isCompressed ? "true" : "false");
770N/A iTXtEntry.setAttribute("compressionMethod",
770N/A Integer.toString(compression));
770N/A iTXtEntry.setAttribute("languageTag", language);
770N/A iTXtEntry.setAttribute("translatedKeyword",
770N/A trasKeyword);
770N/A iTXtEntry.setAttribute("text", text);
770N/A iTXt.appendChild(iTXtEntry);
770N/A return iTXt;
770N/A }
770N/A
770N/A public static ITXtTest getFromNode(IIOMetadataNode n) {
770N/A ITXtTest t = new ITXtTest();
770N/A
770N/A if (!"iTXt".equals(n.getNodeName())) {
770N/A throw new RuntimeException("Invalid node");
770N/A }
770N/A IIOMetadataNode e = (IIOMetadataNode)n.getFirstChild();
770N/A if (!"iTXtEntry".equals(e.getNodeName())) {
770N/A throw new RuntimeException("Invalid entry node");
770N/A }
770N/A t.keyword = e.getAttribute("keyword");
770N/A t.isCompressed =
983N/A Boolean.valueOf(e.getAttribute("compressionFlag")).booleanValue();
770N/A t.compression =
770N/A Integer.valueOf(e.getAttribute("compressionMethod")).intValue();
770N/A t.language = e.getAttribute("languageTag");
770N/A t.trasKeyword = e.getAttribute("translatedKeyword");
770N/A t.text = e.getAttribute("text");
770N/A
770N/A return t;
770N/A }
770N/A
770N/A @Override
770N/A public boolean equals(Object o) {
770N/A if (! (o instanceof ITXtTest)) {
770N/A return false;
770N/A }
770N/A ITXtTest t = (ITXtTest)o;
770N/A if (!keyword.equals(t.keyword)) { return false; }
770N/A if (isCompressed != t.isCompressed) { return false; }
770N/A if (compression != t.compression) { return false; }
770N/A if (!language.equals(t.language)) { return false; }
770N/A if (!trasKeyword.equals(t.trasKeyword)) { return false; }
770N/A if (!text.equals(t.text)) { return false; }
770N/A
770N/A return true;
770N/A }
770N/A
770N/A
770N/A
770N/A private static void doTest(ITXtTest src) {
770N/A
770N/A System.out.println("Test: " + src.description);
770N/A
770N/A File file = new File("test.png");
770N/A
770N/A writeTo(file, src);
770N/A ITXtTest dst = readFrom(file);
770N/A
770N/A if (dst == null || !dst.equals(src)) {
770N/A throw new RuntimeException("Test failed.");
770N/A }
770N/A
770N/A System.out.println("Test passed.");
770N/A }
770N/A
770N/A private static void writeTo(File f, ITXtTest t) {
770N/A BufferedImage src = createBufferedImage();
770N/A try {
770N/A ImageOutputStream imageOutputStream =
770N/A ImageIO.createImageOutputStream(f);
770N/A
770N/A ImageTypeSpecifier imageTypeSpecifier =
770N/A new ImageTypeSpecifier(src);
770N/A ImageWriter imageWriter =
770N/A ImageIO.getImageWritersByFormatName("PNG").next();
770N/A
770N/A imageWriter.setOutput(imageOutputStream);
770N/A
770N/A IIOMetadata m =
770N/A imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
770N/A
770N/A String format = m.getNativeMetadataFormatName();
770N/A Node root = m.getAsTree(format);
770N/A
770N/A IIOMetadataNode iTXt = t.getNode();
770N/A root.appendChild(iTXt);
770N/A m.setFromTree(format, root);
770N/A
770N/A imageWriter.write(new IIOImage(src, null, m));
770N/A imageOutputStream.close();
770N/A System.out.println("Writing done.");
770N/A } catch (Throwable e) {
770N/A throw new RuntimeException("Writing test failed.", e);
770N/A }
770N/A }
770N/A
770N/A private static ITXtTest readFrom(File f) {
770N/A try {
770N/A ImageInputStream iis = ImageIO.createImageInputStream(f);
770N/A ImageReader r = ImageIO.getImageReaders(iis).next();
770N/A r.setInput(iis);
770N/A
770N/A IIOImage dst = r.readAll(0, null);
770N/A
770N/A // look for iTXt node
770N/A IIOMetadata m = dst.getMetadata();
770N/A Node root = m.getAsTree(m.getNativeMetadataFormatName());
770N/A Node n = root.getFirstChild();
770N/A while (n != null && !"iTXt".equals(n.getNodeName())) {
770N/A n = n.getNextSibling();
770N/A }
770N/A if (n == null) {
770N/A throw new RuntimeException("No iTXt node!");
770N/A }
770N/A ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
770N/A return t;
770N/A } catch (Throwable e) {
770N/A throw new RuntimeException("Reading test failed.", e);
770N/A }
770N/A }
770N/A
770N/A private static BufferedImage createBufferedImage() {
770N/A BufferedImage image = new BufferedImage(128, 128,
770N/A BufferedImage.TYPE_4BYTE_ABGR_PRE);
770N/A Graphics2D graph = image.createGraphics();
770N/A graph.setPaintMode();
770N/A graph.setColor(Color.orange);
770N/A graph.fillRect(32, 32, 64, 64);
770N/A graph.dispose();
770N/A return image;
770N/A }
770N/A}