PCL
SeparableFilter.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/SeparableFilter.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_SeparableFilter_h
53 #define __PCL_SeparableFilter_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Matrix.h>
61 #include <pcl/Vector.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
89 class PCL_CLASS SeparableFilter
90 {
91 public:
92 
96  using coefficient = float;
97 
102 
107 
111  SeparableFilter( const String& name = String() )
112  : m_filterName( name )
113  {
114  }
115 
124  SeparableFilter( int n, const String& name = String() )
125  : m_rowFilter( PCL_VALID_KERNEL_SIZE( n ) )
126  , m_colFilter( PCL_VALID_KERNEL_SIZE( n ) )
127  , m_filterName( name )
128  {
129  PCL_PRECONDITION( n == 0 || n >= 3 )
130  PCL_PRECONDITION( n == 0 || (n & 1) )
131  }
132 
140  template <typename T>
141  SeparableFilter( int n, const T& x, const String& name = String() )
142  : m_rowFilter( x, PCL_VALID_KERNEL_SIZE( n ) )
143  , m_colFilter( x, PCL_VALID_KERNEL_SIZE( n ) )
144  , m_filterName( name )
145  {
146  PCL_PRECONDITION( n == 0 || n >= 3 )
147  PCL_PRECONDITION( n == 0 || (n & 1) )
148  }
149 
155  SeparableFilter( const coefficient_vector& h, const coefficient_vector& v, const String& name = String() )
156  : m_rowFilter( h )
157  , m_colFilter( v )
158  , m_filterName( name )
159  {
160  PCL_PRECONDITION( v.Length() == h.Length() )
161  PCL_PRECONDITION( v.IsEmpty() || v.Length() >= 3 )
162  PCL_PRECONDITION( v.IsEmpty() || (v.Length() & 1) )
163  }
164 
171  template <typename T>
172  SeparableFilter( const T* h, const T* v, int n, const String& name = String() )
173  : m_rowFilter( h, PCL_VALID_KERNEL_SIZE( n ) )
174  , m_colFilter( v, PCL_VALID_KERNEL_SIZE( n ) )
175  , m_filterName( name )
176  {
177  PCL_PRECONDITION( n == 0 || n >= 3 )
178  PCL_PRECONDITION( n == 0 || (n & 1) )
179  }
180 
184  SeparableFilter( const SeparableFilter& ) = default;
185 
190 
195  {
196  }
197 
205  virtual SeparableFilter* Clone() const
206  {
207  return new SeparableFilter( *this );
208  }
209 
213  SeparableFilter& operator =( const SeparableFilter& ) = default;
214 
218  SeparableFilter& operator =( SeparableFilter&& ) = default;
219 
224  SeparableFilter& operator =( const coefficient& x )
225  {
226  m_rowFilter = x;
227  m_colFilter = x;
228  return *this;
229  }
230 
236  bool operator ==( const SeparableFilter& f ) const
237  {
238  return Name() == f.Name() && SameCoefficients( f );
239  }
240 
244  String Name() const
245  {
246  return m_filterName;
247  }
248 
252  virtual void Rename( const String& newName )
253  {
254  m_filterName = newName.Trimmed();
255  }
256 
261  int Size() const
262  {
263  return m_rowFilter.Length(); // both 1D filters have the same length
264  }
265 
271  virtual void Resize( int n )
272  {
273  PCL_PRECONDITION( n == 0 || n >= 3 )
274  PCL_PRECONDITION( n == 0 || (n & 1) )
275  if ( n == 0 )
276  m_colFilter = m_rowFilter = coefficient_vector();
277  else
278  m_colFilter = m_rowFilter = coefficient_vector( PCL_VALID_KERNEL_SIZE( n ) );
279  }
280 
284  bool IsEmpty() const
285  {
286  return m_rowFilter.IsEmpty(); // both 1D filters have the same length
287  }
288 
293  {
294  return m_rowFilter;
295  }
296 
301  {
302  return m_colFilter;
303  }
304 
309  {
310  return ColumnFilter();
311  }
312 
317  template <typename T>
319  {
320  return GenericVector<T>( m_rowFilter.Begin(), m_rowFilter.Length() );
321  }
322 
327  template <typename T>
329  {
330  return GenericVector<T>( m_colFilter.Begin(), m_rowFilter.Length() );
331  }
332 
336  template <typename T>
338  {
339  return ColumnFilterAs( (T*)0 );
340  }
341 
347  coefficient_vector Filter( int phase ) const
348  {
349  return phase ? m_colFilter : m_rowFilter;
350  }
351 
358  template <typename T>
359  GenericVector<T> FilterAs( int phase, T* ) const
360  {
361  return phase ? ColumnFilterAs( (T*)0 ) : RowFilterAs( (T*)0 );
362  }
363 
368  double Weight() const
369  {
370  return m_rowFilter.Sum() * m_colFilter.Sum();
371  }
372 
384  bool IsHighPassFilter() const
385  {
386  int s = -1;
387  for ( coefficient_vector::const_iterator i = m_rowFilter.Begin(); i < m_rowFilter.End(); ++i )
388  if ( *i != 0 )
389  for ( s = *i < 0; ++i < m_rowFilter.End(); )
390  if ( *i != 0 && (*i < 0) != s )
391  return true;
392  for ( coefficient_vector::const_iterator i = m_colFilter.Begin(); i < m_colFilter.End(); ++i )
393  if ( *i != 0 )
394  for ( s = (s < 0) ? *i++ < 0 : s; i < m_colFilter.End(); ++i )
395  if ( *i != 0 && (*i < 0) != s )
396  return true;
397  return false;
398  }
399 
404  bool SameCoefficients( const SeparableFilter& f ) const
405  {
406  return m_colFilter == f.m_colFilter && m_rowFilter == f.m_rowFilter;
407  }
408 
413  virtual void Clear()
414  {
415  m_colFilter = m_rowFilter = coefficient_vector();
416  }
417 
418 protected:
419 
420  /*
421  * Horizontal and vertical one-dimensional filters. The 2D filter matrix is
422  * equal to the product of these two vectors.
423  */
424  coefficient_vector m_rowFilter;
425  coefficient_vector m_colFilter;
426 
427  /*
428  * Identifying name.
429  */
430  String m_filterName;
431 };
432 
433 // ----------------------------------------------------------------------------
434 
435 } // pcl
436 
437 #endif // __PCL_SeparableFilter_h
438 
439 // ----------------------------------------------------------------------------
440 // EOF pcl/SeparableFilter.h - Released 2024-12-28T16:53:48Z
bool IsEmpty() const noexcept
Definition: Vector.h:1839
const coefficient * const_iterator
Definition: Vector.h:140
int Length() const noexcept
Definition: Vector.h:1802
Separable filter in two dimensions.
virtual SeparableFilter * Clone() const
virtual void Resize(int n)
SeparableFilter(int n, const String &name=String())
coefficient_vector RowFilter() const
SeparableFilter(const coefficient_vector &h, const coefficient_vector &v, const String &name=String())
coefficient_vector ColFilter() const
SeparableFilter(const SeparableFilter &)=default
SeparableFilter(SeparableFilter &&)=default
coefficient_vector ColumnFilter() const
virtual void Clear()
virtual void Rename(const String &newName)
SeparableFilter(int n, const T &x, const String &name=String())
GenericVector< T > ColFilterAs(T *) const
bool SameCoefficients(const SeparableFilter &f) const
double Weight() const
coefficient_vector Filter(int phase) const
GenericVector< T > RowFilterAs(T *) const
bool IsHighPassFilter() const
SeparableFilter(const String &name=String())
GenericVector< T > FilterAs(int phase, T *) const
SeparableFilter(const T *h, const T *v, int n, const String &name=String())
GenericVector< T > ColumnFilterAs(T *) const
Unicode (UTF-16) string.
Definition: String.h:8146
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2285
String Name(int colorSpace)
PCL root namespace.
Definition: AbstractImage.h:77