jdk/src/share/classes/java/awt/image/PixelInterleavedSampleModel.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 18246 5d1d50a81438
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  This class represents image data which is stored in a pixel interleaved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  fashion and for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  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: 18246
diff changeset
    32
 *  It subclasses ComponentSampleModel but provides a more efficient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  implementation for accessing pixel interleaved image data than is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  by ComponentSampleModel.  This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  stores sample data for all bands in a single bank of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  DataBuffer. Accessor methods are provided so that image data can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  manipulated directly. Pixel stride is the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  data array elements between two samples for the same band on the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  scanline. Scanline stride is the number of data array elements between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  a given sample and the corresponding sample in the same column of the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  scanline.  Band offsets denote the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  of data array elements from the first data array element of the bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  of the DataBuffer holding each band to the first sample of the band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  The bands are numbered from 0 to N-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  Bank indices denote the correspondence between a bank of the data buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *  and a band of image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *  This class supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  {@link DataBuffer#TYPE_INT TYPE_INT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE} datatypes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class PixelInterleavedSampleModel extends ComponentSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Constructs a PixelInterleavedSampleModel with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The number of bands will be given by the length of the bandOffsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param dataType  The data type for storing samples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param w         The width (in pixels) of the region of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *                  image data described.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param h         The height (in pixels) of the region of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *                  image data described.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param pixelStride The pixel stride of the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param scanlineStride The line stride of the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param bandOffsets The offsets of all bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @throws IllegalArgumentException if <code>w</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *         <code>h</code> is not greater than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @throws IllegalArgumentException if any offset between bands is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *         greater than the scanline stride
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @throws IllegalArgumentException if the product of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *         <code>pixelStride</code> and <code>w</code> is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *         than <code>scanlineStride</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @throws IllegalArgumentException if <code>pixelStride</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *         less than any offset between bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @throws IllegalArgumentException if <code>dataType</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *         one of the supported data types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public PixelInterleavedSampleModel(int dataType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                       int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                       int pixelStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                       int scanlineStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                       int bandOffsets[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        super(dataType, w, h, pixelStride, scanlineStride, bandOffsets);
18246
5d1d50a81438 8012438: Better image validation
bae
parents: 5506
diff changeset
    88
        int minBandOff=this.bandOffsets[0];
5d1d50a81438 8012438: Better image validation
bae
parents: 5506
diff changeset
    89
        int maxBandOff=this.bandOffsets[0];
5d1d50a81438 8012438: Better image validation
bae
parents: 5506
diff changeset
    90
        for (int i=1; i<this.bandOffsets.length; i++) {
5d1d50a81438 8012438: Better image validation
bae
parents: 5506
diff changeset
    91
            minBandOff = Math.min(minBandOff,this.bandOffsets[i]);
5d1d50a81438 8012438: Better image validation
bae
parents: 5506
diff changeset
    92
            maxBandOff = Math.max(maxBandOff,this.bandOffsets[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        maxBandOff -= minBandOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (maxBandOff > scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IllegalArgumentException("Offsets between bands must be"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                               " less than the scanline "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                               " stride");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (pixelStride*w > scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalArgumentException("Pixel stride times width "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                               "must be less than or "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                               "equal to the scanline "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                               "stride");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (pixelStride < maxBandOff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new IllegalArgumentException("Pixel stride must be greater"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                               " than or equal to the offsets"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                               " between bands");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Creates a new PixelInterleavedSampleModel with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * width and height.  The new PixelInterleavedSampleModel will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * same number of bands, storage data type, and pixel stride
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * as this PixelInterleavedSampleModel.  The band offsets may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * compressed such that the minimum of all of the band offsets is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param w the width of the resulting <code>SampleModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param h the height of the resulting <code>SampleModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return a new <code>SampleModel</code> with the specified width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *         and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IllegalArgumentException if <code>w</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         <code>h</code> is not greater than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public SampleModel createCompatibleSampleModel(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int minBandoff=bandOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int numBands = bandOffsets.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i=1; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (bandOffsets[i] < minBandoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                minBandoff = bandOffsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int[] bandOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (minBandoff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            bandOff = new int[numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (int i=0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                bandOff[i] = bandOffsets[i] - minBandoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            bandOff = bandOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return new PixelInterleavedSampleModel(dataType, w, h, pixelStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                               pixelStride*w, bandOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Creates a new PixelInterleavedSampleModel with a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * bands of this PixelInterleavedSampleModel.  The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * PixelInterleavedSampleModel can be used with any DataBuffer that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * existing PixelInterleavedSampleModel can be used with.  The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * PixelInterleavedSampleModel/DataBuffer combination will represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * an image with a subset of the bands of the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * PixelInterleavedSampleModel/DataBuffer combination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public SampleModel createSubsetSampleModel(int bands[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int newBandOffsets[] = new int[bands.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (int i=0; i<bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            newBandOffsets[i] = bandOffsets[bands[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return new PixelInterleavedSampleModel(this.dataType, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                               this.pixelStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                               scanlineStride, newBandOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // Differentiate hash code from other ComponentSampleModel subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return super.hashCode() ^ 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}