jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageReader.java
author henryjen
Fri, 21 Feb 2014 15:28:37 -0800
changeset 23312 4711f66e7d5c
parent 5506 202f599c92aa
permissions -rw-r--r--
8033716: Fix raw and unchecked lint warnings in com.sun.imageio Reviewed-by: darcy, prr, bae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3012
diff changeset
     2
 * Copyright (c) 2003, 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: 3012
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: 3012
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: 3012
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3012
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3012
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.wbmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.MultiPixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.imageio.ImageReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.imageio.ImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.imageio.spi.ImageReaderSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.imageio.plugins.common.I18N;
3012
1420628f14e4 5101862: WBMP Image reader tries to load Quicktime MOV files
bae
parents: 2
diff changeset
    48
import com.sun.imageio.plugins.common.ReaderUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/** This class is the Java Image IO plugin reader for WBMP images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  It may subsample the image, clip the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  and shift the decoded image origin if the proper decoding parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  are set in the provided <code>WBMPImageReadParam</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class WBMPImageReader extends ImageReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** The input stream where reads from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private ImageInputStream iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Indicates whether the header is read. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** The original image width. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** The original image height. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int wbmpType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private WBMPMetadata metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** Constructs <code>WBMPImageReader</code> from the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *  <code>ImageReaderSpi</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public WBMPImageReader(ImageReaderSpi originator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(originator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Overrides the method defined in the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void setInput(Object input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         boolean seekForwardOnly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                         boolean ignoreMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        super.setInput(input, seekForwardOnly, ignoreMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        iis = (ImageInputStream) input; // Always works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** Overrides the method defined in the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public int getNumImages(boolean allowSearch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new IllegalStateException(I18N.getString("GetNumImages0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (seekForwardOnly && allowSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new IllegalStateException(I18N.getString("GetNumImages1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public int getWidth(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public int getHeight(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public boolean isRandomAccessEasy(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private void checkIndex(int imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (imageIndex != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new IndexOutOfBoundsException(I18N.getString("WBMPImageReader0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void readHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (gotHeader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new IllegalStateException("Input source not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        metadata = new WBMPMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        wbmpType = iis.readByte();   // TypeField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        byte fixHeaderField = iis.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // check for valid wbmp image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (fixHeaderField != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            || !isValidWbmpType(wbmpType))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new IIOException(I18N.getString("WBMPImageReader2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        metadata.wbmpType = wbmpType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Read image width
3012
1420628f14e4 5101862: WBMP Image reader tries to load Quicktime MOV files
bae
parents: 2
diff changeset
   145
        width = ReaderUtil.readMultiByteInteger(iis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        metadata.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Read image height
3012
1420628f14e4 5101862: WBMP Image reader tries to load Quicktime MOV files
bae
parents: 2
diff changeset
   149
        height = ReaderUtil.readMultiByteInteger(iis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        metadata.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        gotHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   155
    public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        BufferedImage bi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   162
        ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        list.add(new ImageTypeSpecifier(bi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public ImageReadParam getDefaultReadParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return new ImageReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public IIOMetadata getImageMetadata(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (metadata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public IIOMetadata getStreamMetadata() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public BufferedImage read(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalStateException(I18N.getString("WBMPImageReader1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        clearAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        processImageStarted(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (param == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            param = getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        //read header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        computeRegions(param, this.width, this.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                       param.getDestination(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                       sourceRegion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                       destinationRegion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int scaleX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int scaleY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int xOffset = param.getSubsamplingXOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int yOffset = param.getSubsamplingYOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // If the destination is provided, then use it.  Otherwise, create new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        BufferedImage bi = param.getDestination();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (bi == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            bi = new BufferedImage(destinationRegion.x + destinationRegion.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                              destinationRegion.y + destinationRegion.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                              BufferedImage.TYPE_BYTE_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        boolean noTransform =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            destinationRegion.equals(new Rectangle(0, 0, width, height)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            destinationRegion.equals(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Get the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        WritableRaster tile = bi.getWritableTile(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Get the SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        MultiPixelPackedSampleModel sm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            (MultiPixelPackedSampleModel)bi.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (abortRequested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // If noTransform is necessary, read the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            iis.read(((DataBufferByte)tile.getDataBuffer()).getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                     0, height*sm.getScanlineStride());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            processImageUpdate(bi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                               width, height, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                               new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            processImageProgress(100.0F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            int len = (this.width + 7) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            byte[] buf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            byte[] data = ((DataBufferByte)tile.getDataBuffer()).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int lineStride = sm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            iis.skipBytes(len * sourceRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int skipLength = len * (scaleY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // cache the values to avoid duplicated computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int[] srcOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int[] destOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int[] srcPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int[] destPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (int i = destinationRegion.x, x = sourceRegion.x, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                i < destinationRegion.x + destinationRegion.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    i++, j++, x += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                srcPos[j] = x >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                srcOff[j] = 7 - (x & 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                destPos[j] = i >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                destOff[j] = 7 - (i & 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            for (int j = 0, y = sourceRegion.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (abortRequested())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                iis.read(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                for (int i = 0; i < destinationRegion.width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    data[k + destPos[i]] |= v << destOff[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                k += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        processImageUpdate(bi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                           0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                           destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                           new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        processImageProgress(100.0F*j/destinationRegion.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (abortRequested())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public boolean canReadRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public Raster readRaster(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                             ImageReadParam param) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        BufferedImage bi = read(imageIndex, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return bi.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * This method verifies that given byte is valid wbmp type marker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * At the moment only 0x0 marker is described by wbmp spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    boolean isValidWbmpType(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return type == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}