jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEGMetadata.java
author darcy
Tue, 28 Jan 2014 09:42:05 -0800
changeset 22584 eed64ee05369
parent 20112 a1c89924203c
child 23312 4711f66e7d5c
permissions -rw-r--r--
8032733: Fix cast lint warnings in client libraries 8032734: Add cast lint warning to build of jdk repository Reviewed-by: alanb, bae, tbell, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20112
diff changeset
     2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.imageio.plugins.jpeg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.imageio.ImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.imageio.metadata.IIOMetadataNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.imageio.metadata.IIOMetadataFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.imageio.metadata.IIOMetadataFormatImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.imageio.metadata.IIOInvalidTreeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.imageio.plugins.jpeg.JPEGQTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.NodeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.color.ICC_Profile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.color.ICC_ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Metadata for the JPEG plug-in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class JPEGMetadata extends IIOMetadata implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    //////// Private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * A copy of <code>markerSequence</code>, created the first time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <code>markerSequence</code> is modified.  This is used by reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * to restore the original state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private List resetSequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Set to <code>true</code> when reading a thumbnail stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * JPEG.  This is used to enforce the prohibition of JFIF thumbnails
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * containing any JFIF marker segments, and to ensure generation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * a correct native subtree during <code>getAsTree</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean inThumb = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Set by the chroma node construction method to signal the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * presence or absence of an alpha channel to the transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * node construction method.  Used only when constructing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * standard metadata tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private boolean hasAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    //////// end of private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /////// Package-access variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * All data is a list of <code>MarkerSegment</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * When accessing the list, use the tag to identify the particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * subclass.  Any JFIF marker segment must be the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * of the list if it is present, and any JFXX or APP2ICC marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * segments are subordinate to the JFIF marker segment.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * list is package visible so that the writer can access it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see #MarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    List markerSequence = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Indicates whether this object represents stream or image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * metadata.  Package-visible so the writer can see it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    final boolean isStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /////// End of package-access variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /////// Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Constructor containing code shared by other constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    JPEGMetadata(boolean isStream, boolean inThumb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        super(true,  // Supports standard format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
              JPEG.nativeImageMetadataFormatName,  // and a native format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
              JPEG.nativeImageMetadataFormatClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
              null, null);  // No other formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.inThumb = inThumb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // But if we are stream metadata, adjust the variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.isStream = isStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            nativeMetadataFormatName = JPEG.nativeStreamMetadataFormatName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            nativeMetadataFormatClassName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                JPEG.nativeStreamMetadataFormatClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Constructs a <code>JPEGMetadata</code> object by reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * contents of an <code>ImageInputStream</code>.  Has package-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param isStream A boolean indicating whether this object will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * stream or image metadata.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param isThumb A boolean indicating whether this metadata object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * is for an image or for a thumbnail stored as JPEG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param iis An <code>ImageInputStream</code> from which to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the metadata.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param reader The <code>JPEGImageReader</code> calling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * constructor, to which warnings should be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    JPEGMetadata(boolean isStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                 boolean isThumb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                 ImageInputStream iis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                 JPEGImageReader reader) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this(isStream, isThumb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        JPEGBuffer buffer = new JPEGBuffer(iis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        buffer.loadBuf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // The first three bytes should be FF, SOI, FF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (((buffer.buf[0] & 0xff) != 0xff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            || ((buffer.buf[1] & 0xff) != JPEG.SOI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            || ((buffer.buf[2] & 0xff) != 0xff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new IIOException ("Image format error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        buffer.bufAvail -=2;  // Next byte should be the ff before a marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        buffer.bufPtr = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        MarkerSegment newGuy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            byte [] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            int ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            buffer.loadBuf(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                System.out.println("top of loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                buffer.print(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            buffer.scanForFF(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            switch (buffer.buf[buffer.bufPtr] & 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    System.out.println("Skipping 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                buffer.bufPtr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            case JPEG.SOF0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            case JPEG.SOF1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            case JPEG.SOF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        ("SOF not permitted in stream metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                newGuy = new SOFMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            case JPEG.DQT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                newGuy = new DQTMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            case JPEG.DHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                newGuy = new DHTMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            case JPEG.DRI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                newGuy = new DRIMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            case JPEG.APP0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // Either JFIF, JFXX, or unknown APP0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                buffer.loadBuf(8); // tag, length, id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                buf = buffer.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                ptr = buffer.bufPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if ((buf[ptr+3] == 'J')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    && (buf[ptr+4] == 'F')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    && (buf[ptr+5] == 'I')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    && (buf[ptr+6] == 'F')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    && (buf[ptr+7] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    if (inThumb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        reader.warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            (JPEGImageReader.WARNING_NO_JFIF_IN_THUMB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        // Leave newGuy null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        // Read a dummy to skip the segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        JFIFMarkerSegment dummy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            new JFIFMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    } else if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            ("JFIF not permitted in stream metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    } else if (markerSequence.isEmpty() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            ("JFIF APP0 must be first marker after SOI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        newGuy = new JFIFMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                } else if ((buf[ptr+3] == 'J')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                           && (buf[ptr+4] == 'F')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                           && (buf[ptr+5] == 'X')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                           && (buf[ptr+6] == 'X')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                           && (buf[ptr+7] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            ("JFXX not permitted in stream metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    if (inThumb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                          ("JFXX markers not allowed in JFIF JPEG thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        (JFIFMarkerSegment) findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                               (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    if (jfif == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            ("JFXX encountered without prior JFIF!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    jfif.addJFXX(buffer, reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    // newGuy remains null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    newGuy = new MarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    newGuy.loadData(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            case JPEG.APP2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                // Either an ICC profile or unknown APP2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                buffer.loadBuf(15); // tag, length, id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if ((buffer.buf[buffer.bufPtr+3] == 'I')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    && (buffer.buf[buffer.bufPtr+4] == 'C')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    && (buffer.buf[buffer.bufPtr+5] == 'C')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    && (buffer.buf[buffer.bufPtr+6] == '_')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    && (buffer.buf[buffer.bufPtr+7] == 'P')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    && (buffer.buf[buffer.bufPtr+8] == 'R')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    && (buffer.buf[buffer.bufPtr+9] == 'O')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    && (buffer.buf[buffer.bufPtr+10] == 'F')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    && (buffer.buf[buffer.bufPtr+11] == 'I')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    && (buffer.buf[buffer.bufPtr+12] == 'L')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    && (buffer.buf[buffer.bufPtr+13] == 'E')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    && (buffer.buf[buffer.bufPtr+14] == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            ("ICC profiles not permitted in stream metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        (JFIFMarkerSegment) findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (jfif == null) {
20112
a1c89924203c 8023565: JPG causes javax.imageio.IIOException: ICC APP2 encoutered without prior JFIF
mcherkas
parents: 5506
diff changeset
   281
                        newGuy = new MarkerSegment(buffer);
a1c89924203c 8023565: JPG causes javax.imageio.IIOException: ICC APP2 encoutered without prior JFIF
mcherkas
parents: 5506
diff changeset
   282
                        newGuy.loadData(buffer);
a1c89924203c 8023565: JPG causes javax.imageio.IIOException: ICC APP2 encoutered without prior JFIF
mcherkas
parents: 5506
diff changeset
   283
                    } else {
a1c89924203c 8023565: JPG causes javax.imageio.IIOException: ICC APP2 encoutered without prior JFIF
mcherkas
parents: 5506
diff changeset
   284
                        jfif.addICC(buffer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    // newGuy remains null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    newGuy = new MarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    newGuy.loadData(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            case JPEG.APP14:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                // Either Adobe or unknown APP14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                buffer.loadBuf(8); // tag, length, id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                if ((buffer.buf[buffer.bufPtr+3] == 'A')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    && (buffer.buf[buffer.bufPtr+4] == 'd')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    && (buffer.buf[buffer.bufPtr+5] == 'o')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    && (buffer.buf[buffer.bufPtr+6] == 'b')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    && (buffer.buf[buffer.bufPtr+7] == 'e')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                      ("Adobe APP14 markers not permitted in stream metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    newGuy = new AdobeMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    newGuy = new MarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    newGuy.loadData(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            case JPEG.COM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                newGuy = new COMMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            case JPEG.SOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        ("SOS not permitted in stream metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                newGuy = new SOSMarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            case JPEG.RST0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            case JPEG.RST1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            case JPEG.RST2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            case JPEG.RST3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            case JPEG.RST4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            case JPEG.RST5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            case JPEG.RST6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            case JPEG.RST7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    System.out.println("Restart Marker");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                buffer.bufPtr++; // Just skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            case JPEG.EOI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                buffer.bufPtr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                newGuy = new MarkerSegment(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                newGuy.loadData(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                newGuy.unknown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (newGuy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                markerSequence.add(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    newGuy.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                newGuy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // Now that we've read up to the EOI, we need to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // whatever is left in the buffer, so that the next read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // in the native code will work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        buffer.pushBack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (!isConsistent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new IIOException("Inconsistent metadata read from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Constructs a default stream <code>JPEGMetadata</code> object appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * for the given write parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    JPEGMetadata(ImageWriteParam param, JPEGImageWriter writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        this(true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        JPEGImageWriteParam jparam = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if ((param != null) && (param instanceof JPEGImageWriteParam)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            jparam = (JPEGImageWriteParam) param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (!jparam.areTablesSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                jparam = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (jparam != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            markerSequence.add(new DQTMarkerSegment(jparam.getQTables()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            markerSequence.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                (new DHTMarkerSegment(jparam.getDCHuffmanTables(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                      jparam.getACHuffmanTables()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // default tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            markerSequence.add(new DQTMarkerSegment(JPEG.getDefaultQTables()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            markerSequence.add(new DHTMarkerSegment(JPEG.getDefaultHuffmanTables(true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                                    JPEG.getDefaultHuffmanTables(false)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // Defensive programming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (!isConsistent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throw new InternalError("Default stream metadata is inconsistent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Constructs a default image <code>JPEGMetadata</code> object appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * for the given image type and write parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    JPEGMetadata(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                 ImageWriteParam param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                 JPEGImageWriter writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        this(false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        boolean wantJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        boolean wantAdobe = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int transform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        boolean willSubsample = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        boolean wantICC = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        boolean wantProg = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        boolean wantOptimized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        boolean wantExtended = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        boolean wantQTables = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        boolean wantHTables = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        float quality = JPEG.DEFAULT_QUALITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        byte[] componentIDs = { 1, 2, 3, 4};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int numComponents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        ImageTypeSpecifier destType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            destType = param.getDestinationType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (destType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if (imageType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    // Ignore the destination type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    writer.warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        (JPEGImageWriter.WARNING_DEST_IGNORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    destType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // The only progressive mode that makes sense here is MODE_DEFAULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (param.canWriteProgressive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // the param may not be one of ours, so it may return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                // If so, the following would throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                if (param.getProgressiveMode() == ImageWriteParam.MODE_DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    wantProg = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    wantOptimized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    wantHTables = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (param instanceof JPEGImageWriteParam) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                JPEGImageWriteParam jparam = (JPEGImageWriteParam) param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (jparam.areTablesSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    wantQTables = false;  // If the param has them, metadata shouldn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    wantHTables = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    if ((jparam.getDCHuffmanTables().length > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                            || (jparam.getACHuffmanTables().length > 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        wantExtended = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                // Progressive forces optimized, regardless of param setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // so consult the param re optimized only if not progressive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (!wantProg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    wantOptimized = jparam.getOptimizeHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    if (wantOptimized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        wantHTables = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            // compression quality should determine the q tables.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // will be ignored if we already decided not to create any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            // Again, the param may not be one of ours, so we must check that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            // supports compression settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (param.canWriteCompressed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    quality = param.getCompressionQuality();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // We are done with the param, now for the image types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        ColorSpace cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (destType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            ColorModel cm = destType.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            numComponents = cm.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            boolean hasExtraComponents = (cm.getNumColorComponents() != numComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            boolean hasAlpha = cm.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            int type = cs.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                if (hasExtraComponents) {  // e.g. alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            case ColorSpace.TYPE_3CLR:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   494
                if (cs == JPEG.JCS.getYCC()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    componentIDs[0] = (byte) 'Y';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    componentIDs[1] = (byte) 'C';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    componentIDs[2] = (byte) 'c';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        componentIDs[3] = (byte) 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            case ColorSpace.TYPE_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                if (hasExtraComponents) { // e.g. K or alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    if (!hasAlpha) { // Not alpha, so must be K
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        transform = JPEG.ADOBE_YCCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            case ColorSpace.TYPE_RGB:  // with or without alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                componentIDs[0] = (byte) 'R';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                componentIDs[1] = (byte) 'G';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                componentIDs[2] = (byte) 'B';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    componentIDs[3] = (byte) 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                // Everything else is not subsampled, gets no special marker,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // and component ids are 1 - N
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } else if (imageType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            ColorModel cm = imageType.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            numComponents = cm.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            boolean hasExtraComponents = (cm.getNumColorComponents() != numComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            boolean hasAlpha = cm.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            int type = cs.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (hasExtraComponents) {  // e.g. alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            case ColorSpace.TYPE_RGB:  // with or without alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                // without alpha we just accept the JFIF defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            case ColorSpace.TYPE_3CLR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    willSubsample = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    componentIDs[0] = (byte) 'Y';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    componentIDs[1] = (byte) 'C';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    componentIDs[2] = (byte) 'c';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    if (hasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        componentIDs[3] = (byte) 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            case ColorSpace.TYPE_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                if (hasExtraComponents) { // e.g. K or alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (!hasAlpha) {  // then it must be K
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        transform = JPEG.ADOBE_YCCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            case ColorSpace.TYPE_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                transform = JPEG.ADOBE_YCCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                // Everything else is not subsampled, gets no special marker,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                // and component ids are 0 - N
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // do we want an ICC profile?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (wantJFIF && JPEG.isNonStandardICC(cs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            wantICC = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Now step through the markers, consulting our variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (wantJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            JFIFMarkerSegment jfif = new JFIFMarkerSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            markerSequence.add(jfif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (wantICC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    jfif.addICC((ICC_ColorSpace)cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                } catch (IOException e) {} // Can't happen here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        // Adobe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (wantAdobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            markerSequence.add(new AdobeMarkerSegment(transform));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // dqt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (wantQTables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            markerSequence.add(new DQTMarkerSegment(quality, willSubsample));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        // dht
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (wantHTables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            markerSequence.add(new DHTMarkerSegment(willSubsample));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // sof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        markerSequence.add(new SOFMarkerSegment(wantProg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                                wantExtended,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                                willSubsample,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                                componentIDs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                                numComponents));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // sos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (!wantProg) {  // Default progression scans are done in the writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            markerSequence.add(new SOSMarkerSegment(willSubsample,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                                    componentIDs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                                    numComponents));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // Defensive programming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (!isConsistent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            throw new InternalError("Default image metadata is inconsistent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    ////// End of constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    // Utilities for dealing with the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    // The first ones have package access for access from the writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Returns the first MarkerSegment object in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * with the given tag, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    MarkerSegment findMarkerSegment(int tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            MarkerSegment seg = (MarkerSegment)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (seg.tag == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                return seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Returns the first or last MarkerSegment object in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * of the given class, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    MarkerSegment findMarkerSegment(Class cls, boolean first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                MarkerSegment seg = (MarkerSegment)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (cls.isInstance(seg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    return seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            ListIterator iter = markerSequence.listIterator(markerSequence.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            while (iter.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                MarkerSegment seg = (MarkerSegment)iter.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                if (cls.isInstance(seg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    return seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns the index of the first or last MarkerSegment in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * of the given class, or -1 if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    private int findMarkerSegmentPosition(Class cls, boolean first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            ListIterator iter = markerSequence.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            for (int i = 0; iter.hasNext(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                MarkerSegment seg = (MarkerSegment)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                if (cls.isInstance(seg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            ListIterator iter = markerSequence.listIterator(markerSequence.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            for (int i = markerSequence.size()-1; iter.hasPrevious(); i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                MarkerSegment seg = (MarkerSegment)iter.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                if (cls.isInstance(seg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    private int findLastUnknownMarkerSegmentPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        ListIterator iter = markerSequence.listIterator(markerSequence.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        for (int i = markerSequence.size()-1; iter.hasPrevious(); i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            MarkerSegment seg = (MarkerSegment)iter.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (seg.unknown == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    // Implement Cloneable, but restrict access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    protected Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        JPEGMetadata newGuy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            newGuy = (JPEGMetadata) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        } catch (CloneNotSupportedException e) {} // won't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (markerSequence != null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20112
diff changeset
   727
            newGuy.markerSequence = cloneSequence();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        newGuy.resetSequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return newGuy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * Returns a deep copy of the current marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private List cloneSequence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (markerSequence == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        List retval = new ArrayList(markerSequence.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        while(iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            MarkerSegment seg = (MarkerSegment)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            retval.add(seg.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    // Tree methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public Node getAsTree(String formatName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (formatName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            throw new IllegalArgumentException("null formatName!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            if (formatName.equals(JPEG.nativeStreamMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                return getNativeTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (formatName.equals(JPEG.nativeImageMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                return getNativeTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            if (formatName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    (IIOMetadataFormatImpl.standardMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                return getStandardTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        throw  new IllegalArgumentException("Unsupported format name: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                                + formatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    IIOMetadataNode getNativeTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        IIOMetadataNode root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        IIOMetadataNode top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            root = new IIOMetadataNode(JPEG.nativeStreamMetadataFormatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            top = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            IIOMetadataNode sequence = new IIOMetadataNode("markerSequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (!inThumb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                root = new IIOMetadataNode(JPEG.nativeImageMetadataFormatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                IIOMetadataNode header = new IIOMetadataNode("JPEGvariety");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                root.appendChild(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                JFIFMarkerSegment jfif = (JFIFMarkerSegment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    findMarkerSegment(JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    iter.next();  // JFIF must be first, so this skips it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    header.appendChild(jfif.getNativeNode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                root.appendChild(sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                root = sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            top = sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        while(iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            top.appendChild(seg.getNativeNode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    // Standard tree node methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    protected IIOMetadataNode getStandardChromaNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        hasAlpha = false;  // Unless we find otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        // Colorspace type - follow the rules in the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        // First get the SOF marker segment, if there is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        SOFMarkerSegment sof = (SOFMarkerSegment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            findMarkerSegment(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if (sof == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            // No image, so no chroma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        IIOMetadataNode chroma = new IIOMetadataNode("Chroma");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        IIOMetadataNode csType = new IIOMetadataNode("ColorSpaceType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        chroma.appendChild(csType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // get the number of channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        int numChannels = sof.componentSpecs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        IIOMetadataNode numChanNode = new IIOMetadataNode("NumChannels");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        chroma.appendChild(numChanNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        numChanNode.setAttribute("value", Integer.toString(numChannels));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // is there a JFIF marker segment?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (findMarkerSegment(JFIFMarkerSegment.class, true) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (numChannels == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                csType.setAttribute("name", "GRAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                csType.setAttribute("name", "YCbCr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // How about an Adobe marker segment?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        AdobeMarkerSegment adobe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            (AdobeMarkerSegment) findMarkerSegment(AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (adobe != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            switch (adobe.transform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            case JPEG.ADOBE_YCCK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                csType.setAttribute("name", "YCCK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            case JPEG.ADOBE_YCC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                csType.setAttribute("name", "YCbCr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            case JPEG.ADOBE_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                if (numChannels == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    csType.setAttribute("name", "RGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                } else if (numChannels == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    csType.setAttribute("name", "CMYK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // Neither marker.  Check components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (numChannels < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            csType.setAttribute("name", "GRAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            if (numChannels == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                hasAlpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        boolean idsAreJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        for (int i = 0; i < sof.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            int id = sof.componentSpecs[i].componentId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            if ((id < 1) || (id >= sof.componentSpecs.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                idsAreJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        if (idsAreJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            csType.setAttribute("name", "YCbCr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (numChannels == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                hasAlpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // Check against the letters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        if ((sof.componentSpecs[0].componentId == 'R')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            && (sof.componentSpecs[1].componentId == 'G')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            && (sof.componentSpecs[2].componentId == 'B')){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            csType.setAttribute("name", "RGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if ((numChannels == 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                && (sof.componentSpecs[3].componentId == 'A')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                hasAlpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if ((sof.componentSpecs[0].componentId == 'Y')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            && (sof.componentSpecs[1].componentId == 'C')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            && (sof.componentSpecs[2].componentId == 'c')){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            csType.setAttribute("name", "PhotoYCC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            if ((numChannels == 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                && (sof.componentSpecs[3].componentId == 'A')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                hasAlpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        // Finally, 3-channel subsampled are YCbCr, unsubsampled are RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // 4-channel subsampled are YCbCrA, unsubsampled are CMYK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        boolean subsampled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        int hfactor = sof.componentSpecs[0].HsamplingFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        int vfactor = sof.componentSpecs[0].VsamplingFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        for (int i = 1; i<sof.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if ((sof.componentSpecs[i].HsamplingFactor != hfactor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                || (sof.componentSpecs[i].VsamplingFactor != vfactor)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                subsampled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (subsampled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            csType.setAttribute("name", "YCbCr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (numChannels == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                hasAlpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // Not subsampled.  numChannels < 3 is taken care of above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        if (numChannels == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            csType.setAttribute("name", "RGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            csType.setAttribute("name", "CMYK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        return chroma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    protected IIOMetadataNode getStandardCompressionNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        IIOMetadataNode compression = new IIOMetadataNode("Compression");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        // CompressionTypeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        IIOMetadataNode name = new IIOMetadataNode("CompressionTypeName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        name.setAttribute("value", "JPEG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        compression.appendChild(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        // Lossless - false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        IIOMetadataNode lossless = new IIOMetadataNode("Lossless");
2375
bb4dd76ca2c9 5082756: Image I/O plug-ins set metadata boolean attributes to "true" or "false"
bae
parents: 2
diff changeset
   959
        lossless.setAttribute("value", "FALSE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        compression.appendChild(lossless);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        // NumProgressiveScans - count sos segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        int sosCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            MarkerSegment ms = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            if (ms.tag == JPEG.SOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                sosCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (sosCount != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            IIOMetadataNode prog = new IIOMetadataNode("NumProgressiveScans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            prog.setAttribute("value", Integer.toString(sosCount));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            compression.appendChild(prog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        return compression;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    protected IIOMetadataNode getStandardDimensionNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // If we have a JFIF marker segment, we know a little
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // otherwise all we know is the orientation, which is always normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        IIOMetadataNode dim = new IIOMetadataNode("Dimension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        IIOMetadataNode orient = new IIOMetadataNode("ImageOrientation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        orient.setAttribute("value", "normal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        dim.appendChild(orient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            // Aspect Ratio is width of pixel / height of pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            float aspectRatio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (jfif.resUnits == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                // In this case they just encode aspect ratio directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                aspectRatio = ((float) jfif.Xdensity)/jfif.Ydensity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                // They are true densities (e.g. dpi) and must be inverted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                aspectRatio = ((float) jfif.Ydensity)/jfif.Xdensity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            IIOMetadataNode aspect = new IIOMetadataNode("PixelAspectRatio");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            aspect.setAttribute("value", Float.toString(aspectRatio));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            dim.insertBefore(aspect, orient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            // Pixel size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if (jfif.resUnits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                // 1 == dpi, 2 == dpc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                float scale = (jfif.resUnits == 1) ? 25.4F : 10.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                IIOMetadataNode horiz =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    new IIOMetadataNode("HorizontalPixelSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                horiz.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                                   Float.toString(scale/jfif.Xdensity));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                dim.appendChild(horiz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                IIOMetadataNode vert =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    new IIOMetadataNode("VerticalPixelSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                vert.setAttribute("value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                                  Float.toString(scale/jfif.Ydensity));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                dim.appendChild(vert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    protected IIOMetadataNode getStandardTextNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        IIOMetadataNode text = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        // Add a text entry for each COM Marker Segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (findMarkerSegment(JPEG.COM) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            text = new IIOMetadataNode("Text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                if (seg.tag == JPEG.COM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                    COMMarkerSegment com = (COMMarkerSegment) seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    IIOMetadataNode entry = new IIOMetadataNode("TextEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    entry.setAttribute("keyword", "comment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    entry.setAttribute("value", com.getComment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                text.appendChild(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    protected IIOMetadataNode getStandardTransparencyNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        IIOMetadataNode trans = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (hasAlpha == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            trans = new IIOMetadataNode("Transparency");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            IIOMetadataNode alpha = new IIOMetadataNode("Alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            alpha.setAttribute("value", "nonpremultiplied"); // Always assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            trans.appendChild(alpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return trans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // Editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    public boolean isReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        return false;
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 void mergeTree(String formatName, Node root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (formatName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            throw new IllegalArgumentException("null formatName!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            throw new IllegalArgumentException("null root!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        List copy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (resetSequence == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            resetSequence = cloneSequence();  // Deep copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            copy = resetSequence;  // Avoid cloning twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            copy = cloneSequence();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (isStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            (formatName.equals(JPEG.nativeStreamMetadataFormatName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                mergeNativeTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        } else if (!isStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                   (formatName.equals(JPEG.nativeImageMetadataFormatName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            mergeNativeTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        } else if (!isStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                   (formatName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    (IIOMetadataFormatImpl.standardMetadataFormatName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            mergeStandardTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            throw  new IllegalArgumentException("Unsupported format name: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                                                + formatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (!isConsistent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            markerSequence = copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            throw new IIOInvalidTreeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                ("Merged tree is invalid; original restored", root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    private void mergeNativeTree(Node root) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        String name = root.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (name != ((isStream) ? JPEG.nativeStreamMetadataFormatName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                                : JPEG.nativeImageMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            throw new IIOInvalidTreeException("Invalid root node name: " + name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                                              root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (root.getChildNodes().getLength() != 2) { // JPEGvariety and markerSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            throw new IIOInvalidTreeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                "JPEGvariety and markerSequence nodes must be present", root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        mergeJFIFsubtree(root.getFirstChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        mergeSequenceSubtree(root.getLastChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * Merge a JFIF subtree into the marker sequence, if the subtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * is non-empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * If a JFIF marker exists, update it from the subtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * If none exists, create one from the subtree and insert it at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * beginning of the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private void mergeJFIFsubtree(Node JPEGvariety)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        if (JPEGvariety.getChildNodes().getLength() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            Node jfifNode = JPEGvariety.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            // is there already a jfif marker segment?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            JFIFMarkerSegment jfifSeg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (jfifSeg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                jfifSeg.updateFromNativeNode(jfifNode, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                // Add it as the first element in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                markerSequence.add(0, new JFIFMarkerSegment(jfifNode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    private void mergeSequenceSubtree(Node sequenceTree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        NodeList children = sequenceTree.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            Node node = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            String name = node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            if (name.equals("dqt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                mergeDQTNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            } else if (name.equals("dht")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                mergeDHTNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            } else if (name.equals("dri")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                mergeDRINode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            } else if (name.equals("com")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                mergeCOMNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            } else if (name.equals("app14Adobe")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                mergeAdobeNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            } else if (name.equals("unknown")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                mergeUnknownNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            } else if (name.equals("sof")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                mergeSOFNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            } else if (name.equals("sos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                mergeSOSNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                throw new IIOInvalidTreeException("Invalid node: " + name, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Merge the given DQT node into the marker sequence.  If there already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * exist DQT marker segments in the sequence, then each table in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * node replaces the first table, in any DQT segment, with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * table id.  If none of the existing DQT segments contain a table with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * the same id, then the table is added to the last existing DQT segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * If there are no DQT segments, then a new one is created and added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * If there are DHT segments, the new DQT segment is inserted before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * first one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * If there are no DHT segments, the new DQT segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * an SOF segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * If there is no SOF segment, the new DQT segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * the first SOS segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * If there is no SOS segment, the new DQT segment is added to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    private void mergeDQTNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        // First collect any existing DQT nodes into a local list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        ArrayList oldDQTs = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            if (seg instanceof DQTMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                oldDQTs.add(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        if (!oldDQTs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            NodeList children = node.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                Node child = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                int childID = MarkerSegment.getAttributeValue(child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                                                              null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                                                              "qtableId",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                                                              0, 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                                                              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                DQTMarkerSegment dqt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                int tableIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                for (int j = 0; j < oldDQTs.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                    DQTMarkerSegment testDQT = (DQTMarkerSegment) oldDQTs.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    for (int k = 0; k < testDQT.tables.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                        DQTMarkerSegment.Qtable testTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                            (DQTMarkerSegment.Qtable) testDQT.tables.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                        if (childID == testTable.tableID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                            dqt = testDQT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                            tableIndex = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    if (dqt != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                if (dqt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    dqt.tables.set(tableIndex, dqt.getQtableFromNode(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    dqt = (DQTMarkerSegment) oldDQTs.get(oldDQTs.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    dqt.tables.add(dqt.getQtableFromNode(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            DQTMarkerSegment newGuy = new DQTMarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            int firstDHT = findMarkerSegmentPosition(DHTMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            int firstSOF = findMarkerSegmentPosition(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            int firstSOS = findMarkerSegmentPosition(SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            if (firstDHT != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                markerSequence.add(firstDHT, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            } else if (firstSOF != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                markerSequence.add(firstSOF, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            } else if (firstSOS != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                markerSequence.add(firstSOS, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                markerSequence.add(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * Merge the given DHT node into the marker sequence.  If there already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * exist DHT marker segments in the sequence, then each table in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * node replaces the first table, in any DHT segment, with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * table class and table id.  If none of the existing DHT segments contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * a table with the same class and id, then the table is added to the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * existing DHT segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * If there are no DHT segments, then a new one is created and added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * If there are DQT segments, the new DHT segment is inserted immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * following the last DQT segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * If there are no DQT segments, the new DHT segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * an SOF segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * If there is no SOF segment, the new DHT segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * the first SOS segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * If there is no SOS segment, the new DHT segment is added to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    private void mergeDHTNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        // First collect any existing DQT nodes into a local list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        ArrayList oldDHTs = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            if (seg instanceof DHTMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                oldDHTs.add(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        if (!oldDHTs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            NodeList children = node.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                Node child = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                NamedNodeMap attrs = child.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                int childID = MarkerSegment.getAttributeValue(child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                                                              attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                                                              "htableId",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                                                              0, 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                                              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                int childClass = MarkerSegment.getAttributeValue(child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                                                                 attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                                                                 "class",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                                                 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                                                 true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                DHTMarkerSegment dht = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                int tableIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                for (int j = 0; j < oldDHTs.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    DHTMarkerSegment testDHT = (DHTMarkerSegment) oldDHTs.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    for (int k = 0; k < testDHT.tables.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                        DHTMarkerSegment.Htable testTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                            (DHTMarkerSegment.Htable) testDHT.tables.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                        if ((childID == testTable.tableID) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                            (childClass == testTable.tableClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                            dht = testDHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                            tableIndex = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    if (dht != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                if (dht != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                    dht.tables.set(tableIndex, dht.getHtableFromNode(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                    dht = (DHTMarkerSegment) oldDHTs.get(oldDHTs.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    dht.tables.add(dht.getHtableFromNode(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            DHTMarkerSegment newGuy = new DHTMarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            int lastDQT = findMarkerSegmentPosition(DQTMarkerSegment.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            int firstSOF = findMarkerSegmentPosition(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            int firstSOS = findMarkerSegmentPosition(SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (lastDQT != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                markerSequence.add(lastDQT+1, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            } else if (firstSOF != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                markerSequence.add(firstSOF, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            } else if (firstSOS != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                markerSequence.add(firstSOS, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                markerSequence.add(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * Merge the given DRI node into the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * If there already exists a DRI marker segment, the restart interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * value is updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * If there is no DRI segment, then a new one is created and added as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * If there is an SOF segment, the new DRI segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * If there is no SOF segment, the new DRI segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * the first SOS segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * If there is no SOS segment, the new DRI segment is added to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    private void mergeDRINode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        DRIMarkerSegment dri =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            (DRIMarkerSegment) findMarkerSegment(DRIMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        if (dri != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            dri.updateFromNativeNode(node, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            DRIMarkerSegment newGuy = new DRIMarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            int firstSOF = findMarkerSegmentPosition(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            int firstSOS = findMarkerSegmentPosition(SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (firstSOF != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                markerSequence.add(firstSOF, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            } else if (firstSOS != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                markerSequence.add(firstSOS, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                markerSequence.add(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * Merge the given COM node into the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * A new COM marker segment is created and added to the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * using insertCOMMarkerSegment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    private void mergeCOMNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        COMMarkerSegment newGuy = new COMMarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        insertCOMMarkerSegment(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
      * Insert a new COM marker segment into an appropriate place in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
      * marker sequence, as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
      * If there already exist COM marker segments, the new one is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
      * after the last one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
      * If there are no COM segments, the new COM segment is inserted after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
      * JFIF segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
      * If there is no JFIF segment, the new COM segment is inserted after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
      * Adobe marker segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
      * If there is no Adobe segment, the new COM segment is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
      * at the beginning of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    private void insertCOMMarkerSegment(COMMarkerSegment newGuy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        int lastCOM = findMarkerSegmentPosition(COMMarkerSegment.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        boolean hasJFIF = (findMarkerSegment(JFIFMarkerSegment.class, true) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        int firstAdobe = findMarkerSegmentPosition(AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        if (lastCOM != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            markerSequence.add(lastCOM+1, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        } else if (hasJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            markerSequence.add(1, newGuy);  // JFIF is always 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        } else if (firstAdobe != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            markerSequence.add(firstAdobe+1, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            markerSequence.add(0, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * Merge the given Adobe APP14 node into the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * If there already exists an Adobe marker segment, then its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * are updated from the node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * If there is no Adobe segment, then a new one is created and added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * using insertAdobeMarkerSegment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    private void mergeAdobeNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        AdobeMarkerSegment adobe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            (AdobeMarkerSegment) findMarkerSegment(AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            adobe.updateFromNativeNode(node, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            AdobeMarkerSegment newGuy = new AdobeMarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            insertAdobeMarkerSegment(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * Insert the given AdobeMarkerSegment into the marker sequence, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * follows (we assume there is no Adobe segment yet):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * If there is a JFIF segment, then the new Adobe segment is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * after it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * If there is no JFIF segment, the new Adobe segment is inserted after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * last Unknown segment, if there are any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * If there are no Unknown segments, the new Adobe segment is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * at the beginning of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    private void insertAdobeMarkerSegment(AdobeMarkerSegment newGuy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        boolean hasJFIF =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            (findMarkerSegment(JFIFMarkerSegment.class, true) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        int lastUnknown = findLastUnknownMarkerSegmentPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        if (hasJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            markerSequence.add(1, newGuy);  // JFIF is always 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        } else if (lastUnknown != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            markerSequence.add(lastUnknown+1, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            markerSequence.add(0, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * Merge the given Unknown node into the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * A new Unknown marker segment is created and added to the sequence as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * If there already exist Unknown marker segments, the new one is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * after the last one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * If there are no Unknown marker segments, the new Unknown marker segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * is inserted after the JFIF segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * If there is no JFIF segment, the new Unknown segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * the Adobe marker segment, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * If there is no Adobe segment, the new Unknown segment is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * at the beginning of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    private void mergeUnknownNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        MarkerSegment newGuy = new MarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        int lastUnknown = findLastUnknownMarkerSegmentPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        boolean hasJFIF = (findMarkerSegment(JFIFMarkerSegment.class, true) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        int firstAdobe = findMarkerSegmentPosition(AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        if (lastUnknown != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            markerSequence.add(lastUnknown+1, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        } else if (hasJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            markerSequence.add(1, newGuy);  // JFIF is always 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        } if (firstAdobe != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            markerSequence.add(firstAdobe, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            markerSequence.add(0, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * Merge the given SOF node into the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * If there already exists an SOF marker segment in the sequence, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * its values are updated from the node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * If there is no SOF segment, then a new one is created and added as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * If there are any SOS segments, the new SOF segment is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * the first one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * If there is no SOS segment, the new SOF segment is added to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    private void mergeSOFNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        SOFMarkerSegment sof =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            (SOFMarkerSegment) findMarkerSegment(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            sof.updateFromNativeNode(node, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            SOFMarkerSegment newGuy = new SOFMarkerSegment(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            int firstSOS = findMarkerSegmentPosition(SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            if (firstSOS != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                markerSequence.add(firstSOS, newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                markerSequence.add(newGuy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * Merge the given SOS node into the marker sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * If there already exists a single SOS marker segment, then the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * are updated from the node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * If there are more than one existing SOS marker segments, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * IIOInvalidTreeException is thrown, as SOS segments cannot be merged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * into a set of progressive scans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * If there are no SOS marker segments, a new one is created and added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * to the end of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    private void mergeSOSNode(Node node) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        SOSMarkerSegment firstSOS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        SOSMarkerSegment lastSOS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        if (firstSOS != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            if (firstSOS != lastSOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                throw new IIOInvalidTreeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                    ("Can't merge SOS node into a tree with > 1 SOS node", node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            firstSOS.updateFromNativeNode(node, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            markerSequence.add(new SOSMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    private boolean transparencyDone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    private void mergeStandardTree(Node root) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        transparencyDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        NodeList children = root.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            Node node = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            String name = node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            if (name.equals("Chroma")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                mergeStandardChromaNode(node, children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            } else if (name.equals("Compression")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                mergeStandardCompressionNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            } else if (name.equals("Data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                mergeStandardDataNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            } else if (name.equals("Dimension")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                mergeStandardDimensionNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            } else if (name.equals("Document")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                mergeStandardDocumentNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            } else if (name.equals("Text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                mergeStandardTextNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            } else if (name.equals("Transparency")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                mergeStandardTransparencyNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                throw new IIOInvalidTreeException("Invalid node: " + name, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * In general, it could be possible to convert all non-pixel data to some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * textual form and include it in comments, but then this would create the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * expectation that these comment forms be recognized by the reader, thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * creating a defacto extension to JPEG metadata capabilities.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * probably best avoided, so the following convert only text nodes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * comments, and lose the keywords as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    private void mergeStandardChromaNode(Node node, NodeList siblings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        // ColorSpaceType can change the target colorspace for compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        // This must take any transparency node into account as well, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        // that affects the number of channels (if alpha is present).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        // a transparency node is dealt with here, set a flag to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        // this to the transparency processor below.  If we discover that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        // the nodes are not in order, throw an exception as the tree is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        // invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        if (transparencyDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            throw new IIOInvalidTreeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                ("Transparency node must follow Chroma node", node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        Node csType = node.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        if ((csType == null) || !csType.getNodeName().equals("ColorSpaceType")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            // If there is no ColorSpaceType node, we have nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        String csName = csType.getAttributes().getNamedItem("name").getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        int numChannels = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        boolean wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        boolean wantAdobe = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        int transform = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        boolean willSubsample = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        byte [] ids = {1, 2, 3, 4};  // JFIF compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        if (csName.equals("GRAY")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            numChannels = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            wantJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        } else if (csName.equals("YCbCr")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            numChannels = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            wantJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            willSubsample = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        } else if (csName.equals("PhotoYCC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            numChannels = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            transform = JPEG.ADOBE_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            ids[0] = (byte) 'Y';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            ids[1] = (byte) 'C';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            ids[2] = (byte) 'c';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        } else if (csName.equals("RGB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            numChannels = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            transform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            ids[0] = (byte) 'R';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            ids[1] = (byte) 'G';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            ids[2] = (byte) 'B';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        } else if ((csName.equals("XYZ"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                   || (csName.equals("Lab"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                   || (csName.equals("Luv"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                   || (csName.equals("YxY"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                   || (csName.equals("HSV"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                   || (csName.equals("HLS"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                   || (csName.equals("CMY"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                   || (csName.equals("3CLR"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            numChannels = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        } else if (csName.equals("YCCK")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            numChannels = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            transform = JPEG.ADOBE_YCCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            willSubsample = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        } else if (csName.equals("CMYK")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            numChannels = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            wantAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            transform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        } else if (csName.equals("4CLR")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            numChannels = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        } else { // We can't handle them, so don't modify any metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        boolean wantAlpha = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        for (int i = 0; i < siblings.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            Node trans = siblings.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            if (trans.getNodeName().equals("Transparency")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                wantAlpha = wantAlpha(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                break;  // out of for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        if (wantAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            numChannels++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            wantJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            if (ids[0] == (byte) 'R') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                ids[3] = (byte) 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                wantAdobe = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        AdobeMarkerSegment adobe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            (AdobeMarkerSegment) findMarkerSegment(AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        SOFMarkerSegment sof =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            (SOFMarkerSegment) findMarkerSegment(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        SOSMarkerSegment sos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        // If the metadata specifies progressive, then the number of channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // must match, so that we can modify all the existing SOS marker segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        // If they don't match, we don't know what to do with SOS so we can't do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        // the merge.  We then just return silently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        // An exception would not be appropriate.  A warning might, but we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        // nowhere to send it to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if ((sof != null) && (sof.tag == JPEG.SOF2)) { // Progressive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            if ((sof.componentSpecs.length != numChannels) && (sos != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        // JFIF header might be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        if (!wantJFIF && (jfif != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            markerSequence.remove(jfif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        // Now add a JFIF if we do want one, but only if it isn't stream metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        if (wantJFIF && !isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            markerSequence.add(0, new JFIFMarkerSegment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        // Adobe header might be removed or the transform modified, if it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        // stream metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        if (wantAdobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            if ((adobe == null) && !isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                adobe = new AdobeMarkerSegment(transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                insertAdobeMarkerSegment(adobe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                adobe.transform = transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        } else if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            markerSequence.remove(adobe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        boolean updateQtables = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        boolean updateHtables = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        boolean progressive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        int [] subsampledSelectors = {0, 1, 1, 0 } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        int [] nonSubsampledSelectors = { 0, 0, 0, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        int [] newTableSelectors = willSubsample
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                                   ? subsampledSelectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                                   : nonSubsampledSelectors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        // Keep the old componentSpecs array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        SOFMarkerSegment.ComponentSpec [] oldCompSpecs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        // SOF might be modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            oldCompSpecs = sof.componentSpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            progressive = (sof.tag == JPEG.SOF2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            // Now replace the SOF with a new one; it might be the same, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            // this is easier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            markerSequence.set(markerSequence.indexOf(sof),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                               new SOFMarkerSegment(progressive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                                                    false, // we never need extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                                                    willSubsample,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                                                    ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                                                    numChannels));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            // Now suss out if subsampling changed and set the boolean for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            // updating the q tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            // if the old componentSpec q table selectors don't match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            // the new ones, update the qtables.  The new selectors are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            // in place in the new SOF segment above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            for (int i = 0; i < oldCompSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                if (oldCompSpecs[i].QtableSelector != newTableSelectors[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                    updateQtables = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            if (progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                // if the component ids are different, update all the existing scans
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                // ignore Huffman tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                boolean idsDiffer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                for (int i = 0; i < oldCompSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    if (ids[i] != oldCompSpecs[i].componentId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                        idsDiffer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                if (idsDiffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                    // update the ids in each SOS marker segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                    for (Iterator iter = markerSequence.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                        MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                        if (seg instanceof SOSMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                            SOSMarkerSegment target = (SOSMarkerSegment) seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                            for (int i = 0; i < target.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                                int oldSelector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                                    target.componentSpecs[i].componentSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                                // Find the position in the old componentSpecs array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                                // of the old component with the old selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                                // and replace the component selector with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                                // new id at the same position, as these match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                                // the new component specs array in the SOF created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                                // above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                                for (int j = 0; j < oldCompSpecs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                                    if (oldCompSpecs[j].componentId == oldSelector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                                        target.componentSpecs[i].componentSelector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                                            ids[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                if (sos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    // htables - if the old htable selectors don't match the new ones,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                    // update the tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                    for (int i = 0; i < sos.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                        if ((sos.componentSpecs[i].dcHuffTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                             != newTableSelectors[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                            || (sos.componentSpecs[i].acHuffTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                                != newTableSelectors[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                            updateHtables = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                    // Might be the same as the old one, but this is easier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                    markerSequence.set(markerSequence.indexOf(sos),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                               new SOSMarkerSegment(willSubsample,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                                                    ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                                                    numChannels));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            // should be stream metadata if there isn't an SOF, but check it anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            if (isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                // update tables - routines below check if it's really necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                updateQtables = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                updateHtables = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        if (updateQtables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            List tableSegments = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            for (Iterator iter = markerSequence.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                if (seg instanceof DQTMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                    tableSegments.add(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            // If there are no tables, don't add them, as the metadata encodes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            // abbreviated stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            // If we are not subsampling, we just need one, so don't do anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            if (!tableSegments.isEmpty() && willSubsample) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                // Is it really necessary?  There should be at least 2 tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                // If there is only one, assume it's a scaled "standard"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                // luminance table, extract the scaling factor, and generate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                // scaled "standard" chrominance table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                // Find the table with selector 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                for (Iterator iter = tableSegments.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                    DQTMarkerSegment testdqt = (DQTMarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                    for (Iterator tabiter = testdqt.tables.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                         tabiter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                        DQTMarkerSegment.Qtable tab =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                            (DQTMarkerSegment.Qtable) tabiter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                        if (tab.tableID == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                            found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                    //    find the table with selector 0.  There should be one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                    DQTMarkerSegment.Qtable table0 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    for (Iterator iter = tableSegments.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                        DQTMarkerSegment testdqt = (DQTMarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                        for (Iterator tabiter = testdqt.tables.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                             tabiter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                            DQTMarkerSegment.Qtable tab =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                                (DQTMarkerSegment.Qtable) tabiter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                            if (tab.tableID == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                                table0 = tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                    // Assuming that the table with id 0 is a luminance table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    // compute a new chrominance table of the same quality and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                    // add it to the last DQT segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    DQTMarkerSegment dqt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                        (DQTMarkerSegment) tableSegments.get(tableSegments.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                    dqt.tables.add(dqt.getChromaForLuma(table0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        if (updateHtables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            List tableSegments = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            for (Iterator iter = markerSequence.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                MarkerSegment seg = (MarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                if (seg instanceof DHTMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    tableSegments.add(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            // If there are no tables, don't add them, as the metadata encodes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            // abbreviated stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            // If we are not subsampling, we just need one, so don't do anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if (!tableSegments.isEmpty() && willSubsample) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                // Is it really necessary?  There should be at least 2 dc and 2 ac
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                // tables.  If there is only one, add a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                // "standard " chrominance table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                // find a table with selector 1. AC/DC is irrelevant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                for (Iterator iter = tableSegments.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                    DHTMarkerSegment testdht = (DHTMarkerSegment) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                    for (Iterator tabiter = testdht.tables.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                         tabiter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                        DHTMarkerSegment.Htable tab =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                            (DHTMarkerSegment.Htable) tabiter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                        if (tab.tableID == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                            found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                    // Create new standard dc and ac chrominance tables and add them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                    // to the last DHT segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                    DHTMarkerSegment lastDHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                        (DHTMarkerSegment) tableSegments.get(tableSegments.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    lastDHT.addHtable(JPEGHuffmanTable.StdDCLuminance, true, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                    lastDHT.addHtable(JPEGHuffmanTable.StdACLuminance, true, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    private boolean wantAlpha(Node transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        boolean returnValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        Node alpha = transparency.getFirstChild();  // Alpha must be first if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        if (alpha.getNodeName().equals("Alpha")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            if (alpha.hasAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                String value =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                    alpha.getAttributes().getNamedItem("value").getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                if (!value.equals("none")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                    returnValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        transparencyDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        return returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    private void mergeStandardCompressionNode(Node node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        // NumProgressiveScans is ignored.  Progression must be enabled on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        // ImageWriteParam.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        // No-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    private void mergeStandardDataNode(Node node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        // No-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    private void mergeStandardDimensionNode(Node node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        // Pixel Aspect Ratio or pixel size can be incorporated if there is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        // or can be, a JFIF segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        if (jfif == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            // Can there be one?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            // Criteria:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            // SOF must be present with 1 or 3 channels, (stream metadata fails this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            //     Component ids must be JFIF compatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            boolean canHaveJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            SOFMarkerSegment sof =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                (SOFMarkerSegment) findMarkerSegment(SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                int numChannels = sof.componentSpecs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                if ((numChannels == 1) || (numChannels == 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                    canHaveJFIF = true; // remaining tests are negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                    for (int i = 0; i < sof.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                        if (sof.componentSpecs[i].componentId != i+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                            canHaveJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                    // if Adobe present, transform = ADOBE_UNKNOWN for 1-channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                    //     ADOBE_YCC for 3-channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                    AdobeMarkerSegment adobe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                        (AdobeMarkerSegment) findMarkerSegment(AdobeMarkerSegment.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                                                               true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                    if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                        if (adobe.transform != ((numChannels == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                                                ? JPEG.ADOBE_UNKNOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                                                : JPEG.ADOBE_YCC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                            canHaveJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            // If so, create one and insert it into the sequence.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            // default is just pixel ratio at 1:1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            if (canHaveJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                jfif = new JFIFMarkerSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                markerSequence.add(0, jfif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            NodeList children = node.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                Node child = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                NamedNodeMap attrs = child.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                String name = child.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                if (name.equals("PixelAspectRatio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    String valueString = attrs.getNamedItem("value").getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                    float value = Float.parseFloat(valueString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                    Point p = findIntegerRatio(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                    jfif.resUnits = JPEG.DENSITY_UNIT_ASPECT_RATIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                    jfif.Xdensity = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                    jfif.Xdensity = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                } else if (name.equals("HorizontalPixelSize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                    String valueString = attrs.getNamedItem("value").getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    float value = Float.parseFloat(valueString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    // Convert from mm/dot to dots/cm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    int dpcm = (int) Math.round(1.0/(value*10.0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                    jfif.resUnits = JPEG.DENSITY_UNIT_DOTS_CM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                    jfif.Xdensity = dpcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                } else if (name.equals("VerticalPixelSize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                    String valueString = attrs.getNamedItem("value").getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                    float value = Float.parseFloat(valueString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    // Convert from mm/dot to dots/cm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    int dpcm = (int) Math.round(1.0/(value*10.0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    jfif.resUnits = JPEG.DENSITY_UNIT_DOTS_CM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                    jfif.Ydensity = dpcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * Return a pair of integers whose ratio (x/y) approximates the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * float value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    private static Point findIntegerRatio(float value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        float epsilon = 0.005F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        // Normalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        value = Math.abs(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        // Deal with min case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        if (value <= epsilon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            return new Point(1, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        // Deal with max case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        if (value >= 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            return new Point(255, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        // Remember if we invert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        boolean inverted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        if (value < 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            value = 1.0F/value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            inverted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        // First approximation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        int y = 1;
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20112
diff changeset
  2019
        int x = Math.round(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        float ratio = (float) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        float delta = Math.abs(value - ratio);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        while (delta > epsilon) { // not close enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            // Increment y and compute a new x
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            y++;
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20112
diff changeset
  2026
            x = Math.round(y*value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            ratio = (float)x/(float)y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            delta = Math.abs(value - ratio);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        return inverted ? new Point(y, x) : new Point(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    private void mergeStandardDocumentNode(Node node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        // No-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    private void mergeStandardTextNode(Node node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        // Convert to comments.  For the moment ignore the encoding issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        // Ignore keywords, language, and encoding (for the moment).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        // If compression tag is present, use only entries with "none".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        NodeList children = node.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            Node child = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            NamedNodeMap attrs = child.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            Node comp = attrs.getNamedItem("compression");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            boolean copyIt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                String compString = comp.getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                if (!compString.equals("none")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                    copyIt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            if (copyIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                String value = attrs.getNamedItem("value").getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                COMMarkerSegment com = new COMMarkerSegment(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                insertCOMMarkerSegment(com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    private void mergeStandardTransparencyNode(Node node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        // This might indicate that an alpha channel is being added or removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        // The nodes must appear in order, and a Chroma node will process any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        // transparency, so process it here only if there was no Chroma node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        // Do nothing for stream metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        if (!transparencyDone && !isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            boolean wantAlpha = wantAlpha(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            // do we have alpha already?  If the number of channels is 2 or 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            // we do, as we don't support CMYK, nor can we add alpha to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            // The number of channels can be determined from the SOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            JFIFMarkerSegment jfif = (JFIFMarkerSegment) findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            AdobeMarkerSegment adobe = (AdobeMarkerSegment) findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                (AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            SOFMarkerSegment sof = (SOFMarkerSegment) findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                (SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            SOSMarkerSegment sos = (SOSMarkerSegment) findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                (SOSMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            // We can do nothing for progressive, as we don't know how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            // modify the scans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            if ((sof != null) && (sof.tag == JPEG.SOF2)) { // Progressive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            // Do we already have alpha?  We can tell by the number of channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            // We must have an sof, or we can't do anything further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                int numChannels = sof.componentSpecs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                boolean hadAlpha = (numChannels == 2) || (numChannels == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                // proceed only if the old state and the new state differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                if (hadAlpha != wantAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                    if (wantAlpha) {  // Adding alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                        numChannels++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                        if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                            markerSequence.remove(jfif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                        // If an adobe marker is present, transform must be UNKNOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                        if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                            adobe.transform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                        // Add a component spec with appropriate parameters to SOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        SOFMarkerSegment.ComponentSpec [] newSpecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                            new SOFMarkerSegment.ComponentSpec[numChannels];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        for (int i = 0; i < sof.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                            newSpecs[i] = sof.componentSpecs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                        byte oldFirstID = (byte) sof.componentSpecs[0].componentId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                        byte newID = (byte) ((oldFirstID > 1) ? 'A' : 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                        newSpecs[numChannels-1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                            sof.getComponentSpec(newID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                                sof.componentSpecs[0].HsamplingFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                                sof.componentSpecs[0].QtableSelector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                        sof.componentSpecs = newSpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                        // Add a component spec with appropriate parameters to SOS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                        SOSMarkerSegment.ScanComponentSpec [] newScanSpecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                            new SOSMarkerSegment.ScanComponentSpec [numChannels];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                        for (int i = 0; i < sos.componentSpecs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                            newScanSpecs[i] = sos.componentSpecs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                        newScanSpecs[numChannels-1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                            sos.getScanComponentSpec (newID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                        sos.componentSpecs = newScanSpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                    } else {  // Removing alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                        numChannels--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                        // Remove a component spec from SOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                        SOFMarkerSegment.ComponentSpec [] newSpecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                            new SOFMarkerSegment.ComponentSpec[numChannels];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                        for (int i = 0; i < numChannels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                            newSpecs[i] = sof.componentSpecs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                        sof.componentSpecs = newSpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                        // Remove a component spec from SOS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                        SOSMarkerSegment.ScanComponentSpec [] newScanSpecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                            new SOSMarkerSegment.ScanComponentSpec [numChannels];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        for (int i = 0; i < numChannels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                            newScanSpecs[i] = sos.componentSpecs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                        sos.componentSpecs = newScanSpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    public void setFromTree(String formatName, Node root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        if (formatName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            throw new IllegalArgumentException("null formatName!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            throw new IllegalArgumentException("null root!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        if (isStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            (formatName.equals(JPEG.nativeStreamMetadataFormatName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            setFromNativeTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        } else if (!isStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                   (formatName.equals(JPEG.nativeImageMetadataFormatName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            setFromNativeTree(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        } else if (!isStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                   (formatName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                    (IIOMetadataFormatImpl.standardMetadataFormatName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            // In this case a reset followed by a merge is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            super.setFromTree(formatName, root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            throw  new IllegalArgumentException("Unsupported format name: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                                                + formatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    private void setFromNativeTree(Node root) throws IIOInvalidTreeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        if (resetSequence == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            resetSequence = markerSequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        markerSequence = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        // Build a whole new marker sequence from the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        String name = root.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        if (name != ((isStream) ? JPEG.nativeStreamMetadataFormatName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                                : JPEG.nativeImageMetadataFormatName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            throw new IIOInvalidTreeException("Invalid root node name: " + name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                                              root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        if (!isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            if (root.getChildNodes().getLength() != 2) { // JPEGvariety and markerSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                throw new IIOInvalidTreeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    "JPEGvariety and markerSequence nodes must be present", root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            Node JPEGvariety = root.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            if (JPEGvariety.getChildNodes().getLength() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                markerSequence.add(new JFIFMarkerSegment(JPEGvariety.getFirstChild()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        Node markerSequenceNode = isStream ? root : root.getLastChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        setFromMarkerSequenceNode(markerSequenceNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    void setFromMarkerSequenceNode(Node markerSequenceNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        throws IIOInvalidTreeException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        NodeList children = markerSequenceNode.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        // for all the children, add a marker segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        for (int i = 0; i < children.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            Node node = children.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            String childName = node.getNodeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            if (childName.equals("dqt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                markerSequence.add(new DQTMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            } else if (childName.equals("dht")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                markerSequence.add(new DHTMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            } else if (childName.equals("dri")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                markerSequence.add(new DRIMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            } else if (childName.equals("com")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                markerSequence.add(new COMMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
            } else if (childName.equals("app14Adobe")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                markerSequence.add(new AdobeMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            } else if (childName.equals("unknown")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                markerSequence.add(new MarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            } else if (childName.equals("sof")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                markerSequence.add(new SOFMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            } else if (childName.equals("sos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                markerSequence.add(new SOSMarkerSegment(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                throw new IIOInvalidTreeException("Invalid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    + (isStream ? "stream " : "image ") + "child: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    + childName, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * Check that this metadata object is in a consistent state and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * return <code>true</code> if it is or <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * otherwise.  All the constructors and modifiers should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * this method at the end to guarantee that the data is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * consistent, as the writer relies on this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
    private boolean isConsistent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        SOFMarkerSegment sof =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            (SOFMarkerSegment) findMarkerSegment(SOFMarkerSegment.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                                                 true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                                                  true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        AdobeMarkerSegment adobe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            (AdobeMarkerSegment) findMarkerSegment(AdobeMarkerSegment.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                                                   true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        boolean retval = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        if (!isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                // SOF numBands = total scan bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                int numSOFBands = sof.componentSpecs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                int numScanBands = countScanBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                if (numScanBands != 0) {  // No SOS is OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                    if (numScanBands != numSOFBands) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                        retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                // If JFIF is present, component ids are 1-3, bands are 1 or 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                    if ((numSOFBands != 1) && (numSOFBands != 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                        retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                    for (int i = 0; i < numSOFBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                        if (sof.componentSpecs[i].componentId != i+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                            retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                    // If both JFIF and Adobe are present,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                    // Adobe transform == unknown for gray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                    // YCC for 3-chan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                    if ((adobe != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                        && (((numSOFBands == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                             && (adobe.transform != JPEG.ADOBE_UNKNOWN))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                            || ((numSOFBands == 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                                && (adobe.transform != JPEG.ADOBE_YCC)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                        retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                // stream can't have jfif, adobe, sof, or sos
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                SOSMarkerSegment sos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                    (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                                                         true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                if ((jfif != null) || (adobe != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                    || (sof != null) || (sos != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                    retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * Returns the total number of bands referenced in all SOS marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * segments, including 0 if there are no SOS marker segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
    private int countScanBands() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        List ids = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        while(iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            MarkerSegment seg = (MarkerSegment)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            if (seg instanceof SOSMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                SOSMarkerSegment sos = (SOSMarkerSegment) seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                SOSMarkerSegment.ScanComponentSpec [] specs = sos.componentSpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                for (int i = 0; i < specs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                    Integer id = new Integer(specs[i].componentSelector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                    if (!ids.contains(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                        ids.add(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        return ids.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
    ///// Writer support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
    void writeToStream(ImageOutputStream ios,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                       boolean ignoreJFIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                       boolean forceJFIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                       List thumbnails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                       ICC_Profile iccProfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                       boolean ignoreAdobe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                       int newAdobeTransform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                       JPEGImageWriter writer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        if (forceJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            // Write a default JFIF segment, including thumbnails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            // This won't be duplicated below because forceJFIF will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            // set only if there is no JFIF present already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            JFIFMarkerSegment.writeDefaultJFIF(ios,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                                               thumbnails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                                               iccProfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                                               writer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            if ((ignoreAdobe == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                && (newAdobeTransform != JPEG.ADOBE_IMPOSSIBLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                if ((newAdobeTransform != JPEG.ADOBE_UNKNOWN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                    && (newAdobeTransform != JPEG.ADOBE_YCC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    // Not compatible, so ignore Adobe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    ignoreAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    writer.warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                        (JPEGImageWriter.WARNING_METADATA_ADJUSTED_FOR_THUMB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        // Iterate over each MarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        Iterator iter = markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        while(iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            MarkerSegment seg = (MarkerSegment)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            if (seg instanceof JFIFMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                if (ignoreJFIF == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                    JFIFMarkerSegment jfif = (JFIFMarkerSegment) seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                    jfif.writeWithThumbs(ios, thumbnails, writer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                    if (iccProfile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                        JFIFMarkerSegment.writeICC(iccProfile, ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                } // Otherwise ignore it, as requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            } else if (seg instanceof AdobeMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                if (ignoreAdobe == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                    if (newAdobeTransform != JPEG.ADOBE_IMPOSSIBLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                        AdobeMarkerSegment newAdobe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                            (AdobeMarkerSegment) seg.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                        newAdobe.transform = newAdobeTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                        newAdobe.write(ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                    } else if (forceJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                        // If adobe isn't JFIF compatible, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                        AdobeMarkerSegment adobe = (AdobeMarkerSegment) seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                        if ((adobe.transform == JPEG.ADOBE_UNKNOWN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                            || (adobe.transform == JPEG.ADOBE_YCC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                            adobe.write(ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                            writer.warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                         (JPEGImageWriter.WARNING_METADATA_ADJUSTED_FOR_THUMB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                        seg.write(ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                } // Otherwise ignore it, as requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                seg.write(ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
    //// End of writer support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        if (resetSequence != null) {  // Otherwise no need to reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            markerSequence = resetSequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            resetSequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    public void print() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        for (int i = 0; i < markerSequence.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            MarkerSegment seg = (MarkerSegment) markerSequence.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            seg.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
}