88cf65b291f6c25f1511c6dedde1d2f79ff0a79e
[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     /** Pictures, cached by Res */
24     private static Cache cache = new Cache();
25     
26     /** turns a resource into a Picture.Source */
27     public static Picture fromRes(Res r) {
28         // FIXME: put self in background thread if needed
29         Picture ret = (Picture)cache.get(r);
30         if (ret == null) {
31             try {
32                 PushbackInputStream pbis = new PushbackInputStream(r.getInputStream());
33                 int c = pbis.read();
34                 pbis.unread(c);
35                 // FEATURE: cache GIF/PNG objects, reuse int[]'s?
36                 if (c == 'G') ret = new GIF().fromInputStream(pbis, "FIXME");
37                 else if (c == 137) ret = new PNG().fromInputStream(pbis, "FIXME");
38                 else if (c == 0xff) ret = Platform.decodeJPEG(pbis, "FIXME");
39                 else throw new JS.Exn("couldn't figure out image type from first byte");
40                 cache.put(r, ret);
41             } catch (IOException e) {
42                 Log.logJS(Picture.class, e);
43                 return null;
44             }
45         }
46         return ret;
47     }
48
49     /** the height of the picture */
50     public abstract int getHeight();
51     
52     /** the width of the picture */
53     public abstract int getWidth();
54
55 }