1 /* Copyright (C) 2000, 2002, 2003 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 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
48 protected SampleModel sampleModel
;
49 protected DataBuffer dataBuffer
;
54 protected int sampleModelTranslateX
;
55 protected int sampleModelTranslateY
;
56 protected int numBands
;
57 protected int numDataElements
;
58 protected Raster parent
;
60 protected Raster(SampleModel sampleModel
, Point origin
)
62 this(sampleModel
, sampleModel
.createDataBuffer(), origin
);
65 protected Raster(SampleModel sampleModel
, DataBuffer dataBuffer
,
68 this(sampleModel
, dataBuffer
,
69 new Rectangle(origin
.x
, origin
.y
,
70 sampleModel
.getWidth(), sampleModel
.getHeight()),
74 protected Raster(SampleModel sampleModel
, DataBuffer dataBuffer
,
76 Point sampleModelTranslate
, Raster parent
)
78 this.sampleModel
= sampleModel
;
79 this.dataBuffer
= dataBuffer
;
80 this.minX
= aRegion
.x
;
81 this.minY
= aRegion
.y
;
82 this.width
= aRegion
.width
;
83 this.height
= aRegion
.height
;
85 // If sampleModelTranslate is null, use (0,0). Methods such as
86 // Raster.createRaster are specified to allow for a null argument.
87 if (sampleModelTranslate
!= null)
89 this.sampleModelTranslateX
= sampleModelTranslate
.x
;
90 this.sampleModelTranslateY
= sampleModelTranslate
.y
;
93 this.numBands
= sampleModel
.getNumBands();
94 this.numDataElements
= sampleModel
.getNumDataElements();
98 public static WritableRaster
createInterleavedRaster(int dataType
,
103 int[] bandOffsets
= new int[bands
];
104 // TODO: Maybe not generate this every time.
105 for (int b
=0; b
<bands
; b
++) bandOffsets
[b
] = b
;
107 int scanlineStride
= bands
*w
;
108 return createInterleavedRaster(dataType
, w
, h
, scanlineStride
, bands
,
109 bandOffsets
, location
);
112 public static WritableRaster
createInterleavedRaster(int dataType
,
119 SampleModel sm
= new ComponentSampleModel(dataType
,
124 return createWritableRaster(sm
, location
);
127 public static WritableRaster
createBandedRaster(int dataType
,
128 int w
, int h
, int bands
,
131 SampleModel sm
= new BandedSampleModel(dataType
, w
, h
, bands
);
132 return createWritableRaster(sm
, location
);
135 public static WritableRaster
createBandedRaster(int dataType
,
142 SampleModel sm
= new BandedSampleModel(dataType
, w
, h
, scanlineStride
,
143 bankIndices
, bandOffsets
);
144 return createWritableRaster(sm
, location
);
147 public static WritableRaster
createPackedRaster(int dataType
,
152 SampleModel sm
= new SinglePixelPackedSampleModel(dataType
,
155 return createWritableRaster(sm
, location
);
158 public static WritableRaster
createPackedRaster(int dataType
,
160 int bands
, int bitsPerBand
,
163 if (bands
<= 0 || (bands
* bitsPerBand
> getTypeBits(dataType
)))
164 throw new IllegalArgumentException();
169 sm
= new MultiPixelPackedSampleModel(dataType
, w
, h
, bitsPerBand
);
172 int[] bandMasks
= new int[bands
];
174 for (int bits
= bitsPerBand
; --bits
!= 0;)
175 mask
= (mask
<< 1) | 0x1;
176 for (int i
= 0; i
< bands
; i
++)
179 mask
<<= bitsPerBand
;
182 sm
= new SinglePixelPackedSampleModel(dataType
, w
, h
, bandMasks
);
184 return createWritableRaster(sm
, location
);
187 public static WritableRaster
188 createInterleavedRaster(DataBuffer dataBuffer
, int w
, int h
,
189 int scanlineStride
, int pixelStride
,
190 int[] bandOffsets
, Point location
)
192 SampleModel sm
= new ComponentSampleModel(dataBuffer
.getDataType(),
197 return createWritableRaster(sm
, dataBuffer
, location
);
201 WritableRaster
createBandedRaster(DataBuffer dataBuffer
,
208 SampleModel sm
= new BandedSampleModel(dataBuffer
.getDataType(),
209 w
, h
, scanlineStride
,
210 bankIndices
, bandOffsets
);
211 return createWritableRaster(sm
, dataBuffer
, location
);
214 public static WritableRaster
215 createPackedRaster(DataBuffer dataBuffer
,
222 new SinglePixelPackedSampleModel(dataBuffer
.getDataType(),
226 return createWritableRaster(sm
, dataBuffer
, location
);
229 public static WritableRaster
230 createPackedRaster(DataBuffer dataBuffer
,
236 new MultiPixelPackedSampleModel(dataBuffer
.getDataType(),
239 return createWritableRaster(sm
, dataBuffer
, location
);
242 public static Raster
createRaster(SampleModel sm
, DataBuffer db
,
245 return new Raster(sm
, db
, location
);
248 public static WritableRaster
createWritableRaster(SampleModel sm
,
251 return new WritableRaster(sm
, location
);
254 public static WritableRaster
createWritableRaster(SampleModel sm
,
258 return new WritableRaster(sm
, db
, location
);
261 public Raster
getParent()
266 public final int getSampleModelTranslateX()
268 return sampleModelTranslateX
;
271 public final int getSampleModelTranslateY()
273 return sampleModelTranslateY
;
276 public WritableRaster
createCompatibleWritableRaster()
278 return new WritableRaster(getSampleModel(), new Point(minX
, minY
));
281 public WritableRaster
createCompatibleWritableRaster(int w
, int h
)
283 return createCompatibleWritableRaster(minX
, minY
, w
, h
);
286 public WritableRaster
createCompatibleWritableRaster(Rectangle rect
)
288 return createCompatibleWritableRaster(rect
.x
, rect
.y
,
289 rect
.width
, rect
.height
);
292 public WritableRaster
createCompatibleWritableRaster(int x
, int y
,
295 SampleModel sm
= getSampleModel().createCompatibleSampleModel(w
, h
);
296 return new WritableRaster(sm
, sm
.createDataBuffer(),
300 public Raster
createTranslatedChild(int childMinX
, int childMinY
) {
301 int tcx
= sampleModelTranslateX
- minX
+ childMinX
;
302 int tcy
= sampleModelTranslateY
- minY
+ childMinY
;
304 return new Raster(sampleModel
, dataBuffer
,
305 new Rectangle(childMinX
, childMinY
,
311 public Raster
createChild(int parentX
, int parentY
, int width
,
312 int height
, int childMinX
, int childMinY
,
315 /* FIXME: Throw RasterFormatException if child bounds extends
316 beyond the bounds of this raster. */
318 SampleModel sm
= (bandList
== null) ?
320 sampleModel
.createSubsetSampleModel(bandList
);
325 +-------------------------
331 |trans\ /<\-- deltaTrans
333 | /|`| \__ parent [x, y]
340 parent_xy - parent_trans = child_xy - child_trans
342 child_trans = parent_trans + child_xy - parent_xy
345 return new Raster(sm
, dataBuffer
,
346 new Rectangle(childMinX
, childMinY
,
348 new Point(sampleModelTranslateX
+childMinX
-parentX
,
349 sampleModelTranslateY
+childMinY
-parentY
),
353 public Rectangle
getBounds()
355 return new Rectangle(minX
, minY
, width
, height
);
358 public final int getMinX()
363 public final int getMinY()
368 public final int getWidth()
373 public final int getHeight()
378 public final int getNumBands()
383 public final int getNumDataElements()
385 return numDataElements
;
388 public final int getTransferType()
390 return sampleModel
.getTransferType();
393 public DataBuffer
getDataBuffer()
398 public SampleModel
getSampleModel()
403 public Object
getDataElements(int x
, int y
, Object outData
)
405 return sampleModel
.getDataElements(x
-sampleModelTranslateX
,
406 y
-sampleModelTranslateY
,
407 outData
, dataBuffer
);
410 public Object
getDataElements(int x
, int y
, int w
, int h
,
413 return sampleModel
.getDataElements(x
-sampleModelTranslateX
,
414 y
-sampleModelTranslateY
,
415 w
, h
, outData
, dataBuffer
);
418 public int[] getPixel(int x
, int y
, int[] iArray
)
420 return sampleModel
.getPixel(x
-sampleModelTranslateX
,
421 y
-sampleModelTranslateY
,
425 public float[] getPixel(int x
, int y
, float[] fArray
)
427 return sampleModel
.getPixel(x
-sampleModelTranslateX
,
428 y
-sampleModelTranslateY
,
432 public double[] getPixel(int x
, int y
, double[] dArray
)
434 return sampleModel
.getPixel(x
-sampleModelTranslateX
,
435 y
-sampleModelTranslateY
,
439 public int[] getPixels(int x
, int y
, int w
, int h
, int[] iArray
)
441 return sampleModel
.getPixels(x
-sampleModelTranslateX
,
442 y
-sampleModelTranslateY
,
443 w
, h
, iArray
, dataBuffer
);
446 public float[] getPixels(int x
, int y
, int w
, int h
,
449 return sampleModel
.getPixels(x
-sampleModelTranslateX
,
450 y
-sampleModelTranslateY
,
451 w
, h
, fArray
, dataBuffer
);
454 public double[] getPixels(int x
, int y
, int w
, int h
,
457 return sampleModel
.getPixels(x
-sampleModelTranslateX
,
458 y
-sampleModelTranslateY
,
459 w
, h
, dArray
, dataBuffer
);
462 public int getSample(int x
, int y
, int b
)
464 return sampleModel
.getSample(x
-sampleModelTranslateX
,
465 y
-sampleModelTranslateY
,
469 public float getSampleFloat(int x
, int y
, int b
)
471 return sampleModel
.getSampleFloat(x
-sampleModelTranslateX
,
472 y
-sampleModelTranslateY
,
476 public double getSampleDouble(int x
, int y
, int b
)
478 return sampleModel
.getSampleDouble(x
-sampleModelTranslateX
,
479 y
-sampleModelTranslateY
,
483 public int[] getSamples(int x
, int y
, int w
, int h
, int b
,
486 return sampleModel
.getSamples(x
-sampleModelTranslateX
,
487 y
-sampleModelTranslateY
,
488 w
, h
, b
, iArray
, dataBuffer
);
491 public float[] getSamples(int x
, int y
, int w
, int h
, int b
,
494 return sampleModel
.getSamples(x
-sampleModelTranslateX
,
495 y
-sampleModelTranslateY
,
496 w
, h
, b
, fArray
, dataBuffer
);
499 public double[] getSamples(int x
, int y
, int w
, int h
, int b
,
502 return sampleModel
.getSamples(x
-sampleModelTranslateX
,
503 y
-sampleModelTranslateY
,
504 w
, h
, b
, dArray
, dataBuffer
);
508 * Create a String representing the stat of this Raster.
509 * @return A String representing the stat of this Raster.
510 * @see java.lang.Object#toString()
512 public String
toString()
514 StringBuffer result
= new StringBuffer();
516 result
.append(getClass().getName());
518 result
.append(minX
).append(",").append(minY
).append("), ");
519 result
.append(width
).append(" x ").append(height
).append(",");
520 result
.append(sampleModel
).append(",");
521 result
.append(dataBuffer
);
524 return result
.toString();
527 // Map from datatype to bits
528 private static int getTypeBits(int dataType
)
532 case DataBuffer
.TYPE_BYTE
:
534 case DataBuffer
.TYPE_USHORT
:
535 case DataBuffer
.TYPE_SHORT
:
537 case DataBuffer
.TYPE_INT
:
538 case DataBuffer
.TYPE_FLOAT
:
540 case DataBuffer
.TYPE_DOUBLE
: