OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimImageMetaData.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // Author: Garrett Potts
7 //
8 // Description:
9 //
10 // Contains class definition for ossimImageMetaData.
11 //
12 //*******************************************************************
13 // $Id: ossimImageMetaData.cpp 21745 2012-09-16 15:21:53Z dburken $
14 
16 #include <ossim/base/ossimCommon.h>
19 #include <ossim/base/ossimString.h>
21 #include <ossim/base/ossimTrace.h>
22 #include <algorithm>
23 #include <iostream>
24 #include <vector>
25 
26 static ossimTrace traceDebug("ossimImageMetaData:debug");
27 
29  :theNullPixelArray(0),
30  theMinPixelArray(0),
31  theMaxPixelArray(0),
32  theMinValuesValidFlag(false),
33  theMaxValuesValidFlag(false),
34  theNullValuesValidFlag(false),
35  theScalarType(OSSIM_SCALAR_UNKNOWN),
36  theBytesPerPixel(0),
37  theNumberOfBands(0),
38  theRgbBands()
39 {
40 }
41 
43  ossim_uint32 numberOfBands)
44  :theNullPixelArray(0),
45  theMinPixelArray(0),
46  theMaxPixelArray(0),
47  theMinValuesValidFlag(false),
48  theMaxValuesValidFlag(false),
49  theNullValuesValidFlag(false),
50  theScalarType(aType),
51  theBytesPerPixel(0),
52  theNumberOfBands(numberOfBands),
53  theRgbBands()
54 {
56  {
57  theNullPixelArray = new double[theNumberOfBands];
58  theMinPixelArray = new double[theNumberOfBands];
59  theMaxPixelArray = new double[theNumberOfBands];
60 
62  }
64 }
65 
67  :
68  theNullPixelArray(0),
69  theMinPixelArray(0),
70  theMaxPixelArray(0),
71  theMinValuesValidFlag(rhs.theMinValuesValidFlag),
72  theMaxValuesValidFlag(rhs.theMaxValuesValidFlag),
73  theNullValuesValidFlag(rhs.theNullValuesValidFlag),
74  theScalarType(rhs.theScalarType),
75  theBytesPerPixel(rhs.theBytesPerPixel),
76  theNumberOfBands(rhs.theNumberOfBands),
77  theRgbBands(rhs.theRgbBands)
78 {
80  {
81  theNullPixelArray = new double[theNumberOfBands];
82  theMinPixelArray = new double[theNumberOfBands];
83  theMaxPixelArray = new double[theNumberOfBands];
84 
85  std::copy(rhs.theNullPixelArray,
88  std::copy(rhs.theMinPixelArray,
91  std::copy(rhs.theMaxPixelArray,
94  }
95 }
96 
98 {
99  if( this != &rhs )
100  {
101  clear();
108  if(theNumberOfBands)
109  {
110  theNullPixelArray = new double[theNumberOfBands];
111  theMinPixelArray = new double[theNumberOfBands];
112  theMaxPixelArray = new double[theNumberOfBands];
113 
114  std::copy(rhs.theNullPixelArray,
117  std::copy(rhs.theMinPixelArray,
120  std::copy(rhs.theMaxPixelArray,
123  }
124  theRgbBands = rhs.theRgbBands;
125  }
126  return *this;
127 }
128 
130 {
131  clear();
132 }
133 
135 {
137  {
138  delete [] theNullPixelArray;
139  theNullPixelArray = 0;
140  }
141  if(theMinPixelArray)
142  {
143  delete [] theMinPixelArray;
144  theMinPixelArray = 0;
145  }
146  if(theMaxPixelArray)
147  {
148  delete [] theMaxPixelArray;
149  theMaxPixelArray = 0;
150  }
152  theBytesPerPixel = 0;
153  theNumberOfBands = 0;
154  theRgbBands.clear();
155 }
156 
158 {
159  ossim_uint32 i=0;
160 
161  for(i = 0; i < theNumberOfBands; ++i)
162  {
166  }
167 }
168 
170 {
172  {
173  delete [] theNullPixelArray;
174  theNullPixelArray = 0;
175  }
176  if(theMinPixelArray)
177  {
178  delete [] theMinPixelArray;
179  theMinPixelArray = 0;
180  }
181  if(theMaxPixelArray)
182  {
183  delete [] theMaxPixelArray;
184  theMaxPixelArray = 0;
185  }
186 
187  theNumberOfBands = numberOfBands;
188 
189  if(theNumberOfBands)
190  {
191  theNullPixelArray = new double[theNumberOfBands];
192  theMinPixelArray = new double[theNumberOfBands];
193  theMaxPixelArray = new double[theNumberOfBands];
194 
196  }
197 }
198 
199 bool ossimImageMetaData::loadState(const ossimKeywordlist& kwl, const char* prefix)
200 {
201  bool result = false; // return status
202 
203  // Clear the object:
204  clear();
205 
206  std::string pfx = ( prefix ? prefix : "" );
207 
208  theRgbBands = kwl.findKey( pfx, std::string("rgb_bands") );
209 
210  ossim_uint32 bands = getBandCount( kwl, pfx );
211  if ( bands )
212  {
213  result = true;
214 
215  setNumberOfBands( bands );
216 
217  theMinValuesValidFlag = true;
218  theMaxValuesValidFlag = true;
219  theNullValuesValidFlag = true;
220 
221  updateMetaData( kwl, pfx ); // Initializes the rest of object.
222  }
223  else
224  {
225  if (traceDebug())
226  {
228  << " ERROR:\n"
229  << "Required keyword not found: " << ossimKeywordNames::NUMBER_BANDS_KW << std::endl;
230  }
231  }
232 
233  if (traceDebug())
234  {
236  }
237 
238  return result;
239 }
240 
242  const char* prefix)const
243 {
244  kwl.add(prefix, "type", "ossimImageMetaData", true);
245  if ( theNumberOfBands )
246  {
247  ossim_uint32 band = 0;
248 
249  for (band=0; band < theNumberOfBands; ++band)
250  {
252  ossimString::toString(band+1) + "." +
255  ossimString::toString(band+1) + "." +
258  ossimString::toString(band+1) + "." +
260 
261  kwl.add(prefix,
262  kwMin.c_str(),
263  theMinPixelArray[band],
264  true);
265  kwl.add(prefix,
266  kwMax.c_str(),
267  theMaxPixelArray[band],
268  true);
269  kwl.add(prefix,
270  kwNull.c_str(),
271  theNullPixelArray[band],
272  true);
273  }
274  // std::cout << "added bands ******************" << std::endl;
275  kwl.add(prefix,
278  true);
279  kwl.add(prefix,
281  (ossimScalarTypeLut::instance()->getEntryString(theScalarType)),
282  true);
283 
284  kwl.add( prefix, "bytes_per_pixel", theBytesPerPixel, true );
285  }
286 
287  if ( theRgbBands.size() )
288  {
289  kwl.add( prefix, "rgb_bands", theRgbBands.c_str(), true );
290  }
291 
292  return true;
293 }
294 
296 {
297  return theNumberOfBands;
298 }
299 
300 const std::string& ossimImageMetaData::getRgbBands() const
301 {
302  return theRgbBands;
303 }
304 
305 bool ossimImageMetaData::getRgbBands(std::vector<ossim_uint32>& bandList) const
306 {
307  bool result = false;
308  if ( ( theNumberOfBands > 2) && theRgbBands.size() )
309  {
311  if ( bandList.size() == 3 )
312  {
313  result = true;
314  }
315  else
316  {
317  bandList.clear();
318  }
319  }
320  return result;
321 }
322 
324 {
325  theScalarType = aType;
327 }
328 
330 {
331  return theScalarType;
332 }
333 
335 {
336  return theBytesPerPixel;
337 }
338 
340 {
341  if((band < theNumberOfBands)&&(theMinPixelArray))
342  {
343  theMinPixelArray[band] = pix;
344  }
345 }
346 
348 {
349  if((band < theNumberOfBands) && theMaxPixelArray)
350  {
351  theMaxPixelArray[band] = pix;
352  }
353 }
354 
356 {
357  if((band < theNumberOfBands)&&(theNullPixelArray))
358  {
359  theNullPixelArray[band] = pix;
360  }
361 }
362 
364 {
366  {
368 
369  return theMinPixelArray[i];
370  }
372 }
373 
375 {
377  {
379 
380  return theMaxPixelArray[i];
381  }
383 }
384 
386 {
388  {
390 
391  return theNullPixelArray[i];
392  }
394 }
395 
397 {
398  return theMinPixelArray;
399 }
401 {
402  return theMaxPixelArray;
403 }
404 
406 {
407  return theNullPixelArray;
408 }
409 
411 {
412  theMinValuesValidFlag = flag;
413 }
414 
416 {
417  theMaxValuesValidFlag = flag;
418 }
419 
421 {
422  theNullValuesValidFlag = flag;
423 }
424 
426 {
427  return theMinValuesValidFlag;
428 }
429 
431 {
432  return theMaxValuesValidFlag;
433 }
434 
436 {
437  return theNullValuesValidFlag;
438 }
439 
441 {
442  return (theNumberOfBands > 0);
443 // return ((theNumberOfBands>0)&&
444 // (theScalarType != OSSIM_SCALAR_UNKNOWN));
445 }
446 
448  const ossimKeywordlist& kwl, const std::string& prefix )
449 {
450  ossimString value;
451  std::string key;
452 
453  // Get the bands:
454  if ( !theNumberOfBands )
455  {
456  ossim_uint32 bands = getBandCount( kwl, prefix );
457  if ( bands )
458  {
459  setNumberOfBands( bands );
460  }
461  }
462 
463  if ( theNumberOfBands )
464  {
465  //---
466  // See if bands are zero or one based. Bands are ONE based in the "omd"
467  // file! If band0 is present assume zero based.
468  //---
469 
470  ossimString regExpression = std::string("^(") + prefix + std::string("band0+.)");
471  std::vector<ossimString> keys = kwl.getSubstringKeyList( regExpression );
472 
473  ossim_uint32 startBand = (keys.size() ? 0 : 1);
474 
475  ossim_uint32 limit = theNumberOfBands+startBand; // For for loop:
476 
477  std::string bs = "band"; // band sting
478  std::string base;
479 
480  for ( ossim_uint32 band = startBand; band < limit; ++band )
481  {
482  base = bs + ossimString::toString( band ).string() + std::string(".") ;
483 
484  // Min:
485  key = base + std::string(ossimKeywordNames::MIN_VALUE_KW);
486  value = kwl.findKey( prefix, key );
487 
488  if ( value.size() )
489  {
490  theMinPixelArray[ band - startBand ] = value.toFloat64();
491  }
492 
493  // Max:
494  key = base + std::string(ossimKeywordNames::MAX_VALUE_KW);
495  value = kwl.findKey( prefix, key );
496  if ( value.size() )
497  {
498  theMaxPixelArray[ band - startBand ] = value.toFloat64();
499  }
500 
501  // Null:
502  key = base + std::string(ossimKeywordNames::NULL_VALUE_KW);
503  value = kwl.findKey( prefix, key );
504  if ( value.size() )
505  {
506  theNullPixelArray[ band - startBand ] = value.toFloat64();
507  }
508  }
509  }
510 
511 
512  // Scalar, only look for if not set.
514  {
515  key = ossimKeywordNames::SCALAR_TYPE_KW; // "scalar_type"
516  value.string() = kwl.findKey( prefix, key );
517  if ( value.empty() )
518  {
519  key = "radiometry";
520  value.string() = kwl.findKey( prefix, key );
521  }
522  if ( value.size() )
523  {
525  }
526 
528  {
530  }
531  }
532 
533  if (traceDebug())
534  {
536  << "ossimImageMetaData::updateMetaData DEBUG"
537  << *this << std::endl;
538  }
539 }
540 
542 {
543  ossimKeywordlist kwl;
544  saveState( kwl, 0 );
545  out << kwl << std::endl;
546  return out;
547 }
548 
550 {
551  return obj.print( out );
552 }
553 
555  const std::string& prefix) const
556 {
557  ossim_uint32 result = 0;
558 
559  ossimString value;
560  value.string() = kwl.findKey( prefix, std::string(ossimKeywordNames::NUMBER_BANDS_KW) );
561  if ( value.size() )
562  {
563  result = value.toUInt32();
564  }
565  else
566  {
567  value.string() = std::string("^(") + prefix + std::string("band[0-9]+.)");
568  std::vector<ossimString> keys = kwl.getSubstringKeyList( value );
569  result = static_cast<ossim_uint32>( keys.size() );
570  }
571  return result;
572 }
void setScalarType(ossimScalarType aType)
static const char * MIN_VALUE_KW
bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
const double * getNullPixelArray() const
ossimScalarType getScalarTypeFromString(const ossimString &s) const
ossim_uint32 getBytesPerPixel() const
bool getMinValuesValidFlag() const
double getMinPix(ossim_uint32 band) const
Represents serializable keyword/value map.
const std::string & findKey(const std::string &key) const
Find methods that take std::string(s).
ossimScalarType theScalarType
void setMaxValuesValid(bool flag)
void setNullValuesValid(bool flag)
ossim_uint32 theNumberOfBands
ossimScalarType getScalarType() const
static const char * MAX_VALUE_KW
static const char * NULL_VALUE_KW
static ossimString toString(bool aValue)
Numeric to string methods.
double getMaxPix(ossim_uint32 band) const
ossim_uint32 toUInt32() const
std::ostream & print(std::ostream &out) const
Print method.
ossim_uint32 getBandCount(const ossimKeywordlist &kwl, const std::string &prefix) const
Looks for ossimKeywordNames::NUMBER_BANDS_KW, if not found looks for.
bool getNullValuesValidFlag() const
double getNullPix(ossim_uint32 band) const
ossim_uint32 getNumberOfBands() const
bool toSimpleVector(std::vector< T > &result, const ossimString &stringOfPoints)
Definition: ossimCommon.h:537
void setNumberOfBands(ossim_uint32 numberOfBands)
void setMinPix(ossim_uint32 band, double pix)
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
static ossimScalarTypeLut * instance()
Returns the static instance of an ossimScalarTypeLut object.
OSSIM_DLL double defaultMin(ossimScalarType scalarType)
Definition: ossimCommon.cpp:73
std::string::size_type size() const
Definition: ossimString.h:405
OSSIM_DLL double defaultNull(ossimScalarType scalarType)
unsigned int ossim_uint32
void setMinValuesValid(bool flag)
ossim_float64 toFloat64() const
OSSIM_DLL ossim_uint32 scalarSizeInBytes(ossimScalarType scalarType)
const double * getMinPixelArray() const
static const char * NUMBER_BANDS_KW
T min(T a, T b)
Definition: ossimCommon.h:203
bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
static const char * BAND_KW
ossimScalarType
std::ostream & operator<<(std::ostream &out, const ossimImageMetaData &obj)
ossim_uint32 theBytesPerPixel
std::vector< ossimString > getSubstringKeyList(const ossimString &regularExpression) const
OSSIM_DLL double defaultMax(ossimScalarType scalarType)
bool getMaxValuesValidFlag() const
const ossimImageMetaData & operator=(const ossimImageMetaData &rhs)
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
static const char * SCALAR_TYPE_KW
bool empty() const
Definition: ossimString.h:411
const std::string & getRgbBands() const
const double * getMaxPixelArray() const
void setMaxPix(ossim_uint32 band, double pix)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
void updateMetaData(const ossimKeywordlist &kwl, const std::string &prefix)
Method to update band values.
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
void setNullPix(ossim_uint32 band, double pix)
const std::string & string() const
Definition: ossimString.h:414