| 1 | //interp.c |
| 2 | /* |
| 3 | * Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu |
| 4 | * This file is a part of the Frei0r plugin "c0rners" |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /******************************************************************* |
| 22 | * The remapping functions use a map array, which contains a pair |
| 23 | * of floating values fo each pixel of the output image. These |
| 24 | * represent the location in the input image, from where the value |
| 25 | * of the given output pixel should be interpolated. |
| 26 | * They are given in pixels of the input image. |
| 27 | * If the output image is wo pixels wide, then the x coordinate |
| 28 | * of the pixel in row r and column c is at 2*(r*wo+c) in the map |
| 29 | * array, and y at 2*(r*wo+c)+1 |
| 30 | * |
| 31 | * The map array is usually computation intensive to generate, and |
| 32 | * he purpose of the map array is to allow fast remapping of |
| 33 | * several images (video) using the same map array. |
| 34 | ******************************************************************/ |
| 35 | |
| 36 | |
| 37 | //compile: gcc -c -O2 -Wall -std=c99 -fPIC interp.c -o interp.o |
| 38 | |
| 39 | // -std=c99 za roundf() |
| 40 | // -fPIC da lahko linkas v .so (za frei0r) |
| 41 | |
| 42 | #include <math.h> |
| 43 | #include <stdio.h> /* za debug printoute */ |
| 44 | #include <inttypes.h> |
| 45 | |
| 46 | //-------------------------------------------------------- |
| 47 | //pointer to an interpolating function |
| 48 | typedef int (*interpp)(unsigned char*, int, int, float, float, unsigned char*); |
| 49 | |
| 50 | //************************************ |
| 51 | //REMAP AN IMAGE |
| 52 | |
| 53 | //-------------------------------------------------------- |
| 54 | // vhs = vhodna slika velikosti wi x hi |
| 55 | // izs = izhodna slika velikosti wo x ho |
| 56 | // map = za vsak pixel izs pove, kje ga vzamemo is vhs |
| 57 | // bgc = background color |
| 58 | // interp = kazalec na interpolacijsko funkcijo |
| 59 | void remap(int wi, int hi, int wo, int ho, unsigned char *vhs, unsigned char *izs, float *map, unsigned char bgc, interpp interp) |
| 60 | { |
| 61 | int i,j; |
| 62 | float x,y; |
| 63 | |
| 64 | for (i=0;i<ho;i++) |
| 65 | for (j=0;j<wo;j++) |
| 66 | { |
| 67 | x=map[2*(wo*i+j)]; |
| 68 | y=map[2*(wo*i+j)+1]; |
| 69 | if (x>0) |
| 70 | interp(vhs,wi,hi,x,y,&izs[wo*i+j]); |
| 71 | else |
| 72 | izs[wo*i+j]=bgc; //background fill |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | //-------------------------------------------------------- |
| 78 | //for four byte (int, 32 bit) values (packed RGB color) |
| 79 | //little endian !! |
| 80 | // vhs = vhodna slika velikosti wi x hi |
| 81 | // izs = izhodna slika velikosti wo x ho |
| 82 | // map = za vsak pixel izs pove, kje ga vzamemo is vhs |
| 83 | // bgc = background color |
| 84 | // interp = kazalec na interpolacijsko funkcijo |
| 85 | void remap32(int wi, int hi, int wo, int ho, unsigned char *vhs, unsigned char *izs, float *map, uint32_t bgc, interpp interp) |
| 86 | { |
| 87 | int i,j; |
| 88 | float x,y; |
| 89 | |
| 90 | for (i=0;i<ho;i++) |
| 91 | for (j=0;j<wo;j++) |
| 92 | { |
| 93 | x=map[2*(wo*i+j)]; |
| 94 | y=map[2*(wo*i+j)+1]; |
| 95 | if (x>0) |
| 96 | interp(vhs,wi,hi,x,y,&izs[4*(wo*i+j)]); |
| 97 | else //background fill |
| 98 | { |
| 99 | izs[4*(wo*i+j)]=bgc; |
| 100 | izs[4*(wo*i+j)+1]=bgc>>8; |
| 101 | izs[4*(wo*i+j)+2]=bgc>>16; |
| 102 | izs[4*(wo*i+j)+3]=bgc>>24; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | //************************************** |
| 108 | //HERE BEGIN THE INTERPOLATION FUNCTIONS |
| 109 | |
| 110 | |
| 111 | //------------------------------------------------------ |
| 112 | //za debugging - z izpisovanjem |
| 113 | //interpolacija "najblizji sosed" (ni prava interpolacija) |
| 114 | //za byte (char) vrednosti |
| 115 | // *sl vhodni array (slika) |
| 116 | // w,h dimenzija slike je wxh |
| 117 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 118 | // *v interpolirana vrednost |
| 119 | int interpNNpr_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 120 | { |
| 121 | //printf("u=%5.2f v=%5.2f ",x,y); |
| 122 | printf("u=%5.3f v=%5.3f ",x/(w-1),y/(h-1)); |
| 123 | //printf("U=%2d V=%2d ",(int)roundf(x),(int)roundf(y)); |
| 124 | |
| 125 | #ifdef TEST_XY_LIMITS |
| 126 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 127 | #endif |
| 128 | |
| 129 | *v=sl[(int)roundf(x)+(int)roundf(y)*w]; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | //------------------------------------------------------ |
| 134 | //interpolacija "najblizji sosed" (ni prava interpolacija) |
| 135 | //za byte (char) vrednosti |
| 136 | // *sl vhodni array (slika) |
| 137 | // w,h dimenzija slike je wxh |
| 138 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 139 | // *v interpolirana vrednost |
| 140 | int interpNN_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 141 | { |
| 142 | #ifdef TEST_XY_LIMITS |
| 143 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 144 | #endif |
| 145 | |
| 146 | *v=sl[(int)roundf(x)+(int)roundf(y)*w]; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | //------------------------------------------------------ |
| 151 | //interpolacija "najblizji sosed" (ni prava interpolacija) |
| 152 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 153 | //little endian !! |
| 154 | // *sl vhodni array (slika) |
| 155 | // w,h dimenzija slike je wxh |
| 156 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 157 | // *v interpolirana vrednost |
| 158 | int interpNN_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 159 | { |
| 160 | #ifdef TEST_XY_LIMITS |
| 161 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 162 | #endif |
| 163 | |
| 164 | v[0]=sl[(int)roundf(x)*4+(int)roundf(y)*4*w]; |
| 165 | v[1]=sl[(int)roundf(x)*4+(int)roundf(y)*4*w+1]; |
| 166 | v[2]=sl[(int)roundf(x)*4+(int)roundf(y)*4*w+2]; |
| 167 | v[3]=sl[(int)roundf(x)*4+(int)roundf(y)*4*w+3]; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | //------------------------------------------------------ |
| 173 | //bilinearna interpolacija |
| 174 | //za byte (char) vrednosti |
| 175 | // *sl vhodni array (slika) |
| 176 | // w,h dimenzija slike je wxh |
| 177 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 178 | // *v interpolirana vrednost |
| 179 | int interpBL_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 180 | { |
| 181 | int m,n,k,l; |
| 182 | float a,b; |
| 183 | |
| 184 | #ifdef TEST_XY_LIMITS |
| 185 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 186 | #endif |
| 187 | |
| 188 | m=(int)floorf(x); n=(int)floorf(y); |
| 189 | k=n*w+m; l=(n+1)*w+m; |
| 190 | a=sl[k]+(sl[k+1]-sl[k])*(x-(float)m); |
| 191 | b=sl[l]+(sl[l+1]-sl[l])*(x-(float)m); |
| 192 | *v=a+(b-a)*(y-(float)n); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | //------------------------------------------------------ |
| 197 | //bilinearna interpolacija |
| 198 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 199 | int interpBL_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 200 | { |
| 201 | int m,n,k,l,n1,l1,k1; |
| 202 | float a,b; |
| 203 | |
| 204 | #ifdef TEST_XY_LIMITS |
| 205 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 206 | #endif |
| 207 | |
| 208 | m=(int)floorf(x); n=(int)floorf(y); |
| 209 | k=n*w+m; l=(n+1)*w+m; |
| 210 | k1=4*(k+1); l1=4*(l+1); n1=4*((n+1)*w+m); |
| 211 | l=4*l; k=4*k; |
| 212 | |
| 213 | a=sl[k]+(sl[k1]-sl[k])*(x-(float)m); |
| 214 | b=sl[l]+(sl[l1]-sl[n1])*(x-(float)m); |
| 215 | v[0]=a+(b-a)*(y-(float)n); |
| 216 | |
| 217 | a=sl[k+1]+(sl[k1+1]-sl[k+1])*(x-(float)m); |
| 218 | b=sl[l+1]+(sl[l1+1]-sl[n1+1])*(x-(float)m); |
| 219 | v[1]=a+(b-a)*(y-(float)n); |
| 220 | |
| 221 | a=sl[k+2]+(sl[k1+2]-sl[k+2])*(x-(float)m); |
| 222 | b=sl[l+2]+(sl[l1+2]-sl[n1+2])*(x-(float)m); |
| 223 | v[2]=a+(b-a)*(y-(float)n); |
| 224 | |
| 225 | a=sl[k+3]+(sl[k1+3]-sl[k+3])*(x-(float)m); |
| 226 | b=sl[l+3]+(sl[l1+3]-sl[n1+3])*(x-(float)m); |
| 227 | v[3]=a+(b-a)*(y-(float)n); |
| 228 | |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | //------------------------------------------------------ |
| 234 | //bikubicna interpolacija "smooth" |
| 235 | //za byte (char) vrednosti |
| 236 | //kar Aitken-Neville formula iz Bronstajna |
| 237 | // *sl vhodni array (slika) |
| 238 | // w,h dimenzija slike je wxh |
| 239 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 240 | // *v interpolirana vrednost |
| 241 | int interpBC_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 242 | { |
| 243 | int i,j,l,m,n; |
| 244 | float k; |
| 245 | float p[4],p1[4],p2[4],p3[4],p4[4]; |
| 246 | |
| 247 | #ifdef TEST_XY_LIMITS |
| 248 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 249 | #endif |
| 250 | |
| 251 | m=(int)ceilf(x)-2; if (m<0) m=0; if ((m+5)>w) m=w-4; |
| 252 | n=(int)ceilf(y)-2; if (n<0) n=0; if ((n+5)>h) n=h-4; |
| 253 | |
| 254 | //njaprej po y (stiri stolpce) |
| 255 | for (i=0;i<4;i++) |
| 256 | { |
| 257 | l=m+(i+n)*w; |
| 258 | p1[i]=sl[l]; |
| 259 | p2[i]=sl[l+1]; |
| 260 | p3[i]=sl[l+2]; |
| 261 | p4[i]=sl[l+3]; |
| 262 | } |
| 263 | for (j=1;j<4;j++) |
| 264 | for (i=3;i>=j;i--) |
| 265 | { |
| 266 | k=(y-i-n)/j; |
| 267 | p1[i]=p1[i]+k*(p1[i]-p1[i-1]); |
| 268 | p2[i]=p2[i]+k*(p2[i]-p2[i-1]); |
| 269 | p3[i]=p3[i]+k*(p3[i]-p3[i-1]); |
| 270 | p4[i]=p4[i]+k*(p4[i]-p4[i-1]); |
| 271 | } |
| 272 | |
| 273 | //zdaj pa po x |
| 274 | p[0]=p1[3]; p[1]=p2[3]; p[2]=p3[3]; p[3]=p4[3]; |
| 275 | for (j=1;j<4;j++) |
| 276 | for (i=3;i>=j;i--) |
| 277 | p[i]=p[i]+(x-i-m)/j*(p[i]-p[i-1]); |
| 278 | |
| 279 | if (p[3]<0.0) p[3]=0.0; //printf("p=%f ",p[3]); |
| 280 | if (p[3]>256.0) p[3]=255.0; //printf("p=%f ",p[3]); |
| 281 | |
| 282 | *v=p[3]; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | //------------------------------------------------------ |
| 288 | //bikubicna interpolacija "smooth" |
| 289 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 290 | int interpBC_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 291 | { |
| 292 | int i,j,b,l,m,n; |
| 293 | float k; |
| 294 | float p[4],p1[4],p2[4],p3[4],p4[4]; |
| 295 | |
| 296 | #ifdef TEST_XY_LIMITS |
| 297 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 298 | #endif |
| 299 | |
| 300 | m=(int)ceilf(x)-2; if (m<0) m=0; if ((m+5)>w) m=w-4; |
| 301 | n=(int)ceilf(y)-2; if (n<0) n=0; if ((n+5)>h) n=h-4; |
| 302 | |
| 303 | |
| 304 | for (b=0;b<4;b++) |
| 305 | { |
| 306 | //njaprej po y (stiri stolpce) |
| 307 | for (i=0;i<4;i++) |
| 308 | { |
| 309 | l=m+(i+n)*w; |
| 310 | p1[i]=sl[4*l+b]; |
| 311 | p2[i]=sl[4*(l+1)+b]; |
| 312 | p3[i]=sl[4*(l+2)+b]; |
| 313 | p4[i]=sl[4*(l+3)+b]; |
| 314 | } |
| 315 | for (j=1;j<4;j++) |
| 316 | for (i=3;i>=j;i--) |
| 317 | { |
| 318 | k=(y-i-n)/j; |
| 319 | p1[i]=p1[i]+k*(p1[i]-p1[i-1]); |
| 320 | p2[i]=p2[i]+k*(p2[i]-p2[i-1]); |
| 321 | p3[i]=p3[i]+k*(p3[i]-p3[i-1]); |
| 322 | p4[i]=p4[i]+k*(p4[i]-p4[i-1]); |
| 323 | } |
| 324 | |
| 325 | //zdaj pa po x |
| 326 | p[0]=p1[3]; p[1]=p2[3]; p[2]=p3[3]; p[3]=p4[3]; |
| 327 | for (j=1;j<4;j++) |
| 328 | for (i=3;i>=j;i--) |
| 329 | p[i]=p[i]+(x-i-m)/j*(p[i]-p[i-1]); |
| 330 | |
| 331 | if (p[3]<0.0) p[3]=0.0; |
| 332 | if (p[3]>256.0) p[3]=255.0; |
| 333 | |
| 334 | v[b]=p[3]; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | //------------------------------------------------------ |
| 341 | //bikubicna interpolacija "sharp" |
| 342 | //za byte (char) vrednosti |
| 343 | //Helmut Dersch polinom |
| 344 | // *sl vhodni array (slika) |
| 345 | // w,h dimenzija slike je wxh |
| 346 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 347 | // *v interpolirana vrednost |
| 348 | //!!! ODKOD SUM??? (ze po eni rotaciji v interp_test !!) |
| 349 | //!!! v defish tega suma ni??? |
| 350 | int interpBC2_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 351 | { |
| 352 | int i,k,l,m,n; |
| 353 | float pp,p[4],wx[4],wy[4],xx; |
| 354 | |
| 355 | #ifdef TEST_XY_LIMITS |
| 356 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 357 | #endif |
| 358 | |
| 359 | m=(int)ceilf(x)-2; if (m<0) m=0; if ((m+5)>w) m=w-4; |
| 360 | n=(int)ceilf(y)-2; if (n<0) n=0; if ((n+5)>h) n=h-4; |
| 361 | |
| 362 | |
| 363 | //najprej po y (stiri stolpce) |
| 364 | xx=y-n; wy[0]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 365 | xx=xx-1.0; wy[1]=(1.25*xx-2.25)*xx*xx+1.0; |
| 366 | xx=1.0-xx; wy[2]=(1.25*xx-2.25)*xx*xx+1.0; |
| 367 | xx=xx+1.0; wy[3]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 368 | //se po x |
| 369 | xx=x-m; wx[0]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 370 | xx=xx-1.0; wx[1]=(1.25*xx-2.25)*xx*xx+1.0; |
| 371 | xx=1.0-xx; wx[2]=(1.25*xx-2.25)*xx*xx+1.0; |
| 372 | xx=xx+1.0; wx[3]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 373 | |
| 374 | k=n*w+m; |
| 375 | for (i=0;i<4;i++) |
| 376 | { |
| 377 | p[i]=0.0; |
| 378 | l=k+i; |
| 379 | p[i]=wy[0]*sl[l]; l+=w; |
| 380 | p[i]+=wy[1]*sl[l]; l+=w; |
| 381 | p[i]+=wy[2]*sl[l]; l+=w; |
| 382 | p[i]+=wy[3]*sl[l]; |
| 383 | } |
| 384 | |
| 385 | pp=wx[0]*p[0]; |
| 386 | pp+=wx[1]*p[1]; |
| 387 | pp+=wx[2]*p[2]; |
| 388 | pp+=wx[3]*p[3]; |
| 389 | |
| 390 | if (pp<0.0) pp=0.0; |
| 391 | if (pp>256.0) pp=255.0; |
| 392 | |
| 393 | *v=pp; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | //------------------------------------------------------ |
| 398 | //bikubicna interpolacija "sharp" |
| 399 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 400 | //!!! ODKOD SUM??? (ze po eni rotaciji v interp_test !!) |
| 401 | //!!! v defish tega suma ni??? |
| 402 | int interpBC2_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 403 | { |
| 404 | int b,i,k,l,m,n,u; |
| 405 | float pp,p[4],wx[4],wy[4],xx; |
| 406 | |
| 407 | #ifdef TEST_XY_LIMITS |
| 408 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 409 | #endif |
| 410 | |
| 411 | m=(int)ceilf(x)-2; if (m<0) m=0; if ((m+5)>w) m=w-4; |
| 412 | n=(int)ceilf(y)-2; if (n<0) n=0; if ((n+5)>h) n=h-4; |
| 413 | |
| 414 | //najprej po y (stiri stolpce) |
| 415 | xx=y-n; wy[0]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 416 | xx=xx-1.0; wy[1]=(1.25*xx-2.25)*xx*xx+1.0; |
| 417 | xx=1.0-xx; wy[2]=(1.25*xx-2.25)*xx*xx+1.0; |
| 418 | xx=xx+1.0; wy[3]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 419 | //se po x |
| 420 | xx=x-m; wx[0]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 421 | xx=xx-1.0; wx[1]=(1.25*xx-2.25)*xx*xx+1.0; |
| 422 | xx=1.0-xx; wx[2]=(1.25*xx-2.25)*xx*xx+1.0; |
| 423 | xx=xx+1.0; wx[3]=(-0.75*(xx-5.0)*xx-6.0)*xx+3.0; |
| 424 | |
| 425 | k=4*(n*w+m); u=4*w; |
| 426 | for (b=0;b<4;b++) |
| 427 | { |
| 428 | for (i=0;i<4;i++) |
| 429 | { |
| 430 | p[i]=0.0; |
| 431 | l=k+4*i; |
| 432 | p[i]=wy[0]*sl[l]; l+=u; |
| 433 | p[i]+=wy[1]*sl[l]; l+=u; |
| 434 | p[i]+=wy[2]*sl[l]; l+=u; |
| 435 | p[i]+=wy[3]*sl[l]; |
| 436 | } |
| 437 | k++; |
| 438 | |
| 439 | pp=wx[0]*p[0]; |
| 440 | pp+=wx[1]*p[1]; |
| 441 | pp+=wx[2]*p[2]; |
| 442 | pp+=wx[3]*p[3]; |
| 443 | |
| 444 | if (pp<0.0) pp=0.0; |
| 445 | if (pp>256.0) pp=255.0; |
| 446 | |
| 447 | v[b]=pp; |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | //------------------------------------------------------ |
| 454 | //spline 4x4 interpolacija |
| 455 | //za byte (char) vrednosti |
| 456 | //Helmut Dersch polinom |
| 457 | // *sl vhodni array (slika) |
| 458 | // w,h dimenzija slike je wxh |
| 459 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 460 | // *v interpolirana vrednost |
| 461 | int interpSP4_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 462 | { |
| 463 | int i,j,m,n; |
| 464 | float pp,p[4],wx[4],wy[4],xx; |
| 465 | |
| 466 | #ifdef TEST_XY_LIMITS |
| 467 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 468 | #endif |
| 469 | |
| 470 | m=(int)ceilf(x)-2; if (m<0) m=0; if ((m+5)>w) m=w-4; |
| 471 | n=(int)ceilf(y)-2; if (n<0) n=0; if ((n+5)>h) n=h-4; |
| 472 | |
| 473 | //najprej po y (stiri stolpce) |
| 474 | xx=y-n; wy[0]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 475 | xx=xx-1.0; wy[1]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 476 | xx=1.0-xx; wy[2]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 477 | xx=xx+1.0; wy[3]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 478 | //se po x |
| 479 | xx=x-m; wx[0]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 480 | xx=xx-1.0; wx[1]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 481 | xx=1.0-xx; wx[2]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 482 | xx=xx+1.0; wx[3]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 483 | |
| 484 | for (i=0;i<4;i++) |
| 485 | { |
| 486 | p[i]=0.0; |
| 487 | for (j=0;j<4;j++) |
| 488 | { |
| 489 | p[i]=p[i]+wy[j]*sl[(j+n)*w+i+m]; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | pp=0.0; |
| 494 | for (i=0;i<4;i++) |
| 495 | pp=pp+wx[i]*p[i]; |
| 496 | |
| 497 | if (pp<0.0) pp=0.0; |
| 498 | if (pp>256.0) pp=255.0; |
| 499 | |
| 500 | *v=pp; |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | //------------------------------------------------------ |
| 505 | //spline 4x4 interpolacija |
| 506 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 507 | int interpSP4_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 508 | { |
| 509 | int i,j,m,n,b; |
| 510 | float pp,p[4],wx[4],wy[4],xx; |
| 511 | |
| 512 | #ifdef TEST_XY_LIMITS |
| 513 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 514 | #endif |
| 515 | |
| 516 | m=(int)ceilf(x)-2; if (m<0) m=0; if ((m+5)>w) m=w-4; |
| 517 | n=(int)ceilf(y)-2; if (n<0) n=0; if ((n+5)>h) n=h-4; |
| 518 | |
| 519 | //najprej po y (stiri stolpce) |
| 520 | xx=y-n; wy[0]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 521 | xx=xx-1.0; wy[1]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 522 | xx=1.0-xx; wy[2]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 523 | xx=xx+1.0; wy[3]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 524 | //se po x |
| 525 | xx=x-m; wx[0]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 526 | xx=xx-1.0; wx[1]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 527 | xx=1.0-xx; wx[2]=((xx-1.8)*xx-0.2)*xx+1.0; |
| 528 | xx=xx+1.0; wx[3]=((-0.333333*(xx-1.0)+0.8)*(xx-1.0)-0.466667)*(xx-1.0); |
| 529 | |
| 530 | for (b=0;b<4;b++) |
| 531 | { |
| 532 | for (i=0;i<4;i++) |
| 533 | { |
| 534 | p[i]=0.0; |
| 535 | for (j=0;j<4;j++) |
| 536 | { |
| 537 | p[i]=p[i]+wy[j]*sl[4*((j+n)*w+i+m)+b]; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | pp=0.0; |
| 542 | for (i=0;i<4;i++) |
| 543 | pp=pp+wx[i]*p[i]; |
| 544 | |
| 545 | if (pp<0.0) pp=0.0; |
| 546 | if (pp>256.0) pp=255.0; |
| 547 | |
| 548 | v[b]=pp; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | //------------------------------------------------------ |
| 555 | //spline 6x6 interpolacija |
| 556 | //za byte (char) vrednosti |
| 557 | //Helmut Dersch polinom |
| 558 | // *sl vhodni array (slika) |
| 559 | // w,h dimenzija slike je wxh |
| 560 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 561 | // *v interpolirana vrednost |
| 562 | //!!! PAZI, TOLE NE DELA CISTO PRAV ??? belina se siri |
| 563 | //!!! zaenkrat sem dodal fudge factor... |
| 564 | int interpSP6_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 565 | { |
| 566 | int i,j,m,n; |
| 567 | float pp,p[6],wx[6],wy[6],xx; |
| 568 | |
| 569 | #ifdef TEST_XY_LIMITS |
| 570 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 571 | #endif |
| 572 | |
| 573 | m=(int)ceilf(x)-3; if (m<0) m=0; if ((m+7)>w) m=w-6; |
| 574 | n=(int)ceilf(y)-3; if (n<0) n=0; if ((n+7)>h) n=h-6; |
| 575 | |
| 576 | //najprej po y (sest stolpcev) |
| 577 | xx=y-n; |
| 578 | wy[0]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 579 | xx=xx-1.0; |
| 580 | wy[1]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 581 | xx=xx-1.0; |
| 582 | wy[2]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 583 | xx=1.0-xx; |
| 584 | wy[3]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 585 | xx=xx+1.0; |
| 586 | wy[4]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 587 | xx=xx+1.0; |
| 588 | wy[5]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 589 | //se po x |
| 590 | xx=x-m; |
| 591 | wx[0]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 592 | xx=xx-1.0; |
| 593 | wx[1]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 594 | xx=xx-1.0; |
| 595 | wx[2]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 596 | xx=1.0-xx; |
| 597 | wx[3]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 598 | xx=xx+1.0; |
| 599 | wx[4]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 600 | xx=xx+1.0; |
| 601 | wx[5]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 602 | |
| 603 | |
| 604 | for (i=0;i<6;i++) |
| 605 | { |
| 606 | p[i]=0.0; |
| 607 | for (j=0;j<6;j++) |
| 608 | { |
| 609 | p[i]=p[i]+wy[j]*sl[(j+n)*w+i+m]; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | pp=0.0; |
| 614 | for (i=0;i<6;i++) |
| 615 | pp=pp+wx[i]*p[i]; |
| 616 | |
| 617 | pp=0.947*pp; //fudge factor...!!! cca 0.947... |
| 618 | if (pp<0.0) pp=0.0; |
| 619 | if (pp>256.0) pp=255.0; |
| 620 | |
| 621 | *v=pp; |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | //------------------------------------------------------ |
| 626 | //spline 6x6 interpolacija |
| 627 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 628 | //!!! PAZI, TOLE NE DELA CISTO PRAV ??? belina se siri |
| 629 | //!!! zaenkrat sem dodal fudge factor... |
| 630 | int interpSP6_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 631 | { |
| 632 | int i,b,j,m,n; |
| 633 | float pp,p[6],wx[6],wy[6],xx; |
| 634 | |
| 635 | #ifdef TEST_XY_LIMITS |
| 636 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 637 | #endif |
| 638 | |
| 639 | m=(int)ceilf(x)-3; if (m<0) m=0; if ((m+7)>w) m=w-6; |
| 640 | n=(int)ceilf(y)-3; if (n<0) n=0; if ((n+7)>h) n=h-6; |
| 641 | |
| 642 | //najprej po y (sest stolpcev) |
| 643 | xx=y-n; |
| 644 | wy[0]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 645 | xx=xx-1.0; |
| 646 | wy[1]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 647 | xx=xx-1.0; |
| 648 | wy[2]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 649 | xx=1.0-xx; |
| 650 | wy[3]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 651 | xx=xx+1.0; |
| 652 | wy[4]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 653 | xx=xx+1.0; |
| 654 | wy[5]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 655 | //se po x |
| 656 | xx=x-m; |
| 657 | wx[0]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 658 | xx=xx-1.0; |
| 659 | wx[1]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 660 | xx=xx-1.0; |
| 661 | wx[2]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 662 | xx=1.0-xx; |
| 663 | wx[3]=((1.181818*xx-2.167464)*xx+0.014354)*xx+1.0; |
| 664 | xx=xx+1.0; |
| 665 | wx[4]=((-0.545455*(xx-1.0)+1.291866)*(xx-1.0)-0.746411)*(xx-1.0); |
| 666 | xx=xx+1.0; |
| 667 | wx[5]=((0.090909*(xx-2.0)-0.215311)*(xx-2.0)+0.124402)*(xx-2.0); |
| 668 | |
| 669 | |
| 670 | for (b=0;b<4;b++) |
| 671 | { |
| 672 | for (i=0;i<6;i++) |
| 673 | { |
| 674 | p[i]=0.0; |
| 675 | for (j=0;j<6;j++) |
| 676 | { |
| 677 | p[i]=p[i]+wy[j]*sl[4*((j+n)*w+i+m)+b]; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | pp=0.0; |
| 682 | for (i=0;i<6;i++) |
| 683 | pp=pp+wx[i]*p[i]; |
| 684 | |
| 685 | pp=0.947*pp; //fudge factor...!!! cca 0.947... |
| 686 | if (pp<0.0) pp=0.0; |
| 687 | if (pp>256.0) pp=255.0; |
| 688 | |
| 689 | v[b]=pp; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | //------------------------------------------------------ |
| 696 | //truncated sinc "lanczos" 16x16 interpolacija |
| 697 | //za byte (char) vrednosti |
| 698 | // *sl vhodni array (slika) |
| 699 | // w,h dimenzija slike je wxh |
| 700 | // x,y tocka, za katero izracuna interpolirano vrednost |
| 701 | // *v interpolirana vrednost |
| 702 | int interpSC16_b(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 703 | { |
| 704 | int i,j,m,n; |
| 705 | float pp,p[16],wx[16],wy[16],xx,xxx,x1; |
| 706 | float PI=3.141592654; |
| 707 | |
| 708 | #ifdef TEST_XY_LIMITS |
| 709 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 710 | #endif |
| 711 | |
| 712 | m=(int)ceilf(x)-8; if (m<0) m=0; if ((m+17)>w) m=w-16; |
| 713 | n=(int)ceilf(y)-8; if (n<0) n=0; if ((n+17)>h) n=h-16; |
| 714 | |
| 715 | //najprej po y |
| 716 | xx=y-n; |
| 717 | for (i=7;i>=0;i--) |
| 718 | { |
| 719 | x1=xx*PI; |
| 720 | wy[7-i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 721 | xxx=(float)(2*i+1)-xx; |
| 722 | x1=xxx*PI; |
| 723 | wy[8+i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 724 | xx=xx-1.0; |
| 725 | } |
| 726 | //se po x |
| 727 | xx=x-m; |
| 728 | for (i=7;i>=0;i--) |
| 729 | { |
| 730 | x1=xx*PI; |
| 731 | wx[7-i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 732 | xxx=(float)(2*i+1)-xx; |
| 733 | x1=xxx*PI; |
| 734 | wx[8+i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 735 | xx=xx-1.0; |
| 736 | } |
| 737 | |
| 738 | for (i=0;i<16;i++) |
| 739 | { |
| 740 | p[i]=0.0; |
| 741 | for (j=0;j<16;j++) |
| 742 | { |
| 743 | p[i]=p[i]+wy[j]*sl[(j+n)*w+i+m]; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | pp=0.0; |
| 748 | for (i=0;i<16;i++) |
| 749 | pp=pp+wx[i]*p[i]; |
| 750 | |
| 751 | if (pp<0.0) pp=0.0; |
| 752 | if (pp>256.0) pp=255.0; |
| 753 | |
| 754 | *v=pp; |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | //------------------------------------------------------ |
| 759 | //truncated sinc "lanczos" 16x16 interpolacija |
| 760 | //za byte (char) vrednosti v packed color 32 bitnem formatu |
| 761 | int interpSC16_b32(unsigned char *sl, int w, int h, float x, float y, unsigned char *v) |
| 762 | { |
| 763 | int i,j,m,b,n; |
| 764 | float pp,p[16],wx[16],wy[16],xx,xxx,x1; |
| 765 | float PI=3.141592654; |
| 766 | |
| 767 | #ifdef TEST_XY_LIMITS |
| 768 | if ((x<0)||(x>w)||(y<0)||(y>h)) return -1; |
| 769 | #endif |
| 770 | |
| 771 | m=(int)ceilf(x)-8; if (m<0) m=0; if ((m+17)>w) m=w-16; |
| 772 | n=(int)ceilf(y)-8; if (n<0) n=0; if ((n+17)>h) n=h-16; |
| 773 | |
| 774 | //najprej po y |
| 775 | xx=y-n; |
| 776 | for (i=7;i>=0;i--) |
| 777 | { |
| 778 | x1=xx*PI; |
| 779 | wy[7-i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 780 | xxx=(float)(2*i+1)-xx; |
| 781 | x1=xxx*PI; |
| 782 | wy[8+i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 783 | xx=xx-1.0; |
| 784 | } |
| 785 | //se po x |
| 786 | xx=x-m; |
| 787 | for (i=7;i>=0;i--) |
| 788 | { |
| 789 | x1=xx*PI; |
| 790 | wx[7-i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 791 | xxx=(float)(2*i+1)-xx; |
| 792 | x1=xxx*PI; |
| 793 | wx[8+i]=(x1!=0)?(sin(x1)/(x1))*(sin(x1*0.125)/(x1*0.125)):1.0; |
| 794 | xx=xx-1.0; |
| 795 | } |
| 796 | |
| 797 | for (b=0;b<4;b++) |
| 798 | { |
| 799 | for (i=0;i<16;i++) |
| 800 | { |
| 801 | p[i]=0.0; |
| 802 | for (j=0;j<16;j++) |
| 803 | { |
| 804 | p[i]=p[i]+wy[j]*sl[4*((j+n)*w+i+m)+b]; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | pp=0.0; |
| 809 | for (i=0;i<16;i++) |
| 810 | pp=pp+wx[i]*p[i]; |
| 811 | |
| 812 | if (pp<0.0) pp=0.0; |
| 813 | if (pp>256.0) pp=255.0; |
| 814 | |
| 815 | v[b]=pp; |
| 816 | } |
| 817 | |
| 818 | return 0; |
| 819 | } |