1 /* Copyright (C) 2000, 2001, 2002 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., 59 Temple Place, Suite 330, 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. */
37 package java
.awt
.image
;
40 * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
42 public abstract class SampleModel
44 /** Width of image described. */
47 /** Height of image described. */
50 /** Number of bands in the image described. */
51 protected int numBands
;
54 * The DataBuffer type that is used to store the data of the image
57 protected int dataType
;
59 public SampleModel(int dataType
, int w
, int h
, int numBands
)
61 if ((w
<=0) || (h
<=0)) throw new IllegalArgumentException();
63 // FIXME: How can an int be greater than Integer.MAX_VALUE?
64 // FIXME: How do we identify an unsupported data type?
66 this.dataType
= dataType
;
69 this.numBands
= numBands
;
72 public final int getWidth()
77 public final int getHeight()
82 public final int getNumBands()
87 public abstract int getNumDataElements();
89 public final int getDataType()
94 public int getTransferType()
96 // FIXME: Is this a reasonable default implementation?
100 public int[] getPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
102 if (iArray
== null) iArray
= new int[numBands
];
103 for (int b
=0; b
<numBands
; b
++) iArray
[b
] = getSample(x
, y
, b
, data
);
109 * This method is provided as a faster alternative to getPixel(),
110 * that can be used when there is no need to decode the pixel into
111 * separate sample values.
113 * @param obj An array to return the pixel data in. If null, an
114 * array of the right type and size will be created.
116 * @return A single pixel as an array object of a primitive type,
117 * based on the transfer type. Eg. if transfer type is
118 * DataBuffer.TYPE_USHORT, then a short[] object is returned.
120 public abstract Object
getDataElements(int x
, int y
, Object obj
,
124 public Object
getDataElements(int x
, int y
, int w
, int h
, Object obj
,
128 int numDataElements
= getNumDataElements();
129 int dataSize
= numDataElements
*size
;
133 switch (getTransferType())
135 case DataBuffer
.TYPE_BYTE
:
136 obj
= new byte[dataSize
];
138 case DataBuffer
.TYPE_USHORT
:
139 obj
= new short[dataSize
];
141 case DataBuffer
.TYPE_INT
:
142 obj
= new int[dataSize
];
145 // Seems like the only sensible thing to do.
146 throw new ClassCastException();
149 Object pixelData
= null;
151 for (int yy
= y
; yy
<(y
+h
); yy
++)
153 for (int xx
= x
; xx
<(x
+w
); xx
++)
155 pixelData
= getDataElements(xx
, yy
, pixelData
, data
);
156 System
.arraycopy(pixelData
, 0, obj
, outOffset
,
158 outOffset
+= numDataElements
;
164 public abstract void setDataElements(int x
, int y
, Object obj
,
167 public void setDataElements(int x
, int y
, int w
, int h
,
168 Object obj
, DataBuffer data
)
171 int numDataElements
= getNumDataElements();
172 int dataSize
= numDataElements
*size
;
175 switch (getTransferType())
177 case DataBuffer
.TYPE_BYTE
:
178 pixelData
= new byte[numDataElements
];
180 case DataBuffer
.TYPE_USHORT
:
181 pixelData
= new short[numDataElements
];
183 case DataBuffer
.TYPE_INT
:
184 pixelData
= new int[numDataElements
];
187 // Seems like the only sensible thing to do.
188 throw new ClassCastException();
192 for (int yy
=y
; yy
<(y
+h
); yy
++)
194 for (int xx
=x
; xx
<(x
+w
); xx
++)
196 System
.arraycopy(obj
, inOffset
, pixelData
, 0,
198 setDataElements(xx
, yy
, pixelData
, data
);
199 inOffset
+= numDataElements
;
204 public float[] getPixel(int x
, int y
, float[] fArray
, DataBuffer data
)
206 if (fArray
== null) fArray
= new float[numBands
];
208 for (int b
=0; b
<numBands
; b
++)
210 fArray
[b
] = getSampleFloat(x
, y
, b
, data
);
215 public double[] getPixel(int x
, int y
, double[] dArray
, DataBuffer data
) {
216 if (dArray
== null) dArray
= new double[numBands
];
217 for (int b
=0; b
<numBands
; b
++)
219 dArray
[b
] = getSampleDouble(x
, y
, b
, data
);
224 /* FIXME: Should it return a banded or pixel interleaved array of
225 samples? (Assume interleaved.) */
226 public int[] getPixels(int x
, int y
, int w
, int h
, int[] iArray
,
232 if (iArray
== null) iArray
= new int[w
*h
*numBands
];
233 for (int yy
=y
; yy
<(y
+h
); yy
++)
235 for (int xx
=x
; xx
<(x
+w
); xx
++)
237 getPixel(xx
, yy
, pixel
, data
);
238 System
.arraycopy(pixel
, 0, iArray
, outOffset
, numBands
);
239 outOffset
+= numBands
;
245 /* FIXME: Should it return a banded or pixel interleaved array of
246 samples? (Assume interleaved.) */
247 public float[] getPixels(int x
, int y
, int w
, int h
, float[] fArray
,
252 float[] pixel
= null;
253 if (fArray
== null) fArray
= new float[w
*h
*numBands
];
254 for (int yy
=y
; yy
<(y
+h
); yy
++)
256 for (int xx
=x
; xx
<(x
+w
); xx
++)
258 getPixel(xx
, yy
, pixel
, data
);
259 System
.arraycopy(pixel
, 0, fArray
, outOffset
, numBands
);
260 outOffset
+= numBands
;
266 /* FIXME: Should it return a banded or pixel interleaved array of
267 samples? (Assume interleaved.) */
268 public double[] getPixels(int x
, int y
, int w
, int h
, double[] dArray
,
273 double[] pixel
= null;
274 if (dArray
== null) dArray
= new double[w
*h
*numBands
];
275 for (int yy
=y
; yy
<(y
+h
); yy
++)
277 for (int xx
=x
; xx
<(x
+w
); xx
++)
279 getPixel(xx
, yy
, pixel
, data
);
280 System
.arraycopy(pixel
, 0, dArray
, outOffset
, numBands
);
281 outOffset
+= numBands
;
287 public abstract int getSample(int x
, int y
, int b
, DataBuffer data
);
289 public float getSampleFloat(int x
, int y
, int b
, DataBuffer data
)
291 return getSample(x
, y
, b
, data
);
294 public double getSampleDouble(int x
, int y
, int b
, DataBuffer data
)
296 return getSampleFloat(x
, y
, b
, data
);
299 public int[] getSamples(int x
, int y
, int w
, int h
, int b
,
300 int[] iArray
, DataBuffer data
)
304 if (iArray
== null) iArray
= new int[size
];
305 for (int yy
=y
; yy
<(y
+h
); yy
++)
307 for (int xx
=x
; xx
<(x
+w
); xx
++)
309 iArray
[outOffset
++] = getSample(xx
, yy
, b
, data
);
315 public float[] getSamples(int x
, int y
, int w
, int h
, int b
,
316 float[] fArray
, DataBuffer data
)
320 if (fArray
== null) fArray
= new float[size
];
321 for (int yy
=y
; yy
<(y
+h
); yy
++)
323 for (int xx
=x
; xx
<(x
+w
); xx
++)
325 fArray
[outOffset
++] = getSampleFloat(xx
, yy
, b
, data
);
331 public double[] getSamples(int x
, int y
, int w
, int h
, int b
,
332 double[] dArray
, DataBuffer data
)
336 if (dArray
== null) dArray
= new double[size
];
337 for (int yy
=y
; yy
<(y
+h
); yy
++)
339 for (int xx
=x
; xx
<(x
+w
); xx
++)
341 dArray
[outOffset
++] = getSampleDouble(xx
, yy
, b
, data
);
347 public void setPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
349 for (int b
=0; b
<numBands
; b
++) setSample(x
, y
, b
, iArray
[b
], data
);
352 public void setPixel(int x
, int y
, float[] fArray
, DataBuffer data
)
354 for (int b
=0; b
<numBands
; b
++) setSample(x
, y
, b
, fArray
[b
], data
);
357 public void setPixel(int x
, int y
, double[] dArray
, DataBuffer data
)
359 for (int b
=0; b
<numBands
; b
++) setSample(x
, y
, b
, dArray
[b
], data
);
362 public void setPixels(int x
, int y
, int w
, int h
, int[] iArray
,
366 int[] pixel
= new int[numBands
];
367 for (int yy
=y
; yy
<(y
+h
); yy
++)
369 for (int xx
=x
; xx
<(x
+w
); xx
++)
371 System
.arraycopy(iArray
, inOffset
, pixel
, 0, numBands
);
372 setPixel(xx
, yy
, pixel
, data
);
373 inOffset
+= numBands
;
378 public void setPixels(int x
, int y
, int w
, int h
, float[] fArray
,
382 float[] pixel
= new float[numBands
];
383 for (int yy
=y
; yy
<(y
+h
); yy
++)
385 for (int xx
=x
; xx
<(x
+w
); xx
++)
387 System
.arraycopy(fArray
, inOffset
, pixel
, 0, numBands
);
388 setPixel(xx
, yy
, pixel
, data
);
389 inOffset
+= numBands
;
394 public void setPixels(int x
, int y
, int w
, int h
, double[] dArray
,
398 double[] pixel
= new double[numBands
];
399 for (int yy
=y
; yy
<(y
+h
); yy
++)
401 for (int xx
=x
; xx
<(x
+w
); xx
++)
403 System
.arraycopy(dArray
, inOffset
, pixel
, 0, numBands
);
404 setPixel(xx
, yy
, pixel
, data
);
405 inOffset
+= numBands
;
410 public abstract void setSample(int x
, int y
, int b
, int s
,
413 public void setSample(int x
, int y
, int b
, float s
,
416 setSample(x
, y
, b
, (int) s
, data
);
419 public void setSample(int x
, int y
, int b
, double s
,
422 setSample(x
, y
, b
, (float) s
, data
);
425 public void setSamples(int x
, int y
, int w
, int h
, int b
,
426 int[] iArray
, DataBuffer data
)
430 for (int yy
=y
; yy
<(y
+h
); yy
++)
431 for (int xx
=x
; xx
<(x
+w
); xx
++)
432 setSample(xx
, yy
, b
, iArray
[inOffset
++], data
);
435 public void setSamples(int x
, int y
, int w
, int h
, int b
,
436 float[] fArray
, DataBuffer data
)
440 for (int yy
=y
; yy
<(y
+h
); yy
++)
441 for (int xx
=x
; xx
<(x
+w
); xx
++)
442 setSample(xx
, yy
, b
, fArray
[inOffset
++], data
);
446 public void setSamples(int x
, int y
, int w
, int h
, int b
,
447 double[] dArray
, DataBuffer data
) {
450 for (int yy
=y
; yy
<(y
+h
); yy
++)
451 for (int xx
=x
; xx
<(x
+w
); xx
++)
452 setSample(xx
, yy
, b
, dArray
[inOffset
++], data
);
455 public abstract SampleModel
createCompatibleSampleModel(int w
, int h
);
457 public abstract SampleModel
createSubsetSampleModel(int[] bands
);
459 public abstract DataBuffer
createDataBuffer();
461 public abstract int[] getSampleSize();
463 public abstract int getSampleSize(int band
);