< prev index next >

test/javax/imageio/plugins/png/ITXtTest.java

Print this page


   1 /*
   2  * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug     6541476
  27  * @summary Test verifies that ImageIO PNG plugin correcly handles the
  28  *          iTxt chunk (International textual data).
  29  *
  30  * @run     main ITXtTest
  31  */
  32 
  33 
  34 import java.awt.Color;
  35 import java.awt.Graphics2D;
  36 import java.awt.image.BufferedImage;
  37 import java.io.File;
  38 
  39 import javax.imageio.ImageIO;
  40 import javax.imageio.ImageReader;
  41 import javax.imageio.IIOImage;
  42 import javax.imageio.ImageTypeSpecifier;
  43 import javax.imageio.ImageWriter;
  44 import javax.imageio.metadata.IIOMetadata;
  45 import javax.imageio.metadata.IIOMetadataNode;
  46 import javax.imageio.stream.ImageOutputStream;
  47 import javax.imageio.stream.ImageInputStream;
  48 
  49 import org.w3c.dom.Node;
  50 
  51 public class ITXtTest {
  52     static public void main(String args[]) {
  53         ITXtTest t_en = new ITXtTest();


 140         }
 141         ITXtTest t = (ITXtTest)o;
 142         if (!keyword.equals(t.keyword)) { return false; }
 143         if (isCompressed != t.isCompressed) { return false; }
 144         if (compression != t.compression) { return false; }
 145         if (!language.equals(t.language)) { return false; }
 146         if (!trasKeyword.equals(t.trasKeyword)) { return false; }
 147         if (!text.equals(t.text)) { return false; }
 148 
 149         return true;
 150     }
 151 
 152 
 153 
 154     private static void doTest(ITXtTest src) {
 155 
 156         System.out.println("Test: " + src.description);
 157 
 158         File file = new File("test.png");
 159 

 160         writeTo(file, src);
 161         ITXtTest dst = readFrom(file);
 162 
 163         if (dst == null || !dst.equals(src)) {
 164             throw new RuntimeException("Test failed.");
 165         }
 166 


 167         System.out.println("Test passed.");
 168     }
 169 
 170     private static void writeTo(File f, ITXtTest t) {
 171         BufferedImage src = createBufferedImage();
 172         try {
 173             ImageOutputStream imageOutputStream =
 174                 ImageIO.createImageOutputStream(f);
 175 
 176             ImageTypeSpecifier imageTypeSpecifier =
 177                 new ImageTypeSpecifier(src);
 178             ImageWriter imageWriter =
 179                 ImageIO.getImageWritersByFormatName("PNG").next();
 180 
 181             imageWriter.setOutput(imageOutputStream);
 182 
 183             IIOMetadata m =
 184                 imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
 185 
 186             String format = m.getNativeMetadataFormatName();
 187             Node root = m.getAsTree(format);
 188 
 189             IIOMetadataNode iTXt = t.getNode();
 190             root.appendChild(iTXt);
 191             m.setFromTree(format, root);
 192 
 193             imageWriter.write(new IIOImage(src, null, m));
 194             imageOutputStream.close();
 195             System.out.println("Writing done.");
 196         } catch (Throwable e) {
 197             throw new RuntimeException("Writing test failed.", e);
 198         }
 199     }
 200 
 201     private static ITXtTest readFrom(File f) {
 202         try {
 203             ImageInputStream iis = ImageIO.createImageInputStream(f);
 204             ImageReader r = ImageIO.getImageReaders(iis).next();
 205             r.setInput(iis);

 206 
 207             IIOImage dst = r.readAll(0, null);
 208 
 209             // look for iTXt node
 210             IIOMetadata m = dst.getMetadata();
 211             Node root = m.getAsTree(m.getNativeMetadataFormatName());
 212             Node n = root.getFirstChild();
 213             while (n != null && !"iTXt".equals(n.getNodeName())) {
 214                 n = n.getNextSibling();
 215             }
 216             if (n == null) {
 217                 throw new RuntimeException("No iTXt node!");
 218             }
 219             ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
 220             return t;
 221         } catch (Throwable e) {
 222             throw new RuntimeException("Reading test failed.", e);
 223         }
 224     }
 225 
 226     private static BufferedImage createBufferedImage() {
 227         BufferedImage image = new BufferedImage(128, 128,
 228                       BufferedImage.TYPE_4BYTE_ABGR_PRE);
 229         Graphics2D graph = image.createGraphics();
 230         graph.setPaintMode();
 231         graph.setColor(Color.orange);
 232         graph.fillRect(32, 32, 64, 64);
 233         graph.dispose();
 234         return image;
 235     }
 236 }

   1 /*
   2  * Copyright (c) 2008, 2016 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6541476 7059970
  27  * @summary Test verifies that ImageIO PNG plug-in correctly handles the
  28  *          iTxt chunk (International textual data).
  29  *
  30  * @run     main ITXtTest
  31  */
  32 

  33 import java.awt.Color;
  34 import java.awt.Graphics2D;
  35 import java.awt.image.BufferedImage;
  36 import java.io.File;
  37 
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.ImageReader;
  40 import javax.imageio.IIOImage;
  41 import javax.imageio.ImageTypeSpecifier;
  42 import javax.imageio.ImageWriter;
  43 import javax.imageio.metadata.IIOMetadata;
  44 import javax.imageio.metadata.IIOMetadataNode;
  45 import javax.imageio.stream.ImageOutputStream;
  46 import javax.imageio.stream.ImageInputStream;
  47 
  48 import org.w3c.dom.Node;
  49 
  50 public class ITXtTest {
  51     static public void main(String args[]) {
  52         ITXtTest t_en = new ITXtTest();


 139         }
 140         ITXtTest t = (ITXtTest)o;
 141         if (!keyword.equals(t.keyword)) { return false; }
 142         if (isCompressed != t.isCompressed) { return false; }
 143         if (compression != t.compression) { return false; }
 144         if (!language.equals(t.language)) { return false; }
 145         if (!trasKeyword.equals(t.trasKeyword)) { return false; }
 146         if (!text.equals(t.text)) { return false; }
 147 
 148         return true;
 149     }
 150 
 151 
 152 
 153     private static void doTest(ITXtTest src) {
 154 
 155         System.out.println("Test: " + src.description);
 156 
 157         File file = new File("test.png");
 158 
 159         try {
 160             writeTo(file, src);
 161             ITXtTest dst = readFrom(file);

 162             if (dst == null || !dst.equals(src)) {
 163                 throw new RuntimeException("Test failed.");
 164             }
 165         } finally {
 166             file.delete();
 167         }
 168         System.out.println("Test passed.");
 169     }
 170 
 171     private static void writeTo(File f, ITXtTest t) {
 172         BufferedImage src = createBufferedImage();
 173         try (ImageOutputStream imageOutputStream =
 174                 ImageIO.createImageOutputStream(f)) {

 175 
 176             ImageTypeSpecifier imageTypeSpecifier =
 177                 new ImageTypeSpecifier(src);
 178             ImageWriter imageWriter =
 179                 ImageIO.getImageWritersByFormatName("PNG").next();
 180 
 181             imageWriter.setOutput(imageOutputStream);
 182 
 183             IIOMetadata m =
 184                 imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
 185 
 186             String format = m.getNativeMetadataFormatName();
 187             Node root = m.getAsTree(format);
 188 
 189             IIOMetadataNode iTXt = t.getNode();
 190             root.appendChild(iTXt);
 191             m.setFromTree(format, root);
 192 
 193             imageWriter.write(new IIOImage(src, null, m));

 194             System.out.println("Writing done.");
 195         } catch (Throwable e) {
 196             throw new RuntimeException("Writing test failed.", e);
 197         }
 198     }
 199 
 200     private static ITXtTest readFrom(File f) {
 201         try (ImageInputStream imageInputStream =
 202                 ImageIO.createImageInputStream(f)) {
 203 
 204             ImageReader r = ImageIO.getImageReaders(imageInputStream).next();
 205             r.setInput(imageInputStream);
 206 
 207             IIOImage dst = r.readAll(0, null);
 208 
 209             // look for iTXt node
 210             IIOMetadata m = dst.getMetadata();
 211             Node root = m.getAsTree(m.getNativeMetadataFormatName());
 212             Node n = root.getFirstChild();
 213             while (n != null && !"iTXt".equals(n.getNodeName())) {
 214                 n = n.getNextSibling();
 215             }
 216             if (n == null) {
 217                 throw new RuntimeException("No iTXt node!");
 218             }
 219             ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
 220             return t;
 221         } catch (Throwable e) {
 222             throw new RuntimeException("Reading test failed.", e);
 223         }
 224     }
 225 
 226     private static BufferedImage createBufferedImage() {
 227         BufferedImage image = new BufferedImage(128, 128,
 228                       BufferedImage.TYPE_4BYTE_ABGR_PRE);
 229         Graphics2D graph = image.createGraphics();
 230         graph.setPaintMode();
 231         graph.setColor(Color.orange);
 232         graph.fillRect(32, 32, 64, 64);
 233         graph.dispose();
 234         return image;
 235     }
 236 }
 237 
< prev index next >