jdk/src/java.desktop/share/classes/sun/awt/image/ByteComponentRaster.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 30948 0a0972d3b58d
child 37544 d07afcfa7730
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18246
diff changeset
     2
 * Copyright (c) 1997, 2014, 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.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.DataBuffer;
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 8-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * data elements stored in close proximity to each other in a single byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The bit precision per data element is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of the data type (that is, the bit precision for this Raster is 8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * There is only one pixel stride and one scanline stride for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * bands.  This type of Raster can be used with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * ComponentColorModel if there are multiple bands, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * IndexColorModel if there is only one band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * For example, 3-3-2 RGB image data can be represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ByteComponentRaster using a SinglePixelPackedSampleModel and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * a ComponentColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class ByteComponentRaster extends SunWritableRaster {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** private band offset for use by native code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected int bandOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Data offsets for each band of image data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected int[]         dataOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** Scanline stride of the image data contained in this Raster. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected int           scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** Pixel stride of the image data contained in this Raster. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected int           pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** The image data array. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected byte[]        data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** A cached copy of minX + width for use in bounds checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int maxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** A cached copy of minY + height for use in bounds checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private int maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30948
diff changeset
    79
    private static native void initIDs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        NativeLibLoader.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Constructs a ByteComponentRaster with the given SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The Raster's upper left corner is origin and it is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * size as the SampleModel.  A DataBuffer large enough to describe the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Raster is automatically created.  SampleModel must be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * SinglePixelPackedSampleModel or ComponentSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param origin          The Point that specified the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public ByteComponentRaster(SampleModel sampleModel, Point origin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this(sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             sampleModel.createDataBuffer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
             new Rectangle(origin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                           origin.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                           sampleModel.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                           sampleModel.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
             origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
             null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Constructs a ByteComponentRaster with the given SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * and DataBuffer.  The Raster's upper left corner is origin and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * it is the same size as the SampleModel.  The DataBuffer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * initialized and must be a DataBufferByte compatible with SampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * SampleModel must be of type SinglePixelPackedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * or ComponentSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param dataBuffer      The DataBufferShort that contains the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param origin          The Point that specifies the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public ByteComponentRaster(SampleModel sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                  DataBuffer dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                  Point origin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this(sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
             dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             new Rectangle(origin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                           origin.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                           sampleModel.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                           sampleModel.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Constructs a ByteComponentRaster with the given SampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * DataBuffer, and parent.  DataBuffer must be a DataBufferByte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * SampleModel must be of type SinglePixelPackedSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * or ComponentSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * When translated into the base Raster's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * coordinate system, aRegion must be contained by the base Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Origin is the coordinate in the new Raster's coordinate system of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * the origin of the base Raster.  (The base Raster is the Raster's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * ancestor which has no parent.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Note that this constructor should generally be called by other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * constructors or create methods, it should not be used directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param sampleModel     The SampleModel that specifies the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param dataBuffer      The DataBufferShort that contains the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param aRegion         The Rectangle that specifies the image area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param origin          The Point that specifies the origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param parent          The parent (if any) of this raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public ByteComponentRaster(SampleModel sampleModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                  DataBuffer dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                  Rectangle aRegion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                  Point origin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                  ByteComponentRaster parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super(sampleModel, dataBuffer, aRegion, origin, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.maxX = minX + width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.maxY = minY + height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (!(dataBuffer instanceof DataBufferByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new RasterFormatException("ByteComponentRasters must have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                            "byte DataBuffers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        DataBufferByte dbb = (DataBufferByte)dataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.data = stealData(dbb, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (dbb.getNumBanks() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                RasterFormatException("DataBuffer for ByteComponentRasters"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                      " must only have 1 bank.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int dbOffset = dbb.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (sampleModel instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            ComponentSampleModel ism = (ComponentSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            this.type = IntegerComponentRaster.TYPE_BYTE_SAMPLES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            this.scanlineStride = ism.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            this.pixelStride = ism.getPixelStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            this.dataOffsets = ism.getBandOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            int xOffset = aRegion.x - origin.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            int yOffset = aRegion.y - origin.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            for (int i = 0; i < getNumDataElements(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                dataOffsets[i] += dbOffset +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    xOffset*pixelStride+yOffset*scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            SinglePixelPackedSampleModel sppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    (SinglePixelPackedSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            this.type = IntegerComponentRaster.TYPE_BYTE_PACKED_SAMPLES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            this.scanlineStride = sppsm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            this.pixelStride    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            this.dataOffsets = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            this.dataOffsets[0] = dbOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int xOffset = aRegion.x - origin.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int yOffset = aRegion.y - origin.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            dataOffsets[0] += xOffset*pixelStride+yOffset*scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new RasterFormatException("IntegerComponentRasters must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                "have ComponentSampleModel or SinglePixelPackedSampleModel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.bandOffset = this.dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   201
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Returns a copy of the data offsets array. For each band the data offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * is the index into the band's data array, of the first sample of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public int[] getDataOffsets() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18246
diff changeset
   210
        return dataOffsets.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns the data offset for the specified band.  The data offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * is the index into the data array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * in which the first sample of the first scanline is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param band  The band whose offset is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int getDataOffset(int band) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns the scanline stride -- the number of data array elements between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * a given sample and the sample in the same column of the next row in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * same band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public int getScanlineStride() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns pixel stride -- the number of data array elements between two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * samples for the same band on the same scanline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int getPixelStride() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns a reference to the data array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public byte[] getDataStorage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Returns the data elements for all bands at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * if the pixel coordinate is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param x        The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @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
   256
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *                 getTransferType() and length getNumDataElements().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *                 If null an array of appropriate type and size will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *                 allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return         An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *                 getTransferType() with the request pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public Object getDataElements(int x, int y, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        byte outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            outData = new byte[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            outData = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int off = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                  (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        for (int band = 0; band < numDataElements; band++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            outData[band] = data[dataOffsets[band] + off];
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
     * Returns an array of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * region.
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 coordinates are 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
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *       byte[] bandData = (byte[])raster.getDataElements(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *       int numDataElements = raster.getNumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *       byte[] pixel = new byte[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *       // To find a data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *                        pixel, 0, numDataElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @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
   302
     * @param w        Width of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   303
     * @param h        Height of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   304
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *                 getTransferType() and length w*h*getNumDataElements().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *                 If null an array of appropriate type and size will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *                 allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return         An object reference to an array of type defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *                 getTransferType() with the request pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public Object getDataElements(int x, int y, int w, int h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        byte outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            outData = new byte[w*h*numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            outData = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                   (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                for (int c = 0; c < numDataElements; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    outData[off++] = data[dataOffsets[c] + xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns a byte array of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * region for the specified band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *       byte[] bandData = raster.getByteData(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *       // To find the data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *       byte bandElement = bandData[((y2-y)*w + (x2-x))];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @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
   355
     * @param w        Width of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   356
     * @param h        Height of the pixel rectangle.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param band     The band to return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param outData  If non-null, data elements for all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *                 at the specified location are returned in this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @return         Data array with data elements for all bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public byte[] getByteData(int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                              int band, byte[] outData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // Bounds check for 'band' will be performed automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (outData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            outData = new byte[scanlineStride*h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                   (x-minX)*pixelStride + dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (pixelStride == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (scanlineStride == w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                System.arraycopy(data, yoff, outData, 0, w*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    System.arraycopy(data, yoff, outData, off, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    outData[off++] = data[xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns a byte array of data elements from the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *       byte[] bandData = raster.getByteData(x, y, w, h, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *       int numDataElements = raster.getnumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *       byte[] pixel = new byte[numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *       // To find a data element at location (x2, y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *       System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *                        pixel, 0, numDataElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @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
   418
     * @param w        Width of the pixel rectangle.
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   419
     * @param h        Height of the pixel rectangle.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param outData  If non-null, data elements for all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *                 at the specified location are returned in this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return         Data array with data elements for all bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public byte[] getByteData(int x, int y, int w, int h, byte[] outData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (outData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            outData = new byte[numDataElements*scanlineStride*h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                   (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // REMIND: Should keep track if dataOffsets are in a nice order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                for (int c = 0; c < numDataElements; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    outData[off++] = data[dataOffsets[c] + xoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Stores the data elements for all bands at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * if the pixel coordinate is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param x        The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @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
   461
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *                 getTransferType() and length getNumDataElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *                 containing the pixel data to place at x,y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void setDataElements(int x, int y, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            (x >= this.maxX) || (y >= this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        byte inData[] = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int off = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                  (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        for (int i = 0; i < numDataElements; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            data[dataOffsets[i] + off] = inData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Stores the Raster data at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param x          The X coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param y          The Y coordinate of the pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param inRaster   Raster of data to place at x,y location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public void setDataElements(int x, int y, Raster inRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int dstOffX = inRaster.getMinX() + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int dstOffY = inRaster.getMinY() + y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        int width  = inRaster.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        int height = inRaster.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        setDataElements(dstOffX, dstOffY, width, height, inRaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Stores the Raster data at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param dstX The absolute X coordinate of the destination pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * that will receive a copy of the upper-left pixel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * inRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param dstY The absolute Y coordinate of the destination pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * that will receive a copy of the upper-left pixel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * inRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param width      The number of pixels to store horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param height     The number of pixels to store vertically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param inRaster   Raster of data to place at x,y location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private void setDataElements(int dstX, int dstY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                 int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                 Raster inRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // Assume bounds checking has been performed previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (width <= 0 || height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        int srcOffX = inRaster.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        int srcOffY = inRaster.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        Object tdata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (inRaster instanceof ByteComponentRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            ByteComponentRaster bct = (ByteComponentRaster) inRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            byte[] bdata = bct.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // REMIND: Do something faster!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (numDataElements == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                int toff = bct.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                int tss  = bct.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                int srcOffset = toff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                int dstOffset = dataOffsets[0]+(dstY-minY)*scanlineStride+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                                               (dstX-minX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (pixelStride == bct.getPixelStride()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    width *= pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    for (int tmpY=0; tmpY < height; tmpY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        System.arraycopy(bdata, srcOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                         data, dstOffset, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        srcOffset += tss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        dstOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        for (int startY=0; startY < height; startY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            // Grab one scanline at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                             width, 1, tdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            setDataElements(dstX, dstY+startY, width, 1, tdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Stores an array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * A ClassCastException will be thrown if the input object is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * and references anything other than an array of transferType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * for the nth band at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param h        Height of the pixel rectangle.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   580
     * @param obj      An object reference to an array of type defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *                 getTransferType() and length w*h*getNumDataElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *                 containing the pixel data to place between x,y and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *                 x+h, y+h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public void setDataElements(int x, int y, int w, int h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        byte inData[] = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                   (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (numDataElements == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            int srcOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            int dstOffset = yoff + dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            for (ystart=0; ystart < h; ystart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                System.arraycopy(inData, srcOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                 data, dstOffset, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                srcOffset += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                dstOffset += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                for (int c = 0; c < numDataElements; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    data[dataOffsets[c] + xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Stores a byte array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * region for the specified band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *      inData[((y2-y)*w + (x2-x)) + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param h        Height of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param band     The band to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param inData   The data elements to be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public void putByteData(int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                            int band, byte[] inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // Bounds check for 'band' will be performed automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                   (x-minX)*pixelStride + dataOffsets[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (pixelStride == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            if (scanlineStride == w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                System.arraycopy(inData, 0, data, yoff, w*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    System.arraycopy(inData, off, data, yoff, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Stores a byte array of data elements into the specified rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * An ArrayIndexOutOfBounds exception will be thrown at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * if the pixel coordinates are out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * The data elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * data array are assumed to be packed.  That is, a data element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * for the nth band at location (x2, y2) would be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *      inData[((y2-y)*w + (x2-x))*numDataElements + n]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param x        The X coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param y        The Y coordinate of the upper left pixel location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param w        Width of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @param h        Height of the pixel rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param inData   The data elements to be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public void putByteData(int x, int y, int w, int h, byte[] inData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if ((x < this.minX) || (y < this.minY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            (x + w > this.maxX) || (y + h > this.maxY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            throw new ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                ("Coordinate out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        int yoff = (y-minY)*scanlineStride +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                   (x-minX)*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        int xoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        int xstart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        int ystart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (numDataElements == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            yoff += dataOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (pixelStride == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                if (scanlineStride == w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    System.arraycopy(inData, 0, data, yoff, w*h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    for (ystart=0; ystart < h; ystart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        System.arraycopy(inData, off, data, yoff, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        off += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        yoff += scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        data[xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                xoff = yoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    for (int c = 0; c < numDataElements; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        data[dataOffsets[c] + xoff] = inData[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Creates a subraster given a region of the raster.  The x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * coordinates specify the horizontal and vertical offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * from the upper-left corner of this raster to the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * of the subraster.  A subset of the bands of the parent Raster may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * be specified.  If this is null, then all the bands are present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * subRaster. A translation to the subRaster may also be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Note that the subraster will reference the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * DataBuffer as the parent raster, but using different offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @param x               X offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @param y               Y offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @param width           Width (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @param height          Height (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @param x0              Translated X origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param y0              Translated Y origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @param bandList        Array of band indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @exception RasterFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *            if the specified bounding box is outside of the parent raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public Raster createChild(int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                              int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                              int x0, int y0, int[] bandList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        WritableRaster newRaster = createWritableChild(x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                                       width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                                       x0, y0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                                       bandList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return (Raster) newRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Creates a Writable subRaster given a region of the Raster. The x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * coordinates specify the horizontal and vertical offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * from the upper-left corner of this Raster to the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * of the subRaster.  A subset of the bands of the parent Raster may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * be specified.  If this is null, then all the bands are present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * subRaster. A translation to the subRaster may also be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Note that the subRaster will reference the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * DataBuffer as the parent Raster, but using different offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @param x               X offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @param y               Y offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @param width           Width (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @param height          Height (in pixels) of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param x0              Translated X origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @param y0              Translated Y origin of the subraster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @param bandList        Array of band indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @exception RasterFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *            if the specified bounding box is outside of the parent Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    public WritableRaster createWritableChild(int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                              int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                              int x0, int y0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                              int[] bandList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if (x < this.minX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            throw new RasterFormatException("x lies outside the raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        if (y < this.minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            throw new RasterFormatException("y lies outside the raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if ((x+width < x) || (x+width > this.minX + this.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            throw new RasterFormatException("(x + width) is outside of Raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if ((y+height < y) || (y+height > this.minY + this.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            throw new RasterFormatException("(y + height) is outside of Raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        SampleModel sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (bandList != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            sm = sampleModel.createSubsetSampleModel(bandList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            sm = sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        int deltaX = x0 - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        int deltaY = y0 - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return new ByteComponentRaster(sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                       dataBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                       new Rectangle(x0, y0, width, height),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                                       new Point(sampleModelTranslateX+deltaX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                                 sampleModelTranslateY+deltaY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                                       this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Creates a Raster with the same layout but using a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * width and height, and with new zeroed data arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    public WritableRaster createCompatibleWritableRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (w <= 0 || h <=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            throw new RasterFormatException("negative "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                          ((w <= 0) ? "width" : "height"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return new ByteComponentRaster(sm , new Point(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * Creates a Raster with the same layout and the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * width and height, and with new zeroed data arrays.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * the Raster is a subRaster, this will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * createCompatibleRaster(width, height).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public WritableRaster createCompatibleWritableRaster() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        return createCompatibleWritableRaster(width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /**
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   860
     * Verify that the layout parameters are consistent with the data.
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   861
     *
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   862
     * The method verifies whether scanline stride and pixel stride do not
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   863
     * cause an integer overflow during calculation of a position of the pixel
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   864
     * in data buffer. It also verifies whether the data buffer has enough data
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   865
     *  to correspond the raster layout attributes.
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   866
     *
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   867
     * @throws RasterFormatException if an integer overflow is detected,
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   868
     * or if data buffer has not enough capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   870
    protected final void verify() {
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   871
        /* Need to re-verify the dimensions since a sample model may be
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   872
         * specified to the constructor
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   873
         */
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   874
        if (width <= 0 || height <= 0 ||
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   875
            height > (Integer.MAX_VALUE / width))
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   876
        {
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   877
            throw new RasterFormatException("Invalid raster dimension");
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   878
        }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   879
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   880
        for (int i = 0; i < dataOffsets.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            if (dataOffsets[i] < 0) {
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   882
                throw new RasterFormatException("Data offsets for band " + i
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   883
                            + "(" + dataOffsets[i]
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   884
                            + ") must be >= 0");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
25090
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   888
        if ((long)minX - sampleModelTranslateX < 0 ||
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   889
            (long)minY - sampleModelTranslateY < 0) {
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   890
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   891
            throw new RasterFormatException("Incorrect origin/translate: (" +
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   892
                    minX + ", " + minY + ") / (" +
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   893
                    sampleModelTranslateX + ", " + sampleModelTranslateY + ")");
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   894
        }
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   895
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   896
        // we can be sure that width and height are greater than 0
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   897
        if (scanlineStride < 0 ||
25090
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   898
            scanlineStride > (Integer.MAX_VALUE / height))
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   899
        {
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   900
            // integer overflow
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   901
            throw new RasterFormatException("Incorrect scanline stride: "
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   902
                    + scanlineStride);
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   903
        }
25090
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   904
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   905
        if (height > 1 || minY - sampleModelTranslateY > 0) {
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   906
            // buffer should contain at least one scanline
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   907
            if (scanlineStride > data.length) {
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   908
                throw new RasterFormatException("Incorrect scanline stride: "
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   909
                        + scanlineStride);
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   910
            }
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   911
        }
83a78ff886e6 8038000: java.awt.image.RasterFormatException: Incorrect scanline stride
anashaty
parents: 22584
diff changeset
   912
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   913
        int lastScanOffset = (height - 1) * scanlineStride;
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   914
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   915
        if (pixelStride < 0 ||
18246
5d1d50a81438 8012438: Better image validation
bae
parents: 18227
diff changeset
   916
            pixelStride > (Integer.MAX_VALUE / width) ||
5d1d50a81438 8012438: Better image validation
bae
parents: 18227
diff changeset
   917
            pixelStride > data.length)
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   918
        {
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   919
            // integer overflow
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   920
            throw new RasterFormatException("Incorrect pixel stride: "
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   921
                    + pixelStride);
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   922
        }
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   923
        int lastPixelOffset = (width - 1) * pixelStride;
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   924
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   925
        if (lastPixelOffset > (Integer.MAX_VALUE - lastScanOffset)) {
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   926
            // integer overflow
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   927
            throw new RasterFormatException("Incorrect raster attributes");
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   928
        }
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   929
        lastPixelOffset += lastScanOffset;
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   930
18227
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   931
        int index;
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   932
        int maxIndex = 0;
16093
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   933
        for (int i = 0; i < numDataElements; i++) {
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   934
            if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) {
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   935
                throw new RasterFormatException("Incorrect band offset: "
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   936
                            + dataOffsets[i]);
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   937
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   938
            }
b305a8dc867f 8001972: Improve image processing
bae
parents: 5506
diff changeset
   939
18227
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   940
            index = lastPixelOffset + dataOffsets[i];
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 16093
diff changeset
   941
18227
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   942
            if (index > maxIndex) {
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   943
                maxIndex = index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
18227
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   946
        if (data.length <= maxIndex) {
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   947
            throw new RasterFormatException("Data array too small (should be > "
7e588bf6aba5 8011257: Better Byte Component Rasters
prr
parents: 16868
diff changeset
   948
                    + maxIndex + " )");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        return new String ("ByteComponentRaster: width = "+width+" height = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                           + height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                           +" #numDataElements "+numDataElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                           //  +" xOff = "+xOffset+" yOff = "+yOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                           +" dataOff[0] = "+dataOffsets[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
//    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
//     * For debugging...  prints a region of a one-band ByteComponentRaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
//     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
//    public void print(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
//        // REMIND:  Only works for 1 band!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
//        System.out.println(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
//        int offset = dataOffsets[0] + y*scanlineStride + x*pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
//        int off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
//        for (int yoff=0; yoff < h; yoff++, offset += scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
//            off = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
//            System.out.print("Line "+(y+yoff)+": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
//            for (int xoff = 0; xoff < w; xoff++, off+= pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
//                String s = Integer.toHexString(data[off]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
//                if (s.length() == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
//                    s = s.substring(6,8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
//                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
//                System.out.print(s+" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
//            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
//            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
//        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
}