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 <memory>
00032 #include <functional>
00033 #include <vigra/stdimage.hxx>
00034 #include <vigra/imageinfo.hxx>
00035 #include <hugin_utils/utils.h>
00036 #include <appbase/ProgressDisplay.h>
00037 
00038 #define HUGIN_IMGCACHE_MAPPING_INTEGER        0l
00039 #define HUGIN_IMGCACHE_MAPPING_FLOAT          1l
00040 
00041 
00042 namespace HuginBase {
00043     
00052 class IMPEX ImageCache
00053 {
00054 
00055     public:
00057         typedef std::shared_ptr<vigra::BRGBImage> ImageCacheRGB8Ptr;
00058         typedef std::shared_ptr<vigra::UInt16RGBImage> ImageCacheRGB16Ptr;
00059         typedef std::shared_ptr<vigra::FRGBImage> ImageCacheRGBFloatPtr;
00060         typedef std::shared_ptr<vigra::BImage> ImageCache8Ptr;
00061         typedef std::shared_ptr<vigra::ImageImportInfo::ICCProfile> ImageCacheICCProfile;
00062 
00064         struct IMPEX Entry
00065         {
00066             ImageCacheRGB8Ptr image8;
00067             ImageCacheRGB16Ptr image16;
00068             ImageCacheRGBFloatPtr imageFloat;
00069             ImageCache8Ptr mask;
00070             ImageCacheICCProfile iccProfile;
00071 
00072             std::string origType;
00073             int lastAccess;
00074 
00075             public:
00077                 Entry()
00078                   : image8(ImageCacheRGB8Ptr(new vigra::BRGBImage)),
00079                     image16(ImageCacheRGB16Ptr(new vigra::UInt16RGBImage)),
00080                     imageFloat(ImageCacheRGBFloatPtr(new vigra::FRGBImage)),
00081                     mask(ImageCache8Ptr(new vigra::BImage)),
00082                     iccProfile(ImageCacheICCProfile(new vigra::ImageImportInfo::ICCProfile)),
00083                     lastAccess(0)
00084                 {
00085                       DEBUG_TRACE("Constructing an empty ImageCache::Entry");
00086                 };
00087 
00089                 Entry(ImageCacheRGB8Ptr & img, 
00090                       ImageCacheRGB16Ptr & img16,
00091                       ImageCacheRGBFloatPtr & imgFloat,
00092                       ImageCache8Ptr & imgMask,
00093                       ImageCacheICCProfile & ICCProfile,
00094                       const std::string & typ)
00095                   : image8(img), image16(img16), imageFloat(imgFloat), mask(imgMask),
00096                     iccProfile(ICCProfile), origType(typ), lastAccess(0)
00097                 { 
00098                         DEBUG_TRACE("Constructing ImageCache::Entry");
00099                 };
00100 
00102                 ~Entry()
00103                 {
00104                     DEBUG_TRACE("Deleting ImageCacheEntry");
00105                 };
00106 
00108                 ImageCacheRGB8Ptr get8BitImage();
00109         };
00110 
00112         typedef std::shared_ptr<Entry> EntryPtr;
00113         
00117         class Request
00118         {
00119             public:
00120                 Request(std::string filename, bool request_small)
00121                     :m_filename(filename), m_isSmall(request_small)
00122                     {};
00131                 std::vector <std::function<void(EntryPtr, std::string, bool)>> ready;
00132                 bool getIsSmall() const
00133                     {return m_isSmall;};
00134                 const std::string & getFilename() const
00135                     {return m_filename;};
00136             protected:
00137                 std::string m_filename;
00138                 bool m_isSmall;
00139         };
00140         
00151         typedef std::shared_ptr<Request> RequestPtr;
00152 
00153     private:
00154         // ctor. private, nobody execpt us can create an instance.
00155         ImageCache()
00156             : asyncLoadCompleteSignal(0), upperBound(100*1024*1024ull),
00157               m_progress(NULL), m_accessCounter(0)
00158         {};
00159         
00160     public:
00163         virtual ~ImageCache()
00164         {
00165                 images.clear();
00166                 instance = NULL;
00167         }
00168 
00170         static ImageCache & getInstance();
00171         
00172     private:
00173         static ImageCache* instance;
00174 
00175         
00176     public:
00188         EntryPtr getImage(const std::string & filename);
00189         
00197         EntryPtr getImageIfAvailable(const std::string & filename);
00198 
00208         EntryPtr getSmallImage(const std::string & filename);
00209         
00219         EntryPtr getSmallImageIfAvailable(const std::string & filename);
00220          
00227         RequestPtr requestAsyncImage(const std::string & filename);
00228         
00235         RequestPtr requestAsyncSmallImage(const std::string & filename);
00236 
00240         void removeImage(const std::string & filename);
00241 
00247         void flush();
00248 
00253         void softFlush();
00256                 void SetUpperLimit(const unsigned long long newUpperLimit) { upperBound=newUpperLimit; };
00257         
00268         void (*asyncLoadCompleteSignal)(RequestPtr, EntryPtr);
00269         
00277         void postEvent(RequestPtr request, EntryPtr entry);
00278         
00284         void removeRequest(RequestPtr request);
00285 
00286     private:
00287         unsigned long long upperBound;
00288 
00289         template <class SrcPixelType,
00290                   class DestIterator, class DestAccessor>
00291         static void importAndConvertImage(const vigra::ImageImportInfo& info,
00292                                    vigra::pair<DestIterator, DestAccessor> dest,
00293                                    const std::string& type);
00294         
00295     //    template <class SrcPixelType,
00296     //              class DestIterator, class DestAccessor>
00297     //    void importAndConvertGrayImage(const ImageImportInfo& info,
00298     //                                   vigra::pair<DestIterator, DestAccessor> dest,
00299     //                                   wxString type);
00300         
00301     //    template <class SrcPixelType,
00302     //              class DestIterator, class DestAccessor>
00303     //    void importAndConvertGrayAlphaImage(const ImageImportInfo & info,
00304     //                                        vigra::pair<DestIterator, DestAccessor> dest,
00305     //                                        wxString type);
00306         
00307         template <class SrcPixelType,
00308                   class DestIterator, class DestAccessor,
00309                   class MaskIterator, class MaskAccessor>
00310         static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
00311                                         vigra::pair<DestIterator, DestAccessor> dest,
00312                                         vigra::pair<MaskIterator, MaskAccessor> mask,
00313                                         const std::string & type);
00314         
00315         
00316     public:
00318         void setProgressDisplay(AppBase::ProgressDisplay* disp)
00319             { m_progress = disp; }
00320         
00322         void clearProgressDisplay(AppBase::ProgressDisplay* disp)
00323             { m_progress = NULL; }
00324         
00325         
00326     private:
00327         std::map<std::string, EntryPtr> images;
00328 
00329         // our progress display
00330         AppBase::ProgressDisplay* m_progress;
00331 
00332         int m_accessCounter;
00333         
00334         // Requests for full size images that need loading
00335         std::map<std::string, RequestPtr> m_requests;
00336         
00337         // Requests for small images that need generating.
00338         std::map<std::string, RequestPtr> m_smallRequests;
00339         
00341         void spawnAsyncThread();
00342         
00351         static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
00352         
00356         static EntryPtr loadImageSafely(const std::string & filename);
00357         
00362         static EntryPtr loadSmallImageSafely(EntryPtr entry);
00363         
00364     public:
00378     //    const vigra::BImage & getPyramidImage(const std::string& filename,
00379     //                                          int level);
00380 
00381     private:
00382         // key for your pyramid map.
00383         struct PyramidKey
00384         {
00385             std::string filename;
00386             int level;
00387             
00388             public:
00389                 PyramidKey(const std::string& str, int lv)
00390                   : filename(str), level(lv)
00391                 {};
00392             
00393                 std::string toString();
00394         };
00395         
00396         std::map<std::string, vigra::BImage *> pyrImages;
00397 };
00398 
00399 
00400 } //namespace
00401 #endif // _IMAGECACHE_H

Generated on 5 Dec 2016 for Hugintrunk by  doxygen 1.4.7