PCL
FFTConvolution.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/FFTConvolution.h - Released 2019-11-07T10:59:34Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2019 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_FFTConvolution_h
53 #define __PCL_FFTConvolution_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/AutoPointer.h>
62 #include <pcl/KernelFilter.h>
63 #include <pcl/ParallelProcess.h>
64 
65 namespace pcl
66 {
67 
68 // ----------------------------------------------------------------------------
69 
87 #define PCL_FFT_CONVOLUTION_IS_FASTER_THAN_SEPARABLE_FILTER_SIZE 162
88 
103 #define PCL_FFT_CONVOLUTION_IS_FASTER_THAN_NONSEPARABLE_FILTER_SIZE 12
104 
117 class PCL_CLASS FFTConvolution : public ImageTransformation, public ParallelProcess
118 {
119 public:
120 
128  FFTConvolution() = default;
129 
139  FFTConvolution( const KernelFilter& filter ) :
140  m_filter( filter.Clone() )
141  {
142  PCL_CHECK( bool( m_filter ) )
143  }
144 
159  m_image( f )
160  {
161  PCL_CHECK( bool( m_image ) )
162  }
163 
168  ImageTransformation( x ),
169  ParallelProcess( x ),
170  m_image( x.m_image ),
171  m_outputRealCmp( x.m_outputRealCmp )
172  {
173  if ( x.m_filter )
174  m_filter = x.m_filter->Clone();
175  }
176 
180  FFTConvolution( FFTConvolution&& ) = default;
181 
185  virtual ~FFTConvolution()
186  {
187  }
188 
192  FFTConvolution& operator =( const FFTConvolution& x )
193  {
194  if ( &x != this )
195  {
196  (void)ImageTransformation::operator =( x );
197  (void)ParallelProcess::operator =( x );
198  DestroyFilter();
199  if ( x.m_filter )
200  m_filter = x.m_filter->Clone();
201  else
202  m_image = x.m_image;
203  m_outputRealCmp = x.m_outputRealCmp;
204  }
205  return *this;
206  }
207 
211  FFTConvolution& operator =( FFTConvolution&& ) = default;
212 
217  bool UsingFilter() const
218  {
219  return m_filter;
220  }
221 
229  const KernelFilter& Filter() const
230  {
231  PCL_PRECONDITION( bool( m_filter ) )
232  return *m_filter;
233  }
234 
245  const ImageVariant& FilterImage() const
246  {
247  return m_image;
248  }
249 
258  void SetFilter( const KernelFilter& filter )
259  {
260  DestroyFilter();
261  m_filter = filter.Clone();
262  PCL_CHECK( bool( m_filter ) )
263  }
264 
275  void SetFilter( const ImageVariant& filter )
276  {
277  DestroyFilter();
278  m_image = filter;
279  PCL_CHECK( bool( m_image ) )
280  }
281 
304  {
305  return m_h;
306  }
307 
319  {
320  return m_outputRealCmp;
321  }
322 
334  {
335  return !m_outputRealCmp;
336  }
337 
344  void EnableRealComponentOutput( bool enable = true )
345  {
346  m_outputRealCmp = enable;
347  }
348 
355  void EnableMagnitudeOutput( bool enable = true )
356  {
357  m_outputRealCmp = !enable;
358  }
359 
360 protected:
361 
362  /*
363  * The response function for convolution is defined as either a KernelFilter
364  * instance or as an image. In the latter case, the image transported by the
365  * ImageVariant must remain valid while this object is actively used.
366  */
367  AutoPointer<KernelFilter> m_filter;
368  ImageVariant m_image;
369  bool m_outputRealCmp = false;
370 
371  /*
372  * Internal DFT of the response function. Initially zero. This matrix is
373  * generated/reused/regenerated as this object is applied to convolve
374  * different target images. It is destroyed when a new filter is specified.
375  */
376  mutable AutoPointer<ComplexImage> m_h;
377 
378  /*
379  * In-place Fourier-based 2-D convolution algorithm.
380  */
381  void Apply( pcl::Image& ) const override;
382  void Apply( pcl::DImage& ) const override;
383  void Apply( pcl::UInt8Image& ) const override;
384  void Apply( pcl::UInt16Image& ) const override;
385  void Apply( pcl::UInt32Image& ) const override;
386  void Apply( pcl::ComplexImage& ) const override;
387  void Apply( pcl::DComplexImage& ) const override;
388 
389  friend class PCL_FFTConvolutionEngine;
390 
391 private:
392 
393  void DestroyFilter()
394  {
395  m_filter.Destroy();
396  m_image.Free();
397  m_h.Destroy();
398  }
399 
400  void Validate() const;
401  void ValidateFilter() const;
402 };
403 
404 // ----------------------------------------------------------------------------
405 
406 } // pcl
407 
408 #endif // __PCL_FFTConvolution_h
409 
410 // ----------------------------------------------------------------------------
411 // EOF pcl/FFTConvolution.h - Released 2019-11-07T10:59:34Z
void SetFilter(const KernelFilter &filter)
bool IsMagnitudeOutputEnabled() const
void EnableRealComponentOutput(bool enable=true)
const KernelFilter & Filter() const
FFTConvolution(const ImageVariant &f)
bool UsingFilter() const
const ImageVariant & FilterImage() const
PCL root namespace.
Definition: AbstractImage.h:76
virtual KernelFilter * Clone() const
Definition: KernelFilter.h:188
A smart pointer with sole object ownership and optional automatic object destruction.
Definition: AutoPointer.h:241
16-bit unsigned integer image.
void SetFilter(const ImageVariant &filter)
void EnableMagnitudeOutput(bool enable=true)
Acts like a union for all types of images in PCL, with optional class-wide ownership of transported i...
Definition: ImageVariant.h:317
FFTConvolution(const KernelFilter &filter)
32-bit floating point complex image.
void Apply(FI i, FI j, F f)
Definition: Utility.h:249
64-bit floating point real image.
32-bit unsigned integer image.
8-bit unsigned integer image.
ImageVariant & Free()
Root base class of all PCL image transformations.
Kernel filter in two dimensions.
Definition: KernelFilter.h:89
const ComplexImage * ResponseFunctionDFT() const
bool IsRealComponentOutputEnabled() const
64-bit floating point complex image.
Fourier-based two-dimensional convolution.
32-bit floating point real image.
FFTConvolution(const FFTConvolution &x)
A process using multiple concurrent execution threads.