Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / java / awt / peer / gtk / GtkImage.java
blob510646c5a6adf189221ab639ed16618043cb581b
1 /* GtkImage.java
2 Copyright (C) 1999, 2002, 2003 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 gnu.java.awt.peer.gtk;
41 import java.awt.Graphics;
42 import java.awt.Image;
43 import java.awt.image.ColorModel;
44 import java.awt.image.ImageConsumer;
45 import java.awt.image.ImageObserver;
46 import java.awt.image.ImageProducer;
47 import java.util.Hashtable;
48 import java.util.Vector;
50 public class GtkImage extends Image implements ImageConsumer
52 int width = -1, height = -1;
53 Hashtable props = null;
54 boolean isLoaded = false;
55 boolean isCacheable = true;
56 boolean loading = false;
58 Vector widthObservers = new Vector ();
59 Vector heightObservers = new Vector ();
60 Vector propertyObservers = new Vector ();
62 ImageProducer source;
63 ImageObserver observer;
64 Graphics g;
66 /* Variables in which we stored cached data, if possible.
68 An image is cached if the following properties are true:
69 1. The ColorModel passed into setColorModel is the same ColorModel
70 passed to all invocations of setPixels.
71 2. The image contains a single frame.
74 int[] pixelCache;
75 ColorModel model;
77 public
78 GtkImage (ImageProducer producer, Graphics g)
80 source = producer;
81 this.g = g;
83 if (source != null)
84 source.addConsumer (this);
87 public void setObserver (ImageObserver observer)
89 this.observer = observer;
92 public synchronized int[]
93 getPixelCache ()
95 return pixelCache;
98 public synchronized ColorModel
99 getColorModel ()
101 return model;
104 public synchronized int
105 getWidth (ImageObserver observer)
107 if (width == -1)
108 widthObservers.addElement (observer);
110 return width;
113 public synchronized int
114 getHeight (ImageObserver observer)
116 if (height == -1)
117 heightObservers.addElement (observer);
119 return height;
122 public ImageProducer
123 getSource ()
125 return source;
128 public Graphics
129 getGraphics ()
131 return g;
134 public synchronized Object
135 getProperty (String name, ImageObserver observer)
137 if (props == null)
139 propertyObservers.addElement (observer);
140 return null;
143 Object value = props.get (name);
144 return (value == null) ? UndefinedProperty : value;
147 public synchronized void
148 flush ()
150 isLoaded = false;
151 isCacheable = true;
152 width = height = -1;
153 props = null;
154 pixelCache = null;
155 model = null;
157 if (source != null)
159 source.removeConsumer (this);
160 source.addConsumer (this);
164 public boolean
165 isLoaded ()
167 return isLoaded;
170 /* ImageConsumer methods */
172 public synchronized void
173 setDimensions (int width, int height)
175 pixelCache = new int[width*height];
177 this.width = width;
178 this.height = height;
180 for (int i = 0; i < widthObservers.size (); i++)
182 ImageObserver io = (ImageObserver) widthObservers.elementAt (i);
183 if (io != null)
184 io.imageUpdate (this, ImageObserver.WIDTH, -1, -1, width, height);
187 for (int i = 0; i < heightObservers.size (); i++)
189 ImageObserver io = (ImageObserver) heightObservers.elementAt (i);
190 if (io != null)
191 io.imageUpdate (this, ImageObserver.HEIGHT, -1, -1, width, height);
194 if (observer != null)
195 observer.imageUpdate (this,
196 (ImageObserver.WIDTH
197 | ImageObserver.HEIGHT),
198 -1, -1, width, height);
201 public synchronized void
202 setProperties (Hashtable props)
204 this.props = props;
206 for (int i = 0; i < propertyObservers.size (); i++)
208 ImageObserver io = (ImageObserver) propertyObservers.elementAt (i);
209 if (io != null)
210 io.imageUpdate (this, ImageObserver.PROPERTIES, -1, -1, width, height);
214 public synchronized void
215 setColorModel (ColorModel model)
217 if (this.model == null || this.model.equals(model))
218 this.model = model;
219 else
220 isCacheable = false;
223 public synchronized void
224 setHints (int flags)
228 public synchronized void
229 setPixels (int x, int y, int width, int height, ColorModel cm, byte[] pixels,
230 int offset, int scansize)
232 setPixels (x, y, width, height, cm, convertPixels (pixels), offset,
233 scansize);
235 if (observer != null)
236 observer.imageUpdate (this,
237 ImageObserver.SOMEBITS,
238 x, y, width, height);
241 public synchronized void
242 setPixels (int x, int y, int width, int height, ColorModel cm, int[] pixels,
243 int offset, int scansize)
245 loading = true;
247 if (!isCacheable)
248 return;
250 if (!cm.equals(model) || pixelCache == null)
252 isCacheable = false;
253 return;
256 if (scansize == width && height == 1)
258 // Copy contents of pixels array into pixel cache.
259 System.arraycopy (pixels, offset,
260 pixelCache, y * this.width + x,
261 pixels.length - offset);
263 else // skip over scansize-width for each row
265 for (int i = 0; i < height; i++)
266 System.arraycopy (pixels, offset + (i * scansize),
267 pixelCache, (y + i) * this.width + x,
268 width);
272 public synchronized void
273 imageComplete (int status)
275 if (status == ImageConsumer.STATICIMAGEDONE && isCacheable)
276 isLoaded = true;
278 if (status == ImageConsumer.SINGLEFRAME)
279 isCacheable = false;
281 if (observer != null)
283 if (status == ImageConsumer.IMAGEERROR)
284 observer.imageUpdate (null,
285 ImageObserver.ERROR,
286 -1, -1, -1, -1);
287 else
288 observer.imageUpdate (null,
289 ImageObserver.ALLBITS,
290 -1, -1, -1, -1);
293 if (source != null && status != ImageConsumer.SINGLEFRAME)
294 source.removeConsumer (this);
297 public synchronized void
298 startProduction (GtkImagePainter painter)
300 if (isLoaded)
302 painter.setDimensions (width, height);
303 painter.setPixels (0, 0, width, height, model, pixelCache, 0, width);
305 else
307 if (source != null)
309 source.startProduction (painter);
310 source.removeConsumer (painter);
315 private int[]
316 convertPixels (byte[] pixels)
318 int ret[] = new int[pixels.length];
320 for (int i = 0; i < pixels.length; i++)
321 ret[i] = pixels[i];
323 return ret;
326 synchronized int
327 checkImage ()
329 int bits = 0;
331 if (width != -1)
332 bits |= ImageObserver.WIDTH;
333 if (height != -1)
334 bits |= ImageObserver.HEIGHT;
335 if (props != null)
336 bits |= ImageObserver.PROPERTIES;
337 if (loading)
338 bits |= ImageObserver.SOMEBITS;
339 if (isLoaded)
340 bits |= ImageObserver.ALLBITS;
342 return bits;