jdk/test/javax/imageio/plugins/png/ITXtTest.java
author bae
Sat, 01 Nov 2008 20:42:18 +0300
changeset 1734 861400729115
child 2375 bb4dd76ca2c9
permissions -rw-r--r--
6541476: PNG imageio plugin incorrectly handles iTXt chunk Reviewed-by: igor, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     1
/*
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     4
 *
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     7
 * published by the Free Software Foundation.
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     8
 *
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    13
 * accompanied this code).
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    14
 *
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    18
 *
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    21
 * have any questions.
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    22
 */
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    23
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    24
/**
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    25
 * @test
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    26
 * @bug     6541476
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    27
 * @summary Test verifies that ImageIO PNG plugin correcly handles the
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    28
 *          iTxt chunk (International textual data).
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    29
 *
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    30
 * @run     main ITXtTest
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    31
 */
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    32
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    33
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    34
import java.awt.Color;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    35
import java.awt.Graphics2D;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    36
import java.awt.image.BufferedImage;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    37
import java.io.File;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    38
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    39
import javax.imageio.ImageIO;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    40
import javax.imageio.ImageReader;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    41
import javax.imageio.IIOImage;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    42
import javax.imageio.ImageTypeSpecifier;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    43
import javax.imageio.ImageWriter;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    44
import javax.imageio.metadata.IIOMetadata;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    45
import javax.imageio.metadata.IIOMetadataNode;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    46
import javax.imageio.stream.ImageOutputStream;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    47
import javax.imageio.stream.ImageInputStream;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    48
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    49
import org.w3c.dom.Node;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    50
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    51
public class ITXtTest {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    52
    static public void main(String args[]) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    53
        ITXtTest t_en = new ITXtTest();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    54
        t_en.description = "xml - en";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    55
        t_en.keyword = "XML:com.adobe.xmp";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    56
        t_en.isCompressed = false;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    57
        t_en.compression = 0;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    58
        t_en.language = "en";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    59
        t_en.trasKeyword = "XML:com.adobe.xmp";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    60
        t_en.text = "<xml>Something</xml>";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    61
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    62
        doTest(t_en);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    63
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    64
        // check compression case
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    65
        t_en.isCompressed = true;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    66
        t_en.description = "xml - en - compressed";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    67
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    68
        doTest(t_en);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    69
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    70
        ITXtTest t_ru = new ITXtTest();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    71
        t_ru.description = "xml - ru";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    72
        t_ru.keyword = "XML:com.adobe.xmp";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    73
        t_ru.isCompressed = false;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    74
        t_ru.compression = 0;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    75
        t_ru.language = "ru";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    76
        t_ru.trasKeyword = "\u0410\u0410\u0410\u0410\u0410 XML";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    77
        t_ru.text = "<xml>\u042A\u042F\u042F\u042F\u042F\u042F\u042F</xml>";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    78
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    79
        doTest(t_ru);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    80
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    81
        t_ru.isCompressed = true;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    82
        t_ru.description = "xml - ru - compressed";
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    83
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    84
        doTest(t_ru);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    85
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    86
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    87
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    88
    String description;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    89
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    90
    String keyword;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    91
    boolean isCompressed;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    92
    int compression;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    93
    String language;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    94
    String trasKeyword;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    95
    String text;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    96
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    97
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    98
    public IIOMetadataNode getNode() {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
    99
        IIOMetadataNode iTXt = new IIOMetadataNode("iTXt");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   100
        IIOMetadataNode iTXtEntry = new IIOMetadataNode("iTXtEntry");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   101
        iTXtEntry.setAttribute("keyword", keyword);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   102
        iTXtEntry.setAttribute("compressionFlag",
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   103
                               isCompressed ? "true" : "false");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   104
        iTXtEntry.setAttribute("compressionMethod",
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   105
                               Integer.toString(compression));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   106
        iTXtEntry.setAttribute("languageTag", language);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   107
        iTXtEntry.setAttribute("translatedKeyword",
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   108
                               trasKeyword);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   109
        iTXtEntry.setAttribute("text", text);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   110
        iTXt.appendChild(iTXtEntry);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   111
        return iTXt;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   112
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   113
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   114
    public static ITXtTest getFromNode(IIOMetadataNode n) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   115
        ITXtTest t = new ITXtTest();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   116
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   117
        if (!"iTXt".equals(n.getNodeName())) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   118
            throw new RuntimeException("Invalid node");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   119
        }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   120
        IIOMetadataNode e = (IIOMetadataNode)n.getFirstChild();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   121
        if (!"iTXtEntry".equals(e.getNodeName())) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   122
            throw new RuntimeException("Invalid entry node");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   123
        }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   124
        t.keyword = e.getAttribute("keyword");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   125
        t.isCompressed =
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   126
            (Integer.valueOf(e.getAttribute("compressionFlag")).intValue() == 1);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   127
        t.compression =
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   128
            Integer.valueOf(e.getAttribute("compressionMethod")).intValue();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   129
        t.language = e.getAttribute("languageTag");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   130
        t.trasKeyword = e.getAttribute("translatedKeyword");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   131
        t.text = e.getAttribute("text");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   132
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   133
        return t;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   134
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   135
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   136
    @Override
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   137
    public boolean equals(Object o) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   138
        if (! (o instanceof ITXtTest)) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   139
            return false;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   140
        }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   141
        ITXtTest t = (ITXtTest)o;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   142
        if (!keyword.equals(t.keyword)) { return false; }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   143
        if (isCompressed != t.isCompressed) { return false; }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   144
        if (compression != t.compression) { return false; }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   145
        if (!language.equals(t.language)) { return false; }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   146
        if (!trasKeyword.equals(t.trasKeyword)) { return false; }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   147
        if (!text.equals(t.text)) { return false; }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   148
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   149
        return true;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   150
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   151
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   152
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   153
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   154
    private static void doTest(ITXtTest src) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   155
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   156
        System.out.println("Test: " + src.description);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   157
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   158
        File file = new File("test.png");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   159
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   160
        writeTo(file, src);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   161
        ITXtTest dst = readFrom(file);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   162
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   163
        if (dst == null || !dst.equals(src)) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   164
            throw new RuntimeException("Test failed.");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   165
        }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   166
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   167
        System.out.println("Test passed.");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   168
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   169
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   170
    private static void writeTo(File f, ITXtTest t) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   171
        BufferedImage src = createBufferedImage();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   172
        try {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   173
            ImageOutputStream imageOutputStream =
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   174
                ImageIO.createImageOutputStream(f);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   175
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   176
            ImageTypeSpecifier imageTypeSpecifier =
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   177
                new ImageTypeSpecifier(src);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   178
            ImageWriter imageWriter =
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   179
                ImageIO.getImageWritersByFormatName("PNG").next();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   180
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   181
            imageWriter.setOutput(imageOutputStream);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   182
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   183
            IIOMetadata m =
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   184
                imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   185
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   186
            String format = m.getNativeMetadataFormatName();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   187
            Node root = m.getAsTree(format);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   188
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   189
            IIOMetadataNode iTXt = t.getNode();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   190
            root.appendChild(iTXt);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   191
            m.setFromTree(format, root);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   192
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   193
            imageWriter.write(new IIOImage(src, null, m));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   194
            imageOutputStream.close();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   195
            System.out.println("Writing done.");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   196
        } catch (Throwable e) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   197
            throw new RuntimeException("Writing test failed.", e);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   198
        }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   199
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   200
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   201
    private static ITXtTest readFrom(File f) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   202
        try {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   203
            ImageInputStream iis = ImageIO.createImageInputStream(f);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   204
            ImageReader r = ImageIO.getImageReaders(iis).next();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   205
            r.setInput(iis);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   206
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   207
            IIOImage dst = r.readAll(0, null);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   208
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   209
            // look for iTXt node
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   210
            IIOMetadata m = dst.getMetadata();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   211
            Node root = m.getAsTree(m.getNativeMetadataFormatName());
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   212
            Node n = root.getFirstChild();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   213
            while (n != null && !"iTXt".equals(n.getNodeName())) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   214
                n = n.getNextSibling();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   215
            }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   216
            if (n == null) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   217
                throw new RuntimeException("No iTXt node!");
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   218
            }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   219
            ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   220
            return t;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   221
        } catch (Throwable e) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   222
            throw new RuntimeException("Reading test failed.", e);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   223
        }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   224
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   225
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   226
    private static BufferedImage createBufferedImage() {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   227
        BufferedImage image = new BufferedImage(128, 128,
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   228
                      BufferedImage.TYPE_4BYTE_ABGR_PRE);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   229
        Graphics2D graph = image.createGraphics();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   230
        graph.setPaintMode();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   231
        graph.setColor(Color.orange);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   232
        graph.fillRect(32, 32, 64, 64);
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   233
        graph.dispose();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   234
        return image;
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   235
    }
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
diff changeset
   236
}