OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimDespeckleFilter.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 //*******************************************************************
10 // $Id: ossimDespeckleFilter.cpp 2644 2011-05-26 15:20:11Z oscar.kramer $
11 
18 
19 static const ossimKeyword FILTER_RADIUS_KW = ossimKeyword("filter_radius", "");
20 
21 RTTI_DEF1(ossimDespeckleFilter, "ossimDespeckleFilter", ossimImageSourceFilter);
22 
25  theTile(NULL),
26  theFilterRadius(1)
27 {
28 }
29 
31  : ossimImageSourceFilter(inputSource),
32  theTile(NULL),
33  theFilterRadius(filter_radius)
34 {
35  initialize();
36 }
37 
39 {
40  theTile = 0;
41 }
42 
45 {
46  if (!theInputConnection)
47  return NULL;
48 
49  if (!isSourceEnabled())
50  return theInputConnection->getTile(tileRect, resLevel);
51 
52  if (!theTile.valid())
53  {
54  allocate();
55  if (!theTile.valid()) // Throw exception???
56  return theInputConnection->getTile(tileRect, resLevel);
57  }
58 
59  // Fetch input tile with kernel margins:
60  ossimIrect requestRect(tileRect.ul().x - theFilterRadius,
61  tileRect.ul().y - theFilterRadius,
62  tileRect.lr().x + theFilterRadius,
63  tileRect.lr().y + theFilterRadius);
64  ossimRefPtr<ossimImageData> inputTile = theInputConnection->getTile(requestRect, resLevel);
65  if (!inputTile.valid())
66  return NULL;
67 
68  ossim_uint32 w = tileRect.width();
69  ossim_uint32 h = tileRect.height();
72  theTile->setWidth(w);
73  theTile->setHeight(h);
74  if((w*h)!=(tw*th))
76 
77  theTile->makeBlank();
78  theTile->setOrigin(tileRect.ul());
79 
80  ossimDataObjectStatus status = inputTile->getDataObjectStatus();
81  if ((status == OSSIM_NULL) || (status == OSSIM_EMPTY))
82  return inputTile;
83 
84  switch(inputTile->getScalarType())
85  {
86  case OSSIM_UCHAR:
87  despeckle(static_cast<ossim_uint8>(0), inputTile);
88  break;
89 
90  case OSSIM_USHORT16:
91  case OSSIM_USHORT11:
92  case OSSIM_USHORT12:
93  case OSSIM_USHORT13:
94  case OSSIM_USHORT14:
95  case OSSIM_USHORT15:
96  despeckle(static_cast<ossim_uint16>(0), inputTile);
97  break;
98 
99  case OSSIM_SSHORT16:
100  despeckle(static_cast<ossim_sint16>(0), inputTile);
101  break;
102 
103  case OSSIM_FLOAT:
105  despeckle(static_cast<float>(0), inputTile);
106  break;
107 
108  case OSSIM_DOUBLE:
110  despeckle(static_cast<double>(0), inputTile);
111  break;
112 
113  default:
114  theTile->loadTile(inputTile.get());
115  }
116 
117  theTile->validate();
118  return theTile;
119 }
120 
121 template <class T>
122 void ossimDespeckleFilter::despeckle(T /* dummyVariable */, ossimRefPtr<ossimImageData> inputTile)
123 {
124  ossimIpt inUL (inputTile->getImageRectangle().ul());
125  ossimIpt inLR (inputTile->getImageRectangle().lr());
126  ossimIpt outUL (theTile->getImageRectangle().ul());
127  ossimIpt outLR (theTile->getImageRectangle().lr());
128  long inWidth = inputTile->getWidth();
129  long outWidth = theTile->getWidth();
130  long num_bands = theTile->getNumberOfBands();
131 
132  // Loop over all bands first:
133  for(long b = 0; b < num_bands; ++b)
134  {
135  const T* inbuf = (const T*) inputTile->getBuf(b);
136  T* outBuf = (T*) theTile->getBuf(b);
137  T null_pixel = (T) inputTile->getNullPix(b);
138 
139  for (long y=outUL.y; y<=outLR.y; y++)
140  {
141  for (long x=outUL.x; x<=outLR.x; x++)
142  {
143  bool found_valid = false;
144  long idx = (y - inUL.y)*inWidth + x - inUL.x; // index to input buffer
145  long odx = (y-outUL.y)*outWidth + x - outUL.x;// index to output buffer
146 
147  T pixel = inbuf[idx];
148  if (pixel != null_pixel)
149  {
150  // Inside loop over input "kernel" pixels centered at x, y:
151  for (long iy=-theFilterRadius; (iy<=theFilterRadius) && !found_valid; iy++)
152  {
153  // Compute row number in image space and skip this row if outside input tile:
154  long row = y + iy;
155  if ((row < inUL.y) || (row > inLR.y))
156  continue;
157 
158  for (long ix=-theFilterRadius; (ix<=theFilterRadius) && !found_valid; ix++)
159  {
160  // Compute column number in image space and skip this col if outside input tile:
161  long col = x + ix;
162  if ((col < inUL.x) || (col > inLR.x))
163  continue;
164 
165  // Compute offset into input buffer for the neighbor pixel. If we aren't at the
166  // kernel center, consider if valid neighbor was found:
167  idx = (row - inUL.y)*inWidth + col - inUL.x;
168  if ( ((iy != 0) || (ix != 0)) && (inbuf[idx] != null_pixel) )
169  found_valid = true;
170  }
171  }
172  }
173 
174  // Finished scanning neighborhood. Save output to tile buffer:
175  if (found_valid)
176  outBuf[odx] = pixel;
177  else
178  outBuf[odx] = null_pixel;
179  }
180  }
181  }
182 }
183 
185 {
187  theTile = NULL;
188 }
189 
191 {
193  {
195  theTile->initialize();
196  }
197 }
198 
199 bool ossimDespeckleFilter::saveState(ossimKeywordlist& kwl, const char* prefix) const
200 {
201  kwl.add(prefix, FILTER_RADIUS_KW, theFilterRadius, true);
202  return ossimImageSourceFilter::saveState(kwl, prefix);
203 }
204 
205 
206 bool ossimDespeckleFilter::loadState(const ossimKeywordlist& kwl, const char* prefix)
207 {
208  const char* lookup = kwl.find(prefix, FILTER_RADIUS_KW);
209  if (lookup)
210  theFilterRadius = ossimString(lookup).toInt32();
211 
212  return ossimImageSourceFilter::loadState(kwl, prefix);
213 }
214 
216 {
217  ossimString name = property->getName();
218  if(name == "Filter Radius")
219  theFilterRadius = property->valueToString().toInt32();
220  else
222 }
223 
225 {
226  if(name == "Filter Radius")
227  {
229  prop->setCacheRefreshBit();
230  return prop;
231  }
233 }
234 
235 void ossimDespeckleFilter::getPropertyNames(std::vector<ossimString>& propertyNames)const
236 {
238  propertyNames.push_back("Filter Radius");
239 }
240 
16 bit unsigned integer (15 bits used)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=NULL) const
Method to save the state of an object to a keyword list.
virtual ossim_uint32 getWidth() const
ossim_uint32 x
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual ossim_uint32 getNumberOfBands() const
RTTI_DEF1(ossimDespeckleFilter, "ossimDespeckleFilter", ossimImageSourceFilter)
Represents serializable keyword/value map.
ossim_uint32 y
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
ossim_uint32 height() const
Definition: ossimIrect.h:487
static ossimString toString(bool aValue)
Numeric to string methods.
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
const ossimIpt & ul() const
Definition: ossimIrect.h:274
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 ossim_uint32 getHeight() const
16 bit unsigned integer (14 bits used)
16 bit unsigned integer (13 bits used)
virtual void initialize()
Initialize the data buffer.
ossim_int32 toInt32() const
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 void setHeight(ossim_uint32 height)
static ossimImageDataFactory * instance()
virtual ossimDataObjectStatus validate() const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
ossimImageSource * theInputConnection
unsigned int ossim_uint32
virtual const ossim_float64 * getNullPix() const
32 bit normalized floating point
ossimRefPtr< ossimImageData > getTile(const ossimIrect &tileRect, ossim_uint32 resLevel=0)
virtual void setWidth(ossim_uint32 width)
virtual ossimIrect getImageRectangle() const
ossimRefPtr< ossimImageData > theTile
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
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
ossim_uint32 width() const
Definition: ossimIrect.h:500
virtual void setOrigin(const ossimIpt &origin)
return status
virtual void makeBlank()
Initializes data to null pixel values.
64 bit normalized floating point
16 bit unsigned integer (11 bits used)
if(yy_init)
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
virtual const void * getBuf() const
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
void despeckle(T dummyVariable, ossimRefPtr< ossimImageData > inputTile)
ossim_int32 x
Definition: ossimIpt.h:141
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
ossimDataObjectStatus
Definitions for data object status.
32 bit floating point
16 bit unsigned iteger
64 bit floating point
16 bit signed integer
void setCacheRefreshBit()
8 bit unsigned iteger
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)
16 bit unsigned integer (12 bits used)