Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

ImageLoader.h

Go to the documentation of this file.
00001 #ifndef IMAGELOADER_H
00002 #define IMAGELOADER_H
00003 
00004 #ifdef USE_BOOST_THREAD
00005 #ifndef USE_BOOST_THREADPOOL
00006 #include <boost/thread/thread.hpp>
00007 #endif
00008 #endif
00009 
00010 #ifdef USE_BOOST_THREADPOOL
00011 #define USE_BOOST_THREAD
00012 // use this version for boost v. 34 (as on UvA FC8)
00013 //#include "threadpool23/threadpool.hpp"
00014 // use this version for boost >= 37 (as on decent machines)
00015 #include "threadpool25/threadpool.hpp"
00016 #endif
00017 
00018 #ifndef BOOST_THREADPOOL_SIZE
00019 #define BOOST_THREADPOOL_SIZE 8
00020 #endif
00021 
00022 #include "Visualization/RgbOglImage.h"
00023 #include "Core/Array/ReadImage.h"
00024 
00025 #include "OglGui/GetOglImageByIdInterface.h"
00026 
00027 namespace Impala { namespace Application { namespace MediaTable {
00028 
00029 class ImageLoader : public OglGui::GetOglImageByIdInterface {
00030 public:
00031 #ifdef USE_BOOST_THREADPOOL
00032         typedef boost::threadpool::thread_pool
00033                 <boost::threadpool::task_func, boost::threadpool::lifo_scheduler, 
00034                  boost::threadpool::static_size, boost::threadpool::resize_controller, 
00035                  boost::threadpool::wait_for_active_tasks> pool;
00036 #endif
00037         ImageLoader(OglGui::GetOglImageByIdInterface* getOglImageById, int size = BOOST_THREADPOOL_SIZE)
00038         {
00039                 mGetOglImageById = getOglImageById;
00040 #ifdef USE_BOOST_THREADPOOL
00041         /* ToDo:
00042          * mThreadPool = new boost::threadpool::pool(1);
00043          * if(source->MultiThreadImageCacheSupported)
00044          *   mThreadPool->size_controller().resize(8); */
00045                 mThreadPool = pool(size);
00046 #endif
00047         }
00048         
00049         ~ImageLoader() 
00050         {
00051                 WaitUntilDone();
00052         }
00053         
00054         bool IsLoading()
00055         {
00056 #ifdef USE_BOOST_THREADPOOL
00057                 ILOG_DEBUG("TP check: Size " << mThreadPool.size() << " Active tasks " << mThreadPool.active() << " Pending tasks " << mThreadPool.pending());
00058                 if(mThreadPool.active())
00059                         return true;
00060                 if(mThreadPool.pending())
00061                         return true;
00062 #endif
00063                 return false;
00064         }
00065 
00066         /* GetOglImageById returns an OGLIMAGE* that is either the resulting image
00067          * or if Boost::Thread is used an empty image of 0x0.
00068          *
00069          * This empty image will be initialized in a thread with the correct data.
00070          */
00071         OGLIMAGE* GetOglImageById(long long id)
00072         {
00073 #ifdef USE_BOOST_THREAD
00074         Core::Array::Array2dVec3UInt8* emptyArray;
00075         emptyArray = Core::Array::ArrayCreate<Core::Array::Array2dVec3UInt8>(0, 0, 0, 0, 0, true);
00076         OGLIMAGE* oglIm = Visualization::RgbOglImage::OglImage(emptyArray);
00077                 // OGLIMAGE* oglIm = oglSys.OglImage(GL_RGB, 0, 0);
00078 
00079                 // Make sure image does not get destroyed while loading.
00080                 // Comparable to locking the image. Lock is released when
00081                 // image is loaded or loading failed.
00082                 oglIm->refCount++;
00083                 
00084         Worker worker = Worker(oglIm, mGetOglImageById, id);
00085 #ifdef USE_BOOST_THREADPOOL
00086         mThreadPool.schedule(worker);
00087 #else
00088         boost::thread work(worker);
00089 #endif
00090 
00091 #else
00092         OGLIMAGE* oglIm = mGetOglImageById->GetOglImageById(id);
00093 #endif
00094         if(oglIm != 0)
00095                 ILOG_DEBUG("Returning image of " << oglIm->w << "x" << oglIm->h <<" pixels at " << oglIm);
00096         return oglIm;
00097         }
00098 
00099 #ifdef USE_BOOST_THREAD
00100         class Worker {
00101         public:
00102                 Worker(OGLIMAGE* oglIm, OglGui::GetOglImageByIdInterface* getOglImageById, long long id)
00103                 {
00104                         mTargetOglImage         = oglIm;
00105                         mGetOglImageById        = getOglImageById;
00106                         mId                             = id;
00107                 }
00108 
00109                 int operator()()
00110                 {
00111                         if(!mTargetOglImage || !(mTargetOglImage->refCount > 1))
00112                         {
00113                                 // TODO: OglImageCache screws this up, as it ups refCount
00114                                 
00115                                 // We are the only one using this image, so stop loading.
00116                                 ReleaseOglImage(mTargetOglImage);
00117                                 
00118                                 ILOG_INFO("Image has refCount 0, skipped loading image.");
00119                                 // Nothing to do
00120                                 return 0;
00121                         }
00122 
00123                         OGLIMAGE* im = mGetOglImageById->GetOglImageById(mId);
00124 
00125                         if(im != 0) {
00126                                 assert(im->refCount == 1);
00127                                 //mTargetOglImage->imDataFunc  = im->imDataFunc;
00128                                 //mTargetOglImage->onDestroy   = im->onDestroy;
00129                                 mTargetOglImage->imageHandle = im->imageHandle;
00130                                 // Maybe we should change this to:
00131                                 // *mTargetOglImage = *im;
00132                                 // and store refCount of target
00133                                 im->imageHandle = 0;
00134 
00135                                 mTargetOglImage->h = im->h;
00136                                 mTargetOglImage->w = im->w;
00137                                 mTargetOglImage->changed = 1;
00138                                 
00139                                 ILOG_DEBUG("Loaded an image of " << mTargetOglImage->w <<
00140                                                         "x" << mTargetOglImage->h << " pixels to " <<
00141                                                         mTargetOglImage);
00142 
00143                                 ReleaseOglImage(im);
00144                                 if(mTargetOglImage)
00145                                         ReleaseOglImage(mTargetOglImage);
00146                                 return 1;
00147                         }
00148 
00149                         ReleaseOglImage(im);
00150                         if(mTargetOglImage)
00151                                 ReleaseOglImage(mTargetOglImage);
00152                         ILOG_ERROR("Failed to load image for " << mTargetOglImage);
00153                         return 0;
00154                 }
00155 
00156         private:
00157                 OGLIMAGE*                                                       mTargetOglImage;
00158                 OglGui::GetOglImageByIdInterface*       mGetOglImageById;
00159                 long long                                                       mId;
00160         };
00161 
00162 #ifdef USE_BOOST_THREADPOOL
00163         pool                                                                    mThreadPool;
00164 #endif
00165 #endif
00166 
00167         void WaitUntilDone()
00168         {
00169 #ifdef USE_BOOST_THREADPOOL
00170                 mThreadPool.wait();
00171 #endif
00172         }
00173 
00174         OglGui::GetOglImageByIdInterface*       mGetOglImageById;
00175 
00176     ILOG_VAR_DEC;
00177 };
00178 
00179 ILOG_VAR_INIT(ImageLoader, Application.MediaTable);
00180 
00181 } } } // Namespace Impala::Application::MediaTable
00182 
00183 #endif // IMAGELOADER_H

Generated on Fri Mar 19 09:30:32 2010 for ImpalaSrc by  doxygen 1.5.1