jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java
author pnarayanan
Tue, 14 Feb 2017 11:24:21 +0530
changeset 43835 bb3935761c12
parent 43517 31e01190e3c5
permissions -rw-r--r--
6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap Reviewed-by: prr, jdv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
     2
 * Copyright (c) 2003, 2017, 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: 4205
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: 4205
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: 4205
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4205
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4205
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.bmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.color.ICC_ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.color.ICC_Profile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.ComponentColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.ComponentSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.image.DataBufferInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.image.DataBufferUShort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.image.MultiPixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.PixelInterleavedSampleModel;
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.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.image.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.imageio.ImageReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.imageio.ImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.imageio.spi.ImageReaderSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.imageio.event.IIOReadProgressListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.imageio.event.IIOReadUpdateListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.imageio.event.IIOReadWarningListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.nio.*;
4205
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
    65
import java.security.AccessController;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
    66
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import com.sun.imageio.plugins.common.ImageUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import com.sun.imageio.plugins.common.I18N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/** This class is the Java Image IO plugin reader for BMP images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *  It may subsample the image, clip the image, select sub-bands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *  and shift the decoded image origin if the proper decoding parameter
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33257
diff changeset
    77
 *  are set in the provided {@code ImageReadParam}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *  This class supports Microsoft Windows Bitmap Version 3-5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *  as well as OS/2 Bitmap Version 2.x (for single-image BMP file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public class BMPImageReader extends ImageReader implements BMPConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // BMP Image types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int VERSION_2_1_BIT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final int VERSION_2_4_BIT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final int VERSION_2_8_BIT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final int VERSION_2_24_BIT = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final int VERSION_3_1_BIT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final int VERSION_3_4_BIT = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final int VERSION_3_8_BIT = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final int VERSION_3_24_BIT = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final int VERSION_3_NT_16_BIT = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final int VERSION_3_NT_32_BIT = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final int VERSION_4_1_BIT = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final int VERSION_4_4_BIT = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int VERSION_4_8_BIT = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final int VERSION_4_16_BIT = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int VERSION_4_24_BIT = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int VERSION_4_32_BIT = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int VERSION_3_XP_EMBEDDED = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int VERSION_4_XP_EMBEDDED = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int VERSION_5_XP_EMBEDDED = 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // BMP variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private long bitmapFileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private long bitmapOffset;
26743
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents: 25859
diff changeset
   111
    private long bitmapStart;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private long compression;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private long imageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private byte palette[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private int imageType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private boolean isBottomUp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int bitsPerPixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private int redMask, greenMask, blueMask, alphaMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private SampleModel sampleModel, originalSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private ColorModel colorModel, originalColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /** The input stream where reads from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private ImageInputStream iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** Indicates whether the header is read. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private boolean gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /** The original image width. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /** The original image height. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** The destination region. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private Rectangle destinationRegion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /** The source region. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private Rectangle sourceRegion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /** The metadata from the stream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private BMPMetadata metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /** The destination image. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private BufferedImage bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /** Indicates whether subsampled, subregion is required, and offset is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *  defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private boolean noTransform = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /** Indicates whether subband is selected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private boolean seleBand = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /** The scaling factors. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private int scaleX, scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /** source and destination bands. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private int[] sourceBands, destBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33257
diff changeset
   162
    /** Constructs {@code BMPImageReader} from the provided
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33257
diff changeset
   163
     *  {@code ImageReaderSpi}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public BMPImageReader(ImageReaderSpi originator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        super(originator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /** Overrides the method defined in the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void setInput(Object input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                         boolean seekForwardOnly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                         boolean ignoreMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        super.setInput(input, seekForwardOnly, ignoreMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        iis = (ImageInputStream) input; // Always works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if(iis != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        resetHeaderInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /** Overrides the method defined in the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public int getNumImages(boolean allowSearch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IllegalStateException(I18N.getString("GetNumImages0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (seekForwardOnly && allowSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new IllegalStateException(I18N.getString("GetNumImages1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   191
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int getWidth(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        checkIndex(imageIndex);
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   194
        try {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   195
            readHeader();
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   196
        } catch (IllegalArgumentException e) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   197
            throw new IIOException(I18N.getString("BMPImageReader6"), e);
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   198
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public int getHeight(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        checkIndex(imageIndex);
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   204
        try {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   205
            readHeader();
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   206
        } catch (IllegalArgumentException e) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   207
            throw new IIOException(I18N.getString("BMPImageReader6"), e);
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   208
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private void checkIndex(int imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (imageIndex != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new IndexOutOfBoundsException(I18N.getString("BMPImageReader0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   218
    /**
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   219
     * Process the image header.
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   220
     *
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   221
     * @exception IllegalStateException if source stream is not set.
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   222
     *
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   223
     * @exception IOException if image stream is corrupted.
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   224
     *
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   225
     * @exception IllegalArgumentException if the image stream does not contain
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   226
     *             a BMP image, or if a sample model instance to describe the
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   227
     *             image can not be created.
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   228
     */
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   229
    protected void readHeader() throws IOException, IllegalArgumentException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (gotHeader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IllegalStateException("Input source not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int profileData = 0, profileSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        this.metadata = new BMPMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        iis.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // read and check the magic marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        byte[] marker = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        iis.read(marker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (marker[0] != 0x42 || marker[1] != 0x4d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new IllegalArgumentException(I18N.getString("BMPImageReader1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // Read file size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        bitmapFileSize = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // skip the two reserved fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        iis.skipBytes(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // Offset to the bitmap from the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        bitmapOffset = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // End File Header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // Start BitmapCoreHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        long size = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (size == 12) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            width = iis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            height = iis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            width = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            height = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        metadata.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        metadata.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int planes = iis.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        bitsPerPixel = iis.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        //metadata.colorPlane = planes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        metadata.bitsPerPixel = (short)bitsPerPixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // As BMP always has 3 rgb bands, except for Version 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // which is bgra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        numBands = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (size == 12) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // Windows 2.x and OS/2 1.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            metadata.bmpVersion = VERSION_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // Classify the image type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (bitsPerPixel == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                imageType = VERSION_2_1_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } else if (bitsPerPixel == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                imageType = VERSION_2_4_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } else if (bitsPerPixel == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                imageType = VERSION_2_8_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } else if (bitsPerPixel == 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                imageType = VERSION_2_24_BIT;
33257
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   293
            } else {
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   294
                throw new IIOException(I18N.getString("BMPImageReader8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // Read in the palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int numberOfEntries = (int)((bitmapOffset - 14 - size) / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            int sizeOfPalette = numberOfEntries*3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            palette = new byte[sizeOfPalette];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            iis.readFully(palette, 0, sizeOfPalette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            metadata.palette = palette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            metadata.paletteSize = numberOfEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            compression = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            imageSize = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            long xPelsPerMeter = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            long yPelsPerMeter = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            long colorsUsed = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            long colorsImportant = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            metadata.compression = (int)compression;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            metadata.xPixelsPerMeter = (int)xPelsPerMeter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            metadata.yPixelsPerMeter = (int)yPelsPerMeter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            metadata.colorsUsed = (int)colorsUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            metadata.colorsImportant = (int)colorsImportant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (size == 40) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // Windows 3.x and Windows NT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                switch((int)compression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                case BI_JPEG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                case BI_PNG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    metadata.bmpVersion = VERSION_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    imageType = VERSION_3_XP_EMBEDDED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                case BI_RGB:  // No compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                case BI_RLE8:  // 8-bit RLE compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                case BI_RLE4:  // 4-bit RLE compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    // Read in the palette
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   333
                    if (bitmapOffset < (size + 14)) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   334
                        throw new IIOException(I18N.getString("BMPImageReader7"));
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   335
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    int numberOfEntries = (int)((bitmapOffset-14-size) / 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    int sizeOfPalette = numberOfEntries * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    palette = new byte[sizeOfPalette];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    iis.readFully(palette, 0, sizeOfPalette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    metadata.palette = palette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    metadata.paletteSize = numberOfEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    if (bitsPerPixel == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        imageType = VERSION_3_1_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    } else if (bitsPerPixel == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        imageType = VERSION_3_4_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    } else if (bitsPerPixel == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        imageType = VERSION_3_8_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    } else if (bitsPerPixel == 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        imageType = VERSION_3_24_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    } else if (bitsPerPixel == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        imageType = VERSION_3_NT_16_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        redMask = 0x7C00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        greenMask = 0x3E0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        blueMask =  (1 << 5) - 1;// 0x1F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        metadata.redMask = redMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        metadata.greenMask = greenMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        metadata.blueMask = blueMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    } else if (bitsPerPixel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        imageType = VERSION_3_NT_32_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        redMask   = 0x00FF0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        greenMask = 0x0000FF00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        blueMask  = 0x000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        metadata.redMask = redMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        metadata.greenMask = greenMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        metadata.blueMask = blueMask;
33257
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   369
                    } else {
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   370
                        throw new
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   371
                            IIOException(I18N.getString("BMPImageReader8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    metadata.bmpVersion = VERSION_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                case BI_BITFIELDS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    if (bitsPerPixel == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        imageType = VERSION_3_NT_16_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    } else if (bitsPerPixel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        imageType = VERSION_3_NT_32_BIT;
33257
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   383
                    } else {
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   384
                        throw new
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   385
                            IIOException(I18N.getString("BMPImageReader8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    // BitsField encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    redMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    greenMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    blueMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    metadata.redMask = redMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    metadata.greenMask = greenMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    metadata.blueMask = blueMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    if (colorsUsed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        // there is a palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        sizeOfPalette = (int)colorsUsed*4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        palette = new byte[sizeOfPalette];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        iis.readFully(palette, 0, sizeOfPalette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        metadata.palette = palette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        metadata.paletteSize = (int)colorsUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    metadata.bmpVersion = VERSION_3_NT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    throw new
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   410
                        IIOException(I18N.getString("BMPImageReader2"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            } else if (size == 108 || size == 124) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                // Windows 4.x BMP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (size == 108)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    metadata.bmpVersion = VERSION_4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                else if (size == 124)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    metadata.bmpVersion = VERSION_5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                // rgb masks, valid only if comp is BI_BITFIELDS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                redMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                greenMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                blueMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                // Only supported for 32bpp BI_RGB argb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                alphaMask = (int)iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                long csType = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                int redX = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                int redY = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                int redZ = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                int greenX = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                int greenY = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                int greenZ = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                int blueX = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                int blueY = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                int blueZ = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                long gammaRed = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                long gammaGreen = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                long gammaBlue = iis.readUnsignedInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (size == 124) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    metadata.intent = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    profileData = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    profileSize = iis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    iis.skipBytes(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                metadata.colorSpace = (int)csType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if (csType == LCS_CALIBRATED_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    // All the new fields are valid only for this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    metadata.redX = redX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    metadata.redY = redY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    metadata.redZ = redZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    metadata.greenX = greenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    metadata.greenY = greenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    metadata.greenZ = greenZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    metadata.blueX = blueX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    metadata.blueY = blueY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    metadata.blueZ = blueZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    metadata.gammaRed = (int)gammaRed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    metadata.gammaGreen = (int)gammaGreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    metadata.gammaBlue = (int)gammaBlue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                // Read in the palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                int numberOfEntries = (int)((bitmapOffset-14-size) / 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                int sizeOfPalette = numberOfEntries*4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                palette = new byte[sizeOfPalette];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                iis.readFully(palette, 0, sizeOfPalette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                metadata.palette = palette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                metadata.paletteSize = numberOfEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                switch ((int)compression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                case BI_JPEG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                case BI_PNG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    if (size == 108) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        imageType = VERSION_4_XP_EMBEDDED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    } else if (size == 124) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        imageType = VERSION_5_XP_EMBEDDED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    if (bitsPerPixel == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        imageType = VERSION_4_1_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    } else if (bitsPerPixel == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        imageType = VERSION_4_4_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    } else if (bitsPerPixel == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        imageType = VERSION_4_8_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    } else if (bitsPerPixel == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        imageType = VERSION_4_16_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        if ((int)compression == BI_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                            redMask = 0x7C00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                            greenMask = 0x3E0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                            blueMask = 0x1F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    } else if (bitsPerPixel == 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        imageType = VERSION_4_24_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    } else if (bitsPerPixel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        imageType = VERSION_4_32_BIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        if ((int)compression == BI_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            redMask   = 0x00FF0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                            greenMask = 0x0000FF00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            blueMask  = 0x000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        }
33257
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   504
                    } else {
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   505
                        throw new
53772549e154 8066904: NullPointerException when calling ImageIO.read(InputStream) with corrupt BMP
serb
parents: 26743
diff changeset
   506
                            IIOException(I18N.getString("BMPImageReader8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    metadata.redMask = redMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    metadata.greenMask = greenMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    metadata.blueMask = blueMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    metadata.alphaMask = alphaMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                throw new
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   516
                    IIOException(I18N.getString("BMPImageReader3"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            // bottom up image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            isBottomUp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // top down image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            isBottomUp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            height = Math.abs(height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // Reset Image Layout so there's only one tile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        //Define the color space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (metadata.colorSpace == PROFILE_LINKED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            metadata.colorSpace == PROFILE_EMBEDDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            iis.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            iis.skipBytes(profileData - size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            byte[] profile = new byte[profileSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            iis.readFully(profile, 0, profileSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            try {
4205
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   542
                if (metadata.colorSpace == PROFILE_LINKED &&
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   543
                    isLinkedProfileAllowed() &&
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   544
                    !isUncOrDevicePath(profile))
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   545
                {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   546
                    String path = new String(profile, "windows-1252");
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   547
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    colorSpace =
4205
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   549
                        new ICC_ColorSpace(ICC_Profile.getInstance(path));
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   550
                } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    colorSpace =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        new ICC_ColorSpace(ICC_Profile.getInstance(profile));
4205
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
   553
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (bitsPerPixel == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            compression == BI_JPEG || compression == BI_PNG )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            // the colorModel and sampleModel will be initialzed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            // by the  reader of embedded image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            colorModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            sampleModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } else if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            // When number of bitsPerPixel is <= 8, we use IndexColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            numBands = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (bitsPerPixel == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                int[] bandOffsets = new int[numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                for (int i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    bandOffsets[i] = numBands -1 -i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                sampleModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                                    width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                                    numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                                    numBands * width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                                    bandOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                // 1 and 4 bit pixels can be stored in a packed format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                sampleModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                                    width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                                    bitsPerPixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // Create IndexColorModel from the palette.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            byte r[], g[], b[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (imageType == VERSION_2_1_BIT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                imageType == VERSION_2_4_BIT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                imageType == VERSION_2_8_BIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                size = palette.length/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                if (size > 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    size = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                int off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                r = new byte[(int)size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                g = new byte[(int)size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                b = new byte[(int)size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                for (int i=0; i<(int)size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    off = 3 * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    b[i] = palette[off];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    g[i] = palette[off+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    r[i] = palette[off+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                size = palette.length/4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                if (size > 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    size = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                int off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                r = new byte[(int)size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                g = new byte[(int)size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                b = new byte[(int)size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    off = 4 * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    b[i] = palette[off];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    g[i] = palette[off+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    r[i] = palette[off+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (ImageUtil.isIndicesForGrayscale(r, g, b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                colorModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    ImageUtil.createColorModel(null, sampleModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                colorModel = new IndexColorModel(bitsPerPixel, (int)size, r, g, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        } else if (bitsPerPixel == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            numBands = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            sampleModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                                 width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                                 new int[] {redMask, greenMask, blueMask});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            colorModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                new DirectColorModel(colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                     16, redMask, greenMask, blueMask, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                     false, DataBuffer.TYPE_USHORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        } else if (bitsPerPixel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            numBands = alphaMask == 0 ? 3 : 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            // The number of bands in the SampleModel is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            // the length of the mask array passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            int[] bitMasks = numBands == 3 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                new int[] {redMask, greenMask, blueMask} :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                new int[] {redMask, greenMask, blueMask, alphaMask};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                sampleModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                                     width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                                                     bitMasks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                colorModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    new DirectColorModel(colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                         32, redMask, greenMask, blueMask, alphaMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                         false, DataBuffer.TYPE_INT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            numBands = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            // Create SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            int[] bandOffsets = new int[numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            for (int i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                bandOffsets[i] = numBands -1 -i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            sampleModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                                width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                                numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                                numBands * width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                                bandOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            colorModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                ImageUtil.createColorModel(colorSpace, sampleModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        originalSampleModel = sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        originalColorModel = colorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        // Reset to the start of bitmap; then jump to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        //start of image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        iis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        iis.skipBytes(bitmapOffset);
26743
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents: 25859
diff changeset
   692
        bitmapStart = iis.getStreamPosition();
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents: 25859
diff changeset
   693
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        gotHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
   697
    public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        checkIndex(imageIndex);
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   700
        try {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   701
            readHeader();
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   702
        } catch (IllegalArgumentException e) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   703
            throw new IIOException(I18N.getString("BMPImageReader6"), e);
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   704
        }
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
   705
        ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        list.add(new ImageTypeSpecifier(originalColorModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                        originalSampleModel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public ImageReadParam getDefaultReadParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return new ImageReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public IIOMetadata getImageMetadata(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (metadata == null) {
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   719
            try {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   720
                readHeader();
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   721
            } catch (IllegalArgumentException e) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   722
                throw new IIOException(I18N.getString("BMPImageReader6"), e);
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   723
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        return metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public IIOMetadata getStreamMetadata() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public boolean isRandomAccessEasy(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        checkIndex(imageIndex);
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   734
        try {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   735
            readHeader();
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   736
        } catch (IllegalArgumentException e) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   737
            throw new IIOException(I18N.getString("BMPImageReader6"), e);
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   738
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return metadata.compression == BI_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    public BufferedImage read(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            throw new IllegalStateException(I18N.getString("BMPImageReader5"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        clearAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        processImageStarted(imageIndex);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
   752
        if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
   753
            processReadAborted();
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
   754
            return bi;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
   755
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (param == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            param = getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        //read header
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   761
        try {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   762
            readHeader();
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   763
        } catch (IllegalArgumentException e) {
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   764
            throw new IIOException(I18N.getString("BMPImageReader6"), e);
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   765
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        sourceRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        destinationRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        computeRegions(param, this.width, this.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                       param.getDestination(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                       sourceRegion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                       destinationRegion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        scaleX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        scaleY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // If the destination band is set used it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        sourceBands = param.getSourceBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        destBands = param.getDestinationBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        seleBand = (sourceBands != null) && (destBands != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        noTransform =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            destinationRegion.equals(new Rectangle(0, 0, width, height)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            seleBand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (!seleBand) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            sourceBands = new int[numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            destBands = new int[numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            for (int i = 0; i < numBands; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                destBands[i] = sourceBands[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        // If the destination is provided, then use it.  Otherwise, create new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        bi = param.getDestination();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // Get the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        WritableRaster raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (bi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            if (sampleModel != null && colorModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                sampleModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    sampleModel.createCompatibleSampleModel(destinationRegion.x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                                            destinationRegion.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                                            destinationRegion.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                                            destinationRegion.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                if (seleBand)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                raster = Raster.createWritableRaster(sampleModel, new Point());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                bi = new BufferedImage(colorModel, raster, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            raster = bi.getWritableTile(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            sampleModel = bi.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            colorModel = bi.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            noTransform &=  destinationRegion.equals(raster.getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        byte bdata[] = null; // buffer for byte data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        short sdata[] = null; // buffer for short data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        int idata[] = null; // buffer for int data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // the sampleModel can be null in case of embedded image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (sampleModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (sampleModel.getDataType() == DataBuffer.TYPE_BYTE)
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21221
diff changeset
   827
                bdata = ((DataBufferByte)raster.getDataBuffer()).getData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            else if (sampleModel.getDataType() == DataBuffer.TYPE_USHORT)
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21221
diff changeset
   829
                sdata = ((DataBufferUShort)raster.getDataBuffer()).getData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            else if (sampleModel.getDataType() == DataBuffer.TYPE_INT)
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21221
diff changeset
   831
                idata = ((DataBufferInt)raster.getDataBuffer()).getData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
26743
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents: 25859
diff changeset
   834
        iis.seek(bitmapStart);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents: 25859
diff changeset
   835
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        // There should only be one tile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        switch(imageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        case VERSION_2_1_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            // no compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            read1Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        case VERSION_2_4_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            // no compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            read4Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        case VERSION_2_8_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            // no compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            read8Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        case VERSION_2_24_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            // no compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            read24Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        case VERSION_3_1_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            // 1-bit images cannot be compressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            read1Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        case VERSION_3_4_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            switch((int)compression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            case BI_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                read4Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            case BI_RLE4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                readRLE4(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                throw new
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   876
                    IIOException(I18N.getString("BMPImageReader1"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        case VERSION_3_8_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            switch((int)compression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            case BI_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                read8Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            case BI_RLE8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                readRLE8(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                throw new
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   892
                    IIOException(I18N.getString("BMPImageReader1"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        case VERSION_3_24_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            // 24-bit images are not compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            read24Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        case VERSION_3_NT_16_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            read16Bit(sdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        case VERSION_3_NT_32_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            read32Bit(idata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        case VERSION_3_XP_EMBEDDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        case VERSION_4_XP_EMBEDDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        case VERSION_5_XP_EMBEDDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            bi = readEmbedded((int)compression, bi, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        case VERSION_4_1_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            read1Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        case VERSION_4_4_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            switch((int)compression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            case BI_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                read4Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            case BI_RLE4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                readRLE4(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                throw new
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   933
                    IIOException(I18N.getString("BMPImageReader1"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
   935
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        case VERSION_4_8_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            switch((int)compression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            case BI_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                read8Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            case BI_RLE8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                readRLE8(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                throw new
21221
d80929154039 7058602: BMP parser bugs found via zzuf fuzzing
bae
parents: 5506
diff changeset
   950
                    IIOException(I18N.getString("BMPImageReader1"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        case VERSION_4_16_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            read16Bit(sdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        case VERSION_4_24_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            read24Bit(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        case VERSION_4_32_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            read32Bit(idata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (abortRequested())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    public boolean canReadRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    public Raster readRaster(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                             ImageReadParam param) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        BufferedImage bi = read(imageIndex, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return bi.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    private void resetHeaderInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        bi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        sampleModel = originalSampleModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        colorModel = originalColorModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        resetHeaderInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    // Deal with 1 Bit images using IndexColorModels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    private void read1Bit(byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        int bytesPerScanline = (width + 7) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        int padding = bytesPerScanline % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        if (padding != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            padding = 4 - padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        int lineLength = bytesPerScanline + padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            int j = isBottomUp ? (height -1)*bytesPerScanline : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            for (int i=0; i<height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                iis.readFully(bdata, j, bytesPerScanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                iis.skipBytes(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                j += isBottomUp ? -bytesPerScanline : bytesPerScanline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                processImageUpdate(bi, 0, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                processImageProgress(100.0F * i/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1019
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1020
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1021
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            byte[] buf = new byte[lineLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            int lineStride =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                ((MultiPixelPackedSampleModel)sampleModel).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            if (isBottomUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                int lastLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    sourceRegion.y + (destinationRegion.height - 1) * scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                iis.skipBytes(lineLength * (height - 1 - lastLine));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                iis.skipBytes(lineLength * sourceRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            int skipLength = lineLength * (scaleY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            // cache the values to avoid duplicated computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            int[] srcOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            int[] destOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            int[] srcPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            int[] destPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            for (int i = destinationRegion.x, x = sourceRegion.x, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                 i < destinationRegion.x + destinationRegion.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                 i++, j++, x += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                srcPos[j] = x >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                srcOff[j] = 7 - (x & 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                destPos[j] = i >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                destOff[j] = 7 - (i & 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            int k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if (isBottomUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                k += (destinationRegion.height - 1) * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            for (int j = 0, y = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                 j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                iis.read(buf, 0, lineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                for (int i = 0; i < destinationRegion.width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    bdata[k + destPos[i]] |= v << destOff[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                k += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                processImageUpdate(bi, 0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                processImageProgress(100.0F*j/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1071
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1072
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1073
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    // Method to read a 4 bit BMP image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    private void read4Bit(byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        int bytesPerScanline = (width + 1) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        // Padding bytes at the end of each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        int padding = bytesPerScanline % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        if (padding != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            padding = 4 - padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        int lineLength = bytesPerScanline + padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            int j = isBottomUp ? (height -1) * bytesPerScanline : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            for (int i=0; i<height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                iis.readFully(bdata, j, bytesPerScanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                iis.skipBytes(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                j += isBottomUp ? -bytesPerScanline : bytesPerScanline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                processImageUpdate(bi, 0, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                processImageProgress(100.0F * i/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1101
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1102
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1103
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            byte[] buf = new byte[lineLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            int lineStride =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                ((MultiPixelPackedSampleModel)sampleModel).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            if (isBottomUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                int lastLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    sourceRegion.y + (destinationRegion.height - 1) * scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                iis.skipBytes(lineLength * (height - 1 - lastLine));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                iis.skipBytes(lineLength * sourceRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            int skipLength = lineLength * (scaleY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            // cache the values to avoid duplicated computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            int[] srcOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            int[] destOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            int[] srcPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            int[] destPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            for (int i = destinationRegion.x, x = sourceRegion.x, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                 i < destinationRegion.x + destinationRegion.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                 i++, j++, x += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                srcPos[j] = x >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                srcOff[j] = (1 - (x & 1)) << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                destPos[j] = i >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                destOff[j] = (1 - (i & 1)) << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            int k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (isBottomUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                k += (destinationRegion.height - 1) * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            for (int j = 0, y = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                 j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                iis.read(buf, 0, lineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                for (int i = 0; i < destinationRegion.width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    int v = (buf[srcPos[i]] >> srcOff[i]) & 0x0F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    bdata[k + destPos[i]] |= v << destOff[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                k += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                processImageUpdate(bi, 0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                processImageProgress(100.0F*j/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1153
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1154
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1155
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    // Method to read 8 bit BMP image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    private void read8Bit(byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        // Padding bytes at the end of each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        int padding = width % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (padding != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            padding = 4 - padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        int lineLength = width + padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            int j = isBottomUp ? (height -1) * width : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            for (int i=0; i<height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                iis.readFully(bdata, j, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                iis.skipBytes(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                j += isBottomUp ? -width : width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                processImageUpdate(bi, 0, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                processImageProgress(100.0F * i/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1182
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1183
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1184
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            byte[] buf = new byte[lineLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            int lineStride =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                ((ComponentSampleModel)sampleModel).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            if (isBottomUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                int lastLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    sourceRegion.y + (destinationRegion.height - 1) * scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                iis.skipBytes(lineLength * (height - 1 - lastLine));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                iis.skipBytes(lineLength * sourceRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            int skipLength = lineLength * (scaleY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            int k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            if (isBottomUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                k += (destinationRegion.height - 1) * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            k += destinationRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            for (int j = 0, y = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                 j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                iis.read(buf, 0, lineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                for (int i = 0, m = sourceRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                     i < destinationRegion.width; i++, m += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    bdata[k + i] = buf[m];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                k += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                processImageUpdate(bi, 0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                processImageProgress(100.0F*j/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1220
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1221
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1222
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    // Method to read 24 bit BMP image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    private void read24Bit(byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // Padding bytes at the end of each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        // width * bitsPerPixel should be divisible by 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        int padding = width * 3 % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if ( padding != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            padding = 4 - padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        int lineStride = width * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        int lineLength = lineStride + padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            int j = isBottomUp ? (height -1) * width * 3 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            for (int i=0; i<height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                iis.readFully(bdata, j, lineStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                iis.skipBytes(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                j += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                processImageUpdate(bi, 0, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                processImageProgress(100.0F * i/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1249
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1250
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1251
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            byte[] buf = new byte[lineLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            lineStride =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                ((ComponentSampleModel)sampleModel).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            if (isBottomUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                int lastLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                    sourceRegion.y + (destinationRegion.height - 1) * scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                iis.skipBytes(lineLength * (height - 1 - lastLine));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                iis.skipBytes(lineLength * sourceRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            int skipLength = lineLength * (scaleY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            int k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            if (isBottomUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                k += (destinationRegion.height - 1) * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            k += destinationRegion.x * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            for (int j = 0, y = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                 j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                iis.read(buf, 0, lineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                for (int i = 0, m = 3 * sourceRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                     i < destinationRegion.width; i++, m += 3 * scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    int n = 3 * i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    for (int b = 0; b < destBands.length; b++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                        bdata[n + destBands[b]] = buf[m + sourceBands[b]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                k += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                processImageUpdate(bi, 0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                processImageProgress(100.0F*j/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1289
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1290
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1291
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    private void read16Bit(short sdata[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        // Padding bytes at the end of each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        // width * bitsPerPixel should be divisible by 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        int padding = width * 2 % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        if ( padding != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            padding = 4 - padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        int lineLength = width + padding / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            int j = isBottomUp ? (height -1) * width : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            for (int i=0; i<height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                iis.readFully(sdata, j, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                iis.skipBytes(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                j += isBottomUp ? -width : width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                processImageUpdate(bi, 0, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                processImageProgress(100.0F * i/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1317
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1318
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1319
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            short[] buf = new short[lineLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            int lineStride =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                ((SinglePixelPackedSampleModel)sampleModel).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            if (isBottomUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                int lastLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    sourceRegion.y + (destinationRegion.height - 1) * scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                iis.skipBytes(lineLength * (height - 1 - lastLine) << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                iis.skipBytes(lineLength * sourceRegion.y << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            int skipLength = lineLength * (scaleY - 1) << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            int k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            if (isBottomUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                k += (destinationRegion.height - 1) * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            k += destinationRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            for (int j = 0, y = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                 j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                iis.readFully(buf, 0, lineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                for (int i = 0, m = sourceRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                     i < destinationRegion.width; i++, m += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    sdata[k + i] = buf[m];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                k += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                processImageUpdate(bi, 0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                processImageProgress(100.0F*j/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1355
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1356
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1357
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    private void read32Bit(int idata[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            int j = isBottomUp ? (height -1) * width : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            for (int i=0; i<height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                iis.readFully(idata, j, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                j += isBottomUp ? -width : width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                processImageUpdate(bi, 0, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                processImageProgress(100.0F * i/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1373
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1374
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1375
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            int[] buf = new int[width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            int lineStride =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                ((SinglePixelPackedSampleModel)sampleModel).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            if (isBottomUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                int lastLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    sourceRegion.y + (destinationRegion.height - 1) * scaleY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                iis.skipBytes(width * (height - 1 - lastLine) << 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                iis.skipBytes(width * sourceRegion.y << 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            int skipLength = width * (scaleY - 1) << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            int k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            if (isBottomUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                k += (destinationRegion.height - 1) * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            k += destinationRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            for (int j = 0, y = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                 j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                iis.readFully(buf, 0, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                for (int i = 0, m = sourceRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                     i < destinationRegion.width; i++, m += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    idata[k + i] = buf[m];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                k += isBottomUp ? -lineStride : lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                processImageUpdate(bi, 0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                   destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                                   new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                processImageProgress(100.0F*j/destinationRegion.height);
41010
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1411
                if (abortRequested()) {
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1412
                    break;
9824689edeb1 4924727: reader.abort() method does not work when called inside imageStarted for PNG
jdv
parents: 39517
diff changeset
  1413
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    private void readRLE8(byte bdata[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        // If imageSize field is not provided, calculate it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        int imSize = (int)imageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        if (imSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            imSize = (int)(bitmapFileSize - bitmapOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        int padding = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        // If width is not 32 bit aligned, then while uncompressing each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        // scanline will have padding bytes, calculate the amount of padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        int remainder = width % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (remainder != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            padding = 4 - remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        // Read till we have the whole image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        byte values[] = new byte[imSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        iis.readFully(values, 0, imSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        // Since data is compressed, decompress it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        decodeRLE8(imSize, padding, values, bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1442
    private boolean copyRLE8ScanlineToDst(int lineNo,
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1443
                                          byte[] val,
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1444
                                          byte[] bdata) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1445
        // Return value
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1446
        boolean isSuccess = false;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1447
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1448
        // Reusing the code to copy 1 row of pixels or scanline to required
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1449
        // destination buffer.
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1450
        if (lineNo >= sourceRegion.y &&
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1451
            lineNo < sourceRegion.y + sourceRegion.height) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1452
            if (noTransform) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1453
                int pos = lineNo * width;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1454
                for(int i = 0; i < width; i++)
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1455
                    bdata[pos++] = val[i];
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1456
                processImageUpdate(bi, 0, lineNo,
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1457
                                   destinationRegion.width, 1, 1, 1,
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1458
                                   new int[]{0});
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1459
                isSuccess = true;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1460
            } else if ((lineNo - sourceRegion.y) % scaleY == 0) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1461
                int lineStride =
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1462
                    ((ComponentSampleModel)sampleModel).getScanlineStride();
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1463
                int currentLine = (lineNo - sourceRegion.y) / scaleY +
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1464
                    destinationRegion.y;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1465
                int pos = currentLine * lineStride;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1466
                pos += destinationRegion.x;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1467
                for (int i = sourceRegion.x;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1468
                     i < sourceRegion.x + sourceRegion.width;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1469
                     i += scaleX)
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1470
                    bdata[pos++] = val[i];
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1471
                processImageUpdate(bi, 0, currentLine,
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1472
                                   destinationRegion.width, 1, 1, 1,
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1473
                                   new int[]{0});
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1474
                isSuccess = true;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1475
            }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1476
            // Ensure to reset the scanline buffer once the copy is complete.
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1477
            for(int scIndex = 0; scIndex < width; scIndex++) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1478
                val[scIndex] = 0;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1479
            }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1480
        }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1481
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1482
        return isSuccess;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1483
    }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1484
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    private void decodeRLE8(int imSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                            int padding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                            byte[] values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                            byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1490
        byte val[] = new byte[width];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        int count = 0, l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        boolean flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        int lineNo = isBottomUp ? height - 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        int finished = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1497
        // Ensure image source has sufficient data to decode
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1498
        while ((count + 1) < imSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            value = values[count++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            if (value == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                switch(values[count++] & 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                    // End-of-scanline marker
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1505
                    // Copy the decoded scanline to destination
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1506
                    if (copyRLE8ScanlineToDst(lineNo, val, bdata)) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1507
                        finished++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                    processImageProgress(100.0F * finished / destinationRegion.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                    lineNo += isBottomUp ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                    l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    if (abortRequested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                        flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                    // End-of-RLE marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                    flag = true;
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1521
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1522
                    // Check if the last decoded scanline was copied to
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1523
                    // destination bitmap
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1524
                    if (l != 0) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1525
                        // Copy the decoded scanline to destination
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1526
                        if (copyRLE8ScanlineToDst(lineNo, val, bdata)) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1527
                            finished++;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1528
                        }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1529
                        processImageProgress(100.0F * finished / destinationRegion.height);
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1530
                        lineNo += isBottomUp ? -1 : 1;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1531
                        l = 0;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1532
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    // delta or vector marker
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1537
                    if ((count+1) < imSize) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1538
                        int xoff = values[count++] & 0xff;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1539
                        int yoff = values[count++] & 0xff;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1540
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1541
                        // Check if the yOffset shifts the decoding to another
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1542
                        // row. In such cases, the decoded pixels in scanline
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1543
                        // buffer-val must be copied to the destination image.
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1544
                        if (yoff != 0) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1545
                            // Copy the decoded scanline to destination
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1546
                            if (copyRLE8ScanlineToDst(lineNo, val, bdata)) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1547
                                finished++;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1548
                            }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1549
                            processImageProgress(100.0F * finished
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1550
                                                 / destinationRegion.height);
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1551
                            lineNo += isBottomUp ? -yoff : yoff;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1552
                        }
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1553
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1554
                        // Move to the position xoff, yoff down
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1555
                        l += xoff + yoff*width;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1556
                        l %= width;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1557
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                    int end = values[count-1] & 0xff;
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1562
                    byte readByte = 0;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1563
                    // Ensure to check if the source index-count, does not
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1564
                    // exceed the source image size
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1565
                    for (int i=0; (i < end) && (count < imSize); i++) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1566
                        readByte = (byte)(values[count++] & 0xff);
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1567
                        // Ensure to check if scanline index-l, does not
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1568
                        // exceed the scanline buffer size (width of image)
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1569
                        if (l < width) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1570
                            val[l++] = readByte;
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1571
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                    // Whenever end pixels can fit into odd number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                    // an extra padding byte will be present, so skip that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    if ((end & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                    }
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1579
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            } else {
43835
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1582
                // Encoded mode
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1583
                // Ensure to check if the source index-count, does not
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1584
                // exceed the source image size
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1585
                if (count < imSize) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1586
                    for (int i=0; (i < value) && (l < width); i++) {
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1587
                        val[l++] = (byte)(values[count] & 0xff);
bb3935761c12 6852563: ArrayOutOfBoundException when reading RLE8 compressed bitmap
pnarayanan
parents: 43517
diff changeset
  1588
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            // If End-of-RLE data, then exit the while loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            if (flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    private void readRLE4(byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        // If imageSize field is not specified, calculate it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        int imSize = (int)imageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        if (imSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            imSize = (int)(bitmapFileSize - bitmapOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        int padding = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        // If width is not 32 byte aligned, then while uncompressing each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        // scanline will have padding bytes, calculate the amount of padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        int remainder = width % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        if (remainder != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            padding = 4 - remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        // Read till we have the whole image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        byte[] values = new byte[imSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        iis.readFully(values, 0, imSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        // Decompress the RLE4 compressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        decodeRLE4(imSize, padding, values, bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1625
    private boolean copyRLE4ScanlineToDst(int lineNo,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1626
                                          byte[] val,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1627
                                          byte[] bdata) throws IOException {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1628
        // Return value
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1629
        boolean isSuccess = false;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1630
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1631
        // Reusing the code to copy 1 row of pixels or scanline to required
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1632
        // destination buffer.
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1633
        if (lineNo >= sourceRegion.y &&
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1634
            lineNo < sourceRegion.y + sourceRegion.height) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1635
            if (noTransform) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1636
                int pos = lineNo * (width + 1 >> 1);
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1637
                for(int i = 0, j = 0; i < width >> 1; i++)
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1638
                    bdata[pos++] =
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1639
                        (byte)((val[j++] << 4) | val[j++]);
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1640
                if ((width & 1) == 1)
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1641
                    bdata[pos] |= val[width - 1] << 4;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1642
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1643
                processImageUpdate(bi, 0, lineNo,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1644
                                   destinationRegion.width, 1, 1, 1,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1645
                                   new int[]{0});
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1646
                isSuccess = true;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1647
            } else if ((lineNo - sourceRegion.y) % scaleY == 0) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1648
                int lineStride =
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1649
                    ((MultiPixelPackedSampleModel)sampleModel).getScanlineStride();
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1650
                int currentLine = (lineNo - sourceRegion.y) / scaleY +
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1651
                    destinationRegion.y;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1652
                int pos = currentLine * lineStride;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1653
                pos += destinationRegion.x >> 1;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1654
                int shift = (1 - (destinationRegion.x & 1)) << 2;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1655
                for (int i = sourceRegion.x;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1656
                     i < sourceRegion.x + sourceRegion.width;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1657
                     i += scaleX) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1658
                    bdata[pos] |= val[i] << shift;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1659
                    shift += 4;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1660
                    if (shift == 4) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1661
                        pos++;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1662
                    }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1663
                    shift &= 7;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1664
                }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1665
                processImageUpdate(bi, 0, currentLine,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1666
                                   destinationRegion.width, 1, 1, 1,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1667
                                   new int[]{0});
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1668
                isSuccess = true;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1669
            }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1670
            // Ensure to reset the scanline buffer once the copy is complete.
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1671
            for(int scIndex = 0; scIndex < width; scIndex++) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1672
                val[scIndex] = 0;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1673
            }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1674
        }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1675
        return isSuccess;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1676
    }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1677
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    private void decodeRLE4(int imSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                            int padding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                            byte[] values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                            byte[] bdata) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        byte[] val = new byte[width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        int count = 0, l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        boolean flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        int lineNo = isBottomUp ? height - 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        int finished = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1689
        // Ensure the image has sufficient data before proceeding to decode
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1690
        while ((count + 1) < imSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            value = values[count++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            if (value == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                // Absolute mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                switch(values[count++] & 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                    // End-of-scanline marker
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1700
                    // Copy the decoded scanline to destination
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1701
                    if (copyRLE4ScanlineToDst(lineNo, val, bdata)) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1702
                        finished++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    processImageProgress(100.0F * finished / destinationRegion.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    lineNo += isBottomUp ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                    l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                    if (abortRequested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                        flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    // End-of-RLE marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    flag = true;
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1717
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1718
                    // Check if the last decoded scanline was copied to
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1719
                    // destination bitmap
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1720
                    if (l != 0) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1721
                        // Copy the decoded scanline to destination
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1722
                        if (copyRLE4ScanlineToDst(lineNo, val, bdata)) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1723
                            finished++;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1724
                        }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1725
                        processImageProgress(100.0F * finished / destinationRegion.height);
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1726
                        lineNo += isBottomUp ? -1 : 1;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1727
                        l = 0;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1728
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    // delta or vector marker
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1733
                    if ((count + 1) < imSize) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1734
                        int xoff = values[count++] & 0xFF;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1735
                        int yoff = values[count++] & 0xFF;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1736
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1737
                        // Check if the yOffset shifts the decoding to another
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1738
                        // row. In such cases, the decoded pixels in scanline
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1739
                        // buffer-val must be copied to the destination image.
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1740
                        if (yoff != 0) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1741
                            // Copy the decoded scanline to destination
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1742
                            if (copyRLE4ScanlineToDst(lineNo, val, bdata)) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1743
                                finished++;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1744
                            }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1745
                            processImageProgress(100.0F * finished
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1746
                                                 / destinationRegion.height);
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1747
                            lineNo += isBottomUp ? -yoff : yoff;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1748
                        }
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1749
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1750
                        // Move to the position (xoff, yoff). Since l-is used
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1751
                        // to index into the scanline buffer, the accumulated
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1752
                        // offset is limited to the width of the scanline
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1753
                        l += xoff + yoff*width;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1754
                        l %= width;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1755
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    int end = values[count-1] & 0xFF;
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1760
                    byte readByte = 0;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1761
                    // Ensure to check if the source index-count, does not
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1762
                    // exceed the source image size
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1763
                    for (int i = 0; (i < end) && (count < imSize); i++) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1764
                        readByte = (byte)(((i & 1) == 0) ?
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1765
                                        (values[count] & 0xf0) >> 4 :
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1766
                                        (values[count++] & 0x0f));
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1767
                        // Ensure to check if scanline index-l, does not
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1768
                        // exceed the scanline buffer size (width of image)
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1769
                        if (l < width) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1770
                            val[l++] = readByte;
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1771
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                    // When end is odd, the above for loop does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                    // increment count, so do it now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                    if ((end & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                    // Whenever end pixels can fit into odd number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                    // an extra padding byte will be present, so skip that.
39517
2be87ed267ad 6386906: Faulty rounding code in BMPImageReader.decodeRLE4()
jdv
parents: 35667
diff changeset
  1782
                    if ((((end + 1) / 2) & 1) == 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                // Encoded mode
43517
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1789
                // Ensure to check if the source index-count, does not
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1790
                // exceed the source image size
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1791
                if (count < imSize) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1792
                    int alternate[] = { (values[count] & 0xf0) >> 4,
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1793
                                        values[count] & 0x0f };
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1794
                    for (int i=0; (i < value) && (l < width); i++) {
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1795
                        val[l++] = (byte)alternate[i & 1];
31e01190e3c5 8167278: ArrayIndexOutOfBoundsException when calling ImageIO.read(InputStream) with RLE4 BMP
pnarayanan
parents: 41010
diff changeset
  1796
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            // If End-of-RLE data, then exit the while loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            if (flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    /** Decodes the jpeg/png image embedded in the bitmap using any jpeg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     *  ImageIO-style plugin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33257
diff changeset
  1812
     * @param bi The destination {@code BufferedImage}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33257
diff changeset
  1813
     * @param bmpParam The {@code ImageReadParam} for decoding this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *          BMP image.  The parameters for subregion, band selection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *          subsampling are used in decoding the jpeg image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    private BufferedImage readEmbedded(int type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                              BufferedImage bi, ImageReadParam bmpParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        String format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
          case BI_JPEG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
              format = "JPEG";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
          case BI_PNG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
              format = "PNG";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
              throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                  IOException("Unexpected compression type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        ImageReader reader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            ImageIO.getImageReadersByFormatName(format).next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        if (reader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            throw new RuntimeException(I18N.getString("BMPImageReader4") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                                       " " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        // prepare input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        byte[] buff = new byte[(int)imageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        iis.read(buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        reader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(buff)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        if (bi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            ImageTypeSpecifier embType = reader.getImageTypes(0).next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            bi = embType.createBufferedImage(destinationRegion.x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                                             destinationRegion.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                                             destinationRegion.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                                             destinationRegion.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        reader.addIIOReadProgressListener(new EmbeddedProgressAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                public void imageProgress(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                                          float percentageDone)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                    processImageProgress(percentageDone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        reader.addIIOReadUpdateListener(new IIOReadUpdateListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                public void imageUpdate(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                                        BufferedImage theImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                                        int minX, int minY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                                        int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                                        int periodX, int periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                                        int[] bands)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                    processImageUpdate(theImage, minX, minY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                                       width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                                       periodX, periodY, bands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                public void passComplete(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                                         BufferedImage theImage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    processPassComplete(theImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                public void passStarted(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                                        BufferedImage theImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                                        int pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                                        int minPass, int maxPass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                                        int minX, int minY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                                        int periodX, int periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                                        int[] bands)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    processPassStarted(theImage, pass, minPass, maxPass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                                       minX, minY, periodX, periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                                       bands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                public void thumbnailPassComplete(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                                                  BufferedImage thumb) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                public void thumbnailPassStarted(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                                                 BufferedImage thumb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                                                 int pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                                                 int minPass, int maxPass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                                                 int minX, int minY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                                                 int periodX, int periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                                                 int[] bands) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                public void thumbnailUpdate(ImageReader source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                                            BufferedImage theThumbnail,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                                            int minX, int minY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                                            int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                                            int periodX, int periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                                            int[] bands) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        reader.addIIOReadWarningListener(new IIOReadWarningListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                public void warningOccurred(ImageReader source, String warning)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                    processWarningOccurred(warning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        ImageReadParam param = reader.getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        param.setDestination(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        param.setDestinationBands(bmpParam.getDestinationBands());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        param.setDestinationOffset(bmpParam.getDestinationOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        param.setSourceBands(bmpParam.getSourceBands());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        param.setSourceRegion(bmpParam.getSourceRegion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        param.setSourceSubsampling(bmpParam.getSourceXSubsampling(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                                   bmpParam.getSourceYSubsampling(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                                   bmpParam.getSubsamplingXOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                                   bmpParam.getSubsamplingYOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        reader.read(0, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    private class EmbeddedProgressAdapter implements IIOReadProgressListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        public void imageComplete(ImageReader src) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        public void imageProgress(ImageReader src, float percentageDone) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        public void imageStarted(ImageReader src, int imageIndex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        public void thumbnailComplete(ImageReader src) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        public void thumbnailProgress(ImageReader src, float percentageDone) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        public void thumbnailStarted(ImageReader src, int iIdx, int tIdx) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        public void sequenceComplete(ImageReader src) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        public void sequenceStarted(ImageReader src, int minIndex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        public void readAborted(ImageReader src) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    }
4205
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1937
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1938
    private static Boolean isLinkedProfileDisabled = null;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1939
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1940
    private static boolean isLinkedProfileAllowed() {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1941
        if (isLinkedProfileDisabled == null) {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1942
            PrivilegedAction<Boolean> a = new PrivilegedAction<Boolean>() {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1943
                public Boolean run() {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1944
                    return Boolean.getBoolean("sun.imageio.plugins.bmp.disableLinkedProfiles");
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1945
                }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1946
            };
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1947
            isLinkedProfileDisabled = AccessController.doPrivileged(a);
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1948
        }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1949
        return !isLinkedProfileDisabled;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1950
    }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1951
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1952
    private static Boolean isWindowsPlatform = null;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1953
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1954
    /**
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1955
     * Verifies whether the byte array contans a unc path.
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1956
     * Non-UNC path examples:
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1957
     *  c:\path\to\file  - simple notation
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1958
     *  \\?\c:\path\to\file - long notation
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1959
     *
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1960
     * UNC path examples:
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1961
     *  \\server\share - a UNC path in simple notation
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1962
     *  \\?\UNC\server\share - a UNC path in long notation
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1963
     *  \\.\some\device - a path to device.
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1964
     */
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1965
    private static boolean isUncOrDevicePath(byte[] p) {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1966
        if (isWindowsPlatform == null) {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1967
            PrivilegedAction<Boolean> a = new PrivilegedAction<Boolean>() {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1968
                public Boolean run() {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1969
                    String osname = System.getProperty("os.name");
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1970
                    return (osname != null &&
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1971
                            osname.toLowerCase().startsWith("win"));
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1972
                }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1973
            };
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1974
            isWindowsPlatform = AccessController.doPrivileged(a);
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1975
        }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1976
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1977
        if (!isWindowsPlatform) {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1978
            /* no need for the check on platforms except windows */
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1979
            return false;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1980
        }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1981
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1982
        /* normalize prefix of the path */
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1983
        if (p[0] == '/') p[0] = '\\';
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1984
        if (p[1] == '/') p[1] = '\\';
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1985
        if (p[3] == '/') p[3] = '\\';
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1986
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1987
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1988
        if ((p[0] == '\\') && (p[1] == '\\')) {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1989
            if ((p[2] == '?') && (p[3] == '\\')) {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1990
                // long path: whether unc or local
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1991
                return ((p[4] == 'U' || p[4] == 'u') &&
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1992
                        (p[5] == 'N' || p[5] == 'n') &&
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1993
                        (p[6] == 'C' || p[6] == 'c'));
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1994
            } else {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1995
                // device path or short unc notation
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1996
                return true;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1997
            }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1998
        } else {
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  1999
            return false;
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  2000
        }
89ffcfc5fc26 6632445: DoS from parsing BMPs with UNC ICC links
bae
parents: 2
diff changeset
  2001
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
}