OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimErosionFilter.cpp
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // OSSIM Open Source Geospatial Data Processing Library
4 // See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 
13 #include <vector>
14 #include <numeric>
15 
16 using namespace std;
17 
19 
20 // Keywords used throughout.
21 static const ossimString WINDOW_SIZE_KW = "window_size";
22 
24  :ossimImageSourceFilter(owner),
25  theTile(0),
26  theTempTile(0),
27  theWindowSize(3)
28 {
29  setDescription(ossimString("Dilation Filter"));
30 }
31 
33 {
34 }
35 
37 {
39  theTile = NULL;
40 }
41 
43  ossim_uint32 resLevel)
44 {
45  if(!isSourceEnabled())
46  return ossimImageSourceFilter::getTile(rect, resLevel);
47 
49  ossim_uint32 halfSize = getWindowSize()>>1;
50  ossimIrect requestRect(rect.ul().x - halfSize,
51  rect.ul().y - halfSize,
52  rect.lr().x + halfSize,
53  rect.lr().y + halfSize);
54 
55  inputData = ossimImageSourceFilter::getTile(requestRect, resLevel);
56 
57  if(!inputData.valid() || !inputData->getBuf())
58  return inputData;
59 
60  vector<ossimIpt> viv;
63 
64  if(!theTile.valid())
65  {
66  theTile = (ossimImageData*)inputData->dup();
68  }
69  else
70  {
71  theTile->setImageRectangleAndBands(rect, inputData->getNumberOfBands());
72  }
73 
74  switch(inputData->getScalarType())
75  {
76  case OSSIM_UINT8:
77  doErosion(ossim_uint8(0), inputData);
78  break;
79  case OSSIM_USHORT11:
80  case OSSIM_USHORT12:
81  case OSSIM_USHORT13:
82  case OSSIM_USHORT14:
83  case OSSIM_USHORT15:
84  case OSSIM_UINT16:
85  doErosion(ossim_uint16(0), inputData);
86  break;
87  case OSSIM_SINT16:
88  doErosion(ossim_sint16(0), inputData);
89  break;
90  case OSSIM_UINT32:
91  doErosion(ossim_uint32(0), inputData);
92  break;
93  case OSSIM_FLOAT32:
95  doErosion(ossim_float32(0), inputData);
96  break;
97  case OSSIM_FLOAT64:
99  doErosion(ossim_float64(0), inputData);
100  break;
101  default:
102  {
104  << "ossimErosionFilter::applyFilter WARNING:\n"
105  << "Unhandled scalar type!" << endl;
106  }
107  }
108  return theTile;
109 }
110 
111 template <class T>
112 void ossimErosionFilter::doErosion(T /* scalarType */, ossimRefPtr<ossimImageData>& inputData)
113 {
115 
116  if ((status == OSSIM_FULL) || (status == OSSIM_EMPTY))
117  {
118  // Nothing to do just copy the tile.
119  theTile->loadTile(inputData.get());
120  return;
121  }
122 
123  ossim_int32 halfWindow = (ossim_int32)(theWindowSize >> 1);
124  ossim_int32 x, y, kernelX, kernelY;
125  ossim_int32 iw = (ossim_int32)inputData->getWidth();
126  ossim_int32 ih = (ossim_int32)inputData->getHeight();
129  ossim_uint32 numBands = ossim::min(theTile->getNumberOfBands(), inputData->getNumberOfBands());
130 
131  // It may be that the input rect is the same size as the output (i.e., the tile bounds aren't
132  // expanded in the input's request to permit full kernels for output edge pixels:
133  ossim_uint32 i_offset = 0;
134  ossim_int32 delta = (ossim_int32)((iw - ow) >> 1);
135  ossim_int32 xi, yi;
136  if (delta > 0)
137  i_offset = (ossim_uint32) halfWindow*(iw + 1);
138 
139  ossimIpt tile_ul (theTile->getImageRectangle().ul());
140  vector<double> values;
141  for(ossim_uint32 bandIdx = 0; bandIdx < numBands; ++bandIdx)
142  {
143  T* inputBuf = (T*)inputData->getBuf(bandIdx);
144  T* outputBuf = (T*)theTile->getBuf(bandIdx);
145  if (!inputBuf || !outputBuf)
146  {
147  return; // Shouldn't happen...
148  }
149 
150  const T NP = (T)inputData->getNullPix(bandIdx);
151 
152  for(y = 0; y < oh; ++y)
153  {
154  for(x = 0; x < ow; ++x)
155  {
156  bool null_found = true;
157  const T CP = *(inputBuf+i_offset);
158  ossimDpt ipt (tile_ul.x+x, tile_ul.y+y);
159 
160  if ((CP != NP) && theValidImagePoly.isPointWithin(ipt))
161  {
162  // Valid pixel found, need to check neighbors for null:
163  null_found = false;
164  values.clear();
165  for(kernelY = -halfWindow; (kernelY<=halfWindow) && !null_found; ++kernelY)
166  {
167  yi = y + kernelY + delta;
168  if ((yi < 0) || (y > ih))
169  continue;
170 
171  for(kernelX = -halfWindow; (kernelX<=halfWindow) && !null_found; ++kernelX)
172  {
173  xi = x + kernelX + delta;
174  if ((xi < 0) || (x > iw))
175  continue;
176 
177  T neighborPixel = *(inputBuf+kernelX+delta + (kernelY+delta)*iw);
178  if(neighborPixel == NP)
179  {
180  // Found a NULL pixel:
181  (*outputBuf) = NP;
182  null_found = true;
183  }
184  }
185  }
186  }
187 
188  if (null_found)
189  (*outputBuf) = NP;
190  else
191  (*outputBuf) = CP;
192 
193  // Move over...
194  ++inputBuf;
195  ++outputBuf;
196 
197  } // End of loop in x direction.
198 
199  // Move down...
200  inputBuf += iw - ow;
201 
202  } // End of loop in y direction.
203 
204  } // End of band loop.
205 
206  theTile->validate();
207 
208 }
209 
211 {
212  if (!property.valid())
213  return;
214 
215  ossimString name = property->getName();
216 
217  if (name == WINDOW_SIZE_KW)
218  {
219  theWindowSize = property->valueToString().toUInt32();
220  }
221  else
222  {
224  }
225 }
226 
228 {
230  if (name == WINDOW_SIZE_KW)
231  {
232  prop = new ossimNumericProperty(WINDOW_SIZE_KW, ossimString::toString(theWindowSize), 3, 25);
233  prop->setCacheRefreshBit();
234  return prop;
235  }
237 }
238 
239 void ossimErosionFilter::getPropertyNames(vector<ossimString>& propertyNames) const
240 {
241  propertyNames.push_back(WINDOW_SIZE_KW);
243 }
244 
245 bool ossimErosionFilter::saveState(ossimKeywordlist& kwl, const char* prefix)const
246 {
247  kwl.add(prefix, WINDOW_SIZE_KW.c_str(), theWindowSize, true);
248  return ossimImageSourceFilter::saveState(kwl, prefix);
249 }
250 
251 bool ossimErosionFilter::loadState(const ossimKeywordlist& kwl, const char* prefix)
252 {
253  const char* lookup = kwl.find(prefix, WINDOW_SIZE_KW.c_str());
254  if (lookup)
255  theWindowSize = ossimString(lookup).toUInt32();
256 
257  return ossimImageSourceFilter::loadState(kwl, prefix);
258 }
16 bit unsigned integer (15 bits used)
virtual ossim_uint32 getWidth() const
ossim_uint32 x
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
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 setDescription(const ossimString &description)
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual ossim_uint32 getNumberOfBands() const
64 bit floating point
virtual void setImageRectangle(const ossimIrect &rect)
16 bit unsigned integer
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
Represents serializable keyword/value map.
ossim_uint32 y
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
void doErosion(T scalarType, ossimRefPtr< ossimImageData > &inputData)
float ossim_float32
static ossimString toString(bool aValue)
Numeric to string methods.
16 bit signed integer
const ossimIpt & ul() const
Definition: ossimIrect.h:274
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
16 bit unsigned integer (14 bits used)
ossim_uint32 toUInt32() const
16 bit unsigned integer (13 bits used)
32 bit floating point
unsigned short ossim_uint16
ossimRefPtr< ossimImageData > theTile
32 bit unsigned integer
ossimErosionFilter(ossimObject *owner=NULL)
virtual ossimObject * dup() const
virtual void initialize()
ossimPolygon theValidImagePoly
double ossim_float64
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual void loadTile(const void *src, const ossimIrect &src_rect, ossimInterleaveType il_type)
virtual ossimDataObjectStatus validate() const
signed short ossim_sint16
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
virtual void setImageRectangleAndBands(const ossimIrect &rect, ossim_uint32 numberOfBands)
RTTI_DEF1(ossimErosionFilter, "ossimErosionFilter", ossimImageSourceFilter)
virtual void getValidImageVertices(std::vector< ossimIpt > &validVertices, ossimVertexOrdering ordering=OSSIM_CLOCKWISE_ORDER, ossim_uint32 resLevel=0) const
ordering specifies how the vertices should be arranged.
unsigned int ossim_uint32
virtual const ossim_float64 * getNullPix() const
32 bit normalized floating point
ossim_uint32 getWindowSize() const
virtual ossimIrect getImageRectangle() 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.
const ossimIpt & lr() const
Definition: ossimIrect.h:276
T min(T a, T b)
Definition: ossimCommon.h:203
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
return status
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.
ossim_int32 y
Definition: ossimIpt.h:142
bool isPointWithin(const ossimDpt &point) const
virtual const void * getBuf() const
virtual void setProperty(ossimRefPtr< ossimProperty > property)
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
ossim_int32 x
Definition: ossimIpt.h:141
8 bit unsigned integer
ossimDataObjectStatus
Definitions for data object status.
unsigned char ossim_uint8
ossim_uint32 theWindowSize
Used for recursion when recursive fill enabled
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
void setCacheRefreshBit()
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &rect, ossim_uint32 resLevel=0)
int ossim_int32
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)
16 bit unsigned integer (12 bits used)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.