OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimMultiBandHistogramTileSource.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 //*************************************************************************
8 // $Id: ossimMultiBandHistogramTileSource.cpp 17195 2010-04-23 17:32:18Z dburken $
15 #include <ossim/base/ossimCommon.h>
19 #include <algorithm>
20 
21 static const char* MIN_VALUE_PERCENT_KW = "min_percent";
22 static const char* MAX_VALUE_PERCENT_KW = "max_percent";
23 static const char* ALGORITHM_KW = "algorithm";
24 
26  "ossimMultiBandHistogramTileSource",
28 
31  theTile(0),
32  theAlgorithm(OSSIM_HISTOGRAM_LINEAR_STRETCH_ALGORITHM)
33 {
34  disableSource();
35 }
36 
38  double /* minValuePercent */,
39  double /* maxValuePercent */,
40  ossimImageSource* inputSource,
41  ossimMultiResLevelHistogram* histogram)
42  : ossimImageSourceHistogramFilter(inputSource, histogram),
43  theTile(0),
44  theAlgorithm(OSSIM_HISTOGRAM_LINEAR_STRETCH_ALGORITHM)
45 {
46  disableSource();
47 }
48 
50 {
51 }
52 
54  const ossimIrect& tileRect,
55  ossim_uint32 resLevel)
56 {
57  if(!theInputConnection) return 0;
58 
60  resLevel);
61 
62  if(!isSourceEnabled() || !inputTile.valid())
63  {
64  return inputTile;
65  }
66 
67  if(!theTile.valid())
68  {
69  allocate(); // First time through...
70  }
71 
72  if( !theTile.valid() ||
73  !theMinValuePercentArray.size() ||
74  !theMaxValuePercentArray.size() )
75  {
76  return inputTile;
77  }
78 
79  theTile->setImageRectangle(tileRect);
80  if(inputTile->getBuf()&&
81  (inputTile->getDataObjectStatus()!=OSSIM_EMPTY))
82  {
83  theTile->loadTile(inputTile.get());
84  switch(inputTile->getScalarType())
85  {
86  case OSSIM_UCHAR:
87  {
89  theTile);
90  }
91  case OSSIM_USHORT16:
92  case OSSIM_USHORT11:
93  case OSSIM_USHORT12:
94  case OSSIM_USHORT13:
95  case OSSIM_USHORT14:
96  case OSSIM_USHORT15:
97  {
99  theTile);
100  }
101  case OSSIM_SSHORT16:
102  {
103  return runLinearStretchAlgorithm(static_cast<ossim_sint16>(0),
104  theTile);
105  }
106  case OSSIM_DOUBLE:
108  {
109  return runLinearStretchAlgorithm(static_cast<double>(0),
110  theTile);
111  }
112  case OSSIM_FLOAT:
114  {
115  return runLinearStretchAlgorithm(static_cast<float>(0),
116  theTile);
117  }
119  default:
120  {
121  ossimSetError("ossimMultiBandHistogramTileSource",
123  "Unknown scalar type");
124  break;
125  }
126  }
127  theTile->validate();
128  return theTile;
129  }
130 
131  return inputTile;
132 }
133 
135 {
137 
138  // Force an allocate on the next getTile.
139  theTile = 0;
140 }
141 
143 {
145 
146  if(theTile.valid())
147  {
148  theTile->initialize();
149 
150  ossim_uint32 numberOfBands = theTile->getNumberOfBands();
151  ossim_uint32 i = 0;
152  if(numberOfBands)
153  {
154  std::vector<double> copyArray;
155 
156  if(numberOfBands > theMinValuePercentArray.size())
157  {
158  copyArray = theMinValuePercentArray;
159  theMinValuePercentArray.resize(numberOfBands);
160  if(numberOfBands < copyArray.size())
161  {
162  std::copy(copyArray.begin(),
163  copyArray.end(),
164  theMinValuePercentArray.begin());
165  }
166  else
167  {
168  std::copy(copyArray.begin(),
169  copyArray.begin() + copyArray.size(),
170  theMinValuePercentArray.begin());
171  }
172  }
173  if(numberOfBands != theMaxValuePercentArray.size())
174  {
175  copyArray = theMaxValuePercentArray;
176  theMaxValuePercentArray.resize(numberOfBands);
177  if(numberOfBands < copyArray.size())
178  {
179  std::copy(copyArray.begin(),
180  copyArray.end(),
181  theMaxValuePercentArray.begin());
182  }
183  else
184  {
185  std::copy(copyArray.begin(),
186  copyArray.begin() + copyArray.size(),
187  theMaxValuePercentArray.begin());
188  }
189  }
190  if(numberOfBands > theMinValuePercentArray.size())
191  {
192  for(i = (ossim_uint32)theMinValuePercentArray.size(); i < numberOfBands; ++i)
193  {
194  theMinValuePercentArray[i] = 0.0;
195  theMaxValuePercentArray[i] = 0.0;
196  }
197  }
198  }
199  }
200 }
201 
203  const char* prefix)
204 {
205  theMinValuePercentArray.clear();
206  theMaxValuePercentArray.clear();
208  {
209  ossimNotify(ossimNotifyLevel_INFO) << "ossimMultiBandHistogramTileSource::loadState INFO: histogram filter was loaded" << std::endl;
210  ossimString regExpression = ossimString("^(") + ossimString(prefix) + ossimString(MIN_VALUE_PERCENT_KW) + ossimString("[0-9]+)");
211 
212  long numberOfSources = kwl.getNumberOfSubstringKeys(regExpression);
213 
214  unsigned long numberOfMatches = 0;
215  ossim_uint32 i = 0;
216  while(numberOfMatches < static_cast<unsigned long>(numberOfSources))
217  {
218  ossimString key = ossimString(MIN_VALUE_PERCENT_KW) + ossimString::toString(i);
219  const char* value = kwl.find(prefix, key);
220  if(value)
221  {
222  theMinValuePercentArray.push_back(ossimString(value).toDouble());
223  ++numberOfMatches;
224  }
225  ++i;
226  }
227 
228  numberOfMatches = 0;
229  i = 0;
230  regExpression = ossimString("^(") + ossimString(prefix) + ossimString(MAX_VALUE_PERCENT_KW) + ossimString("[0-9]+)");
231  numberOfSources = kwl.getNumberOfSubstringKeys(regExpression);
232  while(numberOfMatches < static_cast<unsigned long>(numberOfSources))
233  {
234  ossimString key = ossimString(MAX_VALUE_PERCENT_KW) + ossimString::toString(i);
235  const char* value = kwl.find(prefix, key);
236 
237  if(value)
238  {
239  theMaxValuePercentArray.push_back(ossimString(value).toDouble());
240  ++numberOfMatches;
241  }
242  ++i;
243  }
244 
245  ossimString algorithm = kwl.find(prefix,
246  ALGORITHM_KW);
247  algorithm = algorithm.downcase();
249  if(algorithm != "")
250  {
251  if(algorithm.contains("linear"))
252  {
254  }
255  }
256  }
257  return true;
258 }
259 
261  const char* prefix)const
262 {
264  ossim_int32 i = 0;
265 
266  for(i = 0; i< (ossim_int32)theMinValuePercentArray.size();++i)
267  {
268  kwl.add(prefix,
269  ossimString(MIN_VALUE_PERCENT_KW) + ossimString::toString(i),
271  true);
272  }
273  for(i = 0; i< (ossim_int32)theMaxValuePercentArray.size();++i)
274  {
275  kwl.add(prefix,
276  ossimString(MAX_VALUE_PERCENT_KW) + ossimString::toString(i),
278  true);
279  }
280  ossimString algorithmString;
281 
282  switch(theAlgorithm)
283  {
285  {
286  algorithmString = "linear_stretch";
287  break;
288  }
290  {
291  algorithmString = "none";
292  break;
293  }
294  default:
295  {
296  algorithmString = "none";
297  break;
298  }
299  }
300 
301  kwl.add(prefix,
302  ALGORITHM_KW,
303  algorithmString.c_str(),
304  true);
305 
306  return true;
307 }
308 
309 template <class T>
311  T /* dummyVariable */, ossimRefPtr<ossimImageData>& tile)
312 {
313  if(!getHistogram())
314  {
315  return tile;
316  }
317 
319  if(histo.valid())
320  {
321  ossim_uint32 maxBands = ( (histo->getNumberOfBands() >
322  tile->getNumberOfBands())?
323  tile->getNumberOfBands():
324  histo->getNumberOfBands());
325 
326  long offsetUpperBound = tile->getHeight()*tile->getWidth();
327 
328  for(ossim_uint32 band = 0; band < maxBands; ++band)
329  {
330  ossimRefPtr<ossimHistogram> h = histo->getHistogram(band);
331 
332  T* buf = static_cast<T*>(tile->getBuf(band));
333  T np = static_cast<T>(tile->getNullPix(band));
334  T minPix = static_cast<T>(tile->getMinPix(band));
335  T maxPix = static_cast<T>(tile->getMaxPix(band));
336  T range = (maxPix - minPix);
337  if(h.valid()&&buf)
338  {
339 
340  double maxClip = minPix + (h->HighClipVal(theMaxValuePercentArray[band])/(double)h->GetRes())*range;
341  double minClip = minPix + (h->LowClipVal(theMinValuePercentArray[band])/(double)h->GetRes())*range;
342  double delta = (maxClip - minClip);
343 
344  if(fabs(delta) > 0.0)
345  {
346  for(long offset = 0; offset < offsetUpperBound; ++offset)
347  {
348  if(buf[offset] != np)
349  {
350  if(buf[offset] < minClip)
351  {
352  buf[offset] = minPix;
353  }
354  else if(buf[offset] > maxClip)
355  {
356  buf[offset] = maxPix;
357  }
358  else
359  {
360  T value = static_cast<T>(minPix + ((buf[offset] - minClip)/delta)*range);
361  buf[offset] = value>maxPix?maxPix:value;
362  }
363  }
364  }
365  }
366  }
367  }
368 
369  tile->validate();
370  }
371 
372  return tile;
373 }
OSSIMDLLEXPORT void ossimSetError(const char *className, ossim_int32 error, const char *fmtString=0,...)
16 bit unsigned integer (15 bits used)
virtual ossim_uint32 getWidth() const
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
ossimRefPtr< ossimImageData > runLinearStretchAlgorithm(T dummyVariable, ossimRefPtr< ossimImageData > &tile)
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=NULL)
Method to the load (recreate) the state of an object from a keyword list.
virtual const ossim_float64 * getMaxPix() const
virtual ossim_uint32 getNumberOfBands() const
ossim_uint32 getNumberOfSubstringKeys(const ossimString &regularExpression) const
virtual void setImageRectangle(const ossimIrect &rect)
Represents serializable keyword/value map.
bool valid() const
Definition: ossimRefPtr.h:75
ossimMultiBandHistogramAlgorithm theAlgorithm
const char * find(const char *key) const
bool contains(char aChar) const
Definition: ossimString.h:58
static ossimString toString(bool aValue)
Numeric to string methods.
virtual void disableSource()
Definition: ossimSource.cpp:89
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
16 bit unsigned integer (14 bits used)
static const ossimErrorCode OSSIM_ERROR
16 bit unsigned integer (13 bits used)
unsigned short ossim_uint16
virtual ossimRefPtr< ossimMultiResLevelHistogram > getHistogram()
virtual void initialize()
Initialize the data buffer.
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual void loadTile(const void *src, const ossimIrect &src_rect, ossimInterleaveType il_type)
int GetRes() const
ossim_uint32 getNumberOfBands() const
static ossimImageDataFactory * instance()
RTTI_DEF1(ossimMultiBandHistogramTileSource, "ossimMultiBandHistogramTileSource", ossimImageSourceHistogramFilter) ossimMultiBandHistogramTileSource
virtual ossimDataObjectStatus validate() const
ossimImageSource * theInputConnection
unsigned int ossim_uint32
virtual const ossim_float64 * getNullPix() const
32 bit normalized floating point
ossimRefPtr< ossimHistogram > getHistogram(ossim_int32 band)
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tileRect, ossim_uint32 resLevel=0)
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
float HighClipVal(float clip_fraction) const
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
float LowClipVal(float clip_fraction) const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=NULL) const
Method to save the state of an object to a keyword list.
virtual const ossim_float64 * getMinPix() const
virtual ossimScalarType getScalarType() const
64 bit normalized floating point
16 bit unsigned integer (11 bits used)
virtual const void * getBuf() const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
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
32 bit floating point
ossimRefPtr< ossimMultiBandHistogram > getMultiBandHistogram(ossim_uint32 resLevel) const
16 bit unsigned iteger
64 bit floating point
16 bit signed integer
unsigned char ossim_uint8
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
8 bit unsigned iteger
int ossim_int32
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< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)
16 bit unsigned integer (12 bits used)