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();
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 
295  // template <class SrcPixelType,
296  // class DestIterator, class DestAccessor>
297  // void importAndConvertGrayImage(const ImageImportInfo& info,
298  // vigra::pair<DestIterator, DestAccessor> dest,
299  // wxString type);
300 
301  // template <class SrcPixelType,
302  // class DestIterator, class DestAccessor>
303  // void importAndConvertGrayAlphaImage(const ImageImportInfo & info,
304  // vigra::pair<DestIterator, DestAccessor> dest,
305  // wxString type);
306 
307  template <class SrcPixelType,
308  class DestIterator, class DestAccessor,
309  class MaskIterator, class MaskAccessor>
310  static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
311  vigra::pair<DestIterator, DestAccessor> dest,
312  vigra::pair<MaskIterator, MaskAccessor> mask,
313  const std::string & type);
314 
315 
316  public:
319  { m_progress = disp; }
320 
323  { m_progress = NULL; }
324 
325 
326  private:
327  std::map<std::string, EntryPtr> images;
328 
329  // our progress display
331 
333 
334  // Requests for full size images that need loading
335  std::map<std::string, RequestPtr> m_requests;
336 
337  // Requests for small images that need generating.
338  std::map<std::string, RequestPtr> m_smallRequests;
339 
341  void spawnAsyncThread();
342 
351  static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
352 
356  static EntryPtr loadImageSafely(const std::string & filename);
357 
362  static EntryPtr loadSmallImageSafely(EntryPtr entry);
363 
364  public:
378  // const vigra::BImage & getPyramidImage(const std::string& filename,
379  // int level);
380 
381  private:
382  // key for your pyramid map.
383  struct PyramidKey
384  {
385  std::string filename;
386  int level;
387 
388  public:
389  PyramidKey(const std::string& str, int lv)
390  : filename(str), level(lv)
391  {};
392 
393  std::string toString();
394  };
395 
396  std::map<std::string, vigra::BImage *> pyrImages;
397 };
398 
399 
400 } //namespace
401 #endif // _IMAGECACHE_H
void clearProgressDisplay(AppBase::ProgressDisplay *disp)
Definition: ImageCache.h:322
ImageCacheICCProfile iccProfile
Definition: ImageCache.h:70
AppBase::ProgressDisplay * m_progress
Definition: ImageCache.h:330
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:338
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:383
ImageCacheRGB16Ptr image16
Definition: ImageCache.h:67
std::map< std::string, RequestPtr > m_requests
Definition: ImageCache.h:335
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:327
#define IMPEX
Definition: hugin_shared.h:39
PyramidKey(const std::string &str, int lv)
Definition: ImageCache.h:389
void setProgressDisplay(AppBase::ProgressDisplay *disp)
Definition: ImageCache.h:318
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:396
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