jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageReader.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3012 1420628f14e4
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.imageio.plugins.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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/** This class is the Java Image IO plugin reader for WBMP images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  It may subsample the image, clip the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  and shift the decoded image origin if the proper decoding parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  are set in the provided <code>WBMPImageReadParam</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class WBMPImageReader extends ImageReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** The input stream where reads from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private ImageInputStream iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** Indicates whether the header is read. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** The original image width. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** The original image height. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int wbmpType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private WBMPMetadata metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Constructs <code>WBMPImageReader</code> from the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *  <code>ImageReaderSpi</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public WBMPImageReader(ImageReaderSpi originator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(originator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Overrides the method defined in the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void setInput(Object input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                         boolean seekForwardOnly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         boolean ignoreMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super.setInput(input, seekForwardOnly, ignoreMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        iis = (ImageInputStream) input; // Always works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** Overrides the method defined in the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public int getNumImages(boolean allowSearch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IllegalStateException(I18N.getString("GetNumImages0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (seekForwardOnly && allowSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new IllegalStateException(I18N.getString("GetNumImages1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public int getWidth(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public int getHeight(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public boolean isRandomAccessEasy(int imageIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private void checkIndex(int imageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (imageIndex != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new IndexOutOfBoundsException(I18N.getString("WBMPImageReader0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public void readHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (gotHeader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new IllegalStateException("Input source not set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        metadata = new WBMPMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        wbmpType = iis.readByte();   // TypeField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        byte fixHeaderField = iis.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // check for valid wbmp image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (fixHeaderField != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            || !isValidWbmpType(wbmpType))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new IIOException(I18N.getString("WBMPImageReader2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        metadata.wbmpType = wbmpType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Read image width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        width = readMultiByteInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        metadata.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Read image height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        height = readMultiByteInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        metadata.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        gotHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public Iterator getImageTypes(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        BufferedImage bi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        ArrayList list = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        list.add(new ImageTypeSpecifier(bi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public ImageReadParam getDefaultReadParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return new ImageReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public IIOMetadata getImageMetadata(int imageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (metadata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return metadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public IIOMetadata getStreamMetadata() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public BufferedImage read(int imageIndex, ImageReadParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (iis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new IllegalStateException(I18N.getString("WBMPImageReader1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        checkIndex(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        clearAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        processImageStarted(imageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (param == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            param = getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        //read header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        computeRegions(param, this.width, this.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                       param.getDestination(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                       sourceRegion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                       destinationRegion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int scaleX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int scaleY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int xOffset = param.getSubsamplingXOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int yOffset = param.getSubsamplingYOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // If the destination is provided, then use it.  Otherwise, create new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        BufferedImage bi = param.getDestination();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (bi == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            bi = new BufferedImage(destinationRegion.x + destinationRegion.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                              destinationRegion.y + destinationRegion.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                              BufferedImage.TYPE_BYTE_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        boolean noTransform =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            destinationRegion.equals(new Rectangle(0, 0, width, height)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            destinationRegion.equals(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Get the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        WritableRaster tile = bi.getWritableTile(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Get the SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        MultiPixelPackedSampleModel sm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            (MultiPixelPackedSampleModel)bi.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (noTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (abortRequested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // If noTransform is necessary, read the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            iis.read(((DataBufferByte)tile.getDataBuffer()).getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                     0, height*sm.getScanlineStride());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            processImageUpdate(bi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                               0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               width, height, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                               new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            processImageProgress(100.0F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            int len = (this.width + 7) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            byte[] buf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            byte[] data = ((DataBufferByte)tile.getDataBuffer()).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int lineStride = sm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            iis.skipBytes(len * sourceRegion.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int skipLength = len * (scaleY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // cache the values to avoid duplicated computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int[] srcOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int[] destOff = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int[] srcPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int[] destPos = new int[destinationRegion.width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            for (int i = destinationRegion.x, x = sourceRegion.x, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                i < destinationRegion.x + destinationRegion.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    i++, j++, x += scaleX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                srcPos[j] = x >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                srcOff[j] = 7 - (x & 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                destPos[j] = i >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                destOff[j] = 7 - (i & 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (int j = 0, y = sourceRegion.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                k = destinationRegion.y * lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                j < destinationRegion.height; j++, y+=scaleY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (abortRequested())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                iis.read(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                for (int i = 0; i < destinationRegion.width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    //get the bit and assign to the data buffer of the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    data[k + destPos[i]] |= v << destOff[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                k += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                iis.skipBytes(skipLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        processImageUpdate(bi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                           0, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                           destinationRegion.width, 1, 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                           new int[]{0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        processImageProgress(100.0F*j/destinationRegion.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (abortRequested())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            processReadAborted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            processImageComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public boolean canReadRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public Raster readRaster(int imageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                             ImageReadParam param) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        BufferedImage bi = read(imageIndex, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return bi.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        iis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        gotHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private int readMultiByteInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int value = iis.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int result = value & 0x7f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        while((value & 0x80) == 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            result <<= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            value = iis.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            result |= (value & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * This method verifies that given byte is valid wbmp type marker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * At the moment only 0x0 marker is described by wbmp spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    boolean isValidWbmpType(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return type == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
}