OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimNBandToIndexFilter.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2005 Garrett Potts
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 //*************************************************************************
11 // $Id: ossimNBandToIndexFilter.cpp 23325 2015-05-27 13:03:38Z gpotts $
12 
20 
21 
22 static const char* KEEP_QUANTIZED_VALUE_FLAG_KW = "keep_quantized_value_flag";
23 
25  "ossimNBandToIndexFilter",
27 
30  theLut(0),
31  theTile(0),
32  theKeepQuantizedValueFlag(false)
33 {
34 }
35 
37 {
38 }
39 
41 {
43 
44  // Force allocate on next getTile.
45  theTile = 0;
46  if(theLut.valid())
47  {
49  {
53  ossim_uint32 idx = 0;
54  if(theMinValues.size() != bands)
55  {
56  theMinValues.resize(bands);
57  theMaxValues.resize(bands);
58  theNullValues.resize(bands);
59  }
60  std::fill(theNullValues.begin(),
61  theNullValues.end(), 0);
62  for(idx = 0; idx < bands; ++idx)
63  {
64  theLut->getMinMax(idx, minValue, maxValue);
65  theMinValues[idx] = minValue;
66  theMaxValues[idx] = maxValue;
68  }
69  }
70  else
71  {
72  if(theMinValues.size() != 1)
73  {
74  theMinValues.resize(1);
75  theMaxValues.resize(1);
76  theNullValues.resize(1);
77  }
78  theMinValues[0] = 0;
81  }
82  }
83 }
84 
86 {
87  theTile = 0;
88 
90  {
92  if(theTile.valid())
93  {
95  }
96  }
97 }
98 
100 {
102  theTile = 0;
103 }
104 
106  ossim_uint32 resLevel)
107 {
108  if(!isSourceEnabled())
109  {
110  return ossimImageSourceFilter::getTile(origin, resLevel);
111  }
112  if(!theInputConnection)
113  {
115  }
116 
117  if(!theTile.valid())
118  {
119  allocate();
120  }
121 
123  resLevel);
124 
125  if(!theTile.valid() || !input.valid())
126  {
127  return input;
128  }
129 
130  theTile->setImageRectangle(origin);
132  theTile->makeBlank();
133 
134  return convertInputTile(input.get());
135 }
136 
138 {
139  switch(getOutputScalarType())
140  {
141  case OSSIM_UINT8:
142  {
144  tile);
145  break;
146  }
147  case OSSIM_UINT16:
148  {
150  tile);
151  break;
152  }
153  case OSSIM_UINT32:
154  {
156  tile);
157  break;
158  }
159  default:
160  {
161  ossimNotify(ossimNotifyLevel_WARN) << "ossimNBandToIndexFilter::convertInputTile: Unsupported scalar type for conversion" << std::endl;
162  }
163  }
164 
165  return theTile;
166 }
167 
168 template <class T>
170  ossimImageData* inputTile)
171 {
172  switch(inputTile->getScalarType())
173  {
174  case OSSIM_UINT8:
175  {
177  outputDummy,
178  inputTile);
179  break;
180  }
181  case OSSIM_SINT8:
182  {
184  outputDummy,
185  inputTile);
186  }
187  case OSSIM_UINT16:
188  {
190  outputDummy,
191  inputTile);
192  }
193  case OSSIM_SINT32:
194  {
196  outputDummy,
197  inputTile);
198  }
199  case OSSIM_UINT32:
200  {
202  outputDummy,
203  inputTile);
204  }
205  default:
206  {
207  ossimNotify(ossimNotifyLevel_WARN) << "convertInputTileToOutputTemplate::convertOutputTileTemplate: Unsupported scalar type for conversion." << std::endl;
208  }
209  }
210 
211  return theTile;
212 }
213 
214 template <class T1, class T2>
216  T1 /* inputDummy */,
217  T2 /* outputDummy */,
218  ossimImageData* inputTile)
219 {
220  if (!inputTile)
221  {
222  return theTile;
223  }
224 
225  const ossim_uint32 BANDS = inputTile->getNumberOfBands();
226 
227  if (!BANDS)
228  {
229  return theTile;
230  }
231 // T2** outBand = (T2*)theTile->getBuf();
232 
233  ossim_uint32 numberOfBands = theLut->getNumberOfBands();
234 
235  std::vector<ossimNBandLutDataObject::LUT_ENTRY_TYPE> bandValuesTemplate(numberOfBands);
236 
237  ossimNBandLutDataObject::LUT_ENTRY_TYPE* bandValues = &bandValuesTemplate.front();//new ossimNBandLutDataObject::LUT_ENTRY_TYPE[numberOfBands];
238 
239  std::fill(bandValuesTemplate.begin(), bandValuesTemplate.end(), 0);
240  numberOfBands = ossim::min(BANDS,
241  numberOfBands);
242  if(!numberOfBands) return theTile;
243  std::vector<T1*> band(numberOfBands);
244  std::vector<T2*> outBand(numberOfBands);
245  // T1** band = new T1*[numberOfBands];
246  //T2** outBand = new T2*[numberOfBands];
247  ossim_int32 idx = 0;
248 
249  for(idx = 0; idx < (ossim_int32)numberOfBands; ++idx)
250  {
251  band[idx] = (T1*)inputTile->getBuf(idx);
252  outBand[idx] = (T2*)theTile->getBuf(idx);
253  }
254  if(band[0])
255  {
256  if(inputTile->getDataObjectStatus() == OSSIM_FULL)
257  {
258  ossim_uint32 upper = inputTile->getWidth()*inputTile->getHeight();
259  ossim_uint32 idx2=0;
260  for(ossim_uint32 offset = 0; offset < upper; ++offset)
261  {
262  for(idx2 = 0; idx2 < numberOfBands; ++idx2)
263  {
264  bandValues[idx2] = *band[idx2];
265  ++band[idx2];
266  }
267  //---
268  // Note call the ossimNBandLutDataObject::findIndex that takes a size as the input
269  // data may have dropped the alpha channel.
270  //---
271  idx = theLut->findIndex(bandValues, numberOfBands);
272 
274  {
275  *outBand[0] = (T2)idx;
276  ++outBand[0];
277  }
278  else
279  {
280  for(idx2 = 0; idx2 < numberOfBands; ++idx2)
281  {
282  if(idx >=0)
283  {
284  *outBand[idx2] = (T2)(*theLut)[idx][idx2];
285  ++outBand[idx2];
286  }
287  }
288  }
289  }
290  }
291  else if(inputTile->getDataObjectStatus() == OSSIM_PARTIAL)
292  {
293  ossim_uint32 upper = inputTile->getWidth()*inputTile->getHeight();
294  ossim_uint32 idx2=0;
295  for(ossim_uint32 offset = 0; offset < upper; ++offset)
296  {
297  for(idx2 = 0; idx2 < numberOfBands; ++idx2)
298  {
299  bandValues[idx2] = *band[idx2];
300  ++band[idx2];
301  }
302  idx = -1;
303  if(!inputTile->isNull(offset))
304  {
305  //---
306  // Note call the ossimNBandLutDataObject::findIndex that takes a size as the input
307  // data may have dropped the alpha channel.
308  //---
309  idx = theLut->findIndex(bandValues, numberOfBands);
310  }
312  {
313  if(idx>=0)
314  {
315  *outBand[0] = (T2)idx;
316  }
317  ++outBand[0];
318  }
319  else
320  {
321  for(idx2 = 0; idx2 < numberOfBands; ++idx2)
322  {
323  if(idx >=0)
324  {
325  if(idx>=0)
326  {
327  *outBand[idx2] = (T2)(*theLut)[idx][idx2];
328  }
329  }
330  ++outBand[idx2];
331  }
332  }
333  }
334  }
335  }
337  {
338  theTile->validate();
339  }
340  else
341  {
343  }
344 
345 
346  return theTile;
347 }
348 
350  const char* prefix)const
351 {
352  ossimString newPrefix = prefix;
353  newPrefix = newPrefix + "lut.";
354 
355  if(theLut.valid())
356  {
357  theLut->saveState(kwl, newPrefix.c_str());
358  }
359 
360  kwl.add(prefix,
361  KEEP_QUANTIZED_VALUE_FLAG_KW,
362  theKeepQuantizedValueFlag?"true":"false",
363  true);
364 
365  return ossimImageSourceFilter::saveState(kwl, prefix);
366 }
367 
368 bool ossimNBandToIndexFilter::loadState(const ossimKeywordlist& kwl, const char* prefix)
369 {
370  ossimString newPrefix = prefix;
371  newPrefix = newPrefix + "lut.";
372 
373  if(!theLut.valid())
374  {
376  }
377  theLut->loadState(kwl, newPrefix.c_str());
378 
379  const char* flag = kwl.find(prefix, KEEP_QUANTIZED_VALUE_FLAG_KW);
380  if(flag)
381  {
383  }
384  return ossimImageSourceFilter::loadState(kwl, prefix);
385 }
386 
388 {
390 
391  // only support 3 band integral values, no floating point for now.
392  //
393  if(result)
394  {
395 
397  if(!theLut.valid() ||
398  (theLut->getNumberOfEntries()<1)||
399  (inputScalarType==OSSIM_FLOAT32)||
400  (inputScalarType==OSSIM_FLOAT64)||
402  {
403  result = false;
404  }
405  }
406 
407  return result;
408 }
409 
411 {
412  if(isSourceEnabled())
413  {
415  {
416  if(theLut.valid())
417  {
418  return theLut->getNumberOfBands();
419  }
420  }
421  else
422  {
423  return 1;
424  }
425  }
426 
428 }
429 
431 {
432  if(isSourceEnabled())
433  {
434 
435  ossim_uint32 numberOfEntries = theLut->getNumberOfEntries();
436 
437  if(numberOfEntries < 257)
438  {
439  return OSSIM_UCHAR;
440  }
441  else if(numberOfEntries < 65537)
442  {
443  return OSSIM_UINT16;
444  }
445  else
446  {
447  return OSSIM_UINT32;
448  }
449  }
450 
452 }
453 
455 {
456  theLut = new ossimNBandLutDataObject(lut);
457 }
458 
460 {
461  if(isSourceEnabled())
462  {
463  if(theLut->getNullPixelIndex() >= 0)
464  {
465  return theLut->getNullPixelIndex();
466  }
467  }
468 
470 }
471 
473 {
474  if(isSourceEnabled())
475  {
476  if(band < theMinValues.size())
477  {
478  return (double)theMinValues[band];
479  }
480  }
481 // if(isSourceEnabled())
482 // {
483 // if(getNullPixelValue() != 0)
484 // {
485 // return 0;
486 // }
487 // else
488 // {
489 // return 1;
490 // }
491 // return 0;
492 // }
493 
495 }
496 
498 {
499  if(isSourceEnabled())
500  {
501  if(band < theMaxValues.size())
502  {
503  return (double)theMaxValues[band];
504  }
505 // if(theLut->getNullPixelIndex() != ((ossim_int32)(theLut->getNumberOfEntries())-1))
506 // {
507 // return theLut->getNumberOfEntries();
508 // }
509 // else
510 // {
511 // return (theLut->getNumberOfEntries()-1);
512 // }
513  }
514 
516 }
517 
519 {
520  if(property->getName() == ossimKeywordNames::FILENAME_KW)
521  {
522  ossimKeywordlist kwl;
523  if(kwl.addFile(ossimFilename(property->valueToString())))
524  {
526  theLut->loadState(kwl);
527  }
528  }
529  else if(property->getName() == KEEP_QUANTIZED_VALUE_FLAG_KW)
530  {
531  theKeepQuantizedValueFlag = property->valueToString().toBool();
532  }
533  else
534  {
536  }
537 }
538 
540 {
542  {
545  property->setCacheRefreshBit();
546 
547  return property;
548  }
549  else if(name == KEEP_QUANTIZED_VALUE_FLAG_KW)
550  {
552  property->setCacheRefreshBit();
553 
554  return property;
555  }
557 }
558 
559 void ossimNBandToIndexFilter::getPropertyNames(std::vector<ossimString>& propertyNames)const
560 {
562 
563  propertyNames.push_back(ossimKeywordNames::FILENAME_KW);
564  propertyNames.push_back(KEEP_QUANTIZED_VALUE_FLAG_KW);
565 }
void setLut(ossimNBandLutDataObject &lut)
8 bit signed integer
virtual void valueToString(ossimString &valueResult) const =0
virtual ossim_uint32 getWidth() const
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
ossimRefPtr< ossimNBandLutDataObject > theLut
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual ossim_uint32 getNumberOfBands() const
64 bit floating point
virtual void setImageRectangle(const ossimIrect &rect)
ossim_uint32 getNumberOfBands() const
16 bit unsigned integer
virtual double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Represents serializable keyword/value map.
bool addFile(const char *file)
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
virtual void disableSource()
Definition: ossimSource.cpp:89
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
signed char ossim_sint8
void getMinMax(ossim_uint32 band, LUT_ENTRY_TYPE &minValue, LUT_ENTRY_TYPE &maxValue)
Will return the min max value for a given band.
32 bit floating point
virtual double getNullPixelValue(ossim_uint32 band=0) const
Each band has a null pixel associated with it.
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
unsigned short ossim_uint16
32 bit unsigned integer
virtual ossimRefPtr< ossimImageData > convertInputTile(ossimImageData *inputTile)
virtual void initialize()
Initialize the data buffer.
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
bool isNull(ossim_uint32 offset) const
virtual double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
ossimRefPtr< ossimImageData > convertInputTileToOutputTemplate(T1 inputDummy, T2 outputDummy, ossimImageData *inputTile)
static ossimImageDataFactory * instance()
virtual ossimDataObjectStatus validate() const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
std::vector< ossimNBandLutDataObject::LUT_ENTRY_TYPE > theMinValues
32 bit signed integer
bool toBool() const
String to numeric methods.
ossim_uint32 getNumberOfEntries() const
ossimImageSource * theInputConnection
unsigned int ossim_uint32
signed int ossim_sint32
ossimRefPtr< ossimImageData > convertOutputTileTemplate(T inputDummy, ossimImageData *inputTile)
virtual bool isSourceEnabled() 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
ossimScalarType
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
Pushes this&#39;s names onto the list of property names.
virtual ossimScalarType getScalarType() const
virtual void makeBlank()
Initializes data to null pixel values.
ossimRefPtr< ossimImageData > theTile
std::vector< ossimNBandLutDataObject::LUT_ENTRY_TYPE > theNullValues
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
virtual const void * getBuf() const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &origin, ossim_uint32 resLevel=0)
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
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
class ossimNBandLutDataObject
virtual void setDataObjectStatus(ossimDataObjectStatus status) const
Full list found in ossimConstants.h.
ossim_uint32 findIndex(ossim_int32 *values) const
This method gives you a way to find an entry index closest to the passed in band values.
ossim_int32 getNullPixelIndex() const
8 bit unsigned integer
RTTI_DEF1(ossimNBandToIndexFilter, "ossimNBandToIndexFilter", ossimImageSourceFilter)
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Loads the state of the object by reading in the keywords listed in the save state.
static const char * FILENAME_KW
std::vector< ossimNBandLutDataObject::LUT_ENTRY_TYPE > theMaxValues
unsigned char ossim_uint8
virtual double getNullPixelValue(ossim_uint32 band=0) const
Each band has a null pixel associated with it.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
virtual void setProperty(ossimRefPtr< ossimProperty > property)
Will set the property whose name matches the argument "property->getName()".
8 bit unsigned iteger
const ossimString & getName() const
int ossim_int32
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)