OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimScalarRemapper.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2001 ImageLinks Inc.
3 //
4 // License: MIT
5 //
6 // Author: David Burken
7 //
8 // Description:
9 //
10 // Contains class definition for ossimScalarRemapper.
11 // This class is used to remap image data from one scalar type to another.
12 //
13 //*******************************************************************
14 // $Id$
15 
22 #include <ossim/base/ossimTrace.h>
24 
25 #include <iostream>
26 
28  "ossimScalarRemapper",
30 
31 static const ossimTrace traceDebug("ossimScalarRemapper:debug");
32 
34  :
36  theNormBuf(NULL),
37  theTile(NULL),
38  theOutputScalarType(OSSIM_UINT8),
39  theByPassFlag(false),
40  thePreserveMagnitudeFlag(false)
41 {
42 }
43 
45  ossimScalarType outputScalarType)
46  :
47  ossimImageSourceFilter(inputSource),
48  theNormBuf(NULL),
49  theTile(NULL),
50  theOutputScalarType(outputScalarType),
51  theByPassFlag(false),
52  thePreserveMagnitudeFlag(false)
53 {
54  if(inputSource)
55  {
56  if (inputSource->getOutputScalarType() == outputScalarType)
57  {
58  // Disable this filter simply return the inputSource's data.
59  theByPassFlag = true;
60 
61  // Nothing else to do.
62  }
63  else
64  {
65  theByPassFlag = false;
66  }
67  }
68  else
69  {
70  theByPassFlag = true;
71  }
72 }
73 
75 {
76  destroy();
77 }
78 
80 {
81  if (theNormBuf)
82  {
83  delete [] theNormBuf;
84  theNormBuf = NULL;
85  }
86  theTile = NULL;
87 }
88 
90  const ossimIrect& tileRect, ossim_uint32 resLevel)
91 {
92  if(traceDebug())
93  {
94  std::cout << "ossimScalarRemapper::getTile START ... " << tileRect << " RES: " << resLevel << std::endl;
95  }
97  {
99  }
100 
101  // Fetch tile from pointer from the input source.
102  ossimRefPtr<ossimImageData> inputTile =
103  theInputConnection->getTile(tileRect, resLevel);
104 
105  // Check for remap bypass:
106  if ( !isSourceEnabled()||theByPassFlag )
107  {
108  return inputTile;
109  }
110 
111  // Check for first time through.
112  if ( !theTile.valid() )
113  {
114  allocate();
115 
116  if ( !theTile.valid() )
117  {
118  // This can happen if input/output scalars are the same.
119  return inputTile;
120  }
121  }
122 
123  // Capture the size prior to a possible resize.
124  ossim_uint32 oldSize = theTile->getSize();
125 
126  // Set the origin,bands of the output tile.
127  theTile->setImageRectangle(tileRect);
128 
129  ossim_uint32 newSize = theTile->getSize();
130 
131  // Check for size change before possible return.
132  if(newSize != oldSize)
133  {
134  if(theNormBuf)
135  {
136  //---
137  // Delete the current buffer since it is the wrong size.
138  //
139  // NOTE:
140  // We won't reallocate it yet since we could return without using it.
141  // It will be checked prior to using later.
142  //---
143  delete [] theNormBuf;
144  theNormBuf = NULL;
145  }
146  }
147 
148  if ( !inputTile.valid() ||
149  (inputTile->getDataObjectStatus() == OSSIM_NULL) ||
150  (inputTile->getDataObjectStatus() == OSSIM_EMPTY) )
151  {
152  //---
153  // Since the filter is enabled, return theTile which is of the
154  // correct scalar type.
155  //---
156  theTile->makeBlank();
157  return theTile;
158  }
159 
160  if (!theNormBuf) // First time through or size changed and was deleted...
161  {
162  theNormBuf = new double[newSize];
163  memset(theNormBuf, '\0', newSize);
164  }
165 
166  if (inputTile->getScalarType() == theOutputScalarType)
167  {
168  // Scalar types already the same. Nothing to do...
169  return inputTile;
170  }
171 
173  {
174  switch(inputTile->getScalarType())
175  {
176  case OSSIM_SINT16:
177  {
179  break;
180  }
181  case OSSIM_UINT16:
182  {
184  break;
185  }
186  case OSSIM_SINT32:
187  {
189  break;
190  }
191  case OSSIM_UINT32:
192  {
194  break;
195  }
196  default:
197  {
198  break;
199  }
200  }
201  }
202  else
203  {
204  switch(inputTile->getScalarType())
205  {
207  {
208  // Un-normalize and copy the buffer to the destination tile.
210  static_cast<double*>( inputTile->getBuf() ) );
211  break;
212  }
214  {
215  // Un-normalize and copy the buffer to the destination tile.
217  static_cast<float*>( inputTile->getBuf() ) );
218  break;
219  }
220  default:
221  {
222  //---
223  // NOTE: stretchMinMax commented out as it was incorrectly not resetting
224  // the tile's min/max data members; hence, messing up the downstream copy
225  // to normalized buffer. (drb 02 Feb. 2013)
226  // Special case. Stretch assuming caller want to view this data.
227  //---
228  // inputTile->stretchMinMax();
229 
230  // Normalize and copy the source tile to a buffer.
232 
233  // Un-normalize and copy the buffer to the destination tile.
235 
236  break;
237  }
238  }
239  }
240 
241  theTile->validate();
242  if(traceDebug())
243  {
244  std::cout << "ossimScalarRemapper::getTile END ... " << tileRect << " RES: " << resLevel << std::endl;
245  }
246 
247 
248  return theTile;
249 }
250 
252 {
254  {
255  return theOutputScalarType;
256  }
257 
259 }
260 
262 {
263  if (scalarType == OSSIM_SCALAR_UNKNOWN)
264  {
265  if(traceDebug())
266  {
268  << "ossimScalarRemapper::setOutputScalarType WARN:\n"
269  << "OSSIM_SCALAR_UNKNOWN passed to method. No action taken..."
270  << std::endl;
271  }
272  return;
273  }
274 
275  if (theInputConnection)
276  {
277  if ( scalarType == theInputConnection->getOutputScalarType() )
278  {
279  // Input same as output, nothing for us to do...
280  theByPassFlag = true;
281  }
282  else // Types not equal...
283  {
284  theByPassFlag = false;
285  destroy();
286  }
287  }
288  else // No input source, disable.
289  {
290  theByPassFlag = true;
291  }
292 
293  theOutputScalarType = scalarType;
294 }
295 
297 {
298  int scalar =
300 
301  if (scalar != ossimLookUpTable::NOT_FOUND)
302  {
303  setOutputScalarType(static_cast<ossimScalarType>(scalar));
304  }
305  else
306  {
307  if(traceDebug())
308  {
310  << "ossimScalarRemapper ERROR:"
311  << "\nUnknown scalar type: " << scalarType.c_str() << std::endl;
312  }
313  }
314 }
315 
317 {
318  //---
319  // Call the base class initialize.
320  // Note: This will reset "theInputConnection" if it changed...
321  //---
323 
324  if (theInputConnection)
325  {
326  // Set the bypass flag accordingly...
329  {
330  theByPassFlag = true;
331  }
332  else
333  {
334  theByPassFlag = false;
335  }
336 
337  if (theTile.valid())
338  {
339  //---
340  // Check for:
341  // - bypass
342  // - disabled(!enabled)
343  // - scalar change
344  // - band count change
345  //---
346  if ( theByPassFlag ||
347  !theEnableFlag ||
352  {
353  destroy(); // Reallocated first unbypassed getTile.
354  }
355  }
356  }
357 }
358 
360 {
361  destroy();
362 
363  if(!theInputConnection) // Nothing to do here.
364  {
365  setInitializedFlag(false);
366  theByPassFlag = true;
367  return;
368  }
369 
371  {
372  // default to OSSIM_UINT8
374  }
375 
376  if(theInputConnection &&
380  {
381  theByPassFlag = false;
382 
384 
385  // Initialize the tile.
386  theTile->initialize();
387 
388  // Set the base class flags to be initialized and enabled.
389  setInitializedFlag(true);
390 
391  } // End of "if(theInputConnection->isConnected()..."
392  else
393  {
394  // Set to not initialized and disabled.
395  setInitializedFlag(false);
396  theByPassFlag = true;
397  }
398 
399  if (traceDebug())
400  {
402  << "ossimScalarRemapper::allocate() DEBUG"
403  << "\ninput scalar: " << theInputConnection->getOutputScalarType()
404  << "\noutput scalar: " << getOutputScalarType()
405  << "\nenabled: " << (isSourceEnabled()?"true":"false")
406  << std::endl;
407  }
408 }
409 
411 {
412  if(!property) return;
413 
414  if(property->getName() == "Output scalar type")
415  {
417  getScalarTypeFromString(property->valueToString());
418  }
419  else
420  {
422  }
423 }
424 
426 {
427  if(name == "Output scalar type")
428  {
429  std::vector<ossimString> scalarNames;
430 
432  getTableSize();
433  ossim_int32 idx;
434 
435  for(idx = 0; idx < tableSize; ++idx)
436  {
437  scalarNames.push_back(ossimScalarTypeLut::instance()->
438  getEntryString(idx));
439  }
440  ossimStringProperty* stringProp =
441  new ossimStringProperty("Output scalar type",
443  false,
444  scalarNames);
445  stringProp->clearChangeType();
446  stringProp->setReadOnlyFlag(false);
447  stringProp->setCacheRefreshBit();
448 
449  return stringProp;
450  }
451 
453 }
454 
455 void ossimScalarRemapper::getPropertyNames(std::vector<ossimString>& propertyNames)const
456 {
458  propertyNames.push_back("Output scalar type");
459 }
460 
462  const char* prefix) const
463 {
465 
466  kwl.add(prefix,
469  true);
470  kwl.add(prefix, "elevation", ossimString::toString(thePreserveMagnitudeFlag).c_str());
471 
472  return true;
473 }
474 
476  const char* prefix)
477 {
479 
481  {
482  if(traceDebug())
483  {
485  << "ossimScalarRemapper::loadState\n"
486  << " ERROR detected in keyword list! State not loaded."
487  << std::endl;
488  }
489  return false;
490  }
491 
492  int scalar = ossimScalarTypeLut::instance()->getEntryNumber(kwl, prefix);
493 
494  if (scalar != ossimLookUpTable::NOT_FOUND)
495  {
496  setOutputScalarType(static_cast<ossimScalarType>(scalar));
497  }
498  const char* lookup = kwl.find(prefix, "elevation");
499  if(lookup)
500  {
502  }
503 
504  return true;
505 }
506 
508 {
510 }
511 
513 {
514  // if my properties have changed then just initialize
515  //
516  if(event.getObject() == this)
517  {
518  initialize();
519  }
520  else // if an input property has changed just check to see if the number
521  { // of bands has changed
522 
523  if(!theTile)
524  {
525  initialize();
526  }
527  else
528  {
530  if((int)theTile->getNumberOfBands() != b)
531  {
532  initialize();
533  }
534  }
535  }
536 }
537 
539 {
540  // if my properties have changed then just initialize
541  if(event.getObject() == this)
542  {
543  initialize();
544  }
545  else // if an input property has changed just check to see if the number
546  { // of bands has changed
547 
548  if(!theTile)
549  {
550  initialize();
551  }
552  else
553  {
555  if((int)theTile->getNumberOfBands() != b)
556  {
557  initialize();
558  }
559  }
560  }
561 }
562 
564 {
566  {
568  {
570  }
571  }
572  else if(theTile.valid())
573  {
574  if (band < theTile->getNumberOfBands())
575  {
576  return theTile->getNullPix(band);
577  }
578  }
579 
581 }
582 
584 {
586  {
588  {
589  return theInputConnection->getMinPixelValue(band);
590  }
591  }
592  else if(theTile.valid())
593  {
594  if (band < theTile->getNumberOfBands())
595  {
596  return theTile->getMinPix(band);
597  }
598  }
599 
601 }
602 
604 {
606  {
608  {
609  return theInputConnection->getMaxPixelValue(band);
610  }
611  }
612  else if(theTile.valid())
613  {
614  if (band < theTile->getNumberOfBands())
615  {
616  return theTile->getMaxPix(band);
617  }
618  }
619 
621 }
622 
624 {
625  thePreserveMagnitudeFlag = value;
626 }
627 
629 {
630  return ossimString("Scalar Remapper, filters between different scalar types.");
631 }
632 
634 {
635  return ossimString("Scalar Remapper");
636 }
637 
virtual void valueToString(ossimString &valueResult) const =0
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual const ossim_float64 * getMaxPix() const
virtual double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
virtual ossim_uint32 getNumberOfBands() const
virtual void refreshEvent(ossimRefreshEvent &event)
virtual void setPreserveMagnitude(bool value)
virtual void setImageRectangle(const ossimIrect &rect)
16 bit unsigned integer
Represents serializable keyword/value map.
bool theEnableFlag
Definition: ossimSource.h:62
void copyTileToFloatBuffer(T, ossim_float32 *buf) const
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
bool valid() const
Definition: ossimRefPtr.h:75
virtual ossimString getOutputScalarTypeString() const
Returns the output pixel type of the tile source as a string.
const char * find(const char *key) const
virtual ossimString getEntryString(ossim_int32 entry_number) const
static ossimString toString(bool aValue)
Numeric to string methods.
virtual ossimString getLongName() const
16 bit signed integer
virtual ossimDataObjectStatus getDataObjectStatus() const
void destroy()
Deletes allocated memory.
static const ossimErrorCode OSSIM_ERROR
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual void setReadOnlyFlag(bool flag)
virtual ~ossimScalarRemapper()
unsigned short ossim_uint16
virtual void setInitializedFlag(bool flag)
32 bit unsigned integer
virtual void initialize()
Initialize the data buffer.
virtual ossimString getShortName() const
virtual ossim_int32 getEntryNumber(const char *entry_string, bool case_insensitive=true) 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)
static ossimScalarTypeLut * instance()
Returns the static instance of an ossimScalarTypeLut object.
OSSIM_DLL double defaultMin(ossimScalarType scalarType)
Definition: ossimCommon.cpp:73
void clearChangeType()
void allocate()
Called on first getTile, will initialize all data needed.
static ossimImageDataFactory * instance()
const ossimObject * getObject() const
This is the originating object that originally produced the event.
Definition: ossimEvent.cpp:64
virtual void setOutputScalarType(ossimScalarType scalarType)
Sets the output scalar type.
virtual ossimDataObjectStatus validate() const
signed short ossim_sint16
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
32 bit signed integer
bool toBool() const
String to numeric methods.
OSSIM_DLL double defaultNull(ossimScalarType scalarType)
ossimImageSource * theInputConnection
unsigned int ossim_uint32
virtual const ossim_float64 * getNullPix() const
32 bit normalized floating point
ossimRefPtr< ossimImageData > theTile
signed int ossim_sint32
virtual void copyTileToNormalizedBuffer(ossim_float64 *buf) const
Copies entire tile to buf passed in.
virtual double getNullPixelValue(ossim_uint32 band) const
Sets the current resolution level.
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 void propertyEvent(ossimPropertyEvent &event)
Override base class so that a disableSource event does not reinitialize the object and enable itself...
virtual void copyNormalizedBufferToTile(ossim_float64 *buf)
Copies buf passed in to tile.
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tile_rect, ossim_uint32 resLevel=0)
ossimScalarType theOutputScalarType
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
ossimScalarType
virtual void initialize()
virtual const ossim_float32 * getFloatBuf() const
virtual const ossim_float64 * getMinPix() const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
virtual ossimScalarType getScalarType() const
virtual void makeBlank()
Initializes data to null pixel values.
64 bit normalized floating point
OSSIM_DLL double defaultMax(ossimScalarType scalarType)
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=NULL) const
Method to save the state of an object to a keyword list.
virtual ossimErrorCode getErrorStatus() const
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
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 ossim_uint32 getSize() const
Returns the total number of pixels in a tile for all bands.
virtual const void * getBuf() const
virtual ossimScalarType getOutputScalarType() const
Returns the output pixel type of the tile source.
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
static const char * SCALAR_TYPE_KW
8 bit unsigned integer
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
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 bool loadState(const ossimKeywordlist &kwl, const char *prefix=NULL)
Method to the load (recreate) the state of an object from a keyword list.
void setCacheRefreshBit()
const ossimString & getName() const
int ossim_int32
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)