OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimCacheTileSource.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: MIT
4 //
5 // Author: Garrett Potts
6 //
7 // Description: ossimCacheTileSource
8 //
9 //*******************************************************************
10 // $Id$
11 
12 #include <ossim/base/ossimTrace.h>
13 #include <ossim/base/ossimNotify.h>
14 #include <ossim/base/ossimString.h>
22 
23 static ossimTrace traceDebug("ossimCacheTileSource:debug");
24 
25 static const ossimString TILE_SIZE_XY_KW("tile_size_xy");
26 static const ossimString USE_INPUT_TILE_SIZE_KW("use_input_tile_size");
27 
28 RTTI_DEF1(ossimCacheTileSource, "ossimCacheTileSource", ossimImageSourceFilter);
29 
32  //theCacheId(ossimAppFixedTileCache::instance()->newTileCache()),
33  theTile(0),
34  theFixedTileSize(),
35  theCachingEnabled(true),
36  theEventProgressFlag(false),
37  theUseInputTileSizeFlag(false),
38  theTileSizeXY()
39 {
42 }
43 
45 {
47  //ossimAppFixedTileCache::instance()->deleteCache(theCacheId);
48 
49  //theCacheId = -1;
50  // theBoundingRect.makeNan();
51 }
52 
54 {
55  //ossimAppFixedTileCache::instance()->flush(theCacheId);
56  ossim_uint32 idx = 0;
57  for(idx = 0; idx < theRLevelCacheList.size();++idx)
58  {
60  }
61 
62 }
63 
65 {
67  flush();
68  theTile = 0;
69 }
70 
72 {
73  theTile = 0;
75  {
78  }
79 }
80 
82  const ossimIrect& tileRect, ossim_uint32 resLevel)
83 {
84  if(traceDebug())
85  {
86  ossimNotify(ossimNotifyLevel_DEBUG) << "CACHE TILE START: " << tileRect << std::endl;
87  }
88  ossimRefPtr<ossimImageData> result = 0;
89 
90  if ( theInputConnection )
91  {
92  if ( isSourceEnabled() )
93  {
94  if(!theTile.valid())
95  {
96  allocate();
97  }
98 
99  if (theTile.valid())
100  {
101  // theTile->setImageRectangle(tileRect);
102  // theTile->makeBlank();
103 
104  // see if we can get a valid cache at the given resolution level
106  if( cacheId < 0)
107  {
108  return theInputConnection->getTile(tileRect, resLevel);
109  }
110  result = fillTile(tileRect, resLevel, cacheId);
111  }
112  }
113  else // Not enabled...
114  {
115  result = theInputConnection->getTile(tileRect, resLevel);
116  }
117 
118  } // End: if ( theInputConnection )
119  if(traceDebug())
120  {
121  ossimNotify(ossimNotifyLevel_DEBUG) << "CACHE TILE END: " << tileRect << std::endl;
122  }
123 
124  return result;
125 }
126 
127 #if 0
129  ossim_uint32 resLevel)
130 {
131  ossimRefPtr<ossimImageData> tempTile = 0;
132  fireProgressEvent(0.0);
133  ossimIrect boundingRect = getBoundingRect(resLevel);
134  ossimIrect tileRect = theTile->getImageRectangle();
135  ossimIrect allignedRect = tileRect.clipToRect(boundingRect);
137  ossimIpt cacheTileSize = ossimAppFixedTileCache::instance()->getTileSize(cacheId);
138  if ( !allignedRect.hasNans() )
139  {
140  allignedRect.stretchToTileBoundary(cacheTileSize);
141  // ossimAppFixedTileCache::ossimAppFixedCacheId cacheId = theCacheId;
142 
143  // check to see if we need to loop
144  if((allignedRect == tileRect)&&
145  (static_cast<ossim_int32>(tileRect.width()) == cacheTileSize.x)&&
146  (static_cast<ossim_int32>(tileRect.height()) == cacheTileSize.y))
147  {
148  ossimIpt origin = tileRect.ul();
150  {
151  tempTile = ossimAppFixedTileCache::instance()->getTile(cacheId,
152  origin);
153  }
154  if(!tempTile.valid())
155  {
156  tempTile = theInputConnection->getTile(tileRect, resLevel);
157 
158  if(tempTile.valid())
159  {
160  if((tempTile->getBuf())&&
161  (tempTile->getDataObjectStatus()!=OSSIM_EMPTY)&&
163  {
165  tempTile);
166  }
167  }
168  }
169 // else
170 // {
171 // std::cout << "FOUND IN CACHE AT RES " << resLevel << "\n";
172 // }
173 
174  if(tempTile.valid())
175  {
176  if((tempTile->getDataObjectStatus() != OSSIM_NULL)&&
177  (tempTile->getDataObjectStatus() != OSSIM_EMPTY))
178  {
180  theTile->loadTile(tempTile.get());
181  }
182  }
183  fireProgressEvent(100.0);
184  }
185  else
186  {
187 
188  ossim_int32 boundaryHeight = allignedRect.height();
189  ossim_int32 boundaryWidth = allignedRect.width();
190  ossimIpt origin(allignedRect.ul());
191  ossim_int32 totalTiles = (boundaryHeight/cacheTileSize.y)*
192  (boundaryWidth/theFixedTileSize.x);
193  ossim_int32 currentTile = 0;
194  for(ossim_int32 row = 0;
195  row < boundaryHeight;
196  row+=theFixedTileSize.y)
197  {
198  origin.x = allignedRect.ul().x;
199  for(ossim_int32 col = 0;
200  col < boundaryWidth;
201  col+=theFixedTileSize.x)
202  {
203  ++currentTile;
205  {
206  tempTile =
208  origin);
209  }
210  else
211  {
212  tempTile = 0;
213  }
214  if(!tempTile.valid())
215  {
216  ossimIrect rect(origin.x,
217  origin.y,
218  origin.x + cacheTileSize.x-1,
219  origin.y + cacheTileSize.y-1);
220 
221  tempTile = theInputConnection->getTile(rect, resLevel);
222 
223  if(tempTile.valid())
224  {
225  // drb if(theTile->getBuf()&&
226  if(tempTile->getBuf()&&
227  (tempTile->getDataObjectStatus()!=OSSIM_EMPTY)&&
229  {
231  addTile(cacheId, tempTile);
232  }
233  }
234  }
235  // else
236  // {
237  // std::cout << "FOUND IN CACHE AT RES " << resLevel << "\n";
238  // }
239 
240  if(tempTile.valid())
241  {
242  if(tempTile->getBuf()&&
243  (tempTile->getDataObjectStatus()!=OSSIM_EMPTY))
244  {
245  theTile->loadTile(tempTile.get());
246  }
247  }
248  double percent = 100.0*((double)currentTile/(double)totalTiles);
249  fireProgressEvent(percent);
250  origin.x += theFixedTileSize.x;
251  }
252  origin.y += theFixedTileSize.y;
253  }
254  theTile->validate();
255  fireProgressEvent(100);
256  }
257 
258  } // End of: if ( !allignedRect.hasNans() )
259 
260  return theTile;
261 }
262 #endif
263 
265  const ossimIrect& tileRect, ossim_uint32 resLevel,
267 {
268  ossimRefPtr<ossimImageData> result = 0;
269  ossimRefPtr<ossimImageData> tempTile = 0;
270  fireProgressEvent(0.0);
271  ossimIrect boundingRect = getBoundingRect(resLevel);
272  ossimIrect allignedRect = tileRect.clipToRect(boundingRect);
273  ossimIpt cacheTileSize = ossimAppFixedTileCache::instance()->getTileSize(cacheId);
274 
275  if ( !allignedRect.hasNans() )
276  {
277  allignedRect.stretchToTileBoundary(cacheTileSize);
278  // ossimAppFixedTileCache::ossimAppFixedCacheId cacheId = theCacheId;
279 
280  // check to see if we need to loop
281  if((allignedRect == tileRect)&&
282  (static_cast<ossim_int32>(tileRect.width()) == cacheTileSize.x)&&
283  (static_cast<ossim_int32>(tileRect.height()) == cacheTileSize.y))
284  {
285  // Grabbing whole tile either from cache or input.
286  ossimIpt origin = tileRect.ul();
288  {
289  result = ossimAppFixedTileCache::instance()->getTile(cacheId, origin);
290  }
291  if(!result.valid())
292  {
293  result = theInputConnection->getTile(tileRect, resLevel);
294 
295  if(result.valid())
296  {
297  if((result->getBuf())&&
298  (result->getDataObjectStatus()!=OSSIM_EMPTY)&&
300  {
301  ossimAppFixedTileCache::instance()->addTile(cacheId, result);
302  }
303  }
304  }
305 // else
306 // {
307 // std::cout << "FOUND IN CACHE AT RES " << resLevel << "\n";
308 // }
309 
310 #if 0
311  if(tempTile.valid())
312  {
313  if((tempTile->getDataObjectStatus() != OSSIM_NULL)&&
314  (tempTile->getDataObjectStatus() != OSSIM_EMPTY))
315  {
317  theTile->loadTile(tempTile.get());
318  }
319  }
320 #endif
321  fireProgressEvent(100.0);
322  }
323  else
324  {
325  theTile->setImageRectangle(tileRect);
326  theTile->makeBlank();
327 
328  ossim_int32 boundaryHeight = allignedRect.height();
329  ossim_int32 boundaryWidth = allignedRect.width();
330  ossimIpt origin(allignedRect.ul());
331  ossim_int32 totalTiles = (boundaryHeight/cacheTileSize.y)*
332  (boundaryWidth/theFixedTileSize.x);
333  ossim_int32 currentTile = 0;
334  for(ossim_int32 row = 0;
335  row < boundaryHeight;
336  row+=theFixedTileSize.y)
337  {
338  origin.x = allignedRect.ul().x;
339  for(ossim_int32 col = 0;
340  col < boundaryWidth;
341  col+=theFixedTileSize.x)
342  {
343  ++currentTile;
345  {
346  tempTile =
348  origin);
349  }
350  else
351  {
352  tempTile = 0;
353  }
354  if(!tempTile.valid())
355  {
356  ossimIrect rect(origin.x,
357  origin.y,
358  origin.x + cacheTileSize.x-1,
359  origin.y + cacheTileSize.y-1);
360 
361  tempTile = theInputConnection->getTile(rect, resLevel);
362 
363  if(tempTile.valid())
364  {
365  // drb if(theTile->getBuf()&&
366  if(tempTile->getBuf()&&
367  (tempTile->getDataObjectStatus()!=OSSIM_EMPTY)&&
369  {
371  addTile(cacheId, tempTile);
372  }
373  }
374  }
375  // else
376  // {
377  // std::cout << "FOUND IN CACHE AT RES " << resLevel << "\n";
378  // }
379 
380  if(tempTile.valid())
381  {
382  if(tempTile->getBuf()&&
383  (tempTile->getDataObjectStatus()!=OSSIM_EMPTY))
384  {
385  theTile->loadTile(tempTile.get());
386  }
387  }
388  double percent = 100.0*((double)currentTile/(double)totalTiles);
389  fireProgressEvent(percent);
390  origin.x += theFixedTileSize.x;
391  }
392  origin.y += theFixedTileSize.y;
393  }
394  theTile->validate();
395  result = theTile;
396  fireProgressEvent(100);
397  }
398 
399  } // End of: if ( !allignedRect.hasNans() )
400  else
401  {
402  theTile->setImageRectangle(tileRect);
403  theTile->makeBlank();
404  result = theTile;
405  }
406 
407  return result;
408 }
409 
411 {
412  return theFixedTileSize.x;
413 }
414 
416 {
417  return theFixedTileSize.y;
418 }
419 
421  const char* prefix)
422 {
423  //ossimAppFixedTileCache::instance()->deleteCache(theCacheId);
424 
425  //theCacheId = ossimAppFixedTileCache::instance()->newTileCache();
426 
428  const char* lookup = kwl.find(prefix, ossimKeywordNames::ENABLE_CACHE_KW);
429  if(lookup)
430  {
432  }
433  lookup = kwl.find(prefix, USE_INPUT_TILE_SIZE_KW);
434  if(lookup)
435  {
437  }
438 
439  lookup = kwl.find(prefix, TILE_SIZE_XY_KW);
440  if (lookup)
441  {
442  ossimIpt pt;
443  pt.toPoint(std::string(lookup));
444  setTileSize(pt);
445  theTileSizeXY = pt;
446  }
447 
448  bool result = ossimImageSourceFilter::loadState(kwl, prefix);
449 
450  initialize();
451 
452  return result;
453 }
454 
456  const char* prefix)const
457 {
458  kwl.add(prefix,
461  true);
462  kwl.add(prefix,
463  USE_INPUT_TILE_SIZE_KW,
465  true);
466 
467  kwl.add(prefix,
468  TILE_SIZE_XY_KW,
470 
471  return ossimImageSourceFilter::saveState(kwl, prefix);
472 }
473 
475  const ossimString& name)const
476 {
477  // Lock for the length of this method.
478 
479  if (name == TILE_SIZE_XY_KW)
480  {
483  result->setCacheRefreshBit();
484  return result;
485  }
486  else if(name == ossimKeywordNames::ENABLE_CACHE_KW)
487  {
490  result->setCacheRefreshBit();
491  return result;
492  }
493  else if(name == USE_INPUT_TILE_SIZE_KW)
494  {
497  result->setCacheRefreshBit();
498  return result;
499  }
501 }
502 
504 {
505  if (!property) return;
506 
507  ossimString name = property->getName();
508  if (name == TILE_SIZE_XY_KW)
509  {
510  ossimIpt pt;
511  pt.toPoint(property->valueToString());
512 
513  if ( (pt.x > 7) && (pt.y > 7) )
514  {
515  setTileSize(pt);
516  }
517  else
518  {
520  << "ossimCacheTileSource::setProperty NOTICE:"
521  << "\nTile dimensions must be at least 8!"
522  << "\nFormat = ( x, y )"
523  << std::endl;
524  }
525  }
526  else if(name == ossimKeywordNames::ENABLE_CACHE_KW)
527  {
528  setCachingEnabledFlag(property->valueToString().toBool());
529  }
530  else if(name == USE_INPUT_TILE_SIZE_KW)
531  {
532  theUseInputTileSizeFlag = property->valueToString().toBool();
534  {
536  }
537  // setCachingEnabledFlag(property->valueToString().toBool());
538  }
539  else
540  {
542  }
543 }
544 
546  std::vector<ossimString>& propertyNames)const
547 {
548  propertyNames.push_back(TILE_SIZE_XY_KW);
549  propertyNames.push_back(ossimKeywordNames::ENABLE_CACHE_KW);
550  propertyNames.push_back(USE_INPUT_TILE_SIZE_KW);
551 
553 }
554 
556 {
557  return ossimString("Tile Cache , cache for ossimImageData objects.");
558 }
559 
561 {
562  return ossimString("Tile Cache");
563 }
564 
566 {
567  if(!value && theCachingEnabled)
568  {
569  flush();
570  }
571  theCachingEnabled = value;
572 }
573 
575 {
576  theEventProgressFlag = value;
577 }
578 
580 {
582 }
583 
585 {
586  if (size != theFixedTileSize)
587  {
588  theTile = 0; // Force an allocate of new tile.
591  // ossimAppFixedTileCache::instance()->setTileSize(theCacheId, size);
592  }
593 }
594 
595 void ossimCacheTileSource::fireProgressEvent(double percentComplete)
596 {
598  {
599  ossimProcessProgressEvent event(this, percentComplete);
600  fireEvent(event);
601  }
602 }
603 
605 {
607  if(theRLevelCacheList.empty())
608  {
610  }
611  if(resLevel < theRLevelCacheList.size())
612  {
613  if(theRLevelCacheList[resLevel] <0)
614  {
615  ossimIpt cacheTileSize(theFixedTileSize);
617  {
618  cacheTileSize = ossimIpt(ossim::min(static_cast<ossim_int32>(theInputConnection->getTileWidth()),
619  static_cast<ossim_int32>(1024)),
620  ossim::min(static_cast<ossim_int32>(theInputConnection->getTileHeight()),
621  static_cast<ossim_int32>(1024)));
622 
623  }
624 
625  //std::cout << cacheTileSize << std::endl;
626  ossimIrect rect = getBoundingRect(resLevel);
627 
628  if((cacheTileSize.x > static_cast<ossim_int64>(rect.width()))&&
629  (cacheTileSize.y > static_cast<ossim_int64>(rect.height())))
630  {
631  cacheTileSize.x = ossim::max(static_cast<ossim_int64>(rect.width()),
632  static_cast<ossim_int64>(theTileSizeXY.x));
633  cacheTileSize.y = ossim::max(static_cast<ossim_int64>(rect.height()),
634  static_cast<ossim_int64>(theTileSizeXY.y));
635  }
636 
638  theRLevelCacheList[resLevel] = ossimAppFixedTileCache::instance()->newTileCache(rect, cacheTileSize);
639  }
640  result = theRLevelCacheList[resLevel];
641  }
642 
643  return result;
644 }
645 
647 {
648  ossim_uint32 idx = 0;
649  for(idx = 0; idx < theRLevelCacheList.size();++idx)
650  {
652  }
653  theRLevelCacheList.clear();
654 }
655 
657 {
660 
661  if(nLevels > 0)
662  {
663  ossim_uint32 idx = 0;
664  theRLevelCacheList.resize(nLevels);
665  for(idx= 0; idx < theRLevelCacheList.size(); ++idx)
666  {
667  theRLevelCacheList[idx] = -1;
668  }
669  }
670 }
671 
virtual void deleteCache(ossimAppFixedCacheId cacheId)
ossimString toString() const
Definition: ossimIpt.cpp:139
virtual void valueToString(ossimString &valueResult) const =0
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
virtual ossimIrect getBoundingRect(ossim_uint32 resLevel=0) const
This will return the bounding rect of the source.
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
void getTileSize(ossimIpt &size) const
virtual void setEventProgressFlag(bool value)
T max(T a, T b)
Definition: ossimCommon.h:236
virtual void setImageRectangle(const ossimIrect &rect)
Represents serializable keyword/value map.
virtual ossimString getLongName() const
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
const ossimIpt & getTileSize(ossimAppFixedCacheId cacheId)
virtual void setProperty(ossimRefPtr< ossimProperty > property)
ossim_uint32 height() const
Definition: ossimIrect.h:487
virtual ossim_uint32 getTileHeight() const
Returns the default processing tile height.
RLevelCacheList theRLevelCacheList
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
OSSIM_DLL void defaultTileSize(ossimIpt &tileSize)
const ossimIpt & ul() const
Definition: ossimIrect.h:274
virtual ossimDataObjectStatus getDataObjectStatus() const
ossimAppFixedTileCache::ossimAppFixedCacheId getCacheId(ossim_uint32 resLevel)
virtual ossim_uint32 getNumberOfDecimationLevels() const
Will return the number of resolution levels.
virtual ossim_uint32 getTileHeight() const
Returns the default processing tile height.
virtual void fireEvent(ossimEvent &event)
virtual void initialize()
Initialize the data buffer.
static ossimAppFixedTileCache * instance(ossim_uint32 maxSize=0)
virtual ossim_uint32 getTileWidth() const
Returns the default processing tile width.
ossimCacheTileSource()
Will construct a new Application cache.
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual void loadTile(const void *src, const ossimIrect &src_rect, ossimInterleaveType il_type)
static ossimImageDataFactory * instance()
virtual void fireProgressEvent(double percentComplete)
ossimRefPtr< ossimImageData > getTile(ossimAppFixedCacheId cacheId, const ossimIpt &origin)
yy_size_t size
virtual ossimDataObjectStatus validate() const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
bool toBool() const
String to numeric methods.
ossimImageSource * theInputConnection
unsigned int ossim_uint32
RTTI_DEF1(ossimCacheTileSource, "ossimCacheTileSource", ossimImageSourceFilter)
Cache Tile Source.
void setTileSize(const ossimIpt &size)
Set the tile size.
virtual ossimIrect getImageRectangle() const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
T min(T a, T b)
Definition: ossimCommon.h:203
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
ossim_uint32 width() const
Definition: ossimIrect.h:500
ossimIrect clipToRect(const ossimIrect &rect) const
Definition: ossimIrect.cpp:501
virtual ossimRefPtr< ossimImageData > fillTile(const ossimIrect &tileRect, ossim_uint32 resLevel, ossimAppFixedTileCache::ossimAppFixedCacheId cacheId)
void toPoint(const std::string &s)
Initializes this point from string.
Definition: ossimIpt.cpp:170
ossimRefPtr< ossimImageData > theTile
virtual ossimString getShortName() const
virtual void makeBlank()
Initializes data to null pixel values.
static const char * ENABLE_CACHE_KW
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
ossim_int32 y
Definition: ossimIpt.h:142
virtual const void * getBuf() const
virtual void setCachingEnabledFlag(bool value)
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
long long ossim_int64
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
void stretchToTileBoundary(const ossimIpt &tileWidthHeight)
Definition: ossimIrect.cpp:212
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tileRect, ossim_uint32 resLevel=0)
virtual void setDataObjectStatus(ossimDataObjectStatus status) const
Full list found in ossimConstants.h.
bool hasNans() const
Definition: ossimIrect.h:337
ossim_int32 x
Definition: ossimIpt.h:141
ossimAppFixedCacheId newTileCache(const ossimIrect &tileBoundaryRect, const ossimIpt &tileSize=ossimIpt(0, 0))
virtual ossim_uint32 getTileWidth() const
Returns the default processing tile width.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
ossimRefPtr< ossimImageData > addTile(ossimAppFixedCacheId cacheId, ossimRefPtr< ossimImageData > data, bool duplicateData=true)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
void setCacheRefreshBit()
int ossim_int32
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)