OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimMultiBandHistogram.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts
6 //
7 // Description:
8 //
9 //*******************************************************************
10 // $Id: ossimMultiBandHistogram.cpp 21631 2012-09-06 18:10:55Z dburken $
11 
14 #include <ossim/base/ossimNotify.h>
16 #include <fstream>
17 
19 {
20 }
21 
23 {
25 }
26 
28 {
29  theHistogramList.resize(rhs.theHistogramList.size());
30 
31  for(ossim_uint32 i = 0; i < theHistogramList.size(); ++i)
32  {
33  theHistogramList[i] = rhs.theHistogramList[i].valid()?
34  new ossimHistogram(*rhs.theHistogramList[i].get()):(ossimHistogram*)0;
35  }
36 }
37 
39  ossim_int32 numberOfBuckets,
40  float minValue,
41  float maxValue)
42 {
43  if(numberOfBands > 0)
44  {
45  create(numberOfBands, numberOfBuckets, minValue, maxValue);
46  }
47 }
48 
50 {
51  if (input)
52  {
53  ossim_uint32 bands = input->getNumberOfOutputBands();;
54  ossim_uint32 numberOfBins = 0;
55  ossim_float64 minValue = 0.0;
56  ossim_float64 maxValue = 0.0;
57 
58  switch(input->getOutputScalarType())
59  {
60  case OSSIM_UINT8:
61  {
62  minValue = 0;
63  maxValue = OSSIM_DEFAULT_MAX_PIX_UCHAR;
64  numberOfBins = 256;
65  break;
66  }
67  case OSSIM_USHORT11:
68  {
69  minValue = 0;
71  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT11 + 1;
72  break;
73  }
74  case OSSIM_USHORT12:
75  {
76  minValue = 0;
78  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT12 + 1;
79  break;
80  }
81  case OSSIM_USHORT13:
82  {
83  minValue = 0;
85  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT13 + 1;
86  break;
87  }
88  case OSSIM_USHORT14:
89  {
90  minValue = 0;
92  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT14 + 1;
93  break;
94  }
95  case OSSIM_USHORT15:
96  {
97  minValue = 0;
99  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT15 + 1;
100  break;
101  }
102  case OSSIM_UINT16:
103  case OSSIM_UINT32:
104  {
105  minValue = 0;
106  maxValue = OSSIM_DEFAULT_MAX_PIX_UINT16;
107  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT16 + 1;
108  break;
109  }
110  case OSSIM_SINT16:
111  case OSSIM_SINT32:
112  case OSSIM_FLOAT32:
113  case OSSIM_FLOAT64:
114  {
115  //---
116  // Special case to handle DTED which has a null of -32767 and SRTM
117  // which has null of -32768. Set the min to -32766 which is OK for
118  // both types. Basically we don't want to count the null values as
119  // a valid pixel. drb - 04 Feb. 2016
120  //
121  // NOTE: OSSIM_DEFAULT_MIN_PIX_SINT16 = -32767
122  //---
123  minValue = OSSIM_DEFAULT_MIN_PIX_SINT16+1;
124  maxValue = OSSIM_DEFAULT_MAX_PIX_SINT16;
126  break;
127  }
130  {
131  minValue = 0;
132  maxValue = 1.0;
133  numberOfBins = OSSIM_DEFAULT_MAX_PIX_UINT16+1;
134  break;
135  }
136  default:
137  {
139  << "Unsupported scalar type in ossimMultiBandHistogram::create()"
140  << std::endl;
141  return;
142  }
143 
144  } // switch(input->getOutputScalarType())
145 
146  create(bands, numberOfBins, minValue, maxValue);
147 
148  } // if (input)
149 }
150 
152  ossim_int32 numberOfBuckets,
153  float minValue,
154  float maxValue)
155 {
156  // Make sure we clear our internal lists before we start.
158 
159  if(numberOfBands > 0)
160  {
161  numberOfBuckets = numberOfBuckets>0?numberOfBuckets:1;
162 
163  for(ossim_int32 bands = 0; bands < numberOfBands; ++bands)
164  {
165  theHistogramList.push_back(new ossimHistogram(numberOfBuckets,
166  minValue,
167  maxValue));
168  }
169  }
170 }
171 
173 {
175  for(ossim_int32 bands = 0; bands < numberOfBands; ++bands)
176  {
177  theHistogramList.push_back(new ossimHistogram);
178  }
179 }
180 
182 {
183  return (ossim_uint32)theHistogramList.size();
184 }
185 
187 {
188  if((band >=0) && (band < (ossim_int32)theHistogramList.size()))
189  {
190  return theHistogramList[band];
191  }
192 
193  return NULL;
194 }
195 
197 {
198  if((band >=0) && (band < (ossim_int32)theHistogramList.size()))
199  {
200  return theHistogramList[band];
201  }
202 
203  return 0;
204 }
205 
206 void ossimMultiBandHistogram::setBinCount(double binNumber, double count)
207 {
208  if(theHistogramList.size() > 0)
209  {
210  for(ossim_uint32 idx = 0; idx < theHistogramList.size(); ++idx)
211  {
212  if(theHistogramList[idx].valid())
213  {
214  theHistogramList[idx]->SetCount(binNumber, count);
215  }
216  }
217  }
218 }
219 
221 {
223 
224  if(theHistogramList.size() > 0)
225  {
226  result = new ossimMultiBandHistogram;
227  result->theHistogramList.resize(theHistogramList.size());
228 
229  for(ossim_uint32 idx = 0; idx < theHistogramList.size(); ++idx)
230  {
231  if(theHistogramList[idx].valid())
232  {
233  result->theHistogramList[idx] = theHistogramList[idx]->CumulativeLessThanEqual();
234  }
235  else
236  {
237  result->theHistogramList[idx] = 0;
238  }
239  }
240  }
241 
242  return result;
243 }
244 
246 {
248 
249  if(theHistogramList.size() > 0)
250  {
251  result = new ossimMultiBandHistogram;
252 
253  for(ossim_uint32 idx = 0; idx < theHistogramList.size(); ++idx)
254  {
255  if(theHistogramList[idx].valid())
256  {
257  result->theHistogramList[idx] = theHistogramList[idx]->CumulativeGreaterThanEqual();
258  }
259  else
260  {
261  result->theHistogramList[idx] = 0;
262  }
263  }
264  }
265 
266  return result;
267 }
268 
270 {
271  theHistogramList.clear();
272 }
273 
274 
276 {
279 
280  if(header.parseStream(in))
281  {
282  ossim_int32 numberOfBands = header.getNumberOfBands();
283 
284  if(numberOfBands)
285  {
286  theHistogramList.resize(numberOfBands);
287 
288  for(ossim_int32 counter = 0; counter < (ossim_int32)theHistogramList.size(); ++counter)
289  {
290  theHistogramList[counter] = 0;
291  }
292  ossimString bandBuffer;
293  ossimString buffer;
294 
295  for(ossim_int32 idx = 0; idx < numberOfBands; ++idx)
296  {
297  getline(in, buffer);
298  if(buffer.find("Band") != string::npos)
299  {
300  string::size_type offset = buffer.find(":");
301  if(offset != string::npos)
302  {
303  bandBuffer = buffer.substr(offset+1);
304  }
305  else
306  {
308  return false;
309  }
310  }
311  else
312  {
314  return false;
315  }
316  ossim_uint32 bandIdx = bandBuffer.toUInt32();
317 
318  if(bandIdx < theHistogramList.size())
319  {
320  if(!theHistogramList[bandIdx].valid())
321  {
323  if(histogram->importHistogram(in))
324  {
325  theHistogramList[bandIdx] = histogram;
326  }
327  else
328  {
330  return false;
331  }
332  }
333  }
334  else
335  {
337  return false;
338  }
339  }
340  }
341  else
342  {
343  return false;
344  }
345  }
346  return true;
347 }
348 
350 {
351  if(file.exists())
352  {
353  std::ifstream input(file.c_str());
354 
355  return importHistogram(input);
356  }
357 
358  return false;
359 }
360 
362 {
363  ossimString inputLine;
364 
365  getline(in, inputLine);
366  if(inputLine.find("File Type") != string::npos)
367  {
368  std::string::size_type idx = inputLine.find(":");
369  if(idx != string::npos)
370  {
371  theFileType = inputLine.substr(idx+1);
373  }
374  else
375  {
376  return false;
377  }
378 
379  }
380  else
381  {
382  return false;
383  }
384 
385  getline(in, inputLine);
386  if(inputLine.find("Version") != string::npos)
387  {
388  std::string::size_type idx = inputLine.find(":");
389  if(idx != string::npos)
390  {
391  theVersion = inputLine.substr(idx+1);
393  }
394  else
395  {
396  return false;
397  }
398  }
399  else
400  {
401  return false;
402  }
403 
404  getline(in, inputLine);
405  if(inputLine.find("Number of Bands") != string::npos)
406  {
407  std::string::size_type idx = inputLine.find(":");
408  if(idx != string::npos)
409  {
410  theNumberOfBands = inputLine.substr(idx+1);
412  }
413  else
414  {
415  return false;
416  }
417  }
418  else
419  {
420  return false;
421  }
422 
423  return true;
424 }
425 
427 {
428  return theNumberOfBands.toUInt32();
429 }
430 
432 {
433  theFileType = "";
434  theVersion = "";
435  theNumberOfBands = "";
436 }
437 
439  const char* prefix)const
440 {
441  kwl.add(prefix,
442  "type",
443  "ossimMultiBandHistogram",
444  true);
445  kwl.add(prefix,
446  "number_of_bands",
447  static_cast<ossim_uint32>(theHistogramList.size()),
448  true);
449 
450  for(ossim_uint32 idx = 0; idx < theHistogramList.size(); ++idx)
451  {
452  ossimString band = ossimString(prefix) + "band";
453  band += (ossimString::toString(idx) + ".");
454  if(theHistogramList[idx].valid())
455  {
456  ossimString newPrefix = (ossimString(prefix) + ossimString::toString(idx) + ".");
457 
458  theHistogramList[idx]->saveState(kwl, band.c_str());
459  }
460  }
461 
462  return true;
463 }
464 
466  const char* prefix)
467 {
469  const char* number_of_bands = kwl.find(prefix, "number_of_bands");
470  if(number_of_bands)
471  {
472  ossim_uint32 numberOfBands = ossimString(number_of_bands).toUInt32();
473 
474  if(numberOfBands>0)
475  {
476  ossimString newPrefix;
477  for(ossim_uint32 idx = 0; idx < numberOfBands; ++idx)
478  {
479  ossimHistogram* histo = new ossimHistogram;
480 
481  newPrefix = prefix;
482  newPrefix += "band";
483  newPrefix += ossimString::toString(idx);
484  newPrefix += ".";
485 
486  histo->loadState(kwl, newPrefix.c_str());
487 
488  theHistogramList.push_back(histo);
489  }
490  }
491  }
492 
493  return true;
494 }
495 
497 {
498  xmlNode->setTag("ossimMutliBandHistogram");
499  for(ossim_uint32 idx = 0; idx < theHistogramList.size(); ++idx)
500  {
503 
504  band->setTag("Band");
505  band->addAttribute("idx", ossimString::toString(idx));
506  band->addChildNode(newNode.get());
507  if(theHistogramList[idx].valid())
508  {
509  theHistogramList[idx]->saveState(newNode);
510  }
511  else
512  {
513  newNode->setTag("ossimHistogram");
514  }
515  xmlNode->addChildNode(band.get());
516  }
517 
518  return true;
519 }
520 
522 {
523  theHistogramList.clear();
524  const vector<ossimRefPtr<ossimXmlNode> >& childNodes = xmlNode->getChildNodes();
525 
526  ossim_uint32 idx = 0;
527  ossim_uint32 maxCount = (ossim_uint32)childNodes.size();
528  for(idx = 0; idx < maxCount; ++idx)
529  {
530  if(childNodes[idx]->getTag() == "Band")
531  {
532  if(childNodes[idx]->getChildNodes().size())
533  {
535  histo->loadState(childNodes[idx]->getChildNodes()[0]);
536  theHistogramList.push_back(histo);
537  }
538  }
539  }
540 
541  return true;
542 }
16 bit unsigned integer (15 bits used)
#define OSSIM_DEFAULT_MAX_PIX_UINT16
void setTag(const ossimString &tag)
64 bit floating point
16 bit unsigned integer
Represents serializable keyword/value map.
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
#define OSSIM_DEFAULT_MAX_PIX_UCHAR
const char * find(const char *key) const
const ossimXmlNode::ChildListType & getChildNodes() const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
static ossimString toString(bool aValue)
Numeric to string methods.
ossimRefPtr< ossimMultiBandHistogram > createAccumulationLessThanEqual() const
16 bit signed integer
16 bit unsigned integer (14 bits used)
ossim_uint32 toUInt32() const
16 bit unsigned integer (13 bits used)
std::istream & getline(std::istream &is, ossimString &str, char delim)
Definition: ossimString.h:916
32 bit floating point
32 bit unsigned integer
double ossim_float64
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
ossim_uint32 getNumberOfBands() const
#define OSSIM_DEFAULT_MAX_PIX_UINT12
#define OSSIM_DEFAULT_MIN_PIX_SINT16
void setBinCount(double binNumber, double count)
yy_size_t size
bool exists() const
32 bit signed integer
#define OSSIM_DEFAULT_MAX_PIX_UINT15
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
unsigned int ossim_uint32
32 bit normalized floating point
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
ossimRefPtr< ossimHistogram > getHistogram(ossim_int32 band)
#define OSSIM_DEFAULT_MAX_PIX_UINT13
virtual bool importHistogram(const ossimFilename &inputFile)
#define OSSIM_DEFAULT_MAX_PIX_SINT16
std::vector< ossimRefPtr< ossimHistogram > > theHistogramList
64 bit normalized floating point
16 bit unsigned integer (11 bits used)
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
#define OSSIM_DEFAULT_MAX_PIX_UINT14
#define OSSIM_DEFAULT_MAX_PIX_UINT11
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
std::string substr(std::string::size_type pos=0, std::string::size_type n=std::string::npos) const
Equivalent to basic_string(*this, pos, n).
Definition: ossimString.h:910
void addAttribute(ossimRefPtr< ossimXmlAttribute > attribute)
8 bit unsigned integer
void create(const ossimImageSource *input)
void addChildNode(ossimRefPtr< ossimXmlNode > node)
std::string::size_type find(const std::string &s, std::string::size_type pos=0) const
Searches for s as a substring of *this, beginning at character pos of *this.
Definition: ossimString.h:753
ossimRefPtr< ossimMultiBandHistogram > createAccumulationGreaterThanEqual() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32
virtual bool importHistogram(const ossimFilename &inputFile)
16 bit unsigned integer (12 bits used)