jdk/src/java.desktop/share/classes/sun/awt/image/ByteInterleavedRaster.java
author aghaisas
Thu, 31 Mar 2016 15:30:05 +0530
changeset 37544 d07afcfa7730
parent 30948 0a0972d3b58d
permissions -rw-r--r--
6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception Reviewed-by: serb, prr, flar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
     2
 * Copyright (c) 1998, 2016, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.RasterFormatException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.ComponentSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.PixelInterleavedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class defines a Raster with pixels consisting of one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * 8-bit data elements stored in close proximity to each other in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * single byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The bit precision per data element is that of the data type (that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is, the bit precision for this Raster is 8).  There is only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * pixel stride and one scanline stride for all bands.  This type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Raster can be used with a ComponentColorModel if there are multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * bands, or an IndexColorModel if there is only one band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class ByteInterleavedRaster extends ByteComponentRaster {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** True if the data offsets range from 0 to (pixelStride - 1) in order. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    boolean inOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The DataBuffer offset, minus sampleModelTranslateX*pixelStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * minus sampleModelTranslateY*scanlineStride, used to calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * pixel offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int dbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** True if a SinglePixelPackedSampleModel is being used. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    boolean packed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** If packed == true, the SampleModel's bit masks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    int[] bitMasks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** If packed == true, the SampleModel's bit offsets. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int[] bitOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** A cached copy of minX + width for use in bounds checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int maxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** A cached copy of minY + height for use in bounds checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Constructs a ByteInterleavedRaster with the given SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The Raster's upper left corner is origin and it is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * size as the SampleModel.  A DataBuffer large enough to describe the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Raster is automatically created.  SampleModel must be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * SinglePixelPackedSampleModel or InterleavedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param origin          The Point that specified the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public ByteInterleavedRaster(SampleModel sampleModel, Point origin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this(sampleModel,
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
    89
             (DataBufferByte) sampleModel.createDataBuffer(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             new Rectangle(origin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                           origin.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                           sampleModel.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                           sampleModel.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Constructs a ByteInterleavedRaster with the given SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * and DataBuffer.  The Raster's upper left corner is origin and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * it is the same size as the SampleModel.  The DataBuffer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * initialized and must be a DataBufferByte compatible with SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * SampleModel must be of type SinglePixelPackedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * or InterleavedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param sampleModel     The SampleModel that specifies the layout.
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   106
     * @param dataBuffer      The DataBufferByte that contains the image data.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param origin          The Point that specifies the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public ByteInterleavedRaster(SampleModel sampleModel,
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   110
                                 DataBufferByte dataBuffer,
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   111
                                 Point origin)
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   112
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this(sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             new Rectangle(origin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                           origin.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                           sampleModel.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                           sampleModel.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
             origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
             null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*** Analyzes a ComponentSampleModel to determine if it can function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * as a PixelInterleavedSampleModel.  In order to do so, it must use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * only bank 0 of its DataBuffer, and the data offsets must span a range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * of less than pixelStride.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p> These properties are trivially true for a 1-banded SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private boolean isInterleaved(ComponentSampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Analyze ComponentSampleModel to determine if it has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // properties of a PixelInterleavedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int numBands = sampleModel.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (numBands == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // Determine banks used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int[] bankIndices = sm.getBankIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (bankIndices[i] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Determine range of band offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int[] bandOffsets = sm.getBandOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int minOffset = bandOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int maxOffset = minOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i = 1; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int offset = bandOffsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (offset < minOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                minOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (offset > maxOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                maxOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (maxOffset - minOffset >= sm.getPixelStride()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Constructs a ByteInterleavedRaster with the given SampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * DataBuffer, and parent.  DataBuffer must be a DataBufferByte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * SampleModel must be of type SinglePixelPackedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * or InterleavedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * When translated into the base Raster's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * coordinate system, aRegion must be contained by the base Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Origin is the coordinate in the new Raster's coordinate system of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * the origin of the base Raster.  (The base Raster is the Raster's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * ancestor which has no parent.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Note that this constructor should generally be called by other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * constructors or create methods, it should not be used directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param sampleModel     The SampleModel that specifies the layout.
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   181
     * @param dataBuffer      The DataBufferByte that contains the image data.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param aRegion         The Rectangle that specifies the image area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param origin          The Point that specifies the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param parent          The parent (if any) of this raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public ByteInterleavedRaster(SampleModel sampleModel,
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   187
                                 DataBufferByte dataBuffer,
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   188
                                 Rectangle aRegion,
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   189
                                 Point origin,
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   190
                                 ByteInterleavedRaster parent)
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   191
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        super(sampleModel, dataBuffer, aRegion, origin, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.maxX = minX + width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this.maxY = minY + height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   196
        this.data = stealData(dataBuffer, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int xOffset = aRegion.x - origin.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int yOffset = aRegion.y - origin.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (sampleModel instanceof PixelInterleavedSampleModel ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            (sampleModel instanceof ComponentSampleModel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
             isInterleaved((ComponentSampleModel)sampleModel))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            ComponentSampleModel csm = (ComponentSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            this.scanlineStride = csm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            this.pixelStride = csm.getPixelStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            this.dataOffsets = csm.getBandOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            for (int i = 0; i < getNumDataElements(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                dataOffsets[i] += xOffset*pixelStride+yOffset*scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            SinglePixelPackedSampleModel sppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    (SinglePixelPackedSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            this.packed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            this.bitMasks = sppsm.getBitMasks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            this.bitOffsets = sppsm.getBitOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            this.scanlineStride = sppsm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            this.pixelStride = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            this.dataOffsets = new int[1];
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
   219
            this.dataOffsets[0] = dataBuffer.getOffset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            dataOffsets[0] += xOffset*pixelStride+yOffset*scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new RasterFormatException("ByteInterleavedRasters must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
              "have PixelInterleavedSampleModel, SinglePixelPackedSampleModel"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
              " or interleaved ComponentSampleModel.  Sample model is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
              sampleModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this.bandOffset = this.dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        this.dbOffsetPacked = dataBuffer.getOffset() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            sampleModelTranslateY*scanlineStride -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            sampleModelTranslateX*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this.dbOffset = dbOffsetPacked -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            (xOffset*pixelStride+yOffset*scanlineStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Set inOrder to true if the data elements are in order and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // have no gaps between them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        this.inOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (numDataElements == pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            inOrder = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            for (int i = 1; i < numDataElements; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (dataOffsets[i] - dataOffsets[0] != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    inOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   248
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Returns a copy of the data offsets array. For each band the data offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * is the index into the band's data array, of the first sample of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public int[] getDataOffsets() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 16093
diff changeset
   257
        return dataOffsets.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns the data offset for the specified band.  The data offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * is the index into the data array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * in which the first sample of the first scanline is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param band  The band whose offset is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public int getDataOffset(int band) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the scanline stride -- the number of data array elements between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * a given sample and the sample in the same column of the next row in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * same band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public int getScanlineStride() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns pixel stride -- the number of data array elements between two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * samples for the same band on the same scanline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public int getPixelStride() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Returns a reference to the data array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public byte[] getDataStorage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the data elements for all bands at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * if the pixel coordinate is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param x        The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param y        The Y coordinate of the pixel location.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   303
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *                 getTransferType() and length getNumDataElements().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *                 If null an array of appropriate type and size will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *                 allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return         An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *                 getTransferType() with the request pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public Object getDataElements(int x, int y, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        byte outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            outData = new byte[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            outData = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int off = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                  (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        for (int band = 0; band < numDataElements; band++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            outData[band] = data[dataOffsets[band] + off];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns an array of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *       byte[] bandData = (byte[])raster.getDataElements(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *       int numDataElements = raster.getNumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *       byte[] pixel = new byte[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *       // To find a data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *                        pixel, 0, numDataElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param y        The Y coordinate of the upper left pixel location.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   349
     * @param w        Width of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   350
     * @param h        Height of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   351
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *                 getTransferType() and length w*h*getNumDataElements().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *                 If null an array of appropriate type and size will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *                 allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return         An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *                 getTransferType() with the request pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public Object getDataElements(int x, int y, int w, int h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return getByteData(x, y, w, h, (byte[])obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Returns a byte array of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * region for the specified band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *       byte[] bandData = raster.getByteData(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *       // To find the data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *       byte bandElement = bandData[((y2-y)*w + (x2-x))];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param y        The Y coordinate of the upper left pixel location.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   374
     * @param w        Width of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   375
     * @param h        Height of the pixel rectangle.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param band     The band to return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param outData  If non-null, data elements for all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *                 at the specified location are returned in this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return         Data array with data elements for all bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public byte[] getByteData(int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                              int band, byte[] outData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Bounds check for 'band' will be performed automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (outData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            outData = new byte[w*h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                   (x-minX)*pixelStride + dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (pixelStride == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (scanlineStride == w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                System.arraycopy(data, yoff, outData, 0, w*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    System.arraycopy(data, yoff, outData, off, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    outData[off++] = data[xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Returns a byte array of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *       byte[] bandData = raster.getByteData(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *       int numDataElements = raster.getnumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *       byte[] pixel = new byte[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *       // To find a data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *                        pixel, 0, numDataElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param y        The Y coordinate of the upper left pixel location.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   435
     * @param w        Width of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   436
     * @param h        Height of the pixel rectangle.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param outData  If non-null, data elements for all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *                 at the specified location are returned in this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return         Data array with data elements for all bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public byte[] getByteData(int x, int y, int w, int h, byte[] outData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (outData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            outData = new byte[numDataElements*w*h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                   (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (inOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            int rowBytes = w*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (scanlineStride == rowBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                System.arraycopy(data, yoff, outData, off, rowBytes*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    System.arraycopy(data, yoff, outData, off, rowBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    off += rowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        } else if (numDataElements == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    outData[off++] = data[xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        } else if (numDataElements == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            int d1 = dataOffsets[1] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    outData[off++] = data[xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    outData[off++] = data[xoff + d1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        } else if (numDataElements == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            int d1 = dataOffsets[1] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            int d2 = dataOffsets[2] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    outData[off++] = data[xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    outData[off++] = data[xoff + d1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    outData[off++] = data[xoff + d2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else if (numDataElements == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            int d1 = dataOffsets[1] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            int d2 = dataOffsets[2] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            int d3 = dataOffsets[3] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    outData[off++] = data[xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    outData[off++] = data[xoff + d1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    outData[off++] = data[xoff + d2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    outData[off++] = data[xoff + d3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    for (int c = 0; c < numDataElements; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        outData[off++] = data[dataOffsets[c] + xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Stores the data elements for all bands at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * if the pixel coordinate is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param x        The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param y        The Y coordinate of the pixel location.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   534
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *                 getTransferType() and length getNumDataElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *                 containing the pixel data to place at x,y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void setDataElements(int x, int y, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        byte inData[] = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        int off = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                  (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        for (int i = 0; i < numDataElements; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            data[dataOffsets[i] + off] = inData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Stores the Raster data at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param x          The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param y          The Y coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param inRaster   Raster of data to place at x,y location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public void setDataElements(int x, int y, Raster inRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int srcOffX = inRaster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        int srcOffY = inRaster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int dstOffX = x + srcOffX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        int dstOffY = y + srcOffY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        int width  = inRaster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int height = inRaster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        setDataElements(dstOffX, dstOffY, srcOffX, srcOffY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        width, height, inRaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Stores the Raster data at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param dstX The absolute X coordinate of the destination pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * that will receive a copy of the upper-left pixel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * inRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @param dstY The absolute Y coordinate of the destination pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * that will receive a copy of the upper-left pixel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * inRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param srcX The absolute X coordinate of the upper-left source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * pixel that will be copied into this Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param srcY The absolute Y coordinate of the upper-left source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * pixel that will be copied into this Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param width      The number of pixels to store horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param height     The number of pixels to store vertically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param inRaster   Raster of data to place at x,y location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    private void setDataElements(int dstX, int dstY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                 int srcX, int srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                 int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                 Raster inRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        // Assume bounds checking has been performed previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (width <= 0 || height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        // Write inRaster (minX, minY) to (dstX, dstY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        int srcOffX = inRaster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        int srcOffY = inRaster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        Object tdata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (inRaster instanceof ByteInterleavedRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            ByteInterleavedRaster bct = (ByteInterleavedRaster) inRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            byte[] bdata = bct.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // copy whole scanlines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (inOrder && bct.inOrder && pixelStride == bct.pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                int toff = bct.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                int tss  = bct.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                int tps  = bct.getPixelStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                int srcOffset = toff +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    (srcY - srcOffY) * tss +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    (srcX - srcOffX) * tps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                int dstOffset = dataOffsets[0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    (dstY - minY) * scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    (dstX - minX) * pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                int nbytes = width*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                for (int tmpY=0; tmpY < height; tmpY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    System.arraycopy(bdata, srcOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                     data, dstOffset, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    srcOffset += tss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    dstOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        for (int startY=0; startY < height; startY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // Grab one scanline at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                             width, 1, tdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            setDataElements(dstX, dstY + startY, width, 1, tdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Stores an array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * for the nth band at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @param h        Height of the pixel rectangle.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   664
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *                 getTransferType() and length w*h*getNumDataElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *                 containing the pixel data to place between x,y and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *                 x+h, y+h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public void setDataElements(int x, int y, int w, int h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        putByteData(x, y, w, h, (byte[])obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Stores a byte array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * region for the specified band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *      inData[((y2-y)*w + (x2-x)) + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param h        Height of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @param band     The band to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param inData   The data elements to be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public void putByteData(int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                            int band, byte[] inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        // Bounds check for 'band' will be performed automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                   (x-minX)*pixelStride + dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (pixelStride == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if (scanlineStride == w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                System.arraycopy(inData, 0, data, yoff, w*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    System.arraycopy(inData, off, data, yoff, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Stores a byte array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * for the nth band at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param h        Height of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param inData   The data elements to be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public void putByteData(int x, int y, int w, int h, byte[] inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                   (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (inOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int rowBytes = w*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            if (rowBytes == scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                System.arraycopy(inData, 0, data, yoff, rowBytes*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    System.arraycopy(inData, off, data, yoff, rowBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    off += rowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        } else if (numDataElements == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        } else if (numDataElements == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            int d1 = dataOffsets[1] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    data[xoff + d1] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        } else if (numDataElements == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            int d1 = dataOffsets[1] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            int d2 = dataOffsets[2] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    data[xoff + d1] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    data[xoff + d2] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        } else if (numDataElements == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            int d1 = dataOffsets[1] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            int d2 = dataOffsets[2] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            int d3 = dataOffsets[3] - dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    data[xoff + d1] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    data[xoff + d2] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    data[xoff + d3] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    for (int c = 0; c < numDataElements; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        data[dataOffsets[c] + xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    public int getSample(int x, int y, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        if (packed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            int offset = y*scanlineStride + x + dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            byte sample = data[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            return (sample & bitMasks[b]) >>> bitOffsets[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            int offset = y*scanlineStride + x*pixelStride + dbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return data[offset + dataOffsets[b]] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public void setSample(int x, int y, int b, int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if (packed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            int offset = y*scanlineStride + x + dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            int bitMask = bitMasks[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            byte value = data[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            value &= ~bitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            value |= (s << bitOffsets[b]) & bitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            data[offset] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            int offset = y*scanlineStride + x*pixelStride + dbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            data[offset + dataOffsets[b]] = (byte)s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public int[] getSamples(int x, int y, int w, int h, int b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                            int[] iArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int samples[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (iArray != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            samples = iArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            samples = new int [w*h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int lineOffset = y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int dstOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (packed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            lineOffset += dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            int bitMask = bitMasks[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            int bitOffset = bitOffsets[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                int sampleOffset = lineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    int value = data[sampleOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    samples[dstOffset++] = ((value & bitMask) >>> bitOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            lineOffset += dbOffset + dataOffsets[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                int sampleOffset = lineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    samples[dstOffset++] = data[sampleOffset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    sampleOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        return samples;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    public void setSamples(int x, int y, int w, int h, int b, int iArray[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        int lineOffset = y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        int srcOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (packed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            lineOffset += dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            int bitMask = bitMasks[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                int sampleOffset = lineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    byte value = data[sampleOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    value &= ~bitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    int sample = iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    value |= (sample << bitOffsets[b]) & bitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    data[sampleOffset++] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            lineOffset += dbOffset + dataOffsets[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            for (int i = 0; i < h; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                int sampleOffset = lineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                for (int j = 0; j < w; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    data[sampleOffset] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    sampleOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public int[] getPixels(int x, int y, int w, int h, int[] iArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        int pixels[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if (iArray != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            pixels = iArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            pixels = new int[w*h*numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        int lineOffset = y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        int dstOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (packed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            lineOffset += dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    int value = data[lineOffset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    for (int k = 0; k < numBands; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        pixels[dstOffset++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                            (value & bitMasks[k]) >>> bitOffsets[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            lineOffset += dbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            int d0 = dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            if (numBands == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                        pixels[dstOffset++] = data[pixelOffset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            } else if (numBands == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                int d1 = dataOffsets[1] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                        pixels[dstOffset++] = data[pixelOffset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                        pixels[dstOffset++] = data[pixelOffset + d1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            } else if (numBands == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                int d1 = dataOffsets[1] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                int d2 = dataOffsets[2] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                        pixels[dstOffset++] = data[pixelOffset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                        pixels[dstOffset++] = data[pixelOffset + d1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                        pixels[dstOffset++] = data[pixelOffset + d2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            } else if (numBands == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                int d1 = dataOffsets[1] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                int d2 = dataOffsets[2] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                int d3 = dataOffsets[3] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                        pixels[dstOffset++] = data[pixelOffset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                        pixels[dstOffset++] = data[pixelOffset + d1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        pixels[dstOffset++] = data[pixelOffset + d2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                        pixels[dstOffset++] = data[pixelOffset + d3] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    int pixelOffset = lineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        for (int k = 0; k < numBands; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                            pixels[dstOffset++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                                data[pixelOffset + dataOffsets[k]] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        return pixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public void setPixels(int x, int y, int w, int h, int[] iArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        int lineOffset = y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        int srcOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if (packed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            lineOffset += dbOffsetPacked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    for (int k = 0; k < numBands; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                        int srcValue = iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                        value |= ((srcValue << bitOffsets[k])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                                  & bitMasks[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    data[lineOffset + i] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            lineOffset += dbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            int d0 = dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            if (numBands == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                        data[pixelOffset] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            } else if (numBands == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                int d1 = dataOffsets[1] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                        data[pixelOffset] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        data[pixelOffset + d1] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            } else if (numBands == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                int d1 = dataOffsets[1] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                int d2 = dataOffsets[2] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                        data[pixelOffset] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        data[pixelOffset + d1] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        data[pixelOffset + d2] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            } else if (numBands == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                int d1 = dataOffsets[1] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                int d2 = dataOffsets[2] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                int d3 = dataOffsets[3] - d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    int pixelOffset = lineOffset + d0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                        data[pixelOffset] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        data[pixelOffset + d1] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                        data[pixelOffset + d2] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        data[pixelOffset + d3] = (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    int pixelOffset = lineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                        for (int k = 0; k < numBands; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                            data[pixelOffset + dataOffsets[k]] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                                (byte)iArray[srcOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                        pixelOffset += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    lineOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public void setRect(int dx, int dy, Raster srcRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        if (!(srcRaster instanceof ByteInterleavedRaster)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            super.setRect(dx, dy, srcRaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        int width  = srcRaster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        int height = srcRaster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        int srcOffX = srcRaster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        int srcOffY = srcRaster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        int dstOffX = dx+srcOffX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        int dstOffY = dy+srcOffY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        // Clip to this raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (dstOffX < this.minX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            int skipX = minX - dstOffX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            width -= skipX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            srcOffX += skipX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            dstOffX = this.minX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        if (dstOffY < this.minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            int skipY = this.minY - dstOffY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            height -= skipY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            srcOffY += skipY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            dstOffY = this.minY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        if (dstOffX+width > this.maxX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            width = this.maxX - dstOffX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (dstOffY+height > this.maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            height = this.maxY - dstOffY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        setDataElements(dstOffX, dstOffY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                        srcOffX, srcOffY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                        width, height, srcRaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Creates a subraster given a region of the raster.  The x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * coordinates specify the horizontal and vertical offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * from the upper-left corner of this raster to the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * of the subraster.  A subset of the bands of the parent Raster may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * be specified.  If this is null, then all the bands are present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * subRaster. A translation to the subRaster may also be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * Note that the subraster will reference the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * DataBuffer as the parent raster, but using different offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @param x               X offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @param y               Y offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @param width           Width (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @param height          Height (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @param x0              Translated X origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @param y0              Translated Y origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @param bandList        Array of band indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * @exception RasterFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     *            if the specified bounding box is outside of the parent raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    public Raster createChild(int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                              int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                              int x0, int y0, int[] bandList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        WritableRaster newRaster = createWritableChild(x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                                                       width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                                                       x0, y0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                                                       bandList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        return (Raster) newRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * Creates a Writable subRaster given a region of the Raster. The x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * coordinates specify the horizontal and vertical offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * from the upper-left corner of this Raster to the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * of the subRaster.  A subset of the bands of the parent Raster may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * be specified.  If this is null, then all the bands are present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * subRaster. A translation to the subRaster may also be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * Note that the subRaster will reference the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * DataBuffer as the parent Raster, but using different offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @param x               X offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @param y               Y offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @param width           Width (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @param height          Height (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @param x0              Translated X origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @param y0              Translated Y origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @param bandList        Array of band indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @exception RasterFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *            if the specified bounding box is outside of the parent Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    public WritableRaster createWritableChild(int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                                              int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                              int x0, int y0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                                              int[] bandList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        if (x < this.minX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            throw new RasterFormatException("x lies outside the raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (y < this.minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            throw new RasterFormatException("y lies outside the raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if ((x+width < x) || (x+width > this.minX + this.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            throw new RasterFormatException("(x + width) is outside of Raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if ((y+height < y) || (y+height > this.minY + this.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            throw new RasterFormatException("(y + height) is outside of Raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        SampleModel sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        if (bandList != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            sm = sampleModel.createSubsetSampleModel(bandList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            sm = sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        int deltaX = x0 - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        int deltaY = y0 - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        return new ByteInterleavedRaster(sm,
37544
d07afcfa7730 6353518: Creation of a WritableRaster with a custom DataBuffer causes erroneous Exception
aghaisas
parents: 30948
diff changeset
  1257
                                       (DataBufferByte) dataBuffer,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                                       new Rectangle(x0, y0, width, height),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                       new Point(sampleModelTranslateX+deltaX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                                                 sampleModelTranslateY+deltaY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                                       this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * Creates a Raster with the same layout but using a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * width and height, and with new zeroed data arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    public WritableRaster createCompatibleWritableRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        if (w <= 0 || h <=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            throw new RasterFormatException("negative "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                                          ((w <= 0) ? "width" : "height"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        return new ByteInterleavedRaster(sm, new Point(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * Creates a Raster with the same layout and the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * width and height, and with new zeroed data arrays.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * the Raster is a subRaster, this will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * createCompatibleRaster(width, height).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    public WritableRaster createCompatibleWritableRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return createCompatibleWritableRaster(width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        return new String ("ByteInterleavedRaster: width = "+width+" height = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                           + height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                           +" #numDataElements "+numDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                           //  +" xOff = "+xOffset+" yOff = "+yOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                           +" dataOff[0] = "+dataOffsets[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
//    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
//     * For debugging...  prints a region of a one-band ByteInterleavedRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
//     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
//    public void print(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
//        // REMIND:  Only works for 1 band!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
//        System.out.println(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
//        int offset = dataOffsets[0] + y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
//        int off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
//        for (int yoff=0; yoff < h; yoff++, offset += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
//            off = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
//            System.out.print("Line "+(y+yoff)+": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
//            for (int xoff = 0; xoff < w; xoff++, off+= pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
//                String s = Integer.toHexString(data[off]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
//                if (s.length() == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
//                    s = s.substring(6,8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
//                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
//                System.out.print(s+" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
//            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
//            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
//        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
}