ImageCache.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00024 #ifndef _HUGINAPP_IMAGECACHE_H
00025 #define _HUGINAPP_IMAGECACHE_H
00026 
00027 #include <hugin_shared.h>
00028 #include <map>
00029 #include <boost/version.hpp>
00030 #include <boost/shared_ptr.hpp>
00031 #if BOOST_VERSION>=105400
00032 #include <boost/signals2.hpp>
00033 #else
00034 #include <boost/signal.hpp>
00035 #endif
00036 #include <vigra/stdimage.hxx>
00037 #include <vigra/imageinfo.hxx>
00038 #include <hugin_utils/utils.h>
00039 #include <appbase/ProgressDisplayOld.h>
00040 
00041 #define HUGIN_IMGCACHE_MAPPING_INTEGER        0l
00042 #define HUGIN_IMGCACHE_MAPPING_FLOAT          1l
00043 
00044 
00045 namespace HuginBase {
00046     
00055 class IMPEX ImageCache
00056 {
00057 
00058     public:
00060         typedef boost::shared_ptr<vigra::BRGBImage> ImageCacheRGB8Ptr;
00061         typedef boost::shared_ptr<vigra::UInt16RGBImage> ImageCacheRGB16Ptr;
00062         typedef boost::shared_ptr<vigra::FRGBImage> ImageCacheRGBFloatPtr;
00063         typedef boost::shared_ptr<vigra::BImage> ImageCache8Ptr;
00064 
00066         struct IMPEX Entry
00067         {
00068             ImageCacheRGB8Ptr image8;
00069             ImageCacheRGB16Ptr image16;
00070             ImageCacheRGBFloatPtr imageFloat;
00071             ImageCache8Ptr mask;
00072 
00073             std::string origType;
00074             int lastAccess;
00075 
00076             public:
00078                 Entry()
00079                   : image8(ImageCacheRGB8Ptr(new vigra::BRGBImage)),
00080                     image16(ImageCacheRGB16Ptr(new vigra::UInt16RGBImage)),
00081                     imageFloat(ImageCacheRGBFloatPtr(new vigra::FRGBImage)),
00082                     mask(ImageCache8Ptr(new vigra::BImage))
00083                 {
00084                       DEBUG_TRACE("Constructing an empty ImageCache::Entry");
00085                 };
00086 
00088                 Entry(ImageCacheRGB8Ptr & img, 
00089                       ImageCacheRGB16Ptr & img16,
00090                       ImageCacheRGBFloatPtr & imgFloat,
00091                       ImageCache8Ptr & imgMask,
00092                       const std::string & typ)
00093                   : image8(img), image16(img16), imageFloat(imgFloat), mask(imgMask), origType(typ), lastAccess(0)
00094                 { 
00095                         DEBUG_TRACE("Constructing ImageCache::Entry");
00096                 };
00097 
00099                 ~Entry()
00100                 {
00101                     DEBUG_TRACE("Deleting ImageCacheEntry");
00102                 };
00103 
00105                 ImageCacheRGB8Ptr get8BitImage();
00106         };
00107 
00109         typedef boost::shared_ptr<Entry> EntryPtr;
00110         
00114         class Request
00115         {
00116             public:
00117                 Request(std::string filename, bool request_small)
00118                     :m_filename(filename), m_isSmall(request_small)
00119                     {};
00128 #if BOOST_VERSION>=105400
00129                 boost::signals2::signal<void(EntryPtr, std::string, bool)> ready;
00130 #else
00131                 boost::signal<void(EntryPtr, std::string, bool)> ready;
00132 #endif
00133                 bool getIsSmall() const
00134                     {return m_isSmall;};
00135                 const std::string & getFilename() const
00136                     {return m_filename;};
00137             protected:
00138                 std::string m_filename;
00139                 bool m_isSmall;
00140         };
00141         
00152         typedef boost::shared_ptr<Request> RequestPtr;
00153 
00154     private:
00155         // ctor. private, nobody execpt us can create an instance.
00156         ImageCache()
00157             : m_progress(NULL), m_accessCounter(0),
00158               asyncLoadCompleteSignal(0), upperBound(100*1024*1024l)
00159         {};
00160         
00161     public:
00164         virtual ~ImageCache()
00165         {
00166                 images.clear();
00167                 instance = NULL;
00168         }
00169 
00171         static ImageCache & getInstance();
00172         
00173     private:
00174         static ImageCache* instance;
00175 
00176         
00177     public:
00189         EntryPtr getImage(const std::string & filename);
00190         
00198         EntryPtr getImageIfAvailable(const std::string & filename);
00199 
00209         EntryPtr getSmallImage(const std::string & filename);
00210         
00220         EntryPtr getSmallImageIfAvailable(const std::string & filename);
00221          
00228         RequestPtr requestAsyncImage(const std::string & filename);
00229         
00236         RequestPtr requestAsyncSmallImage(const std::string & filename);
00237 
00241         void removeImage(const std::string & filename);
00242 
00248         void flush();
00249 
00254         void softFlush();
00257                 void SetUpperLimit(long newUpperLimit) { upperBound=newUpperLimit; };
00258         
00269         void (*asyncLoadCompleteSignal)(RequestPtr, EntryPtr);
00270         
00278         void postEvent(RequestPtr request, EntryPtr entry);
00279 
00280     private:
00281         long upperBound;
00282 
00283         template <class SrcPixelType,
00284                   class DestIterator, class DestAccessor>
00285         static void importAndConvertImage(const vigra::ImageImportInfo& info,
00286                                    vigra::pair<DestIterator, DestAccessor> dest,
00287                                    const std::string& type);
00288         
00289     //    template <class SrcPixelType,
00290     //              class DestIterator, class DestAccessor>
00291     //    void importAndConvertGrayImage(const ImageImportInfo& info,
00292     //                                   vigra::pair<DestIterator, DestAccessor> dest,
00293     //                                   wxString type);
00294         
00295     //    template <class SrcPixelType,
00296     //              class DestIterator, class DestAccessor>
00297     //    void importAndConvertGrayAlphaImage(const ImageImportInfo & info,
00298     //                                        vigra::pair<DestIterator, DestAccessor> dest,
00299     //                                        wxString type);
00300         
00301         template <class SrcPixelType,
00302                   class DestIterator, class DestAccessor,
00303                   class MaskIterator, class MaskAccessor>
00304         static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
00305                                         vigra::pair<DestIterator, DestAccessor> dest,
00306                                         vigra::pair<MaskIterator, MaskAccessor> mask,
00307                                         const std::string & type);
00308         
00309         
00310     public:
00312         void setProgressDisplay(AppBase::MultiProgressDisplay* disp)
00313             { m_progress = disp; }
00314         
00316         void clearProgressDisplay(AppBase::MultiProgressDisplay* disp)
00317             { m_progress = NULL; }
00318         
00319         
00320     private:
00321         std::map<std::string, EntryPtr> images;
00322 
00323         // our progress display
00324         AppBase::MultiProgressDisplay* m_progress;
00325 
00326         int m_accessCounter;
00327         
00328         // Requests for full size images that need loading
00329         std::map<std::string, RequestPtr> m_requests;
00330         
00331         // Requests for small images that need generating.
00332         std::map<std::string, RequestPtr> m_smallRequests;
00333         
00335         void spawnAsyncThread();
00336         
00345         static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
00346         
00350         static EntryPtr loadImageSafely(const std::string & filename);
00351         
00356         static EntryPtr loadSmallImageSafely(EntryPtr entry);
00357         
00358     public:
00372     //    const vigra::BImage & getPyramidImage(const std::string& filename,
00373     //                                          int level);
00374 
00375     private:
00376         // key for your pyramid map.
00377         struct PyramidKey
00378         {
00379             std::string filename;
00380             int level;
00381             
00382             public:
00383                 PyramidKey(const std::string& str, int lv)
00384                   : filename(str), level(lv)
00385                 {};
00386             
00387                 std::string toString()
00388                     { return filename + hugin_utils::lexical_cast<std::string>(level); }
00389         };
00390         
00391         std::map<std::string, vigra::BImage *> pyrImages;
00392 };
00393 
00394 
00395 } //namespace
00396 #endif // _IMAGECACHE_H

Generated on Wed Jul 16 01:25:38 2014 for Hugintrunk by  doxygen 1.3.9.1