From 7bc5f8663889454e707589479e5ec2472c7b30fe Mon Sep 17 00:00:00 2001 From: giraffedata Date: Mon, 2 Nov 2009 16:29:43 +0000 Subject: cleanup git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1007 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- editor/pamscale.c | 226 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 126 insertions(+), 100 deletions(-) (limited to 'editor/pamscale.c') diff --git a/editor/pamscale.c b/editor/pamscale.c index 35c76b17..2c3388e4 100644 --- a/editor/pamscale.c +++ b/editor/pamscale.c @@ -31,9 +31,9 @@ #include #include "pm_c_util.h" -#include "pam.h" -#include "shhopt.h" #include "mallocvar.h" +#include "shhopt.h" +#include "pam.h" /****************************/ @@ -52,20 +52,22 @@ #define EPSILON 1e-7 + /* x^2 and x^3 helper functions */ static __inline__ double -pow2 (double x) -{ - return x*x; +pow2(double const x) { + return x * x; } + + static __inline__ double -pow3 (double x) -{ - return x*x*x; +pow3(double const x) { + return x * x * x; } + /* box, pulse, Fourier window, */ /* box function also know as rectangle function */ /* 1st order (constant) b-spline */ @@ -74,14 +76,15 @@ pow3 (double x) #define radius_box (0.5) static double -filter_box (double x) -{ - if (x < 0.0) x = -x; - if (x <= 0.5) return 1.0; - return 0.0; +filter_box(double const x) { + + double const absx = x < 0.0 ? -x : x; + + return (absx <= 0.5) ? 1.0 : 0.0; } + /* triangle, Bartlett window, */ /* triangle function also known as lambda function */ /* 2nd order (linear) b-spline */ @@ -89,56 +92,66 @@ filter_box (double x) #define radius_triangle (1.0) static double -filter_triangle (double x) -{ - if (x < 0.0) x = -x; - if (x < 1.0) return 1.0-x; - return 0.0; +filter_triangle(double const x) { + + double const absx = x < 0.0 ? -x : x; + + return absx < 1.0 ? 1.0 - absx : 0.0; } + /* 3rd order (quadratic) b-spline */ #define radius_quadratic (1.5) static double -filter_quadratic(double x) -{ - if (x < 0.0) x = -x; - if (x < 0.5) return 0.75-pow2(x); - if (x < 1.5) return 0.50*pow2(x-1.5); - return 0.0; +filter_quadratic(double const x) { + + double const absx = x < 0.0 ? -x : x; + + return + absx < 0.5 ? 0.75 - pow2(absx) : + absx < 1.5 ? 0.50 * pow2(absx - 1.5) : + 0.0; } + /* 4th order (cubic) b-spline */ #define radius_cubic (2.0) static double -filter_cubic(double x) -{ - if (x < 0.0) x = -x; - if (x < 1.0) return 0.5*pow3(x) - pow2(x) + 2.0/3.0; - if (x < 2.0) return pow3(2.0-x)/6.0; - return 0.0; +filter_cubic(double const x) { + + double const absx = x < 0.0 ? -x : x; + + return + absx < 1.0 ? 0.5 * pow3(absx) - pow2(absx) + 2.0/3.0 : + absx < 2.0 ? pow3(2.0-absx)/6.0 : + 0.0; } + /* Catmull-Rom spline, Overhauser spline */ #define radius_catrom (2.0) static double -filter_catrom(double x) -{ - if (x < 0.0) x = -x; - if (x < 1.0) return 1.5*pow3(x) - 2.5*pow2(x) + 1.0; - if (x < 2.0) return -0.5*pow3(x) + 2.5*pow2(x) - 4.0*x + 2.0; - return 0.0; +filter_catrom(double const x) { + + double const absx = x < 0.0 ? -x : x; + + return + absx < 1.0 ? 1.5 * pow3(absx) - 2.5 * pow2(absx) + 1.0 : + absx < 2.0 ? -0.5 * pow3(absx) + 2.5 * pow2(absx) - 4.0 * absx + 2.0 : + 0.0; } + /* Mitchell & Netravali's two-param cubic */ /* see Mitchell&Netravali, */ /* "Reconstruction Filters in Computer Graphics", SIGGRAPH 88 */ @@ -149,98 +162,106 @@ static double filter_mitchell(double x) { - double b = 1.0/3.0; - double c = 1.0/3.0; - - double p0 = ( 6.0 - 2.0*b ) / 6.0; - double p2 = (-18.0 + 12.0*b + 6.0*c) / 6.0; - double p3 = ( 12.0 - 9.0*b - 6.0*c) / 6.0; - double q0 = ( 8.0*b + 24.0*c) / 6.0; - double q1 = ( - 12.0*b - 48.0*c) / 6.0; - double q2 = ( 6.0*b + 30.0*c) / 6.0; - double q3 = ( - b - 6.0*c) / 6.0; - - if (x < 0.0) x = -x; - if (x < 1.0) return p3*pow3(x) + p2*pow2(x) + p0; - if (x < 2.0) return q3*pow3(x) + q2*pow2(x) + q1*x + q0; - return 0.0; + double const b = 1.0/3.0; + double const c = 1.0/3.0; + + double const p0 = ( 6.0 - 2.0*b ) / 6.0; + double const p2 = (-18.0 + 12.0*b + 6.0*c) / 6.0; + double const p3 = ( 12.0 - 9.0*b - 6.0*c) / 6.0; + double const q0 = ( 8.0*b + 24.0*c) / 6.0; + double const q1 = ( - 12.0*b - 48.0*c) / 6.0; + double const q2 = ( 6.0*b + 30.0*c) / 6.0; + double const q3 = ( - b - 6.0*c) / 6.0; + + double const absx = x < 0.0 ? -x : x; + + return + absx < 1.0 ? p3 * pow3(absx) + p2 * pow2(absx) + p0 : + absx < 2.0 ? q3 * pow3(absx) + q2 * pow2(absx) + q1 * absx + q0 : + 0.0; } + /* Gaussian filter (infinite) */ #define radius_gauss (1.25) static double -filter_gauss(double x) -{ +filter_gauss(double const x) { + return exp(-2.0*pow2(x)) * sqrt(2.0/M_PI); } + /* sinc, perfect lowpass filter (infinite) */ #define radius_sinc (4.0) static double -filter_sinc(double x) -{ +filter_sinc(double const x) { /* Note: Some people say sinc(x) is sin(x)/x. Others say it's sin(PI*x)/(PI*x), a horizontal compression of the former which is zero at integer values. We use the latter, whose Fourier transform is a canonical rectangle function (edges at -1/2, +1/2, height 1). */ - if (x == 0.0) return 1.0; - return sin(M_PI*x)/(M_PI*x); + return + x == 0.0 ? 1.0 : + sin(M_PI*x)/(M_PI*x); } + /* Bessel (for circularly symm. 2-d filt, infinite) */ /* See Pratt "Digital Image Processing" p. 97 for Bessel functions */ #define radius_bessel (3.2383) static double -filter_bessel(double x) -{ - if (x == 0.0) return M_PI/4.0; - return j1(M_PI*x)/(2.0*x); +filter_bessel(double const x) { + + return + x == 0.0 ? M_PI/4.0 : + j1(M_PI * x) / (2.0 * x); } + /* Hanning window (infinite) */ #define radius_hanning (1.0) static double -filter_hanning(double x) -{ - return 0.5*cos(M_PI*x) + 0.5; +filter_hanning(double const x) { + + return 0.5 * cos(M_PI * x) + 0.5; } + /* Hamming window (infinite) */ #define radius_hamming (1.0) static double -filter_hamming(double x) -{ - return 0.46*cos(M_PI*x) + 0.54; +filter_hamming(double const x) { + return 0.46 * cos(M_PI * x) + 0.54; } + /* Blackman window (infinite) */ #define radius_blackman (1.0) static double -filter_blackman(double x) -{ - return 0.5*cos(M_PI*x) + 0.08*cos(2.0*M_PI*x) + 0.42; +filter_blackman(double const x) { + return 0.5 * cos(M_PI * x) + 0.08 * cos(2.0 * M_PI * x) + 0.42; } + /* parameterized Kaiser window (infinite) */ /* from Oppenheim & Schafer, Hamming */ @@ -248,8 +269,7 @@ filter_blackman(double x) /* modified zeroth order Bessel function of the first kind. */ static double -bessel_i0(double x) -{ +bessel_i0(double const x) { int i; double sum, y, t; @@ -257,64 +277,70 @@ bessel_i0(double x) sum = 1.0; y = pow2(x)/4.0; t = y; - for (i=2; t>EPSILON; i++) { + for (i=2; t>EPSILON; ++i) { sum += t; t *= (double)y/pow2(i); } return sum; } + + static double -filter_kaiser(double x) -{ - /* typically 4nomix && filterSpec) @@ -2081,14 +2107,14 @@ scaleWithoutMixing(const struct pam * const inpamP, int -main(int argc, char **argv ) { +main(int argc, const char **argv ) { struct cmdlineInfo cmdline; FILE* ifP; struct pam inpam, outpam; float xscale, yscale; - pnm_init(&argc, argv); + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); -- cgit 1.4.1