OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimImageHandlerRegistry.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: MIT
4 //
5 // Author: Garrett Potts
6 //
7 // Description:
8 //
9 // Contains class definition for ImageHandlerRegistry.
10 //
11 //*******************************************************************
12 // $Id$
13 
16 #include <ossim/base/ossimTrace.h>
20 #include <ossim/base/ossimString.h>
25 #include <algorithm>
26 
27 static ossimTrace traceDebug("ossimImageHandlerRegistry:debug");
28 
29 
30 RTTI_DEF1(ossimImageHandlerRegistry, "ossimImageHandlerRegistry", ossimObjectFactory);
31 
32 //ossimImageHandlerRegistry* ossimImageHandlerRegistry::theInstance = 0;
33 
35 {
39 }
40 
42 {
43  static ossimImageHandlerRegistry sharedInstance;
44 
45  return &sharedInstance;
46 }
47 
49 {
51 }
52 
53 
55  const char* prefix)const
56 {
57  ossimObject* result = createObjectFromRegistry(kwl, prefix);
58  if(!result)
59  {
60  result = open(kwl, prefix);
61  }
62  return result;
63 }
64 
66  bool openOverview)const
67 {
68  std::vector<ossimRefPtr<ossimImageHandler> > handlers;
69 
70  getImageHandlersBySuffix(handlers, file.ext());
71  ossim_uint32 idx = 0;
72  ossim_uint32 size = (ossim_uint32) handlers.size();
73 
74  for(idx = 0; idx < size; ++idx)
75  {
76  handlers[idx]->setOpenOverviewFlag(openOverview);
77  if(handlers[idx]->open(file))
78  {
79  return handlers[idx];
80  }
81  }
82 
84 }
85 
87  const ossimString& ext)const
88 {
89  vector<ossimImageHandlerFactoryBase*>::const_iterator iter = m_factoryList.begin();
91  while(iter != m_factoryList.end())
92  {
93  temp.clear();
94  (*iter)->getImageHandlersBySuffix(temp, ext);
95 
96  if(!temp.empty())
97  {
98 
99  // now append to the end of the typeList.
100  result.insert(result.end(),
101  temp.begin(),
102  temp.end());
103  }
104  ++iter;
105  }
106 }
107 
109  ossimImageHandlerFactoryBase::ImageHandlerList& result, const ossimString& mimeType)const
110 {
111  vector<ossimImageHandlerFactoryBase*>::const_iterator iter = m_factoryList.begin();
113  while(iter != m_factoryList.end())
114  {
115  temp.clear();
116  (*iter)->getImageHandlersByMimeType(temp, mimeType);
117 
118  if(!temp.empty())
119  {
120 
121  // now append to the end of the typeList.
122  result.insert(result.end(),
123  temp.begin(),
124  temp.end());
125  }
126  ++iter;
127  }
128 }
129 
130 void ossimImageHandlerRegistry::getTypeNameList( std::vector<ossimString>& typeList ) const
131 {
132  getAllTypeNamesFromRegistry(typeList);
133 }
134 
137 {
138  vector<ossimString> result;
139  vector<ossimImageHandlerFactoryBase*>::const_iterator iter = m_factoryList.begin();
140 
141  while(iter != m_factoryList.end())
142  {
143  (*iter)->getSupportedExtensions(extensionList);
144 
145  ++iter;
146  }
147 
148 }
149 
150 
151 std::shared_ptr<ossim::ImageHandlerState> ossimImageHandlerRegistry::getState(const ossimString& connectionString,
152  ossim_uint32 entry)const
153 {
154  return getState(connectionString + "_e" + ossimString::toString(entry));
155 }
156 
157 std::shared_ptr<ossim::ImageHandlerState> ossimImageHandlerRegistry::getState(const ossimString& id)const
158 {
159  std::shared_ptr<ossim::ImageHandlerState> result;
160 
161  if(m_stateCache)
162  {
163  result = m_stateCache->getItem(id);
164  }
165 
166  return result;
167 }
168 
169 
171  const ossimString& connectionString, bool openOverview )const
172 {
173  if(traceDebug())
174  {
175  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::openConnection: entered.........." << std::endl;
176  }
178 
179  std::string myConnectionString = connectionString.downcase().string();
180  std::string fileStr = "file://";
181  std::size_t found = myConnectionString.find( fileStr );
182  if ( found == 0 )
183  {
184  myConnectionString = connectionString.string().substr( fileStr.size() );
185  }
186  else
187  {
188  myConnectionString = connectionString.string();
189  }
190 
191  // add entry 0
192  std::shared_ptr<ossim::ImageHandlerState> state = getState(myConnectionString, 0);
193  if(state)
194  {
195  if(traceDebug())
196  {
197  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::openConnection: leaving with open(state).........." << std::endl;;
198  }
199  return open(state);
200  }
201 
202  std::shared_ptr<ossim::istream> str = ossim::StreamFactoryRegistry::instance()->
203  createIstream( myConnectionString, std::ios_base::in|std::ios_base::binary);
204 
205  if ( str )
206  {
207  result = open( str, myConnectionString, openOverview );
208  }
209 
210  if ( !result.valid() )
211  {
212  ossimFilename f = myConnectionString;
213  if ( f.exists() )
214  {
215  result = open( f, true, openOverview );
216  }
217  }
218 
219  if(result)
220  {
221  addToStateCache(result.get());
222  }
223  if(traceDebug())
224  {
225  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::openConnection: leaving.........." << std::endl;
226  }
227 
228  return result;
229 }
230 
232  bool trySuffixFirst,
233  bool openOverview)const
234 {
235  if(traceDebug())
236  {
237  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(file, trySuffix,openOverview): entered.........." << std::endl;
238  }
239  std::shared_ptr<ossim::ImageHandlerState> state = getState(filename, 0);
240 
241  if(state)
242  {
244  if(h)
245  {
246  if(traceDebug())
247  {
248  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(file, trySuffix,openOverview): returning with state open.........." << std::endl;;
249  }
250  return h.release();
251  }
252  }
253 
254  if(trySuffixFirst)
255  {
257  if(h.valid())
258  {
259  addToStateCache(h.get());
260  if(traceDebug())
261  {
262  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(file, trySuffix,openOverview): leaving.........." << std::endl;
263  }
264  return h.release();
265  }
266  }
267 
268  // now try magic number opens
269  //
270  ossimImageHandler* result = NULL;
271  vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
272  while((factory != m_factoryList.end()) && !result)
273  {
274  result = (*factory)->open(filename, openOverview);
275  ++factory;
276  }
277 
278  if(result)
279  {
280  addToStateCache(result);
281  }
282  if(traceDebug())
283  {
284  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(file, trySuffix,openOverview): leaving.........." << std::endl;
285  }
286  return result;
287 }
288 
290  const char* prefix)const
291 {
292  ossimImageHandler* result = NULL;
293  vector<ossimImageHandlerFactoryBase*>::const_iterator factory;
294 
295  factory = m_factoryList.begin();
296  while((factory != m_factoryList.end()) && !result)
297  {
298  result = (*factory)->open(kwl, prefix);
299  ++factory;
300  }
301 
302  if(result)
303  {
304  addToStateCache(result);
305  }
306 
307  return result;
308 }
309 
311  std::shared_ptr<ossim::istream>& str,
312  const std::string& connectionString,
313  bool openOverview ) const
314 {
315  if(traceDebug())
316  {
317  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(stream,connectionString,openOverview): entered.........." << std::endl;
318  }
320  std::shared_ptr<ossim::ImageHandlerState> state = getState(connectionString, 0);
321 
322  if(state)
323  {
324  result = open(state);
325  if(result)
326  {
327  if(traceDebug())
328  {
329  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(stream,connectionString,openOverview): leaving with state open..........Valid? "
330  << result.valid() <<std::endl;
331  }
332  return result;
333  }
334  }
335  if ( str )
336  {
337  vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
338  while( factory != m_factoryList.end() )
339  {
340  result = (*factory)->open( str, connectionString, openOverview );
341  if ( result.valid() )
342  {
343  break;
344  }
345  ++factory;
346  }
347  }
348  if(result)
349  {
350  addToStateCache(result.get());
351  }
352  if(traceDebug())
353  {
354  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(stream,connectionString,openOverview): leaving..........Valid? "
355  << result.valid()<<std::endl;
356  }
357  return result;
358 }
359 
360 ossimRefPtr<ossimImageHandler> ossimImageHandlerRegistry::open(std::shared_ptr<ossim::ImageHandlerState> state)const
361 {
363  vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
364  if(traceDebug())
365  {
366  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(state): Entered......." << std::endl;
367  }
368  while( (!result)&&(factory != m_factoryList.end()) )
369  {
370  result = (*factory)->open( state );
371  if ( result )
372  {
373  break;
374  }
375  ++factory;
376  }
377  if(traceDebug())
378  {
379  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::open(state): Leaving.......valid?" << result.valid()<<std::endl;
380  }
381  return result;
382 }
383 
384 #if 0
386  std::streamoff restartPosition,
387  bool youOwnIt ) const
388 {
390  vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
391  while( factory != m_factoryList.end() )
392  {
393  result = (*factory)->open( str, restartPosition, youOwnIt );
394  if ( result.valid() )
395  {
396  break;
397  }
398  ++factory;
399  }
400  return result;
401 }
402 #endif
403 
405  const ossimFilename& file ) const
406 {
408 
409  if(traceDebug())
410  {
411  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::openOverview: Entered......." << std::endl;
412  }
413 
414  std::shared_ptr<ossim::ImageHandlerState> state = getState(file, 0);
415 
416  if(state)
417  {
418  result = open(state);
419 
420  }
421 
422  if(!result)
423  {
424  // See if we can open via the stream interface:
425  std::shared_ptr<ossim::istream> str = ossim::StreamFactoryRegistry::instance()->
426  createIstream( file, std::ios_base::in|std::ios_base::binary);
427 
428  if ( str )
429  {
430  std::vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
431  while( factory != m_factoryList.end() )
432  {
433  result = (*factory)->openOverview( str, file );
434  if ( result.valid() )
435  {
436  break;
437  }
438  ++factory;
439  }
440 
441  str = 0;
442  }
443 
444  if ( (result.valid() == false) && file.exists() )
445  {
446  vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
447  while( factory != m_factoryList.end() )
448  {
449  result = (*factory)->openOverview( file );
450  if ( result.valid() )
451  {
452  break;
453  }
454  ++factory;
455  }
456  }
457 
458  if(result)
459  {
460  addToStateCache(result.get());
461  }
462  }
463  if(traceDebug())
464  {
465  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::openOverview: Leaving.......Valid? "
466  << result.valid() << std::endl;
467  }
468  return result;
469 }
470 
472 {
473  return createObjectFromRegistry(typeName);
474 }
475 
477 {
478  // Loop through factories:
479  vector<ossimImageHandlerFactoryBase*>::const_iterator factory = m_factoryList.begin();
480  while( factory != m_factoryList.end() )
481  {
482  out << "factory: " << (*factory)->getClassName() << "\n";
483 
484  // Loop through factory image handlers:
485  std::vector<ossimString> readerList;
486  (*factory)->getTypeNameList(readerList);
487 
488  std::vector<ossimString>::const_iterator i = readerList.begin();
489  while ( i != readerList.end() )
490  {
492  dynamic_cast<ossimImageHandler*>( (*factory)->createObject( (*i) ) );
493  if ( ih.valid() )
494  {
495  out << "reader: " << ih->getClassName() << "\n";
496 
497  // Loop through image handler properties:
498  std::vector<ossimString> propNames;
499  ih->getPropertyNames(propNames);
500  if ( propNames.size() )
501  {
502  out << "\nproperties:\n";
504  std::vector<ossimString>::const_iterator p = propNames.begin();
505  while ( p != propNames.end() )
506  {
507  out << " " << (*p) << "\n";
508  prop = ih->getProperty( *p );
509  if ( prop.valid() )
510  {
511  ossimStringProperty* stringProp =
512  dynamic_cast<ossimStringProperty*>(prop.get());
513  if ( stringProp )
514  {
515  if ( stringProp->getConstraints().size() )
516  {
517  out << " constraints:\n";
518  std::vector<ossimString>::const_iterator strPropIter =
519  stringProp->getConstraints().begin();
520  while( strPropIter != stringProp->getConstraints().end() )
521  {
522  out << " " << (*strPropIter) << "\n";
523  ++strPropIter;
524  }
525  }
526  }
527  }
528 
529  ++p;
530  }
531  out << "\n";
532  }
533  }
534  ++i;
535  }
536  ++factory;
537  }
538  out << std::endl;
539  return out;
540 }
541 
544 {}
545 
547 {
548  m_stateCache = 0;
549  ossimString enabledString = ossimPreferences::instance()->findPreference("ossim.imaging.handler.registry.state_cache.enabled");
550  ossimString minSizeString = ossimPreferences::instance()->findPreference("ossim.imaging.handler.registry.state_cache.min_size");
551  ossimString maxSizeString = ossimPreferences::instance()->findPreference("ossim.imaging.handler.registry.state_cache.max_size");
552 
553  ossim_uint32 maxSize = 0;
554  ossim_uint32 minSize = 0;
555 
556  if(!enabledString.empty())
557  {
558  if(enabledString.toBool())
559  {
560  m_stateCache = std::make_shared<ossim::ItemCache<ossim::ImageHandlerState> >();
561  if(!maxSizeString.empty())
562  {
563  maxSize = maxSizeString.toUInt32();
564  }
565  if(!minSizeString.empty())
566  {
567  minSize = minSizeString.toUInt32();
568  }
569  else if(maxSize)
570  {
571  minSize = ossim::round<ossim_uint32, ossim_float32>(maxSize*.8);
572  }
573 
574  if(minSize < maxSize)
575  {
576  m_stateCache->setMinAndMaxItemsToCache(minSize, maxSize);
577  }
578  }
579 
580  }
581 }
582 
584 {
585  if(handler)
586  {
587  std::shared_ptr<ossim::ImageHandlerState> state = handler->getState();
588  if(state&&m_stateCache)
589  {
590  ossimString id = handler->getFilename()+"_e"+ossimString::toString(state->getCurrentEntry());
591  if(traceDebug())
592  {
593  ossimNotify(ossimNotifyLevel_DEBUG)<< "ossimImageHandlerRegistry::addToStateCache: " << id << std::endl;
594  }
595  m_stateCache->addItem(id, state);
596  }
597  }
598 }
599 
600 
601 
604 { return rhs; }
605 
606 extern "C"
607 {
609  {
611  }
612 }
virtual bool open()=0
Pure virtual open.
virtual ossimRefPtr< ossimImageHandler > openOverview(const ossimFilename &file) const
Open overview that takes a file name.
virtual void getSupportedExtensions(ossimImageHandlerFactoryBase::UniqueStringList &extensionList) const
const ossimImageHandlerRegistry & operator=(const ossimImageHandlerRegistry &rhs)
virtual ossimImageHandler * open(const ossimFilename &fileName, bool trySuffixFirst=true, bool openOverview=true) const
open that takes a filename.
Represents serializable keyword/value map.
virtual void getTypeNameList(std::vector< ossimString > &typeList) const
RTTI_DEF1(ossimImageHandlerRegistry, "ossimImageHandlerRegistry", ossimObjectFactory)
bool valid() const
Definition: ossimRefPtr.h:75
static ossimString toString(bool aValue)
Numeric to string methods.
ossimRefPtr< ossimImageHandler > openConnection(const ossimString &connectionString, bool openOverview=true) const
std::shared_ptr< ossim::ImageHandlerState > getState()
static ossimObjectFactoryRegistry * instance()
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
ossim_uint32 toUInt32() const
static StreamFactoryRegistry * instance()
void addToStateCache(ossimImageHandler *handler) const
const std::vector< ossimString > & getConstraints() const
virtual bool openOverview()
Searches for an overview.
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
std::shared_ptr< ossim::ImageHandlerState > getState(const ossimString &connectionString, ossim_uint32 entry) const
void * ossimImageHandlerRegistryGetInstance()
ossimImageHandlerRegistry supports the new state cache.
const char * findPreference(const char *key) const
yy_size_t size
bool exists() const
virtual const ossimFilename & getFilename() const
Returns the filename.
bool toBool() const
String to numeric methods.
unsigned int ossim_uint32
virtual ossimObject * createObject(const ossimString &typeName) const
std::shared_ptr< ossim::ItemCache< ossim::ImageHandlerState > > m_stateCache
T * release()
Definition: ossimRefPtr.h:93
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
void registerFactory(T *factory, bool pushToFrontFlag=false)
Will register a factory to the factory list.
virtual void getImageHandlersByMimeType(ossimImageHandlerFactoryBase::ImageHandlerList &result, const ossimString &mimeType) const
Will add to the result list and handler that supports the passed in mime type.
static ossimPreferences * instance()
virtual ossimRefPtr< ossimImageHandler > openBySuffix(const ossimFilename &file, bool openOverview=true) const
openBySuffix will call the mthod getImageHandlersBySuffix and go through each handler to try and open...
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
static ossimImageHandlerFactory * instance()
This class defines an abstract Handler which all image handlers(loaders) should derive from...
bool empty() const
Definition: ossimString.h:411
ossimString ext() const
static ossimImageHandlerRegistry * instance()
std::ostream & printReaderProps(std::ostream &out) const
Prints list of readers and properties.
void getAllTypeNamesFromRegistry(std::vector< ossimString > &typeList) const
Will add all object types the factories can allocate.
std::vector< ossimRefPtr< ossimImageHandler > > ImageHandlerList
virtual void getImageHandlersBySuffix(ossimImageHandlerFactoryBase::ImageHandlerList &result, const ossimString &ext) const
Will add to the result list any handler that supports the passed in extensions.
ossimObject * createObjectFromRegistry(const ossimString &typeName) const
This is the base object return for all objects in the system.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
const std::string & string() const
Definition: ossimString.h:414