39
40 /**
41 * This class extends Raster to provide pixel writing capabilities.
42 * Refer to the class comment for Raster for descriptions of how
43 * a Raster stores pixels.
44 *
45 * <p> The constructors of this class are protected. To instantiate
46 * a WritableRaster, use one of the createWritableRaster factory methods
47 * in the Raster class.
48 */
49 public class WritableRaster extends Raster {
50
51 /**
52 * Constructs a WritableRaster with the given SampleModel. The
53 * WritableRaster's upper left corner is origin and it is the
54 * same size as the SampleModel. A DataBuffer large enough to
55 * describe the WritableRaster is automatically created.
56 * @param sampleModel The SampleModel that specifies the layout.
57 * @param origin The Point that specifies the origin.
58 * @throws RasterFormatException if computing either
59 * <code>origin.x + sampleModel.getWidth()</code> or
60 * <code>origin.y + sampleModel.getHeight()</code> results
61 * in integer overflow
62 */
63 protected WritableRaster(SampleModel sampleModel,
64 Point origin) {
65 this(sampleModel,
66 sampleModel.createDataBuffer(),
67 new Rectangle(origin.x,
68 origin.y,
69 sampleModel.getWidth(),
70 sampleModel.getHeight()),
71 origin,
72 null);
73 }
74
75 /**
76 * Constructs a WritableRaster with the given SampleModel and DataBuffer.
77 * The WritableRaster's upper left corner is origin and it is the same
78 * size as the SampleModel. The DataBuffer is not initialized and must
79 * be compatible with SampleModel.
80 * @param sampleModel The SampleModel that specifies the layout.
81 * @param dataBuffer The DataBuffer that contains the image data.
82 * @param origin The Point that specifies the origin.
83 * @throws RasterFormatException if computing either
84 * <code>origin.x + sampleModel.getWidth()</code> or
85 * <code>origin.y + sampleModel.getHeight()</code> results
86 * in integer overflow
87 */
88 protected WritableRaster(SampleModel sampleModel,
89 DataBuffer dataBuffer,
90 Point origin) {
91 this(sampleModel,
92 dataBuffer,
93 new Rectangle(origin.x,
94 origin.y,
95 sampleModel.getWidth(),
96 sampleModel.getHeight()),
97 origin,
98 null);
99 }
100
101 /**
102 * Constructs a WritableRaster with the given SampleModel, DataBuffer,
103 * and parent. aRegion specifies the bounding rectangle of the new
104 * Raster. When translated into the base Raster's coordinate
105 * system, aRegion must be contained by the base Raster.
106 * (The base Raster is the Raster's ancestor which has no parent.)
107 * sampleModelTranslate specifies the sampleModelTranslateX and
108 * sampleModelTranslateY values of the new Raster.
109 *
110 * Note that this constructor should generally be called by other
111 * constructors or create methods, it should not be used directly.
112 * @param sampleModel The SampleModel that specifies the layout.
113 * @param dataBuffer The DataBuffer that contains the image data.
114 * @param aRegion The Rectangle that specifies the image area.
115 * @param sampleModelTranslate The Point that specifies the translation
116 * from SampleModel to Raster coordinates.
117 * @param parent The parent (if any) of this raster.
118 * @throws RasterFormatException if <code>aRegion</code> has width
119 * or height less than or equal to zero, or computing either
120 * <code>aRegion.x + aRegion.width</code> or
121 * <code>aRegion.y + aRegion.height</code> results in integer
122 * overflow
123 */
124 protected WritableRaster(SampleModel sampleModel,
125 DataBuffer dataBuffer,
126 Rectangle aRegion,
127 Point sampleModelTranslate,
128 WritableRaster parent){
129 super(sampleModel,dataBuffer,aRegion,sampleModelTranslate,parent);
130 }
131
132 /** Returns the parent WritableRaster (if any) of this WritableRaster,
133 * or else null.
134 * @return the parent of this <code>WritableRaster</code>, or
135 * <code>null</code>.
136 */
137 public WritableRaster getWritableParent() {
138 return (WritableRaster)parent;
139 }
140
141 /**
142 * Create a WritableRaster with the same size, SampleModel and DataBuffer
143 * as this one, but with a different location. The new WritableRaster
144 * will possess a reference to the current WritableRaster, accessible
145 * through its getParent() and getWritableParent() methods.
146 *
147 * @param childMinX X coord of the upper left corner of the new Raster.
148 * @param childMinY Y coord of the upper left corner of the new Raster.
149 * @return a <code>WritableRaster</code> the same as this one except
150 * for the specified location.
151 * @throws RasterFormatException if computing either
152 * <code>childMinX + this.getWidth()</code> or
153 * <code>childMinY + this.getHeight()</code> results in integer
154 * overflow
155 */
156 public WritableRaster createWritableTranslatedChild(int childMinX,
157 int childMinY) {
158 return createWritableChild(minX,minY,width,height,
159 childMinX,childMinY,null);
160 }
161
162 /**
163 * Returns a new WritableRaster which shares all or part of this
164 * WritableRaster's DataBuffer. The new WritableRaster will
165 * possess a reference to the current WritableRaster, accessible
166 * through its getParent() and getWritableParent() methods.
167 *
168 * <p> The parentX, parentY, width and height parameters form a
169 * Rectangle in this WritableRaster's coordinate space, indicating
170 * the area of pixels to be shared. An error will be thrown if
171 * this Rectangle is not contained with the bounds of the current
172 * WritableRaster.
173 *
185 * null, it is taken to include all of the bands of the current
186 * WritableRaster in their current order.
187 *
188 * <p> To create a new WritableRaster that contains a subregion of
189 * the current WritableRaster, but shares its coordinate system
190 * and bands, this method should be called with childMinX equal to
191 * parentX, childMinY equal to parentY, and bandList equal to
192 * null.
193 *
194 * @param parentX X coordinate of the upper left corner in this
195 * WritableRaster's coordinates.
196 * @param parentY Y coordinate of the upper left corner in this
197 * WritableRaster's coordinates.
198 * @param w Width of the region starting at (parentX, parentY).
199 * @param h Height of the region starting at (parentX, parentY).
200 * @param childMinX X coordinate of the upper left corner of
201 * the returned WritableRaster.
202 * @param childMinY Y coordinate of the upper left corner of
203 * the returned WritableRaster.
204 * @param bandList Array of band indices, or null to use all bands.
205 * @return a <code>WritableRaster</code> sharing all or part of the
206 * <code>DataBuffer</code> of this <code>WritableRaster</code>.
207 * @exception RasterFormatException if the subregion is outside of the
208 * raster bounds.
209 * @throws RasterFormatException if <code>w</code> or
210 * <code>h</code>
211 * is less than or equal to zero, or computing any of
212 * <code>parentX + w</code>, <code>parentY + h</code>,
213 * <code>childMinX + w</code>, or
214 * <code>childMinY + h</code> results in integer
215 * overflow
216 */
217 public WritableRaster createWritableChild(int parentX, int parentY,
218 int w, int h,
219 int childMinX, int childMinY,
220 int bandList[]) {
221 if (parentX < this.minX) {
222 throw new RasterFormatException("parentX lies outside raster");
223 }
224 if (parentY < this.minY) {
225 throw new RasterFormatException("parentY lies outside raster");
226 }
227 if ((parentX+w < parentX) || (parentX+w > this.width + this.minX)) {
228 throw new RasterFormatException("(parentX + width) is outside raster");
229 }
230 if ((parentY+h < parentY) || (parentY+h > this.height + this.minY)) {
231 throw new RasterFormatException("(parentY + height) is outside raster");
232 }
233
234 SampleModel sm;
354 * @throws ArrayIndexOutOfBoundsException if the coordinates are not
355 * in bounds, or if inData is too small to hold the input.
356 */
357 public void setDataElements(int x, int y, int w, int h, Object inData) {
358 sampleModel.setDataElements(x-sampleModelTranslateX,
359 y-sampleModelTranslateY,
360 w,h,inData,dataBuffer);
361 }
362
363 /**
364 * Copies pixels from Raster srcRaster to this WritableRaster. Each pixel
365 * in srcRaster is copied to the same x,y address in this raster, unless
366 * the address falls outside the bounds of this raster. srcRaster
367 * must have the same number of bands as this WritableRaster. The
368 * copy is a simple copy of source samples to the corresponding destination
369 * samples.
370 * <p>
371 * If all samples of both source and destination Rasters are of
372 * integral type and less than or equal to 32 bits in size, then calling
373 * this method is equivalent to executing the following code for all
374 * <code>x,y</code> addresses valid in both Rasters.
375 * <pre>{@code
376 * Raster srcRaster;
377 * WritableRaster dstRaster;
378 * for (int b = 0; b < srcRaster.getNumBands(); b++) {
379 * dstRaster.setSample(x, y, b, srcRaster.getSample(x, y, b));
380 * }
381 * }</pre>
382 * Thus, when copying an integral type source to an integral type
383 * destination, if the source sample size is greater than the destination
384 * sample size for a particular band, the high order bits of the source
385 * sample are truncated. If the source sample size is less than the
386 * destination size for a particular band, the high order bits of the
387 * destination are zero-extended or sign-extended depending on whether
388 * srcRaster's SampleModel treats the sample as a signed or unsigned
389 * quantity.
390 * <p>
391 * When copying a float or double source to an integral type destination,
392 * each source sample is cast to the destination type. When copying an
393 * integral type source to a float or double destination, the source
394 * is first converted to a 32-bit int (if necessary), using the above
|
39
40 /**
41 * This class extends Raster to provide pixel writing capabilities.
42 * Refer to the class comment for Raster for descriptions of how
43 * a Raster stores pixels.
44 *
45 * <p> The constructors of this class are protected. To instantiate
46 * a WritableRaster, use one of the createWritableRaster factory methods
47 * in the Raster class.
48 */
49 public class WritableRaster extends Raster {
50
51 /**
52 * Constructs a WritableRaster with the given SampleModel. The
53 * WritableRaster's upper left corner is origin and it is the
54 * same size as the SampleModel. A DataBuffer large enough to
55 * describe the WritableRaster is automatically created.
56 * @param sampleModel The SampleModel that specifies the layout.
57 * @param origin The Point that specifies the origin.
58 * @throws RasterFormatException if computing either
59 * {@code origin.x + sampleModel.getWidth()} or
60 * {@code origin.y + sampleModel.getHeight()} results
61 * in integer overflow
62 */
63 protected WritableRaster(SampleModel sampleModel,
64 Point origin) {
65 this(sampleModel,
66 sampleModel.createDataBuffer(),
67 new Rectangle(origin.x,
68 origin.y,
69 sampleModel.getWidth(),
70 sampleModel.getHeight()),
71 origin,
72 null);
73 }
74
75 /**
76 * Constructs a WritableRaster with the given SampleModel and DataBuffer.
77 * The WritableRaster's upper left corner is origin and it is the same
78 * size as the SampleModel. The DataBuffer is not initialized and must
79 * be compatible with SampleModel.
80 * @param sampleModel The SampleModel that specifies the layout.
81 * @param dataBuffer The DataBuffer that contains the image data.
82 * @param origin The Point that specifies the origin.
83 * @throws RasterFormatException if computing either
84 * {@code origin.x + sampleModel.getWidth()} or
85 * {@code origin.y + sampleModel.getHeight()} results
86 * in integer overflow
87 */
88 protected WritableRaster(SampleModel sampleModel,
89 DataBuffer dataBuffer,
90 Point origin) {
91 this(sampleModel,
92 dataBuffer,
93 new Rectangle(origin.x,
94 origin.y,
95 sampleModel.getWidth(),
96 sampleModel.getHeight()),
97 origin,
98 null);
99 }
100
101 /**
102 * Constructs a WritableRaster with the given SampleModel, DataBuffer,
103 * and parent. aRegion specifies the bounding rectangle of the new
104 * Raster. When translated into the base Raster's coordinate
105 * system, aRegion must be contained by the base Raster.
106 * (The base Raster is the Raster's ancestor which has no parent.)
107 * sampleModelTranslate specifies the sampleModelTranslateX and
108 * sampleModelTranslateY values of the new Raster.
109 *
110 * Note that this constructor should generally be called by other
111 * constructors or create methods, it should not be used directly.
112 * @param sampleModel The SampleModel that specifies the layout.
113 * @param dataBuffer The DataBuffer that contains the image data.
114 * @param aRegion The Rectangle that specifies the image area.
115 * @param sampleModelTranslate The Point that specifies the translation
116 * from SampleModel to Raster coordinates.
117 * @param parent The parent (if any) of this raster.
118 * @throws RasterFormatException if {@code aRegion} has width
119 * or height less than or equal to zero, or computing either
120 * {@code aRegion.x + aRegion.width} or
121 * {@code aRegion.y + aRegion.height} results in integer
122 * overflow
123 */
124 protected WritableRaster(SampleModel sampleModel,
125 DataBuffer dataBuffer,
126 Rectangle aRegion,
127 Point sampleModelTranslate,
128 WritableRaster parent){
129 super(sampleModel,dataBuffer,aRegion,sampleModelTranslate,parent);
130 }
131
132 /** Returns the parent WritableRaster (if any) of this WritableRaster,
133 * or else null.
134 * @return the parent of this {@code WritableRaster}, or
135 * {@code null}.
136 */
137 public WritableRaster getWritableParent() {
138 return (WritableRaster)parent;
139 }
140
141 /**
142 * Create a WritableRaster with the same size, SampleModel and DataBuffer
143 * as this one, but with a different location. The new WritableRaster
144 * will possess a reference to the current WritableRaster, accessible
145 * through its getParent() and getWritableParent() methods.
146 *
147 * @param childMinX X coord of the upper left corner of the new Raster.
148 * @param childMinY Y coord of the upper left corner of the new Raster.
149 * @return a {@code WritableRaster} the same as this one except
150 * for the specified location.
151 * @throws RasterFormatException if computing either
152 * {@code childMinX + this.getWidth()} or
153 * {@code childMinY + this.getHeight()} results in integer
154 * overflow
155 */
156 public WritableRaster createWritableTranslatedChild(int childMinX,
157 int childMinY) {
158 return createWritableChild(minX,minY,width,height,
159 childMinX,childMinY,null);
160 }
161
162 /**
163 * Returns a new WritableRaster which shares all or part of this
164 * WritableRaster's DataBuffer. The new WritableRaster will
165 * possess a reference to the current WritableRaster, accessible
166 * through its getParent() and getWritableParent() methods.
167 *
168 * <p> The parentX, parentY, width and height parameters form a
169 * Rectangle in this WritableRaster's coordinate space, indicating
170 * the area of pixels to be shared. An error will be thrown if
171 * this Rectangle is not contained with the bounds of the current
172 * WritableRaster.
173 *
185 * null, it is taken to include all of the bands of the current
186 * WritableRaster in their current order.
187 *
188 * <p> To create a new WritableRaster that contains a subregion of
189 * the current WritableRaster, but shares its coordinate system
190 * and bands, this method should be called with childMinX equal to
191 * parentX, childMinY equal to parentY, and bandList equal to
192 * null.
193 *
194 * @param parentX X coordinate of the upper left corner in this
195 * WritableRaster's coordinates.
196 * @param parentY Y coordinate of the upper left corner in this
197 * WritableRaster's coordinates.
198 * @param w Width of the region starting at (parentX, parentY).
199 * @param h Height of the region starting at (parentX, parentY).
200 * @param childMinX X coordinate of the upper left corner of
201 * the returned WritableRaster.
202 * @param childMinY Y coordinate of the upper left corner of
203 * the returned WritableRaster.
204 * @param bandList Array of band indices, or null to use all bands.
205 * @return a {@code WritableRaster} sharing all or part of the
206 * {@code DataBuffer} of this {@code WritableRaster}.
207 * @exception RasterFormatException if the subregion is outside of the
208 * raster bounds.
209 * @throws RasterFormatException if {@code w} or
210 * {@code h}
211 * is less than or equal to zero, or computing any of
212 * {@code parentX + w}, {@code parentY + h},
213 * {@code childMinX + w}, or
214 * {@code childMinY + h} results in integer
215 * overflow
216 */
217 public WritableRaster createWritableChild(int parentX, int parentY,
218 int w, int h,
219 int childMinX, int childMinY,
220 int bandList[]) {
221 if (parentX < this.minX) {
222 throw new RasterFormatException("parentX lies outside raster");
223 }
224 if (parentY < this.minY) {
225 throw new RasterFormatException("parentY lies outside raster");
226 }
227 if ((parentX+w < parentX) || (parentX+w > this.width + this.minX)) {
228 throw new RasterFormatException("(parentX + width) is outside raster");
229 }
230 if ((parentY+h < parentY) || (parentY+h > this.height + this.minY)) {
231 throw new RasterFormatException("(parentY + height) is outside raster");
232 }
233
234 SampleModel sm;
354 * @throws ArrayIndexOutOfBoundsException if the coordinates are not
355 * in bounds, or if inData is too small to hold the input.
356 */
357 public void setDataElements(int x, int y, int w, int h, Object inData) {
358 sampleModel.setDataElements(x-sampleModelTranslateX,
359 y-sampleModelTranslateY,
360 w,h,inData,dataBuffer);
361 }
362
363 /**
364 * Copies pixels from Raster srcRaster to this WritableRaster. Each pixel
365 * in srcRaster is copied to the same x,y address in this raster, unless
366 * the address falls outside the bounds of this raster. srcRaster
367 * must have the same number of bands as this WritableRaster. The
368 * copy is a simple copy of source samples to the corresponding destination
369 * samples.
370 * <p>
371 * If all samples of both source and destination Rasters are of
372 * integral type and less than or equal to 32 bits in size, then calling
373 * this method is equivalent to executing the following code for all
374 * {@code x,y} addresses valid in both Rasters.
375 * <pre>{@code
376 * Raster srcRaster;
377 * WritableRaster dstRaster;
378 * for (int b = 0; b < srcRaster.getNumBands(); b++) {
379 * dstRaster.setSample(x, y, b, srcRaster.getSample(x, y, b));
380 * }
381 * }</pre>
382 * Thus, when copying an integral type source to an integral type
383 * destination, if the source sample size is greater than the destination
384 * sample size for a particular band, the high order bits of the source
385 * sample are truncated. If the source sample size is less than the
386 * destination size for a particular band, the high order bits of the
387 * destination are zero-extended or sign-extended depending on whether
388 * srcRaster's SampleModel treats the sample as a signed or unsigned
389 * quantity.
390 * <p>
391 * When copying a float or double source to an integral type destination,
392 * each source sample is cast to the destination type. When copying an
393 * integral type source to a float or double destination, the source
394 * is first converted to a 32-bit int (if necessary), using the above
|