OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimLinearStretchRemapper.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: Oscar Kramer
8 //
9 //*************************************************************************
13 #include <ossim/base/ossimCommon.h>
15 #include <ossim/base/ossimTrace.h>
20 
22 
23 static const char* MIN_VALUE_KW = "min_value";
24 static const char* MAX_VALUE_KW = "max_value";
25 
27 {
28  setDescription("Remaps input from min-max to full range (0-1) of normalized float..");
29 }
30 
32 {
33 }
34 
36  ossim_uint32 resLevel)
37 {
39  return 0;
40 
41  ossimRefPtr<ossimImageData> tile = theInputConnection->getTile(tileRect, resLevel);
42  if ( !theEnableFlag )
43  return tile;
44 
45  if (!tile || !tile->getBuf())
46  return 0;
47  if(!m_tile)
48  {
49  initialize();
50  if (!m_tile)
51  return 0;
52  }
53 
54  m_tile->setImageRectangle(tileRect);
55  m_tile->makeBlank();
56 
57  // Quick handling special case of empty input tile:
58  if (tile->getDataObjectStatus() == OSSIM_EMPTY)
59  return m_tile;
60 
61  ossim_uint32 maxLength = tile->getWidth()*tile->getHeight();
62  double pixel;
63  double null_pixel = theInputConnection->getNullPixelValue();
64 
66  for (int i=0; i<numBands; ++i)
67  {
68  double delta = m_maxValues[i] - m_minValues[i];
69  if (delta == 0)
70  continue;
71  double* outBuf = m_tile->getDoubleBuf(i);
72  for (ossim_uint32 offset=0; offset<maxLength; ++offset)
73  {
74  // Convert input pixel to a double index value:
75  switch(tile->getScalarType())
76  {
77  case OSSIM_DOUBLE:
79  pixel = tile->getDoubleBuf(i)[offset];
80  break;
81  case OSSIM_SSHORT16:
82  pixel = (double)(tile->getSshortBuf(i)[offset]);
83  break;
84  case OSSIM_FLOAT:
86  pixel = (double)(tile->getFloatBuf(i)[offset]);
87  break;
88  case OSSIM_UCHAR:
89  pixel = (double)(tile->getUcharBuf(i)[offset]);
90  break;
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  pixel = (double)(tile->getUshortBuf(i)[offset]);
98  break;
99  default:
100  break;
101  }
102 
103  // Do not remap null pixels, leave the output pixel "blank" which is null-pixel value:
104  if (pixel == null_pixel)
105  continue;
106 
107  // Do linear remap:
108  pixel = (pixel-m_minValues[i])/delta;
109  if (pixel < 0.0)
110  pixel = 0.0;
111  if (pixel > 1.0)
112  pixel = 1.0;
113  outBuf[offset] = pixel;
114  }
115  }
116  m_tile->validate();
117  return m_tile;
118 }
119 
121 {
122  if(!theInputConnection)
123  return;
124 
130  if(m_tile.valid())
131  {
132  m_tile->initialize();
133  }
134 }
135 
137 {
138  // This assigns theInputConnection if one is there.
140 
141  m_tile = 0;
142  if ( theInputConnection )
143  {
144  // Initialize the chain on the left hand side of us.
145  //theInputConnection->initialize(); This is done by base class
146  int numBands = theInputConnection->getNumberOfOutputBands();
147  if ((m_minValues.size() != numBands) && (m_maxValues.size() != numBands))
148  {
149  m_minValues.resize(numBands);
150  m_maxValues.resize(numBands);
151  for (int i=0; i<numBands; ++i)
152  {
155  }
156  }
157  allocate();
158  }
159 }
160 
161 bool ossimLinearStretchRemapper::saveState(ossimKeywordlist& kwl, const char* prefix)const
162 {
163  bool rtn_stat = true;
164  ostringstream minstr, maxstr;
165  int numBands = m_minValues.size();
166  if ((numBands == 0) || (numBands != m_maxValues.size()))
167  rtn_stat = false;
168 
169  for (int i=0; i<numBands; ++i)
170  {
171  minstr << " " << m_minValues[i];
172  maxstr << " " << m_maxValues[i];
173  }
174 
175  kwl.add(prefix, MIN_VALUE_KW, minstr.str().c_str(), true);
176  kwl.add(prefix, MAX_VALUE_KW, maxstr.str().c_str(), true);
177 
178  rtn_stat &= ossimImageSourceFilter::saveState(kwl, prefix);
179  return rtn_stat;
180 }
181 
182 bool ossimLinearStretchRemapper::loadState(const ossimKeywordlist& kwl, const char* prefix)
183 {
184  bool return_state = true;
185  m_minValues.clear();
186  m_maxValues.clear();
187 
188  vector<ossimString> minimums;
189  vector<ossimString> maximums;
190  ossimString minVals = kwl.find(prefix, MIN_VALUE_KW);
191  ossimString maxVals = kwl.find(prefix, MAX_VALUE_KW);
192  if(!minVals.empty() && !maxVals.empty())
193  {
194  minimums = minVals.split(" ");
195  maximums = maxVals.split(" ");
196  if (minimums.size() != maximums.size())
197  return false;
198 
199  for (int i=0; i<minimums.size(); ++i)
200  {
201  m_minValues[i] = minimums[i].toDouble();
202  m_maxValues[i] = maximums[i].toDouble();
203  }
204  }
205  return true;
206 }
207 
209 {
210  if (band< m_minValues.size())
211  return m_minValues[band];
212  return ossim::nan();
213 }
214 
216 {
217  if (band< m_maxValues.size())
218  return m_maxValues[band];
219  return ossim::nan();
220 }
221 
223 {
224  if (m_minValues.size() <= band)
225  m_minValues.resize(band+1);
226  m_minValues[band] = value;
227 }
228 
230 {
231  if (m_maxValues.size() <= band)
232  m_maxValues.resize(band+1);
233  m_maxValues[band] = value;
234 }
235 
236 
237 
16 bit unsigned integer (15 bits used)
virtual ossim_uint32 getWidth() const
virtual void setDescription(const ossimString &description)
virtual double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
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 bool saveState(ossimKeywordlist &kwl, const char *prefix=NULL) const
Method to save the state of an object to a keyword list.
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
virtual void setImageRectangle(const ossimIrect &rect)
virtual const ossim_uint16 * getUshortBuf() const
virtual const ossim_uint8 * getUcharBuf() const
Represents serializable keyword/value map.
bool theEnableFlag
Definition: ossimSource.h:62
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
double nan()
Method to return ieee floating point double precision NAN.
Definition: ossimCommon.h:135
RTTI_DEF1(ossimLinearStretchRemapper, "ossimLinearRemapper", ossimImageSourceFilter)
virtual ossim_uint32 getTileHeight() const
Returns the default processing tile height.
void split(std::vector< ossimString > &result, const ossimString &separatorList, bool skipBlankFields=false) const
Splits this string into a vector of strings (fields) using the delimiter list specified.
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
16 bit unsigned integer (14 bits used)
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
16 bit unsigned integer (13 bits used)
virtual const ossim_sint16 * getSshortBuf() const
virtual void initialize()
Initialize the data buffer.
virtual ossim_uint32 getTileWidth() const
Returns the default processing tile width.
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 ossimImageDataFactory * instance()
virtual ossimDataObjectStatus validate() const
ossimImageSource * theInputConnection
unsigned int ossim_uint32
32 bit normalized floating point
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
void setMaxPixelValue(double value, ossim_uint32 band=0)
virtual const ossim_float32 * getFloatBuf() const
virtual ossimScalarType getScalarType() const
virtual void makeBlank()
Initializes data to null pixel values.
64 bit normalized floating point
16 bit unsigned integer (11 bits used)
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
bool empty() const
Definition: ossimString.h:411
ossimRefPtr< ossimImageData > m_tile
void allocate()
Called on first getTile, will initialize all data needed.
void setMinPixelValue(double value, ossim_uint32 band=0)
32 bit floating point
16 bit unsigned iteger
64 bit floating point
16 bit signed integer
virtual double getNullPixelValue(ossim_uint32 band=0) const
Each band has a null pixel associated with it.
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &origin, ossim_uint32 resLevel=0)
8 bit unsigned iteger
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)
16 bit unsigned integer (12 bits used)
virtual const ossim_float64 * getDoubleBuf() const