426266cb911111a21aade82e0762bc124ce71ba6
[org.ibex.core.git] / src / org / xwt / Picture.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3 import java.io.*;
4 import org.xwt.js.*;
5 import org.xwt.util.*;
6 import org.xwt.translators.*;
7
8 /** 
9  *    <p>
10  *    The in-memory representation of a PNG or GIF image. It is
11  *    read-only. It is usually passed to PixelBuffer.drawPicture()
12  *    </p>
13  *
14  *    <p>
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).
19  *    </p>
20  */
21 public abstract class Picture {
22
23     /** the resource that created this Picture */
24     public Res res = null;
25
26     /** the height of the picture */
27     public abstract int getHeight();
28     
29     /** the width of the picture */
30     public abstract int getWidth();
31
32     /** Pictures, cache keyed by Res instance */
33     private static Cache cache = new Cache(100);
34     private static GIF gif = new GIF();
35     
36     public static class Holder {
37         public Picture picture = null;
38     }
39
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);
43         if (ret == null) {
44             ret = new Holder();
45             cache.put(r, ret);
46             if (callback == null) return null;
47         }
48         final Holder holder = ret;
49         if (callback != null)
50             new java.lang.Thread() { public void run() {
51                 try {
52                     final byte[] b = InputStreamToByteArray.convert(r.getInputStream());
53                     Scheduler.add(new Scheduler.Task() { public void perform() {
54                         try {
55                             Picture p = null;
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");
61                             p.res = r;
62                             holder.picture = p;
63                             Scheduler.add(callback);
64                         } catch (Exception e) {
65                             Log.log(Picture.class, e);
66                         } } });
67                 } catch (IOException e) {
68                     Log.log(Picture.class, e);
69                     return;
70                 }
71             } }.start();
72         return ret;
73     }
74 }