1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
7 /** Interface implemented by classes capable of decoding an image file into an int[] */
8 public abstract class ImageDecoder {
10 /** returns the width of the image */
11 public abstract int getWidth();
13 /** returns the height of the image */
14 public abstract int getHeight();
16 /** returns the data of the image, as an array of 32-bit AARRGGBB samples */
17 public abstract int[] getData();
19 /** loads the image described by string str, possibly blocking for a network load */
20 public static ImageDecoder getImageDecoder(String str, final JS.Callable callback) {
22 if (str.indexOf(':') == -1) {
24 byte[] b = Resources.getResource(Resources.resolve(s + ".png", null));
25 if (b != null) return PNG.decode(new ByteArrayInputStream(b), str);
26 b = Resources.getResource(Resources.resolve(s + ".jpeg", null));
27 if (b != null) return Platform.decodeJPEG(new ByteArrayInputStream(b), str);
31 java.lang.Thread thread = java.lang.Thread.currentThread();
32 if (!(thread instanceof ThreadMessage)) {
33 if (Log.on) Log.log(Box.class, "HTTP images can not be loaded from the foreground thread");
36 // FIXME: use primitives here
37 ThreadMessage mythread = (ThreadMessage)thread;
38 mythread.setPriority(java.lang.Thread.MIN_PRIORITY);
39 mythread.done.release();
41 HTTP http = new HTTP(str);
42 final HTTP.HTTPInputStream in = http.GET();
43 final int contentLength = in.getContentLength();
44 InputStream is = new FilterInputStream(in) {
45 int bytesDownloaded = 0;
47 public int read() throws IOException {
51 public int read(byte[] b, int off, int len) throws IOException {
52 int ret = super.read(b, off, len);
53 if (ret != -1) bytesDownloaded += ret;
54 if (clear && callback != null) {
56 ThreadMessage.newthread(new JS.Callable() {
57 public Object call(JS.Array args_) throws JS.Exn {
59 JS.Array args = new JS.Array();
60 args.addElement(new Double(bytesDownloaded));
61 args.addElement(new Double(contentLength));
74 if (str.endsWith(".gif")) return GIF.decode(is, str);
75 else if (str.endsWith(".jpeg") || str.endsWith(".jpg")) return Platform.decodeJPEG(is, str);
76 else return PNG.decode(is, str);
78 } catch (IOException e) {
79 if (Log.on) Log.log(Box.class, "error while trying to load an image from " + str);
80 if (Log.on) Log.log(Box.class, e);
84 MessageQueue.add(mythread);
85 mythread.setPriority(java.lang.Thread.NORM_PRIORITY);
91 /** gets an Image using getImage(), adds it to the cache, and creates a Picture from it */
92 public static Picture getPicture(String os) {
94 ret = (Picture)pictureCache.get(os);
95 if (ret != null) return ret;
96 ImageDecoder id = ImageDecoder.getImageDecoder(os, null);
97 if (id == null) return null;
98 ret = Platform.createPicture(id);
99 pictureCache.put(os, ret);
100 imageToNameMap.put(ret, os);
104 /** caches images, keyed on resource name or url */
105 public static Hash pictureCache = new Hash();
107 /** stores image names, keyed on image object */
108 static Hash imageToNameMap = new Hash();