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