2003/12/25 08:52:13
[org.ibex.core.git] / src / org / xwt / Picture.java
index 8cd3c97..8538d39 100644 (file)
@@ -1,20 +1,81 @@
 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
+import java.io.*;
+import org.xwt.js.*;
+import org.xwt.util.*;
+import org.xwt.translators.*;
 
 /** 
- *    <p>
  *    The in-memory representation of a PNG or GIF image. It is
  *    read-only. It is usually passed to PixelBuffer.drawPicture()
- *    </p>
  *
- *    <p>
  *    Implementations of the Platform class should return objects
  *    supporting this interface from the createPicture() method. These
  *    implementations may choose to implement caching strategies (for
  *    example, using a Pixmap on X11).
- *    </p>
  */
-public abstract class Picture {
-    public abstract int getHeight();
-    public abstract int getWidth();
+public class Picture {
+
+    public Picture() { this.res = null; }
+    public Picture(Res r) { this.res = r; }
+    private static Cache cache = new Cache(100);   ///< Picture, keyed by the Res that loaded them
+
+    public Res res = null;                         ///< the resource we were loaded from
+    public int width = -1;                         ///< the width of the image
+    public int height = -1;                        ///< the height of the image
+    public int[] data = null;                      ///< argb samples
+    public boolean isLoaded = false;               ///< true iff the image is fully loaded
+    
+    /** turns a resource into a Picture.Source and passes it to the callback */
+    public static Picture load(final Res r, final Scheduler.Task callback) {
+        Picture ret = (Picture)cache.get(r);
+        if (ret == null) cache.put(r, ret = Platform.createPicture(r));
+        final Picture p = ret;
+        if (!ret.isLoaded && callback != null)
+            new java.lang.Thread() { public void run() {
+                // get the InputStream for the image
+                InputStream in = null;
+                try {
+                    in = r.getInputStream();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    Log.log(Picture.class, e);
+                    
+                    in = null;
+                    if (r instanceof Res.Ref) {
+                        // add extensions to the resource, looking for the image
+                        Res.Ref ref = (Res.Ref)r;
+                        Res newr;
+
+                        String[] exts = new String[] { ".png", ".jpeg", ".gif" };
+                        for (int i=0; i < exts.length && in == null; i++) {
+                            newr = ref.addExtension(exts[i]);
+                            try { in = newr.getInputStream(); }
+                            catch (IOException f) { in = null; }
+                        }
+                    }
+                }
+        
+                // could not find image
+                if (in == null) { Log.log(Picture.class, "couldn't load image for resource " + r); return; }
+
+                try {
+                    PushbackInputStream pbis = new PushbackInputStream(in);
+                    int firstByte = pbis.read();
+                    if (firstByte == -1) throw new JSExn("empty stream reading image");
+                    pbis.unread(firstByte);
+                    if ((firstByte & 0xff) == 'G') GIF.load(pbis, p);
+                    else if ((firstByte & 0xff) == 137)  PNG.load(pbis, p);
+                    else if ((firstByte & 0xff) == 0xff) Platform.decodeJPEG(pbis, p);
+                    else throw new JSExn("couldn't figure out image type from first byte");
+                    p.isLoaded = true;
+                    Scheduler.add(callback);
+                } catch (Exception e) {
+                    Log.log(this, "exception while loading image");
+                    Log.log(this, e);
+                }
+            } }.start();
+
+        return ret;
+    }
 }