jdk/src/share/classes/sun/awt/image/IntegerInterleavedRaster.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 16868 b5e2827ecc50
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2007, 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.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.DataBufferInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class defines a Raster with pixels consisting of one or more 32-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * data elements stored in close proximity to each other in a integer array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The bit precision per data element is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of the data type (that is, the bit precision for this raster is 32).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * There is only one pixel stride and one scanline stride for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * bands.  For a given pixel, all samples fit in N data elements and these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * N data elements hold samples for only one pixel.  This type of Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * can be used with a PackedColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * For example, if there is only one data element per pixel, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * SinglePixelPackedSampleModel can be used to represent multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * bands with a PackedColorModel (including a DirectColorModel) for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * color interpretation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class IntegerInterleavedRaster extends IntegerComponentRaster {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** A cached copy of minX + width for use in bounds checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int maxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** A cached copy of minY + height for use in bounds checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *  Constructs a IntegerInterleavedRaster with the given SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *  The Raster's upper left corner is origin and it is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *  size as the SampleModel.  A DataBuffer large enough to describe the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *  Raster is automatically created.  SampleModel must be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *  SinglePixelPackedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *  @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *  @param origin          The Point that specified the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public IntegerInterleavedRaster(SampleModel sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                     Point origin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this(sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             sampleModel.createDataBuffer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             new Rectangle(origin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                           origin.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                           sampleModel.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                           sampleModel.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
             null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Constructs a IntegerInterleavedRaster with the given SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * and DataBuffer.  The Raster's upper left corner is origin and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * it is the same sizes the SampleModel.  The DataBuffer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * initialized and must be a DataBufferInt compatible with SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * SampleModel must be of type SinglePixelPackedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param dataBuffer      The DataBufferInt that contains the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param origin          The Point that specifies the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public IntegerInterleavedRaster(SampleModel sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                     DataBuffer dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                     Point origin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this(sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             new Rectangle(origin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                           origin.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                           sampleModel.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                           sampleModel.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
             origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
             null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Constructs a IntegerInterleavedRaster with the given SampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * DataBuffer, and parent.  DataBuffer must be a DataBufferInt and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * SampleModel must be of type SinglePixelPackedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * When translated into the base Raster's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * coordinate system, aRegion must be contained by the base Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Origin is the coodinate in the new Raster's coordinate system of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the origin of the base Raster.  (The base Raster is the Raster's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * ancestor which has no parent.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Note that this constructor should generally be called by other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * constructors or create methods, it should not be used directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param dataBuffer      The DataBufferInt that contains the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param aRegion         The Rectangle that specifies the image area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param origin          The Point that specifies the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param parent          The parent (if any) of this raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public IntegerInterleavedRaster(SampleModel sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                     DataBuffer dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                     Rectangle aRegion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                     Point origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                     IntegerInterleavedRaster parent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        super(sampleModel,dataBuffer,aRegion,origin,parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.maxX = minX + width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.maxY = minY + height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (!(dataBuffer instanceof DataBufferInt)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
           throw new RasterFormatException("IntegerInterleavedRasters must have" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                "integer DataBuffers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        DataBufferInt dbi = (DataBufferInt)dataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.data = stealData(dbi, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (sampleModel instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            SinglePixelPackedSampleModel sppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    (SinglePixelPackedSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            this.scanlineStride = sppsm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            this.pixelStride    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            this.dataOffsets = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            this.dataOffsets[0] = dbi.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            this.bandOffset = this.dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int xOffset = aRegion.x - origin.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int yOffset = aRegion.y - origin.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            dataOffsets[0] += xOffset+yOffset*scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.numDataElems = sppsm.getNumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new RasterFormatException("IntegerInterleavedRasters must have"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                            " SinglePixelPackedSampleModel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        verify(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns a copy of the data offsets array. For each band the data offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * is the index into the band's data array, of the first sample of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public int[] getDataOffsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return (int[]) dataOffsets.clone();
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
     * Returns data offset for the specified band.  The data offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * is the index into the data array in which the first sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * of the first scanline is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public int getDataOffset(int band) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns the scanline stride -- the number of data array elements between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * a given sample and the sample in the same column of the next row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public int getScanlineStride() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns pixel stride -- the number of data array elements  between two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * samples for the same band on the same scanline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public int getPixelStride() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns a reference to the data array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public int[] getDataStorage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Returns the data elements for all bands at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * if the pixel coordinate is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param x        The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param y        The Y coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param outData  An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *                 getTransferType() and length getNumDataElements().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *                 If null an array of appropriate type and size will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *                 allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return         An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *                 getTransferType() with the request pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Object getDataElements(int x, int y, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            outData = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            outData = (int[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int off = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        outData[0] = data[off];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Returns an array  of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *       int[] bandData = (int[])raster.getDataElements(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *       int numDataElements = raster.getNumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *       int[] pixel = new int[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *       // To find a data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *                        pixel, 0, numDataElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param width    Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param height   Height of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param outData  An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *                 getTransferType() and length w*h*getNumDataElements().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *                 If null an array of appropriate type and size will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *                 allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return         An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *                 getTransferType() with the request pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public Object getDataElements(int x, int y, int w, int h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (obj instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            outData = (int[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            outData = new int[w*h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int yoff = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        for (int ystart = 0; ystart < h; ystart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            System.arraycopy(data, yoff, outData, off, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            yoff += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Stores the data elements for all bands at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * if the pixel coordinate is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param x        The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param y        The Y coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param inData   An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *                 getTransferType() and length getNumDataElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *                 containing the pixel data to place at x,y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public void setDataElements(int x, int y, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int inData[] = (int[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int off = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        data[off] = inData[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Stores the Raster data at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * The transferType of the inputRaster must match this raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * An ArrayIndexOutOfBoundsException will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param x          The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param y          The Y coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param inRaster   Raster of data to place at x,y location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void setDataElements(int x, int y, Raster inRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int dstOffX = x + inRaster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int dstOffY = y + inRaster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int width  = inRaster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int height = inRaster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        setDataElements(dstOffX, dstOffY, width, height, inRaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Stores the Raster data at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param dstX The absolute X coordinate of the destination pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * that will receive a copy of the upper-left pixel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * inRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param dstY The absolute Y coordinate of the destination pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * that will receive a copy of the upper-left pixel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * inRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param width      The number of pixels to store horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param height     The number of pixels to store vertically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param inRaster   Raster of data to place at x,y location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private void setDataElements(int dstX, int dstY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                 int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                 Raster inRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // Assume bounds checking has been performed previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (width <= 0 || height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // Write inRaster (minX, minY) to (dstX, dstY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int srcOffX = inRaster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int srcOffY = inRaster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        int tdata[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (inRaster instanceof IntegerInterleavedRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            IntegerInterleavedRaster ict = (IntegerInterleavedRaster) inRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            // Extract the raster parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            tdata    = ict.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            int tss  = ict.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            int toff = ict.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            int srcOffset = toff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            int dstOffset = dataOffsets[0]+(dstY-minY)*scanlineStride+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                           (dstX-minX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // Fastest case.  We can copy scanlines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // Loop through all of the scanlines and copy the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            for (int startY=0; startY < height; startY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                System.arraycopy(tdata, srcOffset, data, dstOffset, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                srcOffset += tss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                dstOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        Object odata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        for (int startY=0; startY < height; startY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // Grab one scanline at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            odata = inRaster.getDataElements(srcOffX, srcOffY+startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                             width, 1, odata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            setDataElements(dstX, dstY+startY, width, 1, odata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Stores an array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * for the nth band at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param h        Height of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param inData   An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *                 getTransferType() and length w*h*getNumDataElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *                 containing the pixel data to place between x,y and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *                 x+h, y+h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public void setDataElements(int x, int y, int w, int h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        int inData[] = (int[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int yoff = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        for (int ystart = 0; ystart < h; ystart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            System.arraycopy(inData, off, data, yoff, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            yoff += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Creates a subraster given a region of the raster.  The x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * coordinates specify the horizontal and vertical offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * from the upper-left corner of this raster to the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * of the subraster.  A subset of the bands of the parent Raster may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * be specified.  If this is null, then all the bands are present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * subRaster. A translation to the subRaster may also be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Note that the subraster will reference the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * DataBuffer as the parent raster, but using different offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param x               X offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param y               Y offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param width           Width (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param height          Height (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param x0              Translated X origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param y0              Translated Y origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param bandList        Array of band indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception RasterFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *            if the specified bounding box is outside of the parent raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public WritableRaster createWritableChild (int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                               int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                               int x0, int y0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                               int bandList[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (x < this.minX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throw new RasterFormatException("x lies outside raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (y < this.minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            throw new RasterFormatException("y lies outside raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if ((x+width < x) || (x+width > this.minX + this.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new RasterFormatException("(x + width) is outside raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if ((y+height < y) || (y+height > this.minY + this.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            throw new RasterFormatException("(y + height) is outside raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        SampleModel sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (bandList != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            sm = sampleModel.createSubsetSampleModel(bandList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            sm = sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        int deltaX = x0 - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        int deltaY = y0 - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return new IntegerInterleavedRaster(sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                          dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                          new Rectangle(x0,y0,width,height),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                          new Point(sampleModelTranslateX+deltaX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                                    sampleModelTranslateY+deltaY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                          this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Creates a subraster given a region of the raster.  The x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * coordinates specify the horizontal and vertical offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * from the upper-left corner of this raster to the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * of the subraster.  A subset of the bands of the parent raster may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * be specified. If this is null, then all the bands are present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * subRaster. Note that the subraster will reference the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * DataBuffer as the parent raster, but using different offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param x               X offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param y               Y offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param width           Width (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param height          Height (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param x0              Translated X origin of the subRaster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param y0              Translated Y origin of the subRaster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param bandList        Array of band indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @exception RasterFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *            if the specified bounding box is outside of the parent raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public Raster createChild (int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                   int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                   int x0, int y0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                   int bandList[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return createWritableChild(x, y, width, height, x0, y0, bandList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Creates a raster with the same band layout but using a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * width and height, and with new zeroed data arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public WritableRaster createCompatibleWritableRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (w <= 0 || h <=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            throw new RasterFormatException("negative "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                          ((w <= 0) ? "width" : "height"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        SampleModel sm = sampleModel.createCompatibleSampleModel(w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return new IntegerInterleavedRaster(sm, new Point(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Creates a raster with the same data layout and the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * width and height, and with new zeroed data arrays.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the raster is a subraster, this will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * createCompatibleRaster(width, height).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public WritableRaster createCompatibleWritableRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return createCompatibleWritableRaster(width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Verify that the layout parameters are consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * the data.  If strictCheck
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * is false, this method will check for ArrayIndexOutOfBounds conditions.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * strictCheck is true, this method will check for additional error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * conditions such as line wraparound (width of a line greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * the scanline stride).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return   String   Error string, if the layout is incompatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *                    the data.  Otherwise returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    private void verify (boolean strictCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int maxSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        size = (height-1)*scanlineStride + (width-1) + dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (size > maxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            maxSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (data.length < maxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            throw new RasterFormatException("Data array too small (should be "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                          maxSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                          +" but is "+data.length+" )");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return new String ("IntegerInterleavedRaster: width = "+width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                           +" height = " + height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                           +" #Bands = " + numBands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                           +" xOff = "+sampleModelTranslateX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                           +" yOff = "+sampleModelTranslateY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                           +" dataOffset[0] "+dataOffsets[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
//    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
//     * For debugging...  prints a region of a one-band IntegerInterleavedRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
//     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
//    public void print(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
//        // REMIND:  Only works for 1 band!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
//        System.out.println(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
//        int offset = dataOffsets[0] + y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
//        int off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
//        for (int yoff=0; yoff < h; yoff++, offset += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
//            off = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
//            System.out.print("Line "+(sampleModelTranslateY+y+yoff)+": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
//            for (int xoff = 0; xoff < w; xoff++, off+= pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
//                System.out.print(Integer.toHexString(data[off])+" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
//            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
//            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
//        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
}