1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
6 import org.xwt.translators.*;
10 * The in-memory representation of a PNG or GIF image. It is
11 * read-only. It is usually passed to PixelBuffer.drawPicture()
15 * Implementations of the Platform class should return objects
16 * supporting this interface from the createPicture() method. These
17 * implementations may choose to implement caching strategies (for
18 * example, using a Pixmap on X11).
21 public abstract class Picture {
23 /** the resource that created this Picture */
24 public Res res = null;
26 /** the height of the picture */
27 public abstract int getHeight();
29 /** the width of the picture */
30 public abstract int getWidth();
32 /** Pictures, cache keyed by Res instance */
33 private static Cache cache = new Cache();
34 private static GIF gif = new GIF();
36 // FIXME: return a Picture that gets filled in later
37 /** turns a resource into a Picture.Source and passes it to the callback */
38 public static Picture fromRes(final Res r, final Callback callback) {
39 Picture ret = (Picture)cache.get(r);
40 if (ret != null) return ret;
42 Platform.inputStreamToByteArray(r.getInputStream(), new Callback() { public Object call(Object o) {
46 InputStream pbis = new ByteArrayInputStream(b);
47 if ((b[0] & 0xff) == 'G') ret = gif.fromInputStream(pbis, r.getDescriptiveName());
48 else if ((b[0] & 0xff) == 137) ret = new PNG().fromInputStream(pbis, r.getDescriptiveName());
49 else if ((b[0] & 0xff) == 0xff) ret = Platform.decodeJPEG(pbis, r.getDescriptiveName());
50 else throw new JS.Exn("couldn't figure out image type from first byte");
54 } catch (Exception e) {
55 Log.log(Picture.class, e);
59 } catch (Exception e) {
60 Log.log(Picture.class, e);