00001 package org.hfbk.vis;
00002
00003 import java.awt.Image;
00004 import java.awt.image.BufferedImage;
00005 import java.io.IOException;
00006 import java.net.URL;
00007 import javax.imageio.ImageIO;
00008 import javax.imageio.stream.MemoryCacheImageInputStream;
00009
00010 public class ImageLoader {
00011 static ImageLoader def;
00012
00013 public static ImageLoader getDefault() {
00014 if (def == null)
00015 def = new ImageLoader();
00016 return def;
00017 }
00018
00025 public BufferedImage getImg(String imageUrl) {
00026 ImageIO.setUseCache(false);
00027 if (imageUrl == null)
00028 return null;
00029 BufferedImage img = null;
00030
00031 try {
00032 if (! imageUrl.matches("http.*") ) imageUrl="file://"+imageUrl;
00033 URL url = new URL(imageUrl);
00034
00035 img = ImageIO.read(new MemoryCacheImageInputStream(url.openStream()));
00036 } catch (Exception e) {
00037 if (Prefs.current.verbose) System.out.println("ImageLoader: couldnt read " + imageUrl + ", for reason:" + e);
00038 }
00039 return img;
00040 }
00041
00046 public static Image getRessourceImage(String iconfile) {
00047 try {
00048 return ImageIO.read(new MemoryCacheImageInputStream(ClassLoader.getSystemResourceAsStream(iconfile)));
00049 } catch (IOException e) {
00050 System.out.println("ImageLoader: couldnt read " + iconfile + ", for reason:" + e);
00051 return null;
00052 }
00053 }
00054
00055 public String thumbUrl(String url){
00056 int slash=url.lastIndexOf('/');
00057 if (slash==-1) slash=url.lastIndexOf('\\');
00058 return url.substring(0,slash+1)+"thumbs"+url.substring(slash);
00059 }
00060
00061 public Image getCachedThumbnail(String url) {
00062 if(!url.matches("http.*")) return getImg(thumbUrl(url));
00063 else return null;
00064 }
00065
00066 public Image getThumbnail(String url) {
00067 BufferedImage img=null;
00068
00069 img=getImg(url);
00070 if (img==null) return null;
00071
00072 float w=img.getWidth(), h=img.getHeight();
00073 if(w*h<Prefs.current.thumbpixels)
00074 return img;
00075 else {
00076 double scale=Math.sqrt(Prefs.current.thumbpixels/ (w*h));
00077 return img.getScaledInstance((int)(w*scale), (int)(h*scale), BufferedImage.SCALE_FAST);
00078 }
00079 }
00080 }