- Picture ret = null;
- byte[] b = (byte[])o;
- InputStream pbis = new ByteArrayInputStream(b);
- if ((b[0] & 0xff) == 'G') ret = gif.fromInputStream(pbis, r.getDescriptiveName());
- else if ((b[0] & 0xff) == 137) ret = new PNG().fromInputStream(pbis, r.getDescriptiveName());
- else if ((b[0] & 0xff) == 0xff) ret = Platform.decodeJPEG(pbis, r.getDescriptiveName());
- else throw new JS.Exn("couldn't figure out image type from first byte");
- ret.res = r;
- cache.put(r, ret);
- callback.call(ret);
+ in = r.getInputStream();
+ } catch (IOException 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.info(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);