1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
/* various interpolation templates
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
* Various casts which assume that the data is already in range. (That
* is, they are to be used with monotone samplers.)
*/
template <typename T> static T inline
to_fptypes( const double val )
{
const T newval = val;
return( newval );
}
template <typename T> static T inline
to_withsign( const double val )
{
const int sign_of_val = 2 * ( val >= 0. ) - 1;
const int rounded_abs_val = .5 + sign_of_val * val;
const T newval = sign_of_val * rounded_abs_val;
return( newval );
}
template <typename T> static T inline
to_nosign( const double val )
{
const T newval = .5 + val;
return( newval );
}
/*
* Various bilinear implementation templates. Note that no clampling
* is used: There is an assumption that the data is such that
* over/underflow is not an issue:
*/
/*
* Bilinear interpolation for float and double types. The first four
* inputs are weights, the last four are the corresponding pixel
* values:
*/
template <typename T> static T inline
bilinear_fptypes(
const double w_times_z,
const double x_times_z,
const double w_times_y,
const double x_times_y,
const double tre_thr,
const double tre_thrfou,
const double trequa_thr,
const double trequa_thrfou )
{
const T newval =
w_times_z * tre_thr +
x_times_z * tre_thrfou +
w_times_y * trequa_thr +
x_times_y * trequa_thrfou;
return( newval );
}
/*
* Bilinear interpolation for signed integer types:
*/
template <typename T> static T inline
bilinear_withsign(
const double w_times_z,
const double x_times_z,
const double w_times_y,
const double x_times_y,
const double tre_thr,
const double tre_thrfou,
const double trequa_thr,
const double trequa_thrfou )
{
const double val =
w_times_z * tre_thr +
x_times_z * tre_thrfou +
w_times_y * trequa_thr +
x_times_y * trequa_thrfou;
const int sign_of_val = 2 * ( val >= 0. ) - 1;
const int rounded_abs_val = .5 + sign_of_val * val;
const T newval = sign_of_val * rounded_abs_val;
return( newval );
}
/*
* Bilinear Interpolation for unsigned integer types:
*/
template <typename T> static T inline
bilinear_nosign(
const double w_times_z,
const double x_times_z,
const double w_times_y,
const double x_times_y,
const double tre_thr,
const double tre_thrfou,
const double trequa_thr,
const double trequa_thrfou )
{
const T newval =
w_times_z * tre_thr +
x_times_z * tre_thrfou +
w_times_y * trequa_thr +
x_times_y * trequa_thrfou +
0.5;
return( newval );
}
/*
* Bicubic (Catmull-Rom) interpolation templates:
*/
static int inline
unsigned_fixed_round( int v )
{
const int round_by = VIPS_INTERPOLATE_SCALE >> 1;
return( (v + round_by) >> VIPS_INTERPOLATE_SHIFT );
}
/* Fixed-point integer bicubic, used for 8-bit types.
*/
template <typename T> static int inline
bicubic_unsigned_int(
const T uno_one, const T uno_two, const T uno_thr, const T uno_fou,
const T dos_one, const T dos_two, const T dos_thr, const T dos_fou,
const T tre_one, const T tre_two, const T tre_thr, const T tre_fou,
const T qua_one, const T qua_two, const T qua_thr, const T qua_fou,
const int* restrict cx, const int* restrict cy )
{
const int c0 = cx[0];
const int c1 = cx[1];
const int c2 = cx[2];
const int c3 = cx[3];
const int r0 = unsigned_fixed_round(
c0 * uno_one +
c1 * uno_two +
c2 * uno_thr +
c3 * uno_fou );
const int r1 = unsigned_fixed_round(
c0 * dos_one +
c1 * dos_two +
c2 * dos_thr +
c3 * dos_fou );
const int r2 = unsigned_fixed_round(
c0 * tre_one +
c1 * tre_two +
c2 * tre_thr +
c3 * tre_fou );
const int r3 = unsigned_fixed_round(
c0 * qua_one +
c1 * qua_two +
c2 * qua_thr +
c3 * qua_fou );
return( unsigned_fixed_round(
cy[0] * r0 +
cy[1] * r1 +
cy[2] * r2 +
cy[3] * r3 ) );
}
static int inline
signed_fixed_round( int v )
{
const int sign_of_v = 2 * (v > 0) - 1;
const int round_by = sign_of_v * (VIPS_INTERPOLATE_SCALE >> 1);
return( (v + round_by) >> VIPS_INTERPOLATE_SHIFT );
}
/* Fixed-point integer bicubic, used for 8-bit types.
*/
template <typename T> static int inline
bicubic_signed_int(
const T uno_one, const T uno_two, const T uno_thr, const T uno_fou,
const T dos_one, const T dos_two, const T dos_thr, const T dos_fou,
const T tre_one, const T tre_two, const T tre_thr, const T tre_fou,
const T qua_one, const T qua_two, const T qua_thr, const T qua_fou,
const int* restrict cx, const int* restrict cy )
{
const int c0 = cx[0];
const int c1 = cx[1];
const int c2 = cx[2];
const int c3 = cx[3];
const int r0 = signed_fixed_round(
c0 * uno_one +
c1 * uno_two +
c2 * uno_thr +
c3 * uno_fou );
const int r1 = signed_fixed_round(
c0 * dos_one +
c1 * dos_two +
c2 * dos_thr +
c3 * dos_fou );
const int r2 = signed_fixed_round(
c0 * tre_one +
c1 * tre_two +
c2 * tre_thr +
c3 * tre_fou );
const int r3 = signed_fixed_round(
c0 * qua_one +
c1 * qua_two +
c2 * qua_thr +
c3 * qua_fou );
return( signed_fixed_round(
cy[0] * r0 +
cy[1] * r1 +
cy[2] * r2 +
cy[3] * r3 ) );
}
template <typename T> static T inline
cubic_float(
const T one, const T two, const T thr, const T fou,
const double* restrict cx )
{
return( cx[0] * one +
cx[1] * two +
cx[2] * thr +
cx[3] * fou );
}
/* Floating-point bicubic, used for int/float/double types.
*/
template <typename T> static T inline
bicubic_float(
const T uno_one, const T uno_two, const T uno_thr, const T uno_fou,
const T dos_one, const T dos_two, const T dos_thr, const T dos_fou,
const T tre_one, const T tre_two, const T tre_thr, const T tre_fou,
const T qua_one, const T qua_two, const T qua_thr, const T qua_fou,
const double* restrict cx, const double* restrict cy )
{
const double r0 = cubic_float<T>(
uno_one, uno_two, uno_thr, uno_fou, cx );
const double r1 = cubic_float<T>(
dos_one, dos_two, dos_thr, dos_fou, cx );
const double r2 = cubic_float<T>(
tre_one, tre_two, tre_thr, tre_fou, cx );
const double r3 = cubic_float<T>(
qua_one, qua_two, qua_thr, qua_fou, cx );
return( cubic_float<T>( r0, r1, r2, r3, cy ) );
}
/* Given an offset in [0,1] (we can have x == 1 when building tables),
* calculate c0, c1, c2, c3, the catmull-rom coefficients. This is called
* from the interpolator as well as from the table builder.
*/
static void inline
calculate_coefficients_catmull( double c[4], const double x )
{
/* Nicolas believes that the following is an hitherto unknown
* hyper-efficient method of computing Catmull-Rom coefficients. It
* only uses 4* & 1+ & 5- for a total of only 10 flops to compute
* four coefficients.
*/
const double cr1 = 1. - x;
const double cr2 = -.5 * x;
const double cr3 = cr1 * cr2;
const double cone = cr1 * cr3;
const double cfou = x * cr3;
const double cr4 = cfou - cone;
const double ctwo = cr1 - cone + cr4;
const double cthr = x - cfou - cr4;
g_assert( x >= 0. && x <= 1. );
c[0] = cone;
c[3] = cfou;
c[1] = ctwo;
c[2] = cthr;
}
/* Given an x in [0,1] (we can have x == 1 when building tables),
* calculate c0 .. c(@shrink + 1), the triangle coefficients. This is called
* from the interpolator as well as from the table builder.
*/
static void inline
calculate_coefficients_triangle( double *c,
const double shrink, const double x )
{
/* Needs to be in sync with vips_reduce_get_points().
*/
const int n_points = 2 * rint( shrink ) + 1;
const double half = x + n_points / 2.0 - 1;
int i;
double sum;
sum = 0;
for( i = 0; i < n_points; i++ ) {
const double xp = (i - half) / shrink;
double l;
l = 1.0 - VIPS_FABS( xp );
l = VIPS_MAX( 0.0, l );
c[i] = l;
sum += l;
}
for( i = 0; i < n_points; i++ )
c[i] /= sum;
}
/* Generate a cubic filter. See:
*
* Mitchell and Netravali, Reconstruction Filters in Computer Graphics
* Computer Graphics, Volume 22, Number 4, August 1988.
*
* B = 1, C = 0 - cubic B-spline
* B = 1/3, C = 1/3 - Mitchell
* B = 0, C = 1/2 - Catmull-Rom spline
*/
static void inline
calculate_coefficients_cubic( double *c,
const double shrink, const double x, double B, double C )
{
/* Needs to be in sync with vips_reduce_get_points().
*/
const int n_points = 2 * rint( 2 * shrink ) + 1;
const double half = x + n_points / 2.0 - 1;
int i;
double sum;
sum = 0;
for( i = 0; i < n_points; i++ ) {
const double xp = (i - half) / shrink;
const double axp = VIPS_FABS( xp );
const double axp2 = axp * axp;
const double axp3 = axp2 * axp;
double l;
if( axp <= 1 )
l = ((12 - 9 * B - 6 * C) * axp3 +
(-18 + 12 * B + 6 * C) * axp2 +
(6 - 2 * B)) / 6;
else if( axp <= 2 )
l = ((-B - 6 * C) * axp3 +
(6 * B + 30 * C) * axp2 +
(-12 * B - 48 * C) * axp +
(8 * B + 24 * C)) / 6;
else
l = 0.0;
c[i] = l;
sum += l;
}
for( i = 0; i < n_points; i++ )
c[i] /= sum;
}
/* Given an x in [0,1] (we can have x == 1 when building tables),
* calculate c0 .. c(@a * @shrink + 1), the lanczos coefficients. This is called
* from the interpolator as well as from the table builder.
*
* @a is the number of lobes, so usually 2 or 3. @shrink is the reduction
* factor, so 1 for interpolation, 2 for a x2 reduction, etc. We need more
* points for large decimations to avoid aliasing.
*/
static void inline
calculate_coefficients_lanczos( double *c,
const int a, const double shrink, const double x )
{
/* Needs to be in sync with vips_reduce_get_points().
*/
const int n_points = 2 * rint( a * shrink ) + 1;
const double half = x + n_points / 2.0 - 1;
int i;
double sum;
sum = 0;
for( i = 0; i < n_points; i++ ) {
const double xp = (i - half) / shrink;
double l;
if( xp == 0.0 )
l = 1.0;
else if( xp < -a )
l = 0.0;
else if( xp > a )
l = 0.0;
else
l = (double) a * sin( VIPS_PI * xp ) *
sin( VIPS_PI * xp / (double) a ) /
(VIPS_PI * VIPS_PI * xp * xp);
c[i] = l;
sum += l;
}
for( i = 0; i < n_points; i++ )
c[i] /= sum;
}
/* Our inner loop for resampling with a convolution. Operate on elements of
* type T, gather results in an intermediate of type IT.
*/
template <typename T, typename IT>
static IT
reduce_sum( const T * restrict in, int stride, const IT * restrict c, int n )
{
IT sum;
sum = 0;
for( int i = 0; i < n; i++ ) {
sum += c[i] * in[0];
in += stride;
}
return( sum );
}
|