1 /* Copyright (C) 2000, 2002, 2003  Free Software Foundation
2 
3 This file is part of GNU Classpath.
4 
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.
9 
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.
14 
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.
19 
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.
24 
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. */
36 
37 
38 package java.awt.image;
39 
40 import java.awt.Point;
41 import java.awt.Rectangle;
42 
43 /**
44  * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
45  */
46 public class Raster
47 {
48   protected SampleModel sampleModel;
49   protected DataBuffer dataBuffer;
50   protected int minX;
51   protected int minY;
52   protected int width;
53   protected int height;
54   protected int sampleModelTranslateX;
55   protected int sampleModelTranslateY;
56   protected int numBands;
57   protected int numDataElements;
58   protected Raster parent;
59 
Raster(SampleModel sampleModel, Point origin)60   protected Raster(SampleModel sampleModel, Point origin)
61   {
62     this(sampleModel, sampleModel.createDataBuffer(), origin);
63   }
64 
Raster(SampleModel sampleModel, DataBuffer dataBuffer, Point origin)65   protected Raster(SampleModel sampleModel, DataBuffer dataBuffer,
66 		   Point origin)
67   {
68     this(sampleModel, dataBuffer,
69 	 new Rectangle(origin.x, origin.y,
70 		       sampleModel.getWidth(), sampleModel.getHeight()),
71 	 origin, null);
72   }
73 
Raster(SampleModel sampleModel, DataBuffer dataBuffer, Rectangle aRegion, Point sampleModelTranslate, Raster parent)74   protected Raster(SampleModel sampleModel, DataBuffer dataBuffer,
75 		   Rectangle aRegion,
76 		   Point sampleModelTranslate, Raster parent)
77   {
78     this.sampleModel = sampleModel;
79     this.dataBuffer = dataBuffer;
80     this.minX = aRegion.x;
81     this.minY = aRegion.y;
82     this.width = aRegion.width;
83     this.height = aRegion.height;
84 
85     // If sampleModelTranslate is null, use (0,0).  Methods such as
86     // Raster.createRaster are specified to allow for a null argument.
87     if (sampleModelTranslate != null)
88     {
89       this.sampleModelTranslateX = sampleModelTranslate.x;
90       this.sampleModelTranslateY = sampleModelTranslate.y;
91     }
92 
93     this.numBands = sampleModel.getNumBands();
94     this.numDataElements = sampleModel.getNumDataElements();
95     this.parent = parent;
96   }
97 
createInterleavedRaster(int dataType, int w, int h, int bands, Point location)98   public static WritableRaster createInterleavedRaster(int dataType,
99 						       int w, int h,
100 						       int bands,
101 						       Point location)
102   {
103     int[] bandOffsets = new int[bands];
104     // TODO: Maybe not generate this every time.
105     for (int b=0; b<bands; b++) bandOffsets[b] = b;
106 
107     int scanlineStride = bands*w;
108     return createInterleavedRaster(dataType, w, h, scanlineStride, bands,
109 				   bandOffsets, location);
110   }
111 
createInterleavedRaster(int dataType, int w, int h, int scanlineStride, int pixelStride, int[] bandOffsets, Point location)112   public static WritableRaster createInterleavedRaster(int dataType,
113 						       int w, int h,
114 						       int scanlineStride,
115 						       int pixelStride,
116 						       int[] bandOffsets,
117 						       Point location)
118   {
119     SampleModel sm = new ComponentSampleModel(dataType,
120 					      w, h,
121 					      pixelStride,
122 					      scanlineStride,
123 					      bandOffsets);
124     return createWritableRaster(sm, location);
125   }
126 
createBandedRaster(int dataType, int w, int h, int bands, Point location)127   public static WritableRaster createBandedRaster(int dataType,
128 						  int w, int h, int bands,
129 						  Point location)
130   {
131     // FIXME: Implement;
132     throw new UnsupportedOperationException("not implemented yet");
133   }
134 
createBandedRaster(int dataType, int w, int h, int scanlineStride, int[] bankIndices, int[] bandOffsets, Point location)135   public static WritableRaster createBandedRaster(int dataType,
136 						  int w, int h,
137 						  int scanlineStride,
138 						  int[] bankIndices,
139 						  int[] bandOffsets,
140 						  Point location)
141   {
142     // FIXME: Implement;
143     throw new UnsupportedOperationException("not implemented yet");
144   }
145 
createPackedRaster(int dataType, int w, int h, int[] bandMasks, Point location)146   public static WritableRaster createPackedRaster(int dataType,
147 						  int w, int h,
148 						  int[] bandMasks,
149 						  Point location)
150   {
151     SampleModel sm = new SinglePixelPackedSampleModel(dataType,
152 						      w, h,
153 						      bandMasks);
154     return createWritableRaster(sm, location);
155   }
156 
157   public static WritableRaster
createInterleavedRaster(DataBuffer dataBuffer, int w, int h, int scanlineStride, int pixelStride, int[] bandOffsets, Point location)158   createInterleavedRaster(DataBuffer dataBuffer, int w, int h,
159 			  int scanlineStride, int pixelStride,
160 			  int[] bandOffsets, Point location)
161   {
162     SampleModel sm = new ComponentSampleModel(dataBuffer.getDataType(),
163 					      w, h,
164 					      scanlineStride,
165 					      pixelStride,
166 					      bandOffsets);
167     return createWritableRaster(sm, dataBuffer, location);
168   }
169 
170   public static
createBandedRaster(DataBuffer dataBuffer, int w, int h, int scanlineStride, int[] bankIndices, int[] bandOffsets, Point location)171   WritableRaster createBandedRaster(DataBuffer dataBuffer,
172 				    int w, int h,
173 				    int scanlineStride,
174 				    int[] bankIndices,
175 				    int[] bandOffsets,
176 				    Point location)
177   {
178     // FIXME: Implement;
179     throw new UnsupportedOperationException("not implemented yet");
180   }
181 
182   public static WritableRaster
createPackedRaster(DataBuffer dataBuffer, int w, int h, int scanlineStride, int[] bandMasks, Point location)183   createPackedRaster(DataBuffer dataBuffer,
184 		     int w, int h,
185 		     int scanlineStride,
186 		     int[] bandMasks,
187 		     Point location) {
188     SampleModel sm =
189       new SinglePixelPackedSampleModel(dataBuffer.getDataType(),
190 				       w, h,
191 				       scanlineStride,
192 				       bandMasks);
193     return createWritableRaster(sm, dataBuffer, location);
194   }
195 
createRaster(SampleModel sm, DataBuffer db, Point location)196   public static Raster createRaster(SampleModel sm, DataBuffer db,
197 				    Point location)
198   {
199     return new Raster(sm, db, location);
200   }
201 
createWritableRaster(SampleModel sm, Point location)202   public static WritableRaster createWritableRaster(SampleModel sm,
203 						    Point location)
204   {
205     return new WritableRaster(sm, location);
206   }
207 
createWritableRaster(SampleModel sm, DataBuffer db, Point location)208   public static WritableRaster createWritableRaster(SampleModel sm,
209 						    DataBuffer db,
210 						    Point location)
211   {
212     return new WritableRaster(sm, db, location);
213   }
214 
getParent()215   public Raster getParent()
216   {
217     return parent;
218   }
219 
getSampleModelTranslateX()220   public final int getSampleModelTranslateX()
221   {
222     return sampleModelTranslateX;
223   }
224 
getSampleModelTranslateY()225   public final int getSampleModelTranslateY()
226   {
227     return sampleModelTranslateY;
228   }
229 
createCompatibleWritableRaster()230   public WritableRaster createCompatibleWritableRaster()
231   {
232     return new WritableRaster(getSampleModel(), new Point(minX, minY));
233   }
234 
createCompatibleWritableRaster(int w, int h)235   public WritableRaster createCompatibleWritableRaster(int w, int h)
236   {
237     return createCompatibleWritableRaster(minX, minY, w, h);
238   }
239 
createCompatibleWritableRaster(Rectangle rect)240   public WritableRaster createCompatibleWritableRaster(Rectangle rect)
241   {
242     return createCompatibleWritableRaster(rect.x, rect.y,
243 					  rect.width, rect.height);
244   }
245 
createCompatibleWritableRaster(int x, int y, int w, int h)246   public WritableRaster createCompatibleWritableRaster(int x, int y,
247 						       int w, int h)
248   {
249     SampleModel sm = getSampleModel().createCompatibleSampleModel(w, h);
250     return new WritableRaster(sm, sm.createDataBuffer(),
251 			      new Point(x, y));
252   }
253 
createTranslatedChild(int childMinX, int childMinY)254   public Raster createTranslatedChild(int childMinX, int childMinY) {
255     int tcx = sampleModelTranslateX - minX + childMinX;
256     int tcy = sampleModelTranslateY - minY + childMinY;
257 
258     return new Raster(sampleModel, dataBuffer,
259 		      new Rectangle(childMinX, childMinY,
260 				    width, height),
261 		      new Point(tcx, tcy),
262 		      this);
263   }
264 
createChild(int parentX, int parentY, int width, int height, int childMinX, int childMinY, int[] bandList)265   public Raster createChild(int parentX, int parentY, int width,
266 			    int height, int childMinX, int childMinY,
267 			    int[] bandList)
268   {
269     /* FIXME: Throw RasterFormatException if child bounds extends
270        beyond the bounds of this raster. */
271 
272     SampleModel sm = (bandList == null) ?
273       sampleModel :
274       sampleModel.createSubsetSampleModel(bandList);
275 
276     /*
277         data origin
278        /
279       +-------------------------
280       |\. __ parent trans
281       | \`.
282       |  \ `.    parent origin
283       |   \  `. /
284       |   /\   +-------- - -
285       |trans\ /<\-- deltaTrans
286       |child +-+-\---- - -
287       |     /|`|  \__ parent [x, y]
288       |child | |`. \
289       |origin| :  `.\
290       |      |    / `\
291       |      :   /    +
292       | child [x, y]
293 
294       parent_xy - parent_trans = child_xy - child_trans
295 
296       child_trans = parent_trans + child_xy - parent_xy
297     */
298 
299     return new Raster(sm, dataBuffer,
300 		      new Rectangle(childMinX, childMinY,
301 				    width, height),
302 		      new Point(sampleModelTranslateX+childMinX-parentX,
303 				sampleModelTranslateY+childMinY-parentY),
304 		      this);
305   }
306 
getBounds()307   public Rectangle getBounds()
308   {
309     return new Rectangle(minX, minY, width, height);
310   }
311 
getMinX()312   public final int getMinX()
313   {
314     return minX;
315   }
316 
getMinY()317   public final int getMinY()
318   {
319     return minY;
320   }
321 
getWidth()322   public final int getWidth()
323   {
324     return width;
325   }
326 
getHeight()327   public final int getHeight()
328   {
329     return height;
330   }
331 
getNumDataElements()332   public final int getNumDataElements()
333   {
334     return numDataElements;
335   }
336 
getTransferType()337   public final int getTransferType()
338   {
339     return sampleModel.getTransferType();
340   }
341 
getDataBuffer()342   public DataBuffer getDataBuffer()
343   {
344     return dataBuffer;
345   }
346 
getSampleModel()347   public SampleModel getSampleModel()
348   {
349     return sampleModel;
350   }
351 
getDataElements(int x, int y, Object outData)352   public Object getDataElements(int x, int y, Object outData)
353   {
354     return sampleModel.getDataElements(x-sampleModelTranslateX,
355 				       y-sampleModelTranslateY,
356 				       outData, dataBuffer);
357   }
358 
getDataElements(int x, int y, int w, int h, Object outData)359   public Object getDataElements(int x, int y, int w, int h,
360 				Object outData)
361   {
362     return sampleModel.getDataElements(x-sampleModelTranslateX,
363 				       y-sampleModelTranslateY,
364 				       w, h, outData, dataBuffer);
365   }
366 
getPixel(int x, int y, int[] iArray)367   public int[] getPixel(int x, int y, int[] iArray)
368   {
369     return sampleModel.getPixel(x-sampleModelTranslateX,
370 				y-sampleModelTranslateY,
371 				iArray, dataBuffer);
372   }
373 
getPixel(int x, int y, float[] fArray)374   public float[] getPixel(int x, int y, float[] fArray)
375   {
376     return sampleModel.getPixel(x-sampleModelTranslateX,
377 				y-sampleModelTranslateY,
378 				fArray, dataBuffer);
379   }
380 
getPixel(int x, int y, double[] dArray)381   public double[] getPixel(int x, int y, double[] dArray)
382   {
383     return sampleModel.getPixel(x-sampleModelTranslateX,
384 				y-sampleModelTranslateY,
385 				dArray, dataBuffer);
386   }
387 
getPixels(int x, int y, int w, int h, int[] iArray)388   public int[] getPixels(int x, int y, int w, int h, int[] iArray)
389   {
390     return sampleModel.getPixels(x-sampleModelTranslateX,
391 				 y-sampleModelTranslateY,
392 				 w, h, iArray, dataBuffer);
393   }
394 
getPixels(int x, int y, int w, int h, float[] fArray)395   public float[] getPixels(int x, int y, int w, int h,
396 			   float[] fArray)
397   {
398     return sampleModel.getPixels(x-sampleModelTranslateX,
399 				 y-sampleModelTranslateY,
400 				 w, h, fArray, dataBuffer);
401   }
402 
getPixels(int x, int y, int w, int h, double[] dArray)403   public double[] getPixels(int x, int y, int w, int h,
404 			    double[] dArray)
405   {
406     return sampleModel.getPixels(x-sampleModelTranslateX,
407 				 y-sampleModelTranslateY,
408 				 w, h, dArray, dataBuffer);
409   }
410 
getSample(int x, int y, int b)411   public int getSample(int x, int y, int b)
412   {
413     return sampleModel.getSample(x-sampleModelTranslateX,
414 				 y-sampleModelTranslateY,
415 				 b, dataBuffer);
416   }
417 
getSampleFloat(int x, int y, int b)418   public float getSampleFloat(int x, int y, int b)
419   {
420     return sampleModel.getSampleFloat(x-sampleModelTranslateX,
421 				      y-sampleModelTranslateY,
422 				      b, dataBuffer);
423   }
424 
getSampleDouble(int x, int y, int b)425   public double getSampleDouble(int x, int y, int b)
426   {
427     return sampleModel.getSampleDouble(x-sampleModelTranslateX,
428 				       y-sampleModelTranslateY,
429 				       b, dataBuffer);
430   }
431 
getSamples(int x, int y, int w, int h, int b, int[] iArray)432   public int[] getSamples(int x, int y, int w, int h, int b,
433 			  int[] iArray)
434   {
435     return sampleModel.getSamples(x-sampleModelTranslateX,
436 				  y-sampleModelTranslateY,
437 				  w, h, b, iArray, dataBuffer);
438   }
439 
getSamples(int x, int y, int w, int h, int b, float[] fArray)440   public float[] getSamples(int x, int y, int w, int h, int b,
441 			    float[] fArray)
442   {
443     return sampleModel.getSamples(x-sampleModelTranslateX,
444 				  y-sampleModelTranslateY,
445 				  w, h, b, fArray, dataBuffer);
446   }
447 
getSamples(int x, int y, int w, int h, int b, double[] dArray)448   public double[] getSamples(int x, int y, int w, int h, int b,
449 			     double[] dArray)
450   {
451     return sampleModel.getSamples(x-sampleModelTranslateX,
452 				  y-sampleModelTranslateY,
453 				  w, h, b, dArray, dataBuffer);
454   }
455 }
456