Merge from the pain train
[official-gcc.git] / libjava / java / awt / image / MemoryImageSource.java
blob6b43d00c37b459c57bd359d3df3496bc3ff82890
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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;
48 private int[] pixeli;
49 private int width;
50 private int height;
51 private int offset;
52 private int scansize;
53 private byte[] pixelb;
54 private ColorModel cm;
55 private Hashtable props = new Hashtable();
56 private Vector consumers = new Vector();
58 /**
59 * Construct an image producer that reads image data from a byte
60 * array.
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,
70 int scan)
72 this(w, h, cm, pix, off, scan, null);
75 /**
76 * Constructs an ImageProducer from memory
78 public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
79 int scan, Hashtable props)
81 width = w;
82 height = h;
83 this.cm = cm;
84 offset = off;
85 scansize = scan;
86 this.props = props;
87 int max = ((scansize > width) ? scansize : width);
88 pixelb = pix;
91 /**
92 * Construct an image producer that reads image data from an
93 * integer array.
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,
103 int scan)
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)
114 width = w;
115 height = h;
116 this.cm = cm;
117 offset = off;
118 scansize = scan;
119 this.props = props;
120 int max = ((scansize > width) ? scansize : width);
121 pixeli = pix;
125 * Constructs an ImageProducer from memory using the default RGB ColorModel
127 public MemoryImageSource(int w, int h, int[] pix, int off, int scan,
128 Hashtable props)
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))
148 return;
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))
160 return true;
161 return false;
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);
188 sendPicture(ic);
189 if (animated)
190 ic.imageComplete(ImageConsumer.SINGLEFRAME);
191 else
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)
203 startProduction(ic);
207 * Changes a flag to indicate whether this MemoryImageSource supports
208 * animations.
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)
236 ImageConsumer ic;
237 Vector list = (Vector) consumers.clone();
238 for (int i = 0; i < list.size(); i++)
240 ic = (ImageConsumer) list.elementAt(i);
241 sendPicture(ic);
242 ic.imageComplete(ImageConsumer.SINGLEFRAME);
247 private void sendPicture(ImageConsumer ic)
249 ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
250 if (props != null)
251 ic.setProperties(props);
252 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 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)
268 if (fullbuffers)
269 newPixels();
270 else
272 ImageConsumer ic;
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);
278 if (props != null)
279 ic.setProperties(props);
280 if (pixeli != null)
282 int[] pixelbuf = new int[w * h];
283 for (int row = y; row < y + h; row++)
284 System.arraycopy(pixeli, row * scansize + x + offset,
285 pixelbuf, 0, w * h);
286 ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
288 else
290 byte[] pixelbuf = new byte[w * h];
291 for (int row = y; row < y + h; row++)
292 System.arraycopy(pixelb, row * scansize + x + offset,
293 pixelbuf, 0, w * h);
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,
311 boolean framenotify)
313 if (animated == true)
315 if (fullbuffers)
316 newPixels();
317 else
319 ImageConsumer ic;
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);
325 if (props != null)
326 ic.setProperties(props);
327 if (pixeli != null)
329 int[] pixelbuf = new int[w * h];
330 for (int row = y; row < y + h; row++)
331 System.arraycopy(pixeli, row * scansize + x + offset,
332 pixelbuf, 0, w * h);
333 ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
335 else
337 byte[] pixelbuf = new byte[w * h];
338 for (int row = y; row < y + h; row++)
339 System.arraycopy(pixelb, row * scansize + x + offset,
340 pixelbuf, 0, w * h);
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)
353 pixeli = null;
354 pixelb = newpix;
355 cm = newmodel;
356 this.offset = offset;
357 this.scansize = scansize;
358 if (animated == true)
359 newPixels();
362 public synchronized void newPixels(int[] newpix, ColorModel newmodel,
363 int offset, int scansize)
365 pixelb = null;
366 pixeli = newpix;
367 cm = newmodel;
368 this.offset = offset;
369 this.scansize = scansize;
370 if (animated == true)
371 newPixels();