src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGMetadata.java
author jdv
Wed, 18 Apr 2018 12:33:21 +0530
changeset 49995 6f595ec05539
parent 47216 71c04702a3d5
child 50342 ddbd2037f9ef
permissions -rw-r--r--
6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images Reviewed-by: prr, pnarayanan, kaddepalli
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49995
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.imageio.plugins.png;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.StringTokenizer;
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    33
import java.util.ListIterator;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    34
import java.time.LocalDateTime;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    35
import java.time.OffsetDateTime;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    36
import java.time.ZoneId;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    37
import java.time.ZoneOffset;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    38
import java.time.format.DateTimeFormatter;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    39
import java.time.format.DateTimeParseException;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
    40
import java.time.temporal.TemporalAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.imageio.metadata.IIOInvalidTreeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.imageio.metadata.IIOMetadataFormatImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.imageio.metadata.IIOMetadataNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class PNGMetadata extends IIOMetadata implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // package scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        nativeMetadataFormatName = "javax_imageio_png_1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected static final String nativeMetadataFormatClassName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        = "com.sun.imageio.plugins.png.PNGMetadataFormat";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Color types for IHDR chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    58
    static final String[] IHDR_colorTypeNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "Grayscale", null, "RGB", "Palette",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "GrayAlpha", null, "RGBAlpha"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    63
    static final int[] IHDR_numChannels = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        1, 0, 3, 3, 2, 0, 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Bit depths for IHDR chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    68
    static final String[] IHDR_bitDepths = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "1", "2", "4", "8", "16"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Compression methods for IHDR chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    73
    static final String[] IHDR_compressionMethodNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "deflate"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // Filter methods for IHDR chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    78
    static final String[] IHDR_filterMethodNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "adaptive"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // Interlace methods for IHDR chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    83
    static final String[] IHDR_interlaceMethodNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        "none", "adam7"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Compression methods for iCCP chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    88
    static final String[] iCCP_compressionMethodNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        "deflate"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // Compression methods for zTXt chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
    93
    static final String[] zTXt_compressionMethodNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        "deflate"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // "Unknown" unit for pHYs chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static final int PHYS_UNIT_UNKNOWN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // "Meter" unit for pHYs chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final int PHYS_UNIT_METER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // Unit specifiers for pHYs chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
   104
    static final String[] unitSpecifierNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        "unknown", "meter"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Rendering intents for sRGB chunk
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
   109
    static final String[] renderingIntentNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        "Perceptual", // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        "Relative colorimetric", // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        "Saturation", // 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        "Absolute colorimetric" // 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // Color space types for Chroma->ColorSpaceType node
20415
a88fd521ef00 8001119: [fingbugs] Evaluate necessity to make some arrays package protected
vadim
parents: 5506
diff changeset
   118
    static final String[] colorSpaceTypeNames = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        "GRAY", null, "RGB", "RGB",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        "GRAY", null, "RGB"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // IHDR chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean IHDR_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int IHDR_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public int IHDR_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public int IHDR_bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int IHDR_colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public int IHDR_compressionMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public int IHDR_filterMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public int IHDR_interlaceMethod; // 0 == none, 1 == adam7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // PLTE chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public boolean PLTE_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public byte[] PLTE_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public byte[] PLTE_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public byte[] PLTE_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // If non-null, used to reorder palette entries during encoding in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // order to minimize the size of the tRNS chunk.  Thus an index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // 'i' in the source should be encoded as index 'PLTE_order[i]'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // PLTE_order will be null unless 'initialize' is called with an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // IndexColorModel image type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public int[] PLTE_order = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // bKGD chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // If external (non-PNG sourced) data has red = green = blue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // always store it as gray and promote when writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public boolean bKGD_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public int bKGD_colorType; // PNG_COLOR_GRAY, _RGB, or _PALETTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public int bKGD_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public int bKGD_gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public int bKGD_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public int bKGD_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public int bKGD_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // cHRM chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public boolean cHRM_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public int cHRM_whitePointX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int cHRM_whitePointY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int cHRM_redX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public int cHRM_redY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public int cHRM_greenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public int cHRM_greenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int cHRM_blueX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int cHRM_blueY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // gAMA chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public boolean gAMA_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int gAMA_gamma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // hIST chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public boolean hIST_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public char[] hIST_histogram;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // iCCP chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public boolean iCCP_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public String iCCP_profileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public int iCCP_compressionMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public byte[] iCCP_compressedProfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // iTXt chunk
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   183
    public ArrayList<String> iTXt_keyword = new ArrayList<String>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   184
    public ArrayList<Boolean> iTXt_compressionFlag = new ArrayList<Boolean>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   185
    public ArrayList<Integer> iTXt_compressionMethod = new ArrayList<Integer>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   186
    public ArrayList<String> iTXt_languageTag = new ArrayList<String>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   187
    public ArrayList<String> iTXt_translatedKeyword = new ArrayList<String>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   188
    public ArrayList<String> iTXt_text = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // pHYs chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public boolean pHYs_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int pHYs_pixelsPerUnitXAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public int pHYs_pixelsPerUnitYAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public int pHYs_unitSpecifier; // 0 == unknown, 1 == meter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // sBIT chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public boolean sBIT_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public int sBIT_colorType; // PNG_COLOR_GRAY, _GRAY_ALPHA, _RGB, _RGB_ALPHA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public int sBIT_grayBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public int sBIT_redBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public int sBIT_greenBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public int sBIT_blueBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public int sBIT_alphaBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // sPLT chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public boolean sPLT_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public String sPLT_paletteName; // 1-79 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public int sPLT_sampleDepth; // 8 or 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public int[] sPLT_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public int[] sPLT_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public int[] sPLT_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public int[] sPLT_alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public int[] sPLT_frequency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // sRGB chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public boolean sRGB_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public int sRGB_renderingIntent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // tEXt chunk
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   220
    public ArrayList<String> tEXt_keyword = new ArrayList<String>(); // 1-79 characters
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   221
    public ArrayList<String> tEXt_text = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   223
    // tIME chunk. Gives the image modification time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean tIME_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public int tIME_year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public int tIME_month;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public int tIME_day;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public int tIME_hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public int tIME_minute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public int tIME_second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   232
    // Specifies whether metadata contains Standard/Document/ImageCreationTime
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   233
    public boolean creation_time_present;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   234
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   235
    // Values that make up Standard/Document/ImageCreationTime
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   236
    public int creation_time_year;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   237
    public int creation_time_month;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   238
    public int creation_time_day;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   239
    public int creation_time_hour;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   240
    public int creation_time_minute;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   241
    public int creation_time_second;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   242
    public ZoneOffset creation_time_offset;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   243
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   244
    /*
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   245
     * tEXt_creation_time_present- Specifies whether any text chunk (tEXt, iTXt,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   246
     * zTXt) exists with image creation time. The data structure corresponding
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   247
     * to the last decoded text chunk with creation time is indicated by the
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   248
     * iterator- tEXt_creation_time_iter.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   249
     *
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   250
     * Any update to the text chunks with creation time is reflected on
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   251
     * Standard/Document/ImageCreationTime after retrieving time from the text
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   252
     * chunk. If there are multiple text chunks with creation time, the time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   253
     * retrieved from the last decoded text chunk will be used. A point to note
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   254
     * is that, retrieval of time from text chunks is possible only if the
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   255
     * encoded time in the chunk confirms to either the recommended RFC1123
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   256
     * format or ISO format.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   257
     *
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   258
     * Similarly, any update to Standard/Document/ImageCreationTime is reflected
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   259
     * on the last decoded text chunk's data structure with time encoded in
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   260
     * RFC1123 format. By updating the text chunk's data structure, we also
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   261
     * ensure that PNGImageWriter will write image creation time on the output.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   262
     */
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   263
    public boolean tEXt_creation_time_present;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   264
    private ListIterator<String> tEXt_creation_time_iter = null;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   265
    public static final String tEXt_creationTimeKey = "Creation Time";
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
   266
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // tRNS chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // If external (non-PNG sourced) data has red = green = blue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    // always store it as gray and promote when writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public boolean tRNS_present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public int tRNS_colorType; // PNG_COLOR_GRAY, _RGB, or _PALETTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public byte[] tRNS_alpha; // May have fewer entries than PLTE_red, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public int tRNS_gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public int tRNS_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public int tRNS_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public int tRNS_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    // zTXt chunk
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   279
    public ArrayList<String> zTXt_keyword = new ArrayList<String>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   280
    public ArrayList<Integer> zTXt_compressionMethod = new ArrayList<Integer>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   281
    public ArrayList<String> zTXt_text = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // Unknown chunks
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   284
    public ArrayList<String> unknownChunkType = new ArrayList<String>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   285
    public ArrayList<byte[]> unknownChunkData = new ArrayList<byte[]>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public PNGMetadata() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        super(true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
              nativeMetadataFormatName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
              nativeMetadataFormatClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
              null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public PNGMetadata(IIOMetadata metadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // TODO -- implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Sets the IHDR_bitDepth and IHDR_colorType variables.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   300
     * The {@code numBands} parameter is necessary since
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * we may only be writing a subset of the image bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public void initialize(ImageTypeSpecifier imageType, int numBands) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        ColorModel colorModel = imageType.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        SampleModel sampleModel = imageType.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // Initialize IHDR_bitDepth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int[] sampleSize = sampleModel.getSampleSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int bitDepth = sampleSize[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // Choose max bit depth over all channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Fixes bug 4413109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        for (int i = 1; i < sampleSize.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (sampleSize[i] > bitDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                bitDepth = sampleSize[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // Multi-channel images must have a bit depth of 8 or 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (sampleSize.length > 1 && bitDepth < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            bitDepth = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // Round bit depth up to a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (bitDepth > 2 && bitDepth < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            bitDepth = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else if (bitDepth > 4 && bitDepth < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            bitDepth = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else if (bitDepth > 8 && bitDepth < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            bitDepth = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else if (bitDepth > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new RuntimeException("bitDepth > 16!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        IHDR_bitDepth = bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // Initialize IHDR_colorType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (colorModel instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            IndexColorModel icm = (IndexColorModel)colorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            int size = icm.getMapSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            byte[] reds = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            icm.getReds(reds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            byte[] greens = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            icm.getGreens(greens);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            byte[] blues = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            icm.getBlues(blues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // Determine whether the color tables are actually a gray ramp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // if the color type has not been set previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            boolean isGray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (!IHDR_present ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                (IHDR_colorType != PNGImageReader.PNG_COLOR_PALETTE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                isGray = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                int scale = 255/((1 << IHDR_bitDepth) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    byte red = reds[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if ((red != (byte)(i*scale)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        (red != greens[i]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        (red != blues[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        isGray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // Determine whether transparency exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            boolean hasAlpha = colorModel.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            byte[] alpha = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                alpha = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                icm.getAlphas(alpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             * NB: PNG_COLOR_GRAY_ALPHA color type may be not optimal for images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * contained more than 1024 pixels (or even than 768 pixels in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             * single transparent pixel in palette).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             * For such images alpha samples in raster will occupy more space than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             * it is required to store palette so it could be reasonable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             * use PNG_COLOR_PALETTE color type for large images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (isGray && hasAlpha && (bitDepth == 8 || bitDepth == 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            } else if (isGray && !hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                IHDR_colorType = PNGImageReader.PNG_COLOR_PALETTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                PLTE_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                PLTE_order = null;
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   390
                PLTE_red = reds.clone();
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   391
                PLTE_green = greens.clone();
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   392
                PLTE_blue = blues.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    tRNS_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    PLTE_order = new int[alpha.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    // Reorder the palette so that non-opaque entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    // come first.  Since the tRNS chunk does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    // to store trailing 255's, this can save a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    // considerable amount of space when encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    // images with only one transparent pixel value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    // e.g., images from GIF sources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    byte[] newAlpha = new byte[alpha.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // Scan for non-opaque entries and assign them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    // positions starting at 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    int newIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    for (int i = 0; i < alpha.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        if (alpha[i] != (byte)255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            PLTE_order[i] = newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            newAlpha[newIndex] = alpha[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            ++newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    int numTransparent = newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    // Scan for opaque entries and assign them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    // positions following the non-opaque entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    for (int i = 0; i < alpha.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        if (alpha[i] == (byte)255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            PLTE_order[i] = newIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    // Reorder the palettes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    byte[] oldRed = PLTE_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    byte[] oldGreen = PLTE_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    byte[] oldBlue = PLTE_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    int len = oldRed.length; // All have the same length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    PLTE_red = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    PLTE_green = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    PLTE_blue = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        PLTE_red[PLTE_order[i]] = oldRed[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        PLTE_green[PLTE_order[i]] = oldGreen[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        PLTE_blue[PLTE_order[i]] = oldBlue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    // Copy only the transparent entries into tRNS_alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    tRNS_alpha = new byte[numTransparent];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    System.arraycopy(newAlpha, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                     tRNS_alpha, 0, numTransparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (numBands == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            } else if (numBands == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            } else if (numBands == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                IHDR_colorType = PNGImageReader.PNG_COLOR_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            } else if (numBands == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                IHDR_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                throw new RuntimeException("Number of bands not 1-4!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        IHDR_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public boolean isReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   470
    private ArrayList<byte[]> cloneBytesArrayList(ArrayList<byte[]> in) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } else {
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   474
            ArrayList<byte[]> list = new ArrayList<byte[]>(in.size());
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
   475
            for (byte[] b: in) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   476
                list.add((b == null) ? null : b.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    // Deep clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        PNGMetadata metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            metadata = (PNGMetadata)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // unknownChunkData needs deep clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        metadata.unknownChunkData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            cloneBytesArrayList(this.unknownChunkData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public Node getAsTree(String formatName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (formatName.equals(nativeMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return getNativeTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } else if (formatName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                   (IIOMetadataFormatImpl.standardMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return getStandardTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            throw new IllegalArgumentException("Not a recognized format!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private Node getNativeTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // IHDR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (IHDR_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            IIOMetadataNode IHDR_node = new IIOMetadataNode("IHDR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            IHDR_node.setAttribute("width", Integer.toString(IHDR_width));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            IHDR_node.setAttribute("height", Integer.toString(IHDR_height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            IHDR_node.setAttribute("bitDepth",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                   Integer.toString(IHDR_bitDepth));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            IHDR_node.setAttribute("colorType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                                   IHDR_colorTypeNames[IHDR_colorType]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            // IHDR_compressionMethod must be 0 in PNG 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            IHDR_node.setAttribute("compressionMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                          IHDR_compressionMethodNames[IHDR_compressionMethod]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            // IHDR_filterMethod must be 0 in PNG 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            IHDR_node.setAttribute("filterMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                    IHDR_filterMethodNames[IHDR_filterMethod]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            IHDR_node.setAttribute("interlaceMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                              IHDR_interlaceMethodNames[IHDR_interlaceMethod]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            root.appendChild(IHDR_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // PLTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (PLTE_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            IIOMetadataNode PLTE_node = new IIOMetadataNode("PLTE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            int numEntries = PLTE_red.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                IIOMetadataNode entry = new IIOMetadataNode("PLTEEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                entry.setAttribute("index", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                entry.setAttribute("red",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                   Integer.toString(PLTE_red[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                entry.setAttribute("green",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                   Integer.toString(PLTE_green[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                entry.setAttribute("blue",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                   Integer.toString(PLTE_blue[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                PLTE_node.appendChild(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            root.appendChild(PLTE_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // bKGD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (bKGD_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            IIOMetadataNode bKGD_node = new IIOMetadataNode("bKGD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (bKGD_colorType == PNGImageReader.PNG_COLOR_PALETTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                node = new IIOMetadataNode("bKGD_Palette");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                node.setAttribute("index", Integer.toString(bKGD_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            } else if (bKGD_colorType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                node = new IIOMetadataNode("bKGD_Grayscale");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                node.setAttribute("gray", Integer.toString(bKGD_gray));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            } else if (bKGD_colorType == PNGImageReader.PNG_COLOR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                node = new IIOMetadataNode("bKGD_RGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                node.setAttribute("red", Integer.toString(bKGD_red));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                node.setAttribute("green", Integer.toString(bKGD_green));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                node.setAttribute("blue", Integer.toString(bKGD_blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            bKGD_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            root.appendChild(bKGD_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // cHRM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (cHRM_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            IIOMetadataNode cHRM_node = new IIOMetadataNode("cHRM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            cHRM_node.setAttribute("whitePointX",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                              Integer.toString(cHRM_whitePointX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            cHRM_node.setAttribute("whitePointY",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                              Integer.toString(cHRM_whitePointY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            cHRM_node.setAttribute("redX", Integer.toString(cHRM_redX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            cHRM_node.setAttribute("redY", Integer.toString(cHRM_redY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            cHRM_node.setAttribute("greenX", Integer.toString(cHRM_greenX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            cHRM_node.setAttribute("greenY", Integer.toString(cHRM_greenY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            cHRM_node.setAttribute("blueX", Integer.toString(cHRM_blueX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            cHRM_node.setAttribute("blueY", Integer.toString(cHRM_blueY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            root.appendChild(cHRM_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // gAMA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (gAMA_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            IIOMetadataNode gAMA_node = new IIOMetadataNode("gAMA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            gAMA_node.setAttribute("value", Integer.toString(gAMA_gamma));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            root.appendChild(gAMA_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        // hIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (hIST_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            IIOMetadataNode hIST_node = new IIOMetadataNode("hIST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            for (int i = 0; i < hIST_histogram.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                IIOMetadataNode hist =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    new IIOMetadataNode("hISTEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                hist.setAttribute("index", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                hist.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                  Integer.toString(hIST_histogram[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                hIST_node.appendChild(hist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            root.appendChild(hIST_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // iCCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (iCCP_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            IIOMetadataNode iCCP_node = new IIOMetadataNode("iCCP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            iCCP_node.setAttribute("profileName", iCCP_profileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            iCCP_node.setAttribute("compressionMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                          iCCP_compressionMethodNames[iCCP_compressionMethod]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            Object profile = iCCP_compressedProfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (profile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                profile = ((byte[])profile).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            iCCP_node.setUserObject(profile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            root.appendChild(iCCP_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // iTXt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (iTXt_keyword.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            IIOMetadataNode iTXt_parent = new IIOMetadataNode("iTXt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            for (int i = 0; i < iTXt_keyword.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                IIOMetadataNode iTXt_node = new IIOMetadataNode("iTXtEntry");
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   635
                iTXt_node.setAttribute("keyword", iTXt_keyword.get(i));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   636
                iTXt_node.setAttribute("compressionFlag",
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
   637
                        iTXt_compressionFlag.get(i) ? "TRUE" : "FALSE");
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   638
                iTXt_node.setAttribute("compressionMethod",
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   639
                        iTXt_compressionMethod.get(i).toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                iTXt_node.setAttribute("languageTag",
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   641
                                       iTXt_languageTag.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                iTXt_node.setAttribute("translatedKeyword",
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   643
                                       iTXt_translatedKeyword.get(i));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
   644
                iTXt_node.setAttribute("text", iTXt_text.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                iTXt_parent.appendChild(iTXt_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            root.appendChild(iTXt_parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        // pHYs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (pHYs_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            IIOMetadataNode pHYs_node = new IIOMetadataNode("pHYs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            pHYs_node.setAttribute("pixelsPerUnitXAxis",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                              Integer.toString(pHYs_pixelsPerUnitXAxis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            pHYs_node.setAttribute("pixelsPerUnitYAxis",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                   Integer.toString(pHYs_pixelsPerUnitYAxis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            pHYs_node.setAttribute("unitSpecifier",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                                   unitSpecifierNames[pHYs_unitSpecifier]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            root.appendChild(pHYs_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // sBIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (sBIT_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            IIOMetadataNode sBIT_node = new IIOMetadataNode("sBIT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                node = new IIOMetadataNode("sBIT_Grayscale");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                node.setAttribute("gray",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                  Integer.toString(sBIT_grayBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            } else if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                node = new IIOMetadataNode("sBIT_GrayAlpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                node.setAttribute("gray",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                  Integer.toString(sBIT_grayBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                node.setAttribute("alpha",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                  Integer.toString(sBIT_alphaBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            } else if (sBIT_colorType == PNGImageReader.PNG_COLOR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                node = new IIOMetadataNode("sBIT_RGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                node.setAttribute("red",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                  Integer.toString(sBIT_redBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                node.setAttribute("green",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                  Integer.toString(sBIT_greenBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                node.setAttribute("blue",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                  Integer.toString(sBIT_blueBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            } else if (sBIT_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                node = new IIOMetadataNode("sBIT_RGBAlpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                node.setAttribute("red",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                  Integer.toString(sBIT_redBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                node.setAttribute("green",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                  Integer.toString(sBIT_greenBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                node.setAttribute("blue",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                  Integer.toString(sBIT_blueBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                node.setAttribute("alpha",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                  Integer.toString(sBIT_alphaBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            } else if (sBIT_colorType == PNGImageReader.PNG_COLOR_PALETTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                node = new IIOMetadataNode("sBIT_Palette");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                node.setAttribute("red",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                                  Integer.toString(sBIT_redBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                node.setAttribute("green",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                  Integer.toString(sBIT_greenBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                node.setAttribute("blue",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                  Integer.toString(sBIT_blueBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            sBIT_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            root.appendChild(sBIT_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        // sPLT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (sPLT_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            IIOMetadataNode sPLT_node = new IIOMetadataNode("sPLT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            sPLT_node.setAttribute("name", sPLT_paletteName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            sPLT_node.setAttribute("sampleDepth",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                                   Integer.toString(sPLT_sampleDepth));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            int numEntries = sPLT_red.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                IIOMetadataNode entry = new IIOMetadataNode("sPLTEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                entry.setAttribute("index", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                entry.setAttribute("red", Integer.toString(sPLT_red[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                entry.setAttribute("green", Integer.toString(sPLT_green[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                entry.setAttribute("blue", Integer.toString(sPLT_blue[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                entry.setAttribute("alpha", Integer.toString(sPLT_alpha[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                entry.setAttribute("frequency",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                  Integer.toString(sPLT_frequency[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                sPLT_node.appendChild(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            root.appendChild(sPLT_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (sRGB_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            IIOMetadataNode sRGB_node = new IIOMetadataNode("sRGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            sRGB_node.setAttribute("renderingIntent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                   renderingIntentNames[sRGB_renderingIntent]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            root.appendChild(sRGB_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // tEXt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (tEXt_keyword.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            IIOMetadataNode tEXt_parent = new IIOMetadataNode("tEXt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            for (int i = 0; i < tEXt_keyword.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                IIOMetadataNode tEXt_node = new IIOMetadataNode("tEXtEntry");
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   749
                tEXt_node.setAttribute("keyword" , tEXt_keyword.get(i));
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   750
                tEXt_node.setAttribute("value" , tEXt_text.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                tEXt_parent.appendChild(tEXt_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            root.appendChild(tEXt_parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        // tIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (tIME_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            IIOMetadataNode tIME_node = new IIOMetadataNode("tIME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            tIME_node.setAttribute("year", Integer.toString(tIME_year));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            tIME_node.setAttribute("month", Integer.toString(tIME_month));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            tIME_node.setAttribute("day", Integer.toString(tIME_day));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            tIME_node.setAttribute("hour", Integer.toString(tIME_hour));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            tIME_node.setAttribute("minute", Integer.toString(tIME_minute));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            tIME_node.setAttribute("second", Integer.toString(tIME_second));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            root.appendChild(tIME_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        // tRNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (tRNS_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            IIOMetadataNode tRNS_node = new IIOMetadataNode("tRNS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            if (tRNS_colorType == PNGImageReader.PNG_COLOR_PALETTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                node = new IIOMetadataNode("tRNS_Palette");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                for (int i = 0; i < tRNS_alpha.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    IIOMetadataNode entry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                        new IIOMetadataNode("tRNS_PaletteEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    entry.setAttribute("index", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    entry.setAttribute("alpha",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                       Integer.toString(tRNS_alpha[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    node.appendChild(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            } else if (tRNS_colorType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                node = new IIOMetadataNode("tRNS_Grayscale");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                node.setAttribute("gray", Integer.toString(tRNS_gray));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            } else if (tRNS_colorType == PNGImageReader.PNG_COLOR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                node = new IIOMetadataNode("tRNS_RGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                node.setAttribute("red", Integer.toString(tRNS_red));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                node.setAttribute("green", Integer.toString(tRNS_green));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                node.setAttribute("blue", Integer.toString(tRNS_blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            tRNS_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            root.appendChild(tRNS_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // zTXt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        if (zTXt_keyword.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            IIOMetadataNode zTXt_parent = new IIOMetadataNode("zTXt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            for (int i = 0; i < zTXt_keyword.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                IIOMetadataNode zTXt_node = new IIOMetadataNode("zTXtEntry");
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   805
                zTXt_node.setAttribute("keyword", zTXt_keyword.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   807
                int cm = (zTXt_compressionMethod.get(i)).intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                zTXt_node.setAttribute("compressionMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                       zTXt_compressionMethodNames[cm]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   811
                zTXt_node.setAttribute("text", zTXt_text.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                zTXt_parent.appendChild(zTXt_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            root.appendChild(zTXt_parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // Unknown chunks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (unknownChunkType.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            IIOMetadataNode unknown_parent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                new IIOMetadataNode("UnknownChunks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            for (int i = 0; i < unknownChunkType.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                IIOMetadataNode unknown_node =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    new IIOMetadataNode("UnknownChunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                unknown_node.setAttribute("type",
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   827
                                          unknownChunkType.get(i));
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
   828
                unknown_node.setUserObject(unknownChunkData.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                unknown_parent.appendChild(unknown_node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            root.appendChild(unknown_parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    private int getNumChannels() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // Determine number of channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // Be careful about palette color with transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        int numChannels = IHDR_numChannels[IHDR_colorType];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            tRNS_present && tRNS_colorType == IHDR_colorType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            numChannels = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return numChannels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    public IIOMetadataNode getStandardChromaNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        IIOMetadataNode chroma_node = new IIOMetadataNode("Chroma");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        node = new IIOMetadataNode("ColorSpaceType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        node.setAttribute("name", colorSpaceTypeNames[IHDR_colorType]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        chroma_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        node = new IIOMetadataNode("NumChannels");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        node.setAttribute("value", Integer.toString(getNumChannels()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        chroma_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (gAMA_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            node = new IIOMetadataNode("Gamma");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            node.setAttribute("value", Float.toString(gAMA_gamma*1.0e-5F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            chroma_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        node = new IIOMetadataNode("BlackIsZero");
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
   869
        node.setAttribute("value", "TRUE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        chroma_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (PLTE_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            boolean hasAlpha = tRNS_present &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                (tRNS_colorType == PNGImageReader.PNG_COLOR_PALETTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            node = new IIOMetadataNode("Palette");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            for (int i = 0; i < PLTE_red.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                IIOMetadataNode entry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    new IIOMetadataNode("PaletteEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                entry.setAttribute("index", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                entry.setAttribute("red",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                   Integer.toString(PLTE_red[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                entry.setAttribute("green",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                   Integer.toString(PLTE_green[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                entry.setAttribute("blue",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                                   Integer.toString(PLTE_blue[i] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    int alpha = (i < tRNS_alpha.length) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                        (tRNS_alpha[i] & 0xff) : 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                    entry.setAttribute("alpha", Integer.toString(alpha));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                node.appendChild(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            chroma_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (bKGD_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if (bKGD_colorType == PNGImageReader.PNG_COLOR_PALETTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                node = new IIOMetadataNode("BackgroundIndex");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                node.setAttribute("value", Integer.toString(bKGD_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                node = new IIOMetadataNode("BackgroundColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                int r, g, b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                if (bKGD_colorType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    r = g = b = bKGD_gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    r = bKGD_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    g = bKGD_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    b = bKGD_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                node.setAttribute("red", Integer.toString(r));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                node.setAttribute("green", Integer.toString(g));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                node.setAttribute("blue", Integer.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            chroma_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        return chroma_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public IIOMetadataNode getStandardCompressionNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        IIOMetadataNode compression_node = new IIOMetadataNode("Compression");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        node = new IIOMetadataNode("CompressionTypeName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        node.setAttribute("value", "deflate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        compression_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        node = new IIOMetadataNode("Lossless");
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
   931
        node.setAttribute("value", "TRUE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        compression_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        node = new IIOMetadataNode("NumProgressiveScans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        node.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                          (IHDR_interlaceMethod == 0) ? "1" : "7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        compression_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        return compression_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    private String repeat(String s, int times) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (times == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22584
diff changeset
   946
        StringBuilder sb = new StringBuilder((s.length() + 1)*times - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        sb.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        for (int i = 1; i < times; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            sb.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            sb.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    public IIOMetadataNode getStandardDataNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        IIOMetadataNode data_node = new IIOMetadataNode("Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        node = new IIOMetadataNode("PlanarConfiguration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        node.setAttribute("value", "PixelInterleaved");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        data_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        node = new IIOMetadataNode("SampleFormat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        node.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                          IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                          "Index" : "UnsignedIntegral");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        data_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        String bitDepth = Integer.toString(IHDR_bitDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        node = new IIOMetadataNode("BitsPerSample");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        node.setAttribute("value", repeat(bitDepth, getNumChannels()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        data_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        if (sBIT_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            node = new IIOMetadataNode("SignificantBitsPerSample");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            String sbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                sbits = Integer.toString(sBIT_grayBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            } else { // sBIT_colorType == PNGImageReader.PNG_COLOR_RGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                     // sBIT_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                sbits = Integer.toString(sBIT_redBits) + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    Integer.toString(sBIT_greenBits) + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    Integer.toString(sBIT_blueBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                sBIT_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                sbits += " " + Integer.toString(sBIT_alphaBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            node.setAttribute("value", sbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            data_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // SampleMSB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        return data_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    public IIOMetadataNode getStandardDimensionNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        IIOMetadataNode dimension_node = new IIOMetadataNode("Dimension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        node = new IIOMetadataNode("PixelAspectRatio");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        float ratio = pHYs_present ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            (float)pHYs_pixelsPerUnitXAxis/pHYs_pixelsPerUnitYAxis : 1.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        node.setAttribute("value", Float.toString(ratio));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        dimension_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        node = new IIOMetadataNode("ImageOrientation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        node.setAttribute("value", "Normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        dimension_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        if (pHYs_present && pHYs_unitSpecifier == PHYS_UNIT_METER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            node = new IIOMetadataNode("HorizontalPixelSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            node.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                              Float.toString(1000.0F/pHYs_pixelsPerUnitXAxis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            dimension_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            node = new IIOMetadataNode("VerticalPixelSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            node.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                              Float.toString(1000.0F/pHYs_pixelsPerUnitYAxis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            dimension_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return dimension_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    public IIOMetadataNode getStandardDocumentNode() {
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1031
        IIOMetadataNode document_node = null;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1032
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1033
        // Check if image modification time exists
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1034
        if (tIME_present) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1035
            // Create new document node
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1036
            document_node = new IIOMetadataNode("Document");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1037
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1038
            // Node to hold image modification time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1039
            IIOMetadataNode node = new IIOMetadataNode("ImageModificationTime");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1040
            node.setAttribute("year", Integer.toString(tIME_year));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1041
            node.setAttribute("month", Integer.toString(tIME_month));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1042
            node.setAttribute("day", Integer.toString(tIME_day));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1043
            node.setAttribute("hour", Integer.toString(tIME_hour));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1044
            node.setAttribute("minute", Integer.toString(tIME_minute));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1045
            node.setAttribute("second", Integer.toString(tIME_second));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1046
            document_node.appendChild(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1049
        // Check if image creation time exists
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1050
        if (creation_time_present) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1051
            if (document_node == null) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1052
                // Create new document node
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1053
                document_node = new IIOMetadataNode("Document");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1054
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1056
            // Node to hold image creation time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1057
            IIOMetadataNode node = new IIOMetadataNode("ImageCreationTime");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1058
            node.setAttribute("year", Integer.toString(creation_time_year));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1059
            node.setAttribute("month", Integer.toString(creation_time_month));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1060
            node.setAttribute("day", Integer.toString(creation_time_day));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1061
            node.setAttribute("hour", Integer.toString(creation_time_hour));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1062
            node.setAttribute("minute", Integer.toString(creation_time_minute));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1063
            node.setAttribute("second", Integer.toString(creation_time_second));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1064
            document_node.appendChild(node);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1065
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        return document_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public IIOMetadataNode getStandardTextNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        int numEntries = tEXt_keyword.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            iTXt_keyword.size() + zTXt_keyword.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (numEntries == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        IIOMetadataNode text_node = new IIOMetadataNode("Text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        for (int i = 0; i < tEXt_keyword.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            node = new IIOMetadataNode("TextEntry");
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
  1082
            node.setAttribute("keyword", tEXt_keyword.get(i));
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
  1083
            node.setAttribute("value", tEXt_text.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            node.setAttribute("encoding", "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            node.setAttribute("compression", "none");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            text_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        for (int i = 0; i < iTXt_keyword.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            node = new IIOMetadataNode("TextEntry");
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  1092
            node.setAttribute("keyword", iTXt_keyword.get(i));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  1093
            node.setAttribute("value", iTXt_text.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            node.setAttribute("language",
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  1095
                              iTXt_languageTag.get(i));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  1096
            if (iTXt_compressionFlag.get(i)) {
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1097
                node.setAttribute("compression", "zip");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                node.setAttribute("compression", "none");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            text_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        for (int i = 0; i < zTXt_keyword.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            node = new IIOMetadataNode("TextEntry");
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
  1107
            node.setAttribute("keyword", zTXt_keyword.get(i));
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
  1108
            node.setAttribute("value", zTXt_text.get(i));
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1109
            node.setAttribute("compression", "zip");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            text_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        return text_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    public IIOMetadataNode getStandardTransparencyNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        IIOMetadataNode transparency_node =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            new IIOMetadataNode("Transparency");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        IIOMetadataNode node = null; // scratch node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        node = new IIOMetadataNode("Alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        boolean hasAlpha =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            (IHDR_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            (IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            (IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
             tRNS_present &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
             (tRNS_colorType == IHDR_colorType) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
             (tRNS_alpha != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        node.setAttribute("value", hasAlpha ? "nonpremultipled" : "none");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        transparency_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        if (tRNS_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            node = new IIOMetadataNode("TransparentColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (tRNS_colorType == PNGImageReader.PNG_COLOR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                node.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                                  Integer.toString(tRNS_red) + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                                  Integer.toString(tRNS_green) + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                                  Integer.toString(tRNS_blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            } else if (tRNS_colorType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                node.setAttribute("value", Integer.toString(tRNS_gray));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            transparency_node.appendChild(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        return transparency_node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    // Shorthand for throwing an IIOInvalidTreeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    private void fatal(Node node, String reason)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        throw new IIOInvalidTreeException(reason, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    // Get an integer-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    private String getStringAttribute(Node node, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                                      String defaultValue, boolean required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        Node attr = node.getAttributes().getNamedItem(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (!required) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                fatal(node, "Required attribute " + name + " not present!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        return attr.getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    // Get an integer-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    private int getIntAttribute(Node node, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                                int defaultValue, boolean required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        String value = getStringAttribute(node, name, null, required);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        return Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    // Get a float-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    private float getFloatAttribute(Node node, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                                    float defaultValue, boolean required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        String value = getStringAttribute(node, name, null, required);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        return Float.parseFloat(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    // Get a required integer-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    private int getIntAttribute(Node node, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        return getIntAttribute(node, name, -1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    // Get a required float-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    private float getFloatAttribute(Node node, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        return getFloatAttribute(node, name, -1.0F, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    // Get a boolean-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    private boolean getBooleanAttribute(Node node, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                        boolean defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                                        boolean required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        Node attr = node.getAttributes().getNamedItem(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            if (!required) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                fatal(node, "Required attribute " + name + " not present!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        String value = attr.getNodeValue();
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
  1219
        // Allow lower case booleans for backward compatibility, #5082756
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
  1220
        if (value.equals("TRUE") || value.equals("true")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            return true;
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
  1222
        } else if (value.equals("FALSE") || value.equals("false")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        } else {
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2368
diff changeset
  1225
            fatal(node, "Attribute " + name + " must be 'TRUE' or 'FALSE'!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    // Get a required boolean-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    private boolean getBooleanAttribute(Node node, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        return getBooleanAttribute(node, name, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    // Get an enumerated attribute as an index into a String array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    private int getEnumeratedAttribute(Node node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                                       String name, String[] legalNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                                       int defaultValue, boolean required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        Node attr = node.getAttributes().getNamedItem(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            if (!required) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                fatal(node, "Required attribute " + name + " not present!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        String value = attr.getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        for (int i = 0; i < legalNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if (value.equals(legalNames[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        fatal(node, "Illegal value for attribute " + name + "!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    // Get a required enumerated attribute as an index into a String array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    private int getEnumeratedAttribute(Node node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                                       String name, String[] legalNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        return getEnumeratedAttribute(node, name, legalNames, -1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    // Get a String-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    private String getAttribute(Node node, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                                String defaultValue, boolean required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        Node attr = node.getAttributes().getNamedItem(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            if (!required) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                fatal(node, "Required attribute " + name + " not present!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        return attr.getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    // Get a required String-valued attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    private String getAttribute(Node node, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            return getAttribute(node, name, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    public void mergeTree(String formatName, Node root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        if (formatName.equals(nativeMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                throw new IllegalArgumentException("root == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            mergeNativeTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        } else if (formatName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                   (IIOMetadataFormatImpl.standardMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                throw new IllegalArgumentException("root == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            mergeStandardTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            throw new IllegalArgumentException("Not a recognized format!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    private void mergeNativeTree(Node root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        Node node = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        if (!node.getNodeName().equals(nativeMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            fatal(node, "Root must be " + nativeMetadataFormatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        while (node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            String name = node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            if (name.equals("IHDR")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                IHDR_width = getIntAttribute(node, "width");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                IHDR_height = getIntAttribute(node, "height");
25766
f979c6d18e22 4991647: PNGMetadata.getAsTree() sets bitDepth to invalid value
mcherkas
parents: 24969
diff changeset
  1320
                IHDR_bitDepth =
f979c6d18e22 4991647: PNGMetadata.getAsTree() sets bitDepth to invalid value
mcherkas
parents: 24969
diff changeset
  1321
                        Integer.valueOf(IHDR_bitDepths[
f979c6d18e22 4991647: PNGMetadata.getAsTree() sets bitDepth to invalid value
mcherkas
parents: 24969
diff changeset
  1322
                                getEnumeratedAttribute(node,
f979c6d18e22 4991647: PNGMetadata.getAsTree() sets bitDepth to invalid value
mcherkas
parents: 24969
diff changeset
  1323
                                                    "bitDepth",
f979c6d18e22 4991647: PNGMetadata.getAsTree() sets bitDepth to invalid value
mcherkas
parents: 24969
diff changeset
  1324
                                                    IHDR_bitDepths)]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                IHDR_colorType = getEnumeratedAttribute(node, "colorType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                                                        IHDR_colorTypeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                IHDR_compressionMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    getEnumeratedAttribute(node, "compressionMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                                           IHDR_compressionMethodNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                IHDR_filterMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    getEnumeratedAttribute(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                           "filterMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                                           IHDR_filterMethodNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                IHDR_interlaceMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    getEnumeratedAttribute(node, "interlaceMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                                           IHDR_interlaceMethodNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                IHDR_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            } else if (name.equals("PLTE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                byte[] red = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                byte[] green  = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                byte[] blue = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                int maxindex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                Node PLTE_entry = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                if (PLTE_entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    fatal(node, "Palette has no entries!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                while (PLTE_entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    if (!PLTE_entry.getNodeName().equals("PLTEEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                              "Only a PLTEEntry may be a child of a PLTE!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                    int index = getIntAttribute(PLTE_entry, "index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    if (index < 0 || index > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                              "Bad value for PLTEEntry attribute index!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    if (index > maxindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                        maxindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    red[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        (byte)getIntAttribute(PLTE_entry, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    green[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        (byte)getIntAttribute(PLTE_entry, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    blue[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                        (byte)getIntAttribute(PLTE_entry, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    PLTE_entry = PLTE_entry.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                int numEntries = maxindex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                PLTE_red = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                PLTE_green = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                PLTE_blue = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                System.arraycopy(red, 0, PLTE_red, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                System.arraycopy(green, 0, PLTE_green, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                System.arraycopy(blue, 0, PLTE_blue, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                PLTE_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            } else if (name.equals("bKGD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                bKGD_present = false; // Guard against partial overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                Node bKGD_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                if (bKGD_node == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    fatal(node, "bKGD node has no children!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                String bKGD_name = bKGD_node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                if (bKGD_name.equals("bKGD_Palette")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    bKGD_index = getIntAttribute(bKGD_node, "index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    bKGD_colorType = PNGImageReader.PNG_COLOR_PALETTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                } else if (bKGD_name.equals("bKGD_Grayscale")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    bKGD_gray = getIntAttribute(bKGD_node, "gray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                    bKGD_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                } else if (bKGD_name.equals("bKGD_RGB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                    bKGD_red = getIntAttribute(bKGD_node, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    bKGD_green = getIntAttribute(bKGD_node, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                    bKGD_blue = getIntAttribute(bKGD_node, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    bKGD_colorType = PNGImageReader.PNG_COLOR_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                    fatal(node, "Bad child of a bKGD node!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                if (bKGD_node.getNextSibling() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    fatal(node, "bKGD node has more than one child!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                bKGD_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            } else if (name.equals("cHRM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                cHRM_whitePointX = getIntAttribute(node, "whitePointX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                cHRM_whitePointY = getIntAttribute(node, "whitePointY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                cHRM_redX = getIntAttribute(node, "redX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                cHRM_redY = getIntAttribute(node, "redY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                cHRM_greenX = getIntAttribute(node, "greenX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                cHRM_greenY = getIntAttribute(node, "greenY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                cHRM_blueX = getIntAttribute(node, "blueX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                cHRM_blueY = getIntAttribute(node, "blueY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                cHRM_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            } else if (name.equals("gAMA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                gAMA_gamma = getIntAttribute(node, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                gAMA_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            } else if (name.equals("hIST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                char[] hist = new char[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                int maxindex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                Node hIST_entry = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                if (hIST_entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                    fatal(node, "hIST node has no children!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                while (hIST_entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                    if (!hIST_entry.getNodeName().equals("hISTEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                              "Only a hISTEntry may be a child of a hIST!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    int index = getIntAttribute(hIST_entry, "index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    if (index < 0 || index > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                              "Bad value for histEntry attribute index!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    if (index > maxindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                        maxindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                    hist[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                        (char)getIntAttribute(hIST_entry, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    hIST_entry = hIST_entry.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                int numEntries = maxindex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                hIST_histogram = new char[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                System.arraycopy(hist, 0, hIST_histogram, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                hIST_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            } else if (name.equals("iCCP")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                iCCP_profileName = getAttribute(node, "profileName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                iCCP_compressionMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    getEnumeratedAttribute(node, "compressionMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                                           iCCP_compressionMethodNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                Object compressedProfile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    ((IIOMetadataNode)node).getUserObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                if (compressedProfile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                    fatal(node, "No ICCP profile present in user object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                if (!(compressedProfile instanceof byte[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    fatal(node, "User object not a byte array!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20415
diff changeset
  1469
                iCCP_compressedProfile = ((byte[])compressedProfile).clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                iCCP_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            } else if (name.equals("iTXt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                Node iTXt_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                while (iTXt_node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                    if (!iTXt_node.getNodeName().equals("iTXtEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                              "Only an iTXtEntry may be a child of an iTXt!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    String keyword = getAttribute(iTXt_node, "keyword");
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1481
                    if (isValidKeyword(keyword)) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1482
                        iTXt_keyword.add(keyword);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1484
                        boolean compressionFlag =
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1485
                            getBooleanAttribute(iTXt_node, "compressionFlag");
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1486
                        iTXt_compressionFlag.add(Boolean.valueOf(compressionFlag));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1488
                        String compressionMethod =
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1489
                            getAttribute(iTXt_node, "compressionMethod");
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1490
                        iTXt_compressionMethod.add(Integer.valueOf(compressionMethod));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1492
                        String languageTag =
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1493
                            getAttribute(iTXt_node, "languageTag");
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1494
                        iTXt_languageTag.add(languageTag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1496
                        String translatedKeyword =
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1497
                            getAttribute(iTXt_node, "translatedKeyword");
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1498
                        iTXt_translatedKeyword.add(translatedKeyword);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1500
                        String text = getAttribute(iTXt_node, "text");
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1501
                        iTXt_text.add(text);
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1502
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1503
                        // Check if the text chunk contains image creation time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1504
                        if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1505
                            // Update Standard/Document/ImageCreationTime
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1506
                            int index = iTXt_text.size()-1;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1507
                            decodeImageCreationTimeFromTextChunk(
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1508
                                    iTXt_text.listIterator(index));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1509
                        }
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1510
                    }
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1511
                    // silently skip invalid text entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    iTXt_node = iTXt_node.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            } else if (name.equals("pHYs")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                pHYs_pixelsPerUnitXAxis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                    getIntAttribute(node, "pixelsPerUnitXAxis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                pHYs_pixelsPerUnitYAxis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                    getIntAttribute(node, "pixelsPerUnitYAxis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                pHYs_unitSpecifier =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                    getEnumeratedAttribute(node, "unitSpecifier",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                                           unitSpecifierNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                pHYs_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            } else if (name.equals("sBIT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                sBIT_present = false; // Guard against partial overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                Node sBIT_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                if (sBIT_node == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    fatal(node, "sBIT node has no children!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                String sBIT_name = sBIT_node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                if (sBIT_name.equals("sBIT_Grayscale")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                    sBIT_grayBits = getIntAttribute(sBIT_node, "gray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    sBIT_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                } else if (sBIT_name.equals("sBIT_GrayAlpha")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    sBIT_grayBits = getIntAttribute(sBIT_node, "gray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    sBIT_alphaBits = getIntAttribute(sBIT_node, "alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    sBIT_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                } else if (sBIT_name.equals("sBIT_RGB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    sBIT_redBits = getIntAttribute(sBIT_node, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    sBIT_greenBits = getIntAttribute(sBIT_node, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    sBIT_blueBits = getIntAttribute(sBIT_node, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    sBIT_colorType = PNGImageReader.PNG_COLOR_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                } else if (sBIT_name.equals("sBIT_RGBAlpha")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    sBIT_redBits = getIntAttribute(sBIT_node, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    sBIT_greenBits = getIntAttribute(sBIT_node, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    sBIT_blueBits = getIntAttribute(sBIT_node, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    sBIT_alphaBits = getIntAttribute(sBIT_node, "alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    sBIT_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                } else if (sBIT_name.equals("sBIT_Palette")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    sBIT_redBits = getIntAttribute(sBIT_node, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    sBIT_greenBits = getIntAttribute(sBIT_node, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    sBIT_blueBits = getIntAttribute(sBIT_node, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    sBIT_colorType = PNGImageReader.PNG_COLOR_PALETTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    fatal(node, "Bad child of an sBIT node!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                if (sBIT_node.getNextSibling() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                    fatal(node, "sBIT node has more than one child!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                sBIT_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            } else if (name.equals("sPLT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                sPLT_paletteName = getAttribute(node, "name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                sPLT_sampleDepth = getIntAttribute(node, "sampleDepth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                int[] red = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                int[] green  = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                int[] blue = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                int[] alpha = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                int[] frequency = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                int maxindex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                Node sPLT_entry = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                if (sPLT_entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    fatal(node, "sPLT node has no children!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                while (sPLT_entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                    if (!sPLT_entry.getNodeName().equals("sPLTEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                              "Only an sPLTEntry may be a child of an sPLT!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    int index = getIntAttribute(sPLT_entry, "index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    if (index < 0 || index > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                              "Bad value for PLTEEntry attribute index!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                    if (index > maxindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                        maxindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                    red[index] = getIntAttribute(sPLT_entry, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                    green[index] = getIntAttribute(sPLT_entry, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                    blue[index] = getIntAttribute(sPLT_entry, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                    alpha[index] = getIntAttribute(sPLT_entry, "alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                    frequency[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                        getIntAttribute(sPLT_entry, "frequency");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                    sPLT_entry = sPLT_entry.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                int numEntries = maxindex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                sPLT_red = new int[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                sPLT_green = new int[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                sPLT_blue = new int[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                sPLT_alpha = new int[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                sPLT_frequency = new int[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                System.arraycopy(red, 0, sPLT_red, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                System.arraycopy(green, 0, sPLT_green, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                System.arraycopy(blue, 0, sPLT_blue, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                System.arraycopy(alpha, 0, sPLT_alpha, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                System.arraycopy(frequency, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                                 sPLT_frequency, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                sPLT_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            } else if (name.equals("sRGB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                sRGB_renderingIntent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    getEnumeratedAttribute(node, "renderingIntent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                                           renderingIntentNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                sRGB_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            } else if (name.equals("tEXt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                Node tEXt_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                while (tEXt_node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    if (!tEXt_node.getNodeName().equals("tEXtEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                              "Only an tEXtEntry may be a child of an tEXt!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                    String keyword = getAttribute(tEXt_node, "keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                    tEXt_keyword.add(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                    String text = getAttribute(tEXt_node, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    tEXt_text.add(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1637
                    // Check if the text chunk contains image creation time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1638
                    if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1639
                        // Update Standard/Document/ImageCreationTime
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1640
                        int index = tEXt_text.size()-1;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1641
                        decodeImageCreationTimeFromTextChunk(
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1642
                                tEXt_text.listIterator(index));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1643
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    tEXt_node = tEXt_node.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            } else if (name.equals("tIME")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                tIME_year = getIntAttribute(node, "year");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                tIME_month = getIntAttribute(node, "month");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                tIME_day = getIntAttribute(node, "day");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                tIME_hour = getIntAttribute(node, "hour");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                tIME_minute = getIntAttribute(node, "minute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                tIME_second = getIntAttribute(node, "second");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                tIME_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            } else if (name.equals("tRNS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                tRNS_present = false; // Guard against partial overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                Node tRNS_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                if (tRNS_node == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    fatal(node, "tRNS node has no children!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                String tRNS_name = tRNS_node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                if (tRNS_name.equals("tRNS_Palette")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    byte[] alpha = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    int maxindex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    Node tRNS_paletteEntry = tRNS_node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                    if (tRNS_paletteEntry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                        fatal(node, "tRNS_Palette node has no children!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    while (tRNS_paletteEntry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                        if (!tRNS_paletteEntry.getNodeName().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                                                        "tRNS_PaletteEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                            fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                 "Only a tRNS_PaletteEntry may be a child of a tRNS_Palette!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                        int index =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                            getIntAttribute(tRNS_paletteEntry, "index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                        if (index < 0 || index > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                            fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                           "Bad value for tRNS_PaletteEntry attribute index!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                        if (index > maxindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                            maxindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                        alpha[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                            (byte)getIntAttribute(tRNS_paletteEntry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                                                  "alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                        tRNS_paletteEntry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                            tRNS_paletteEntry.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    int numEntries = maxindex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                    tRNS_alpha = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                    tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                    System.arraycopy(alpha, 0, tRNS_alpha, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                } else if (tRNS_name.equals("tRNS_Grayscale")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                    tRNS_gray = getIntAttribute(tRNS_node, "gray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                    tRNS_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                } else if (tRNS_name.equals("tRNS_RGB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    tRNS_red = getIntAttribute(tRNS_node, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    tRNS_green = getIntAttribute(tRNS_node, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    tRNS_blue = getIntAttribute(tRNS_node, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    tRNS_colorType = PNGImageReader.PNG_COLOR_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                    fatal(node, "Bad child of a tRNS node!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                if (tRNS_node.getNextSibling() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    fatal(node, "tRNS node has more than one child!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                tRNS_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            } else if (name.equals("zTXt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                Node zTXt_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                while (zTXt_node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    if (!zTXt_node.getNodeName().equals("zTXtEntry")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                              "Only an zTXtEntry may be a child of an zTXt!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                    String keyword = getAttribute(zTXt_node, "keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    zTXt_keyword.add(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                    int compressionMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                        getEnumeratedAttribute(zTXt_node, "compressionMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                                               zTXt_compressionMethodNames);
25777
bb88947b6766 8049893: Replace uses of 'new Integer()' with appropriate alternative across client classes
prr
parents: 25766
diff changeset
  1727
                    zTXt_compressionMethod.add(compressionMethod);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    String text = getAttribute(zTXt_node, "text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    zTXt_text.add(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1732
                    // Check if the text chunk contains image creation time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1733
                    if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1734
                        // Update Standard/Document/ImageCreationTime
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1735
                        int index = zTXt_text.size()-1;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1736
                        decodeImageCreationTimeFromTextChunk(
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1737
                                zTXt_text.listIterator(index));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  1738
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                    zTXt_node = zTXt_node.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            } else if (name.equals("UnknownChunks")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                Node unknown_node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                while (unknown_node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                    if (!unknown_node.getNodeName().equals("UnknownChunk")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                        fatal(node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                   "Only an UnknownChunk may be a child of an UnknownChunks!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                    String chunkType = getAttribute(unknown_node, "type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                    Object chunkData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                        ((IIOMetadataNode)unknown_node).getUserObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                    if (chunkType.length() != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                        fatal(unknown_node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                              "Chunk type must be 4 characters!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                    if (chunkData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                        fatal(unknown_node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                              "No chunk data present in user object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    if (!(chunkData instanceof byte[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                        fatal(unknown_node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                              "User object not a byte array!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                    unknownChunkType.add(chunkType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                    unknownChunkData.add(((byte[])chunkData).clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                    unknown_node = unknown_node.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                fatal(node, "Unknown child of root node!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            node = node.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1777
    /*
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1778
     * Accrding to PNG spec, keywords are restricted to 1 to 79 bytes
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1779
     * in length. Keywords shall contain only printable Latin-1 characters
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1780
     * and spaces; To reduce the chances for human misreading of a keyword,
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1781
     * leading spaces, trailing spaces, and consecutive spaces are not
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1782
     * permitted in keywords.
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1783
     *
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1784
     * See: http://www.w3.org/TR/PNG/#11keywords
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1785
     */
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1786
    private boolean isValidKeyword(String s) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1787
        int len = s.length();
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1788
        if (len < 1 || len >= 80) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1789
            return false;
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1790
        }
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1791
        if (s.startsWith(" ") || s.endsWith(" ") || s.contains("  ")) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1792
            return false;
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1793
        }
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1794
        return isISOLatin(s, false);
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1795
    }
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1796
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1797
    /*
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1798
     * According to PNG spec, keyword shall contain only printable
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1799
     * Latin-1 [ISO-8859-1] characters and spaces; that is, only
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1800
     * character codes 32-126 and 161-255 decimal are allowed.
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1801
     * For Latin-1 value fields the 0x10 (linefeed) control
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1802
     * character is aloowed too.
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1803
     *
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1804
     * See: http://www.w3.org/TR/PNG/#11keywords
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1805
     */
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1806
    private boolean isISOLatin(String s, boolean isLineFeedAllowed) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        for (int i = 0; i < len; i++) {
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1809
            char c = s.charAt(i);
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1810
            if (c < 32 || c > 255 || (c > 126 && c < 161)) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1811
                // not printable. Check whether this is an allowed
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1812
                // control char
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1813
                if (!isLineFeedAllowed || c != 0x10) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1814
                    return false;
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  1815
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    private void mergeStandardTree(Node root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        Node node = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        if (!node.getNodeName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            .equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            fatal(node, "Root must be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                  IIOMetadataFormatImpl.standardMetadataFormatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        node = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        while (node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            String name = node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            if (name.equals("Chroma")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                    String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    if (childName.equals("Gamma")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                        float gamma = getFloatAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                        gAMA_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                        gAMA_gamma = (int)(gamma*100000 + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                    } else if (childName.equals("Palette")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                        byte[] red = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                        byte[] green = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                        byte[] blue = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                        int maxindex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                        Node entry = child.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                        while (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                            int index = getIntAttribute(entry, "index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                            if (index >= 0 && index <= 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                                red[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                                    (byte)getIntAttribute(entry, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                                green[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                                    (byte)getIntAttribute(entry, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                                blue[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                                    (byte)getIntAttribute(entry, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                                if (index > maxindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                                    maxindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                            entry = entry.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                        int numEntries = maxindex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                        PLTE_red = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                        PLTE_green = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                        PLTE_blue = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                        System.arraycopy(red, 0, PLTE_red, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                        System.arraycopy(green, 0, PLTE_green, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                        System.arraycopy(blue, 0, PLTE_blue, 0, numEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                        PLTE_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    } else if (childName.equals("BackgroundIndex")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                        bKGD_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                        bKGD_colorType = PNGImageReader.PNG_COLOR_PALETTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                        bKGD_index = getIntAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                    } else if (childName.equals("BackgroundColor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                        int red = getIntAttribute(child, "red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                        int green = getIntAttribute(child, "green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                        int blue = getIntAttribute(child, "blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                        if (red == green && red == blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                            bKGD_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                            bKGD_gray = red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                            bKGD_red = red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                            bKGD_green = green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                            bKGD_blue = blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                        bKGD_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
//                  } else if (childName.equals("ColorSpaceType")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
//                  } else if (childName.equals("NumChannels")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                    child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            } else if (name.equals("Compression")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                    String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                    if (childName.equals("NumProgressiveScans")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                        // Use Adam7 if NumProgressiveScans > 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                        int scans = getIntAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                        IHDR_interlaceMethod = (scans > 1) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
//                  } else if (childName.equals("CompressionTypeName")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
//                  } else if (childName.equals("Lossless")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
//                  } else if (childName.equals("BitRate")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                    child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            } else if (name.equals("Data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                    String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                    if (childName.equals("BitsPerSample")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                        String s = getAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                        StringTokenizer t = new StringTokenizer(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                        int maxBits = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                        while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                            int bits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                            if (bits > maxBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                                maxBits = bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                        if (maxBits < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                            maxBits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                        if (maxBits == 3) maxBits = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                        if (maxBits > 4 || maxBits < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                            maxBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                        if (maxBits > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                            maxBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                        IHDR_bitDepth = maxBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                    } else if (childName.equals("SignificantBitsPerSample")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                        String s = getAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                        StringTokenizer t = new StringTokenizer(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                        int numTokens = t.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                        if (numTokens == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                            sBIT_colorType = PNGImageReader.PNG_COLOR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                            sBIT_grayBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                        } else if (numTokens == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                            sBIT_colorType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                              PNGImageReader.PNG_COLOR_GRAY_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                            sBIT_grayBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                            sBIT_alphaBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                        } else if (numTokens == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                            sBIT_colorType = PNGImageReader.PNG_COLOR_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                            sBIT_redBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                            sBIT_greenBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                            sBIT_blueBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                        } else if (numTokens == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                            sBIT_colorType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                              PNGImageReader.PNG_COLOR_RGB_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                            sBIT_redBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                            sBIT_greenBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                            sBIT_blueBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                            sBIT_alphaBits = Integer.parseInt(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                        if (numTokens >= 1 && numTokens <= 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                            sBIT_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
//                      } else if (childName.equals("PlanarConfiguration")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
//                      } else if (childName.equals("SampleFormat")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
//                      } else if (childName.equals("SampleMSB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                    child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            } else if (name.equals("Dimension")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                boolean gotWidth = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                boolean gotHeight = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                boolean gotAspectRatio = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                float width = -1.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                float height = -1.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                float aspectRatio = -1.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    if (childName.equals("PixelAspectRatio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                        aspectRatio = getFloatAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                        gotAspectRatio = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    } else if (childName.equals("HorizontalPixelSize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                        width = getFloatAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                        gotWidth = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    } else if (childName.equals("VerticalPixelSize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                        height = getFloatAttribute(child, "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                        gotHeight = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
//                  } else if (childName.equals("ImageOrientation")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
//                  } else if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
//                      (childName.equals("HorizontalPhysicalPixelSpacing")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
//                  } else if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
//                      (childName.equals("VerticalPhysicalPixelSpacing")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
//                  } else if (childName.equals("HorizontalPosition")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
//                  } else if (childName.equals("VerticalPosition")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
//                  } else if (childName.equals("HorizontalPixelOffset")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
//                  } else if (childName.equals("VerticalPixelOffset")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                    child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                if (gotWidth && gotHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                    pHYs_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    pHYs_unitSpecifier = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                    pHYs_pixelsPerUnitXAxis = (int)(width*1000 + 0.5F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    pHYs_pixelsPerUnitYAxis = (int)(height*1000 + 0.5F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                } else if (gotAspectRatio) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                    pHYs_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    pHYs_unitSpecifier = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    // Find a reasonable rational approximation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                    int denom = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                    for (; denom < 100; denom++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                        int num = (int)(aspectRatio*denom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        if (Math.abs(num/denom - aspectRatio) < 0.001) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    pHYs_pixelsPerUnitXAxis = (int)(aspectRatio*denom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    pHYs_pixelsPerUnitYAxis = denom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            } else if (name.equals("Document")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                    String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    if (childName.equals("ImageModificationTime")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                        tIME_present = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                        tIME_year = getIntAttribute(child, "year");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                        tIME_month = getIntAttribute(child, "month");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                        tIME_day = getIntAttribute(child, "day");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                        tIME_hour =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                            getIntAttribute(child, "hour", 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                        tIME_minute =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                            getIntAttribute(child, "minute", 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                        tIME_second =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                            getIntAttribute(child, "second", 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
//                  } else if (childName.equals("SubimageInterpretation")) {
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2039
                    } else if (childName.equals("ImageCreationTime")) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2040
                        // Extract the creation time values
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2041
                        int year  = getIntAttribute(child, "year");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2042
                        int month = getIntAttribute(child, "month");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2043
                        int day   = getIntAttribute(child, "day");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2044
                        int hour  = getIntAttribute(child, "hour", 0, false);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2045
                        int mins  = getIntAttribute(child, "minute", 0, false);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2046
                        int sec   = getIntAttribute(child, "second", 0, false);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2047
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2048
                        /*
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2049
                         * Update Standard/Document/ImageCreationTime and encode
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2050
                         * the same in the last decoded text chunk with creation
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2051
                         * time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2052
                         */
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2053
                        initImageCreationTime(year, month, day, hour, mins, sec);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2054
                        encodeImageCreationTimeToTextChunk();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                    child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            } else if (name.equals("Text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                    String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    if (childName.equals("TextEntry")) {
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2063
                        String keyword =
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2064
                            getAttribute(child, "keyword", "", false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                        String value = getAttribute(child, "value");
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2066
                        String language =
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2067
                            getAttribute(child, "language", "", false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                        String compression =
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2069
                            getAttribute(child, "compression", "none", false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2071
                        if (!isValidKeyword(keyword)) {
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2072
                            // Just ignore this node, PNG requires keywords
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2073
                        } else if (isISOLatin(value, true)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                            if (compression.equals("zip")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                                // Use a zTXt node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                                zTXt_keyword.add(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                                zTXt_text.add(value);
2368
5a872c1edd4f 5106550: PNG writer merge standard metadata fails for TextEntry sans #IMPLIED attributes
bae
parents: 1734
diff changeset
  2078
                                zTXt_compressionMethod.add(Integer.valueOf(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                                // Use a tEXt node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                                tEXt_keyword.add(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                                tEXt_text.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                            // Use an iTXt node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                            iTXt_keyword.add(keyword);
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2087
                            iTXt_compressionFlag.add(Boolean.valueOf(compression.equals("zip")));
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2088
                            iTXt_compressionMethod.add(Integer.valueOf(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                            iTXt_languageTag.add(language);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                            iTXt_translatedKeyword.add(keyword); // fake it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                            iTXt_text.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                    child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
//          } else if (name.equals("Transparency")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
//              Node child = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
//              while (child != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
//                  String childName = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
//                  if (childName.equals("Alpha")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
//                  } else if (childName.equals("TransparentIndex")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
//                  } else if (childName.equals("TransparentColor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
//                  } else if (childName.equals("TileTransparencies")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
//                  } else if (childName.equals("TileOpacities")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
//                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
//                  child = child.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
//              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
//          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
//              // fatal(node, "Unknown child of root node!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            node = node.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2116
    void initImageCreationTime(OffsetDateTime offsetDateTime) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2117
        // Check for incoming arguments
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2118
        if (offsetDateTime != null) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2119
            // set values that make up Standard/Document/ImageCreationTime
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2120
            creation_time_present = true;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2121
            creation_time_year    = offsetDateTime.getYear();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2122
            creation_time_month   = offsetDateTime.getMonthValue();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2123
            creation_time_day     = offsetDateTime.getDayOfMonth();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2124
            creation_time_hour    = offsetDateTime.getHour();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2125
            creation_time_minute  = offsetDateTime.getMinute();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2126
            creation_time_second  = offsetDateTime.getSecond();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2127
            creation_time_offset  = offsetDateTime.getOffset();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2128
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2129
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2130
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2131
    void initImageCreationTime(int year, int month, int day,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2132
            int hour, int min,int second) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2133
        /*
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2134
         * Though LocalDateTime suffices the need to store Standard/Document/
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2135
         * ImageCreationTime, we require the zone offset to encode the same
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2136
         * in the text chunk based on RFC1123 format.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2137
         */
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2138
        LocalDateTime locDT = LocalDateTime.of(year, month, day, hour, min, second);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2139
        ZoneOffset offset = ZoneId.systemDefault()
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2140
                                  .getRules()
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2141
                                  .getOffset(locDT);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2142
        OffsetDateTime offDateTime = OffsetDateTime.of(locDT,offset);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2143
        initImageCreationTime(offDateTime);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2144
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2145
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2146
    void decodeImageCreationTimeFromTextChunk(ListIterator<String> iterChunk) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2147
        // Check for incoming arguments
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2148
        if (iterChunk != null && iterChunk.hasNext()) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2149
            /*
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2150
             * Save the iterator to mark the last decoded text chunk with
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2151
             * creation time. The contents of this chunk will be updated when
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2152
             * user provides creation time by merging a standard tree with
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2153
             * Standard/Document/ImageCreationTime.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2154
             */
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2155
            setCreationTimeChunk(iterChunk);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2156
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2157
            // Parse encoded time and set Standard/Document/ImageCreationTime.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2158
            String encodedTime = getEncodedTime();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2159
            initImageCreationTime(parseEncodedTime(encodedTime));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2160
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2161
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2162
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2163
    void encodeImageCreationTimeToTextChunk() {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2164
        // Check if Standard/Document/ImageCreationTime exists.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2165
        if (creation_time_present) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2166
            // Check if a text chunk with creation time exists.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2167
            if (tEXt_creation_time_present == false) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2168
                // No text chunk exists with image creation time. Add an entry.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2169
                this.tEXt_keyword.add(tEXt_creationTimeKey);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2170
                this.tEXt_text.add("Creation Time Place Holder");
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2171
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2172
                // Update the iterator
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2173
                int index = tEXt_text.size() - 1;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2174
                setCreationTimeChunk(tEXt_text.listIterator(index));
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2175
            }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2176
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2177
            // Encode image creation time with RFC1123 formatter
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2178
            OffsetDateTime offDateTime = OffsetDateTime.of(creation_time_year,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2179
                    creation_time_month, creation_time_day,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2180
                    creation_time_hour, creation_time_minute,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2181
                    creation_time_second, 0, creation_time_offset);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2182
            DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2183
            String encodedTime = offDateTime.format(formatter);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2184
            setEncodedTime(encodedTime);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2185
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2186
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2187
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2188
    private void setCreationTimeChunk(ListIterator<String> iter) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2189
        // Check for iterator's valid state
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2190
        if (iter != null && iter.hasNext()) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2191
            tEXt_creation_time_iter = iter;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2192
            tEXt_creation_time_present = true;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2193
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2194
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2195
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2196
    private void setEncodedTime(String encodedTime) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2197
        if (tEXt_creation_time_iter != null
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2198
                && tEXt_creation_time_iter.hasNext()
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2199
                && encodedTime != null) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2200
            // Set the value at the iterator and reset its state
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2201
            tEXt_creation_time_iter.next();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2202
            tEXt_creation_time_iter.set(encodedTime);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2203
            tEXt_creation_time_iter.previous();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2204
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2205
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2206
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2207
    private String getEncodedTime() {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2208
        String encodedTime = null;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2209
        if (tEXt_creation_time_iter != null
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2210
                && tEXt_creation_time_iter.hasNext()) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2211
            // Get the value at iterator and reset its state
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2212
            encodedTime = tEXt_creation_time_iter.next();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2213
            tEXt_creation_time_iter.previous();
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2214
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2215
        return encodedTime;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2216
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2217
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2218
    private OffsetDateTime parseEncodedTime(String encodedTime) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2219
        OffsetDateTime retVal = null;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2220
        boolean timeDecoded = false;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2221
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2222
        /*
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2223
         * PNG specification recommends that image encoders use RFC1123 format
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2224
         * to represent time in String but doesn't mandate. Encoders could
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2225
         * use any convenient format. Hence, we extract time provided the
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2226
         * encoded time complies with either RFC1123 or ISO standards.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2227
         */
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2228
        try {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2229
            // Check if the encoded time complies with RFC1123
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2230
            retVal = OffsetDateTime.parse(encodedTime,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2231
                                          DateTimeFormatter.RFC_1123_DATE_TIME);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2232
            timeDecoded = true;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2233
        } catch (DateTimeParseException exception) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2234
            // No Op. Encoded time did not comply with RFC1123 standard.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2235
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2236
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2237
        if (timeDecoded == false) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2238
            try {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2239
                // Check if the encoded time complies with ISO standard.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2240
                DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2241
                TemporalAccessor dt = formatter.parseBest(encodedTime,
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2242
                        OffsetDateTime::from, LocalDateTime::from);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2243
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2244
                if (dt instanceof OffsetDateTime) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2245
                    // Encoded time contains date time and zone offset
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2246
                    retVal = (OffsetDateTime) dt;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2247
                } else if (dt instanceof LocalDateTime) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2248
                    /*
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2249
                     * Encoded time contains only date and time. Since zone
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2250
                     * offset information isn't available, we set to the default
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2251
                     */
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2252
                    LocalDateTime locDT = (LocalDateTime) dt;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2253
                    retVal = OffsetDateTime.of(locDT, ZoneOffset.UTC);
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2254
                }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2255
            }  catch (DateTimeParseException exception) {
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2256
                // No Op. Encoded time did not comply with ISO standard.
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2257
            }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2258
        }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2259
        return retVal;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2260
    }
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2261
49995
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
  2262
    boolean hasTransparentColor() {
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
  2263
        return tRNS_present &&
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
  2264
               (tRNS_colorType == PNGImageReader.PNG_COLOR_RGB ||
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
  2265
               tRNS_colorType == PNGImageReader.PNG_COLOR_GRAY);
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
  2266
    }
6f595ec05539 6788458: PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images
jdv
parents: 47216
diff changeset
  2267
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
    // Reset all instance variables to their initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        IHDR_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        PLTE_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        bKGD_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        cHRM_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        gAMA_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        hIST_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        iCCP_present = false;
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2277
        iTXt_keyword = new ArrayList<String>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2278
        iTXt_compressionFlag = new ArrayList<Boolean>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2279
        iTXt_compressionMethod = new ArrayList<Integer>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2280
        iTXt_languageTag = new ArrayList<String>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2281
        iTXt_translatedKeyword = new ArrayList<String>();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 2
diff changeset
  2282
        iTXt_text = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        pHYs_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        sBIT_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        sPLT_present = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        sRGB_present = false;
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2287
        tEXt_keyword = new ArrayList<String>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2288
        tEXt_text = new ArrayList<String>();
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2289
        // tIME chunk with Image modification time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        tIME_present = false;
47197
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2291
        // Text chunk with Image creation time
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2292
        tEXt_creation_time_present = false;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2293
        tEXt_creation_time_iter = null;
a7033867ee20 8164971: PNG metadata does not handle ImageCreationTime
pnarayanan
parents: 35667
diff changeset
  2294
        creation_time_present = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        tRNS_present = false;
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2296
        zTXt_keyword = new ArrayList<String>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2297
        zTXt_compressionMethod = new ArrayList<Integer>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2298
        zTXt_text = new ArrayList<String>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2299
        unknownChunkType = new ArrayList<String>();
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 2375
diff changeset
  2300
        unknownChunkData = new ArrayList<byte[]>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
}