jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageReader.java
author bae
Tue, 26 Feb 2013 00:41:40 +0400
changeset 16881 f105ea43e3aa
parent 14342 8435a30053c1
child 20798 d03e6abc2b51
permissions -rw-r--r--
8007667: Better image reading Reviewed-by: prr, jgodinez, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12559
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3013
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3013
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3013
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3013
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3013
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.imageio.plugins.jpeg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.imageio.ImageReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.imageio.ImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.imageio.spi.ImageReaderSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.imageio.plugins.jpeg.JPEGImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.imageio.plugins.jpeg.JPEGQTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.color.ICC_Profile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.color.ICC_ColorSpace;
3013
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
    44
import java.awt.color.CMMException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.image.ColorConvertOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.ArrayList;
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
    57
import java.util.NoSuchElementException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.java2d.Disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.java2d.DisposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class JPEGImageReader extends ImageReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The following variable contains a pointer to the IJG library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * structure for this reader.  It is assigned in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * and then is passed in to every native call.  It is set to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * by dispose to avoid disposing twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private long structPointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** The input stream we read from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private ImageInputStream iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * List of stream positions for images, reinitialized every time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * a new input source is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private List imagePositions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The number of images in the stream, or 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private int numImages = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    90
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    91
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    92
                    System.loadLibrary("jpeg");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    93
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    94
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    95
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        initReaderIDs(ImageInputStream.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                      JPEGQTable.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                      JPEGHuffmanTable.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // The following warnings are converted to strings when used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // as keys to get localized resources from JPEGImageReaderResources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // and its children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Warning code to be passed to warningOccurred to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * that the EOI marker is missing from the end of the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * This usually signals that the stream is corrupted, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * everything up to the last MCU should be usable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected static final int WARNING_NO_EOI = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Warning code to be passed to warningOccurred to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * that a JFIF segment was encountered inside a JFXX JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * thumbnail and is being ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected static final int WARNING_NO_JFIF_IN_THUMB = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Warning code to be passed to warningOccurred to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * that embedded ICC profile is invalid and will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected static final int WARNING_IGNORE_INVALID_ICC = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final int MAX_WARNING = WARNING_IGNORE_INVALID_ICC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Image index of image for which header information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private int currentImage = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // The following is copied out from C after reading the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // Unlike metadata, which may never be retrieved, we need this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // if we are to read an image at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /** Set by setImageData native code callback */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /** Set by setImageData native code callback */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Set by setImageData native code callback.  A modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * IJG+NIFTY colorspace code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private int colorSpaceCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Set by setImageData native code callback.  A modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * IJG+NIFTY colorspace code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private int outColorSpaceCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /** Set by setImageData native code callback */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private int numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /** Set by setImageData native code callback */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private ColorSpace iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /** If we need to post-convert in Java, convert with this op */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private ColorConvertOp convert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /** The image we are going to fill */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private BufferedImage image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /** An intermediate Raster to hold decoded data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private WritableRaster raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /** A view of our target Raster that we can setRect to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private WritableRaster target = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /** The databuffer for the above Raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private DataBufferByte buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** The region in the destination where we will write pixels */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private Rectangle destROI = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /** The list of destination bands, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private int [] destinationBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /** Stream metadata, cached, even when the stream is changed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private JPEGMetadata streamMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /** Image metadata, valid for the imageMetadataIndex only. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private JPEGMetadata imageMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private int imageMetadataIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Set to true every time we seek in the stream; used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * invalidate the native buffer contents in C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private boolean haveSeeked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Tables that have been read from a tables-only image at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * beginning of a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private JPEGQTable [] abbrevQTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private JPEGHuffmanTable[] abbrevDCHuffmanTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private JPEGHuffmanTable[] abbrevACHuffmanTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private int minProgressivePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private int maxProgressivePass = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Variables used by progress monitoring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private static final int UNKNOWN = -1;  // Number of passes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private static final int MIN_ESTIMATED_PASSES = 10; // IJG default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private int knownPassCount = UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private int pass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private float percentToDate = 0.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private float previousPassPercentage = 0.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private int progInterval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Set to true once stream has been checked for stream metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private boolean tablesOnlyChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /** The referent to be registered with the Disposer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private Object disposerReferent = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /** The DisposerRecord that handles the actual disposal of this reader. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private DisposerRecord disposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /** Sets up static C structures. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static native void initReaderIDs(Class iisClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                             Class qTableClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                             Class huffClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public JPEGImageReader(ImageReaderSpi originator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        super(originator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        structPointer = initJPEGImageReader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        disposerRecord = new JPEGReaderDisposerRecord(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Disposer.addRecord(disposerReferent, disposerRecord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /** Sets up per-reader C structure and returns a pointer to it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private native long initJPEGImageReader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Called by the native code or other classes to signal a warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The code is used to lookup a localized message to be used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * sending warnings to listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    protected void warningOccurred(int code) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   246
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   247
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   248
            if ((code < 0) || (code > MAX_WARNING)){
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   249
                throw new InternalError("Invalid warning index");
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   250
            }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   251
            processWarningOccurred
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   252
                ("com.sun.imageio.plugins.jpeg.JPEGImageReaderResources",
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   253
                 Integer.toString(code));
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   254
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   255
            cbLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * The library has it's own error facility that emits warning messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * This routine is called by the native code when it has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * formatted a string for output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * XXX  For truly complete localization of all warning messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * the sun_jpeg_output_message routine in the native code should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * send only the codes and parameters to a method here in Java,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * which will then format and send the warnings, using localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * strings.  This method will have to deal with all the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * and formats (%u with possibly large numbers, %02d, %02x, etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * that actually occur in the JPEG library.  For now, this prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * library warnings from being printed to stderr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    protected void warningWithMessage(String msg) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   273
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   274
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   275
            processWarningOccurred(msg);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   276
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   277
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   278
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public void setInput(Object input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                         boolean seekForwardOnly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                         boolean ignoreMetadata)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   287
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   288
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            super.setInput(input, seekForwardOnly, ignoreMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            this.ignoreMetadata = ignoreMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            resetInternalState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            iis = (ImageInputStream) input; // Always works
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   293
            setSource(structPointer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   299
    /**
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   300
     * This method is called from native code in order to fill
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   301
     * native input buffer.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   302
     *
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   303
     * We block any attempt to change the reading state during this
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   304
     * method, in order to prevent a corruption of the native decoder
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   305
     * state.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   306
     *
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   307
     * @return number of bytes read from the stream.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   308
     */
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   309
    private int readInputData(byte[] buf, int off, int len) throws IOException {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   310
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   311
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   312
            return iis.read(buf, off, len);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   313
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   314
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   315
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   316
    }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   317
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   318
    /**
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   319
     * This method is called from the native code in order to
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   320
     * skip requested number of bytes in the input stream.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   321
     *
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   322
     * @param n
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   323
     * @return
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   324
     * @throws IOException
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   325
     */
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   326
    private long skipInputBytes(long n) throws IOException {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   327
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   328
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   329
            return iis.skipBytes(n);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   330
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   331
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   332
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   333
    }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   334
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   335
    private native void setSource(long structPointer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private void checkTablesOnly() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            System.out.println("Checking for tables-only image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        long savePos = iis.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            System.out.println("saved pos is " + savePos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            System.out.println("length is " + iis.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // Read the first header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        boolean tablesOnly = readNativeHeader(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (tablesOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                System.out.println("tables-only image found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                long pos = iis.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                System.out.println("pos after return from native is " + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // This reads the tables-only image twice, once from C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            // and once from Java, but only if ignoreMetadata is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (ignoreMetadata == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                iis.seek(savePos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                haveSeeked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                streamMetadata = new JPEGMetadata(true, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                                  iis, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                long pos = iis.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        ("pos after constructing stream metadata is " + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // Now we are at the first image if there are any, so add it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (hasNextImage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                imagePositions.add(new Long(iis.getStreamPosition()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else { // Not tables only, so add original pos to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            imagePositions.add(new Long(savePos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // And set current image since we've read it now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            currentImage = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            Long pos = (Long) imagePositions.get(imagePositions.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            iis.flushBefore(pos.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        tablesOnlyChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public int getNumImages(boolean allowSearch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        try { // locked thread
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   387
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   388
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return getNumImagesOnThread(allowSearch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private int getNumImagesOnThread(boolean allowSearch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (numImages != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return numImages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new IllegalStateException("Input not set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (allowSearch == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    "seekForwardOnly and allowSearch can't both be true!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            // Otherwise we have to read the entire stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (!tablesOnlyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                checkTablesOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            iis.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            gotoImage(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            JPEGBuffer buffer = new JPEGBuffer(iis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            buffer.loadBuf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                done = buffer.scanForFF(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                switch (buffer.buf[buffer.bufPtr] & 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                case JPEG.SOI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    numImages++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    // FALL THROUGH to decrement buffer vars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    // This first set doesn't have a length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                case 0: // not a marker, just a data 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                case JPEG.RST0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                case JPEG.RST1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                case JPEG.RST2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                case JPEG.RST3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                case JPEG.RST4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                case JPEG.RST5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                case JPEG.RST6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                case JPEG.RST7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                case JPEG.EOI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    buffer.bufPtr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    // All the others have a length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    buffer.bufPtr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    buffer.loadBuf(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    int length = ((buffer.buf[buffer.bufPtr++] & 0xff) << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        (buffer.buf[buffer.bufPtr++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    buffer.bufAvail -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    length -= 2; // length includes itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    buffer.skipData(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return numImages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return -1;  // Search is necessary for JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Sets the input stream to the start of the requested image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @exception IllegalStateException if the input source has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @exception IndexOutOfBoundsException if the supplied index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private void gotoImage(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throw new IllegalStateException("Input not set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (imageIndex < minIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (!tablesOnlyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            checkTablesOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (imageIndex < imagePositions.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            iis.seek(((Long)(imagePositions.get(imageIndex))).longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            // read to start of image, saving positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            // First seek to the last position we already have, and skip the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // entire image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            Long pos = (Long) imagePositions.get(imagePositions.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            iis.seek(pos.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            skipImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            // Now add all intervening positions, skipping images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            for (int index = imagePositions.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                 index <= imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                 index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                // Is there an image?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (!hasNextImage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                pos = new Long(iis.getStreamPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                imagePositions.add(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    iis.flushBefore(pos.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                if (index < imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    skipImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }  // Otherwise we are where we want to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            minIndex = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        haveSeeked = true;  // No way is native buffer still valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Skip over a complete image in the stream, leaving the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * positioned such that the next byte to be read is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * byte of the next image.  For JPEG, this means that we read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * until we encounter an EOI marker or until the end of the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * If the stream ends before an EOI marker is encountered, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * IndexOutOfBoundsException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private void skipImage() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            System.out.println("skipImage called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        boolean foundFF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        for (int byteval = iis.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
             byteval != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             byteval = iis.read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (foundFF == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                if (byteval == JPEG.EOI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            foundFF = (byteval == 0xff) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Returns <code>true</code> if there is an image beyond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * the current stream position.  Does not disturb the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * stream position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    private boolean hasNextImage() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            System.out.print("hasNextImage called; returning ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        iis.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        boolean foundFF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        for (int byteval = iis.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             byteval != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
             byteval = iis.read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (foundFF == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (byteval == JPEG.SOI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        System.out.println("true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            foundFF = (byteval == 0xff) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // We hit the end of the stream before we hit an SOI, so no image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            System.out.println("false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Push back the given number of bytes to the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Called by the native code at the end of each image so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * that the next one can be identified from Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private void pushBack(int num) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            System.out.println("pushing back " + num + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   588
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   589
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   590
            iis.seek(iis.getStreamPosition()-num);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   591
            // The buffer is clear after this, so no need to set haveSeeked.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   592
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   593
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   594
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Reads header information for the given image, if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    private void readHeader(int imageIndex, boolean reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        gotoImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        readNativeHeader(reset); // Ignore return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        currentImage = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private boolean readNativeHeader(boolean reset) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        boolean retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        retval = readImageHeader(structPointer, haveSeeked, reset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        haveSeeked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Read in the header information starting from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * stream position, returning <code>true</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * header was a tables-only image.  After this call, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * native IJG decompression struct will contain the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * information required by most query calls below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * (e.g. getWidth, getHeight, etc.), if the header was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * a tables-only image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * If reset is <code>true</code>, the state of the IJG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * object is reset so that it can read a header again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * This happens automatically if the header was a tables-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private native boolean readImageHeader(long structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                           boolean clearBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                           boolean reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Called by the native code whenever an image header has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * read.  Whether we read metadata or not, we always need this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * information, so it is passed back independently of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * metadata, which may never be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    private void setImageData(int width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                              int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                              int colorSpaceCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                              int outColorSpaceCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                              int numComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                              byte [] iccData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        this.colorSpaceCode = colorSpaceCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        this.outColorSpaceCode = outColorSpaceCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        this.numComponents = numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if (iccData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        ICC_Profile newProfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            newProfile = ICC_Profile.getInstance(iccData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
             * Color profile data seems to be invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
             * Ignore this profile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            warningOccurred(WARNING_IGNORE_INVALID_ICC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        byte[] newData = newProfile.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        ICC_Profile oldProfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (iccCS instanceof ICC_ColorSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            oldProfile = ((ICC_ColorSpace)iccCS).getProfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        byte[] oldData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (oldProfile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            oldData = oldProfile.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         * At the moment we can't rely on the ColorSpace.equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * and ICC_Profile.equals() because they do not detect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * the case when two profiles are created from same data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         * So, we have to do data comparison in order to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * creation of different ColorSpace instances for the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         * embedded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (oldData == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            !java.util.Arrays.equals(oldData, newData))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            iccCS = new ICC_ColorSpace(newProfile);
3013
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   692
            // verify new color space
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   693
            try {
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   694
                float[] colors = iccCS.fromRGB(new float[] {1f, 0f, 0f});
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   695
            } catch (CMMException e) {
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   696
                /*
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   697
                 * Embedded profile seems to be corrupted.
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   698
                 * Ignore this profile.
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   699
                 */
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   700
                iccCS = null;
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   701
                cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   702
                try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   703
                    warningOccurred(WARNING_IGNORE_INVALID_ICC);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   704
                } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   705
                    cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   706
                }
3013
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   707
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public int getWidth(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   715
                cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    public int getHeight(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   728
                cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /////////// Color Conversion and Image Types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * Return an ImageTypeSpecifier corresponding to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * color space code, or null if the color space is unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   743
    private ImageTypeProducer getImageType(int code) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   744
        ImageTypeProducer ret = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if ((code > 0) && (code < JPEG.NUM_JCS_CODES)) {
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   747
            ret = ImageTypeProducer.getTypeProducer(code);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public ImageTypeSpecifier getRawImageType(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   757
                cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   758
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // Returns null if it can't be represented
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   763
            return getImageType(colorSpaceCode).getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public Iterator getImageTypes(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            return getImageTypesOnThread(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    private Iterator getImageTypesOnThread(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   782
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        // We return an iterator containing the default, any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        // conversions that the library provides, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        // all the other default types with the same number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        // of components, as we can do these as a post-process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        // As we convert Rasters rather than images, images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        // with alpha cannot be converted in a post-process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        // If this image can't be interpreted, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        // returns an empty Iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        // Get the raw ITS, if there is one.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // won't always be the same as the default.
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   798
        ImageTypeProducer raw = getImageType(colorSpaceCode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // Given the encoded colorspace, build a list of ITS's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        // representing outputs you could handle starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        // with the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   804
        ArrayList<ImageTypeProducer> list = new ArrayList<ImageTypeProducer>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        switch (colorSpaceCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        case JPEG.JCS_GRAYSCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            list.add(getImageType(JPEG.JCS_RGB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        case JPEG.JCS_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            list.add(getImageType(JPEG.JCS_GRAYSCALE));
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   814
            list.add(getImageType(JPEG.JCS_YCC));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        case JPEG.JCS_RGBA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        case JPEG.JCS_YCC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (raw != null) {  // Might be null if PYCC.pf not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                list.add(getImageType(JPEG.JCS_RGB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        case JPEG.JCS_YCCA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (raw != null) {  // Might be null if PYCC.pf not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        case JPEG.JCS_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            // As there is no YCbCr ColorSpace, we can't support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            // the raw type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            // due to 4705399, use RGB as default in order to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            // slowing down of drawing operations with result image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            list.add(getImageType(JPEG.JCS_RGB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if (iccCS != null) {
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   839
                list.add(new ImageTypeProducer() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   840
                    protected ImageTypeSpecifier produce() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   841
                        return ImageTypeSpecifier.createInterleaved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                         (iccCS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                          JPEG.bOffsRGB,  // Assume it's for RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                          DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                          false,
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   846
                          false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   847
                    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   848
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            list.add(getImageType(JPEG.JCS_GRAYSCALE));
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   853
            list.add(getImageType(JPEG.JCS_YCC));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        case JPEG.JCS_YCbCrA:  // Default is to convert to RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            // As there is no YCbCr ColorSpace, we can't support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // the raw type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            list.add(getImageType(JPEG.JCS_RGBA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   862
        return new ImageTypeIterator(list.iterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Checks the implied color conversion between the stream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * the target image, altering the IJG output color space if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * If a java color conversion is required, then this sets up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <code>convert</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * If bands are being rearranged at all (either source or destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * bands are specified in the param), then the default color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * conversions are assumed to be correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Throws an IIOException if there is no conversion available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    private void checkColorConversion(BufferedImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                                      ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        // If we are rearranging channels at all, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        // conversions remain in place.  If the user wants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        // raw channels then he should do this while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        // a Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            if ((param.getSourceBands() != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                (param.getDestinationBands() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                // Accept default conversions out of decoder, silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        // XXX - We do not currently support any indexed color models,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        // though we could, as IJG will quantize for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        // This is a performance and memory-use issue, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // users can read RGB and then convert to indexed in Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        ColorModel cm = image.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (cm instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            throw new IIOException("IndexColorModel not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        // Now check the ColorSpace type against outColorSpaceCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        // We may want to tweak the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        ColorSpace cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        int csType = cs.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        convert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        switch (outColorSpaceCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        case JPEG.JCS_GRAYSCALE:  // Its gray in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if  (csType == ColorSpace.TYPE_RGB) { // We want RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                // IJG can do this for us more efficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                setOutColorSpace(structPointer, JPEG.JCS_RGB);
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   912
                // Update java state according to changes
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   913
                // in the native part of decoder.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   914
                outColorSpaceCode = JPEG.JCS_RGB;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   915
                numComponents = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            } else if (csType != ColorSpace.TYPE_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        case JPEG.JCS_RGB:  // IJG wants to go to RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            if (csType ==  ColorSpace.TYPE_GRAY) {  // We want gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                if (colorSpaceCode == JPEG.JCS_YCbCr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                    // If the jpeg space is YCbCr, IJG can do it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    setOutColorSpace(structPointer, JPEG.JCS_GRAYSCALE);
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   925
                    // Update java state according to changes
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   926
                    // in the native part of decoder.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   927
                    outColorSpaceCode = JPEG.JCS_GRAYSCALE;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   928
                    numComponents = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            } else if ((iccCS != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                       (cm.getNumComponents() == numComponents) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                       (cs != iccCS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                // We have an ICC profile but it isn't used in the dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                // image.  So convert from the profile cs to the target cs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                convert = new ColorConvertOp(iccCS, cs, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                // Leave IJG conversion in place; we still need it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            } else if ((iccCS == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                       (!cs.isCS_sRGB()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                       (cm.getNumComponents() == numComponents)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                // Target isn't sRGB, so convert from sRGB to the target
2381
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   941
                convert = new ColorConvertOp(JPEG.JCS.sRGB, cs, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            } else if (csType != ColorSpace.TYPE_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        case JPEG.JCS_RGBA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            // No conversions available; image must be RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if ((csType != ColorSpace.TYPE_RGB) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                (cm.getNumComponents() != numComponents)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        case JPEG.JCS_YCC:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   954
            {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   955
                ColorSpace YCC = JPEG.JCS.getYCC();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   956
                if (YCC == null) { // We can't do YCC at all
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   957
                    throw new IIOException("Incompatible color conversion");
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   958
                }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   959
                if ((cs != YCC) &&
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   960
                    (cm.getNumComponents() == numComponents)) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   961
                    convert = new ColorConvertOp(YCC, cs, null);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   962
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        case JPEG.JCS_YCCA:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   966
            {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   967
                ColorSpace YCC = JPEG.JCS.getYCC();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   968
                // No conversions available; image must be YCCA
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   969
                if ((YCC == null) || // We can't do YCC at all
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   970
                    (cs != YCC) ||
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   971
                    (cm.getNumComponents() != numComponents)) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   972
                    throw new IIOException("Incompatible color conversion");
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   973
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            // Anything else we can't handle at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Set the IJG output space to the given value.  The library will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * perform the appropriate colorspace conversions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    private native void setOutColorSpace(long structPointer, int id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /////// End of Color Conversion & Image Types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    public ImageReadParam getDefaultReadParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        return new JPEGImageReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public IIOMetadata getStreamMetadata() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            if (!tablesOnlyChecked) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   998
                cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                checkTablesOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            return streamMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public IIOMetadata getImageMetadata(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            // imageMetadataIndex will always be either a valid index or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            // -1, in which case imageMetadata will not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            // So we can leave checking imageIndex for gotoImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if ((imageMetadataIndex == imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                && (imageMetadata != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                return imageMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1019
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1020
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            gotoImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            imageMetadata = new JPEGMetadata(false, false, iis, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            imageMetadataIndex = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            return imageMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    public BufferedImage read(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1037
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                readInternal(imageIndex, param, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            BufferedImage ret = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            image = null;  // don't keep a reference here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    private Raster readInternal(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                                ImageReadParam param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                                boolean wantRaster) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        readHeader(imageIndex, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        WritableRaster imRas = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        int numImageBands = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if (!wantRaster){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            // Can we read this image?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            Iterator imageTypes = getImageTypes(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            if (imageTypes.hasNext() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                throw new IIOException("Unsupported Image Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            image = getDestination(param, imageTypes, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            imRas = image.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            // The destination may still be incompatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            numImageBands = image.getSampleModel().getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            // Check whether we can handle any implied color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            // Throws IIOException if the stream and the image are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            // incompatible, and sets convert if a java conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            checkColorConversion(image, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            // Check the source and destination bands in the param
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            checkReadParamBandSettings(param, numComponents, numImageBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // Set the output color space equal to the input colorspace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // This disables all conversions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            setOutColorSpace(structPointer, colorSpaceCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        // Create an intermediate 1-line Raster that will hold the decoded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        // subsampled, clipped, band-selected image data in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        // byte-interleaved buffer.  The above transformations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        // will occur in C for performance.  Every time this Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        // is filled we will call back to acceptPixels below to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // this to whatever kind of buffer our image has.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        int [] srcBands = JPEG.bandOffsets[numComponents-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        int numRasterBands = (wantRaster ? numComponents : numImageBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        destinationBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        Rectangle srcROI = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        destROI = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        computeRegions(param, width, height, image, srcROI, destROI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        int periodX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        int periodY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        minProgressivePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        maxProgressivePass = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            periodX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            periodY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            int[] sBands = param.getSourceBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            if (sBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                srcBands = sBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                numRasterBands = srcBands.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            if (!wantRaster) {  // ignore dest bands for Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                destinationBands = param.getDestinationBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            minProgressivePass = param.getSourceMinProgressivePass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            maxProgressivePass = param.getSourceMaxProgressivePass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            if (param instanceof JPEGImageReadParam) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                JPEGImageReadParam jparam = (JPEGImageReadParam) param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                if (jparam.areTablesSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    abbrevQTables = jparam.getQTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                    abbrevDCHuffmanTables = jparam.getDCHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    abbrevACHuffmanTables = jparam.getACHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        int lineSize = destROI.width*numRasterBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        buffer = new DataBufferByte(lineSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        int [] bandOffs = JPEG.bandOffsets[numRasterBands-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        raster = Raster.createInterleavedRaster(buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                                                destROI.width, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                                                lineSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                                                numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                                                bandOffs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        // Now that we have the Raster we'll decode to, get a view of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        // target Raster that will permit a simple setRect for each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (wantRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            target =  Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                                                     destROI.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                                                     destROI.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                                                     lineSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                                                     numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                                                     bandOffs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                                                     null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            target = imRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        int [] bandSizes = target.getSampleModel().getSampleSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         * If the process is sequential, and we have restart markers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         * we could skip to the correct restart marker, if the library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
         * lets us.  That's an optimization to investigate later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        // Check for update listeners (don't call back if none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        boolean callbackUpdates = ((updateListeners != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                                   || (progressListeners != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        // Set up progression data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        initProgressData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        // if we have a metadata object, we can count the scans
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        // and set knownPassCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (imageIndex == imageMetadataIndex) { // We have metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            knownPassCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            for (Iterator iter = imageMetadata.markerSequence.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                 iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                if (iter.next() instanceof SOSMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                    knownPassCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        progInterval = Math.max((target.getHeight()-1) / 20, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        if (knownPassCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            progInterval *= knownPassCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        } else if (maxProgressivePass != Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            progInterval *= (maxProgressivePass - minProgressivePass + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            System.out.println("**** Read Data *****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            System.out.println("numRasterBands is " + numRasterBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            System.out.print("srcBands:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            for (int i = 0; i<srcBands.length;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                System.out.print(" " + srcBands[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            System.out.println("destination bands is " + destinationBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            if (destinationBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                for (int i = 0; i < destinationBands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    System.out.print(" " + destinationBands[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            System.out.println("sourceROI is " + srcROI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            System.out.println("destROI is " + destROI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            System.out.println("periodX is " + periodX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            System.out.println("periodY is " + periodY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            System.out.println("minProgressivePass is " + minProgressivePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            System.out.println("maxProgressivePass is " + maxProgressivePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            System.out.println("callbackUpdates is " + callbackUpdates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // Finally, we are ready to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        processImageStarted(currentImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        boolean aborted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        // Note that getData disables acceleration on buffer, but it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // just a 1-line intermediate data transfer buffer that will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        // affect the acceleration of the resulting image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        aborted = readImage(structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                            buffer.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                            numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                            srcBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                            bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                            srcROI.x, srcROI.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                            srcROI.width, srcROI.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                            periodX, periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                            abbrevQTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                            abbrevDCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                            abbrevACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                            minProgressivePass, maxProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                            callbackUpdates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (aborted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * This method is called back from C when the intermediate Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * is full.  The parameter indicates the scanline in the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * Raster to which the intermediate Raster should be copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * After the copy, we notify update listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    private void acceptPixels(int y, boolean progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (convert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            convert.filter(raster, raster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        target.setRect(destROI.x, destROI.y + y, raster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1267
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1268
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1269
            processImageUpdate(image,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1270
                               destROI.x, destROI.y+y,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1271
                               raster.getWidth(), 1,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1272
                               1, 1,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1273
                               destinationBands);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1274
            if ((y > 0) && (y%progInterval == 0)) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1275
                int height = target.getHeight()-1;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1276
                float percentOfPass = ((float)y)/height;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1277
                if (progressive) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1278
                    if (knownPassCount != UNKNOWN) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1279
                        processImageProgress((pass + percentOfPass)*100.0F
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1280
                                             / knownPassCount);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1281
                    } else if (maxProgressivePass != Integer.MAX_VALUE) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1282
                        // Use the range of allowed progressive passes
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1283
                        processImageProgress((pass + percentOfPass)*100.0F
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1284
                                             / (maxProgressivePass - minProgressivePass + 1));
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1285
                    } else {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1286
                        // Assume there are a minimum of MIN_ESTIMATED_PASSES
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1287
                        // and that there is always one more pass
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1288
                        // Compute the percentage as the percentage at the end
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1289
                        // of the previous pass, plus the percentage of this
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1290
                        // pass scaled to be the percentage of the total remaining,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1291
                        // assuming a minimum of MIN_ESTIMATED_PASSES passes and
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1292
                        // that there is always one more pass.  This is monotonic
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1293
                        // and asymptotic to 1.0, which is what we need.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1294
                        int remainingPasses = // including this one
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1295
                            Math.max(2, MIN_ESTIMATED_PASSES-pass);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1296
                        int totalPasses = pass + remainingPasses-1;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1297
                        progInterval = Math.max(height/20*totalPasses,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1298
                                                totalPasses);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1299
                        if (y%progInterval == 0) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1300
                            percentToDate = previousPassPercentage +
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1301
                                (1.0F - previousPassPercentage)
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1302
                                * (percentOfPass)/remainingPasses;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1303
                            if (debug) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1304
                                System.out.print("pass= " + pass);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1305
                                System.out.print(", y= " + y);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1306
                                System.out.print(", progInt= " + progInterval);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1307
                                System.out.print(", % of pass: " + percentOfPass);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1308
                                System.out.print(", rem. passes: "
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1309
                                                 + remainingPasses);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1310
                                System.out.print(", prev%: "
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1311
                                                 + previousPassPercentage);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1312
                                System.out.print(", %ToDate: " + percentToDate);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1313
                                System.out.print(" ");
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1314
                            }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1315
                            processImageProgress(percentToDate*100.0F);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1316
                        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1317
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                } else {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1319
                    processImageProgress(percentOfPass * 100.0F);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            }
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1322
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1323
            cbLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    private void initProgressData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        knownPassCount = UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        pass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        percentToDate = 0.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        previousPassPercentage = 0.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        progInterval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    private void passStarted (int pass) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1336
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1337
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1338
            this.pass = pass;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1339
            previousPassPercentage = percentToDate;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1340
            processPassStarted(image,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1341
                               pass,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1342
                               minProgressivePass,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1343
                               maxProgressivePass,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1344
                               0, 0,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1345
                               1,1,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1346
                               destinationBands);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1347
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1348
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1349
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    private void passComplete () {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1353
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1354
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1355
            processPassComplete(image);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1356
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1357
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1358
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    void thumbnailStarted(int thumbnailIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1362
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1363
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1364
            processThumbnailStarted(currentImage, thumbnailIndex);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1365
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1366
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1367
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    // Provide access to protected superclass method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    void thumbnailProgress(float percentageDone) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1372
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1373
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1374
            processThumbnailProgress(percentageDone);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1375
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1376
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1377
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    // Provide access to protected superclass method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    void thumbnailComplete() {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1382
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1383
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1384
            processThumbnailComplete();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1385
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1386
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1387
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Returns <code>true</code> if the read was aborted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    private native boolean readImage(long structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                                     byte [] buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                                     int numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                                     int [] srcBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                                     int [] bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                                     int sourceXOffset, int sourceYOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                                     int sourceWidth, int sourceHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                                     int periodX, int periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                                     JPEGQTable [] abbrevQTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                                     JPEGHuffmanTable [] abbrevDCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                                     JPEGHuffmanTable [] abbrevACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                                     int minProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                                     int maxProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                                     boolean wantUpdates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    public void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1411
            /**
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1412
             * NB: we do not check the call back lock here,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1413
             * we allow to abort the reader any time.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1414
             */
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1415
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            super.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            abortRead(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    /** Set the C level abort flag. Keep it atomic for thread safety. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    private native void abortRead(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    /** Resets library state when an exception occurred during a read. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    private native void resetLibraryState(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    public boolean canReadRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    public Raster readRaster(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        Raster retval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1438
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
             * This could be further optimized by not resetting the dest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
             * offset and creating a translated raster in readInternal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
             * (see bug 4994702 for more info).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            // For Rasters, destination offset is logical, not physical, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            // set it to 0 before calling computeRegions, so that the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            // region is not clipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            Point saveDestOffset = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                saveDestOffset = param.getDestinationOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                param.setDestinationOffset(new Point(0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            retval = readInternal(imageIndex, param, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            // Apply the destination offset, if any, as a logical offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            if (saveDestOffset != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                target = target.createWritableTranslatedChild(saveDestOffset.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                                                              saveDestOffset.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    public boolean readerSupportsThumbnails() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    public int getNumThumbnails(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1478
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1479
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            getImageMetadata(imageIndex);  // checks iis state for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            // Now check the jfif segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                retval = (jfif.thumb == null) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                retval += jfif.extSegments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    public int getThumbnailWidth(int imageIndex, int thumbnailIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1500
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1501
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            if ((thumbnailIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                || (thumbnailIndex >= getNumThumbnails(imageIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                throw new IndexOutOfBoundsException("No such thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            // Now we know that there is a jfif segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            return  jfif.getThumbnailWidth(thumbnailIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    public int getThumbnailHeight(int imageIndex, int thumbnailIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1520
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1521
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            if ((thumbnailIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                || (thumbnailIndex >= getNumThumbnails(imageIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                throw new IndexOutOfBoundsException("No such thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            // Now we know that there is a jfif segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            return  jfif.getThumbnailHeight(thumbnailIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    public BufferedImage readThumbnail(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                                       int thumbnailIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1541
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1542
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            if ((thumbnailIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                || (thumbnailIndex >= getNumThumbnails(imageIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                throw new IndexOutOfBoundsException("No such thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            // Now we know that there is a jfif segment and that iis is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            return  jfif.getThumbnail(iis, thumbnailIndex, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    private void resetInternalState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        // reset C structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        resetReader(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        // reset local Java structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        numImages = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        imagePositions = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        currentImage = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        target = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        destROI = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        destinationBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        streamMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        imageMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        imageMetadataIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        haveSeeked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        tablesOnlyChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        initProgressData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1583
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    private native void resetReader(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1595
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1596
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            if (structPointer != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                disposerRecord.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                structPointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    private static native void disposeReader(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    private static class JPEGReaderDisposerRecord implements DisposerRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        private long pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        public JPEGReaderDisposerRecord(long pData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            this.pData = pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        public synchronized void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            if (pData != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                disposeReader(pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                pData = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    private Thread theThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    private int theLockCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    private synchronized void setThreadLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        Thread currThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        if (theThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            if (theThread != currThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                // it looks like that this reader instance is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                // by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                throw new IllegalStateException("Attempt to use instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                                                this + " locked on thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                                                theThread + " from thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                                                currThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                theLockCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            theThread = currThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            theLockCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    private synchronized void clearThreadLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        Thread currThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        if (theThread == null || theThread != currThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            throw new IllegalStateException("Attempt to clear thread lock " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                                            " form wrong thread." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                                            " Locked thread: " + theThread +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                                            "; current thread: " + currThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        theLockCount --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (theLockCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            theThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    }
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1658
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1659
    private CallBackLock cbLock = new CallBackLock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1660
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1661
    private static class CallBackLock {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1662
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1663
        private State lockState;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1664
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1665
        CallBackLock() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1666
            lockState = State.Unlocked;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1667
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1668
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1669
        void check() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1670
            if (lockState != State.Unlocked) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1671
                throw new IllegalStateException("Access to the reader is not allowed");
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1672
            }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1673
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1674
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1675
        private void lock() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1676
            lockState = State.Locked;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1677
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1678
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1679
        private void unlock() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1680
            lockState = State.Unlocked;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1681
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1682
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1683
        private static enum State {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1684
            Unlocked,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1685
            Locked
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1686
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1687
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
}
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1689
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1690
/**
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1691
 * An internal helper class that wraps producer's iterator
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1692
 * and extracts specifier instances on demand.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1693
 */
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1694
class ImageTypeIterator implements Iterator<ImageTypeSpecifier> {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1695
     private Iterator<ImageTypeProducer> producers;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1696
     private ImageTypeSpecifier theNext = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1697
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1698
     public ImageTypeIterator(Iterator<ImageTypeProducer> producers) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1699
         this.producers = producers;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1700
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1701
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1702
     public boolean hasNext() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1703
         if (theNext != null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1704
             return true;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1705
         }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1706
         if (!producers.hasNext()) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1707
             return false;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1708
         }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1709
         do {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1710
             theNext = producers.next().getType();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1711
         } while (theNext == null && producers.hasNext());
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1712
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1713
         return (theNext != null);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1714
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1715
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1716
     public ImageTypeSpecifier next() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1717
         if (theNext != null || hasNext()) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1718
             ImageTypeSpecifier t = theNext;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1719
             theNext = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1720
             return t;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1721
         } else {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1722
             throw new NoSuchElementException();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1723
         }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1724
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1725
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1726
     public void remove() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1727
         producers.remove();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1728
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1729
}
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1730
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1731
/**
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1732
 * An internal helper class that provides means for deferred creation
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1733
 * of ImageTypeSpecifier instance required to describe available
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1734
 * destination types.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1735
 *
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1736
 * This implementation only supports standard
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1737
 * jpeg color spaces (defined by corresponding JCS color space code).
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1738
 *
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1739
 * To support other color spaces one can override produce() method to
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1740
 * return custom instance of ImageTypeSpecifier.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1741
 */
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1742
class ImageTypeProducer {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1743
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1744
    private ImageTypeSpecifier type = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1745
    boolean failed = false;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1746
    private int csCode;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1747
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1748
    public ImageTypeProducer(int csCode) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1749
        this.csCode = csCode;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1750
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1751
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1752
    public ImageTypeProducer() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1753
        csCode = -1; // undefined
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1754
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1755
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1756
    public synchronized ImageTypeSpecifier getType() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1757
        if (!failed && type == null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1758
            try {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1759
                type = produce();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1760
            } catch (Throwable e) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1761
                failed = true;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1762
            }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1763
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1764
        return type;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1765
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1766
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1767
    private static final ImageTypeProducer [] defaultTypes =
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1768
            new ImageTypeProducer [JPEG.NUM_JCS_CODES];
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1769
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1770
    public synchronized static ImageTypeProducer getTypeProducer(int csCode) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1771
        if (csCode < 0 || csCode >= JPEG.NUM_JCS_CODES) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1772
            return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1773
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1774
        if (defaultTypes[csCode] == null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1775
            defaultTypes[csCode] = new ImageTypeProducer(csCode);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1776
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1777
        return defaultTypes[csCode];
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1778
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1779
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1780
    protected ImageTypeSpecifier produce() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1781
        switch (csCode) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1782
            case JPEG.JCS_GRAYSCALE:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1783
                return ImageTypeSpecifier.createFromBufferedImageType
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1784
                        (BufferedImage.TYPE_BYTE_GRAY);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1785
            case JPEG.JCS_RGB:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1786
                return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1787
                        JPEG.bOffsRGB,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1788
                        DataBuffer.TYPE_BYTE,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1789
                        false,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1790
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1791
            case JPEG.JCS_RGBA:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1792
                return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1793
                        0xff000000,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1794
                        0x00ff0000,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1795
                        0x0000ff00,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1796
                        0x000000ff,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1797
                        DataBuffer.TYPE_INT,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1798
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1799
            case JPEG.JCS_YCC:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1800
                if (JPEG.JCS.getYCC() != null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1801
                    return ImageTypeSpecifier.createInterleaved(
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1802
                            JPEG.JCS.getYCC(),
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1803
                        JPEG.bandOffsets[2],
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1804
                        DataBuffer.TYPE_BYTE,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1805
                        false,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1806
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1807
                } else {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1808
                    return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1809
                }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1810
            case JPEG.JCS_YCCA:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1811
                if (JPEG.JCS.getYCC() != null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1812
                    return ImageTypeSpecifier.createInterleaved(
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1813
                            JPEG.JCS.getYCC(),
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1814
                        JPEG.bandOffsets[3],
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1815
                        DataBuffer.TYPE_BYTE,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1816
                        true,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1817
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1818
                } else {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1819
                    return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1820
                }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1821
            default:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1822
                return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1823
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1824
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1825
}