1 |
/* MemoryImageSource.java -- Java class for providing image data |
/* MemoryImageSource.java -- Java class for providing image data |
2 |
Copyright (C) 1999, 2004 Free Software Foundation, Inc. |
Copyright (C) 1999, 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
41 |
import java.util.Hashtable; |
import java.util.Hashtable; |
42 |
import java.util.Vector; |
import java.util.Vector; |
43 |
|
|
44 |
public class MemoryImageSource implements ImageProducer |
public class MemoryImageSource implements ImageProducer |
45 |
{ |
{ |
46 |
private boolean animated = false; |
private boolean animated = false; |
47 |
private boolean fullbuffers = false; |
private boolean fullbuffers = false; |
48 |
private int pixeli[], width, height, offset, scansize; |
private int[] pixeli; |
49 |
private byte pixelb[]; |
private int width; |
50 |
private ColorModel cm; |
private int height; |
51 |
private Hashtable props = new Hashtable(); |
private int offset; |
52 |
private Vector consumers = new Vector(); |
private int scansize; |
53 |
|
private byte[] pixelb; |
54 |
/** |
private ColorModel cm; |
55 |
Constructs an ImageProducer from memory |
private Hashtable props = new Hashtable(); |
56 |
*/ |
private Vector consumers = new Vector(); |
57 |
public MemoryImageSource(int w, int h, ColorModel cm, |
|
58 |
byte pix[], int off, int scan) |
/** |
59 |
{ |
* Construct an image producer that reads image data from a byte |
60 |
this ( w, h, cm, pix, off, scan, null ); |
* array. |
61 |
} |
* |
62 |
/** |
* @param w width of image |
63 |
Constructs an ImageProducer from memory |
* @param h height of image |
64 |
*/ |
* @param cm the color model used to represent pixel values |
65 |
public MemoryImageSource( int w, int h, ColorModel cm, |
* @param pix a byte array of pixel values |
66 |
byte pix[], int off, int scan, |
* @param off the offset into the array at which the first pixel is stored |
67 |
Hashtable props) |
* @param scan the number of array elements that represents a single pixel row |
68 |
{ |
*/ |
69 |
width = w; |
public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, |
70 |
height = h; |
int scan) |
71 |
this.cm = cm; |
{ |
72 |
offset = off; |
this(w, h, cm, pix, off, scan, null); |
73 |
scansize = scan; |
} |
74 |
this.props = props; |
|
75 |
int max = (( scansize > width ) ? scansize : width ); |
/** |
76 |
pixelb = new byte[ max * height ]; |
* Constructs an ImageProducer from memory |
77 |
System.arraycopy( pix, 0, pixelb, 0, max * height ); |
*/ |
78 |
} |
public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, |
79 |
/** |
int scan, Hashtable props) |
80 |
Constructs an ImageProducer from memory |
{ |
81 |
*/ |
width = w; |
82 |
public MemoryImageSource(int w, int h, ColorModel cm, |
height = h; |
83 |
int pix[], int off, int scan) |
this.cm = cm; |
84 |
{ |
offset = off; |
85 |
this ( w, h, cm, pix, off, scan, null ); |
scansize = scan; |
86 |
} |
this.props = props; |
87 |
|
int max = ((scansize > width) ? scansize : width); |
88 |
/** |
pixelb = pix; |
89 |
Constructs an ImageProducer from memory |
} |
90 |
*/ |
|
91 |
public MemoryImageSource(int w, int h, ColorModel cm, |
/** |
92 |
int pix[], int off, int scan, |
* Construct an image producer that reads image data from an |
93 |
Hashtable props) |
* integer array. |
94 |
{ |
* |
95 |
width = w; |
* @param w width of image |
96 |
height = h; |
* @param h height of image |
97 |
this.cm = cm; |
* @param cm the color model used to represent pixel values |
98 |
offset = off; |
* @param pix an integer array of pixel values |
99 |
scansize = scan; |
* @param off the offset into the array at which the first pixel is stored |
100 |
this.props = props; |
* @param scan the number of array elements that represents a single pixel row |
101 |
int max = (( scansize > width ) ? scansize : width ); |
*/ |
102 |
pixeli = new int[ max * height ]; |
public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, |
103 |
System.arraycopy( pix, 0, pixeli, 0, max * height ); |
int scan) |
104 |
} |
{ |
105 |
/** |
this(w, h, cm, pix, off, scan, null); |
106 |
Constructs an ImageProducer from memory using the default RGB ColorModel |
} |
107 |
*/ |
|
108 |
public MemoryImageSource(int w, int h, |
/** |
109 |
int pix[], int off, int scan, |
Constructs an ImageProducer from memory |
110 |
Hashtable props) |
*/ |
111 |
{ |
public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, |
112 |
this ( w, h, ColorModel.getRGBdefault(), pix, off, scan, props); |
int scan, Hashtable props) |
113 |
} |
{ |
114 |
|
width = w; |
115 |
/** |
height = h; |
116 |
Constructs an ImageProducer from memory using the default RGB ColorModel |
this.cm = cm; |
117 |
*/ |
offset = off; |
118 |
public MemoryImageSource(int w, int h, |
scansize = scan; |
119 |
int pix[], int off, int scan) |
this.props = props; |
120 |
{ |
int max = ((scansize > width) ? scansize : width); |
121 |
this ( w, h, ColorModel.getRGBdefault(), pix, off, scan, null); |
pixeli = pix; |
122 |
} |
} |
123 |
|
|
124 |
/** |
/** |
125 |
* Used to register an <code>ImageConsumer</code> with this |
* Constructs an ImageProducer from memory using the default RGB ColorModel |
126 |
* <code>ImageProducer</code>. |
*/ |
127 |
*/ |
public MemoryImageSource(int w, int h, int[] pix, int off, int scan, |
128 |
public synchronized void addConsumer(ImageConsumer ic) { |
Hashtable props) |
129 |
if (consumers.contains(ic)) |
{ |
130 |
return; |
this(w, h, ColorModel.getRGBdefault(), pix, off, scan, props); |
131 |
|
} |
132 |
consumers.addElement(ic); |
|
133 |
} |
/** |
134 |
|
* Constructs an ImageProducer from memory using the default RGB ColorModel |
135 |
/** |
*/ |
136 |
* Used to determine if the given <code>ImageConsumer</code> is |
public MemoryImageSource(int w, int h, int[] pix, int off, int scan) |
137 |
* already registered with this <code>ImageProducer</code>. |
{ |
138 |
*/ |
this(w, h, ColorModel.getRGBdefault(), pix, off, scan, null); |
139 |
public synchronized boolean isConsumer(ImageConsumer ic) { |
} |
140 |
if (consumers.contains(ic)) |
|
141 |
return true; |
/** |
142 |
return false; |
* Used to register an <code>ImageConsumer</code> with this |
143 |
} |
* <code>ImageProducer</code>. |
144 |
|
*/ |
145 |
/** |
public synchronized void addConsumer(ImageConsumer ic) |
146 |
* Used to remove an <code>ImageConsumer</code> from the list of |
{ |
147 |
* registered consumers for this <code>ImageProducer</code>. |
if (consumers.contains(ic)) |
148 |
*/ |
return; |
149 |
public synchronized void removeConsumer(ImageConsumer ic) { |
|
150 |
consumers.removeElement(ic); |
consumers.addElement(ic); |
151 |
} |
} |
152 |
|
|
153 |
/** |
/** |
154 |
* Used to register an <code>ImageConsumer</code> with this |
* Used to determine if the given <code>ImageConsumer</code> is |
155 |
* <code>ImageProducer</code> and then immediately start |
* already registered with this <code>ImageProducer</code>. |
156 |
* reconstruction of the image data to be delivered to all |
*/ |
157 |
* registered consumers. |
public synchronized boolean isConsumer(ImageConsumer ic) |
158 |
*/ |
{ |
159 |
public void startProduction(ImageConsumer ic) { |
if (consumers.contains(ic)) |
160 |
if (!(consumers.contains(ic))) { |
return true; |
161 |
consumers.addElement(ic); |
return false; |
162 |
} |
} |
163 |
|
|
164 |
|
/** |
165 |
|
* Used to remove an <code>ImageConsumer</code> from the list of |
166 |
|
* registered consumers for this <code>ImageProducer</code>. |
167 |
|
*/ |
168 |
|
public synchronized void removeConsumer(ImageConsumer ic) |
169 |
|
{ |
170 |
|
consumers.removeElement(ic); |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
|
* Used to register an <code>ImageConsumer</code> with this |
175 |
|
* <code>ImageProducer</code> and then immediately start |
176 |
|
* reconstruction of the image data to be delivered to all |
177 |
|
* registered consumers. |
178 |
|
*/ |
179 |
|
public void startProduction(ImageConsumer ic) |
180 |
|
{ |
181 |
|
if (! (consumers.contains(ic))) |
182 |
|
consumers.addElement(ic); |
183 |
|
|
184 |
|
Vector list = (Vector) consumers.clone(); |
185 |
|
for (int i = 0; i < list.size(); i++) |
186 |
|
{ |
187 |
|
ic = (ImageConsumer) list.elementAt(i); |
188 |
|
sendPicture(ic); |
189 |
|
if (animated) |
190 |
|
ic.imageComplete(ImageConsumer.SINGLEFRAME); |
191 |
|
else |
192 |
|
ic.imageComplete(ImageConsumer.STATICIMAGEDONE); |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
|
* Used to register an <code>ImageConsumer</code> with this |
198 |
|
* <code>ImageProducer</code> and then request that this producer |
199 |
|
* resend the image data in the order top-down, left-right. |
200 |
|
*/ |
201 |
|
public void requestTopDownLeftRightResend(ImageConsumer ic) |
202 |
|
{ |
203 |
|
startProduction(ic); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Changes a flag to indicate whether this MemoryImageSource supports |
208 |
|
* animations. |
209 |
|
* |
210 |
|
* @param animated A flag indicating whether this class supports animations |
211 |
|
*/ |
212 |
|
public synchronized void setAnimated(boolean animated) |
213 |
|
{ |
214 |
|
this.animated = animated; |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
|
* A flag to indicate whether or not to send full buffer updates when |
219 |
|
* sending animation. If this flag is set then full buffers are sent |
220 |
|
* in the newPixels methods instead of just regions. |
221 |
|
* |
222 |
|
* @param fullbuffers - a flag indicating whether to send the full buffers |
223 |
|
*/ |
224 |
|
public synchronized void setFullBufferUpdates(boolean fullbuffers) |
225 |
|
{ |
226 |
|
this.fullbuffers = fullbuffers; |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
|
* Send an animation frame to the image consumers. |
231 |
|
*/ |
232 |
|
public void newPixels() |
233 |
|
{ |
234 |
|
if (animated == true) |
235 |
|
{ |
236 |
|
ImageConsumer ic; |
237 |
Vector list = (Vector) consumers.clone(); |
Vector list = (Vector) consumers.clone(); |
238 |
for(int i = 0; i < list.size(); i++) { |
for (int i = 0; i < list.size(); i++) |
239 |
|
{ |
240 |
ic = (ImageConsumer) list.elementAt(i); |
ic = (ImageConsumer) list.elementAt(i); |
241 |
sendPicture( ic ); |
sendPicture(ic); |
242 |
ic.imageComplete( ImageConsumer.STATICIMAGEDONE ); |
ic.imageComplete(ImageConsumer.SINGLEFRAME); |
243 |
} |
} |
244 |
} |
} |
245 |
|
} |
246 |
/** |
|
247 |
* Used to register an <code>ImageConsumer</code> with this |
private void sendPicture(ImageConsumer ic) |
248 |
* <code>ImageProducer</code> and then request that this producer |
{ |
249 |
* resend the image data in the order top-down, left-right. |
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT); |
250 |
*/ |
if (props != null) |
251 |
public void requestTopDownLeftRightResend(ImageConsumer ic) { |
ic.setProperties(props); |
252 |
startProduction ( ic ); |
ic.setDimensions(width, height); |
253 |
} |
ic.setColorModel(cm); |
254 |
|
if (pixeli != null) |
255 |
|
ic.setPixels(0, 0, width, height, cm, pixeli, offset, scansize); |
256 |
/** |
else |
257 |
Changes a flag to indicate whether this MemoryImageSource supports |
ic.setPixels(0, 0, width, height, cm, pixelb, offset, scansize); |
258 |
animations. |
} |
259 |
|
|
260 |
@param animated A flag indicating whether this class supports animations |
/** |
261 |
*/ |
* Send an animation frame to the image consumers containing the specified |
262 |
public synchronized void setAnimated(boolean animated) |
* pixels unless setFullBufferUpdates is set. |
263 |
{ |
*/ |
264 |
this.animated = animated; |
public synchronized void newPixels(int x, int y, int w, int h) |
265 |
} |
{ |
266 |
|
if (animated == true) |
267 |
|
{ |
268 |
/** |
if (fullbuffers) |
269 |
A flag to indicate whether or not to send full buffer updates when |
newPixels(); |
270 |
sending animation. If this flag is set then full buffers are sent |
else |
271 |
in the newPixels methods instead of just regions. |
{ |
272 |
|
ImageConsumer ic; |
273 |
@param fullbuffers - a flag indicating whether to send the full buffers |
Vector list = (Vector) consumers.clone(); |
274 |
*/ |
for (int i = 0; i < list.size(); i++) |
275 |
public synchronized void setFullBufferUpdates(boolean fullbuffers) |
{ |
276 |
{ |
ic = (ImageConsumer) list.elementAt(i); |
277 |
this.fullbuffers = fullbuffers; |
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT); |
278 |
} |
if (props != null) |
279 |
|
ic.setProperties(props); |
280 |
/** |
if (pixeli != null) |
281 |
Send an animation frame to the image consumers. |
{ |
282 |
*/ |
int[] pixelbuf = new int[w * h]; |
283 |
public void newPixels() |
for (int row = y; row < y + h; row++) |
284 |
{ |
System.arraycopy(pixeli, row * scansize + x + offset, |
285 |
if( animated == true ) { |
pixelbuf, 0, w * h); |
286 |
ImageConsumer ic; |
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w); |
287 |
Vector list = (Vector) consumers.clone(); |
} |
288 |
for(int i = 0; i < list.size(); i++) { |
else |
289 |
ic = (ImageConsumer) list.elementAt(i); |
{ |
290 |
sendPicture( ic ); |
byte[] pixelbuf = new byte[w * h]; |
291 |
ic.imageComplete( ImageConsumer.SINGLEFRAME ); |
for (int row = y; row < y + h; row++) |
292 |
} |
System.arraycopy(pixelb, row * scansize + x + offset, |
293 |
} |
pixelbuf, 0, w * h); |
294 |
} |
|
295 |
|
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w); |
296 |
|
} |
297 |
private void sendPicture ( ImageConsumer ic ) |
ic.imageComplete(ImageConsumer.SINGLEFRAME); |
298 |
{ |
} |
299 |
ic.setHints( ImageConsumer.TOPDOWNLEFTRIGHT ); |
} |
300 |
if( props != null ) { |
} |
301 |
ic.setProperties( props ); |
} |
302 |
} |
|
303 |
ic.setDimensions(width, height); |
/** |
304 |
ic.setColorModel(cm); |
* Send an animation frame to the image consumers containing the specified |
305 |
if( pixeli != null ) { |
* pixels unless setFullBufferUpdates is set. |
306 |
ic.setPixels( 0, 0, width, height, cm, pixeli, offset, scansize ); |
* |
307 |
} else { |
* If framenotify is set then a notification is sent when the frame |
308 |
ic.setPixels( 0, 0, width, height, cm, pixelb, offset, scansize ); |
* is sent otherwise no status is sent. |
309 |
} |
*/ |
310 |
} |
public synchronized void newPixels(int x, int y, int w, int h, |
311 |
|
boolean framenotify) |
312 |
/** |
{ |
313 |
Send an animation frame to the image consumers containing the specified |
if (animated == true) |
314 |
pixels unless setFullBufferUpdates is set. |
{ |
315 |
*/ |
if (fullbuffers) |
316 |
public synchronized void newPixels(int x, |
newPixels(); |
317 |
int y, |
else |
318 |
int w, |
{ |
319 |
int h) |
ImageConsumer ic; |
320 |
{ |
Vector list = (Vector) consumers.clone(); |
321 |
if( animated == true ) |
for (int i = 0; i < list.size(); i++) |
322 |
{ |
{ |
323 |
if( fullbuffers ) { |
ic = (ImageConsumer) list.elementAt(i); |
324 |
newPixels(); |
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT); |
325 |
} else { |
if (props != null) |
326 |
ImageConsumer ic; |
ic.setProperties(props); |
327 |
Vector list = (Vector) consumers.clone(); |
if (pixeli != null) |
328 |
for(int i = 0; i < list.size(); i++) { |
{ |
329 |
ic = (ImageConsumer) list.elementAt(i); |
int[] pixelbuf = new int[w * h]; |
330 |
ic.setHints( ImageConsumer.TOPDOWNLEFTRIGHT ); |
for (int row = y; row < y + h; row++) |
331 |
if( props != null ) { |
System.arraycopy(pixeli, row * scansize + x + offset, |
332 |
ic.setProperties( props ); |
pixelbuf, 0, w * h); |
333 |
} |
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w); |
334 |
if( pixeli != null ) { |
} |
335 |
int[] pixelbuf = new int[w * h]; |
else |
336 |
for (int row = y; row < h; row++) |
{ |
337 |
System.arraycopy(pixeli, row * scansize + x + offset, pixelbuf, row * w, w); |
byte[] pixelbuf = new byte[w * h]; |
338 |
ic.setPixels( x, y, w, h, cm, pixelbuf, 0, w ); |
for (int row = y; row < y + h; row++) |
339 |
} else { |
System.arraycopy(pixelb, row * scansize + x + offset, |
340 |
byte[] pixelbuf = new byte[w * h]; |
pixelbuf, 0, w * h); |
341 |
for (int row = y; row < h; row++) |
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w); |
342 |
System.arraycopy(pixelb, row * scansize + x + offset, pixelbuf, row * w, w); |
} |
343 |
ic.setPixels( x, y, w, h, cm, pixelbuf, 0, w ); |
if (framenotify == true) |
344 |
} |
ic.imageComplete(ImageConsumer.SINGLEFRAME); |
345 |
ic.imageComplete( ImageConsumer.SINGLEFRAME ); |
} |
346 |
} |
} |
347 |
} |
} |
348 |
} |
} |
349 |
} |
|
350 |
|
public synchronized void newPixels(byte[] newpix, ColorModel newmodel, |
351 |
|
int offset, int scansize) |
352 |
|
{ |
353 |
/** |
pixeli = null; |
354 |
Send an animation frame to the image consumers containing the specified |
pixelb = newpix; |
355 |
pixels unless setFullBufferUpdates is set. |
cm = newmodel; |
356 |
|
this.offset = offset; |
357 |
If framenotify is set then a notification is sent when the frame |
this.scansize = scansize; |
358 |
is sent otherwise no status is sent. |
if (animated == true) |
359 |
*/ |
newPixels(); |
360 |
public synchronized void newPixels(int x, |
} |
361 |
int y, |
|
362 |
int w, |
public synchronized void newPixels(int[] newpix, ColorModel newmodel, |
363 |
int h, |
int offset, int scansize) |
364 |
boolean framenotify) |
{ |
365 |
{ |
pixelb = null; |
366 |
if( animated == true ) |
pixeli = newpix; |
367 |
{ |
cm = newmodel; |
368 |
if( fullbuffers ) { |
this.offset = offset; |
369 |
newPixels(); |
this.scansize = scansize; |
370 |
} else { |
if (animated == true) |
371 |
ImageConsumer ic; |
newPixels(); |
372 |
Vector list = (Vector) consumers.clone(); |
} |
|
for(int i = 0; i < list.size(); i++) { |
|
|
ic = (ImageConsumer) list.elementAt(i); |
|
|
ic.setHints( ImageConsumer.TOPDOWNLEFTRIGHT ); |
|
|
if( props != null ) { |
|
|
ic.setProperties( props ); |
|
|
} |
|
|
if( pixeli != null ) { |
|
|
int[] pixelbuf = new int[w * h]; |
|
|
for (int row = y; row < h; row++) |
|
|
System.arraycopy(pixeli, row * scansize + x + offset, pixelbuf, row * w, w); |
|
|
ic.setPixels( x, y, w, h, cm, pixelbuf, 0, w ); |
|
|
} else { |
|
|
byte[] pixelbuf = new byte[w * h]; |
|
|
for (int row = y; row < h; row++) |
|
|
System.arraycopy(pixelb, row * scansize + x + offset, pixelbuf, row * w, w); |
|
|
ic.setPixels( x, y, w, h, cm, pixelbuf, 0, w ); |
|
|
} |
|
|
if( framenotify == true ) |
|
|
ic.imageComplete( ImageConsumer.SINGLEFRAME ); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
public synchronized void newPixels(byte newpix[], |
|
|
ColorModel newmodel, |
|
|
int offset, |
|
|
int scansize) |
|
|
|
|
|
{ |
|
|
pixeli = null; |
|
|
pixelb = newpix; |
|
|
cm = newmodel; |
|
|
this.offset = offset; |
|
|
this.scansize = scansize; |
|
|
if( animated == true ) |
|
|
{ |
|
|
newPixels(); |
|
|
} |
|
|
} |
|
|
|
|
|
public synchronized void newPixels(int newpix[], |
|
|
ColorModel newmodel, |
|
|
int offset, |
|
|
int scansize) |
|
|
|
|
|
{ |
|
|
pixelb = null; |
|
|
pixeli = newpix; |
|
|
cm = newmodel; |
|
|
this.offset = offset; |
|
|
this.scansize = scansize; |
|
|
if( animated == true ) |
|
|
{ |
|
|
newPixels(); |
|
|
} |
|
|
} |
|
|
|
|
373 |
} |
} |