1 /* MemoryImageSource.java -- Java class for providing image data
2 Copyright (C) 1999, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java
.awt
.image
;
41 import java
.util
.Hashtable
;
42 import java
.util
.Vector
;
44 public class MemoryImageSource
implements ImageProducer
46 private boolean animated
= false;
47 private boolean fullbuffers
= false;
53 private byte[] pixelb
;
54 private ColorModel cm
;
55 private Hashtable props
= new Hashtable();
56 private Vector consumers
= new Vector();
59 * Construct an image producer that reads image data from a byte
62 * @param w width of image
63 * @param h height of image
64 * @param cm the color model used to represent pixel values
65 * @param pix a byte array of pixel values
66 * @param off the offset into the array at which the first pixel is stored
67 * @param scan the number of array elements that represents a single pixel row
69 public MemoryImageSource(int w
, int h
, ColorModel cm
, byte[] pix
, int off
,
72 this(w
, h
, cm
, pix
, off
, scan
, null);
76 * Constructs an ImageProducer from memory
78 public MemoryImageSource(int w
, int h
, ColorModel cm
, byte[] pix
, int off
,
79 int scan
, Hashtable props
)
87 int max
= ((scansize
> width
) ? scansize
: width
);
92 * Construct an image producer that reads image data from an
95 * @param w width of image
96 * @param h height of image
97 * @param cm the color model used to represent pixel values
98 * @param pix an integer array of pixel values
99 * @param off the offset into the array at which the first pixel is stored
100 * @param scan the number of array elements that represents a single pixel row
102 public MemoryImageSource(int w
, int h
, ColorModel cm
, int[] pix
, int off
,
105 this(w
, h
, cm
, pix
, off
, scan
, null);
109 Constructs an ImageProducer from memory
111 public MemoryImageSource(int w
, int h
, ColorModel cm
, int[] pix
, int off
,
112 int scan
, Hashtable props
)
120 int max
= ((scansize
> width
) ? scansize
: width
);
125 * Constructs an ImageProducer from memory using the default RGB ColorModel
127 public MemoryImageSource(int w
, int h
, int[] pix
, int off
, int scan
,
130 this(w
, h
, ColorModel
.getRGBdefault(), pix
, off
, scan
, props
);
134 * Constructs an ImageProducer from memory using the default RGB ColorModel
136 public MemoryImageSource(int w
, int h
, int[] pix
, int off
, int scan
)
138 this(w
, h
, ColorModel
.getRGBdefault(), pix
, off
, scan
, null);
142 * Used to register an <code>ImageConsumer</code> with this
143 * <code>ImageProducer</code>.
145 public synchronized void addConsumer(ImageConsumer ic
)
147 if (consumers
.contains(ic
))
150 consumers
.addElement(ic
);
154 * Used to determine if the given <code>ImageConsumer</code> is
155 * already registered with this <code>ImageProducer</code>.
157 public synchronized boolean isConsumer(ImageConsumer ic
)
159 if (consumers
.contains(ic
))
165 * Used to remove an <code>ImageConsumer</code> from the list of
166 * registered consumers for this <code>ImageProducer</code>.
168 public synchronized void removeConsumer(ImageConsumer ic
)
170 consumers
.removeElement(ic
);
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.
179 public void startProduction(ImageConsumer ic
)
181 if (! (consumers
.contains(ic
)))
182 consumers
.addElement(ic
);
184 Vector list
= (Vector
) consumers
.clone();
185 for (int i
= 0; i
< list
.size(); i
++)
187 ic
= (ImageConsumer
) list
.elementAt(i
);
190 ic
.imageComplete(ImageConsumer
.SINGLEFRAME
);
192 ic
.imageComplete(ImageConsumer
.STATICIMAGEDONE
);
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.
201 public void requestTopDownLeftRightResend(ImageConsumer ic
)
207 * Changes a flag to indicate whether this MemoryImageSource supports
210 * @param animated A flag indicating whether this class supports animations
212 public synchronized void setAnimated(boolean animated
)
214 this.animated
= animated
;
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.
222 * @param fullbuffers - a flag indicating whether to send the full buffers
224 public synchronized void setFullBufferUpdates(boolean fullbuffers
)
226 this.fullbuffers
= fullbuffers
;
230 * Send an animation frame to the image consumers.
232 public void newPixels()
234 if (animated
== true)
237 Vector list
= (Vector
) consumers
.clone();
238 for (int i
= 0; i
< list
.size(); i
++)
240 ic
= (ImageConsumer
) list
.elementAt(i
);
242 ic
.imageComplete(ImageConsumer
.SINGLEFRAME
);
247 private void sendPicture(ImageConsumer ic
)
249 ic
.setHints(ImageConsumer
.TOPDOWNLEFTRIGHT
);
251 ic
.setProperties(props
);
252 ic
.setDimensions(width
, height
);
253 ic
.setColorModel(cm
);
255 ic
.setPixels(0, 0, width
, height
, cm
, pixeli
, offset
, scansize
);
257 ic
.setPixels(0, 0, width
, height
, cm
, pixelb
, offset
, scansize
);
261 * Send an animation frame to the image consumers containing the specified
262 * pixels unless setFullBufferUpdates is set.
264 public synchronized void newPixels(int x
, int y
, int w
, int h
)
266 if (animated
== true)
273 Vector list
= (Vector
) consumers
.clone();
274 for (int i
= 0; i
< list
.size(); i
++)
276 ic
= (ImageConsumer
) list
.elementAt(i
);
277 ic
.setHints(ImageConsumer
.TOPDOWNLEFTRIGHT
);
279 ic
.setProperties(props
);
282 int[] pixelbuf
= new int[w
* h
];
283 for (int row
= y
; row
< y
+ h
; row
++)
284 System
.arraycopy(pixeli
, row
* scansize
+ x
+ offset
,
286 ic
.setPixels(x
, y
, w
, h
, cm
, pixelbuf
, 0, w
);
290 byte[] pixelbuf
= new byte[w
* h
];
291 for (int row
= y
; row
< y
+ h
; row
++)
292 System
.arraycopy(pixelb
, row
* scansize
+ x
+ offset
,
295 ic
.setPixels(x
, y
, w
, h
, cm
, pixelbuf
, 0, w
);
297 ic
.imageComplete(ImageConsumer
.SINGLEFRAME
);
304 * Send an animation frame to the image consumers containing the specified
305 * pixels unless setFullBufferUpdates is set.
307 * If framenotify is set then a notification is sent when the frame
308 * is sent otherwise no status is sent.
310 public synchronized void newPixels(int x
, int y
, int w
, int h
,
313 if (animated
== true)
320 Vector list
= (Vector
) consumers
.clone();
321 for (int i
= 0; i
< list
.size(); i
++)
323 ic
= (ImageConsumer
) list
.elementAt(i
);
324 ic
.setHints(ImageConsumer
.TOPDOWNLEFTRIGHT
);
326 ic
.setProperties(props
);
329 int[] pixelbuf
= new int[w
* h
];
330 for (int row
= y
; row
< y
+ h
; row
++)
331 System
.arraycopy(pixeli
, row
* scansize
+ x
+ offset
,
333 ic
.setPixels(x
, y
, w
, h
, cm
, pixelbuf
, 0, w
);
337 byte[] pixelbuf
= new byte[w
* h
];
338 for (int row
= y
; row
< y
+ h
; row
++)
339 System
.arraycopy(pixelb
, row
* scansize
+ x
+ offset
,
341 ic
.setPixels(x
, y
, w
, h
, cm
, pixelbuf
, 0, w
);
343 if (framenotify
== true)
344 ic
.imageComplete(ImageConsumer
.SINGLEFRAME
);
350 public synchronized void newPixels(byte[] newpix
, ColorModel newmodel
,
351 int offset
, int scansize
)
356 this.offset
= offset
;
357 this.scansize
= scansize
;
358 if (animated
== true)
362 public synchronized void newPixels(int[] newpix
, ColorModel newmodel
,
363 int offset
, int scansize
)
368 this.offset
= offset
;
369 this.scansize
= scansize
;
370 if (animated
== true)