[37ffb3]: src / hugin_base / huginapp / ImageCache.h  Maximize  Restore  History

Download this file

397 lines (339 with data), 15.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// -*- c-basic-offset: 4 -*-
/** @file ImageCache.h
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef _HUGINAPP_IMAGECACHE_H
#define _HUGINAPP_IMAGECACHE_H
#include <hugin_shared.h>
#include <map>
#include <boost/version.hpp>
#include <boost/shared_ptr.hpp>
#if BOOST_VERSION>=105400
#include <boost/signals2.hpp>
#else
#include <boost/signal.hpp>
#endif
#include <vigra/stdimage.hxx>
#include <vigra/imageinfo.hxx>
#include <hugin_utils/utils.h>
#include <appbase/ProgressDisplayOld.h>
#define HUGIN_IMGCACHE_MAPPING_INTEGER 0l
#define HUGIN_IMGCACHE_MAPPING_FLOAT 1l
namespace HuginBase {
/** This is a cache for all the images we use.
*
* is a singleton for easy access from everywhere.
* The cache is used as an image source, that needs
* to know how to reproduce the requested images, in case
* that they have been deleted.
*
*/
class IMPEX ImageCache
{
public:
/// use reference counted pointers
typedef boost::shared_ptr<vigra::BRGBImage> ImageCacheRGB8Ptr;
typedef boost::shared_ptr<vigra::UInt16RGBImage> ImageCacheRGB16Ptr;
typedef boost::shared_ptr<vigra::FRGBImage> ImageCacheRGBFloatPtr;
typedef boost::shared_ptr<vigra::BImage> ImageCache8Ptr;
/** information about an image inside the cache */
struct IMPEX Entry
{
ImageCacheRGB8Ptr image8;
ImageCacheRGB16Ptr image16;
ImageCacheRGBFloatPtr imageFloat;
ImageCache8Ptr mask;
std::string origType;
int lastAccess;
public:
///
Entry()
: image8(ImageCacheRGB8Ptr(new vigra::BRGBImage)),
image16(ImageCacheRGB16Ptr(new vigra::UInt16RGBImage)),
imageFloat(ImageCacheRGBFloatPtr(new vigra::FRGBImage)),
mask(ImageCache8Ptr(new vigra::BImage))
{
DEBUG_TRACE("Constructing an empty ImageCache::Entry");
};
///
Entry(ImageCacheRGB8Ptr & img,
ImageCacheRGB16Ptr & img16,
ImageCacheRGBFloatPtr & imgFloat,
ImageCache8Ptr & imgMask,
const std::string & typ)
: image8(img), image16(img16), imageFloat(imgFloat), mask(imgMask), origType(typ), lastAccess(0)
{
DEBUG_TRACE("Constructing ImageCache::Entry");
};
///
~Entry()
{
DEBUG_TRACE("Deleting ImageCacheEntry");
};
///
ImageCacheRGB8Ptr get8BitImage();
};
/** a shared pointer to the entry */
typedef boost::shared_ptr<Entry> EntryPtr;
/** Request for an image to load
* Connect to the ready signal so when the image loads you can respond.
*/
class Request
{
public:
Request(std::string filename, bool request_small)
:m_filename(filename), m_isSmall(request_small)
{};
/** Signal that fires when the image is loaded.
* Function must return void and have three arguments: EntryPtr
* for the requested image, std::string for the filename, and a
* bool that is true iff this is a small image.
*
* The image could be freed after the signal fires, but keeping
* the EntryPtr prevents this.
*/
#if BOOST_VERSION>=105400
boost::signals2::signal<void(EntryPtr, std::string, bool)> ready;
#else
boost::signal<void(EntryPtr, std::string, bool)> ready;
#endif
bool getIsSmall() const
{return m_isSmall;};
const std::string & getFilename() const
{return m_filename;};
protected:
std::string m_filename;
bool m_isSmall;
};
/** Reference counted request for an image to load.
* Hold on to this when you want an image to load. If you no longer
* want the image, just delete it. Deleting it before the image loads
* lets other images load next.
*
* Connect to the ready signal to respond to the image loading. To keep
* the image loaded, keep the EntryPtr given to the signal handler.
*
* It is reference counted, so you can freely copy and delete it.
*/
typedef boost::shared_ptr<Request> RequestPtr;
private:
// ctor. private, nobody execpt us can create an instance.
ImageCache()
: m_progress(NULL), m_accessCounter(0),
asyncLoadCompleteSignal(0), upperBound(100*1024*1024l)
{};
public:
/** dtor.
*/
virtual ~ImageCache()
{
images.clear();
instance = NULL;
}
/** get the global ImageCache object */
static ImageCache & getInstance();
private:
static ImageCache* instance;
public:
/** get a image.
*
* it will be loaded if its not already in the cache
*
* Hold the EntryPtr as long as the image data is needed!
*
* If it isn't vital that the real image is obtained immediately, use
* getImageIfAvailable instead. This means you can keep the UI
* responsive while the real image is fetched from a disk or network
* and decoded.
*/
EntryPtr getImage(const std::string & filename);
/** Get an image if already loaded.
* If not already in the cache, the pointer returned is 0.
*
* Hold a non-zero EntryPtr as long as the image data is needed.
*
* If you really need the image immediately, use getImage() instead.
*/
EntryPtr getImageIfAvailable(const std::string & filename);
/** get an small image.
*
* This image is 512x512 pixel maximum and can be used for icons
* and different previews. It is directly derived from the original.
*
* If it isn't vital that the real image is obtained immediately, use
* getSmallImageIfAvailable instead. This means you can keep the UI
* responsive while the real image is fetched, decoded, and scaled.
*/
EntryPtr getSmallImage(const std::string & filename);
/** Get a small image if already loaded.
* The EntryPtr returned is 0 if the image isn't loaded yet.
*
* This image is 512x512 pixels maximum and can be used for icons
* and different previews. It is directly derived from the original.
*
* If you really need the image immediately, use getSmallImage()
* instead.
*/
EntryPtr getSmallImageIfAvailable(const std::string & filename);
/** Request an image be loaded.
* This function returns quickly even when the image is not cached.
*
* @return Object to keep while you want the image. Connect to its
* ready signal to be notified when the image is ready.
*/
RequestPtr requestAsyncImage(const std::string & filename);
/** Request a small image be loaded.
* This function returns quickly even when the image is not cached.
*
* @return Object to keep while you want the image. Connect to its
* ready signal to be notified when it is ready.
*/
RequestPtr requestAsyncSmallImage(const std::string & filename);
/** remove a specific image (and dependant images)
* from the cache
*/
void removeImage(const std::string & filename);
/** release all images in the cache.
*
* useful on project load, or maybe before stitching really
* big pictures
*/
void flush();
/** a soft version of flush.
*
* Releases some images if they go over a certain threshold
*/
void softFlush();
/** sets the upper limit, which is used by softFlush()
*/
void SetUpperLimit(long newUpperLimit) { upperBound=newUpperLimit; };
/** Signal for when a asynchronous load completes.
* If you use the requestAsync functions, ensure there is something
* connected to this signal. The signal is raised in another thread,
* so the handler must be thread safe.
*
* The signal handler must pass the request and entry to postEvent from
* the main thread when it is safe. For example, it you can wrap the
* request and entry in some wxEvent and the main thread can handle it
* later.
*/
void (*asyncLoadCompleteSignal)(RequestPtr, EntryPtr);
/** Pass on a loaded event for any images loaded asynchronously.
* Call from the main GUI thread when an ImageLoadedEvent occurs.
* The ImageLoadedEvent originates from async_load_thread.
*
* @param request The RequestPtr from the ImageLoadedEvent.
* @param entry the EntryPtr from the ImageLoadedEvent.
*/
void postEvent(RequestPtr request, EntryPtr entry);
private:
long upperBound;
template <class SrcPixelType,
class DestIterator, class DestAccessor>
static void importAndConvertImage(const vigra::ImageImportInfo& info,
vigra::pair<DestIterator, DestAccessor> dest,
const std::string& type);
// template <class SrcPixelType,
// class DestIterator, class DestAccessor>
// void importAndConvertGrayImage(const ImageImportInfo& info,
// vigra::pair<DestIterator, DestAccessor> dest,
// wxString type);
// template <class SrcPixelType,
// class DestIterator, class DestAccessor>
// void importAndConvertGrayAlphaImage(const ImageImportInfo & info,
// vigra::pair<DestIterator, DestAccessor> dest,
// wxString type);
template <class SrcPixelType,
class DestIterator, class DestAccessor,
class MaskIterator, class MaskAccessor>
static void importAndConvertAlphaImage(const vigra::ImageImportInfo & info,
vigra::pair<DestIterator, DestAccessor> dest,
vigra::pair<MaskIterator, MaskAccessor> mask,
const std::string & type);
public:
///
void setProgressDisplay(AppBase::MultiProgressDisplay* disp)
{ m_progress = disp; }
///
void clearProgressDisplay(AppBase::MultiProgressDisplay* disp)
{ m_progress = NULL; }
private:
std::map<std::string, EntryPtr> images;
// our progress display
AppBase::MultiProgressDisplay* m_progress;
int m_accessCounter;
// Requests for full size images that need loading
std::map<std::string, RequestPtr> m_requests;
// Requests for small images that need generating.
std::map<std::string, RequestPtr> m_smallRequests;
/// Start a background thread to load an image.
void spawnAsyncThread();
/** Load a requested image in a way that will work in parallel.
* When done, it sends an event with the newly created EntryPtr and
* request.
* @param RequestPtr request for the image to load.
* @param large EntryPtr for the large image when a small image is to
* be generated from it. Use a 0 pointer (the default) to
* generate a full size image.
*/
static void loadSafely(RequestPtr request, EntryPtr large = EntryPtr());
/** Load a full size image, in a way that will work in parallel.
* If the image cannot be loaded, the pointer returned is 0.
*/
static EntryPtr loadImageSafely(const std::string & filename);
/** Load a small image, in a way that will work in parallel.
* If the image cannot be loaded, the pointer returned is 0.
* @param entry Large image to scale down.
*/
static EntryPtr loadSmallImageSafely(EntryPtr entry);
public:
/** get a pyramid image.
*
* A image pyramid is a image in multiple resolutions.
* Usually it is used to accelerate image processing, by using
* lower resolutions first. they are properly low pass filtered,
* so no undersampling occurs (it would if one just takes
* every 2^level pixel instead).
*
* @param filename of source image
* @param level of pyramid. height and width are calculated as
* follows: height/(level^2), width/(level^1)
*
*/
// const vigra::BImage & getPyramidImage(const std::string& filename,
// int level);
private:
// key for your pyramid map.
struct PyramidKey
{
std::string filename;
int level;
public:
PyramidKey(const std::string& str, int lv)
: filename(str), level(lv)
{};
std::string toString()
{ return filename + hugin_utils::lexical_cast<std::string>(level); }
};
std::map<std::string, vigra::BImage *> pyrImages;
};
} //namespace
#endif // _IMAGECACHE_H

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks