OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimFftFilter.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
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: ossimFftFilter.cpp 17206 2010-04-25 23:20:40Z dburken $
12 
15 #include <ossim/matrix/newmat.h>
16 #include <ossim/matrix/newmatap.h>
19 
21 
23  :ossimImageSourceFilter(owner),
24  theTile(0),
25  theDirectionType(FORWARD),
26  theScalarRemapper(new ossimScalarRemapper())
27 {
29 }
30 
32  :ossimImageSourceFilter(inputSource),
33  theTile(0),
34  theDirectionType(FORWARD),
35  theScalarRemapper(new ossimScalarRemapper())
36 {
38 }
39 
41  ossimImageSource* inputSource)
42  :ossimImageSourceFilter(owner, inputSource),
43  theTile(0),
44  theDirectionType(FORWARD),
45  theScalarRemapper(new ossimScalarRemapper())
46 {
48 }
49 
51 {
53  {
56  }
57 }
58 
60  ossim_uint32 resLevel)
61 {
62  if(!isSourceEnabled())
63  return ossimImageSourceFilter::getTile(rect, resLevel);
64 
65  ossimIrect tempRequest = rect;
66 
67  ossim_uint32 w = rect.width();
68  ossim_uint32 h = rect.height();
69 
70  if(w & 1)
71  ++w;
72  if(h&1)
73  ++h;
74 
75  tempRequest = ossimIrect(rect.ul().x, rect.ul().y,
76  rect.ul().x + (w-1), rect.ul().y + (h-1));
77 
78  ossimRefPtr<ossimImageData> inTile = theScalarRemapper->getTile(tempRequest, resLevel);
79  if(!inTile.valid())
80  return inTile;
81  if(!theTile.valid())
82  initialize();
83  if(!theTile.valid() || !inTile->getBuf())
84  return theTile;
85 
88 
89  if(rect != tempRequest)
90  {
91  tempTile = (ossimImageData*)theTile->dup();
92  tempTile->setImageRectangle(tempRequest);
93  }
94 
95  runFft(inTile, tempTile);
96 
97  if(tempTile != theTile)
98  {
99  theTile->loadTile(tempTile.get());
100  }
101 
102  theTile->validate();
103  return theTile;
104 }
105 
107 {
109 
111 
112  if(theTile.valid())
113  {
114  theTile->initialize();
115  }
117  {
119  }
120  else
121  {
123  }
125 }
126 
128 {
129  if(!isSourceEnabled())
130  {
132  }
133 
135  {
137  }
138 
140 }
141 
143 {
144  if(!isSourceEnabled())
145  {
147  }
149  {
150  return ossim::nan();
151  }
152 
153  // it will invert to a normalized float output
154  return 0.0;
155 }
156 
158 {
159  if(!isSourceEnabled())
160  {
162  }
163  return 0.0;
164 }
165 
167 {
168  if(!isSourceEnabled())
169  {
171  }
172  return 1.0;
173 }
174 
176 {
177  if(!isSourceEnabled())
178  {
180  }
182 
184  {
185  bands *=2;
186  }
187  else
188  {
189  bands /=2;
190  }
191 
192  return bands;
193 }
194 
196 {
198 }
199 
201 {
203 }
204 
206 {
208  {
209  return "Forward";
210  }
211 
212  return "Inverse";
213 }
214 
216 {
217  ossimString tempDirectionType = directionType;
218  tempDirectionType = tempDirectionType.downcase();
219 
220  if(tempDirectionType.contains("forward"))
221  {
223  }
224  else
225  {
227  }
228 }
229 
231 {
232  theDirectionType = directionType;
233  if(theTile.valid())
234  {
235  theTile = NULL;
236  }
237 }
238 
240 {
241  if(name == "FFT Direction")
242  {
243  std::vector<ossimString> filterNames;
244  filterNames.push_back("Forward");
245  filterNames.push_back("Inverse");
246  ossimStringProperty* stringProp = new ossimStringProperty("FFT Direction",
248  false,
249  filterNames);
250  stringProp->setCacheRefreshBit();
251 
252  return stringProp;
253  }
254 
256 }
257 
259 {
260  if(!property) return;
261 
262  if(property->getName() == "FFT Direction")
263  {
264  if(theTile.valid())
265  {
266  theTile = NULL;
267  }
268  setDirectionType(property->valueToString());
269  }
270  else
271  {
273  }
274 }
275 
276 void ossimFftFilter::getPropertyNames(std::vector<ossimString>& propertyNames)const
277 {
279  propertyNames.push_back("FFT Direction");
280 }
281 
282 
285 {
286 
287  NEWMAT::Matrix* realIn = new NEWMAT::Matrix(input->getHeight(),
288  input->getWidth());
289  NEWMAT::Matrix* imgIn = new NEWMAT::Matrix(input->getHeight(),
290  input->getWidth());
291  NEWMAT::Matrix* realOut = new NEWMAT::Matrix(input->getHeight(),
292  input->getWidth());
293  NEWMAT::Matrix* imgOut = new NEWMAT::Matrix(input->getHeight(),
294  input->getWidth());
295  ossim_uint32 bandIdx = 0;
296  ossim_uint32 w = input->getWidth();
297  ossim_uint32 h = input->getHeight();
298  ossim_uint32 x = 0;
299  ossim_uint32 y = 0;
301  {
302  ossim_uint32 bands = input->getNumberOfBands();
303  for(bandIdx = 0; bandIdx < bands; ++bandIdx)
304  {
305  ossim_float64* bandReal = 0;
306  ossim_float64* bandImg = 0;
307  fillMatrixForward((ossim_float64*)input->getBuf(bandIdx),
308  (ossim_float64)input->getNullPix(bandIdx),
309  *realIn,
310  *imgIn);
311  NEWMAT::FFT2(*realIn, *imgIn, *realOut, *imgOut);
312  bandReal = (ossim_float64*)output->getBuf(2*bandIdx);
313  bandImg = (ossim_float64*)output->getBuf(2*bandIdx + 1);
314  if(bandReal&&bandImg)
315  {
316  for(y = 0; y < h; ++y)
317  {
318  for(x = 0; x < w; ++x)
319  {
320  *bandReal = (ossim_float64)((*realOut)[y][x]);
321  *bandImg = (ossim_float64)((*imgOut)[y][x]);
322  ++bandReal;
323  ++bandImg;
324  }
325  }
326  }
327  }
328  }
329  else
330  {
331  ossim_float64* bandReal = 0;
332  ossim_uint32 bands = input->getNumberOfBands();
333  for(bandIdx = 0; bandIdx < bands; bandIdx+=2)
334  {
335  bandReal = (ossim_float64*)output->getBuf(bandIdx/2);
336  if(input->getBuf(bandIdx)&&
337  input->getBuf(bandIdx+1))
338  {
339  fillMatrixInverse((double*)input->getBuf(bandIdx),
340  (double*)input->getBuf(bandIdx+1),
341  *realIn,
342  *imgIn);
343  NEWMAT::FFT2I(*realIn, *imgIn, *realOut, *imgOut);
344  for(y = 0; y < h; ++y)
345  {
346  for(x = 0; x < w; ++x)
347  {
348  *bandReal = (ossim_float64)((*realOut)[y][x]);
349 // if(*bandReal > 1.0)
350 // {
351 // *bandReal = 1.0;
352 // }
353 // if(*bandReal < 0.0)
354 // {
355 // *bandReal = 0.0;
356 // }
357  ++bandReal;
358  }
359  }
360  }
361  }
362  }
363 
364  delete realIn;
365  delete imgIn;
366  delete realOut;
367  delete imgOut;
368 }
369 
370 template <class T>
372  T nullPix,
373  NEWMAT::Matrix& real,
374  NEWMAT::Matrix& img)const
375 {
376  ossim_uint32 w = real.Ncols();
377  ossim_uint32 h = real.Nrows();
378  ossim_uint32 yIdx = 0;
379  ossim_uint32 xIdx = 0;
380 
381  for(yIdx = 0; yIdx < h; ++yIdx)
382  {
383  for(xIdx = 0; xIdx < w; ++xIdx)
384  {
385  if((double)(*realPart) != nullPix)
386  {
387  real[yIdx][xIdx] = (double)(*realPart);
388  }
389  else
390  {
391  real[yIdx][xIdx] = 0.0;
392  }
393 
394  img[yIdx][xIdx] = 0.0;
395 
396  ++realPart;
397  }
398  }
399 }
400 
401 template <class T>
403  T *imgPart,
404  NEWMAT::Matrix& real,
405  NEWMAT::Matrix& img)const
406 {
407  ossim_uint32 w = real.Ncols();
408  ossim_uint32 h = real.Nrows();
409  ossim_uint32 yIdx = 0;
410  ossim_uint32 xIdx = 0;
411 
412  for(yIdx = 0; yIdx < h; ++yIdx)
413  {
414  for(xIdx = 0; xIdx < w; ++xIdx)
415  {
416  real[yIdx][xIdx] = (double)(*realPart);
417  img[yIdx][xIdx] = (double)(*imgPart);
418 
419  ++realPart;
420  ++imgPart;
421  }
422  }
423 }
424 
425 
427  const char* prefix)
428 {
429  const char* direction = kwl.find(prefix, "fft_direction");
430  if(direction)
431  {
432  setDirectionType(ossimString(direction));
433  }
434 
435  return ossimImageSourceFilter::loadState(kwl, prefix);
436 }
437 
439  const char* prefix)const
440 {
441  kwl.add(prefix,
442  "fft_direction",
444  true);
445 
446  return ossimImageSourceFilter::saveState(kwl, prefix);
447 }
virtual void valueToString(ossimString &valueResult) const =0
virtual ossim_uint32 getWidth() const
ossim_uint32 x
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
virtual double getNullPixelValue(ossim_uint32 band=0) const
virtual void setProperty(ossimRefPtr< ossimProperty > property)
virtual ossim_uint32 getNumberOfBands() const
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
virtual void setImageRectangle(const ossimIrect &rect)
virtual void disconnect(ossimConnectableObject *object=0)
Will disconnect the object passed in.
ossimFftFilter(ossimObject *owner=NULL)
Represents serializable keyword/value map.
virtual ~ossimFftFilter()
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
ossim_uint32 y
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
ossim_uint32 height() const
Definition: ossimIrect.h:487
bool contains(char aChar) const
Definition: ossimString.h:58
void fillMatrixInverse(T *realPart, T *imgPart, NEWMAT::Matrix &real, NEWMAT::Matrix &img) const
const ossimIpt & ul() const
Definition: ossimIrect.h:274
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
virtual ossim_uint32 getHeight() const
virtual void initialize()
Initialize the data buffer.
virtual ossimObject * dup() const
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &rect, ossim_uint32 resLevel=0)
ossimConnectableObject * getInput(ossim_uint32 index=0)
returns the object at the specified index.
virtual double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
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)
void FFT2(const Matrix &U, const Matrix &V, Matrix &X, Matrix &Y)
Definition: fft.cpp:440
static ossimImageDataFactory * instance()
virtual void setOutputScalarType(ossimScalarType scalarType)
Sets the output scalar type.
void setDirectionType(const ossimString &directionType)
virtual ossimDataObjectStatus validate() const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
virtual void setProperty(ossimRefPtr< ossimProperty > property)
unsigned int ossim_uint32
virtual const ossim_float64 * getNullPix() 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.
ossimRefPtr< ossimImageData > theTile
void FFT2I(const Matrix &U, const Matrix &V, Matrix &X, Matrix &Y)
Definition: fft.cpp:461
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tile_rect, ossim_uint32 resLevel=0)
virtual ossim_int32 connectMyInputTo(ossimConnectableObject *inputObject, bool makeOutputConnection=true, bool createEventFlag=true)
Will try to connect this objects input to the passed in object.
virtual double getMaxPixelValue(ossim_uint32 band=0) const
virtual void initialize()
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
ossim_uint32 width() const
Definition: ossimIrect.h:500
ossimRefPtr< ossimScalarRemapper > theScalarRemapper
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
ossimScalarType
RTTI_DEF1(ossimFftFilter, "ossimFftFilter", ossimImageSourceFilter)
64 bit normalized floating point
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.
ossim_int32 y
Definition: ossimIpt.h:142
virtual const void * getBuf() const
ossimFftFilterDirectionType theDirectionType
ossimString getDirectionTypeAsString() const
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
ossim_int32 x
Definition: ossimIpt.h:141
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 double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
void fillMatrixForward(T *realPart, T nullPix, NEWMAT::Matrix &real, NEWMAT::Matrix &img) const
virtual double getNullPixelValue(ossim_uint32 band=0) const
Each band has a null pixel associated with it.
void setCacheRefreshBit()
virtual void runFft(ossimRefPtr< ossimImageData > &input, ossimRefPtr< ossimImageData > &output)
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
const ossimString & getName() const
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)