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 
00279     private:
00280         unsigned long long upperBound;
00281 
00282         template <class SrcPixelType,
00283                   class DestIterator, class DestAccessor>
00284         static void importAndConvertImage(const vigra::ImageImportInfo& info,
00285                                    vigra::pair<DestIterator, DestAccessor> dest,
00286                                    const std::string& type);
00287         
00288     //    template <class SrcPixelType,
00289     //              class DestIterator, class DestAccessor>
00290     //    void importAndConvertGrayImage(const ImageImportInfo& info,
00291     //                                   vigra::pair<DestIterator, DestAccessor> dest,
00292     //                                   wxString type);
00293         
00294     //    template <class SrcPixelType,
00295     //              class DestIterator, class DestAccessor>
00296     //    void importAndConvertGrayAlphaImage(const ImageImportInfo & info,
00297     //                                        vigra::pair<DestIterator, DestAccessor> dest,
00298     //                                        wxString type);
00299         
00300         template <class SrcPixelType,
00301                   class DestIterator, class DestAccessor,
00302                   class MaskIterator, class MaskAccessor>
00303         static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
00304                                         vigra::pair<DestIterator, DestAccessor> dest,
00305                                         vigra::pair<MaskIterator, MaskAccessor> mask,
00306                                         const std::string & type);
00307         
00308         
00309     public:
00311         void setProgressDisplay(AppBase::ProgressDisplay* disp)
00312             { m_progress = disp; }
00313         
00315         void clearProgressDisplay(AppBase::ProgressDisplay* disp)
00316             { m_progress = NULL; }
00317         
00318         
00319     private:
00320         std::map<std::string, EntryPtr> images;
00321 
00322         // our progress display
00323         AppBase::ProgressDisplay* m_progress;
00324 
00325         int m_accessCounter;
00326         
00327         // Requests for full size images that need loading
00328         std::map<std::string, RequestPtr> m_requests;
00329         
00330         // Requests for small images that need generating.
00331         std::map<std::string, RequestPtr> m_smallRequests;
00332         
00334         void spawnAsyncThread();
00335         
00344         static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
00345         
00349         static EntryPtr loadImageSafely(const std::string & filename);
00350         
00355         static EntryPtr loadSmallImageSafely(EntryPtr entry);
00356         
00357     public:
00371     //    const vigra::BImage & getPyramidImage(const std::string& filename,
00372     //                                          int level);
00373 
00374     private:
00375         // key for your pyramid map.
00376         struct PyramidKey
00377         {
00378             std::string filename;
00379             int level;
00380             
00381             public:
00382                 PyramidKey(const std::string& str, int lv)
00383                   : filename(str), level(lv)
00384                 {};
00385             
00386                 std::string toString();
00387         };
00388         
00389         std::map<std::string, vigra::BImage *> pyrImages;
00390 };
00391 
00392 
00393 } //namespace
00394 #endif // _IMAGECACHE_H

Generated on 26 Jul 2016 for Hugintrunk by  doxygen 1.4.7