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