PCL
BicubicInterpolation.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/BicubicInterpolation.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_BicubicInterpolation_h
53 #define __PCL_BicubicInterpolation_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
61 #include <pcl/Math.h>
62 #include <pcl/Utility.h>
63 
64 namespace pcl
65 {
66 
67 // ----------------------------------------------------------------------------
68 
69 #define m_width this->m_width
70 #define m_height this->m_height
71 #define m_fillBorder this->m_fillBorder
72 #define m_fillValue this->m_fillValue
73 #define m_data this->m_data
74 
75 // ----------------------------------------------------------------------------
76 
86 template <typename T>
88 {
89 public:
90 
95 
100 
101 protected:
102 
103  void InitXY( int& i1, int& j1, double* p0, double* p1, double* p2, double* p3, double x, double y ) const noexcept
104  {
105  PCL_PRECONDITION( int( x ) >= 0 )
106  PCL_PRECONDITION( int( x ) < m_width )
107  PCL_PRECONDITION( int( y ) >= 0 )
108  PCL_PRECONDITION( int( y ) < m_height )
109 
110  // Central grid coordinates
111  i1 = pcl::Range( TruncInt( y ), 0, m_height-1 );
112  j1 = pcl::Range( TruncInt( x ), 0, m_width-1 );
113 
114  // Set up source matrix
115 
116  int j0 = j1 - 1;
117  int i0 = i1 - 1;
118 
119  int j2 = j1 + 1;
120  int i2 = i1 + 1;
121 
122  int j3 = j1 + 2;
123  int i3 = i1 + 2;
124 
125  const T* fp = m_data + (int64( i0 )*m_width + j0);
126 
127  // Row 0
128 
129  if ( i0 < 0 )
130  {
131  fp += m_width;
132 
133  if ( m_fillBorder )
134  {
135  p0[0] = p0[1] = p0[2] = p0[3] = m_fillValue;
136  goto __row1;
137  }
138  }
139 
140  GetRow( p0, fp, j0, j2, j3 );
141 
142  if ( i0 >= 0 )
143  fp += m_width;
144 
145 __row1: // Row 1
146 
147  GetRow( p1, fp, j0, j2, j3 );
148 
149  // Row 2
150 
151  if ( i2 < m_height )
152  fp += m_width;
153 
154  GetRow( p2, fp, j0, j2, j3 );
155 
156  // Row 3
157 
158  if ( i3 < m_height )
159  fp += m_width;
160  else if ( m_fillBorder )
161  {
162  p3[0] = p3[1] = p3[2] = p3[3] = m_fillValue;
163  goto __done;
164  }
165 
166  GetRow( p3, fp, j0, j2, j3 );
167 
168 __done:
169  ;
170  }
171 
172  void InitYX( int& i1, int& j1, double* p0, double* p1, double* p2, double* p3, double x, double y ) const noexcept
173  {
174  PCL_PRECONDITION( int( x ) >= 0 )
175  PCL_PRECONDITION( int( x ) < m_width )
176  PCL_PRECONDITION( int( y ) >= 0 )
177  PCL_PRECONDITION( int( y ) < m_height )
178 
179  // Central grid coordinates
180  i1 = pcl::Range( TruncInt( y ), 0, m_height-1 );
181  j1 = pcl::Range( TruncInt( x ), 0, m_width-1 );
182 
183  // Set up source matrix
184 
185  int j0 = j1 - 1;
186  int i0 = i1 - 1;
187 
188  int j2 = j1 + 1;
189  int i2 = i1 + 1;
190 
191  int j3 = j1 + 2;
192  int i3 = i1 + 2;
193 
194  const T* fp = m_data + (int64( i0 )*m_width + j0);
195 
196  // Column 0
197 
198  if ( j0 < 0 )
199  {
200  ++fp;
201 
202  if ( m_fillBorder )
203  {
204  p0[0] = p0[1] = p0[2] = p0[3] = m_fillValue;
205  goto __col1;
206  }
207  }
208 
209  GetColumn( p0, fp, i0, i2, i3 );
210 
211  if ( j0 >= 0 )
212  ++fp;
213 
214 __col1: // Column 1
215 
216  GetColumn( p1, fp, i0, i2, i3 );
217 
218  // Column 2
219 
220  if ( j2 < m_width )
221  ++fp;
222 
223  GetColumn( p2, fp, i0, i2, i3 );
224 
225  // Column 3
226 
227  if ( j3 < m_width )
228  ++fp;
229  else if ( m_fillBorder )
230  {
231  p3[0] = p3[1] = p3[2] = p3[3] = m_fillValue;
232  goto __done;
233  }
234 
235  GetColumn( p3, fp, i0, i2, i3 );
236 
237 __done:
238  ;
239  }
240 
241 private:
242 
243  void GetRow( double* p, const T* fp, int j0, int j2, int j3 ) const noexcept
244  {
245  if ( m_fillBorder )
246  {
247  *p = (j0 >= 0) ? *fp : m_fillValue;
248  *++p = *++fp;
249 
250  if ( j2 < m_width )
251  {
252  *++p = *++fp;
253  *++p = (j3 < m_width) ? *++fp : m_fillValue;
254  }
255  else
256  p[1] = p[2] = m_fillValue;
257  }
258  else
259  {
260  if ( j0 < 0 )
261  ++fp;
262  *p = *fp;
263  if ( j0 >= 0 )
264  ++fp;
265  *++p = *fp;
266 
267  if ( j2 < m_width )
268  {
269  *++p = *++fp;
270  if ( j3 < m_width )
271  ++fp;
272  *++p = *fp;
273  }
274  else
275  {
276  *++p = *fp;
277  *++p = *(fp - 1);
278  }
279  }
280  }
281 
282  void GetColumn( double* p, const T* fp, int i0, int i2, int i3 ) const noexcept
283  {
284  if ( m_fillBorder )
285  {
286  *p = (i0 >= 0) ? *fp : m_fillValue;
287  *++p = *(fp += m_width);
288 
289  if ( i2 < m_height )
290  {
291  *++p = *(fp += m_width);
292  *++p = (i3 < m_height) ? *(fp += m_width) : m_fillValue;
293  }
294  else
295  p[1] = p[2] = m_fillValue;
296  }
297  else
298  {
299  if ( i0 < 0 )
300  fp += m_width;
301  *p = *fp;
302  if ( i0 >= 0 )
303  fp += m_width;
304  *++p = *fp;
305 
306  if ( i2 < m_height )
307  {
308  *++p = *(fp += m_width);
309  if ( i3 < m_height )
310  fp += m_width;
311  *++p = *fp;
312  }
313  else
314  {
315  *++p = *fp;
316  *++p = *(fp - m_width);
317  }
318  }
319  }
320 };
321 
322 // ----------------------------------------------------------------------------
323 
324 #define InitXY this->InitXY
325 #define InitYX this->InitYX
326 
327 /*
328  * Undefine the following to use any free constant value a != -1/2
329  */
330 #define __PCL_BICUBIC_SPLINE_A_IS_MINUS_ONE_HALF 1
331 
332 /*
333  * The "a" constant controls the depth of the negative lobes in the bicubic
334  * spline interpolation function, -1 <= a < 0. Larger values of a (in absolute
335  * value) lead to more ringing (sharpness). The default is -1/2, as recommended
336  * in [Keys 1981].
337  */
338 #define __PCL_BICUBIC_SPLINE_A -0.5
339 
340 /*
341  * Default clamping threshold for linear images. This value has been optimized
342  * for a = -1/2. If you use another value for a, this must also be fine tuned.
343  */
344 #ifndef __PCL_BICUBIC_SPLINE_CLAMPING_THRESHOLD
345 #define __PCL_BICUBIC_SPLINE_CLAMPING_THRESHOLD 0.3F
346 #endif
347 
378 template <typename T>
380 {
381 public:
382 
392  BicubicSplineInterpolation( float clamp = __PCL_BICUBIC_SPLINE_CLAMPING_THRESHOLD )
393  : m_clamp( Range( clamp, 0.0F, 1.0F ) )
394  {
395  PCL_PRECONDITION( 0 <= clamp && clamp <= 1 )
396  }
397 
402 
407  {
408  }
409 
419  double operator()( double x, double y ) const override
420  {
421  PCL_PRECONDITION( m_data != nullptr )
422  PCL_PRECONDITION( m_width > 0 && m_height > 0 )
423 
424  // Initialize grid coordinates and source matrix
425  int i1, j1;
426  double p0[ 4 ], p1[ 4 ], p2[ 4 ], p3[ 4 ];
427  InitXY( i1, j1, p0, p1, p2, p3, x, y );
428 
429  // Cubic spline coefficients
430  double C[ 4 ];
431  GetSplineCoefficients( C, x-j1 );
432 
433  // Interpolate neighbor rows
434  double c[ 4 ];
435  c[0] = Interpolate( p0, C );
436  c[1] = Interpolate( p1, C );
437  c[2] = Interpolate( p2, C );
438  c[3] = Interpolate( p3, C );
439 
440  // Interpolate result vertically
441  GetSplineCoefficients( C, y-i1 );
442  return Interpolate( c, C );
443  }
444 
459  void InterpolateX( double fx[], double x, double y ) const noexcept
460  {
461  PCL_PRECONDITION( m_data != nullptr )
462  PCL_PRECONDITION( m_width > 0 && m_height > 0 )
463 
464  // Initialize grid coordinates and source matrix
465  int i1, j1;
466  double p0[ 4 ], p1[ 4 ], p2[ 4 ], p3[ 4 ];
467  InitXY( i1, j1, p0, p1, p2, p3, x, y );
468 
469  // Cubic spline coefficients
470  double C[ 4 ];
471  GetSplineCoefficients( C, x-j1 );
472 
473  // Interpolate neighbor rows
474  fx[0] = Interpolate( p0, C );
475  fx[1] = Interpolate( p1, C );
476  fx[2] = Interpolate( p2, C );
477  fx[3] = Interpolate( p3, C );
478  }
479 
494  void InterpolateY( double fy[], double x, double y ) const noexcept
495  {
496  PCL_PRECONDITION( m_data != nullptr )
497  PCL_PRECONDITION( m_width > 0 && m_height > 0 )
498 
499  // Initialize grid coordinates and source matrix
500  int i1, j1;
501  double p0[ 4 ], p1[ 4 ], p2[ 4 ], p3[ 4 ];
502  InitYX( i1, j1, p0, p1, p2, p3, x, y );
503 
504  // Cubic spline coefficients
505  double C[ 4 ];
506  GetSplineCoefficients( C, y-i1 );
507 
508  // Interpolate neighbor columns
509  fy[0] = Interpolate( p0, C );
510  fy[1] = Interpolate( p1, C );
511  fy[2] = Interpolate( p2, C );
512  fy[3] = Interpolate( p3, C );
513  }
514 
534  double InterpolateVector( const double c[], double dx ) const noexcept
535  {
536  double C[ 4 ];
537  GetSplineCoefficients( C, dx );
538  return Interpolate( c, C );
539  }
540 
548  float ClampingThreshold() const noexcept
549  {
550  return m_clamp;
551  }
552 
576  void SetClampingThreshold( float c ) noexcept
577  {
578  PCL_PRECONDITION( 0 <= c && c <= 1 )
579  m_clamp = Range( c, 0.0F, 1.0F );
580  }
581 
582 private:
583 
584  double m_clamp;
585 
586  PCL_HOT_FUNCTION
587  double Interpolate( const double* __restrict__ p, const double* __restrict__ C ) const noexcept
588  {
589  // Unclamped code:
590  //return p[0]*C[0] + p[1]*C[1] + p[2]*C[2] + p[3]*C[3];
591  double f12 = p[1]*C[1] + p[2]*C[2];
592  double f03 = p[0]*C[0] + p[3]*C[3];
593  return (-f03 < f12*m_clamp) ? f12 + f03 : f12/(C[1] + C[2]);
594  }
595 
596  PCL_HOT_FUNCTION
597  void GetSplineCoefficients( double* __restrict__ C, double dx ) const noexcept
598  {
599  double dx2 = dx*dx;
600  double dx3 = dx2*dx;
601 
602 #ifdef __PCL_BICUBIC_SPLINE_A_IS_MINUS_ONE_HALF
603  // Optimized code for a = -1/2
604  // We *really* need optimization here since this routine is called twice
605  // for each interpolated pixel.
606  double dx1_2 = dx/2;
607  double dx2_2 = dx2/2;
608  double dx3_2 = dx3/2;
609  double dx22 = dx2 + dx2;
610  double dx315 = dx3 + dx3_2;
611  C[0] = dx2 - dx3_2 - dx1_2;
612  C[1] = dx315 - dx22 - dx2_2 + 1;
613  C[2] = dx22 - dx315 + dx1_2;
614  C[3] = dx3_2 - dx2_2;
615 #else
616 # define a (__PCL_BICUBIC_SPLINE_A)
617  C[0] = a*dx3 - 2*a*dx2 + a*dx;
618  C[1] = (a + 2)*dx3 - (a + 3)*dx2 + 1;
619  C[2] = -(a + 2)*dx3 + (2*a + 3)*dx2 - a*dx;
620  C[3] = -a*dx3 + a*dx2;
621 # undef a
622 #endif
623  }
624 };
625 
637 template <typename T>
639 {
640 public:
641 
645  BicubicInterpolation() = default;
646 
651 
656  {
657  }
658 };
659 
660 // ----------------------------------------------------------------------------
661 
678 template <typename T>
680 {
681 public:
682 
687 
692 
697  {
698  }
699 
706  double operator()( double x, double y ) const override
707  {
708  PCL_PRECONDITION( f != 0 )
709  PCL_PRECONDITION( m_width > 0 && m_height > 0 )
710 
711  // Initialize grid coordinates and source matrix
712  int i1, j1;
713  double p0[ 4 ], p1[ 4 ], p2[ 4 ], p3[ 4 ];
714  InitXY( i1, j1, p0, p1, p2, p3, x, y );
715 
716  // Bicubic B-Spline interpolation functions
717 
718  double dx = x - j1;
719  double dx0 = -1 - dx;
720  double dx1 = -dx;
721  double dx2 = 1 - dx;
722  double dx3 = 2 - dx;
723 
724  double dy = y - i1;
725  double dy0 = -1 - dy;
726  double dy1 = -dy;
727  double dy2 = 1 - dy;
728  double dy3 = 2 - dy;
729 
730  return
731  BXY( p0, 0, dx0, dy0 ) +
732  BXY( p0, 1, dx1, dy0 ) +
733  BXY( p0, 2, dx2, dy0 ) +
734  BXY( p0, 3, dx3, dy0 ) +
735  BXY( p1, 0, dx0, dy1 ) +
736  BXY( p1, 1, dx1, dy1 ) +
737  BXY( p1, 2, dx2, dy1 ) +
738  BXY( p1, 3, dx3, dy1 ) +
739  BXY( p2, 0, dx0, dy2 ) +
740  BXY( p2, 1, dx1, dy2 ) +
741  BXY( p2, 2, dx2, dy2 ) +
742  BXY( p2, 3, dx3, dy2 ) +
743  BXY( p3, 0, dx0, dy3 ) +
744  BXY( p3, 1, dx1, dy3 ) +
745  BXY( p3, 2, dx2, dy3 ) +
746  BXY( p3, 3, dx3, dy3 );
747  }
748 
749 private:
750 
751  double BXY( const double* __restrict__ p, int j, double x, double y ) const noexcept
752  {
753  return p[j]*B( x )*B( y );
754  }
755 
756  double B( double x ) const noexcept
757  {
758  double fx = (x > 0) ? x*x*x : 0;
759 
760  double fxp1 = x + 1;
761  fxp1 = (fxp1 > 0) ? fxp1*fxp1*fxp1 : 0;
762 
763  double fxp2 = x + 2;
764  fxp2 = (fxp2 > 0) ? fxp2*fxp2*fxp2 : 0;
765 
766  double fxm1 = x - 1;
767  fxm1 = (fxm1 > 0) ? fxm1*fxm1*fxm1 : 0;
768 
769  return (fxp2 - 4*fxp1 + 6*fx - 4*fxm1)/6;
770  }
771 };
772 
773 // ----------------------------------------------------------------------------
774 
775 #undef InitXY
776 #undef InitYX
777 
778 // ----------------------------------------------------------------------------
779 
780 #undef m_width
781 #undef m_height
782 #undef m_fillBorder
783 #undef m_fillValue
784 #undef m_data
785 
786 // ----------------------------------------------------------------------------
787 
788 } // pcl
789 
790 #endif // __PCL_BicubicInterpolation_h
791 
792 // ----------------------------------------------------------------------------
793 // EOF pcl/BicubicInterpolation.h - Released 2024-12-28T16:53:48Z
Bicubic B-Spline Interpolation Algorithm.
double operator()(double x, double y) const override
BicubicBSplineInterpolation(const BicubicBSplineInterpolation &)=default
Base class for bicubic interpolation algorithm implementations.
BicubicInterpolationBase(const BicubicInterpolationBase &)=default
Bicubic interpolation - an alias for BicubicSplineInterpolation.
BicubicInterpolation(const BicubicInterpolation &)=default
Bicubic spline interpolation algorithm.
BicubicSplineInterpolation(const BicubicSplineInterpolation &)=default
void InterpolateY(double fy[], double x, double y) const noexcept
float ClampingThreshold() const noexcept
BicubicSplineInterpolation(float clamp=__PCL_BICUBIC_SPLINE_CLAMPING_THRESHOLD)
double InterpolateVector(const double c[], double dx) const noexcept
void SetClampingThreshold(float c) noexcept
double operator()(double x, double y) const override
void InterpolateX(double fx[], double x, double y) const noexcept
A generic interface to two-dimensional interpolation algorithms.
int TruncInt(T x) noexcept
Definition: Math.h:1203
signed long long int64
Definition: Defs.h:676
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
Definition: Utility.h:190
PCL root namespace.
Definition: AbstractImage.h:77