source: trunk/gcc/libjava/java/awt/image/SampleModel.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.1 KB
Line 
1/* Copyright (C) 2000, 2001, 2002 Free Software Foundation
2
3This file is part of GNU Classpath.
4
5GNU Classpath is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10GNU Classpath is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Classpath; see the file COPYING. If not, write to the
17Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1802111-1307 USA.
19
20Linking this library statically or dynamically with other modules is
21making a combined work based on this library. Thus, the terms and
22conditions of the GNU General Public License cover the whole
23combination.
24
25As a special exception, the copyright holders of this library give you
26permission to link this library with independent modules to produce an
27executable, regardless of the license terms of these independent
28modules, and to copy and distribute the resulting executable under
29terms of your choice, provided that you also meet, for each linked
30independent module, the terms and conditions of the license of that
31module. An independent module is a module which is not derived from
32or based on this library. If you modify this library, you may extend
33this exception to your version of the library, but you are not
34obligated to do so. If you do not wish to do so, delete this
35exception statement from your version. */
36
37package java.awt.image;
38
39/**
40 * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
41 */
42public abstract class SampleModel
43{
44 /** Width of image described. */
45 protected int width;
46
47 /** Height of image described. */
48 protected int height;
49
50 /** Number of bands in the image described. */
51 protected int numBands;
52
53 /**
54 * The DataBuffer type that is used to store the data of the image
55 * described.
56 */
57 protected int dataType;
58
59 public SampleModel(int dataType, int w, int h, int numBands)
60 {
61 if ((w<=0) || (h<=0)) throw new IllegalArgumentException();
62
63 // FIXME: How can an int be greater than Integer.MAX_VALUE?
64 // FIXME: How do we identify an unsupported data type?
65
66 this.dataType = dataType;
67 this.width = w;
68 this.height = h;
69 this.numBands = numBands;
70 }
71
72 public final int getWidth()
73 {
74 return width;
75 }
76
77 public final int getHeight()
78 {
79 return height;
80 }
81
82 public final int getNumBands()
83 {
84 return numBands;
85 }
86
87 public abstract int getNumDataElements();
88
89 public final int getDataType()
90 {
91 return dataType;
92 }
93
94 public int getTransferType()
95 {
96 // FIXME: Is this a reasonable default implementation?
97 return dataType;
98 }
99
100 public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
101 {
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;
105 }
106
107 /**
108 *
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.
112 *
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.
115 *
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.
119 */
120 public abstract Object getDataElements(int x, int y, Object obj,
121 DataBuffer data);
122
123
124 public Object getDataElements(int x, int y, int w, int h, Object obj,
125 DataBuffer data)
126 {
127 int size = w*h;
128 int numDataElements = getNumDataElements();
129 int dataSize = numDataElements*size;
130
131 if (obj == null)
132 {
133 switch (getTransferType())
134 {
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();
147 }
148 }
149 Object pixelData = null;
150 int outOffset = 0;
151 for (int yy = y; yy<(y+h); yy++)
152 {
153 for (int xx = x; xx<(x+w); xx++)
154 {
155 pixelData = getDataElements(xx, yy, pixelData, data);
156 System.arraycopy(pixelData, 0, obj, outOffset,
157 numDataElements);
158 outOffset += numDataElements;
159 }
160 }
161 return obj;
162 }
163
164 public abstract void setDataElements(int x, int y, Object obj,
165 DataBuffer data);
166
167 public void setDataElements(int x, int y, int w, int h,
168 Object obj, DataBuffer data)
169 {
170 int size = w*h;
171 int numDataElements = getNumDataElements();
172 int dataSize = numDataElements*size;
173
174 Object pixelData;
175 switch (getTransferType())
176 {
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();
189 }
190 int inOffset = 0;
191
192 for (int yy=y; yy<(y+h); yy++)
193 {
194 for (int xx=x; xx<(x+w); xx++)
195 {
196 System.arraycopy(obj, inOffset, pixelData, 0,
197 numDataElements);
198 setDataElements(xx, yy, pixelData, data);
199 inOffset += numDataElements;
200 }
201 }
202 }
203
204 public float[] getPixel(int x, int y, float[] fArray, DataBuffer data)
205 {
206 if (fArray == null) fArray = new float[numBands];
207
208 for (int b=0; b<numBands; b++)
209 {
210 fArray[b] = getSampleFloat(x, y, b, data);
211 }
212 return fArray;
213 }
214
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++)
218 {
219 dArray[b] = getSampleDouble(x, y, b, data);
220 }
221 return dArray;
222 }
223
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)
228 {
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++)
234 {
235 for (int xx=x; xx<(x+w); xx++)
236 {
237 getPixel(xx, yy, pixel, data);
238 System.arraycopy(pixel, 0, iArray, outOffset, numBands);
239 outOffset += numBands;
240 }
241 }
242 return iArray;
243 }
244
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)
249 {
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++)
255 {
256 for (int xx=x; xx<(x+w); xx++)
257 {
258 getPixel(xx, yy, pixel, data);
259 System.arraycopy(pixel, 0, fArray, outOffset, numBands);
260 outOffset += numBands;
261 }
262 }
263 return fArray;
264 }
265
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)
270 {
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++)
276 {
277 for (int xx=x; xx<(x+w); xx++)
278 {
279 getPixel(xx, yy, pixel, data);
280 System.arraycopy(pixel, 0, dArray, outOffset, numBands);
281 outOffset += numBands;
282 }
283 }
284 return dArray;
285 }
286
287 public abstract int getSample(int x, int y, int b, DataBuffer data);
288
289 public float getSampleFloat(int x, int y, int b, DataBuffer data)
290 {
291 return getSample(x, y, b, data);
292 }
293
294 public double getSampleDouble(int x, int y, int b, DataBuffer data)
295 {
296 return getSampleFloat(x, y, b, data);
297 }
298
299 public int[] getSamples(int x, int y, int w, int h, int b,
300 int[] iArray, DataBuffer data)
301 {
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++)
306 {
307 for (int xx=x; xx<(x+w); xx++)
308 {
309 iArray[outOffset++] = getSample(xx, yy, b, data);
310 }
311 }
312 return iArray;
313 }
314
315 public float[] getSamples(int x, int y, int w, int h, int b,
316 float[] fArray, DataBuffer data)
317 {
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++)
322 {
323 for (int xx=x; xx<(x+w); xx++)
324 {
325 fArray[outOffset++] = getSampleFloat(xx, yy, b, data);
326 }
327 }
328 return fArray;
329 }
330
331 public double[] getSamples(int x, int y, int w, int h, int b,
332 double[] dArray, DataBuffer data)
333 {
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++)
338 {
339 for (int xx=x; xx<(x+w); xx++)
340 {
341 dArray[outOffset++] = getSampleDouble(xx, yy, b, data);
342 }
343 }
344 return dArray;
345 }
346
347 public void setPixel(int x, int y, int[] iArray, DataBuffer data)
348 {
349 for (int b=0; b<numBands; b++) setSample(x, y, b, iArray[b], data);
350 }
351
352 public void setPixel(int x, int y, float[] fArray, DataBuffer data)
353 {
354 for (int b=0; b<numBands; b++) setSample(x, y, b, fArray[b], data);
355 }
356
357 public void setPixel(int x, int y, double[] dArray, DataBuffer data)
358 {
359 for (int b=0; b<numBands; b++) setSample(x, y, b, dArray[b], data);
360 }
361
362 public void setPixels(int x, int y, int w, int h, int[] iArray,
363 DataBuffer data)
364 {
365 int inOffset = 0;
366 int[] pixel = new int[numBands];
367 for (int yy=y; yy<(y+h); yy++)
368 {
369 for (int xx=x; xx<(x+w); xx++)
370 {
371 System.arraycopy(iArray, inOffset, pixel, 0, numBands);
372 setPixel(xx, yy, pixel, data);
373 inOffset += numBands;
374 }
375 }
376 }
377
378 public void setPixels(int x, int y, int w, int h, float[] fArray,
379 DataBuffer data)
380 {
381 int inOffset = 0;
382 float[] pixel = new float[numBands];
383 for (int yy=y; yy<(y+h); yy++)
384 {
385 for (int xx=x; xx<(x+w); xx++)
386 {
387 System.arraycopy(fArray, inOffset, pixel, 0, numBands);
388 setPixel(xx, yy, pixel, data);
389 inOffset += numBands;
390 }
391 }
392 }
393
394 public void setPixels(int x, int y, int w, int h, double[] dArray,
395 DataBuffer data)
396 {
397 int inOffset = 0;
398 double[] pixel = new double[numBands];
399 for (int yy=y; yy<(y+h); yy++)
400 {
401 for (int xx=x; xx<(x+w); xx++)
402 {
403 System.arraycopy(dArray, inOffset, pixel, 0, numBands);
404 setPixel(xx, yy, pixel, data);
405 inOffset += numBands;
406 }
407 }
408 }
409
410 public abstract void setSample(int x, int y, int b, int s,
411 DataBuffer data);
412
413 public void setSample(int x, int y, int b, float s,
414 DataBuffer data)
415 {
416 setSample(x, y, b, (int) s, data);
417 }
418
419 public void setSample(int x, int y, int b, double s,
420 DataBuffer data)
421 {
422 setSample(x, y, b, (float) s, data);
423 }
424
425 public void setSamples(int x, int y, int w, int h, int b,
426 int[] iArray, DataBuffer data)
427 {
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);
433 }
434
435 public void setSamples(int x, int y, int w, int h, int b,
436 float[] fArray, DataBuffer data)
437 {
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);
443
444 }
445
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);
453 }
454
455 public abstract SampleModel createCompatibleSampleModel(int w, int h);
456
457 public abstract SampleModel createSubsetSampleModel(int[] bands);
458
459 public abstract DataBuffer createDataBuffer();
460
461 public abstract int[] getSampleSize();
462
463 public abstract int getSampleSize(int band);
464}
Note: See TracBrowser for help on using the repository browser.