Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/javax/imageio/plugins/png/ITXtTest.java
83406 views
/*1* Copyright (c) 2008, 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 654147626* @summary Test verifies that ImageIO PNG plugin correcly handles the27* iTxt chunk (International textual data).28*29* @run main ITXtTest30*/313233import java.awt.Color;34import java.awt.Graphics2D;35import java.awt.image.BufferedImage;36import java.io.File;3738import javax.imageio.ImageIO;39import javax.imageio.ImageReader;40import javax.imageio.IIOImage;41import javax.imageio.ImageTypeSpecifier;42import javax.imageio.ImageWriter;43import javax.imageio.metadata.IIOMetadata;44import javax.imageio.metadata.IIOMetadataNode;45import javax.imageio.stream.ImageOutputStream;46import javax.imageio.stream.ImageInputStream;4748import org.w3c.dom.Node;4950public class ITXtTest {51static public void main(String args[]) {52ITXtTest t_en = new ITXtTest();53t_en.description = "xml - en";54t_en.keyword = "XML:com.adobe.xmp";55t_en.isCompressed = false;56t_en.compression = 0;57t_en.language = "en";58t_en.trasKeyword = "XML:com.adobe.xmp";59t_en.text = "<xml>Something</xml>";6061doTest(t_en);6263// check compression case64t_en.isCompressed = true;65t_en.description = "xml - en - compressed";6667doTest(t_en);6869ITXtTest t_ru = new ITXtTest();70t_ru.description = "xml - ru";71t_ru.keyword = "XML:com.adobe.xmp";72t_ru.isCompressed = false;73t_ru.compression = 0;74t_ru.language = "ru";75t_ru.trasKeyword = "\u0410\u0410\u0410\u0410\u0410 XML";76t_ru.text = "<xml>\u042A\u042F\u042F\u042F\u042F\u042F\u042F</xml>";7778doTest(t_ru);7980t_ru.isCompressed = true;81t_ru.description = "xml - ru - compressed";8283doTest(t_ru);84}858687String description;8889String keyword;90boolean isCompressed;91int compression;92String language;93String trasKeyword;94String text;959697public IIOMetadataNode getNode() {98IIOMetadataNode iTXt = new IIOMetadataNode("iTXt");99IIOMetadataNode iTXtEntry = new IIOMetadataNode("iTXtEntry");100iTXtEntry.setAttribute("keyword", keyword);101iTXtEntry.setAttribute("compressionFlag",102isCompressed ? "true" : "false");103iTXtEntry.setAttribute("compressionMethod",104Integer.toString(compression));105iTXtEntry.setAttribute("languageTag", language);106iTXtEntry.setAttribute("translatedKeyword",107trasKeyword);108iTXtEntry.setAttribute("text", text);109iTXt.appendChild(iTXtEntry);110return iTXt;111}112113public static ITXtTest getFromNode(IIOMetadataNode n) {114ITXtTest t = new ITXtTest();115116if (!"iTXt".equals(n.getNodeName())) {117throw new RuntimeException("Invalid node");118}119IIOMetadataNode e = (IIOMetadataNode)n.getFirstChild();120if (!"iTXtEntry".equals(e.getNodeName())) {121throw new RuntimeException("Invalid entry node");122}123t.keyword = e.getAttribute("keyword");124t.isCompressed =125Boolean.valueOf(e.getAttribute("compressionFlag")).booleanValue();126t.compression =127Integer.valueOf(e.getAttribute("compressionMethod")).intValue();128t.language = e.getAttribute("languageTag");129t.trasKeyword = e.getAttribute("translatedKeyword");130t.text = e.getAttribute("text");131132return t;133}134135@Override136public boolean equals(Object o) {137if (! (o instanceof ITXtTest)) {138return false;139}140ITXtTest t = (ITXtTest)o;141if (!keyword.equals(t.keyword)) { return false; }142if (isCompressed != t.isCompressed) { return false; }143if (compression != t.compression) { return false; }144if (!language.equals(t.language)) { return false; }145if (!trasKeyword.equals(t.trasKeyword)) { return false; }146if (!text.equals(t.text)) { return false; }147148return true;149}150151152153private static void doTest(ITXtTest src) {154155System.out.println("Test: " + src.description);156157File file = new File("test.png");158159writeTo(file, src);160ITXtTest dst = readFrom(file);161162if (dst == null || !dst.equals(src)) {163throw new RuntimeException("Test failed.");164}165166System.out.println("Test passed.");167}168169private static void writeTo(File f, ITXtTest t) {170BufferedImage src = createBufferedImage();171try {172ImageOutputStream 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));193imageOutputStream.close();194System.out.println("Writing done.");195} catch (Throwable e) {196throw new RuntimeException("Writing test failed.", e);197}198}199200private static ITXtTest readFrom(File f) {201try {202ImageInputStream iis = ImageIO.createImageInputStream(f);203ImageReader r = ImageIO.getImageReaders(iis).next();204r.setInput(iis);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}236237238