Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageCache.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
24 #ifndef _HUGINAPP_IMAGECACHE_H
25 #define _HUGINAPP_IMAGECACHE_H
26 
27 #include <hugin_shared.h>
28 #include "hugin_config.h"
29 #include <map>
30 #include <vector>
31 #include <memory>
32 #include <functional>
33 #include <vigra/stdimage.hxx>
34 #include <vigra/imageinfo.hxx>
35 #include <hugin_utils/utils.h>
37 
38 #define HUGIN_IMGCACHE_MAPPING_INTEGER 0l
39 #define HUGIN_IMGCACHE_MAPPING_FLOAT 1l
40 
41 
42 namespace HuginBase {
43 
53 {
54 
55  public:
57  typedef std::shared_ptr<vigra::BRGBImage> ImageCacheRGB8Ptr;
58  typedef std::shared_ptr<vigra::UInt16RGBImage> ImageCacheRGB16Ptr;
59  typedef std::shared_ptr<vigra::FRGBImage> ImageCacheRGBFloatPtr;
60  typedef std::shared_ptr<vigra::BImage> ImageCache8Ptr;
61  typedef std::shared_ptr<vigra::ImageImportInfo::ICCProfile> ImageCacheICCProfile;
62 
64  struct IMPEX Entry
65  {
71 
72  std::string origType;
74 
75  public:
78  : image8(ImageCacheRGB8Ptr(new vigra::BRGBImage)),
79  image16(ImageCacheRGB16Ptr(new vigra::UInt16RGBImage)),
80  imageFloat(ImageCacheRGBFloatPtr(new vigra::FRGBImage)),
81  mask(ImageCache8Ptr(new vigra::BImage)),
82  iccProfile(ImageCacheICCProfile(new vigra::ImageImportInfo::ICCProfile)),
83  lastAccess(0)
84  {
85  DEBUG_TRACE("Constructing an empty ImageCache::Entry");
86  };
87 
90  ImageCacheRGB16Ptr & img16,
91  ImageCacheRGBFloatPtr & imgFloat,
92  ImageCache8Ptr & imgMask,
93  ImageCacheICCProfile & ICCProfile,
94  const std::string & typ)
95  : image8(img), image16(img16), imageFloat(imgFloat), mask(imgMask),
96  iccProfile(ICCProfile), origType(typ), lastAccess(0)
97  {
98  DEBUG_TRACE("Constructing ImageCache::Entry");
99  };
100 
103  {
104  DEBUG_TRACE("Deleting ImageCacheEntry");
105  };
106 
108  ImageCacheRGB8Ptr get8BitImage(int desiredMapping = -1);
109  };
110 
112  typedef std::shared_ptr<Entry> EntryPtr;
113 
117  class Request
118  {
119  public:
120  Request(std::string filename, bool request_small)
121  :m_filename(filename), m_isSmall(request_small)
122  {};
131  std::vector <std::function<void(EntryPtr, std::string, bool)>> ready;
132  bool getIsSmall() const
133  {return m_isSmall;};
134  const std::string & getFilename() const
135  {return m_filename;};
136  protected:
137  std::string m_filename;
138  bool m_isSmall;
139  };
140 
151  typedef std::shared_ptr<Request> RequestPtr;
152 
153  private:
154  // ctor. private, nobody execpt us can create an instance.
156  : asyncLoadCompleteSignal(0), upperBound(100*1024*1024ull),
157  m_progress(NULL), m_accessCounter(0)
158  {};
159 
160  public:
163  virtual ~ImageCache()
164  {
165  images.clear();
166  instance = NULL;
167  }
168 
170  static ImageCache & getInstance();
171 
172  private:
174 
175 
176  public:
188  EntryPtr getImage(const std::string & filename);
189 
197  EntryPtr getImageIfAvailable(const std::string & filename);
198 
208  EntryPtr getSmallImage(const std::string & filename);
209 
219  EntryPtr getSmallImageIfAvailable(const std::string & filename);
220 
227  RequestPtr requestAsyncImage(const std::string & filename);
228 
235  RequestPtr requestAsyncSmallImage(const std::string & filename);
236 
240  void removeImage(const std::string & filename);
241 
247  void flush();
248 
253  void softFlush();
256  void SetUpperLimit(const unsigned long long newUpperLimit) { upperBound=newUpperLimit; };
257 
268  void (*asyncLoadCompleteSignal)(RequestPtr, EntryPtr);
269 
277  void postEvent(RequestPtr request, EntryPtr entry);
278 
284  void removeRequest(RequestPtr request);
285 
286  private:
287  unsigned long long upperBound;
288 
289  template <class SrcPixelType,
290  class DestIterator, class DestAccessor>
291  static void importAndConvertImage(const vigra::ImageImportInfo& info,
292  vigra::pair<DestIterator, DestAccessor> dest,
293  const std::string& type);
294  template <class SrcPixelType,
295  class DestIterator, class DestAccessor,
296  class MaskIterator, class MaskAccessor>
297  static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
298  vigra::pair<DestIterator, DestAccessor> dest,
299  vigra::pair<MaskIterator, MaskAccessor> mask,
300  const std::string & type);
301 
302 
303  public:
306  { m_progress = disp; }
307 
310  { m_progress = NULL; }
311 
312 
313  private:
314  std::map<std::string, EntryPtr> images;
315 
316  // our progress display
318 
320 
321  // Requests for full size images that need loading
322  std::map<std::string, RequestPtr> m_requests;
323 
324  // Requests for small images that need generating.
325  std::map<std::string, RequestPtr> m_smallRequests;
326 
328  void spawnAsyncThread();
329 
338  static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
339 
343  static EntryPtr loadImageSafely(const std::string & filename);
344 
349  static EntryPtr loadSmallImageSafely(EntryPtr entry);
350 
351  public:
365  // const vigra::BImage & getPyramidImage(const std::string& filename,
366  // int level);
367 
368  private:
369  // key for your pyramid map.
370  struct PyramidKey
371  {
372  std::string filename;
373  int level;
374 
375  public:
376  PyramidKey(const std::string& str, int lv)
377  : filename(str), level(lv)
378  {};
379 
380  std::string toString();
381  };
382 
383  std::map<std::string, vigra::BImage *> pyrImages;
384 };
385 
386 
387 } //namespace
388 #endif // _IMAGECACHE_H
void clearProgressDisplay(AppBase::ProgressDisplay *disp)
Definition: ImageCache.h:309
ImageCacheICCProfile iccProfile
Definition: ImageCache.h:70
AppBase::ProgressDisplay * m_progress
Definition: ImageCache.h:317
Request for an image to load Connect to the ready signal so when the image loads you can respond...
Definition: ImageCache.h:117
std::shared_ptr< vigra::FRGBImage > ImageCacheRGBFloatPtr
Definition: ImageCache.h:59
unsigned long long upperBound
Definition: ImageCache.h:287
#define DEBUG_TRACE(msg)
Definition: utils.h:67
std::shared_ptr< vigra::ImageImportInfo::ICCProfile > ImageCacheICCProfile
Definition: ImageCache.h:61
This is a cache for all the images we use.
Definition: ImageCache.h:52
std::map< std::string, RequestPtr > m_smallRequests
Definition: ImageCache.h:325
ImageCacheRGB8Ptr image8
Definition: ImageCache.h:66
information about an image inside the cache
Definition: ImageCache.h:64
std::shared_ptr< vigra::BImage > ImageCache8Ptr
Definition: ImageCache.h:60
virtual ~ImageCache()
dtor.
Definition: ImageCache.h:163
const std::string & getFilename() const
Definition: ImageCache.h:134
std::shared_ptr< Entry > EntryPtr
a shared pointer to the entry
Definition: ImageCache.h:112
std::shared_ptr< vigra::BRGBImage > ImageCacheRGB8Ptr
use reference counted pointers
Definition: ImageCache.h:57
get a pyramid image.
Definition: ImageCache.h:370
ImageCacheRGB16Ptr image16
Definition: ImageCache.h:67
std::map< std::string, RequestPtr > m_requests
Definition: ImageCache.h:322
std::shared_ptr< Request > RequestPtr
Reference counted request for an image to load.
Definition: ImageCache.h:151
static ImageCache * instance
Definition: ImageCache.h:173
ImageCacheRGBFloatPtr imageFloat
Definition: ImageCache.h:68
std::map< std::string, EntryPtr > images
Definition: ImageCache.h:314
#define IMPEX
Definition: hugin_shared.h:39
PyramidKey(const std::string &str, int lv)
Definition: ImageCache.h:376
void setProgressDisplay(AppBase::ProgressDisplay *disp)
Definition: ImageCache.h:305
Entry(ImageCacheRGB8Ptr &img, ImageCacheRGB16Ptr &img16, ImageCacheRGBFloatPtr &imgFloat, ImageCache8Ptr &imgMask, ImageCacheICCProfile &ICCProfile, const std::string &typ)
Definition: ImageCache.h:89
void SetUpperLimit(const unsigned long long newUpperLimit)
sets the upper limit, which is used by softFlush()
Definition: ImageCache.h:256
std::map< std::string, vigra::BImage * > pyrImages
Definition: ImageCache.h:383
Request(std::string filename, bool request_small)
Definition: ImageCache.h:120
static void info(const char *fmt,...)
Definition: svm.cpp:95
std::shared_ptr< vigra::UInt16RGBImage > ImageCacheRGB16Ptr
Definition: ImageCache.h:58