Path: blob/master/test/jdk/javax/imageio/plugins/png/ITXtTest.java
41155 views
/*1* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6541476 705997026* @summary Test verifies that ImageIO PNG plug-in correctly handles the27* iTxt chunk (International textual data).28*29* @run main ITXtTest30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.File;3637import javax.imageio.ImageIO;38import javax.imageio.ImageReader;39import javax.imageio.IIOImage;40import javax.imageio.ImageTypeSpecifier;41import javax.imageio.ImageWriter;42import javax.imageio.metadata.IIOMetadata;43import javax.imageio.metadata.IIOMetadataNode;44import javax.imageio.stream.ImageOutputStream;45import javax.imageio.stream.ImageInputStream;4647import org.w3c.dom.Node;4849public class ITXtTest {50static public void main(String args[]) {51ITXtTest t_en = new ITXtTest();52t_en.description = "xml - en";53t_en.keyword = "XML:com.adobe.xmp";54t_en.isCompressed = false;55t_en.compression = 0;56t_en.language = "en";57t_en.trasKeyword = "XML:com.adobe.xmp";58t_en.text = "<xml>Something</xml>";5960doTest(t_en);6162// check compression case63t_en.isCompressed = true;64t_en.description = "xml - en - compressed";6566doTest(t_en);6768ITXtTest t_ru = new ITXtTest();69t_ru.description = "xml - ru";70t_ru.keyword = "XML:com.adobe.xmp";71t_ru.isCompressed = false;72t_ru.compression = 0;73t_ru.language = "ru";74t_ru.trasKeyword = "\u0410\u0410\u0410\u0410\u0410 XML";75t_ru.text = "<xml>\u042A\u042F\u042F\u042F\u042F\u042F\u042F</xml>";7677doTest(t_ru);7879t_ru.isCompressed = true;80t_ru.description = "xml - ru - compressed";8182doTest(t_ru);83}848586String description;8788String keyword;89boolean isCompressed;90int compression;91String language;92String trasKeyword;93String text;949596public IIOMetadataNode getNode() {97IIOMetadataNode iTXt = new IIOMetadataNode("iTXt");98IIOMetadataNode iTXtEntry = new IIOMetadataNode("iTXtEntry");99iTXtEntry.setAttribute("keyword", keyword);100iTXtEntry.setAttribute("compressionFlag",101isCompressed ? "true" : "false");102iTXtEntry.setAttribute("compressionMethod",103Integer.toString(compression));104iTXtEntry.setAttribute("languageTag", language);105iTXtEntry.setAttribute("translatedKeyword",106trasKeyword);107iTXtEntry.setAttribute("text", text);108iTXt.appendChild(iTXtEntry);109return iTXt;110}111112public static ITXtTest getFromNode(IIOMetadataNode n) {113ITXtTest t = new ITXtTest();114115if (!"iTXt".equals(n.getNodeName())) {116throw new RuntimeException("Invalid node");117}118IIOMetadataNode e = (IIOMetadataNode)n.getFirstChild();119if (!"iTXtEntry".equals(e.getNodeName())) {120throw new RuntimeException("Invalid entry node");121}122t.keyword = e.getAttribute("keyword");123t.isCompressed =124Boolean.valueOf(e.getAttribute("compressionFlag")).booleanValue();125t.compression =126Integer.valueOf(e.getAttribute("compressionMethod")).intValue();127t.language = e.getAttribute("languageTag");128t.trasKeyword = e.getAttribute("translatedKeyword");129t.text = e.getAttribute("text");130131return t;132}133134@Override135public boolean equals(Object o) {136if (! (o instanceof ITXtTest)) {137return false;138}139ITXtTest t = (ITXtTest)o;140if (!keyword.equals(t.keyword)) { return false; }141if (isCompressed != t.isCompressed) { return false; }142if (compression != t.compression) { return false; }143if (!language.equals(t.language)) { return false; }144if (!trasKeyword.equals(t.trasKeyword)) { return false; }145if (!text.equals(t.text)) { return false; }146147return true;148}149150151152private static void doTest(ITXtTest src) {153154System.out.println("Test: " + src.description);155156File file = new File("test.png");157158try {159writeTo(file, src);160ITXtTest dst = readFrom(file);161if (dst == null || !dst.equals(src)) {162throw new RuntimeException("Test failed.");163}164} finally {165file.delete();166}167System.out.println("Test passed.");168}169170private static void writeTo(File f, ITXtTest t) {171BufferedImage src = createBufferedImage();172try (ImageOutputStream imageOutputStream =173ImageIO.createImageOutputStream(f)) {174175ImageTypeSpecifier imageTypeSpecifier =176new ImageTypeSpecifier(src);177ImageWriter imageWriter =178ImageIO.getImageWritersByFormatName("PNG").next();179180imageWriter.setOutput(imageOutputStream);181182IIOMetadata m =183imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);184185String format = m.getNativeMetadataFormatName();186Node root = m.getAsTree(format);187188IIOMetadataNode iTXt = t.getNode();189root.appendChild(iTXt);190m.setFromTree(format, root);191192imageWriter.write(new IIOImage(src, null, m));193System.out.println("Writing done.");194} catch (Throwable e) {195throw new RuntimeException("Writing test failed.", e);196}197}198199private static ITXtTest readFrom(File f) {200try (ImageInputStream imageInputStream =201ImageIO.createImageInputStream(f)) {202203ImageReader r = ImageIO.getImageReaders(imageInputStream).next();204r.setInput(imageInputStream);205206IIOImage dst = r.readAll(0, null);207208// look for iTXt node209IIOMetadata m = dst.getMetadata();210Node root = m.getAsTree(m.getNativeMetadataFormatName());211Node n = root.getFirstChild();212while (n != null && !"iTXt".equals(n.getNodeName())) {213n = n.getNextSibling();214}215if (n == null) {216throw new RuntimeException("No iTXt node!");217}218ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);219return t;220} catch (Throwable e) {221throw new RuntimeException("Reading test failed.", e);222}223}224225private static BufferedImage createBufferedImage() {226BufferedImage image = new BufferedImage(128, 128,227BufferedImage.TYPE_4BYTE_ABGR_PRE);228Graphics2D graph = image.createGraphics();229graph.setPaintMode();230graph.setColor(Color.orange);231graph.fillRect(32, 32, 64, 64);232graph.dispose();233return image;234}235}236237238239