jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageReader.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
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.gif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.ByteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.imageio.ImageReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.imageio.ImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.imageio.spi.ImageReaderSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import com.sun.imageio.plugins.common.ReaderUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class GIFImageReader extends ImageReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // The current ImageInputStream source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    ImageInputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Per-stream settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // True if the file header including stream metadata has been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    boolean gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Global metadata, read once per input setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    GIFStreamMetadata streamMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // The current image index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int currIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Metadata for image at 'currIndex', or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    GIFImageMetadata imageMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // A List of Longs indicating the stream positions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // start of the metadata for each image.  Entries are added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    List imageStartPosition = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Length of metadata for image at 'currIndex', valid only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // imageMetadata != null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    int imageMetadataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // The number of images in the stream, if known, otherwise -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int numImages = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Variables used by the LZW decoding process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    byte[] block = new byte[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int blockLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int bitPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int nextByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    int initCodeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    int clearCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    int eofCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // 32-bit lookahead buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    int next32Bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // Try if the end of the data blocks has been found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // and we are simply draining the 32-bit buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    boolean lastBlockFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // The image to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    BufferedImage theImage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // The image's tile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    WritableRaster theTile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // The image dimensions (from the stream).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    int width = -1, height = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // The pixel currently being decoded (in the stream's coordinates).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    int streamX = -1, streamY = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // The number of rows decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    int rowsDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // The current interlace pass, starting with 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    int interlacePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // End per-stream settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Constants used to control interlacing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static final int[] interlaceIncrement = { 8, 8, 4, 2, -1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static final int[] interlaceOffset = { 0, 4, 2, 1, -1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public GIFImageReader(ImageReaderSpi originatingProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        super(originatingProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // Take input from an ImageInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void setInput(Object input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                         boolean seekForwardOnly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                         boolean ignoreMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        super.setInput(input, seekForwardOnly, ignoreMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (!(input instanceof ImageInputStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    ("input not an ImageInputStream!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            this.stream = (ImageInputStream)input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            this.stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Clear all values based on the previous stream contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        resetStreamSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public int getNumImages(boolean allowSearch) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new IllegalStateException("Input not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (seekForwardOnly && allowSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                ("seekForwardOnly and allowSearch can't both be true!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (numImages > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return numImages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (allowSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            this.numImages = locateImage(Integer.MAX_VALUE) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return numImages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // Throw an IndexOutOfBoundsException if index < minIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // and bump minIndex if required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private void checkIndex(int imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (imageIndex < minIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new IndexOutOfBoundsException("imageIndex < minIndex!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            minIndex = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public int getWidth(int imageIndex) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int index = locateImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (index != imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        readMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return imageMetadata.imageWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int getHeight(int imageIndex) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int index = locateImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (index != imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        readMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return imageMetadata.imageHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public Iterator getImageTypes(int imageIndex) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int index = locateImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (index != imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        readMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        List l = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        byte[] colorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (imageMetadata.localColorTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            colorTable = imageMetadata.localColorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            colorTable = streamMetadata.globalColorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // Normalize color table length to 2^1, 2^2, 2^4, or 2^8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int length = colorTable.length/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            bits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } else if (length == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            bits = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else if (length == 8 || length == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // Bump from 3 to 4 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            bits = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // Bump to 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            bits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int lutLength = 1 << bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        byte[] r = new byte[lutLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        byte[] g = new byte[lutLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        byte[] b = new byte[lutLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // Entries from length + 1 to lutLength - 1 will be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int rgbIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            r[i] = colorTable[rgbIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            g[i] = colorTable[rgbIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            b[i] = colorTable[rgbIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        byte[] a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (imageMetadata.transparentColorFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            a = new byte[lutLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            Arrays.fill(a, (byte)255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // Some files erroneously have a transparent color index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // of 255 even though there are fewer than 256 colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int idx = Math.min(imageMetadata.transparentColorIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                               lutLength - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            a[idx] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int[] bitsPerSample = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        bitsPerSample[0] = bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        l.add(ImageTypeSpecifier.createIndexed(r, g, b, a, bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                               DataBuffer.TYPE_BYTE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return l.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public ImageReadParam getDefaultReadParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return new ImageReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public IIOMetadata getStreamMetadata() throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return streamMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public IIOMetadata getImageMetadata(int imageIndex) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int index = locateImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (index != imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new IndexOutOfBoundsException("Bad image index!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        readMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return imageMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // BEGIN LZW STUFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private void initNext32Bits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        next32Bits = block[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        next32Bits |= (block[1] & 0xff) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        next32Bits |= (block[2] & 0xff) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        next32Bits |= block[3] << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        nextByte = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Load a block (1-255 bytes) at a time, and maintain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // a 32-bit lookahead buffer that is filled from the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    // and extracted from the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // When the last block is found, we continue to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private int getCode(int codeSize, int codeMask) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (bitPos + codeSize > 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return eofCode; // No more data available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int code = (next32Bits >> bitPos) & codeMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        bitPos += codeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Shift in a byte of new data at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        while (bitPos >= 8 && !lastBlockFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            next32Bits >>>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            bitPos -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // Check if current block is out of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (nextByte >= blockLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                // Get next block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                blockLength = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (blockLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    lastBlockFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    int left = blockLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    while (left > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        int nbytes = stream.read(block, off, left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        left -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    nextByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            next32Bits |= block[nextByte++] << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public void initializeStringTable(int[] prefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                      byte[] suffix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                      byte[] initial,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                      int[] length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int numEntries = 1 << initCodeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            prefix[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            suffix[i] = (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            initial[i] = (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            length[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // Fill in the entire table for robustness against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // out-of-sequence codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        for (int i = numEntries; i < 4096; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            prefix[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            length[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // tableIndex = numEntries + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // codeSize = initCodeSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // codeMask = (1 << codeSize) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    Rectangle sourceRegion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    int sourceXSubsampling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    int sourceYSubsampling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    int sourceMinProgressivePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    int sourceMaxProgressivePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    Point destinationOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    Rectangle destinationRegion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    // Used only if IIOReadUpdateListeners are present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    int updateMinY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    int updateYStep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    boolean decodeThisRow = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    int destY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    byte[] rowBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private void outputRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Clip against ImageReadParam
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int width = Math.min(sourceRegion.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                             destinationRegion.width*sourceXSubsampling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int destX = destinationRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (sourceXSubsampling == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            theTile.setDataElements(destX, destY, width, 1, rowBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            for (int x = 0; x < width; x += sourceXSubsampling, destX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                theTile.setSample(destX, destY, 0, rowBuf[x] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // Update IIOReadUpdateListeners, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (updateListeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            int[] bands = { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            // updateYStep will have been initialized if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // updateListeners is non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            processImageUpdate(theImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                               destX, destY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                               width, 1, 1, updateYStep,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                               bands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private void computeDecodeThisRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        this.decodeThisRow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            (destY < destinationRegion.y + destinationRegion.height) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            (streamY >= sourceRegion.y) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            (streamY < sourceRegion.y + sourceRegion.height) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            (((streamY - sourceRegion.y) % sourceYSubsampling) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private void outputPixels(byte[] string, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (interlacePass < sourceMinProgressivePass ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            interlacePass > sourceMaxProgressivePass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (streamX >= sourceRegion.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                rowBuf[streamX - sourceRegion.x] = string[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // Process end-of-row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            ++streamX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (streamX == width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                // Update IIOReadProgressListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                ++rowsDone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                processImageProgress(100.0F*rowsDone/height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                if (decodeThisRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    outputRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                streamX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                if (imageMetadata.interlaceFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    streamY += interlaceIncrement[interlacePass];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    if (streamY >= height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        // Inform IIOReadUpdateListeners of end of pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        if (updateListeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            processPassComplete(theImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        ++interlacePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        if (interlacePass > sourceMaxProgressivePass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        streamY = interlaceOffset[interlacePass];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        startPass(interlacePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    ++streamY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                // Determine whether pixels from this row will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                // be written to the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                this.destY = destinationRegion.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    (streamY - sourceRegion.y)/sourceYSubsampling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                computeDecodeThisRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    // END LZW STUFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private void readHeader() throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (gotHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throw new IllegalStateException("Input not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // Create an object to store the stream metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        this.streamMetadata = new GIFStreamMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            byte[] signature = new byte[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            stream.readFully(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            StringBuffer version = new StringBuffer(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            version.append((char)signature[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            version.append((char)signature[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            version.append((char)signature[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            streamMetadata.version = version.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            streamMetadata.logicalScreenWidth = stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            streamMetadata.logicalScreenHeight = stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int packedFields = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            boolean globalColorTableFlag = (packedFields & 0x80) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            streamMetadata.colorResolution = ((packedFields >> 4) & 0x7) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            streamMetadata.sortFlag = (packedFields & 0x8) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            int numGCTEntries = 1 << ((packedFields & 0x7) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            streamMetadata.backgroundColorIndex = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            streamMetadata.pixelAspectRatio = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (globalColorTableFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                streamMetadata.globalColorTable = new byte[3*numGCTEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                stream.readFully(streamMetadata.globalColorTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                streamMetadata.globalColorTable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // Found position of metadata for image 0
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   507
            imageStartPosition.add(Long.valueOf(stream.getStreamPosition()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            throw new IIOException("I/O error reading header!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        gotHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private boolean skipImage() throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Stream must be at the beginning of an image descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        // upon exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                int blockType = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (blockType == 0x2c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    stream.skipBytes(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    int packedFields = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    if ((packedFields & 0x80) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        // Skip color table if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        int bits = (packedFields & 0x7) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        stream.skipBytes(3*(1 << bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    stream.skipBytes(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        length = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        stream.skipBytes(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    } while (length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                } else if (blockType == 0x3b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                } else if (blockType == 0x21) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    int label = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        length = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        stream.skipBytes(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    } while (length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                } else if (blockType == 0x0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        length = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        stream.skipBytes(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    } while (length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } catch (EOFException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            throw new IIOException("I/O error locating image!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    private int locateImage(int imageIndex) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            // Find closest known index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            int index = Math.min(imageIndex, imageStartPosition.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            // Seek to that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            Long l = (Long)imageStartPosition.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            stream.seek(l.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            // Skip images until at desired index or last image found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            while (index < imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                if (!skipImage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    --index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                Long l1 = new Long(stream.getStreamPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                imageStartPosition.add(l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                ++index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            throw new IIOException("Couldn't seek!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (currIndex != imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            imageMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        currIndex = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    // Read blocks of 1-255 bytes, stop at a 0-length block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private byte[] concatenateBlocks() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        byte[] data = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            int length = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            byte[] newData = new byte[data.length + length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            System.arraycopy(data, 0, newData, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            stream.readFully(newData, data.length, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            data = newData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    // Stream must be positioned at start of metadata for 'currIndex'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    private void readMetadata() throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            throw new IllegalStateException("Input not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // Create an object to store the image metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            this.imageMetadata = new GIFImageMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            long startPosition = stream.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                int blockType = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                if (blockType == 0x2c) { // Image Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    imageMetadata.imageLeftPosition =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                        stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    imageMetadata.imageTopPosition =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    imageMetadata.imageWidth = stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    imageMetadata.imageHeight = stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    int idPackedFields = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    boolean localColorTableFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        (idPackedFields & 0x80) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    imageMetadata.interlaceFlag = (idPackedFields & 0x40) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    imageMetadata.sortFlag = (idPackedFields & 0x20) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    int numLCTEntries = 1 << ((idPackedFields & 0x7) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    if (localColorTableFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        // Read color table if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        imageMetadata.localColorTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                            new byte[3*numLCTEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        stream.readFully(imageMetadata.localColorTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        imageMetadata.localColorTable = 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
                    // Record length of this metadata block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    this.imageMetadataLength =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        (int)(stream.getStreamPosition() - startPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    // Now positioned at start of LZW-compressed pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                } else if (blockType == 0x21) { // Extension block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    int label = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    if (label == 0xf9) { // Graphics Control Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        int gceLength = stream.readUnsignedByte(); // 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        int gcePackedFields = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        imageMetadata.disposalMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            (gcePackedFields >> 2) & 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        imageMetadata.userInputFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                            (gcePackedFields & 0x2) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        imageMetadata.transparentColorFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                            (gcePackedFields & 0x1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        imageMetadata.delayTime = stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        imageMetadata.transparentColorIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        int terminator = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    } else if (label == 0x1) { // Plain text extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        int length = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        imageMetadata.hasPlainTextExtension = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        imageMetadata.textGridLeft =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        imageMetadata.textGridTop =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        imageMetadata.textGridWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        imageMetadata.textGridHeight =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                            stream.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        imageMetadata.characterCellWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                            stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        imageMetadata.characterCellHeight =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        imageMetadata.textForegroundColor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        imageMetadata.textBackgroundColor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                            stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        imageMetadata.text = concatenateBlocks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    } else if (label == 0xfe) { // Comment extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        byte[] comment = concatenateBlocks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        if (imageMetadata.comments == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                            imageMetadata.comments = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        imageMetadata.comments.add(comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    } else if (label == 0xff) { // Application extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        int blockSize = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        byte[] applicationID = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        byte[] authCode = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        // read available data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        byte[] blockData = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        stream.readFully(blockData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        int offset = copyData(blockData, 0, applicationID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        offset = copyData(blockData, offset, authCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        byte[] applicationData = concatenateBlocks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        if (offset < blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            int len = blockSize - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            byte[] data =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                                new byte[len + applicationData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                            System.arraycopy(blockData, offset, data, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                            System.arraycopy(applicationData, 0, data, len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                             applicationData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                            applicationData = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        // Init lists if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                        if (imageMetadata.applicationIDs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                            imageMetadata.applicationIDs = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            imageMetadata.authenticationCodes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                            imageMetadata.applicationData = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        imageMetadata.applicationIDs.add(applicationID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        imageMetadata.authenticationCodes.add(authCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        imageMetadata.applicationData.add(applicationData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                        // Skip over unknown extension blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                            length = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                            stream.skipBytes(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                        } while (length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                } else if (blockType == 0x3b) { // Trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        ("Attempt to read past end of image sequence!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    throw new IIOException("Unexpected block type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                           blockType + "!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        } catch (IIOException iioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            throw iioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            throw new IIOException("I/O error reading image metadata!", ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private int copyData(byte[] src, int offset, byte[] dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        int len = dst.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        int rest = src.length - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (len > rest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            len = rest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        System.arraycopy(src, offset, dst, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return offset + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private void startPass(int pass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (updateListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        int yStep = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (imageMetadata.interlaceFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            y = interlaceOffset[interlacePass];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            yStep = interlaceIncrement[interlacePass];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        int[] vals = ReaderUtil.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            computeUpdatedPixels(sourceRegion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                 destinationOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                                 destinationRegion.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                 destinationRegion.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                 destinationRegion.x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                 destinationRegion.width - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                 destinationRegion.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                                 destinationRegion.height - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                 sourceXSubsampling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                 sourceYSubsampling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                 y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                 destinationRegion.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                 (destinationRegion.height + yStep - 1)/yStep,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                 yStep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // Initialized updateMinY and updateYStep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        this.updateMinY = vals[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        this.updateYStep = vals[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        // Inform IIOReadUpdateListeners of new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        int[] bands = { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        processPassStarted(theImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                           interlacePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                           sourceMinProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                           sourceMaxProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                           0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                           updateMinY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                           1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                           updateYStep,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                           bands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public BufferedImage read(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            throw new IllegalStateException("Input not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        int index = locateImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (index != imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            throw new IndexOutOfBoundsException("imageIndex out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        clearAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        readMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // A null ImageReadParam means we use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (param == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            param = getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        // Initialize the destination image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        Iterator imageTypes = getImageTypes(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        this.theImage = getDestination(param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                       imageTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                                       imageMetadata.imageWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                       imageMetadata.imageHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        this.theTile = theImage.getWritableTile(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        this.width = imageMetadata.imageWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        this.height = imageMetadata.imageHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        this.streamX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        this.streamY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        this.rowsDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        this.interlacePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // Get source region, taking subsampling offsets into account,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // and clipping against the true source bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        this.sourceRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        this.destinationRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        computeRegions(param, width, height, theImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                       sourceRegion, destinationRegion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        this.destinationOffset = new Point(destinationRegion.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                                           destinationRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        this.sourceXSubsampling = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        this.sourceYSubsampling = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        this.sourceMinProgressivePass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            Math.max(param.getSourceMinProgressivePass(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        this.sourceMaxProgressivePass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            Math.min(param.getSourceMaxProgressivePass(), 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        this.destY = destinationRegion.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            (streamY - sourceRegion.y)/sourceYSubsampling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        computeDecodeThisRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        // Inform IIOReadProgressListeners of start of image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        processImageStarted(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        startPass(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        this.rowBuf = new byte[width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            // Read and decode the image data, fill in theImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            this.initCodeSize = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            // Read first data block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            this.blockLength = stream.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            int left = blockLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            while (left > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                int nbytes = stream.read(block, off, left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                left -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            this.bitPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            this.nextByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            this.lastBlockFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            this.interlacePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            // Init 32-bit buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            initNext32Bits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            this.clearCode = 1 << initCodeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            this.eofCode = clearCode + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            int code, oldCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            int[] prefix = new int[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            byte[] suffix = new byte[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            byte[] initial = new byte[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            int[] length = new int[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            byte[] string = new byte[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            initializeStringTable(prefix, suffix, initial, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            int tableIndex = (1 << initCodeSize) + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            int codeSize = initCodeSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            int codeMask = (1 << codeSize) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            while (!abortRequested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                code = getCode(codeSize, codeMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                if (code == clearCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    initializeStringTable(prefix, suffix, initial, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    tableIndex = (1 << initCodeSize) + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    codeSize = initCodeSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    codeMask = (1 << codeSize) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    code = getCode(codeSize, codeMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    if (code == eofCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        // Inform IIOReadProgressListeners of end of image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                        processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        return theImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                } else if (code == eofCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    // Inform IIOReadProgressListeners of end of image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    return theImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    int newSuffixIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    if (code < tableIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        newSuffixIndex = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    } else { // code == tableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                        newSuffixIndex = oldCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                        if (code != tableIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                            // warning - code out of sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                            // possibly data corruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                            processWarningOccurred("Out-of-sequence code!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    int ti = tableIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    int oc = oldCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    prefix[ti] = oc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    suffix[ti] = initial[newSuffixIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    initial[ti] = initial[oc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    length[ti] = length[oc] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    ++tableIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    if ((tableIndex == (1 << codeSize)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                        (tableIndex < 4096)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                        ++codeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        codeMask = (1 << codeSize) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                // Reverse code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                int c = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                int len = length[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                for (int i = len - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    string[i] = suffix[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    c = prefix[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                outputPixels(string, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                oldCode = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            return theImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            throw new IIOException("I/O error reading image!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Remove all settings including global settings such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * <code>Locale</code>s and listeners, as well as stream settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        resetStreamSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Remove local settings based on parsing of a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    private void resetStreamSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        streamMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        currIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        imageMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        imageStartPosition = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        numImages = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        // No need to reinitialize 'block'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        blockLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        bitPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        nextByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        next32Bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        lastBlockFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        theImage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        theTile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        width = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        height = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        streamX = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        streamY = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        rowsDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        interlacePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
}