1 /* Copyright (C) 2000, 2002, 2003, 2006, Free Software Foundation
3 This file is part of GNU Classpath.
5 GNU Classpath is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Classpath is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Classpath; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version. */
38 package java
.awt
.image
;
40 import java
.awt
.Point
;
41 import java
.awt
.Rectangle
;
44 * A raster with methods to support updating pixel values.
46 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
48 public class WritableRaster
extends Raster
51 * Creates a new <code>WritableRaster</code>.
53 * @param sampleModel the sample model.
54 * @param origin the origin.
56 protected WritableRaster(SampleModel sampleModel
, Point origin
)
58 this(sampleModel
, sampleModel
.createDataBuffer(), origin
);
62 * Creates a new <code>WritableRaster</code> instance.
64 * @param sampleModel the sample model.
65 * @param dataBuffer the data buffer.
66 * @param origin the origin.
68 protected WritableRaster(SampleModel sampleModel
, DataBuffer dataBuffer
,
71 this(sampleModel
, dataBuffer
,
72 new Rectangle(origin
!= null ? origin
.x
: 0,
73 origin
!= null ? origin
.y
: 0,
74 sampleModel
.getWidth(), sampleModel
.getHeight()),
79 * Creates a new <code>WritableRaster</code> instance.
81 * @param sampleModel the sample model.
82 * @param dataBuffer the data buffer.
83 * @param aRegion the raster's bounds.
84 * @param sampleModelTranslate the translation.
85 * @param parent the parent.
87 protected WritableRaster(SampleModel sampleModel
,
88 DataBuffer dataBuffer
,
90 Point sampleModelTranslate
,
91 WritableRaster parent
)
93 super(sampleModel
, dataBuffer
, aRegion
, sampleModelTranslate
, parent
);
97 * Returns the raster's parent, cast as a {@link WritableRaster}.
99 * @return The raster's parent.
101 public WritableRaster
getWritableParent()
103 return (WritableRaster
) getParent();
111 public WritableRaster
createWritableTranslatedChild(int childMinX
,
114 return createWritableChild(minX
, minY
, width
, height
,
115 childMinX
, childMinY
, null);
129 public WritableRaster
createWritableChild(int parentX
, int parentY
,
130 int w
, int h
, int childMinX
, int childMinY
, int[] bandList
)
132 // This mirrors the code from the super class
134 if (parentX
< minX
|| parentX
+ w
> minX
+ width
135 || parentY
< minY
|| parentY
+ h
> minY
+ height
)
136 throw new RasterFormatException("Child raster extends beyond parent");
138 SampleModel sm
= (bandList
== null) ?
140 sampleModel
.createSubsetSampleModel(bandList
);
142 return new WritableRaster(sm
, getDataBuffer(),
143 new Rectangle(childMinX
, childMinY
, w
, h
),
144 new Point(sampleModelTranslateX
+ childMinX
-
146 sampleModelTranslateY
+ childMinY
-
151 public Raster
createChild(int parentX
, int parentY
, int width
,
152 int height
, int childMinX
, int childMinY
,
155 if (parentX
< minX
|| parentX
+ width
> minX
+ this.width
156 || parentY
< minY
|| parentY
+ height
> minY
+ this.height
)
157 throw new RasterFormatException("Child raster extends beyond parent");
159 SampleModel sm
= (bandList
== null) ?
161 sampleModel
.createSubsetSampleModel(bandList
);
163 return new WritableRaster(sm
, dataBuffer
,
164 new Rectangle(childMinX
, childMinY
, width
, height
),
165 new Point(sampleModelTranslateX
+ childMinX
- parentX
,
166 sampleModelTranslateY
+ childMinY
- parentY
),
170 public void setDataElements(int x
, int y
, Object inData
)
172 sampleModel
.setDataElements(x
- sampleModelTranslateX
,
173 y
- sampleModelTranslateY
, inData
, dataBuffer
);
176 public void setDataElements(int x
, int y
, Raster inRaster
)
178 Object dataElements
= getDataElements(0, 0, inRaster
.getWidth(),
179 inRaster
.getHeight(), null);
180 setDataElements(x
, y
, dataElements
);
183 public void setDataElements(int x
, int y
, int w
, int h
, Object inData
)
185 sampleModel
.setDataElements(x
- sampleModelTranslateX
,
186 y
- sampleModelTranslateY
, w
, h
, inData
, dataBuffer
);
193 public void setRect(Raster srcRaster
)
195 setRect(0, 0, srcRaster
);
204 public void setRect(int dx
, int dy
, Raster srcRaster
)
206 Rectangle targetUnclipped
= new Rectangle(srcRaster
.getMinX() + dx
,
207 srcRaster
.getMinY() + dy
, srcRaster
.getWidth(), srcRaster
.getHeight());
209 Rectangle target
= getBounds().intersection(targetUnclipped
);
211 if (target
.isEmpty()) return;
213 int sx
= target
.x
- dx
;
214 int sy
= target
.y
- dy
;
216 // FIXME: Do tests on rasters and use get/set data instead.
218 /* The JDK documentation seems to imply this implementation.
219 (the trucation of higher bits), but an implementation using
220 get/setDataElements would be more efficient. None of the
221 implementations would do anything sensible when the sample
224 But this is probably not the place to consider such
227 int[] pixels
= srcRaster
.getPixels(sx
, sy
, target
.width
, target
.height
,
230 setPixels(target
.x
, target
.y
, target
.width
, target
.height
, pixels
);
234 * Sets the samples for the pixel at (x, y) in the raster to the specified
237 * @param x the x-coordinate of the pixel.
238 * @param y the y-coordinate of the pixel.
239 * @param iArray the sample values (<code>null</code> not permitted).
241 * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
243 public void setPixel(int x
, int y
, int[] iArray
)
245 sampleModel
.setPixel(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
250 * Sets the samples for the pixel at (x, y) in the raster to the specified
253 * @param x the x-coordinate of the pixel.
254 * @param y the y-coordinate of the pixel.
255 * @param fArray the sample values (<code>null</code> not permitted).
257 * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
259 public void setPixel(int x
, int y
, float[] fArray
)
261 sampleModel
.setPixel(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
266 * Sets the samples for the pixel at (x, y) in the raster to the specified
269 * @param x the x-coordinate of the pixel.
270 * @param y the y-coordinate of the pixel.
271 * @param dArray the sample values (<code>null</code> not permitted).
273 * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
275 public void setPixel(int x
, int y
, double[] dArray
)
277 sampleModel
.setPixel(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
282 * Sets the sample values for the pixels in the region specified by
283 * (x, y, w, h) in the raster. The array is ordered by pixels (that is, all
284 * the samples for the first pixel are grouped together, followed by all the
285 * samples for the second pixel, and so on).
287 * @param x the x-coordinate of the top-left pixel.
288 * @param y the y-coordinate of the top-left pixel.
289 * @param w the width of the region of pixels.
290 * @param h the height of the region of pixels.
291 * @param iArray the pixel sample values (<code>null</code> not permitted).
293 * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
295 public void setPixels(int x
, int y
, int w
, int h
, int[] iArray
)
297 sampleModel
.setPixels(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
298 w
, h
, iArray
, dataBuffer
);
302 * Sets the sample values for the pixels in the region specified by
303 * (x, y, w, h) in the raster. The array is ordered by pixels (that is, all
304 * the samples for the first pixel are grouped together, followed by all the
305 * samples for the second pixel, and so on).
307 * @param x the x-coordinate of the top-left pixel.
308 * @param y the y-coordinate of the top-left pixel.
309 * @param w the width of the region of pixels.
310 * @param h the height of the region of pixels.
311 * @param fArray the pixel sample values (<code>null</code> not permitted).
313 * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
315 public void setPixels(int x
, int y
, int w
, int h
, float[] fArray
)
317 sampleModel
.setPixels(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
318 w
, h
, fArray
, dataBuffer
);
322 * Sets the sample values for the pixels in the region specified by
323 * (x, y, w, h) in the raster. The array is ordered by pixels (that is, all
324 * the samples for the first pixel are grouped together, followed by all the
325 * samples for the second pixel, and so on).
327 * @param x the x-coordinate of the top-left pixel.
328 * @param y the y-coordinate of the top-left pixel.
329 * @param w the width of the region of pixels.
330 * @param h the height of the region of pixels.
331 * @param dArray the pixel sample values (<code>null</code> not permitted).
333 * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
335 public void setPixels(int x
, int y
, int w
, int h
, double[] dArray
)
337 sampleModel
.setPixels(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
338 w
, h
, dArray
, dataBuffer
);
342 * Sets the sample value for a band for the pixel at (x, y) in the raster.
344 * @param x the x-coordinate of the pixel.
345 * @param y the y-coordinate of the pixel.
346 * @param b the band (in the range <code>0</code> to
347 * <code>getNumBands() - 1</code>).
348 * @param s the sample value.
350 public void setSample(int x
, int y
, int b
, int s
)
352 sampleModel
.setSample(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
357 * Sets the sample value for a band for the pixel at (x, y) in the raster.
359 * @param x the x-coordinate of the pixel.
360 * @param y the y-coordinate of the pixel.
361 * @param b the band (in the range <code>0</code> to
362 * <code>getNumBands() - 1</code>).
363 * @param s the sample value.
365 public void setSample(int x
, int y
, int b
, float s
)
367 sampleModel
.setSample(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
372 * Sets the sample value for a band for the pixel at (x, y) in the raster.
374 * @param x the x-coordinate of the pixel.
375 * @param y the y-coordinate of the pixel.
376 * @param b the band (in the range <code>0</code> to
377 * <code>getNumBands() - 1</code>).
378 * @param s the sample value.
380 public void setSample(int x
, int y
, int b
, double s
)
382 sampleModel
.setSample(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
387 * Sets the sample values for one band for the pixels in the region
388 * specified by (x, y, w, h) in the raster.
390 * @param x the x-coordinate of the top-left pixel.
391 * @param y the y-coordinate of the top-left pixel.
392 * @param w the width of the region of pixels.
393 * @param h the height of the region of pixels.
394 * @param b the band (in the range <code>0</code> to
395 * </code>getNumBands() - 1</code>).
396 * @param iArray the sample values (<code>null</code> not permitted).
398 * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
400 public void setSamples(int x
, int y
, int w
, int h
, int b
,
403 sampleModel
.setSamples(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
404 w
, h
, b
, iArray
, dataBuffer
);
408 * Sets the sample values for one band for the pixels in the region
409 * specified by (x, y, w, h) in the raster.
411 * @param x the x-coordinate of the top-left pixel.
412 * @param y the y-coordinate of the top-left pixel.
413 * @param w the width of the region of pixels.
414 * @param h the height of the region of pixels.
415 * @param b the band (in the range <code>0</code> to
416 * </code>getNumBands() - 1</code>).
417 * @param fArray the sample values (<code>null</code> not permitted).
419 * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
421 public void setSamples(int x
, int y
, int w
, int h
, int b
,
424 sampleModel
.setSamples(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
425 w
, h
, b
, fArray
, dataBuffer
);
429 * Sets the sample values for one band for the pixels in the region
430 * specified by (x, y, w, h) in the raster.
432 * @param x the x-coordinate of the top-left pixel.
433 * @param y the y-coordinate of the top-left pixel.
434 * @param w the width of the region of pixels.
435 * @param h the height of the region of pixels.
436 * @param b the band (in the range <code>0</code> to
437 * </code>getNumBands() - 1</code>).
438 * @param dArray the sample values (<code>null</code> not permitted).
440 * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
442 public void setSamples(int x
, int y
, int w
, int h
, int b
,
445 sampleModel
.setSamples(x
- sampleModelTranslateX
, y
- sampleModelTranslateY
,
446 w
, h
, b
, dArray
, dataBuffer
);