src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageReader.java
author jdv
Wed, 10 Jan 2018 12:45:14 +0530
changeset 48643 2ea3667af41d
parent 47216 71c04702a3d5
child 50489 580159eeac07
permissions -rw-r--r--
8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image Reviewed-by: bpb, pnarayanan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48643
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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
     */
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
    81
    private List<Long> imagePositions = null;
2
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() {
25103
4dcb7cd7652e 8043805: Allow using a system-installed libjpeg
omajid
parents: 23328
diff changeset
    92
                    System.loadLibrary("javajpeg");
12559
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. */
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   226
    private static native void initReaderIDs(Class<?> iisClass,
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   227
                                             Class<?> qTableClass,
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   228
                                             Class<?> huffClass);
2
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()) {
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 25103
diff changeset
   370
                imagePositions.add(iis.getStreamPosition());
2
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
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 25103
diff changeset
   373
            imagePositions.add(savePos);
2
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
        }
48643
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   377
        // If the image positions list is empty as in the case of a tables-only
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   378
        // stream, then attempting to access the element at index
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   379
        // imagePositions.size() - 1 will cause an IndexOutOfBoundsException.
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   380
        if (seekForwardOnly && !imagePositions.isEmpty()) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   381
            Long pos = imagePositions.get(imagePositions.size()-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            iis.flushBefore(pos.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        tablesOnlyChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public int getNumImages(boolean allowSearch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try { // locked thread
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   390
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   391
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return getNumImagesOnThread(allowSearch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
45973
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   398
    private void skipPastImage(int imageIndex) {
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   399
        cbLock.lock();
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   400
        try {
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   401
            gotoImage(imageIndex);
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   402
            skipImage();
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   403
        } catch (IOException | IndexOutOfBoundsException e) {
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   404
        } finally {
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   405
            cbLock.unlock();
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   406
        }
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   407
    }
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
   408
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   409
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private int getNumImagesOnThread(boolean allowSearch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (numImages != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return numImages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            throw new IllegalStateException("Input not set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (allowSearch == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    "seekForwardOnly and allowSearch can't both be true!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            // Otherwise we have to read the entire stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (!tablesOnlyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                checkTablesOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            iis.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            gotoImage(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            JPEGBuffer buffer = new JPEGBuffer(iis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            buffer.loadBuf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                done = buffer.scanForFF(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                switch (buffer.buf[buffer.bufPtr] & 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                case JPEG.SOI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    numImages++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    // FALL THROUGH to decrement buffer vars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    // This first set doesn't have a length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                case 0: // not a marker, just a data 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                case JPEG.RST0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                case JPEG.RST1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                case JPEG.RST2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                case JPEG.RST3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                case JPEG.RST4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                case JPEG.RST5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                case JPEG.RST6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                case JPEG.RST7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                case JPEG.EOI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    buffer.bufPtr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    // All the others have a length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    buffer.bufAvail--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    buffer.bufPtr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    buffer.loadBuf(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    int length = ((buffer.buf[buffer.bufPtr++] & 0xff) << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        (buffer.buf[buffer.bufPtr++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    buffer.bufAvail -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    length -= 2; // length includes itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    buffer.skipData(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return numImages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return -1;  // Search is necessary for JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Sets the input stream to the start of the requested image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @exception IllegalStateException if the input source has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @exception IndexOutOfBoundsException if the supplied index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    private void gotoImage(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throw new IllegalStateException("Input not set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (imageIndex < minIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (!tablesOnlyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            checkTablesOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
48643
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   498
        // If the image positions list is empty as in the case of a tables-only
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   499
        // stream, then no image data can be read.
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   500
        if (imagePositions.isEmpty()) {
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   501
            throw new IIOException("No image data present to read");
2ea3667af41d 8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image
jdv
parents: 47216
diff changeset
   502
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (imageIndex < imagePositions.size()) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   504
            iis.seek(imagePositions.get(imageIndex).longValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // read to start of image, saving positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // First seek to the last position we already have, and skip the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // entire image
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   509
            Long pos = imagePositions.get(imagePositions.size()-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            iis.seek(pos.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            skipImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // Now add all intervening positions, skipping images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            for (int index = imagePositions.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                 index <= imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                 index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                // Is there an image?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                if (!hasNextImage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 25103
diff changeset
   520
                pos = iis.getStreamPosition();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                imagePositions.add(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    iis.flushBefore(pos.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                if (index < imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    skipImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                }  // Otherwise we are where we want to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (seekForwardOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            minIndex = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        haveSeeked = true;  // No way is native buffer still valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Skip over a complete image in the stream, leaving the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * positioned such that the next byte to be read is the first
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   541
     * byte of the next image. For JPEG, this means that we read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * until we encounter an EOI marker or until the end of the stream.
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   543
     * We can find data same as EOI marker in some headers
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   544
     * or comments, so we have to skip bytes related to these headers.
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   545
     * If the stream ends before an EOI marker is encountered,
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   546
     * an IndexOutOfBoundsException is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    private void skipImage() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            System.out.println("skipImage called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   552
        // verify if image starts with an SOI marker
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   553
        int initialFF = iis.read();
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   554
        if (initialFF == 0xff) {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   555
            int soiMarker = iis.read();
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   556
            if (soiMarker != JPEG.SOI) {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   557
                throw new IOException("skipImage : Invalid image doesn't "
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   558
                        + "start with SOI marker");
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   559
            }
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   560
        } else {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   561
            throw new IOException("skipImage : Invalid image doesn't start "
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   562
                    + "with 0xff");
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   563
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        boolean foundFF = false;
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   565
        String IOOBE = "skipImage : Reached EOF before we got EOI marker";
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   566
        int markerLength = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        for (int byteval = iis.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
             byteval != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
             byteval = iis.read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (foundFF == true) {
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   572
                switch (byteval) {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   573
                    case JPEG.EOI:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   574
                        if (debug) {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   575
                            System.out.println("skipImage : Found EOI at " +
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   576
                                    (iis.getStreamPosition() - markerLength));
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   577
                        }
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   578
                        return;
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   579
                    case JPEG.SOI:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   580
                        throw new IOException("skipImage : Found extra SOI"
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   581
                                + " marker before getting to EOI");
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   582
                    case 0:
40169
2f908aa3e922 8160943: skipImage() in JPEGImageReader class throws IIOException if we have gaps between markers in Jpeg image.
jdv
parents: 39545
diff changeset
   583
                    case 255:
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   584
                    // markers which doesn't contain length data
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   585
                    case JPEG.RST0:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   586
                    case JPEG.RST1:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   587
                    case JPEG.RST2:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   588
                    case JPEG.RST3:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   589
                    case JPEG.RST4:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   590
                    case JPEG.RST5:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   591
                    case JPEG.RST6:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   592
                    case JPEG.RST7:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   593
                    case JPEG.TEM:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   594
                        break;
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   595
                    // markers which contains length data
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   596
                    case JPEG.SOF0:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   597
                    case JPEG.SOF1:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   598
                    case JPEG.SOF2:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   599
                    case JPEG.SOF3:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   600
                    case JPEG.DHT:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   601
                    case JPEG.SOF5:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   602
                    case JPEG.SOF6:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   603
                    case JPEG.SOF7:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   604
                    case JPEG.JPG:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   605
                    case JPEG.SOF9:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   606
                    case JPEG.SOF10:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   607
                    case JPEG.SOF11:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   608
                    case JPEG.DAC:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   609
                    case JPEG.SOF13:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   610
                    case JPEG.SOF14:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   611
                    case JPEG.SOF15:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   612
                    case JPEG.SOS:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   613
                    case JPEG.DQT:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   614
                    case JPEG.DNL:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   615
                    case JPEG.DRI:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   616
                    case JPEG.DHP:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   617
                    case JPEG.EXP:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   618
                    case JPEG.APP0:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   619
                    case JPEG.APP1:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   620
                    case JPEG.APP2:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   621
                    case JPEG.APP3:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   622
                    case JPEG.APP4:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   623
                    case JPEG.APP5:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   624
                    case JPEG.APP6:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   625
                    case JPEG.APP7:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   626
                    case JPEG.APP8:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   627
                    case JPEG.APP9:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   628
                    case JPEG.APP10:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   629
                    case JPEG.APP11:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   630
                    case JPEG.APP12:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   631
                    case JPEG.APP13:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   632
                    case JPEG.APP14:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   633
                    case JPEG.APP15:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   634
                    case JPEG.COM:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   635
                        // read length of header from next 2 bytes
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   636
                        int lengthHigherBits, lengthLowerBits, length;
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   637
                        lengthHigherBits = iis.read();
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   638
                        if (lengthHigherBits != (-1)) {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   639
                            lengthLowerBits = iis.read();
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   640
                            if (lengthLowerBits != (-1)) {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   641
                                length = (lengthHigherBits << 8) |
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   642
                                        lengthLowerBits;
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   643
                                // length contains already read 2 bytes
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   644
                                length -= 2;
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   645
                            } else {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   646
                                throw new IndexOutOfBoundsException(IOOBE);
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   647
                            }
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   648
                        } else {
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   649
                            throw new IndexOutOfBoundsException(IOOBE);
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   650
                        }
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   651
                        // skip the length specified in marker
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   652
                        iis.skipBytes(length);
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   653
                        break;
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   654
                    case (-1):
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   655
                        throw new IndexOutOfBoundsException(IOOBE);
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   656
                    default:
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   657
                        throw new IOException("skipImage : Invalid marker "
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   658
                                + "starting with ff "
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   659
                                + Integer.toHexString(byteval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   662
            foundFF = (byteval == 0xff);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
39545
1b92907cf159 8152672: IIOException while getting second image properties for JPEG
jdv
parents: 35678
diff changeset
   664
        throw new IndexOutOfBoundsException(IOOBE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   668
     * Returns {@code true} if there is an image beyond
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * the current stream position.  Does not disturb the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * stream position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private boolean hasNextImage() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            System.out.print("hasNextImage called; returning ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        iis.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        boolean foundFF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        for (int byteval = iis.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
             byteval != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
             byteval = iis.read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (foundFF == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                if (byteval == JPEG.SOI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        System.out.println("true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            foundFF = (byteval == 0xff) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        // We hit the end of the stream before we hit an SOI, so no image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            System.out.println("false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Push back the given number of bytes to the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Called by the native code at the end of each image so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * that the next one can be identified from Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    private void pushBack(int num) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            System.out.println("pushing back " + num + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   710
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   711
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   712
            iis.seek(iis.getStreamPosition()-num);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   713
            // The buffer is clear after this, so no need to set haveSeeked.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   714
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   715
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   716
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Reads header information for the given image, if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    private void readHeader(int imageIndex, boolean reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        gotoImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        readNativeHeader(reset); // Ignore return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        currentImage = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    private boolean readNativeHeader(boolean reset) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        boolean retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        retval = readImageHeader(structPointer, haveSeeked, reset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        haveSeeked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        return retval;
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
     * Read in the header information starting from the current
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   738
     * stream position, returning {@code true} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * header was a tables-only image.  After this call, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * native IJG decompression struct will contain the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * information required by most query calls below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * (e.g. getWidth, getHeight, etc.), if the header was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * a tables-only image.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   744
     * If reset is {@code true}, the state of the IJG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * object is reset so that it can read a header again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * This happens automatically if the header was a tables-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    private native boolean readImageHeader(long structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                                           boolean clearBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                                           boolean reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Called by the native code whenever an image header has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * read.  Whether we read metadata or not, we always need this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * information, so it is passed back independently of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * metadata, which may never be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    private void setImageData(int width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                              int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                              int colorSpaceCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                              int outColorSpaceCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                              int numComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                              byte [] iccData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        this.colorSpaceCode = colorSpaceCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        this.outColorSpaceCode = outColorSpaceCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        this.numComponents = numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (iccData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        ICC_Profile newProfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            newProfile = ICC_Profile.getInstance(iccData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
             * Color profile data seems to be invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
             * Ignore this profile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            warningOccurred(WARNING_IGNORE_INVALID_ICC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        byte[] newData = newProfile.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        ICC_Profile oldProfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (iccCS instanceof ICC_ColorSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            oldProfile = ((ICC_ColorSpace)iccCS).getProfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        byte[] oldData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (oldProfile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            oldData = oldProfile.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         * At the moment we can't rely on the ColorSpace.equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * and ICC_Profile.equals() because they do not detect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * the case when two profiles are created from same data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         * So, we have to do data comparison in order to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         * creation of different ColorSpace instances for the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * embedded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if (oldData == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            !java.util.Arrays.equals(oldData, newData))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            iccCS = new ICC_ColorSpace(newProfile);
3013
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   814
            // verify new color space
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   815
            try {
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   816
                float[] colors = iccCS.fromRGB(new float[] {1f, 0f, 0f});
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   817
            } catch (CMMException e) {
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   818
                /*
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   819
                 * Embedded profile seems to be corrupted.
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   820
                 * Ignore this profile.
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   821
                 */
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   822
                iccCS = null;
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   823
                cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   824
                try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   825
                    warningOccurred(WARNING_IGNORE_INVALID_ICC);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   826
                } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   827
                    cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   828
                }
3013
d661931d1ca0 6829549: JVM crash on certain images
bae
parents: 3009
diff changeset
   829
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    public int getWidth(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   837
                cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    public int getHeight(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   850
                cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /////////// Color Conversion and Image Types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * Return an ImageTypeSpecifier corresponding to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * color space code, or null if the color space is unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     */
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   865
    private ImageTypeProducer getImageType(int code) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   866
        ImageTypeProducer ret = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        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
   869
            ret = ImageTypeProducer.getTypeProducer(code);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public ImageTypeSpecifier getRawImageType(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   879
                cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   880
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            // 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
   885
            return getImageType(colorSpaceCode).getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   891
    public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            return getImageTypesOnThread(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
   901
    private Iterator<ImageTypeSpecifier> getImageTypesOnThread(int imageIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if (currentImage != imageIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
   904
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            readHeader(imageIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // We return an iterator containing the default, any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // conversions that the library provides, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        // all the other default types with the same number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // of components, as we can do these as a post-process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // As we convert Rasters rather than images, images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        // with alpha cannot be converted in a post-process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // If this image can't be interpreted, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        // returns an empty Iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        // Get the raw ITS, if there is one.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        // 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
   920
        ImageTypeProducer raw = getImageType(colorSpaceCode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        // Given the encoded colorspace, build a list of ITS's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        // representing outputs you could handle starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        // with the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   926
        ArrayList<ImageTypeProducer> list = new ArrayList<ImageTypeProducer>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        switch (colorSpaceCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        case JPEG.JCS_GRAYSCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            list.add(getImageType(JPEG.JCS_RGB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        case JPEG.JCS_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            list.add(getImageType(JPEG.JCS_GRAYSCALE));
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   936
            list.add(getImageType(JPEG.JCS_YCC));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        case JPEG.JCS_RGBA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        case JPEG.JCS_YCC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            if (raw != null) {  // Might be null if PYCC.pf not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                list.add(getImageType(JPEG.JCS_RGB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        case JPEG.JCS_YCCA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if (raw != null) {  // Might be null if PYCC.pf not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                list.add(raw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        case JPEG.JCS_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            // As there is no YCbCr ColorSpace, we can't support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            // the raw type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            // due to 4705399, use RGB as default in order to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            // slowing down of drawing operations with result image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            list.add(getImageType(JPEG.JCS_RGB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            if (iccCS != null) {
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   961
                list.add(new ImageTypeProducer() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   962
                    protected ImageTypeSpecifier produce() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   963
                        return ImageTypeSpecifier.createInterleaved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                         (iccCS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                          JPEG.bOffsRGB,  // Assume it's for RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                          DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                          false,
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   968
                          false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   969
                    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   970
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            list.add(getImageType(JPEG.JCS_GRAYSCALE));
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   975
            list.add(getImageType(JPEG.JCS_YCC));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        case JPEG.JCS_YCbCrA:  // Default is to convert to RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            // As there is no YCbCr ColorSpace, we can't support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            // the raw type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            list.add(getImageType(JPEG.JCS_RGBA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   984
        return new ImageTypeIterator(list.iterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * Checks the implied color conversion between the stream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * the target image, altering the IJG output color space if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * If a java color conversion is required, then this sets up
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   991
     * {@code convert}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * If bands are being rearranged at all (either source or destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * bands are specified in the param), then the default color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * conversions are assumed to be correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * Throws an IIOException if there is no conversion available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    private void checkColorConversion(BufferedImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                                      ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        // If we are rearranging channels at all, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        // conversions remain in place.  If the user wants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        // raw channels then he should do this while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // a Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if ((param.getSourceBands() != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                (param.getDestinationBands() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                // Accept default conversions out of decoder, silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        // XXX - We do not currently support any indexed color models,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        // though we could, as IJG will quantize for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        // This is a performance and memory-use issue, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        // users can read RGB and then convert to indexed in Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        ColorModel cm = image.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (cm instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            throw new IIOException("IndexColorModel not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        // Now check the ColorSpace type against outColorSpaceCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        // We may want to tweak the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        ColorSpace cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        int csType = cs.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        convert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        switch (outColorSpaceCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        case JPEG.JCS_GRAYSCALE:  // Its gray in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if  (csType == ColorSpace.TYPE_RGB) { // We want RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                // IJG can do this for us more efficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                setOutColorSpace(structPointer, JPEG.JCS_RGB);
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1034
                // Update java state according to changes
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1035
                // in the native part of decoder.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1036
                outColorSpaceCode = JPEG.JCS_RGB;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1037
                numComponents = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            } else if (csType != ColorSpace.TYPE_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        case JPEG.JCS_RGB:  // IJG wants to go to RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            if (csType ==  ColorSpace.TYPE_GRAY) {  // We want gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                if (colorSpaceCode == JPEG.JCS_YCbCr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    // If the jpeg space is YCbCr, IJG can do it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    setOutColorSpace(structPointer, JPEG.JCS_GRAYSCALE);
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1047
                    // Update java state according to changes
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1048
                    // in the native part of decoder.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1049
                    outColorSpaceCode = JPEG.JCS_GRAYSCALE;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1050
                    numComponents = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            } else if ((iccCS != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                       (cm.getNumComponents() == numComponents) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                       (cs != iccCS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                // We have an ICC profile but it isn't used in the dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                // image.  So convert from the profile cs to the target cs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                convert = new ColorConvertOp(iccCS, cs, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                // Leave IJG conversion in place; we still need it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            } else if ((iccCS == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                       (!cs.isCS_sRGB()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                       (cm.getNumComponents() == numComponents)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                // 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
  1063
                convert = new ColorConvertOp(JPEG.JCS.sRGB, cs, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            } else if (csType != ColorSpace.TYPE_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        case JPEG.JCS_RGBA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // No conversions available; image must be RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            if ((csType != ColorSpace.TYPE_RGB) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                (cm.getNumComponents() != numComponents)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        case JPEG.JCS_YCC:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1076
            {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1077
                ColorSpace YCC = JPEG.JCS.getYCC();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1078
                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
  1079
                    throw new IIOException("Incompatible color conversion");
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1080
                }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1081
                if ((cs != YCC) &&
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1082
                    (cm.getNumComponents() == numComponents)) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1083
                    convert = new ColorConvertOp(YCC, cs, null);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1084
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        case JPEG.JCS_YCCA:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1088
            {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1089
                ColorSpace YCC = JPEG.JCS.getYCC();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1090
                // No conversions available; image must be YCCA
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1091
                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
  1092
                    (cs != YCC) ||
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1093
                    (cm.getNumComponents() != numComponents)) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1094
                    throw new IIOException("Incompatible color conversion");
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1095
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            // Anything else we can't handle at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            throw new IIOException("Incompatible color conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Set the IJG output space to the given value.  The library will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * perform the appropriate colorspace conversions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    private native void setOutColorSpace(long structPointer, int id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    /////// End of Color Conversion & Image Types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    public ImageReadParam getDefaultReadParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        return new JPEGImageReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    public IIOMetadata getStreamMetadata() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            if (!tablesOnlyChecked) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1120
                cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                checkTablesOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            return streamMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    public IIOMetadata getImageMetadata(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            // imageMetadataIndex will always be either a valid index or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            // -1, in which case imageMetadata will not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            // So we can leave checking imageIndex for gotoImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            if ((imageMetadataIndex == imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                && (imageMetadata != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                return imageMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1141
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            gotoImage(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            imageMetadata = new JPEGMetadata(false, false, iis, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            imageMetadataIndex = imageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            return imageMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    public BufferedImage read(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1159
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                readInternal(imageIndex, param, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            BufferedImage ret = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            image = null;  // don't keep a reference here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    private Raster readInternal(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                                ImageReadParam param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                                boolean wantRaster) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        readHeader(imageIndex, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        WritableRaster imRas = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        int numImageBands = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        if (!wantRaster){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            // Can we read this image?
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
  1188
            Iterator<ImageTypeSpecifier> imageTypes = getImageTypes(imageIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            if (imageTypes.hasNext() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                throw new IIOException("Unsupported Image Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            image = getDestination(param, imageTypes, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            imRas = image.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            // The destination may still be incompatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            numImageBands = image.getSampleModel().getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            // Check whether we can handle any implied color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            // Throws IIOException if the stream and the image are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            // incompatible, and sets convert if a java conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            // is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            checkColorConversion(image, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            // Check the source and destination bands in the param
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            checkReadParamBandSettings(param, numComponents, numImageBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            // Set the output color space equal to the input colorspace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            // This disables all conversions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            setOutColorSpace(structPointer, colorSpaceCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        // Create an intermediate 1-line Raster that will hold the decoded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        // subsampled, clipped, band-selected image data in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        // byte-interleaved buffer.  The above transformations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        // will occur in C for performance.  Every time this Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        // is filled we will call back to acceptPixels below to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        // this to whatever kind of buffer our image has.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        int [] srcBands = JPEG.bandOffsets[numComponents-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        int numRasterBands = (wantRaster ? numComponents : numImageBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        destinationBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        Rectangle srcROI = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        destROI = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        computeRegions(param, width, height, image, srcROI, destROI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        int periodX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        int periodY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        minProgressivePass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        maxProgressivePass = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            periodX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            periodY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            int[] sBands = param.getSourceBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            if (sBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                srcBands = sBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                numRasterBands = srcBands.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            if (!wantRaster) {  // ignore dest bands for Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                destinationBands = param.getDestinationBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            minProgressivePass = param.getSourceMinProgressivePass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            maxProgressivePass = param.getSourceMaxProgressivePass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            if (param instanceof JPEGImageReadParam) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                JPEGImageReadParam jparam = (JPEGImageReadParam) param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                if (jparam.areTablesSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    abbrevQTables = jparam.getQTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                    abbrevDCHuffmanTables = jparam.getDCHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    abbrevACHuffmanTables = jparam.getACHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        int lineSize = destROI.width*numRasterBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        buffer = new DataBufferByte(lineSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        int [] bandOffs = JPEG.bandOffsets[numRasterBands-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        raster = Raster.createInterleavedRaster(buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                                                destROI.width, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                                                lineSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                                                numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                                                bandOffs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        // Now that we have the Raster we'll decode to, get a view of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // target Raster that will permit a simple setRect for each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        if (wantRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            target =  Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                                                     destROI.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                                     destROI.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                                     lineSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                                                     numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                                     bandOffs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                                                     null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            target = imRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        int [] bandSizes = target.getSampleModel().getSampleSize();
20798
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16881
diff changeset
  1290
        for (int i = 0; i < bandSizes.length; i++) {
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16881
diff changeset
  1291
            if (bandSizes[i] <= 0 || bandSizes[i] > 8) {
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16881
diff changeset
  1292
                throw new IIOException("Illegal band size: should be 0 < size <= 8");
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16881
diff changeset
  1293
            }
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16881
diff changeset
  1294
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * If the process is sequential, and we have restart markers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         * we could skip to the correct restart marker, if the library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         * lets us.  That's an optimization to investigate later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        // Check for update listeners (don't call back if none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        boolean callbackUpdates = ((updateListeners != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                   || (progressListeners != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        // Set up progression data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        initProgressData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        // if we have a metadata object, we can count the scans
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // and set knownPassCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if (imageIndex == imageMetadataIndex) { // We have metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            knownPassCount = 0;
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
  1312
            for (Iterator<MarkerSegment> iter =
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
  1313
                    imageMetadata.markerSequence.iterator(); iter.hasNext();) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                if (iter.next() instanceof SOSMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    knownPassCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        progInterval = Math.max((target.getHeight()-1) / 20, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (knownPassCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            progInterval *= knownPassCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        } else if (maxProgressivePass != Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            progInterval *= (maxProgressivePass - minProgressivePass + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            System.out.println("**** Read Data *****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            System.out.println("numRasterBands is " + numRasterBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            System.out.print("srcBands:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            for (int i = 0; i<srcBands.length;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                System.out.print(" " + srcBands[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            System.out.println("destination bands is " + destinationBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            if (destinationBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                for (int i = 0; i < destinationBands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    System.out.print(" " + destinationBands[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            System.out.println("sourceROI is " + srcROI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            System.out.println("destROI is " + destROI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            System.out.println("periodX is " + periodX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            System.out.println("periodY is " + periodY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            System.out.println("minProgressivePass is " + minProgressivePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            System.out.println("maxProgressivePass is " + maxProgressivePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            System.out.println("callbackUpdates is " + callbackUpdates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1349
        /*
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1350
         * All the Jpeg processing happens in native, we should clear
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1351
         * abortFlag of imageIODataStruct in imageioJPEG.c. And we need to
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1352
         * clear abortFlag because if in previous read() if we had called
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1353
         * reader.abort() that will continue to be valid for present call also.
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1354
         */
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1355
        clearNativeReadAbortFlag(structPointer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        processImageStarted(currentImage);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1357
        /*
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1358
         * Note that getData disables acceleration on buffer, but it is
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1359
         * just a 1-line intermediate data transfer buffer that will not
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1360
         * affect the acceleration of the resulting image.
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1361
         */
45973
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
  1362
        boolean aborted = readImage(imageIndex,
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
  1363
                                    structPointer,
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1364
                                    buffer.getData(),
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1365
                                    numRasterBands,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1366
                                    srcBands,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1367
                                    bandSizes,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1368
                                    srcROI.x, srcROI.y,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1369
                                    srcROI.width, srcROI.height,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1370
                                    periodX, periodY,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1371
                                    abbrevQTables,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1372
                                    abbrevDCHuffmanTables,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1373
                                    abbrevACHuffmanTables,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1374
                                    minProgressivePass, maxProgressivePass,
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1375
                                    callbackUpdates);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        if (aborted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * This method is called back from C when the intermediate Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * is full.  The parameter indicates the scanline in the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * Raster to which the intermediate Raster should be copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * After the copy, we notify update listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    private void acceptPixels(int y, boolean progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        if (convert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            convert.filter(raster, raster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        target.setRect(destROI.x, destROI.y + y, raster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1399
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1400
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1401
            processImageUpdate(image,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1402
                               destROI.x, destROI.y+y,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1403
                               raster.getWidth(), 1,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1404
                               1, 1,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1405
                               destinationBands);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1406
            if ((y > 0) && (y%progInterval == 0)) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1407
                int height = target.getHeight()-1;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1408
                float percentOfPass = ((float)y)/height;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1409
                if (progressive) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1410
                    if (knownPassCount != UNKNOWN) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1411
                        processImageProgress((pass + percentOfPass)*100.0F
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1412
                                             / knownPassCount);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1413
                    } else if (maxProgressivePass != Integer.MAX_VALUE) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1414
                        // Use the range of allowed progressive passes
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1415
                        processImageProgress((pass + percentOfPass)*100.0F
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1416
                                             / (maxProgressivePass - minProgressivePass + 1));
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1417
                    } else {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1418
                        // Assume there are a minimum of MIN_ESTIMATED_PASSES
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1419
                        // and that there is always one more pass
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1420
                        // Compute the percentage as the percentage at the end
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1421
                        // of the previous pass, plus the percentage of this
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1422
                        // pass scaled to be the percentage of the total remaining,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1423
                        // assuming a minimum of MIN_ESTIMATED_PASSES passes and
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1424
                        // that there is always one more pass.  This is monotonic
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1425
                        // and asymptotic to 1.0, which is what we need.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1426
                        int remainingPasses = // including this one
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1427
                            Math.max(2, MIN_ESTIMATED_PASSES-pass);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1428
                        int totalPasses = pass + remainingPasses-1;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1429
                        progInterval = Math.max(height/20*totalPasses,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1430
                                                totalPasses);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1431
                        if (y%progInterval == 0) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1432
                            percentToDate = previousPassPercentage +
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1433
                                (1.0F - previousPassPercentage)
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1434
                                * (percentOfPass)/remainingPasses;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1435
                            if (debug) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1436
                                System.out.print("pass= " + pass);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1437
                                System.out.print(", y= " + y);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1438
                                System.out.print(", progInt= " + progInterval);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1439
                                System.out.print(", % of pass: " + percentOfPass);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1440
                                System.out.print(", rem. passes: "
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1441
                                                 + remainingPasses);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1442
                                System.out.print(", prev%: "
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1443
                                                 + previousPassPercentage);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1444
                                System.out.print(", %ToDate: " + percentToDate);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1445
                                System.out.print(" ");
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1446
                            }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1447
                            processImageProgress(percentToDate*100.0F);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1448
                        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1449
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                } else {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1451
                    processImageProgress(percentOfPass * 100.0F);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            }
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1454
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1455
            cbLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    private void initProgressData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        knownPassCount = UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        pass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        percentToDate = 0.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        previousPassPercentage = 0.0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        progInterval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    private void passStarted (int pass) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1468
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1469
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1470
            this.pass = pass;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1471
            previousPassPercentage = percentToDate;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1472
            processPassStarted(image,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1473
                               pass,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1474
                               minProgressivePass,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1475
                               maxProgressivePass,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1476
                               0, 0,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1477
                               1,1,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1478
                               destinationBands);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1479
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1480
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1481
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    private void passComplete () {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1485
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1486
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1487
            processPassComplete(image);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1488
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1489
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1490
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    void thumbnailStarted(int thumbnailIndex) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1494
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1495
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1496
            processThumbnailStarted(currentImage, thumbnailIndex);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1497
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1498
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1499
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    // Provide access to protected superclass method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    void thumbnailProgress(float percentageDone) {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1504
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1505
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1506
            processThumbnailProgress(percentageDone);
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1507
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1508
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1509
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    // Provide access to protected superclass method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    void thumbnailComplete() {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1514
        cbLock.lock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1515
        try {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1516
            processThumbnailComplete();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1517
        } finally {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1518
            cbLock.unlock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1519
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
  1523
     * Returns {@code true} if the read was aborted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     */
45973
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
  1525
    private native boolean readImage(int imageIndex,
3daf29464c02 8169209: Improved image post-processing steps
prr
parents: 41010
diff changeset
  1526
                                     long structPointer,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                                     byte [] buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                                     int numRasterBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                                     int [] srcBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                                     int [] bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                                     int sourceXOffset, int sourceYOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                                     int sourceWidth, int sourceHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                                     int periodX, int periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                     JPEGQTable [] abbrevQTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                                     JPEGHuffmanTable [] abbrevDCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                                     JPEGHuffmanTable [] abbrevACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                                     int minProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                     int maxProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                     boolean wantUpdates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1541
    /*
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1542
     * We should call clearNativeReadAbortFlag() before we start reading
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1543
     * jpeg image as image processing happens at native side.
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1544
     */
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1545
    private native void clearNativeReadAbortFlag(long structPointer);
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 40169
diff changeset
  1546
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    public void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1550
            /**
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1551
             * NB: we do not check the call back lock here,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1552
             * we allow to abort the reader any time.
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1553
             */
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1554
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            super.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            abortRead(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /** Set the C level abort flag. Keep it atomic for thread safety. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    private native void abortRead(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    /** Resets library state when an exception occurred during a read. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    private native void resetLibraryState(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    public boolean canReadRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    public Raster readRaster(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        Raster retval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1577
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
             * This could be further optimized by not resetting the dest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
             * offset and creating a translated raster in readInternal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
             * (see bug 4994702 for more info).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            // For Rasters, destination offset is logical, not physical, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            // set it to 0 before calling computeRegions, so that the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            // region is not clipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            Point saveDestOffset = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                saveDestOffset = param.getDestinationOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                param.setDestinationOffset(new Point(0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            retval = readInternal(imageIndex, param, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            // Apply the destination offset, if any, as a logical offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            if (saveDestOffset != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                target = target.createWritableTranslatedChild(saveDestOffset.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                                                              saveDestOffset.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            resetLibraryState(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    public boolean readerSupportsThumbnails() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    public int getNumThumbnails(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1617
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1618
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            getImageMetadata(imageIndex);  // checks iis state for us
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            // Now check the jfif segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                retval = (jfif.thumb == null) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                retval += jfif.extSegments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    public int getThumbnailWidth(int imageIndex, int thumbnailIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1639
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1640
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            if ((thumbnailIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                || (thumbnailIndex >= getNumThumbnails(imageIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                throw new IndexOutOfBoundsException("No such thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            // Now we know that there is a jfif segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            return  jfif.getThumbnailWidth(thumbnailIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    public int getThumbnailHeight(int imageIndex, int thumbnailIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1659
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1660
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            if ((thumbnailIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                || (thumbnailIndex >= getNumThumbnails(imageIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                throw new IndexOutOfBoundsException("No such thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            // Now we know that there is a jfif segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            return  jfif.getThumbnailHeight(thumbnailIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    public BufferedImage readThumbnail(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                                       int thumbnailIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1680
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1681
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            if ((thumbnailIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                || (thumbnailIndex >= getNumThumbnails(imageIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                throw new IndexOutOfBoundsException("No such thumbnail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            // Now we know that there is a jfif segment and that iis is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            JFIFMarkerSegment jfif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                (JFIFMarkerSegment) imageMetadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            return  jfif.getThumbnail(iis, thumbnailIndex, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    private void resetInternalState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        // reset C structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        resetReader(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        // reset local Java structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        numImages = 0;
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 20798
diff changeset
  1702
        imagePositions = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        currentImage = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        target = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        destROI = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        destinationBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        streamMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        imageMetadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        imageMetadataIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        haveSeeked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        tablesOnlyChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        iccCS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        initProgressData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1722
            cbLock.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    private native void resetReader(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        try {
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1734
            cbLock.check();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1735
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            if (structPointer != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                disposerRecord.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                structPointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    private static native void disposeReader(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    private static class JPEGReaderDisposerRecord implements DisposerRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        private long pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        public JPEGReaderDisposerRecord(long pData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            this.pData = pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        public synchronized void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            if (pData != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                disposeReader(pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                pData = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    private Thread theThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    private int theLockCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    private synchronized void setThreadLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        Thread currThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if (theThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            if (theThread != currThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                // it looks like that this reader instance is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                // by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                throw new IllegalStateException("Attempt to use instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                                                this + " locked on thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                                                theThread + " from thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                                                currThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                theLockCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            theThread = currThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            theLockCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    private synchronized void clearThreadLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        Thread currThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        if (theThread == null || theThread != currThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            throw new IllegalStateException("Attempt to clear thread lock " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                                            " form wrong thread." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                                            " Locked thread: " + theThread +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                                            "; current thread: " + currThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        theLockCount --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        if (theLockCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            theThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    }
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1797
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1798
    private CallBackLock cbLock = new CallBackLock();
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1799
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1800
    private static class CallBackLock {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1801
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1802
        private State lockState;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1803
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1804
        CallBackLock() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1805
            lockState = State.Unlocked;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1806
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1807
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1808
        void check() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1809
            if (lockState != State.Unlocked) {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1810
                throw new IllegalStateException("Access to the reader is not allowed");
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1811
            }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1812
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1813
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1814
        private void lock() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1815
            lockState = State.Locked;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1816
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1817
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1818
        private void unlock() {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1819
            lockState = State.Unlocked;
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1820
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1821
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1822
        private static enum State {
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1823
            Unlocked,
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1824
            Locked
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1825
        }
f105ea43e3aa 8007667: Better image reading
bae
parents: 14342
diff changeset
  1826
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
}
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1828
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1829
/**
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1830
 * 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
  1831
 * and extracts specifier instances on demand.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1832
 */
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1833
class ImageTypeIterator implements Iterator<ImageTypeSpecifier> {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1834
     private Iterator<ImageTypeProducer> producers;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1835
     private ImageTypeSpecifier theNext = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1836
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1837
     public ImageTypeIterator(Iterator<ImageTypeProducer> producers) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1838
         this.producers = producers;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1839
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1840
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1841
     public boolean hasNext() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1842
         if (theNext != null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1843
             return true;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1844
         }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1845
         if (!producers.hasNext()) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1846
             return false;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1847
         }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1848
         do {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1849
             theNext = producers.next().getType();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1850
         } while (theNext == null && producers.hasNext());
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1851
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1852
         return (theNext != null);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1853
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1854
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1855
     public ImageTypeSpecifier next() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1856
         if (theNext != null || hasNext()) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1857
             ImageTypeSpecifier t = theNext;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1858
             theNext = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1859
             return t;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1860
         } else {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1861
             throw new NoSuchElementException();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1862
         }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1863
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1864
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1865
     public void remove() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1866
         producers.remove();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1867
     }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1868
}
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1869
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1870
/**
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1871
 * 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
  1872
 * of ImageTypeSpecifier instance required to describe available
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1873
 * destination types.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1874
 *
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1875
 * This implementation only supports standard
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1876
 * 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
  1877
 *
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1878
 * 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
  1879
 * return custom instance of ImageTypeSpecifier.
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1880
 */
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1881
class ImageTypeProducer {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1882
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1883
    private ImageTypeSpecifier type = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1884
    boolean failed = false;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1885
    private int csCode;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1886
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1887
    public ImageTypeProducer(int csCode) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1888
        this.csCode = csCode;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1889
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1890
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1891
    public ImageTypeProducer() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1892
        csCode = -1; // undefined
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1893
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1894
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1895
    public synchronized ImageTypeSpecifier getType() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1896
        if (!failed && type == null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1897
            try {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1898
                type = produce();
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1899
            } catch (Throwable e) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1900
                failed = true;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1901
            }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1902
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1903
        return type;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1904
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1905
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1906
    private static final ImageTypeProducer [] defaultTypes =
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1907
            new ImageTypeProducer [JPEG.NUM_JCS_CODES];
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1908
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
  1909
    public static synchronized ImageTypeProducer getTypeProducer(int csCode) {
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1910
        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
  1911
            return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1912
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1913
        if (defaultTypes[csCode] == null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1914
            defaultTypes[csCode] = new ImageTypeProducer(csCode);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1915
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1916
        return defaultTypes[csCode];
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1917
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1918
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1919
    protected ImageTypeSpecifier produce() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1920
        switch (csCode) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1921
            case JPEG.JCS_GRAYSCALE:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1922
                return ImageTypeSpecifier.createFromBufferedImageType
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1923
                        (BufferedImage.TYPE_BYTE_GRAY);
35678
ad6cfe2f2e13 8143562: JPEG reader returns null for getRawImageType()
jdv
parents: 35667
diff changeset
  1924
            case JPEG.JCS_YCbCr:
ad6cfe2f2e13 8143562: JPEG reader returns null for getRawImageType()
jdv
parents: 35667
diff changeset
  1925
            //there is no YCbCr raw type so by default we assume it as RGB
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1926
            case JPEG.JCS_RGB:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1927
                return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1928
                        JPEG.bOffsRGB,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1929
                        DataBuffer.TYPE_BYTE,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1930
                        false,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1931
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1932
            case JPEG.JCS_RGBA:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1933
                return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1934
                        0xff000000,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1935
                        0x00ff0000,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1936
                        0x0000ff00,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1937
                        0x000000ff,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1938
                        DataBuffer.TYPE_INT,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1939
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1940
            case JPEG.JCS_YCC:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1941
                if (JPEG.JCS.getYCC() != null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1942
                    return ImageTypeSpecifier.createInterleaved(
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1943
                            JPEG.JCS.getYCC(),
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1944
                        JPEG.bandOffsets[2],
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1945
                        DataBuffer.TYPE_BYTE,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1946
                        false,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1947
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1948
                } else {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1949
                    return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1950
                }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1951
            case JPEG.JCS_YCCA:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1952
                if (JPEG.JCS.getYCC() != null) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1953
                    return ImageTypeSpecifier.createInterleaved(
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1954
                            JPEG.JCS.getYCC(),
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1955
                        JPEG.bandOffsets[3],
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1956
                        DataBuffer.TYPE_BYTE,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1957
                        true,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1958
                        false);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1959
                } else {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1960
                    return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1961
                }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1962
            default:
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1963
                return null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1964
        }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1965
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1966
}