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(100);
34 private static GIF gif = new GIF();
36 public static class Holder {
37 public Picture picture = null;
40 /** turns a resource into a Picture.Source and passes it to the callback */
41 public static Holder fromRes(final Res r, final Scheduler.Task callback) {
42 Holder ret = (Holder)cache.get(r);
46 if (callback == null) return null;
48 final Holder holder = ret;
50 new java.lang.Thread() { public void run() {
52 final byte[] b = InputStreamToByteArray.convert(r.getInputStream());
53 Scheduler.add(new Scheduler.Task() { public void perform() {
56 InputStream pbis = new ByteArrayInputStream(b);
57 if ((b[0] & 0xff) == 'G') p = gif.fromInputStream(pbis, "some picture");
58 else if ((b[0] & 0xff) == 137) p = new PNG().fromInputStream(pbis, "some picture");
59 else if ((b[0] & 0xff) == 0xff) p = Platform.decodeJPEG(pbis, "some picture");
60 else throw new JSExn("couldn't figure out image type from first byte");
63 Scheduler.add(callback);
64 } catch (Exception e) {
65 Log.log(Picture.class, e);
67 } catch (IOException e) {
68 Log.log(Picture.class, e);