2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / image / SampleModel.java
blob219a871d42cdfc6082460f5af418d9c773475294
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)
8 any later version.
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
18 02111-1307 USA.
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
23 combination.
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;
39 /**
40 * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
42 public abstract class SampleModel
44 /** Width of image described. */
45 protected int width;
47 /** Height of image described. */
48 protected int height;
50 /** Number of bands in the image described. */
51 protected int numBands;
53 /**
54 * The DataBuffer type that is used to store the data of the image
55 * described.
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;
67 this.width = w;
68 this.height = h;
69 this.numBands = numBands;
72 public final int getWidth()
74 return width;
77 public final int getHeight()
79 return height;
82 public final int getNumBands()
84 return numBands;
87 public abstract int getNumDataElements();
89 public final int getDataType()
91 return dataType;
94 public int getTransferType()
96 // FIXME: Is this a reasonable default implementation?
97 return dataType;
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);
104 return iArray;
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,
121 DataBuffer data);
124 public Object getDataElements(int x, int y, int w, int h, Object obj,
125 DataBuffer data)
127 int size = w*h;
128 int numDataElements = getNumDataElements();
129 int dataSize = numDataElements*size;
131 if (obj == null)
133 switch (getTransferType())
135 case DataBuffer.TYPE_BYTE:
136 obj = new byte[dataSize];
137 break;
138 case DataBuffer.TYPE_USHORT:
139 obj = new short[dataSize];
140 break;
141 case DataBuffer.TYPE_INT:
142 obj = new int[dataSize];
143 break;
144 default:
145 // Seems like the only sensible thing to do.
146 throw new ClassCastException();
149 Object pixelData = null;
150 int outOffset = 0;
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,
157 numDataElements);
158 outOffset += numDataElements;
161 return obj;
164 public abstract void setDataElements(int x, int y, Object obj,
165 DataBuffer data);
167 public void setDataElements(int x, int y, int w, int h,
168 Object obj, DataBuffer data)
170 int size = w*h;
171 int numDataElements = getNumDataElements();
172 int dataSize = numDataElements*size;
174 Object pixelData;
175 switch (getTransferType())
177 case DataBuffer.TYPE_BYTE:
178 pixelData = new byte[numDataElements];
179 break;
180 case DataBuffer.TYPE_USHORT:
181 pixelData = new short[numDataElements];
182 break;
183 case DataBuffer.TYPE_INT:
184 pixelData = new int[numDataElements];
185 break;
186 default:
187 // Seems like the only sensible thing to do.
188 throw new ClassCastException();
190 int inOffset = 0;
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,
197 numDataElements);
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);
212 return fArray;
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);
221 return dArray;
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,
227 DataBuffer data)
229 int size = w*h;
230 int outOffset = 0;
231 int[] pixel = null;
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;
242 return iArray;
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,
248 DataBuffer data)
250 int size = w*h;
251 int outOffset = 0;
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;
263 return fArray;
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,
269 DataBuffer data)
271 int size = w*h;
272 int outOffset = 0;
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;
284 return dArray;
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)
302 int size = w*h;
303 int outOffset = 0;
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);
312 return iArray;
315 public float[] getSamples(int x, int y, int w, int h, int b,
316 float[] fArray, DataBuffer data)
318 int size = w*h;
319 int outOffset = 0;
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);
328 return fArray;
331 public double[] getSamples(int x, int y, int w, int h, int b,
332 double[] dArray, DataBuffer data)
334 int size = w*h;
335 int outOffset = 0;
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);
344 return dArray;
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,
363 DataBuffer data)
365 int inOffset = 0;
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,
379 DataBuffer data)
381 int inOffset = 0;
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,
395 DataBuffer data)
397 int inOffset = 0;
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,
411 DataBuffer data);
413 public void setSample(int x, int y, int b, float s,
414 DataBuffer data)
416 setSample(x, y, b, (int) s, data);
419 public void setSample(int x, int y, int b, double s,
420 DataBuffer data)
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)
428 int size = w*h;
429 int inOffset = 0;
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)
438 int size = w*h;
439 int inOffset = 0;
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) {
448 int size = w*h;
449 int inOffset = 0;
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);