< prev index next >

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

Print this page




  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();
  54         t_en.description = "xml - en";
  55         t_en.keyword = "XML:com.adobe.xmp";
  56         t_en.isCompressed = false;
  57         t_en.compression = 0;
  58         t_en.language = "en";
  59         t_en.trasKeyword = "XML:com.adobe.xmp";
  60         t_en.text = "<xml>Something</xml>";
  61 
  62         doTest(t_en);
  63 
  64         // check compression case
  65         t_en.isCompressed = true;
  66         t_en.description = "xml - en - compressed";
  67 
  68         doTest(t_en);
  69 
  70         ITXtTest t_ru = new ITXtTest();
  71         t_ru.description = "xml - ru";
  72         t_ru.keyword = "XML:com.adobe.xmp";


 134     }
 135 
 136     @Override
 137     public boolean equals(Object o) {
 138         if (! (o instanceof ITXtTest)) {
 139             return false;
 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 }


  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 import java.io.IOException;
  49 
  50 import org.w3c.dom.Node;
  51 
  52 public class ITXtTest {
  53     static public void main(String args[]) throws IOException {
  54         ITXtTest t_en = new ITXtTest();
  55         t_en.description = "xml - en";
  56         t_en.keyword = "XML:com.adobe.xmp";
  57         t_en.isCompressed = false;
  58         t_en.compression = 0;
  59         t_en.language = "en";
  60         t_en.trasKeyword = "XML:com.adobe.xmp";
  61         t_en.text = "<xml>Something</xml>";
  62 
  63         doTest(t_en);
  64 
  65         // check compression case
  66         t_en.isCompressed = true;
  67         t_en.description = "xml - en - compressed";
  68 
  69         doTest(t_en);
  70 
  71         ITXtTest t_ru = new ITXtTest();
  72         t_ru.description = "xml - ru";
  73         t_ru.keyword = "XML:com.adobe.xmp";


 135     }
 136 
 137     @Override
 138     public boolean equals(Object o) {
 139         if (! (o instanceof ITXtTest)) {
 140             return false;
 141         }
 142         ITXtTest t = (ITXtTest)o;
 143         if (!keyword.equals(t.keyword)) { return false; }
 144         if (isCompressed != t.isCompressed) { return false; }
 145         if (compression != t.compression) { return false; }
 146         if (!language.equals(t.language)) { return false; }
 147         if (!trasKeyword.equals(t.trasKeyword)) { return false; }
 148         if (!text.equals(t.text)) { return false; }
 149 
 150         return true;
 151     }
 152 
 153 
 154 
 155     private static void doTest(ITXtTest src) throws IOException {
 156 
 157         System.out.println("Test: " + src.description);
 158 
 159         File file = new File("test.png");
 160 
 161         writeTo(file, src);
 162         ITXtTest dst = readFrom(file);
 163 
 164         if (dst == null || !dst.equals(src)) {
 165             file.delete();
 166             throw new RuntimeException("Test failed.");
 167         } else {
 168             file.delete();
 169             System.out.println("Test passed.");
 170         }
 171     }
 172 
 173     private static void writeTo(File f, ITXtTest t) throws IOException {
 174         BufferedImage src = createBufferedImage();
 175         ImageOutputStream imageOutputStream = null;
 176         try {
 177             imageOutputStream =
 178                 ImageIO.createImageOutputStream(f);
 179 
 180             ImageTypeSpecifier imageTypeSpecifier =
 181                 new ImageTypeSpecifier(src);
 182             ImageWriter imageWriter =
 183                 ImageIO.getImageWritersByFormatName("PNG").next();
 184 
 185             imageWriter.setOutput(imageOutputStream);
 186 
 187             IIOMetadata m =
 188                 imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
 189 
 190             String format = m.getNativeMetadataFormatName();
 191             Node root = m.getAsTree(format);
 192 
 193             IIOMetadataNode iTXt = t.getNode();
 194             root.appendChild(iTXt);
 195             m.setFromTree(format, root);
 196 
 197             imageWriter.write(new IIOImage(src, null, m));
 198             imageOutputStream.close();
 199             System.out.println("Writing done.");
 200         } catch (Throwable e) {
 201             if (imageOutputStream != null) {
 202                 imageOutputStream.close();
 203             }
 204             f.delete();
 205             throw new RuntimeException("Writing test failed.", e);
 206         }
 207     }
 208 
 209     private static ITXtTest readFrom(File f) throws IOException {
 210         ImageInputStream iis = null;
 211         try {
 212             iis = ImageIO.createImageInputStream(f);
 213             ImageReader r = ImageIO.getImageReaders(iis).next();
 214             r.setInput(iis);
 215 
 216             IIOImage dst = r.readAll(0, null);
 217 
 218             // look for iTXt node
 219             IIOMetadata m = dst.getMetadata();
 220             Node root = m.getAsTree(m.getNativeMetadataFormatName());
 221             Node n = root.getFirstChild();
 222             while (n != null && !"iTXt".equals(n.getNodeName())) {
 223                 n = n.getNextSibling();
 224             }
 225             if (n == null) {
 226                 throw new RuntimeException("No iTXt node!");
 227             }
 228             ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
 229             iis.close();
 230             return t;
 231         } catch (Throwable e) {
 232             if (iis != null) {
 233                 iis.close();
 234             }
 235             f.delete();
 236             throw new RuntimeException("Reading test failed.", e);
 237         }
 238     }
 239 
 240     private static BufferedImage createBufferedImage() {
 241         BufferedImage image = new BufferedImage(128, 128,
 242                       BufferedImage.TYPE_4BYTE_ABGR_PRE);
 243         Graphics2D graph = image.createGraphics();
 244         graph.setPaintMode();
 245         graph.setColor(Color.orange);
 246         graph.fillRect(32, 32, 64, 64);
 247         graph.dispose();
 248         return image;
 249     }
 250 }
< prev index next >