diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2009-11-02 16:29:43 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2009-11-02 16:29:43 +0000 |
commit | 7bc5f8663889454e707589479e5ec2472c7b30fe (patch) | |
tree | 656e671c03536613fb602ec8e36a45b1a2e94b4c /editor/pamscale.c | |
parent | 5821c709fe6b9099884c7260b3070b9a699a85d6 (diff) | |
download | netpbm-mirror-7bc5f8663889454e707589479e5ec2472c7b30fe.tar.gz netpbm-mirror-7bc5f8663889454e707589479e5ec2472c7b30fe.tar.xz netpbm-mirror-7bc5f8663889454e707589479e5ec2472c7b30fe.zip |
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1007 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'editor/pamscale.c')
-rw-r--r-- | editor/pamscale.c | 226 |
1 files changed, 126 insertions, 100 deletions
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 <assert.h> #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 4<a<9 */ +filter_kaiser(double const x) { + /* typically 4 < a < 9 */ /* param a trades off main lobe width (sharpness) */ /* for side lobe amplitude (ringing) */ - double a = 6.5; - double i0a = 1.0/bessel_i0(a); + double const a = 6.5; + double const i0a = 1.0/bessel_i0(a); - return i0a*bessel_i0(a*sqrt(1.0-pow2(x))); + return i0a * bessel_i0(a * sqrt(1.0-pow2(x))); } + /* normal distribution (infinite) */ /* Normal(x) = Gaussian(x/2)/2 */ #define radius_normal (1.0) static double -filter_normal(double x) -{ +filter_normal(double const x) { return exp(-pow2(x)/2.0) / sqrt(2.0*M_PI); - return 0.0; } + /* Hermite filter */ #define radius_hermite (1.0) static double -filter_hermite(double x) -{ +filter_hermite(double const x) { /* f(x) = 2|x|^3 - 3|x|^2 + 1, -1 <= x <= 1 */ - if (x < 0.0) x = -x; - if (x < 1.0) return 2.0*pow3(x) - 3.0*pow2(x) + 1.0; - return 0.0; + + double const absx = x < 0.0 ? -x : x; + + return + absx < 1.0 ? 2.0 * pow3(absx) - 3.0 * pow2(absx) + 1.0 : + 0.0; } + /* Lanczos filter */ #define radius_lanczos (3.0) static double -filter_lanczos(double x) -{ - if (x < 0.0) x = -x; - if (x < 3.0) return filter_sinc(x) * filter_sinc(x/3.0); - return(0.0); +filter_lanczos(double const x) { + + double const absx = x < 0.0 ? -x : x; + + return + x < 3.0 ? filter_sinc(absx) * filter_sinc(absx/3.0) : + 0.0; } @@ -491,7 +517,7 @@ processFilterOptions(unsigned int const filterSpec, static void parseXyParms(int const argc, - char ** const argv, + const char ** const argv, struct cmdlineInfo * const cmdlineP) { /* parameters are box width (columns), box height (rows), and @@ -533,7 +559,7 @@ parseXyParms(int const argc, static void parseScaleParms(int const argc, - char ** const argv, + const char ** const argv, struct cmdlineInfo * const cmdlineP) { /* parameters are scale factor and optional filespec */ @@ -559,7 +585,7 @@ parseScaleParms(int const argc, static void parseFilespecOnlyParms(int const argc, - char ** const argv, + const char ** const argv, struct cmdlineInfo * const cmdlineP) { /* Only parameter allowed is optional filespec */ @@ -572,7 +598,7 @@ parseFilespecOnlyParms(int const argc, static void parseCommandLine(int argc, - char ** argv, + const char ** argv, struct cmdlineInfo * const cmdlineP) { /* -------------------------------------------------------------------------- Parse program command line described in Unix standard form by argc @@ -585,8 +611,8 @@ parseCommandLine(int argc, was passed to us as the argv array. We also trash *argv. --------------------------------------------------------------------------*/ optEntry *option_def; - /* Instructions to optParseOptions3 on how to parse our options. */ optStruct3 opt; + /* Instructions to optParseOptions3 on how to parse our options. */ unsigned int option_def_index; unsigned int xyfit, xyfill; @@ -633,7 +659,7 @@ parseCommandLine(int argc, opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ - optParseOptions3( &argc, argv, opt, sizeof(opt), 0 ); + optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); /* Uses and sets argc, argv, and some of *cmdlineP and others. */ if (cmdlineP->nomix && 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); |