VisClient/org/hfbk/vis/ImageFetcher.java

Go to the documentation of this file.
00001 /*
00002  * Created on 20.04.2008
00003  */
00004 package org.hfbk.vis;
00005 
00006 import java.awt.Image;
00007 import java.nio.ByteBuffer;
00008 
00009 import org.dronus.gl.Texture;
00010 import org.hfbk.util.Sleeper;
00011 import org.hfbk.vid.AVImageLoader;
00012 import org.lwjgl.opengl.GL11;
00013 
00025 public class ImageFetcher extends Thread {
00026 
00027         static final String LIBAV_PATTERN="(?i).*\\.(jpg|jpeg)";
00028         
00029         static final int SIMULTANEOUS_FETCH_COUNT = 4;
00030 
00031         static int activeCount = 0;
00032 
00033         String url;
00034 
00035         // public Image image;
00036         public ByteBuffer pixels;
00037 
00038         public int width, height;
00039         public int pixelformat;
00040 
00041         boolean thumbnail;
00042         
00043         
00044         AVImageLoader avs;
00045         
00046         public ImageFetcher(String url, boolean asThumbnail) {
00047                 super("ImageFetcherThread");
00048                 this.url = url;
00049                 this.thumbnail = asThumbnail;
00050 
00051                 setPriority(Thread.MIN_PRIORITY);
00052 
00053                 start();
00054         }
00055 
00059         public void run() {             
00060                 synchronized (ImageFetcher.class) {
00061                         waitReady();
00062                         activeCount++;
00063                 }
00064                 //tex=new AVStillPicture().get(imageUrl);
00065                 
00066                 try{
00067                         deliverImage(url);
00068                 }catch(Exception e){
00069                         System.out.println("Could not open image "+url);
00070                         if (Prefs.current.verbose) System.out.println("Reason: "+e.getMessage());
00071                 }
00072                 
00073                 synchronized(ImageFetcher.class){
00074                         activeCount--;
00075                         ImageFetcher.class.notify();
00076                 }
00077         }
00078         
00079         void deliverImage(String url) throws Exception{
00080                 
00081                 if (thumbnail){ //check for cached .thumbs/filename thumb
00082                         Image img=ImageLoader.getDefault().getCachedThumbnail(url);
00083                         if (img!=null) {
00084                                 deliverImage(img);
00085                                 return;
00086                         }
00087                 }
00088                 
00089                 if(url.matches(LIBAV_PATTERN)) { //use libav image io
00090                         avs=new AVImageLoader(url, thumbnail);
00091                         width=avs.width; height=avs.height;
00092                         pixelformat=GL11.GL_RGB;
00093                         
00094                         pixels=avs.pixels;
00095                         return;
00096                 }
00097                 
00098                 // use java image io
00099                 if (thumbnail)
00100                         deliverImage(ImageLoader.getDefault().getThumbnail(url));
00101                 else
00102                         deliverImage(ImageLoader.getDefault().getImg(url));             
00103         }
00104         
00105         
00106         void deliverImage(Image img){
00107                 if (img!=null){
00108                         width=img.getWidth(null); height=img.getHeight(null);
00109                         pixelformat=GL11.GL_RGBA;
00110                         pixels=Texture.getImageBuffer(img);
00111                 }
00112         }       
00113         
00114         public void free(){
00115                 if (avs!=null)
00116                         avs.free();
00117                 avs=null;
00118         }
00119         
00123         public static synchronized void waitReady(){
00124                 while (activeCount > SIMULTANEOUS_FETCH_COUNT)
00125                         try{
00126                                 ImageFetcher.class.wait();
00127                         }catch (Exception e) {
00128                                 throw new RuntimeException(e);
00129                         }
00130         }
00131 }
00132 

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1