2003/08/10 06:03:01
[org.ibex.core.git] / src / org / xwt / ImageDecoder.java
1 // Copyright 2002 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
7 /** Interface implemented by classes capable of decoding an image file into an int[] */
8 public abstract class ImageDecoder {
9     
10     /** returns the width of the image */
11     public abstract int getWidth();
12
13     /** returns the height of the image */
14     public abstract int getHeight();
15
16     /** returns the data of the image, as an array of 32-bit AARRGGBB samples */
17     public abstract int[] getData();
18
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) {
21
22         if (str.indexOf(':') == -1) {
23             String s = str;
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);
28             return null;
29             
30         } else {
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");
34                 return null;
35             }
36             // FIXME: use primitives here
37             ThreadMessage mythread = (ThreadMessage)thread;
38             mythread.setPriority(java.lang.Thread.MIN_PRIORITY);
39             mythread.done.release();
40             try {
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;
46                         boolean clear = true;
47                         public int read() throws IOException {
48                             bytesDownloaded++;
49                             return super.read();
50                         }
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) {
55                                 clear = false;
56                                 ThreadMessage.newthread(new JS.Callable() {
57                                         public Object call(JS.Array args_) throws JS.Exn {
58                                             try {
59                                                 JS.Array args = new JS.Array();
60                                                 args.addElement(new Double(bytesDownloaded));
61                                                 args.addElement(new Double(contentLength));
62                                                 callback.call(args);
63                                             } finally {
64                                                 clear = true;
65                                             }
66                                             return null;
67                                         }
68                                     });
69                             }
70                             return ret;
71                         }
72                     };
73
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);
77
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);
81                 return null;
82
83             } finally {
84                 MessageQueue.add(mythread);
85                 mythread.setPriority(java.lang.Thread.NORM_PRIORITY);
86                 mythread.go.block();
87             }
88         }
89     }
90
91     /** gets an Image using getImage(), adds it to the cache, and creates a Picture from it */
92     public static Picture getPicture(String os) {
93         Picture ret = null;
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);
101         return ret;
102     }
103
104     /** caches images, keyed on resource name or url */
105     public static Hash pictureCache = new Hash();
106
107     /** stores image names, keyed on image object */
108     static Hash imageToNameMap = new Hash();
109
110 }