43 #include "sinfo_new_resampling.h"
44 #include "sinfo_pixel_handling.h"
45 #include "sinfo_globals.h"
47 #include "sinfo_resampling.h"
52 static void new_reverse_tanh_kernel(
double * data,
int nn) ;
53 static double sinfo_new_sinc(
double x);
93 sinfo_new_generate_interpolation_kernel(
const char * kernel_type)
100 int samples = KERNEL_SAMPLES ;
102 if (kernel_type==NULL) {
103 tab = sinfo_new_generate_interpolation_kernel(
"tanh") ;
104 }
else if (!strcmp(kernel_type,
"default")) {
105 tab = sinfo_new_generate_interpolation_kernel(
"tanh") ;
106 }
else if (!strcmp(kernel_type,
"sinfo_new_sinc")) {
107 tab = cpl_malloc(samples *
sizeof(
double)) ;
109 tab[samples-1] = 0.0 ;
110 for (i=1 ; i<samples ; i++) {
111 x = (double)KERNEL_WIDTH * (
double)i/(double)(samples-1) ;
112 tab[i] = sinfo_new_sinc(x) ;
114 }
else if (!strcmp(kernel_type,
"sinc2")) {
115 tab = cpl_malloc(samples *
sizeof(
double)) ;
117 tab[samples-1] = 0.0 ;
118 for (i=1 ; i<samples ; i++) {
119 x = 2.0 * (double)i/(
double)(samples-1) ;
120 tab[i] = sinfo_new_sinc(x) ;
123 }
else if (!strcmp(kernel_type,
"lanczos")) {
124 tab = cpl_malloc(samples *
sizeof(
double)) ;
125 for (i=0 ; i<samples ; i++) {
126 x = (double)KERNEL_WIDTH * (
double)i/(double)(samples-1) ;
128 tab[i] = sinfo_new_sinc(x) * sinfo_new_sinc(x/2) ;
133 }
else if (!strcmp(kernel_type,
"hamming")) {
134 tab = cpl_malloc(samples *
sizeof(
double)) ;
136 inv_norm = 1.00 / (double)(samples - 1) ;
137 for (i=0 ; i<samples ; i++) {
139 if (i<(samples-1)/2) {
140 tab[i] = alpha + (1-alpha) * cos(2.0*PI_NUMB*x*inv_norm) ;
145 }
else if (!strcmp(kernel_type,
"hann")) {
146 tab = cpl_malloc(samples *
sizeof(
double)) ;
148 inv_norm = 1.00 / (double)(samples - 1) ;
149 for (i=0 ; i<samples ; i++) {
151 if (i<(samples-1)/2) {
152 tab[i] = alpha + (1-alpha) * cos(2.0*PI_NUMB*x*inv_norm) ;
157 }
else if (!strcmp(kernel_type,
"tanh")) {
158 tab = sinfo_new_generate_tanh_kernel(TANH_STEEPNESS) ;
182 sinfo_new_sinc(
double x)
185 return (
double)1.00 ;
187 return ((sin(x * (
double)PI_NUMB)) / (x * (
double)PI_NUMB)) ;
231 sinfo_new_warp_image_generic(
232 cpl_image * image_in,
234 cpl_polynomial * poly_u,
235 cpl_polynomial * poly_v
238 cpl_image * image_out ;
242 double neighbors[16] ;
256 if (image_in == NULL)
return NULL ;
260 kernel = sinfo_new_generate_interpolation_kernel(kernel_type) ;
261 if (kernel == NULL) {
265 ilx=cpl_image_get_size_x(image_in);
266 ily=cpl_image_get_size_y(image_in);
267 pidata=cpl_image_get_data_float(image_in);
273 image_out = cpl_image_new(lx_out, ly_out,CPL_TYPE_FLOAT) ;
274 podata=cpl_image_get_data_float(image_out);
278 leaps[0] = -1 - ilx ;
288 leaps[8] = -1 + ilx ;
293 leaps[12]= -1 + 2*ilx ;
295 leaps[14]= 1 + 2*ilx ;
296 leaps[15]= 2 + 2*ilx ;
298 vx=cpl_vector_new(2);
300 for (j=0 ; j < ly_out ; j++) {
301 for (i=0 ; i< lx_out ; i++) {
303 cpl_vector_set(vx,0,(
double)i);
304 cpl_vector_set(vx,1,(
double)j);
305 x = cpl_polynomial_eval(poly_u, vx);
306 y = cpl_polynomial_eval(poly_v, vx);
316 podata[i+j*lx_out] = (pixelvalue)0.0/0.0 ;
319 pos = px + py * ilx ;
320 for (k=0 ; k<16 ; k++)
321 neighbors[k] = (
double)(pidata[(int)(pos+leaps[k])]) ;
324 tabx = (x - (double)px) * (double)(TABSPERPIX) ;
325 taby = (y - (double)py) * (double)(TABSPERPIX) ;
330 rsc[0] = kernel[TABSPERPIX + tabx] ;
331 rsc[1] = kernel[tabx] ;
332 rsc[2] = kernel[TABSPERPIX - tabx] ;
333 rsc[3] = kernel[2 * TABSPERPIX - tabx] ;
334 rsc[4] = kernel[TABSPERPIX + taby] ;
335 rsc[5] = kernel[taby] ;
336 rsc[6] = kernel[TABSPERPIX - taby] ;
337 rsc[7] = kernel[2 * TABSPERPIX - taby] ;
339 sumrs = (rsc[0]+rsc[1]+rsc[2]+rsc[3]) *
340 (rsc[4]+rsc[5]+rsc[6]+rsc[7]) ;
343 cur = rsc[4] * ( rsc[0]*neighbors[0] +
344 rsc[1]*neighbors[1] +
345 rsc[2]*neighbors[2] +
346 rsc[3]*neighbors[3] ) +
347 rsc[5] * ( rsc[0]*neighbors[4] +
348 rsc[1]*neighbors[5] +
349 rsc[2]*neighbors[6] +
350 rsc[3]*neighbors[7] ) +
351 rsc[6] * ( rsc[0]*neighbors[8] +
352 rsc[1]*neighbors[9] +
353 rsc[2]*neighbors[10] +
354 rsc[3]*neighbors[11] ) +
355 rsc[7] * ( rsc[0]*neighbors[12] +
356 rsc[1]*neighbors[13] +
357 rsc[2]*neighbors[14] +
358 rsc[3]*neighbors[15] ) ;
361 podata[i+j*lx_out] = (pixelvalue)(cur/sumrs) ;
366 cpl_vector_delete(vx);
395 #define hk_gen(x,s) (((tanh(s*(x+0.5))+1)/2)*((tanh(s*(-x+0.5))+1)/2))
397 double * sinfo_new_generate_tanh_kernel(
double steep)
408 width = (double)TABSPERPIX / 2.0 ;
409 samples = KERNEL_SAMPLES ;
411 inv_np = 1.00 / (double)np ;
417 x = cpl_malloc((2*np+1)*
sizeof(
double)) ;
418 for (i=0 ; i<np/2 ; i++) {
419 ind = (double)i * 2.0 * width * inv_np ;
420 x[2*i] = hk_gen(ind, steep) ;
423 for (i=np/2 ; i<np ; i++) {
424 ind = (double)(i-np) * 2.0 * width * inv_np ;
425 x[2*i] = hk_gen(ind, steep) ;
432 new_reverse_tanh_kernel(x, np) ;
437 kernel = cpl_malloc(samples *
sizeof(
double)) ;
438 for (i=0 ; i<samples ; i++) {
439 kernel[i] = 2.0 * width * x[2*i] * inv_np ;
460 #define KERNEL_SW(a,b) tempr=(a);(a)=(b);(b)=tempr
461 static void new_reverse_tanh_kernel(
double * data,
int nn)
473 n = (
unsigned long)nn << 1;
475 for (i=1 ; i<n ; i+=2) {
477 KERNEL_SW(data[j-1],data[i-1]);
478 KERNEL_SW(data[j],data[i]);
481 while (m>=2 && j>m) {
489 unsigned long istep = mmax << 1;
490 double theta = 2 * PI_NUMB / mmax;
491 double wtemp = sin(0.5 * theta);
492 double wpr = -2.0 * wtemp * wtemp;
493 double wpi = sin(theta);
496 for (m=1 ; m<mmax ; m+=2) {
497 for (i=m ; i<=n ; i+=istep) {
499 tempr = wr * data[j-1] - wi * data[j];
500 tempi = wr * data[j] + wi * data[j-1];
501 data[j-1] = data[i-1] - tempr;
502 data[j] = data[i] - tempi;
506 wr = (wtemp = wr) * wpr - wi * wpi + wr;
507 wi = wi * wpr + wtemp * wpi + wi;
531 void sinfo_new_show_interpolation_kernel(
char * kernel_name)
538 ker = sinfo_new_generate_interpolation_kernel(kernel_name) ;
542 (void)fprintf(stdout,
"# Kernel is %s\n", kernel_name) ;
544 for (i=0 ; i<KERNEL_SAMPLES ; i++) {
545 (void)fprintf(stdout,
"%g %g\n", x, ker[i]) ;
546 x += 1.00 / (double)TABSPERPIX ;
#define sinfo_msg_error(...)
Print an error message.