2003/10/28 10:10:17
[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     private static GIF gif = new GIF();
27     
28     /** turns a resource into a Picture.Source */
29     public static Picture fromRes(Res r) {
30         Picture ret = (Picture)cache.get(r);
31         if (ret == null) {
32             try {
33                 PushbackInputStream pbis = new PushbackInputStream(r.getInputStream());
34                 int c = pbis.read();
35                 pbis.unread(c);
36                 if (c == 'G') ret = gif.fromInputStream(pbis, r.getDescriptiveName());
37                 else if (c == 137) ret = new PNG().fromInputStream(pbis, r.getDescriptiveName());
38                 else if (c == 0xff) ret = Platform.decodeJPEG(pbis, r.getDescriptiveName());
39                 else throw new JS.Exn("couldn't figure out image type from first byte");
40                 cache.put(r, ret);
41                 ret.res = r;
42             } catch (IOException e) {
43                 Log.logJS(Picture.class, e);
44                 return null;
45             }
46         }
47         return ret;
48     }
49
50     /** the resource that created this Picture */
51     public Res res = null;
52
53     /** the height of the picture */
54     public abstract int getHeight();
55     
56     /** the width of the picture */
57     public abstract int getWidth();
58
59 }