PCL
SeparableConvolution.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/SeparableConvolution.h - Released 2024-12-28T16:53:48Z
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-2024 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 (https://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_SeparableConvolution_h
53 #define __PCL_SeparableConvolution_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/AutoPointer.h>
62 #include <pcl/ParallelProcess.h>
63 #include <pcl/SeparableFilter.h>
64 
65 #define __PCL_SEPARABLE_CONVOLUTION_TINY_WEIGHT 1.0e-20
66 
67 namespace pcl
68 {
69 
70 // ----------------------------------------------------------------------------
71 
83  public ParallelProcess
84 {
85 public:
86 
91 
96 
103  SeparableConvolution() = default;
104 
115  {
116  SetFilter( filter );
117  }
118 
124  , ParallelProcess( x )
125  , m_weight( x.m_weight )
126  , m_highPass( x.m_highPass )
127  , m_rawHighPass( x.m_rawHighPass )
128  , m_rescaleHighPass( x.m_rescaleHighPass )
129  , m_convolveRows( x.m_convolveRows )
130  , m_convolveCols( x.m_convolveCols )
131  {
132  if ( !x.m_filter.IsNull() )
133  m_filter = x.m_filter->Clone();
134  }
135 
140 
145  {
146  }
147 
152  {
153  if ( &x != this )
154  {
155  (void)InterlacedTransformation::operator =( x );
156  (void)ParallelProcess::operator =( x );
157  if ( x.m_filter.IsNull() )
158  m_filter.Destroy();
159  else
160  m_filter = x.m_filter->Clone();
161  m_weight = x.m_weight;
162  m_highPass = x.m_highPass;
163  m_rawHighPass = x.m_rawHighPass;
164  m_rescaleHighPass = x.m_rescaleHighPass;
165  m_convolveRows = x.m_convolveRows;
166  m_convolveCols = x.m_convolveCols;
167  }
168  return *this;
169  }
170 
174  SeparableConvolution& operator =( SeparableConvolution&& ) = default;
175 
183  const SeparableFilter& Filter() const
184  {
185  PCL_PRECONDITION( !m_filter.IsNull() )
186  return *m_filter;
187  }
188 
198  coefficient_vector Filter( int phase ) const
199  {
200  PCL_PRECONDITION( !m_filter.IsNull() )
201  return m_filter->Filter( phase );
202  }
203 
210  template <typename T>
211  GenericVector<T> FilterAs( int phase, T* ) const
212  {
213  PCL_PRECONDITION( !m_filter.IsNull() )
214  return m_filter->FilterAs( phase, (T*)0 );
215  }
216 
220  void SetFilter( const SeparableFilter& filter )
221  {
222  m_filter = filter.Clone();
223  CacheFilterProperties();
224  }
225 
235  double FilterWeight() const
236  {
237  return m_weight;
238  }
239 
251  bool IsHighPassFilter() const
252  {
253  return m_highPass;
254  }
255 
275  {
276  return m_rescaleHighPass;
277  }
278 
284  void EnableHighPassRescaling( bool enable = true )
285  {
286  m_rescaleHighPass = enable;
287  }
288 
294  void DisableHighPassRescaling( bool disable = true )
295  {
296  EnableHighPassRescaling( !disable );
297  }
298 
309  bool IsRawHighPassEnabled() const
310  {
311  return m_rawHighPass;
312  }
313 
318  void EnableRawHighPass( bool enable = true )
319  {
320  m_rawHighPass = enable;
321  }
322 
327  void DisableRawHighPass( bool disable = true )
328  {
329  EnableRawHighPass( !disable );
330  }
331 
336  {
337  return m_convolveRows;
338  }
339 
343  void EnableRowConvolution( bool enable = true )
344  {
345  m_convolveRows = enable;
346  }
347 
354  void DisableRowConvolution( bool disable = true )
355  {
356  EnableRowConvolution( !disable );
357  }
358 
363  {
364  return m_convolveCols;
365  }
366 
370  void EnableColumnConvolution( bool enable = true )
371  {
372  m_convolveCols = enable;
373  }
374 
381  void DisableColumnConvolution( bool disable = true )
382  {
383  EnableColumnConvolution( !disable );
384  }
385 
392  {
393  PCL_PRECONDITION( !m_filter.IsNull() )
394  return m_filter->Size() + (m_filter->Size() - 1)*(InterlacingDistance() - 1);
395  }
396 
409  static constexpr int FasterThanNonseparableFilterSize( int numThreads )
410  {
411  if ( numThreads >= 32 )
412  return 29;
413  if ( numThreads >= 28 )
414  return 25;
415  if ( numThreads >= 24 )
416  return 21;
417  if ( numThreads >= 16 )
418  return 19;
419  if ( numThreads >= 8 )
420  return 13;
421  if ( numThreads >= 4 )
422  return 11;
423  if ( numThreads >= 2 )
424  return 9;
425  return 7;
426  }
427 
428 protected:
429 
430  /*
431  * The response function for convolution is a separable filter.
432  */
434 
435  /*
436  * Cached filter properties.
437  */
438  double m_weight = 0; // filter weight for low-pass normalization
439  bool m_highPass = false; // true iff this is a high-pass filter
440 
441  /*
442  * User-selectable options.
443  */
444  bool m_rawHighPass = false; // neither truncate nor normalize out-of-range values
445  bool m_rescaleHighPass = false; // truncate out-of-range values instead of normalize
446  bool m_convolveRows = true; // perform one-dimensional convolution of pixel rows
447  bool m_convolveCols = true; // perform one-dimensional convolution of pixel columns
448 
449  /*
450  * In-place 2-D separable convolution algorithm.
451  */
452  void Apply( pcl::Image& ) const override;
453  void Apply( pcl::DImage& ) const override;
454  void Apply( pcl::UInt8Image& ) const override;
455  void Apply( pcl::UInt16Image& ) const override;
456  void Apply( pcl::UInt32Image& ) const override;
457 
458 private:
459 
460  void CacheFilterProperties()
461  {
462  PCL_PRECONDITION( !m_filter.IsNull() )
463  PCL_PRECONDITION( !m_filter->IsEmpty() )
464  ValidateFilter();
465  m_highPass = m_filter->IsHighPassFilter();
466  m_weight = m_filter->Weight();
467  if ( pcl::Abs( m_weight ) < __PCL_SEPARABLE_CONVOLUTION_TINY_WEIGHT )
468  m_weight = 1;
469  }
470 
471  void ValidateFilter() const;
472 };
473 
474 // ----------------------------------------------------------------------------
475 
476 } // pcl
477 
478 #endif // __PCL_SeparableConvolution_h
479 
480 // ----------------------------------------------------------------------------
481 // EOF pcl/SeparableConvolution.h - Released 2024-12-28T16:53:48Z
A smart pointer with exclusive object ownership and optional automatic object destruction.
Definition: AutoPointer.h:246
bool IsNull() const
Definition: AutoPointer.h:458
Implements a generic, two-dimensional, shared or local image.
Definition: Image.h:278
Interlaced image transformation in the spatial domain.
A process using multiple concurrent execution threads.
Discrete two-dimensional separable convolution in the spatial domain.
void Apply(pcl::Image &) const override
SeparableConvolution(SeparableConvolution &&)=default
void DisableHighPassRescaling(bool disable=true)
const SeparableFilter & Filter() const
SeparableConvolution(const SeparableConvolution &x)
void Apply(pcl::UInt8Image &) const override
void Apply(pcl::UInt32Image &) const override
void EnableHighPassRescaling(bool enable=true)
void DisableColumnConvolution(bool disable=true)
void DisableRawHighPass(bool disable=true)
void EnableRowConvolution(bool enable=true)
void EnableColumnConvolution(bool enable=true)
coefficient_vector Filter(int phase) const
void DisableRowConvolution(bool disable=true)
GenericVector< T > FilterAs(int phase, T *) const
SeparableConvolution(const SeparableFilter &filter)
SeparableFilter::coefficient coefficient
void EnableRawHighPass(bool enable=true)
void Apply(pcl::UInt16Image &) const override
void SetFilter(const SeparableFilter &filter)
void Apply(pcl::DImage &) const override
Separable filter in two dimensions.
virtual SeparableFilter * Clone() const
GenericVector< coefficient > coefficient_vector
T Abs(const Complex< T > &c) noexcept
Definition: Complex.h:443
static constexpr int FasterThanNonseparableFilterSize(int numThreads)
PCL root namespace.
Definition: AbstractImage.h:77