Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / java / awt / image / SampleModel.java
blob1159662223c0e50b53480ab22aa4ce98e581d139
1 /* Copyright (C) 2000, 2001, 2002, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 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. Package-private here,
51 shadowed by ComponentSampleModel. */
52 protected int numBands;
54 /**
55 * The DataBuffer type that is used to store the data of the image
56 * described.
58 protected int dataType;
60 public SampleModel(int dataType, int w, int h, int numBands)
62 if ((w <= 0) || (h <= 0))
63 throw new IllegalArgumentException((w <= 0 ? " width<=0" : " width is ok")
64 +(h <= 0 ? " height<=0" : " height is ok"));
66 // FIXME: How can an int be greater than Integer.MAX_VALUE?
67 // FIXME: How do we identify an unsupported data type?
69 this.dataType = dataType;
70 this.width = w;
71 this.height = h;
72 this.numBands = numBands;
75 public final int getWidth()
77 return width;
80 public final int getHeight()
82 return height;
85 public final int getNumBands()
87 return numBands;
90 public abstract int getNumDataElements();
92 public final int getDataType()
94 return dataType;
97 public int getTransferType()
99 // FIXME: Is this a reasonable default implementation?
100 return dataType;
103 public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
105 if (iArray == null) iArray = new int[numBands];
106 for (int b=0; b<numBands; b++) iArray[b] = getSample(x, y, b, data);
107 return iArray;
112 * This method is provided as a faster alternative to getPixel(),
113 * that can be used when there is no need to decode the pixel into
114 * separate sample values.
116 * @param obj An array to return the pixel data in. If null, an
117 * array of the right type and size will be created.
119 * @return A single pixel as an array object of a primitive type,
120 * based on the transfer type. Eg. if transfer type is
121 * DataBuffer.TYPE_USHORT, then a short[] object is returned.
123 public abstract Object getDataElements(int x, int y, Object obj,
124 DataBuffer data);
127 public Object getDataElements(int x, int y, int w, int h, Object obj,
128 DataBuffer data)
130 int size = w*h;
131 int numDataElements = getNumDataElements();
132 int dataSize = numDataElements*size;
134 if (obj == null)
136 switch (getTransferType())
138 case DataBuffer.TYPE_BYTE:
139 obj = new byte[dataSize];
140 break;
141 case DataBuffer.TYPE_USHORT:
142 obj = new short[dataSize];
143 break;
144 case DataBuffer.TYPE_INT:
145 obj = new int[dataSize];
146 break;
147 default:
148 // Seems like the only sensible thing to do.
149 throw new ClassCastException();
152 Object pixelData = null;
153 int outOffset = 0;
154 for (int yy = y; yy<(y+h); yy++)
156 for (int xx = x; xx<(x+w); xx++)
158 pixelData = getDataElements(xx, yy, pixelData, data);
159 System.arraycopy(pixelData, 0, obj, outOffset,
160 numDataElements);
161 outOffset += numDataElements;
164 return obj;
167 public abstract void setDataElements(int x, int y, Object obj,
168 DataBuffer data);
170 public void setDataElements(int x, int y, int w, int h,
171 Object obj, DataBuffer data)
173 int size = w*h;
174 int numDataElements = getNumDataElements();
175 int dataSize = numDataElements*size;
177 Object pixelData;
178 switch (getTransferType())
180 case DataBuffer.TYPE_BYTE:
181 pixelData = new byte[numDataElements];
182 break;
183 case DataBuffer.TYPE_USHORT:
184 pixelData = new short[numDataElements];
185 break;
186 case DataBuffer.TYPE_INT:
187 pixelData = new int[numDataElements];
188 break;
189 default:
190 // Seems like the only sensible thing to do.
191 throw new ClassCastException();
193 int inOffset = 0;
195 for (int yy=y; yy<(y+h); yy++)
197 for (int xx=x; xx<(x+w); xx++)
199 System.arraycopy(obj, inOffset, pixelData, 0,
200 numDataElements);
201 setDataElements(xx, yy, pixelData, data);
202 inOffset += numDataElements;
207 public float[] getPixel(int x, int y, float[] fArray, DataBuffer data)
209 if (fArray == null) fArray = new float[numBands];
211 for (int b=0; b<numBands; b++)
213 fArray[b] = getSampleFloat(x, y, b, data);
215 return fArray;
218 public double[] getPixel(int x, int y, double[] dArray, DataBuffer data) {
219 if (dArray == null) dArray = new double[numBands];
220 for (int b=0; b<numBands; b++)
222 dArray[b] = getSampleDouble(x, y, b, data);
224 return dArray;
227 /* FIXME: Should it return a banded or pixel interleaved array of
228 samples? (Assume interleaved.) */
229 public int[] getPixels(int x, int y, int w, int h, int[] iArray,
230 DataBuffer data)
232 int size = w*h;
233 int outOffset = 0;
234 int[] pixel = null;
235 if (iArray == null) iArray = new int[w*h*numBands];
236 for (int yy=y; yy<(y+h); yy++)
238 for (int xx=x; xx<(x+w); xx++)
240 pixel = getPixel(xx, yy, pixel, data);
241 System.arraycopy(pixel, 0, iArray, outOffset, numBands);
242 outOffset += numBands;
245 return iArray;
248 /* FIXME: Should it return a banded or pixel interleaved array of
249 samples? (Assume interleaved.) */
250 public float[] getPixels(int x, int y, int w, int h, float[] fArray,
251 DataBuffer data)
253 int size = w*h;
254 int outOffset = 0;
255 float[] pixel = null;
256 if (fArray == null) fArray = new float[w*h*numBands];
257 for (int yy=y; yy<(y+h); yy++)
259 for (int xx=x; xx<(x+w); xx++)
261 pixel = getPixel(xx, yy, pixel, data);
262 System.arraycopy(pixel, 0, fArray, outOffset, numBands);
263 outOffset += numBands;
266 return fArray;
269 /* FIXME: Should it return a banded or pixel interleaved array of
270 samples? (Assume interleaved.) */
271 public double[] getPixels(int x, int y, int w, int h, double[] dArray,
272 DataBuffer data)
274 int size = w*h;
275 int outOffset = 0;
276 double[] pixel = null;
277 if (dArray == null) dArray = new double[w*h*numBands];
278 for (int yy=y; yy<(y+h); yy++)
280 for (int xx=x; xx<(x+w); xx++)
282 pixel = getPixel(xx, yy, pixel, data);
283 System.arraycopy(pixel, 0, dArray, outOffset, numBands);
284 outOffset += numBands;
287 return dArray;
290 public abstract int getSample(int x, int y, int b, DataBuffer data);
292 public float getSampleFloat(int x, int y, int b, DataBuffer data)
294 return getSample(x, y, b, data);
297 public double getSampleDouble(int x, int y, int b, DataBuffer data)
299 return getSampleFloat(x, y, b, data);
302 public int[] getSamples(int x, int y, int w, int h, int b,
303 int[] iArray, DataBuffer data)
305 int size = w*h;
306 int outOffset = 0;
307 if (iArray == null) iArray = new int[size];
308 for (int yy=y; yy<(y+h); yy++)
310 for (int xx=x; xx<(x+w); xx++)
312 iArray[outOffset++] = getSample(xx, yy, b, data);
315 return iArray;
318 public float[] getSamples(int x, int y, int w, int h, int b,
319 float[] fArray, DataBuffer data)
321 int size = w*h;
322 int outOffset = 0;
323 if (fArray == null) fArray = new float[size];
324 for (int yy=y; yy<(y+h); yy++)
326 for (int xx=x; xx<(x+w); xx++)
328 fArray[outOffset++] = getSampleFloat(xx, yy, b, data);
331 return fArray;
334 public double[] getSamples(int x, int y, int w, int h, int b,
335 double[] dArray, DataBuffer data)
337 int size = w*h;
338 int outOffset = 0;
339 if (dArray == null) dArray = new double[size];
340 for (int yy=y; yy<(y+h); yy++)
342 for (int xx=x; xx<(x+w); xx++)
344 dArray[outOffset++] = getSampleDouble(xx, yy, b, data);
347 return dArray;
350 public void setPixel(int x, int y, int[] iArray, DataBuffer data)
352 for (int b=0; b<numBands; b++) setSample(x, y, b, iArray[b], data);
355 public void setPixel(int x, int y, float[] fArray, DataBuffer data)
357 for (int b=0; b<numBands; b++) setSample(x, y, b, fArray[b], data);
360 public void setPixel(int x, int y, double[] dArray, DataBuffer data)
362 for (int b=0; b<numBands; b++) setSample(x, y, b, dArray[b], data);
365 public void setPixels(int x, int y, int w, int h, int[] iArray,
366 DataBuffer data)
368 int inOffset = 0;
369 int[] pixel = new int[numBands];
370 for (int yy=y; yy<(y+h); yy++)
372 for (int xx=x; xx<(x+w); xx++)
374 System.arraycopy(iArray, inOffset, pixel, 0, numBands);
375 setPixel(xx, yy, pixel, data);
376 inOffset += numBands;
381 public void setPixels(int x, int y, int w, int h, float[] fArray,
382 DataBuffer data)
384 int inOffset = 0;
385 float[] pixel = new float[numBands];
386 for (int yy=y; yy<(y+h); yy++)
388 for (int xx=x; xx<(x+w); xx++)
390 System.arraycopy(fArray, inOffset, pixel, 0, numBands);
391 setPixel(xx, yy, pixel, data);
392 inOffset += numBands;
397 public void setPixels(int x, int y, int w, int h, double[] dArray,
398 DataBuffer data)
400 int inOffset = 0;
401 double[] pixel = new double[numBands];
402 for (int yy=y; yy<(y+h); yy++)
404 for (int xx=x; xx<(x+w); xx++)
406 System.arraycopy(dArray, inOffset, pixel, 0, numBands);
407 setPixel(xx, yy, pixel, data);
408 inOffset += numBands;
413 public abstract void setSample(int x, int y, int b, int s,
414 DataBuffer data);
416 public void setSample(int x, int y, int b, float s,
417 DataBuffer data)
419 setSample(x, y, b, (int) s, data);
422 public void setSample(int x, int y, int b, double s,
423 DataBuffer data)
425 setSample(x, y, b, (float) s, data);
428 public void setSamples(int x, int y, int w, int h, int b,
429 int[] iArray, DataBuffer data)
431 int size = w*h;
432 int inOffset = 0;
433 for (int yy=y; yy<(y+h); yy++)
434 for (int xx=x; xx<(x+w); xx++)
435 setSample(xx, yy, b, iArray[inOffset++], data);
438 public void setSamples(int x, int y, int w, int h, int b,
439 float[] fArray, DataBuffer data)
441 int size = w*h;
442 int inOffset = 0;
443 for (int yy=y; yy<(y+h); yy++)
444 for (int xx=x; xx<(x+w); xx++)
445 setSample(xx, yy, b, fArray[inOffset++], data);
449 public void setSamples(int x, int y, int w, int h, int b,
450 double[] dArray, DataBuffer data) {
451 int size = w*h;
452 int inOffset = 0;
453 for (int yy=y; yy<(y+h); yy++)
454 for (int xx=x; xx<(x+w); xx++)
455 setSample(xx, yy, b, dArray[inOffset++], data);
458 public abstract SampleModel createCompatibleSampleModel(int w, int h);
461 * Return a SampleModel with a subset of the bands in this model.
463 * Selects bands.length bands from this sample model. The bands chosen
464 * are specified in the indices of bands[]. This also permits permuting
465 * the bands as well as taking a subset. Thus, giving an array with
466 * 1, 2, 3, ..., numbands, will give an identical sample model.
468 * @param bands Array with band indices to include.
469 * @return A new sample model
471 public abstract SampleModel createSubsetSampleModel(int[] bands);
473 public abstract DataBuffer createDataBuffer();
475 public abstract int[] getSampleSize();
477 public abstract int getSampleSize(int band);