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 "hugin_config.h"
00029 #include <map>
00030 #include <vector>
00031 #include "hugin_utils/shared_ptr.h"
00032 #ifdef HAVE_CXX11
00033 #include <functional>
00034 #else
00035 #include <boost/function.hpp>
00036 #endif
00037 #include <vigra/stdimage.hxx>
00038 #include <vigra/imageinfo.hxx>
00039 #include <hugin_utils/utils.h>
00040 #include <appbase/ProgressDisplay.h>
00041 
00042 #define HUGIN_IMGCACHE_MAPPING_INTEGER        0l
00043 #define HUGIN_IMGCACHE_MAPPING_FLOAT          1l
00044 
00045 
00046 namespace HuginBase {
00047     
00056 class IMPEX ImageCache
00057 {
00058 
00059     public:
00061         typedef sharedPtrNamespace::shared_ptr<vigra::BRGBImage> ImageCacheRGB8Ptr;
00062         typedef sharedPtrNamespace::shared_ptr<vigra::UInt16RGBImage> ImageCacheRGB16Ptr;
00063         typedef sharedPtrNamespace::shared_ptr<vigra::FRGBImage> ImageCacheRGBFloatPtr;
00064         typedef sharedPtrNamespace::shared_ptr<vigra::BImage> ImageCache8Ptr;
00065         typedef sharedPtrNamespace::shared_ptr<vigra::ImageImportInfo::ICCProfile> ImageCacheICCProfile;
00066 
00068         struct IMPEX Entry
00069         {
00070             ImageCacheRGB8Ptr image8;
00071             ImageCacheRGB16Ptr image16;
00072             ImageCacheRGBFloatPtr imageFloat;
00073             ImageCache8Ptr mask;
00074             ImageCacheICCProfile iccProfile;
00075 
00076             std::string origType;
00077             int lastAccess;
00078 
00079             public:
00081                 Entry()
00082                   : image8(ImageCacheRGB8Ptr(new vigra::BRGBImage)),
00083                     image16(ImageCacheRGB16Ptr(new vigra::UInt16RGBImage)),
00084                     imageFloat(ImageCacheRGBFloatPtr(new vigra::FRGBImage)),
00085                     mask(ImageCache8Ptr(new vigra::BImage)),
00086                     iccProfile(ImageCacheICCProfile(new vigra::ImageImportInfo::ICCProfile)),
00087                     lastAccess(0)
00088                 {
00089                       DEBUG_TRACE("Constructing an empty ImageCache::Entry");
00090                 };
00091 
00093                 Entry(ImageCacheRGB8Ptr & img, 
00094                       ImageCacheRGB16Ptr & img16,
00095                       ImageCacheRGBFloatPtr & imgFloat,
00096                       ImageCache8Ptr & imgMask,
00097                       ImageCacheICCProfile & ICCProfile,
00098                       const std::string & typ)
00099                   : image8(img), image16(img16), imageFloat(imgFloat), mask(imgMask), origType(typ), lastAccess(0),
00100                     iccProfile(ICCProfile)
00101                 { 
00102                         DEBUG_TRACE("Constructing ImageCache::Entry");
00103                 };
00104 
00106                 ~Entry()
00107                 {
00108                     DEBUG_TRACE("Deleting ImageCacheEntry");
00109                 };
00110 
00112                 ImageCacheRGB8Ptr get8BitImage();
00113         };
00114 
00116         typedef sharedPtrNamespace::shared_ptr<Entry> EntryPtr;
00117         
00121         class Request
00122         {
00123             public:
00124                 Request(std::string filename, bool request_small)
00125                     :m_filename(filename), m_isSmall(request_small)
00126                     {};
00135 #ifdef HAVE_CXX11
00136                 std::vector <std::function<void(EntryPtr, std::string, bool)>> ready;
00137 #else
00138                 std::vector <boost::function<void(EntryPtr, std::string, bool)> > ready;
00139 #endif
00140                 bool getIsSmall() const
00141                     {return m_isSmall;};
00142                 const std::string & getFilename() const
00143                     {return m_filename;};
00144             protected:
00145                 std::string m_filename;
00146                 bool m_isSmall;
00147         };
00148         
00159         typedef sharedPtrNamespace::shared_ptr<Request> RequestPtr;
00160 
00161     private:
00162         // ctor. private, nobody execpt us can create an instance.
00163         ImageCache()
00164             : m_progress(NULL), m_accessCounter(0),
00165               asyncLoadCompleteSignal(0), upperBound(100*1024*1024l)
00166         {};
00167         
00168     public:
00171         virtual ~ImageCache()
00172         {
00173                 images.clear();
00174                 instance = NULL;
00175         }
00176 
00178         static ImageCache & getInstance();
00179         
00180     private:
00181         static ImageCache* instance;
00182 
00183         
00184     public:
00196         EntryPtr getImage(const std::string & filename);
00197         
00205         EntryPtr getImageIfAvailable(const std::string & filename);
00206 
00216         EntryPtr getSmallImage(const std::string & filename);
00217         
00227         EntryPtr getSmallImageIfAvailable(const std::string & filename);
00228          
00235         RequestPtr requestAsyncImage(const std::string & filename);
00236         
00243         RequestPtr requestAsyncSmallImage(const std::string & filename);
00244 
00248         void removeImage(const std::string & filename);
00249 
00255         void flush();
00256 
00261         void softFlush();
00264                 void SetUpperLimit(long newUpperLimit) { upperBound=newUpperLimit; };
00265         
00276         void (*asyncLoadCompleteSignal)(RequestPtr, EntryPtr);
00277         
00285         void postEvent(RequestPtr request, EntryPtr entry);
00286 
00287     private:
00288         long upperBound;
00289 
00290         template <class SrcPixelType,
00291                   class DestIterator, class DestAccessor>
00292         static void importAndConvertImage(const vigra::ImageImportInfo& info,
00293                                    vigra::pair<DestIterator, DestAccessor> dest,
00294                                    const std::string& type);
00295         
00296     //    template <class SrcPixelType,
00297     //              class DestIterator, class DestAccessor>
00298     //    void importAndConvertGrayImage(const ImageImportInfo& info,
00299     //                                   vigra::pair<DestIterator, DestAccessor> dest,
00300     //                                   wxString type);
00301         
00302     //    template <class SrcPixelType,
00303     //              class DestIterator, class DestAccessor>
00304     //    void importAndConvertGrayAlphaImage(const ImageImportInfo & info,
00305     //                                        vigra::pair<DestIterator, DestAccessor> dest,
00306     //                                        wxString type);
00307         
00308         template <class SrcPixelType,
00309                   class DestIterator, class DestAccessor,
00310                   class MaskIterator, class MaskAccessor>
00311         static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
00312                                         vigra::pair<DestIterator, DestAccessor> dest,
00313                                         vigra::pair<MaskIterator, MaskAccessor> mask,
00314                                         const std::string & type);
00315         
00316         
00317     public:
00319         void setProgressDisplay(AppBase::ProgressDisplay* disp)
00320             { m_progress = disp; }
00321         
00323         void clearProgressDisplay(AppBase::ProgressDisplay* disp)
00324             { m_progress = NULL; }
00325         
00326         
00327     private:
00328         std::map<std::string, EntryPtr> images;
00329 
00330         // our progress display
00331         AppBase::ProgressDisplay* m_progress;
00332 
00333         int m_accessCounter;
00334         
00335         // Requests for full size images that need loading
00336         std::map<std::string, RequestPtr> m_requests;
00337         
00338         // Requests for small images that need generating.
00339         std::map<std::string, RequestPtr> m_smallRequests;
00340         
00342         void spawnAsyncThread();
00343         
00352         static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
00353         
00357         static EntryPtr loadImageSafely(const std::string & filename);
00358         
00363         static EntryPtr loadSmallImageSafely(EntryPtr entry);
00364         
00365     public:
00379     //    const vigra::BImage & getPyramidImage(const std::string& filename,
00380     //                                          int level);
00381 
00382     private:
00383         // key for your pyramid map.
00384         struct PyramidKey
00385         {
00386             std::string filename;
00387             int level;
00388             
00389             public:
00390                 PyramidKey(const std::string& str, int lv)
00391                   : filename(str), level(lv)
00392                 {};
00393             
00394                 std::string toString();
00395         };
00396         
00397         std::map<std::string, vigra::BImage *> pyrImages;
00398 };
00399 
00400 
00401 } //namespace
00402 #endif // _IMAGECACHE_H

Generated on 27 Aug 2015 for Hugintrunk by  doxygen 1.4.7