GDAL
gdalresamplingkernels.h
1/******************************************************************************
2 * $Id$
3 *
4 * Project: GDAL Raster Interpolation
5 * Purpose: Interpolation algorithms with cache
6 * Author: Frank Warmerdam, warmerdam@pobox.com
7 *
8 ******************************************************************************
9 * Copyright (c) 2001, Frank Warmerdam
10 * Copyright (c) 2008-2012, Even Rouault <even dot rouault at spatialys.com>
11 * Copyright (c) 2024, Javier Jimenez Shaw
12 *
13 * SPDX-License-Identifier: MIT
14 ****************************************************************************/
15
16#ifndef GDALRESAMPLINGKERNELS_H_INCLUDED
17#define GDALRESAMPLINGKERNELS_H_INCLUDED
18
19#include <cmath>
20
23static inline double CubicKernel(double dfX)
24{
25 // http://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm
26 // W(x) formula with a = -0.5 (cubic hermite spline )
27 // or
28 // https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf
29 // k(x) (formula 8) with (B,C)=(0,0.5) the Catmull-Rom spline
30 double dfAbsX = fabs(dfX);
31 if (dfAbsX <= 1.0)
32 {
33 double dfX2 = dfX * dfX;
34 return dfX2 * (1.5 * dfAbsX - 2.5) + 1;
35 }
36 else if (dfAbsX <= 2.0)
37 {
38 double dfX2 = dfX * dfX;
39 return dfX2 * (-0.5 * dfAbsX + 2.5) - 4 * dfAbsX + 2;
40 }
41 else
42 return 0.0;
43}
44
45static inline double CubicSplineKernel(double dfVal)
46{
47 if (dfVal > 2.0)
48 return 0.0;
49
50 const double xm1 = dfVal - 1.0;
51 const double xp1 = dfVal + 1.0;
52 const double xp2 = dfVal + 2.0;
53
54 const double a = xp2 <= 0.0 ? 0.0 : xp2 * xp2 * xp2;
55 const double b = xp1 <= 0.0 ? 0.0 : xp1 * xp1 * xp1;
56 const double c = dfVal <= 0.0 ? 0.0 : dfVal * dfVal * dfVal;
57 const double d = xm1 <= 0.0 ? 0.0 : xm1 * xm1 * xm1;
58
59 return 0.16666666666666666667 * (a - (4.0 * b) + (6.0 * c) - (4.0 * d));
60}
61
64#endif /* ndef GDALRESAMPLINGKERNELS_H_INCLUDED */