PCL
SeparableFilter.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.4.7
6 // ----------------------------------------------------------------------------
7 // pcl/SeparableFilter.h - Released 2020-12-17T15:46:29Z
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-2020 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  typedef float coefficient;
97 
102 
107 
111  SeparableFilter( const String& name = String() )
112  : filterName( name )
113  {
114  }
115 
124  SeparableFilter( int n, const String& name = String() )
125  : rowFilter( PCL_VALID_KERNEL_SIZE( n ) )
126  , colFilter( PCL_VALID_KERNEL_SIZE( n ) )
127  , 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  : rowFilter( x, PCL_VALID_KERNEL_SIZE( n ) )
143  , colFilter( x, PCL_VALID_KERNEL_SIZE( n ) )
144  , 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  : rowFilter( h )
157  , colFilter( v )
158  , 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  : rowFilter( h, PCL_VALID_KERNEL_SIZE( n ) )
174  , colFilter( v, PCL_VALID_KERNEL_SIZE( n ) )
175  , 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 
189  SeparableFilter( SeparableFilter&& ) = default;
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  rowFilter = x;
227  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 filterName;
247  }
248 
252  virtual void Rename( const String& newName )
253  {
254  filterName = newName.Trimmed();
255  }
256 
261  int Size() const
262  {
263  return 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  colFilter = rowFilter = coefficient_vector();
277  else
278  colFilter = rowFilter = coefficient_vector( PCL_VALID_KERNEL_SIZE( n ) );
279  }
280 
284  bool IsEmpty() const
285  {
286  return rowFilter.IsEmpty(); // both 1D filters have the same length
287  }
288 
292  coefficient_vector RowFilter() const
293  {
294  return rowFilter;
295  }
296 
300  coefficient_vector ColumnFilter() const
301  {
302  return colFilter;
303  }
304 
308  coefficient_vector ColFilter() const
309  {
310  return colFilter;
311  }
312 
318  coefficient_vector Filter( int phase ) const
319  {
320  return phase ? colFilter : rowFilter;
321  }
322 
327  double Weight() const
328  {
329  return rowFilter.Sum() * colFilter.Sum();
330  }
331 
343  bool IsHighPassFilter() const
344  {
345  int s = -1;
346  for ( coefficient_vector::const_iterator i = rowFilter.Begin(); i < rowFilter.End(); ++i )
347  if ( *i != 0 )
348  for ( s = *i < 0; ++i < rowFilter.End(); )
349  if ( *i != 0 && (*i < 0) != s )
350  return true;
351  for ( coefficient_vector::const_iterator i = colFilter.Begin(); i < colFilter.End(); ++i )
352  if ( *i != 0 )
353  for ( s = (s < 0) ? *i++ < 0 : s; i < colFilter.End(); ++i )
354  if ( *i != 0 && (*i < 0) != s )
355  return true;
356  return false;
357  }
358 
363  bool SameCoefficients( const SeparableFilter& f ) const
364  {
365  return colFilter == f.colFilter && rowFilter == f.rowFilter;
366  }
367 
372  virtual void Clear()
373  {
374  colFilter = rowFilter = coefficient_vector();
375  }
376 
377 protected:
378 
379  /*
380  * Horizontal and vertical one-dimensional filters. The 2D filter matrix is
381  * equal to the product of these two vectors.
382  */
383  coefficient_vector rowFilter;
384  coefficient_vector colFilter;
385 
386  /*
387  * Identifying name.
388  */
389  String filterName;
390 };
391 
392 // ----------------------------------------------------------------------------
393 
394 } // pcl
395 
396 #endif // __PCL_SeparableFilter_h
397 
398 // ----------------------------------------------------------------------------
399 // EOF pcl/SeparableFilter.h - Released 2020-12-17T15:46:29Z
SeparableFilter(int n, const String &name=String())
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2092
virtual SeparableFilter * Clone() const
PCL root namespace.
Definition: AbstractImage.h:76
GenericVector< coefficient > coefficient_vector
Separable filter in two dimensions.
coefficient_vector Filter(int phase) const
virtual void Resize(int n)
coefficient_vector ColumnFilter() const
GenericMatrix< coefficient > coefficient_matrix
double Weight() const
bool SameCoefficients(const SeparableFilter &f) const
Unicode (UTF-16) string.
Definition: String.h:7923
virtual void Clear()
bool IsEmpty() const noexcept
Definition: Vector.h:1749
SeparableFilter(const T *h, const T *v, int n, const String &name=String())
SeparableFilter(const coefficient_vector &h, const coefficient_vector &v, const String &name=String())
SeparableFilter(int n, const T &x, const String &name=String())
virtual void Rename(const String &newName)
int Length() const noexcept
Definition: Vector.h:1712
const coefficient * const_iterator
Definition: Vector.h:128
String Name(int colorSpace)
bool IsHighPassFilter() const
coefficient_vector RowFilter() const
SeparableFilter(const String &name=String())
coefficient_vector ColFilter() const