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))
62 throw new IllegalArgumentException((w
<= 0 ?
" width<=0" : " width is ok")
63 +(h
<= 0 ?
" height<=0" : " height is ok"));
65 // FIXME: How can an int be greater than Integer.MAX_VALUE?
66 // FIXME: How do we identify an unsupported data type?
68 this.dataType
= dataType
;
71 this.numBands
= numBands
;
74 public final int getWidth()
79 public final int getHeight()
84 public final int getNumBands()
89 public abstract int getNumDataElements();
91 public final int getDataType()
96 public int getTransferType()
98 // FIXME: Is this a reasonable default implementation?
102 public int[] getPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
104 if (iArray
== null) iArray
= new int[numBands
];
105 for (int b
=0; b
<numBands
; b
++) iArray
[b
] = getSample(x
, y
, b
, data
);
111 * This method is provided as a faster alternative to getPixel(),
112 * that can be used when there is no need to decode the pixel into
113 * separate sample values.
115 * @param obj An array to return the pixel data in. If null, an
116 * array of the right type and size will be created.
118 * @return A single pixel as an array object of a primitive type,
119 * based on the transfer type. Eg. if transfer type is
120 * DataBuffer.TYPE_USHORT, then a short[] object is returned.
122 public abstract Object
getDataElements(int x
, int y
, Object obj
,
126 public Object
getDataElements(int x
, int y
, int w
, int h
, Object obj
,
130 int numDataElements
= getNumDataElements();
131 int dataSize
= numDataElements
*size
;
135 switch (getTransferType())
137 case DataBuffer
.TYPE_BYTE
:
138 obj
= new byte[dataSize
];
140 case DataBuffer
.TYPE_USHORT
:
141 obj
= new short[dataSize
];
143 case DataBuffer
.TYPE_INT
:
144 obj
= new int[dataSize
];
147 // Seems like the only sensible thing to do.
148 throw new ClassCastException();
151 Object pixelData
= null;
153 for (int yy
= y
; yy
<(y
+h
); yy
++)
155 for (int xx
= x
; xx
<(x
+w
); xx
++)
157 pixelData
= getDataElements(xx
, yy
, pixelData
, data
);
158 System
.arraycopy(pixelData
, 0, obj
, outOffset
,
160 outOffset
+= numDataElements
;
166 public abstract void setDataElements(int x
, int y
, Object obj
,
169 public void setDataElements(int x
, int y
, int w
, int h
,
170 Object obj
, DataBuffer data
)
173 int numDataElements
= getNumDataElements();
174 int dataSize
= numDataElements
*size
;
177 switch (getTransferType())
179 case DataBuffer
.TYPE_BYTE
:
180 pixelData
= new byte[numDataElements
];
182 case DataBuffer
.TYPE_USHORT
:
183 pixelData
= new short[numDataElements
];
185 case DataBuffer
.TYPE_INT
:
186 pixelData
= new int[numDataElements
];
189 // Seems like the only sensible thing to do.
190 throw new ClassCastException();
194 for (int yy
=y
; yy
<(y
+h
); yy
++)
196 for (int xx
=x
; xx
<(x
+w
); xx
++)
198 System
.arraycopy(obj
, inOffset
, pixelData
, 0,
200 setDataElements(xx
, yy
, pixelData
, data
);
201 inOffset
+= numDataElements
;
206 public float[] getPixel(int x
, int y
, float[] fArray
, DataBuffer data
)
208 if (fArray
== null) fArray
= new float[numBands
];
210 for (int b
=0; b
<numBands
; b
++)
212 fArray
[b
] = getSampleFloat(x
, y
, b
, data
);
217 public double[] getPixel(int x
, int y
, double[] dArray
, DataBuffer data
) {
218 if (dArray
== null) dArray
= new double[numBands
];
219 for (int b
=0; b
<numBands
; b
++)
221 dArray
[b
] = getSampleDouble(x
, y
, b
, data
);
226 /* FIXME: Should it return a banded or pixel interleaved array of
227 samples? (Assume interleaved.) */
228 public int[] getPixels(int x
, int y
, int w
, int h
, int[] iArray
,
234 if (iArray
== null) iArray
= new int[w
*h
*numBands
];
235 for (int yy
=y
; yy
<(y
+h
); yy
++)
237 for (int xx
=x
; xx
<(x
+w
); xx
++)
239 getPixel(xx
, yy
, pixel
, data
);
240 System
.arraycopy(pixel
, 0, iArray
, outOffset
, numBands
);
241 outOffset
+= numBands
;
247 /* FIXME: Should it return a banded or pixel interleaved array of
248 samples? (Assume interleaved.) */
249 public float[] getPixels(int x
, int y
, int w
, int h
, float[] fArray
,
254 float[] pixel
= null;
255 if (fArray
== null) fArray
= new float[w
*h
*numBands
];
256 for (int yy
=y
; yy
<(y
+h
); yy
++)
258 for (int xx
=x
; xx
<(x
+w
); xx
++)
260 getPixel(xx
, yy
, pixel
, data
);
261 System
.arraycopy(pixel
, 0, fArray
, outOffset
, numBands
);
262 outOffset
+= numBands
;
268 /* FIXME: Should it return a banded or pixel interleaved array of
269 samples? (Assume interleaved.) */
270 public double[] getPixels(int x
, int y
, int w
, int h
, double[] dArray
,
275 double[] pixel
= null;
276 if (dArray
== null) dArray
= new double[w
*h
*numBands
];
277 for (int yy
=y
; yy
<(y
+h
); yy
++)
279 for (int xx
=x
; xx
<(x
+w
); xx
++)
281 getPixel(xx
, yy
, pixel
, data
);
282 System
.arraycopy(pixel
, 0, dArray
, outOffset
, numBands
);
283 outOffset
+= numBands
;
289 public abstract int getSample(int x
, int y
, int b
, DataBuffer data
);
291 public float getSampleFloat(int x
, int y
, int b
, DataBuffer data
)
293 return getSample(x
, y
, b
, data
);
296 public double getSampleDouble(int x
, int y
, int b
, DataBuffer data
)
298 return getSampleFloat(x
, y
, b
, data
);
301 public int[] getSamples(int x
, int y
, int w
, int h
, int b
,
302 int[] iArray
, DataBuffer data
)
306 if (iArray
== null) iArray
= new int[size
];
307 for (int yy
=y
; yy
<(y
+h
); yy
++)
309 for (int xx
=x
; xx
<(x
+w
); xx
++)
311 iArray
[outOffset
++] = getSample(xx
, yy
, b
, data
);
317 public float[] getSamples(int x
, int y
, int w
, int h
, int b
,
318 float[] fArray
, DataBuffer data
)
322 if (fArray
== null) fArray
= new float[size
];
323 for (int yy
=y
; yy
<(y
+h
); yy
++)
325 for (int xx
=x
; xx
<(x
+w
); xx
++)
327 fArray
[outOffset
++] = getSampleFloat(xx
, yy
, b
, data
);
333 public double[] getSamples(int x
, int y
, int w
, int h
, int b
,
334 double[] dArray
, DataBuffer data
)
338 if (dArray
== null) dArray
= new double[size
];
339 for (int yy
=y
; yy
<(y
+h
); yy
++)
341 for (int xx
=x
; xx
<(x
+w
); xx
++)
343 dArray
[outOffset
++] = getSampleDouble(xx
, yy
, b
, data
);
349 public void setPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
351 for (int b
=0; b
<numBands
; b
++) setSample(x
, y
, b
, iArray
[b
], data
);
354 public void setPixel(int x
, int y
, float[] fArray
, DataBuffer data
)
356 for (int b
=0; b
<numBands
; b
++) setSample(x
, y
, b
, fArray
[b
], data
);
359 public void setPixel(int x
, int y
, double[] dArray
, DataBuffer data
)
361 for (int b
=0; b
<numBands
; b
++) setSample(x
, y
, b
, dArray
[b
], data
);
364 public void setPixels(int x
, int y
, int w
, int h
, int[] iArray
,
368 int[] pixel
= new int[numBands
];
369 for (int yy
=y
; yy
<(y
+h
); yy
++)
371 for (int xx
=x
; xx
<(x
+w
); xx
++)
373 System
.arraycopy(iArray
, inOffset
, pixel
, 0, numBands
);
374 setPixel(xx
, yy
, pixel
, data
);
375 inOffset
+= numBands
;
380 public void setPixels(int x
, int y
, int w
, int h
, float[] fArray
,
384 float[] pixel
= new float[numBands
];
385 for (int yy
=y
; yy
<(y
+h
); yy
++)
387 for (int xx
=x
; xx
<(x
+w
); xx
++)
389 System
.arraycopy(fArray
, inOffset
, pixel
, 0, numBands
);
390 setPixel(xx
, yy
, pixel
, data
);
391 inOffset
+= numBands
;
396 public void setPixels(int x
, int y
, int w
, int h
, double[] dArray
,
400 double[] pixel
= new double[numBands
];
401 for (int yy
=y
; yy
<(y
+h
); yy
++)
403 for (int xx
=x
; xx
<(x
+w
); xx
++)
405 System
.arraycopy(dArray
, inOffset
, pixel
, 0, numBands
);
406 setPixel(xx
, yy
, pixel
, data
);
407 inOffset
+= numBands
;
412 public abstract void setSample(int x
, int y
, int b
, int s
,
415 public void setSample(int x
, int y
, int b
, float s
,
418 setSample(x
, y
, b
, (int) s
, data
);
421 public void setSample(int x
, int y
, int b
, double s
,
424 setSample(x
, y
, b
, (float) s
, data
);
427 public void setSamples(int x
, int y
, int w
, int h
, int b
,
428 int[] iArray
, DataBuffer data
)
432 for (int yy
=y
; yy
<(y
+h
); yy
++)
433 for (int xx
=x
; xx
<(x
+w
); xx
++)
434 setSample(xx
, yy
, b
, iArray
[inOffset
++], data
);
437 public void setSamples(int x
, int y
, int w
, int h
, int b
,
438 float[] fArray
, DataBuffer data
)
442 for (int yy
=y
; yy
<(y
+h
); yy
++)
443 for (int xx
=x
; xx
<(x
+w
); xx
++)
444 setSample(xx
, yy
, b
, fArray
[inOffset
++], data
);
448 public void setSamples(int x
, int y
, int w
, int h
, int b
,
449 double[] dArray
, DataBuffer data
) {
452 for (int yy
=y
; yy
<(y
+h
); yy
++)
453 for (int xx
=x
; xx
<(x
+w
); xx
++)
454 setSample(xx
, yy
, b
, dArray
[inOffset
++], data
);
457 public abstract SampleModel
createCompatibleSampleModel(int w
, int h
);
460 * Return a SampleModel with a subset of the bands in this model.
462 * Selects bands.length bands from this sample model. The bands chosen
463 * are specified in the indices of bands[]. This also permits permuting
464 * the bands as well as taking a subset. Thus, giving an array with
465 * 1, 2, 3, ..., numbands, will give an identical sample model.
467 * @param bands Array with band indices to include.
468 * @return A new sample model
470 public abstract SampleModel
createSubsetSampleModel(int[] bands
);
472 public abstract DataBuffer
createDataBuffer();
474 public abstract int[] getSampleSize();
476 public abstract int getSampleSize(int band
);