jdk/src/share/classes/java/awt/image/BandedSampleModel.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 23010 6dadb192ad81
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
     4
 *
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
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: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    10
 *
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    15
 * accompanied this code).
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    16
 *
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    24
 */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    25
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    26
/* ****************************************************************
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    27
 ******************************************************************
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    28
 ******************************************************************
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    29
 *** COPYRIGHT (c) Eastman Kodak Company, 1997
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    30
 *** As  an unpublished  work pursuant to Title 17 of the United
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    31
 *** States Code.  All rights reserved.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    32
 ******************************************************************
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    33
 ******************************************************************
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    34
 ******************************************************************/
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    35
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    36
package java.awt.image;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    37
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    38
/**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    39
 *  This class represents image data which is stored in a band interleaved
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    40
 *  fashion and for
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    41
 *  which each sample of a pixel occupies one data element of the DataBuffer.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 8938
diff changeset
    42
 *  It subclasses ComponentSampleModel but provides a more efficient
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    43
 *  implementation for accessing band interleaved image data than is provided
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    44
 *  by ComponentSampleModel.  This class should typically be used when working
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    45
 *  with images which store sample data for each band in a different bank of the
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    46
 *  DataBuffer. Accessor methods are provided so that image data can be
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    47
 *  manipulated directly. Pixel stride is the number of
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    48
 *  data array elements between two samples for the same band on the same
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    49
 *  scanline. The pixel stride for a BandedSampleModel is one.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    50
 *  Scanline stride is the number of data array elements between
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    51
 *  a given sample and the corresponding sample in the same column of the next
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    52
 *  scanline.  Band offsets denote the number
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    53
 *  of data array elements from the first data array element of the bank
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    54
 *  of the DataBuffer holding each band to the first sample of the band.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    55
 *  The bands are numbered from 0 to N-1.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    56
 *  Bank indices denote the correspondence between a bank of the data buffer
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    57
 *  and a band of image data.  This class supports
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    58
 *  {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    59
 *  {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    60
 *  {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    61
 *  {@link DataBuffer#TYPE_INT TYPE_INT},
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    62
 *  {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT}, and
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    63
 *  {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE} datatypes
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    64
 */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    65
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    66
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    67
public final class BandedSampleModel extends ComponentSampleModel
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    68
{
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    69
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    70
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    71
     * Constructs a BandedSampleModel with the specified parameters.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    72
     * The pixel stride will be one data element.  The scanline stride
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    73
     * will be the same as the width.  Each band will be stored in
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    74
     * a separate bank and all band offsets will be zero.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    75
     * @param dataType  The data type for storing samples.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    76
     * @param w         The width (in pixels) of the region of
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    77
     *                  image data described.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    78
     * @param h         The height (in pixels) of the region of image
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    79
     *                  data described.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    80
     * @param numBands  The number of bands for the image data.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    81
     * @throws IllegalArgumentException if <code>dataType</code> is not
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    82
     *         one of the supported data types
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    83
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    84
    public BandedSampleModel(int dataType, int w, int h, int numBands) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    85
        super(dataType, w, h, 1, w,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    86
              BandedSampleModel.createIndicesArray(numBands),
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    87
              BandedSampleModel.createOffsetArray(numBands));
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    88
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    89
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    90
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    91
     * Constructs a BandedSampleModel with the specified parameters.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    92
     * The number of bands will be inferred from the lengths of the
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    93
     * bandOffsets bankIndices arrays, which must be equal.  The pixel
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    94
     * stride will be one data element.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    95
     * @param dataType  The data type for storing samples.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    96
     * @param w         The width (in pixels) of the region of
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    97
     *                  image data described.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    98
     * @param h         The height (in pixels) of the region of
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
    99
     *                  image data described.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   100
     * @param scanlineStride The line stride of the of the image data.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   101
     * @param bankIndices The bank index for each band.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   102
     * @param bandOffsets The band offset for each band.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   103
     * @throws IllegalArgumentException if <code>dataType</code> is not
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   104
     *         one of the supported data types
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   105
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   106
    public BandedSampleModel(int dataType,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   107
                             int w, int h,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   108
                             int scanlineStride,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   109
                             int bankIndices[],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   110
                             int bandOffsets[]) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   111
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   112
        super(dataType, w, h, 1,scanlineStride, bankIndices, bandOffsets);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   113
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   114
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   115
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   116
     * Creates a new BandedSampleModel with the specified
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   117
     * width and height.  The new BandedSampleModel will have the same
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   118
     * number of bands, storage data type, and bank indices
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   119
     * as this BandedSampleModel.  The band offsets will be compressed
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   120
     * such that the offset between bands will be w*pixelStride and
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   121
     * the minimum of all of the band offsets is zero.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   122
     * @param w the width of the resulting <code>BandedSampleModel</code>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   123
     * @param h the height of the resulting <code>BandedSampleModel</code>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   124
     * @return a new <code>BandedSampleModel</code> with the specified
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   125
     *         width and height.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   126
     * @throws IllegalArgumentException if <code>w</code> or
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   127
     *         <code>h</code> equals either
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   128
     *         <code>Integer.MAX_VALUE</code> or
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   129
     *         <code>Integer.MIN_VALUE</code>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   130
     * @throws IllegalArgumentException if <code>dataType</code> is not
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   131
     *         one of the supported data types
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   132
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   133
    public SampleModel createCompatibleSampleModel(int w, int h) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   134
        int[] bandOffs;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   135
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   136
        if (numBanks == 1) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   137
            bandOffs = orderBands(bandOffsets, w*h);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   138
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   139
        else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   140
            bandOffs = new int[bandOffsets.length];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   141
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   142
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   143
        SampleModel sampleModel =
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   144
            new BandedSampleModel(dataType, w, h, w, bankIndices, bandOffs);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   145
        return sampleModel;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   146
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   147
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   148
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   149
     * Creates a new BandedSampleModel with a subset of the bands of this
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   150
     * BandedSampleModel.  The new BandedSampleModel can be
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   151
     * used with any DataBuffer that the existing BandedSampleModel
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   152
     * can be used with.  The new BandedSampleModel/DataBuffer
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   153
     * combination will represent an image with a subset of the bands
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   154
     * of the original BandedSampleModel/DataBuffer combination.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   155
     * @throws RasterFormatException if the number of bands is greater than
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   156
     *                               the number of banks in this sample model.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   157
     * @throws IllegalArgumentException if <code>dataType</code> is not
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   158
     *         one of the supported data types
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   159
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   160
    public SampleModel createSubsetSampleModel(int bands[]) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   161
        if (bands.length > bankIndices.length)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   162
            throw new RasterFormatException("There are only " +
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   163
                                            bankIndices.length +
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   164
                                            " bands");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   165
        int newBankIndices[] = new int[bands.length];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   166
        int newBandOffsets[] = new int[bands.length];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   167
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   168
        for (int i=0; i<bands.length; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   169
            newBankIndices[i] = bankIndices[bands[i]];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   170
            newBandOffsets[i] = bandOffsets[bands[i]];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   171
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   172
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   173
        return new BandedSampleModel(this.dataType, width, height,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   174
                                     this.scanlineStride,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   175
                                     newBankIndices, newBandOffsets);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   176
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   177
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   178
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   179
     * Creates a DataBuffer that corresponds to this BandedSampleModel,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   180
     * The DataBuffer's data type, number of banks, and size
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   181
     * will be consistent with this BandedSampleModel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   182
     * @throws IllegalArgumentException if <code>dataType</code> is not
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   183
     *         one of the supported types.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   184
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   185
    public DataBuffer createDataBuffer() {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   186
        DataBuffer dataBuffer = null;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   187
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   188
        int size = scanlineStride * height;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   189
        switch (dataType) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   190
        case DataBuffer.TYPE_BYTE:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   191
            dataBuffer = new DataBufferByte(size, numBanks);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   192
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   193
        case DataBuffer.TYPE_USHORT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   194
            dataBuffer = new DataBufferUShort(size, numBanks);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   195
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   196
        case DataBuffer.TYPE_SHORT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   197
            dataBuffer = new DataBufferShort(size, numBanks);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   198
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   199
        case DataBuffer.TYPE_INT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   200
            dataBuffer = new DataBufferInt(size, numBanks);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   201
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   202
        case DataBuffer.TYPE_FLOAT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   203
            dataBuffer = new DataBufferFloat(size, numBanks);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   204
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   205
        case DataBuffer.TYPE_DOUBLE:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   206
            dataBuffer = new DataBufferDouble(size, numBanks);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   207
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   208
        default:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   209
            throw new IllegalArgumentException("dataType is not one " +
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   210
                "of the supported types.");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   211
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   212
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   213
        return dataBuffer;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   214
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   215
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   216
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   217
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   218
     * Returns data for a single pixel in a primitive array of type
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   219
     * TransferType.  For a BandedSampleModel, this will be the same
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   220
     * as the data type, and samples will be returned one per array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   221
     * element.  Generally, obj
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   222
     * should be passed in as null, so that the Object will be created
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   223
     * automatically and will be of the right primitive data type.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   224
     * <p>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   225
     * The following code illustrates transferring data for one pixel from
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   226
     * DataBuffer <code>db1</code>, whose storage layout is described by
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   227
     * BandedSampleModel <code>bsm1</code>, to DataBuffer <code>db2</code>,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   228
     * whose storage layout is described by
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   229
     * BandedSampleModel <code>bsm2</code>.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   230
     * The transfer will generally be more efficient than using
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   231
     * getPixel/setPixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   232
     * <pre>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   233
     *       BandedSampleModel bsm1, bsm2;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   234
     *       DataBufferInt db1, db2;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   235
     *       bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1),
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   236
     *                            db2);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   237
     * </pre>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   238
     * Using getDataElements/setDataElements to transfer between two
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   239
     * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   240
     * the same number of bands, corresponding bands have the same number of
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   241
     * bits per sample, and the TransferTypes are the same.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   242
     * <p>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   243
     * If obj is non-null, it should be a primitive array of type TransferType.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   244
     * Otherwise, a ClassCastException is thrown.  An
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   245
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   246
     * not in bounds, or if obj is non-null and is not large enough to hold
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   247
     * the pixel data.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   248
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   249
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   250
     * @param obj       If non-null, a primitive array in which to return
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   251
     *                  the pixel data.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   252
     * @param data      The DataBuffer containing the image data.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   253
     * @return the data for the specified pixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   254
     * @see #setDataElements(int, int, Object, DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   255
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   256
    public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   257
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   258
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   259
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   260
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   261
        int type = getTransferType();
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   262
        int numDataElems = getNumDataElements();
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   263
        int pixelOffset = y*scanlineStride + x;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   264
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   265
        switch(type) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   266
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   267
        case DataBuffer.TYPE_BYTE:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   268
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   269
            byte[] bdata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   270
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   271
            if (obj == null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   272
                bdata = new byte[numDataElems];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   273
            } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   274
                bdata = (byte[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   275
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   276
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   277
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   278
                bdata[i] = (byte)data.getElem(bankIndices[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   279
                                              pixelOffset + bandOffsets[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   280
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   281
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   282
            obj = (Object)bdata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   283
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   284
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   285
        case DataBuffer.TYPE_USHORT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   286
        case DataBuffer.TYPE_SHORT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   287
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   288
            short[] sdata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   289
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   290
            if (obj == null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   291
                sdata = new short[numDataElems];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   292
            } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   293
                sdata = (short[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   294
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   295
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   296
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   297
                sdata[i] = (short)data.getElem(bankIndices[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   298
                                               pixelOffset + bandOffsets[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   299
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   300
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   301
            obj = (Object)sdata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   302
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   303
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   304
        case DataBuffer.TYPE_INT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   305
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   306
            int[] idata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   307
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   308
            if (obj == null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   309
                idata = new int[numDataElems];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   310
            } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   311
                idata = (int[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   312
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   313
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   314
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   315
                idata[i] = data.getElem(bankIndices[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   316
                                        pixelOffset + bandOffsets[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   317
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   318
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   319
            obj = (Object)idata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   320
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   321
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   322
        case DataBuffer.TYPE_FLOAT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   323
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   324
            float[] fdata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   325
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   326
            if (obj == null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   327
                fdata = new float[numDataElems];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   328
            } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   329
                fdata = (float[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   330
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   331
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   332
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   333
                fdata[i] = data.getElemFloat(bankIndices[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   334
                                             pixelOffset + bandOffsets[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   335
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   336
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   337
            obj = (Object)fdata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   338
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   339
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   340
        case DataBuffer.TYPE_DOUBLE:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   341
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   342
            double[] ddata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   343
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   344
            if (obj == null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   345
                ddata = new double[numDataElems];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   346
            } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   347
                ddata = (double[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   348
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   349
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   350
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   351
                ddata[i] = data.getElemDouble(bankIndices[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   352
                                              pixelOffset + bandOffsets[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   353
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   354
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   355
            obj = (Object)ddata;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   356
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   357
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   358
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   359
        return obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   360
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   361
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   362
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   363
     * Returns all samples for the specified pixel in an int array.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   364
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   365
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   366
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   367
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   368
     * @param iArray    If non-null, returns the samples in this array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   369
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   370
     * @return the samples for the specified pixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   371
     * @see #setPixel(int, int, int[], DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   372
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   373
    public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   374
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   375
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   376
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   377
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   378
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   379
        int[] pixels;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   380
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   381
        if (iArray != null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   382
           pixels = iArray;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   383
        } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   384
           pixels = new int [numBands];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   385
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   386
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   387
        int pixelOffset = y*scanlineStride + x;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   388
        for (int i=0; i<numBands; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   389
            pixels[i] = data.getElem(bankIndices[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   390
                                     pixelOffset + bandOffsets[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   391
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   392
        return pixels;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   393
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   394
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   395
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   396
     * Returns all samples for the specified rectangle of pixels in
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   397
     * an int array, one sample per data array element.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   398
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   399
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   400
     * @param x         The X coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   401
     * @param y         The Y coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   402
     * @param w         The width of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   403
     * @param h         The height of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   404
     * @param iArray    If non-null, returns the samples in this array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   405
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   406
     * @return the samples for the pixels within the specified region.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   407
     * @see #setPixels(int, int, int, int, int[], DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   408
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   409
    public int[] getPixels(int x, int y, int w, int h,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   410
                           int iArray[], DataBuffer data) {
8938
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   411
        int x1 = x + w;
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   412
        int y1 = y + h;
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   413
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   414
        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   415
            y < 0 || y >= height || h > height || y1 < 0 || y1 >  height)
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   416
        {
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   417
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   418
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   419
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   420
        int[] pixels;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   421
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   422
        if (iArray != null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   423
           pixels = iArray;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   424
        } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   425
           pixels = new int[w*h*numBands];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   426
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   427
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   428
        for (int k = 0; k < numBands; k++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   429
            int lineOffset = y*scanlineStride + x + bandOffsets[k];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   430
            int srcOffset = k;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   431
            int bank = bankIndices[k];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   432
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   433
            for (int i = 0; i < h; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   434
                int pixelOffset = lineOffset;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   435
                for (int j = 0; j < w; j++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   436
                    pixels[srcOffset] = data.getElem(bank, pixelOffset++);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   437
                    srcOffset += numBands;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   438
                }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   439
                lineOffset += scanlineStride;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   440
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   441
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   442
        return pixels;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   443
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   444
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   445
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   446
     * Returns as int the sample in a specified band for the pixel
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   447
     * located at (x,y).
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   448
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   449
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   450
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   451
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   452
     * @param b         The band to return
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   453
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   454
     * @return the sample in the specified band for the specified pixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   455
     * @see #setSample(int, int, int, int, DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   456
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   457
    public int getSample(int x, int y, int b, DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   458
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   459
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   460
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   461
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   462
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   463
        int sample =
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   464
            data.getElem(bankIndices[b],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   465
                         y*scanlineStride + x + bandOffsets[b]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   466
        return sample;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   467
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   468
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   469
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   470
     * Returns the sample in a specified band
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   471
     * for the pixel located at (x,y) as a float.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   472
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   473
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   474
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   475
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   476
     * @param b         The band to return
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   477
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   478
     * @return a float value that represents the sample in the specified
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   479
     * band for the specified pixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   480
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   481
    public float getSampleFloat(int x, int y, int b, DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   482
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   483
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   484
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   485
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   486
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   487
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   488
        float sample = data.getElemFloat(bankIndices[b],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   489
                                    y*scanlineStride + x + bandOffsets[b]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   490
        return sample;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   491
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   492
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   493
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   494
     * Returns the sample in a specified band
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   495
     * for a pixel located at (x,y) as a double.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   496
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   497
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   498
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   499
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   500
     * @param b         The band to return
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   501
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   502
     * @return a double value that represents the sample in the specified
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   503
     * band for the specified pixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   504
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   505
    public double getSampleDouble(int x, int y, int b, DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   506
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   507
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   508
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   509
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   510
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   511
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   512
        double sample = data.getElemDouble(bankIndices[b],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   513
                                       y*scanlineStride + x + bandOffsets[b]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   514
        return sample;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   515
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   516
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   517
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   518
     * Returns the samples in a specified band for the specified rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   519
     * of pixels in an int array, one sample per data array element.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   520
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   521
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   522
     * @param x         The X coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   523
     * @param y         The Y coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   524
     * @param w         The width of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   525
     * @param h         The height of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   526
     * @param b         The band to return
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   527
     * @param iArray    If non-null, returns the samples in this array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   528
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   529
     * @return the samples in the specified band for the pixels within
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   530
     * the specified region.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   531
     * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   532
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   533
    public int[] getSamples(int x, int y, int w, int h, int b,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   534
                            int iArray[], DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   535
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   536
        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   537
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   538
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   539
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   540
        int samples[];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   541
        if (iArray != null) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   542
           samples = iArray;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   543
        } else {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   544
           samples = new int [w*h];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   545
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   546
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   547
        int lineOffset = y*scanlineStride + x + bandOffsets[b];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   548
        int srcOffset = 0;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   549
        int bank = bankIndices[b];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   550
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   551
        for (int i = 0; i < h; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   552
           int sampleOffset = lineOffset;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   553
           for (int j = 0; j < w; j++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   554
               samples[srcOffset++] = data.getElem(bank, sampleOffset++);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   555
           }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   556
           lineOffset += scanlineStride;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   557
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   558
        return samples;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   559
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   560
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   561
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   562
     * Sets the data for a single pixel in the specified DataBuffer from a
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   563
     * primitive array of type TransferType.  For a BandedSampleModel,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   564
     * this will be the same as the data type, and samples are transferred
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   565
     * one per array element.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   566
     * <p>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   567
     * The following code illustrates transferring data for one pixel from
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   568
     * DataBuffer <code>db1</code>, whose storage layout is described by
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   569
     * BandedSampleModel <code>bsm1</code>, to DataBuffer <code>db2</code>,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   570
     * whose storage layout is described by
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   571
     * BandedSampleModel <code>bsm2</code>.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   572
     * The transfer will generally be more efficient than using
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   573
     * getPixel/setPixel.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   574
     * <pre>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   575
     *       BandedSampleModel bsm1, bsm2;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   576
     *       DataBufferInt db1, db2;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   577
     *       bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1),
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   578
     *                            db2);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   579
     * </pre>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   580
     * Using getDataElements/setDataElements to transfer between two
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   581
     * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   582
     * the same number of bands, corresponding bands have the same number of
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   583
     * bits per sample, and the TransferTypes are the same.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   584
     * <p>
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   585
     * obj must be a primitive array of type TransferType.  Otherwise,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   586
     * a ClassCastException is thrown.  An
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   587
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   588
     * not in bounds, or if obj is not large enough to hold the pixel data.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   589
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   590
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   591
     * @param obj       If non-null, returns the primitive array in this
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   592
     *                  object
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   593
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   594
     * @see #getDataElements(int, int, Object, DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   595
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   596
    public void setDataElements(int x, int y, Object obj, DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   597
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   598
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   599
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   600
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   601
        int type = getTransferType();
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   602
        int numDataElems = getNumDataElements();
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   603
        int pixelOffset = y*scanlineStride + x;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   604
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   605
        switch(type) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   606
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   607
        case DataBuffer.TYPE_BYTE:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   608
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   609
            byte[] barray = (byte[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   610
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   611
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   612
                data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   613
                             barray[i] & 0xff);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   614
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   615
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   616
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   617
        case DataBuffer.TYPE_USHORT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   618
        case DataBuffer.TYPE_SHORT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   619
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   620
            short[] sarray = (short[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   621
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   622
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   623
                data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   624
                             sarray[i] & 0xffff);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   625
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   626
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   627
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   628
        case DataBuffer.TYPE_INT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   629
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   630
            int[] iarray = (int[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   631
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   632
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   633
                data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   634
                             iarray[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   635
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   636
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   637
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   638
        case DataBuffer.TYPE_FLOAT:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   639
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   640
            float[] farray = (float[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   641
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   642
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   643
                data.setElemFloat(bankIndices[i], pixelOffset + bandOffsets[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   644
                                  farray[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   645
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   646
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   647
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   648
        case DataBuffer.TYPE_DOUBLE:
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   649
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   650
            double[] darray = (double[])obj;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   651
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   652
            for (int i=0; i<numDataElems; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   653
                data.setElemDouble(bankIndices[i], pixelOffset + bandOffsets[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   654
                                   darray[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   655
            }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   656
            break;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   657
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   658
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   659
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   660
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   661
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   662
     * Sets a pixel in the DataBuffer using an int array of samples for input.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   663
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   664
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   665
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   666
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   667
     * @param iArray    The input samples in an int array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   668
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   669
     * @see #getPixel(int, int, int[], DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   670
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   671
    public void setPixel(int x, int y, int iArray[], DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   672
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   673
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   674
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   675
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   676
       int pixelOffset = y*scanlineStride + x;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   677
       for (int i=0; i<numBands; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   678
           data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   679
                        iArray[i]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   680
       }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   681
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   682
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   683
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   684
     * Sets all samples for a rectangle of pixels from an int array containing
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   685
     * one sample per array element.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   686
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   687
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   688
     * @param x         The X coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   689
     * @param y         The Y coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   690
     * @param w         The width of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   691
     * @param h         The height of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   692
     * @param iArray    The input samples in an int array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   693
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   694
     * @see #getPixels(int, int, int, int, int[], DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   695
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   696
    public void setPixels(int x, int y, int w, int h,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   697
                          int iArray[], DataBuffer data) {
8938
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   698
        int x1 = x + w;
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   699
        int y1 = y + h;
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   700
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   701
        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   702
            y < 0 || y >= height || h > height || y1 < 0 || y1 >  height)
8e3cd84800e3 6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
bae
parents: 5506
diff changeset
   703
        {
539
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   704
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   705
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   706
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   707
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   708
        for (int k = 0; k < numBands; k++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   709
            int lineOffset = y*scanlineStride + x + bandOffsets[k];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   710
            int srcOffset = k;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   711
            int bank = bankIndices[k];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   712
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   713
            for (int i = 0; i < h; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   714
                int pixelOffset = lineOffset;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   715
                for (int j = 0; j < w; j++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   716
                    data.setElem(bank, pixelOffset++, iArray[srcOffset]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   717
                    srcOffset += numBands;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   718
                }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   719
                lineOffset += scanlineStride;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   720
           }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   721
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   722
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   723
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   724
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   725
     * Sets a sample in the specified band for the pixel located at (x,y)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   726
     * in the DataBuffer using an int for input.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   727
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   728
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   729
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   730
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   731
     * @param b         The band to set
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   732
     * @param s         The input sample as an int
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   733
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   734
     * @see #getSample(int, int, int, DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   735
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   736
    public void setSample(int x, int y, int b, int s,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   737
                          DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   738
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   739
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   740
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   741
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   742
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   743
        data.setElem(bankIndices[b],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   744
                     y*scanlineStride + x + bandOffsets[b], s);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   745
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   746
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   747
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   748
     * Sets a sample in the specified band for the pixel located at (x,y)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   749
     * in the DataBuffer using a float for input.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   750
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   751
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   752
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   753
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   754
     * @param b         The band to set
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   755
     * @param s         The input sample as a float
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   756
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   757
     * @see #getSample(int, int, int, DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   758
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   759
    public void setSample(int x, int y, int b,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   760
                          float s ,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   761
                          DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   762
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   763
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   764
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   765
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   766
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   767
        data.setElemFloat(bankIndices[b],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   768
                          y*scanlineStride + x + bandOffsets[b], s);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   769
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   770
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   771
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   772
     * Sets a sample in the specified band for the pixel located at (x,y)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   773
     * in the DataBuffer using a double for input.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   774
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   775
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   776
     * @param x         The X coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   777
     * @param y         The Y coordinate of the pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   778
     * @param b         The band to set
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   779
     * @param s         The input sample as a double
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   780
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   781
     * @see #getSample(int, int, int, DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   782
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   783
    public void setSample(int x, int y, int b,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   784
                          double s,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   785
                          DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   786
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   787
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   788
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   789
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   790
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   791
        data.setElemDouble(bankIndices[b],
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   792
                          y*scanlineStride + x + bandOffsets[b], s);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   793
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   794
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   795
    /**
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   796
     * Sets the samples in the specified band for the specified rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   797
     * of pixels from an int array containing one sample per data array element.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   798
     * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   799
     * not in bounds.
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   800
     * @param x         The X coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   801
     * @param y         The Y coordinate of the upper left pixel location
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   802
     * @param w         The width of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   803
     * @param h         The height of the pixel rectangle
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   804
     * @param b         The band to set
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   805
     * @param iArray    The input sample array
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   806
     * @param data      The DataBuffer containing the image data
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   807
     * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   808
     */
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   809
    public void setSamples(int x, int y, int w, int h, int b,
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   810
                           int iArray[], DataBuffer data) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   811
        // Bounds check for 'b' will be performed automatically
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   812
        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   813
            throw new ArrayIndexOutOfBoundsException
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   814
                ("Coordinate out of bounds!");
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   815
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   816
        int lineOffset = y*scanlineStride + x + bandOffsets[b];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   817
        int srcOffset = 0;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   818
        int bank = bankIndices[b];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   819
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   820
        for (int i = 0; i < h; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   821
           int sampleOffset = lineOffset;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   822
           for (int j = 0; j < w; j++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   823
              data.setElem(bank, sampleOffset++, iArray[srcOffset++]);
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   824
           }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   825
           lineOffset += scanlineStride;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   826
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   827
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   828
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   829
    private static int[] createOffsetArray(int numBands) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   830
        int[] bandOffsets = new int[numBands];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   831
        for (int i=0; i < numBands; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   832
            bandOffsets[i] = 0;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   833
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   834
        return bandOffsets;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   835
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   836
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   837
    private static int[] createIndicesArray(int numBands) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   838
        int[] bankIndices = new int[numBands];
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   839
        for (int i=0; i < numBands; i++) {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   840
            bankIndices[i] = i;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   841
        }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   842
        return bankIndices;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   843
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   844
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   845
    // Differentiate hash code from other ComponentSampleModel subclasses
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   846
    public int hashCode() {
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   847
        return super.hashCode() ^ 0x2;
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   848
    }
7952521a4ad3 6662775: Move imaging and color classes from closed to open
prr
parents:
diff changeset
   849
}