KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > PixelInterleavedSampleModel


1 /*
2  * @(#)PixelInterleavedSampleModel.java 1.20 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.image;
9
10 /**
11  * This class represents image data which is stored in a pixel interleaved
12  * fashion and for
13  * which each sample of a pixel occupies one data element of the DataBuffer.
14  * It subclasses ComponentSampleModel but provides a more efficent
15  * implementation for accessing pixel interleaved image data than is provided
16  * by ComponentSampleModel. This class
17  * stores sample data for all bands in a single bank of the
18  * DataBuffer. Accessor methods are provided so that image data can be
19  * manipulated directly. Pixel stride is the number of
20  * data array elements between two samples for the same band on the same
21  * scanline. Scanline stride is the number of data array elements between
22  * a given sample and the corresponding sample in the same column of the next
23  * scanline. Band offsets denote the number
24  * of data array elements from the first data array element of the bank
25  * of the DataBuffer holding each band to the first sample of the band.
26  * The bands are numbered from 0 to N-1.
27  * Bank indices denote the correspondence between a bank of the data buffer
28  * and a band of image data.
29  * This class supports
30  * {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
31  * {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
32  * {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
33  * {@link DataBuffer#TYPE_INT TYPE_INT},
34  * {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT} and
35  * {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE} datatypes.
36  */

37
38 public class PixelInterleavedSampleModel extends ComponentSampleModel JavaDoc
39 {
40     /**
41      * Constructs a PixelInterleavedSampleModel with the specified parameters.
42      * The number of bands will be given by the length of the bandOffsets
43      * array.
44      * @param dataType The data type for storing samples.
45      * @param w The width (in pixels) of the region of
46      * image data described.
47      * @param h The height (in pixels) of the region of
48      * image data described.
49      * @param pixelStride The pixel stride of the image data.
50      * @param scanlineStride The line stride of the image data.
51      * @param bandOffsets The offsets of all bands.
52      * @throws IllegalArgumentException if <code>w</code> or
53      * <code>h</code> is not greater than 0
54      * @throws IllegalArgumentException if any offset between bands is
55      * greater than the scanline stride
56      * @throws IllegalArgumentException if the product of
57      * <code>pixelStride</code> and <code>w</code> is greater
58      * than <code>scanlineStride</code>
59      * @throws IllegalArgumentException if <code>pixelStride</code> is
60      * less than any offset between bands
61      * @throws IllegalArgumentException if <code>dataType</code> is not
62      * one of the supported data types
63      */

64     public PixelInterleavedSampleModel(int dataType,
65                                        int w, int h,
66                                        int pixelStride,
67                                        int scanlineStride,
68                                        int bandOffsets[]) {
69         super(dataType, w, h, pixelStride, scanlineStride, bandOffsets);
70         int minBandOff=bandOffsets[0];
71         int maxBandOff=bandOffsets[0];
72         for (int i=1; i<bandOffsets.length; i++) {
73             minBandOff = Math.min(minBandOff,bandOffsets[i]);
74             maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
75         }
76         maxBandOff -= minBandOff;
77         if (maxBandOff > scanlineStride) {
78             throw new IllegalArgumentException JavaDoc("Offsets between bands must be"+
79                                                " less than the scanline "+
80                                                " stride");
81         }
82         if (pixelStride*w > scanlineStride) {
83             throw new IllegalArgumentException JavaDoc("Pixel stride times width "+
84                                                "must be less than or "+
85                                                "equal to the scanline "+
86                                                "stride");
87         }
88         if (pixelStride < maxBandOff) {
89             throw new IllegalArgumentException JavaDoc("Pixel stride must be greater"+
90                                                " than or equal to the offsets"+
91                                                " between bands");
92         }
93     }
94
95     /**
96      * Creates a new PixelInterleavedSampleModel with the specified
97      * width and height. The new PixelInterleavedSampleModel will have the
98      * same number of bands, storage data type, and pixel stride
99      * as this PixelInterleavedSampleModel. The band offsets may be
100      * compressed such that the minimum of all of the band offsets is zero.
101      * @param w the width of the resulting <code>SampleModel</code>
102      * @param h the height of the resulting <code>SampleModel</code>
103      * @return a new <code>SampleModel</code> with the specified width
104      * and height.
105      * @throws IllegalArgumentException if <code>w</code> or
106      * <code>h</code> is not greater than 0
107      */

108     public SampleModel JavaDoc createCompatibleSampleModel(int w, int h) {
109         int minBandoff=bandOffsets[0];
110         int numBands = bandOffsets.length;
111         for (int i=1; i < numBands; i++) {
112             if (bandOffsets[i] < minBandoff) {
113                 minBandoff = bandOffsets[i];
114             }
115         }
116         int[] bandOff;
117         if (minBandoff > 0) {
118             bandOff = new int[numBands];
119             for (int i=0; i < numBands; i++) {
120                 bandOff[i] = bandOffsets[i] - minBandoff;
121             }
122         }
123         else {
124             bandOff = bandOffsets;
125         }
126         return new PixelInterleavedSampleModel JavaDoc(dataType, w, h, pixelStride,
127                                                pixelStride*w, bandOff);
128     }
129
130     /**
131      * Creates a new PixelInterleavedSampleModel with a subset of the
132      * bands of this PixelInterleavedSampleModel. The new
133      * PixelInterleavedSampleModel can be used with any DataBuffer that the
134      * existing PixelInterleavedSampleModel can be used with. The new
135      * PixelInterleavedSampleModel/DataBuffer combination will represent
136      * an image with a subset of the bands of the original
137      * PixelInterleavedSampleModel/DataBuffer combination.
138      */

139     public SampleModel JavaDoc createSubsetSampleModel(int bands[]) {
140         int newBandOffsets[] = new int[bands.length];
141         for (int i=0; i<bands.length; i++) {
142             newBandOffsets[i] = bandOffsets[bands[i]];
143         }
144         return new PixelInterleavedSampleModel JavaDoc(this.dataType, width, height,
145                                                this.pixelStride,
146                                                scanlineStride, newBandOffsets);
147     }
148
149     // Differentiate hash code from other ComponentSampleModel subclasses
150
public int hashCode() {
151         return super.hashCode() ^ 0x1;
152     }
153 }
154
Popular Tags