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)
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 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 ();
63 ImageObserver observer;
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.
78 GtkImage (ImageProducer producer, Graphics g)
84 source.addConsumer (this);
87 public void setObserver (ImageObserver observer)
89 this.observer = observer;
92 public synchronized int[]
98 public synchronized ColorModel
104 public synchronized int
105 getWidth (ImageObserver observer)
108 widthObservers.addElement (observer);
113 public synchronized int
114 getHeight (ImageObserver observer)
117 heightObservers.addElement (observer);
134 public synchronized Object
135 getProperty (String name, ImageObserver observer)
139 propertyObservers.addElement (observer);
143 Object value = props.get (name);
144 return (value == null) ? UndefinedProperty : value;
147 public synchronized void
159 source.removeConsumer (this);
160 source.addConsumer (this);
170 /* ImageConsumer methods */
172 public synchronized void
173 setDimensions (int width, int height)
175 pixelCache = new int[width*height];
178 this.height = height;
180 for (int i = 0; i < widthObservers.size (); i++)
182 ImageObserver io = (ImageObserver) widthObservers.elementAt (i);
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);
191 io.imageUpdate (this, ImageObserver.HEIGHT, -1, -1, width, height);
194 if (observer != null)
195 observer.imageUpdate (this,
197 | ImageObserver.HEIGHT),
198 -1, -1, width, height);
201 public synchronized void
202 setProperties (Hashtable props)
206 for (int i = 0; i < propertyObservers.size (); i++)
208 ImageObserver io = (ImageObserver) propertyObservers.elementAt (i);
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))
223 public synchronized void
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,
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)
250 if (!cm.equals(model) || pixelCache == null)
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,
272 public synchronized void
273 imageComplete (int status)
275 if (status == ImageConsumer.STATICIMAGEDONE && isCacheable)
278 if (status == ImageConsumer.SINGLEFRAME)
281 if (observer != null)
283 if (status == ImageConsumer.IMAGEERROR)
284 observer.imageUpdate (null,
288 observer.imageUpdate (null,
289 ImageObserver.ALLBITS,
293 if (source != null && status != ImageConsumer.SINGLEFRAME)
294 source.removeConsumer (this);
297 public synchronized void
298 startProduction (GtkImagePainter painter)
302 painter.setDimensions (width, height);
303 painter.setPixels (0, 0, width, height, model, pixelCache, 0, width);
309 source.startProduction (painter);
310 source.removeConsumer (painter);
316 convertPixels (byte[] pixels)
318 int ret[] = new int[pixels.length];
320 for (int i = 0; i < pixels.length; i++)
332 bits |= ImageObserver.WIDTH;
334 bits |= ImageObserver.HEIGHT;
336 bits |= ImageObserver.PROPERTIES;
338 bits |= ImageObserver.SOMEBITS;
340 bits |= ImageObserver.ALLBITS;