00001 package edu.stanford.hci.r3.util.graphics;
00002
00003 import java.awt.image.BufferedImage;
00004 import java.io.File;
00005 import java.io.IOException;
00006 import java.lang.ref.SoftReference;
00007 import java.net.URISyntaxException;
00008 import java.net.URL;
00009 import java.util.WeakHashMap;
00010
00011 import javax.imageio.ImageIO;
00012 import javax.media.jai.JAI;
00013 import javax.media.jai.PlanarImage;
00014
00023 public class ImageCache {
00024
00028 public class ImageObject {
00029 private SoftReference<BufferedImage> bImgSoft;
00030
00031 private SoftReference<PlanarImage> pImgSoft;
00032
00033 public ImageObject() {
00034 }
00035
00039 public BufferedImage getBufferedImage(File imagePath) {
00040 if (bImgSoft != null) {
00041 final BufferedImage cachedImg = bImgSoft.get();
00042 if (cachedImg != null) {
00043
00044 return cachedImg;
00045 }
00046 }
00047
00048
00049
00050
00051 try {
00052 bImgSoft = new SoftReference<BufferedImage>(ImageIO.read(imagePath));
00053 } catch (IOException e) {
00054 e.printStackTrace();
00055 }
00056 return bImgSoft.get();
00057 }
00058
00062 public PlanarImage getPlanarImage(File imagePath) {
00063 if (pImgSoft != null) {
00064 final PlanarImage cachedImg = pImgSoft.get();
00065 if (cachedImg != null) {
00066
00067
00068 return cachedImg;
00069 } else {
00070 System.out.println("Image Cache MISS for Planar Image");
00071 }
00072 }
00073 pImgSoft = new SoftReference<PlanarImage>(JAI.create("fileload", imagePath
00074 .getAbsolutePath()));
00075 return pImgSoft.get();
00076 }
00077
00083 public void setBufferedImage(BufferedImage bImg) {
00084 bImgSoft = new SoftReference<BufferedImage>(bImg);
00085 }
00086 }
00087
00088 private static ImageCache instance = new ImageCache();
00089
00090 public static ImageCache getInstance() {
00091 return instance;
00092 }
00093
00098 public static BufferedImage loadBufferedImage(File path) {
00099 return getInstance().getBufferedImage(path);
00100 }
00101
00107 public static BufferedImage loadBufferedImage(URL resource) {
00108 try {
00109 return getInstance().getBufferedImage(new File(resource.toURI()));
00110 } catch (URISyntaxException e) {
00111 e.printStackTrace();
00112 }
00113 return null;
00114 }
00115
00120 public static PlanarImage loadPlanarImage(File path) {
00121 return getInstance().getPlanarImage(path);
00122 }
00123
00124
00125 private WeakHashMap<File, ImageObject> cache = new WeakHashMap<File, ImageObject>();
00126
00127 private ImageCache() {
00128
00129 }
00130
00137 public void addBufferedImageToCache(final File pathToImage, BufferedImage bImg) {
00138
00139 ImageObject image = cache.get(pathToImage);
00140 if (image == null) {
00141 image = new ImageObject();
00142 cache.put(pathToImage, image);
00143 }
00144 image.setBufferedImage(bImg);
00145 }
00146
00151 private ImageObject get(final File pathToImage) {
00152 ImageObject image = cache.get(pathToImage);
00153 if (image == null) {
00154 image = new ImageObject();
00155 cache.put(pathToImage, image);
00156 }
00157 return image;
00158 }
00159
00167 public BufferedImage getBufferedImage(File pathToImage) {
00168 return get(pathToImage).getBufferedImage(pathToImage);
00169 }
00170
00175 public PlanarImage getPlanarImage(File pathToImage) {
00176 if (pathToImage == null) {
00177 return null;
00178 }
00179 return get(pathToImage).getPlanarImage(pathToImage);
00180 }
00181 }