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