2004/01/13 10:27:46
[org.ibex.core.git] / src / org / xwt / Picture.java
1 // Copyright 2004 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  *    The in-memory representation of a PNG or GIF image. It is
10  *    read-only. It is usually passed to PixelBuffer.drawPicture()
11  *
12  *    Implementations of the Platform class should return objects
13  *    supporting this interface from the createPicture() method. These
14  *    implementations may choose to implement caching strategies (for
15  *    example, using a Pixmap on X11).
16  */
17 public class Picture {
18
19     public Picture() { this.res = null; }
20     public Picture(JS r) { this.res = r; }
21     private static Cache cache = new Cache(100);   ///< Picture, keyed by the Stream that loaded them
22
23     public JS res = null;                          ///< the resource we were loaded from
24     public int width = -1;                         ///< the width of the image
25     public int height = -1;                        ///< the height of the image
26     public int[] data = null;                      ///< argb samples
27     public boolean isLoaded = false;               ///< true iff the image is fully loaded
28     
29     /** turns a resource into a Picture.Source and passes it to the callback */
30     public static Picture load(JS rr, final Scheduler.Task callback) {
31         final JS r = rr;
32         Picture ret = (Picture)cache.get(r);
33         if (ret == null) cache.put(r, ret = Platform.createPicture(r));
34         final Picture p = ret;
35         if (!ret.isLoaded && callback != null)
36             new java.lang.Thread() { public void run() {
37                 // get the InputStream for the image
38                 Log.info(Picture.class, "r is a " + r.getClass().getName());
39                 InputStream in = null;
40                 try {
41                     in = (r instanceof XWT.Blessing) ? ((XWT.Blessing)r).getImage() : Stream.getInputStream(r);
42                 } catch (IOException e) {
43                     Log.error(Picture.class, r);
44                 } catch (JSExn e) {
45                     Log.error(Picture.class, r);
46                 }
47         
48                 // could not find image
49                 if (in == null) { Log.info(Picture.class, "couldn't load image for resource " + r); return; }
50
51                 try {
52                     PushbackInputStream pbis = new PushbackInputStream(in);
53                     int firstByte = pbis.read();
54                     if (firstByte == -1) throw new JSExn("empty stream reading image");
55                     pbis.unread(firstByte);
56                     if ((firstByte & 0xff) == 'G') GIF.load(pbis, p);
57                     else if ((firstByte & 0xff) == 137)  PNG.load(pbis, p);
58                     else if ((firstByte & 0xff) == 0xff) Platform.decodeJPEG(pbis, p);
59                     else throw new JSExn("couldn't figure out image type from first byte");
60                     p.isLoaded = true;
61                     Scheduler.add(callback);
62                 } catch (Exception e) {
63                     Log.info(this, "exception while loading image");
64                     Log.info(this, e);
65                 }
66             } }.start();
67
68         return ret;
69     }
70 }