001: /*
002: * $RCSfile: UnpackedImageData.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:23 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.Raster;
017:
018: /**
019: * This class is used by <code>PixelAccessor</code> to store unpacked
020: * image data and the information needed to access it. The data are stored in
021: * a two-dimensional primitive array.
022: *
023: * @since JAI 1.1
024: *
025: */
026: public final class UnpackedImageData {
027:
028: /** The <code>Raster</code> containing the pixel data. */
029: public final Raster raster;
030:
031: /**
032: * The rectangular region within the <code>Raster</code> where the
033: * data are to be accessed.
034: */
035: public final Rectangle rect;
036:
037: /** The type of the primitive array used to store the data. */
038: public final int type;
039:
040: /** The data array supplied to store the unpacked data. */
041: public final Object data;
042:
043: /**
044: * The number of array elements to skip to get to the next
045: * pixel on the same scanline.
046: */
047: public final int pixelStride;
048:
049: /** The number of array elements per scanline. */
050: public final int lineStride;
051:
052: /**
053: * The number of array elements from the beginning of the data array
054: * to the first pixel of the <code>Rectangle</code> for all bands.
055: */
056: public final int[] bandOffsets;
057:
058: /**
059: * Indicates whether the <code>PixelAccessor</code> can and must set the
060: * data back into the <code>Raster</code>. If the data does not need
061: * to be copied back to the <code>Raster</code>, this variable should be
062: * set to <code>false</code>. Only destinations can be set.
063: */
064: public final boolean convertToDest;
065:
066: /** Constructs a PackedImageRaster.
067: * @param raster The <code>Raster</code> containing the pixel data.
068: * @param rect The rectangular region containing the data.
069: * @param type The type of the primitive array supplied to store the data.
070: * @param data The data array supplied to store the data.
071: * @param pixelStride The data array increment needed to move from band x
072: * of pixel i to band x of pixel i+1 on the same
073: scanline.
074: * @param lineStride The data array increment to move from the pixel x
075: * of line i to pixel x of line i+1.
076: * @param bandOffsets The number of bytes from the start of the data array
077: * to the location of the first pixel of the rectangle
078: * for all bands.
079: * @param convertToDest A <code>boolean</code> indicating whether the data can and
080: * must be set back into the <code>Raster</code>. This applies
081: * only to destinations.
082: */
083: public UnpackedImageData(Raster raster, Rectangle rect, int type,
084: Object data, int pixelStride, int lineStride,
085: int[] bandOffsets, boolean convertToDest) {
086: this .raster = raster;
087: this .rect = rect;
088: this .type = type;
089: this .data = data;
090: this .pixelStride = pixelStride;
091: this .lineStride = lineStride;
092: this .bandOffsets = bandOffsets;
093: this .convertToDest = convertToDest;
094: }
095:
096: /**
097: * Returns the two-dimensional byte data array, or <code>null</code>
098: * if the data are not stored in a byte array.
099: * The array format is <code>data[band][]</code> where the second
100: * index is navigated using the pixel and line strides.
101: * @return The two-dimensional byte data array.
102: */
103: public byte[][] getByteData() {
104: return type == DataBuffer.TYPE_BYTE ? (byte[][]) data : null;
105: }
106:
107: /**
108: * Returns byte data array for a specific band, or <code>null</code>
109: * if the data are not stored in a byte array.
110: * @param b The band whose data array is to be retrieved.
111: * @return The one-dimensional byte data array for the requested band.
112: */
113: public byte[] getByteData(int b) {
114: byte[][] d = getByteData();
115: return d == null ? null : d[b];
116: }
117:
118: /**
119: * Returns the two-dimensional short data array, or <code>null</code>
120: * if the data are not stored in a short array.
121: * The array format is <code>data[band][]</code> where the second
122: * index is navigated using the pixel and line strides.
123: * @return The two-dimensional short data array.
124: */
125: public short[][] getShortData() {
126: return (type == DataBuffer.TYPE_USHORT || type == DataBuffer.TYPE_SHORT) ? (short[][]) data
127: : null;
128: }
129:
130: /**
131: * Returns short data array for a specific band, or <code>null</code>
132: * if the data are not stored in a short array.
133: * @param b The band whose data array is to be retrieved.
134: * @return The one-dimensional short data array for the requested band.
135: */
136: public short[] getShortData(int b) {
137: short[][] d = getShortData();
138: return d == null ? null : d[b];
139: }
140:
141: /**
142: * Returns the two-dimensional integer data array, or <code>null</code>
143: * if the data are not stored in an integer array.
144: * The array format is <code>data[band][]</code> where the second
145: * index is navigated using the pixel and line strides.
146: * @return The two-dimensional int data array.
147: */
148: public int[][] getIntData() {
149: return type == DataBuffer.TYPE_INT ? (int[][]) data : null;
150: }
151:
152: /**
153: * Returns integer data array for a specific band, or <code>null</code>
154: * if the data are not stored in an integer array.
155: * @param b The band whose data array is to be retrieved.
156: * @return The one-dimensional int data array for the requested band.
157: */
158: public int[] getIntData(int b) {
159: int[][] d = getIntData();
160: return d == null ? null : d[b];
161: }
162:
163: /**
164: * Returns the two-dimensional float data array, or <code>null</code>
165: * if the data are not stored in a float array.
166: * The array format is <code>data[band][]</code> where the second
167: * index is navigated using the pixel and line strides.
168: * @return The two-dimensional float data array.
169: */
170: public float[][] getFloatData() {
171: return type == DataBuffer.TYPE_FLOAT ? (float[][]) data : null;
172: }
173:
174: /**
175: * Returns float data array for a specific band, or <code>null</code>
176: * if the data are not stored in a float array.
177: * @param b The band whose data array is to be retrieved.
178: * @return The one-dimensional float data array for the requested band.
179: */
180: public float[] getFloatData(int b) {
181: float[][] d = getFloatData();
182: return d == null ? null : d[b];
183: }
184:
185: /**
186: * Returns the two-dimensional double data array, or <code>null</code>
187: * if the data are not stored in a double array.
188: * The array format is <code>data[band][]</code> where the second
189: * index is navigated using the pixel and line strides.
190: * @return The two-dimensional double data array.
191: */
192: public double[][] getDoubleData() {
193: return type == DataBuffer.TYPE_DOUBLE ? (double[][]) data
194: : null;
195: }
196:
197: /**
198: * Returns double data array for a specific band, or <code>null</code>
199: * if the data are not stored in a double array.
200: * @param b The band whose data array is to be retrieved.
201: * @return The one-dimensional double data array for the requested band.
202: */
203: public double[] getDoubleData(int b) {
204: double[][] d = getDoubleData();
205: return d == null ? null : d[b];
206: }
207:
208: /** Returns the offset for a band.
209: * @param b The band whose offset is to be returned.
210: * @return The offset of the requested band.
211: */
212: public int getOffset(int b) {
213: return bandOffsets[b];
214: }
215:
216: /** Returns the minimum offset of all bands.
217: * @return The minimum offset of all bands.
218: */
219: public int getMinOffset() {
220: int min = bandOffsets[0];
221: for (int i = 1; i < bandOffsets.length; i++) {
222: min = Math.min(min, bandOffsets[i]);
223: }
224: return min;
225: }
226:
227: /** Returns the maximum offset of all bands.
228: * @return The maximum offset of all the bands.
229: */
230: public int getMaxOffset() {
231: int max = bandOffsets[0];
232: for (int i = 1; i < bandOffsets.length; i++) {
233: max = Math.max(max, bandOffsets[i]);
234: }
235: return max;
236: }
237: }
|