1 /* Copyright (C) 2000, 2001, 2002, 2005, 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. */
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. Package-private here,
51 shadowed by ComponentSampleModel. */
52 protected int numBands
;
55 * The DataBuffer type that is used to store the data of the image
58 protected int dataType
;
61 * Creates a new sample model with the specified attributes.
63 * @param dataType the data type (one of {@link DataBuffer#TYPE_BYTE},
64 * {@link DataBuffer#TYPE_USHORT}, {@link DataBuffer#TYPE_SHORT},
65 * {@link DataBuffer#TYPE_INT}, {@link DataBuffer#TYPE_FLOAT},
66 * {@link DataBuffer#TYPE_DOUBLE} or {@link DataBuffer#TYPE_UNDEFINED}).
67 * @param w the width in pixels (must be greater than zero).
68 * @param h the height in pixels (must be greater than zero).
69 * @param numBands the number of bands (must be greater than zero).
71 * @throws IllegalArgumentException if <code>dataType</code> is not one of
73 * @throws IllegalArgumentException if <code>w</code> is less than or equal
75 * @throws IllegalArgumentException if <code>h</code> is less than or equal
77 * @throws IllegalArgumentException if <code>w * h</code> is greater than
78 * {@link Integer#MAX_VALUE}.
80 public SampleModel(int dataType
, int w
, int h
, int numBands
)
82 if (dataType
!= DataBuffer
.TYPE_UNDEFINED
)
83 if (dataType
< DataBuffer
.TYPE_BYTE
|| dataType
> DataBuffer
.TYPE_DOUBLE
)
84 throw new IllegalArgumentException("Unrecognised 'dataType' argument.");
86 if ((w
<= 0) || (h
<= 0))
87 throw new IllegalArgumentException((w
<= 0 ?
" width<=0" : " width is ok")
88 + (h
<= 0 ?
" height<=0" : " height is ok"));
90 long area
= (long) w
* (long) h
;
91 if (area
> Integer
.MAX_VALUE
)
92 throw new IllegalArgumentException("w * h exceeds Integer.MAX_VALUE.");
95 throw new IllegalArgumentException("Requires numBands > 0.");
97 this.dataType
= dataType
;
100 this.numBands
= numBands
;
103 public final int getWidth()
108 public final int getHeight()
113 public final int getNumBands()
118 public abstract int getNumDataElements();
120 public final int getDataType()
125 public int getTransferType()
127 // FIXME: Is this a reasonable default implementation?
131 public int[] getPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
134 iArray
= new int[numBands
];
135 for (int b
= 0; b
< numBands
; b
++)
136 iArray
[b
] = getSample(x
, y
, b
, data
);
142 * This method is provided as a faster alternative to getPixel(),
143 * that can be used when there is no need to decode the pixel into
144 * separate sample values.
146 * @param obj An array to return the pixel data in. If null, an
147 * array of the right type and size will be created.
149 * @return A single pixel as an array object of a primitive type,
150 * based on the transfer type. Eg. if transfer type is
151 * DataBuffer.TYPE_USHORT, then a short[] object is returned.
153 public abstract Object
getDataElements(int x
, int y
, Object obj
,
157 public Object
getDataElements(int x
, int y
, int w
, int h
, Object obj
,
161 int numDataElements
= getNumDataElements();
162 int dataSize
= numDataElements
* size
;
166 switch (getTransferType())
168 case DataBuffer
.TYPE_BYTE
:
169 obj
= new byte[dataSize
];
171 case DataBuffer
.TYPE_USHORT
:
172 obj
= new short[dataSize
];
174 case DataBuffer
.TYPE_INT
:
175 obj
= new int[dataSize
];
178 // Seems like the only sensible thing to do.
179 throw new ClassCastException();
182 Object pixelData
= null;
184 for (int yy
= y
; yy
< (y
+ h
); yy
++)
186 for (int xx
= x
; xx
< (x
+ w
); xx
++)
188 pixelData
= getDataElements(xx
, yy
, pixelData
, data
);
189 System
.arraycopy(pixelData
, 0, obj
, outOffset
,
191 outOffset
+= numDataElements
;
197 public abstract void setDataElements(int x
, int y
, Object obj
,
200 public void setDataElements(int x
, int y
, int w
, int h
,
201 Object obj
, DataBuffer data
)
204 int numDataElements
= getNumDataElements();
205 int dataSize
= numDataElements
* size
;
208 switch (getTransferType())
210 case DataBuffer
.TYPE_BYTE
:
211 pixelData
= new byte[numDataElements
];
213 case DataBuffer
.TYPE_USHORT
:
214 pixelData
= new short[numDataElements
];
216 case DataBuffer
.TYPE_INT
:
217 pixelData
= new int[numDataElements
];
220 // Seems like the only sensible thing to do.
221 throw new ClassCastException();
225 for (int yy
= y
; yy
< (y
+ h
); yy
++)
227 for (int xx
= x
; xx
< (x
+ w
); xx
++)
229 System
.arraycopy(obj
, inOffset
, pixelData
, 0,
231 setDataElements(xx
, yy
, pixelData
, data
);
232 inOffset
+= numDataElements
;
237 public float[] getPixel(int x
, int y
, float[] fArray
, DataBuffer data
)
240 fArray
= new float[numBands
];
242 for (int b
= 0; b
< numBands
; b
++)
244 fArray
[b
] = getSampleFloat(x
, y
, b
, data
);
249 public double[] getPixel(int x
, int y
, double[] dArray
, DataBuffer data
) {
251 dArray
= new double[numBands
];
252 for (int b
= 0; b
< numBands
; b
++)
254 dArray
[b
] = getSampleDouble(x
, y
, b
, data
);
259 /* FIXME: Should it return a banded or pixel interleaved array of
260 samples? (Assume interleaved.) */
261 public int[] getPixels(int x
, int y
, int w
, int h
, int[] iArray
,
268 iArray
= new int[w
* h
* numBands
];
269 for (int yy
= y
; yy
< (y
+ h
); yy
++)
271 for (int xx
= x
; xx
< (x
+ w
); xx
++)
273 pixel
= getPixel(xx
, yy
, pixel
, data
);
274 System
.arraycopy(pixel
, 0, iArray
, outOffset
, numBands
);
275 outOffset
+= numBands
;
281 /* FIXME: Should it return a banded or pixel interleaved array of
282 samples? (Assume interleaved.) */
283 public float[] getPixels(int x
, int y
, int w
, int h
, float[] fArray
,
288 float[] pixel
= null;
289 if (fArray
== null) fArray
= new float[w
* h
* numBands
];
290 for (int yy
= y
; yy
< (y
+ h
); yy
++)
292 for (int xx
= x
; xx
< (x
+ w
); xx
++)
294 pixel
= getPixel(xx
, yy
, pixel
, data
);
295 System
.arraycopy(pixel
, 0, fArray
, outOffset
, numBands
);
296 outOffset
+= numBands
;
302 /* FIXME: Should it return a banded or pixel interleaved array of
303 samples? (Assume interleaved.) */
304 public double[] getPixels(int x
, int y
, int w
, int h
, double[] dArray
,
309 double[] pixel
= null;
311 dArray
= new double[w
* h
* numBands
];
312 for (int yy
= y
; yy
< (y
+ h
); yy
++)
314 for (int xx
= x
; xx
< (x
+ w
); xx
++)
316 pixel
= getPixel(xx
, yy
, pixel
, data
);
317 System
.arraycopy(pixel
, 0, dArray
, outOffset
, numBands
);
318 outOffset
+= numBands
;
324 public abstract int getSample(int x
, int y
, int b
, DataBuffer data
);
326 public float getSampleFloat(int x
, int y
, int b
, DataBuffer data
)
328 return getSample(x
, y
, b
, data
);
331 public double getSampleDouble(int x
, int y
, int b
, DataBuffer data
)
333 return getSampleFloat(x
, y
, b
, data
);
336 public int[] getSamples(int x
, int y
, int w
, int h
, int b
,
337 int[] iArray
, DataBuffer data
)
342 iArray
= new int[size
];
343 for (int yy
= y
; yy
< (y
+ h
); yy
++)
345 for (int xx
= x
; xx
< (x
+ w
); xx
++)
347 iArray
[outOffset
++] = getSample(xx
, yy
, b
, data
);
353 public float[] getSamples(int x
, int y
, int w
, int h
, int b
,
354 float[] fArray
, DataBuffer data
)
359 fArray
= new float[size
];
360 for (int yy
= y
; yy
< (y
+ h
); yy
++)
362 for (int xx
= x
; xx
< (x
+ w
); xx
++)
364 fArray
[outOffset
++] = getSampleFloat(xx
, yy
, b
, data
);
370 public double[] getSamples(int x
, int y
, int w
, int h
, int b
,
371 double[] dArray
, DataBuffer data
)
376 dArray
= new double[size
];
377 for (int yy
= y
; yy
< (y
+ h
); yy
++)
379 for (int xx
= x
; xx
< (x
+ w
); xx
++)
381 dArray
[outOffset
++] = getSampleDouble(xx
, yy
, b
, data
);
387 public void setPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
389 for (int b
= 0; b
< numBands
; b
++)
390 setSample(x
, y
, b
, iArray
[b
], data
);
393 public void setPixel(int x
, int y
, float[] fArray
, DataBuffer data
)
395 for (int b
= 0; b
< numBands
; b
++)
396 setSample(x
, y
, b
, fArray
[b
], data
);
399 public void setPixel(int x
, int y
, double[] dArray
, DataBuffer data
)
401 for (int b
= 0; b
< numBands
; b
++)
402 setSample(x
, y
, b
, dArray
[b
], data
);
405 public void setPixels(int x
, int y
, int w
, int h
, int[] iArray
,
409 int[] pixel
= new int[numBands
];
410 for (int yy
= y
; yy
< (y
+ h
); yy
++)
412 for (int xx
= x
; xx
< (x
+ w
); xx
++)
414 System
.arraycopy(iArray
, inOffset
, pixel
, 0, numBands
);
415 setPixel(xx
, yy
, pixel
, data
);
416 inOffset
+= numBands
;
421 public void setPixels(int x
, int y
, int w
, int h
, float[] fArray
,
425 float[] pixel
= new float[numBands
];
426 for (int yy
= y
; yy
< (y
+ h
); yy
++)
428 for (int xx
= x
; xx
< (x
+ w
); xx
++)
430 System
.arraycopy(fArray
, inOffset
, pixel
, 0, numBands
);
431 setPixel(xx
, yy
, pixel
, data
);
432 inOffset
+= numBands
;
437 public void setPixels(int x
, int y
, int w
, int h
, double[] dArray
,
441 double[] pixel
= new double[numBands
];
442 for (int yy
= y
; yy
< (y
+ h
); yy
++)
444 for (int xx
= x
; xx
< (x
+ w
); xx
++)
446 System
.arraycopy(dArray
, inOffset
, pixel
, 0, numBands
);
447 setPixel(xx
, yy
, pixel
, data
);
448 inOffset
+= numBands
;
453 public abstract void setSample(int x
, int y
, int b
, int s
,
456 public void setSample(int x
, int y
, int b
, float s
,
459 setSample(x
, y
, b
, (int) s
, data
);
462 public void setSample(int x
, int y
, int b
, double s
,
465 setSample(x
, y
, b
, (float) s
, data
);
468 public void setSamples(int x
, int y
, int w
, int h
, int b
,
469 int[] iArray
, DataBuffer data
)
473 for (int yy
= y
; yy
< (y
+ h
); yy
++)
474 for (int xx
= x
; xx
< (x
+ w
); xx
++)
475 setSample(xx
, yy
, b
, iArray
[inOffset
++], data
);
478 public void setSamples(int x
, int y
, int w
, int h
, int b
,
479 float[] fArray
, DataBuffer data
)
483 for (int yy
= y
; yy
< (y
+ h
); yy
++)
484 for (int xx
= x
; xx
< (x
+ w
); xx
++)
485 setSample(xx
, yy
, b
, fArray
[inOffset
++], data
);
489 public void setSamples(int x
, int y
, int w
, int h
, int b
,
490 double[] dArray
, DataBuffer data
) {
493 for (int yy
= y
; yy
< (y
+ h
); yy
++)
494 for (int xx
= x
; xx
< (x
+ w
); xx
++)
495 setSample(xx
, yy
, b
, dArray
[inOffset
++], data
);
498 public abstract SampleModel
createCompatibleSampleModel(int w
, int h
);
501 * Return a SampleModel with a subset of the bands in this model.
503 * Selects bands.length bands from this sample model. The bands chosen
504 * are specified in the indices of bands[]. This also permits permuting
505 * the bands as well as taking a subset. Thus, giving an array with
506 * 1, 2, 3, ..., numbands, will give an identical sample model.
508 * @param bands Array with band indices to include.
509 * @return A new sample model
511 public abstract SampleModel
createSubsetSampleModel(int[] bands
);
513 public abstract DataBuffer
createDataBuffer();
515 public abstract int[] getSampleSize();
517 public abstract int getSampleSize(int band
);