Stream->Fountain, move Scheduler to Platform, HashMap->Hash
[org.ibex.core.git] / src / org / ibex / graphics / Picture.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.graphics;
6 import java.io.*;
7 import org.ibex.js.*;
8 import org.ibex.plat.*;
9 import org.ibex.util.*;
10
11 /** 
12  *    The in-memory representation of a PNG or GIF image. It is
13  *    read-only. It is usually passed to PixelBuffer.drawPicture()
14  *
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  */
20 public class Picture {
21
22     public Picture() { this.stream = null; }
23     public Picture(JS r) { this.stream = r; }
24     private static Cache cache = new Cache(100, true); ///< Picture, keyed by the Stream that loaded them
25
26     public JS stream = null;                       ///< the stream we were loaded from
27     public int width = -1;                         ///< the width of the image
28     public int height = -1;                        ///< the height of the image
29     public int[] data = null;                      ///< argb samples
30     public boolean isLoaded = false;               ///< true iff the image is fully loaded
31
32     /** invoked when an image is fully loaded; subclasses can use this to initialize platform-specific constructs */
33     protected void loaded() { isLoaded = true; }
34
35     /** turns a stream into a Picture.Source and passes it to the callback */
36     public static Picture load(final JS stream, final Callable callback) {
37         if(stream == null) throw new NullPointerException();
38         Picture ret = (Picture)cache.get(stream);
39         if (ret == null) {
40             ret = Platform.createPicture(stream);
41             if(ret == null) throw new NullPointerException();
42             cache.put(stream, ret);
43         }
44         final Picture p = ret;
45         if (!ret.isLoaded && callback != null) {
46             new java.lang.Thread() { public void run() {
47                 InputStream in = null;
48                 try {
49                     in = JSU.getInputStream(stream);
50                 } catch (IOException e) { Log.error(Picture.class, e);
51                 //} catch (JSExn e) { Log.error(Picture.class, e);
52                 }
53                 if (in == null) { Log.warn(Picture.class, "couldn't load image for stream " + stream.unclone()); return; }
54                 try {
55                     PushbackInputStream pbis = new PushbackInputStream(in);
56                     int firstByte = pbis.read();
57                     if (firstByte == -1) throw new JSExn("empty stream reading image");
58                     pbis.unread(firstByte);
59                     if ((firstByte & 0xff) == 'G') GIF.load(pbis, p);
60                     else if ((firstByte & 0xff) == 137)  PNG.load(pbis, p);
61                     else if ((firstByte & 0xff) == 0xff) Platform.decodeJPEG(pbis, p);
62                     else throw new JSExn("couldn't figure out image type from first byte");
63                     p.loaded();
64                     Platform.Scheduler.add(callback);
65                 } catch (Exception e) {
66                     Log.info(this, "exception while loading image");
67                     Log.info(this, e);
68                 }
69             } }.start();
70         }
71         return ret;
72     }
73 }