| 1 |
#include <config.h> |
| 2 |
|
| 3 |
#include <string.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <math.h> |
| 6 |
|
| 7 |
#include "renderer.h" |
| 8 |
#include "display.h" |
| 9 |
#include "main.h" |
| 10 |
|
| 11 |
#define wrap(a) ( a < 0 ? 0 : ( a > 255 ? 255 : a )) |
| 12 |
#define assign_max(p,a) ( *p = ( *p > a ? *p : a )) |
| 13 |
#define PI 3.14159 |
| 14 |
|
| 15 |
void _inf_generate_colors(InfinitePrivate *priv) |
| 16 |
{ |
| 17 |
int i,k; |
| 18 |
float colors[NB_PALETTES][2][3]={{{1,1,1},{1,1,1}}, |
| 19 |
{{2,1.5,0},{0,0.5,2}}, |
| 20 |
{{0,1,2},{0,1,0}}, |
| 21 |
{{0,2,1},{0,0,1}}, |
| 22 |
{{2,0,0},{0,1,1}}}; |
| 23 |
|
| 24 |
for (k=0;k<NB_PALETTES;k++) { |
| 25 |
for ( i=0; i<128; i++ ) |
| 26 |
{ |
| 27 |
priv->color_table[k][i].r = colors[k][0][0]*i; |
| 28 |
priv->color_table[k][i].g = colors[k][0][1]*i; |
| 29 |
priv->color_table[k][i].b = colors[k][0][2]*i; |
| 30 |
} |
| 31 |
for ( i=0; i<128; i++ ) |
| 32 |
{ |
| 33 |
priv->color_table[k][i+128].r = colors[k][0][0]*127+colors[k][1][0]*i; |
| 34 |
priv->color_table[k][i+128].g = colors[k][0][1]*127+colors[k][1][1]*i; |
| 35 |
priv->color_table[k][i+128].b = colors[k][0][2]*127+colors[k][1][2]*i; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
void _inf_change_color(InfinitePrivate *priv, int t2,int t1,int w) |
| 41 |
{ |
| 42 |
int i; |
| 43 |
|
| 44 |
for (i=0;i<256;i++) { |
| 45 |
int r,g,b; |
| 46 |
r=(priv->color_table[t1][i].r*w+priv->color_table[t2][i].r*(256-w))>>8; |
| 47 |
g=(priv->color_table[t1][i].g*w+priv->color_table[t2][i].g*(256-w))>>8; |
| 48 |
b=(priv->color_table[t1][i].b*w+priv->color_table[t2][i].b*(256-w))>>8; |
| 49 |
|
| 50 |
priv->pal.colors[i].r = r; |
| 51 |
priv->pal.colors[i].g = g; |
| 52 |
priv->pal.colors[i].b = b; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
void _inf_compute_surface(InfinitePrivate *priv, t_interpol* vector_field) |
| 57 |
{ |
| 58 |
int i,j; |
| 59 |
int add_dest=0,add_src; |
| 60 |
t_interpol *interpol; |
| 61 |
register uint8_t* ptr_pix; |
| 62 |
int color = 0; |
| 63 |
uint8_t* ptr_swap; |
| 64 |
|
| 65 |
/* FIXME OK we're going to take a different approach: |
| 66 |
* have a whole set of interpolate buffers: |
| 67 |
* every buffer containing one element, the different weights, the upper and lower part of |
| 68 |
* the interpolation coords etc, with this setup it's MUCH easier to vectorize it damn hard. |
| 69 |
*/ |
| 70 |
|
| 71 |
/* FIXME ok this sucked, is een mislukte actie geweest zie ik, omdat ik mmx regs te kort kwam |
| 72 |
* zat ik met restore problemen, waardoor het uiteindelijk even snel werd, moet de hele blur routine |
| 73 |
* van deze plugin from scratch doen... */ |
| 74 |
if (0) { |
| 75 |
#if 0 |
| 76 |
#if defined(VISUAL_ARCH_X86) || defined(VISUAL_ARCH_X86_64) |
| 77 |
int add_src2[2]; |
| 78 |
uint8_t *ptr_pix2[2]; |
| 79 |
int plugwidth = priv->plugwidth; |
| 80 |
uint8_t *ptr_surf = priv->surface1; |
| 81 |
int aflag = 0xffff; |
| 82 |
|
| 83 |
/* Prep general used regs, mm6 as empty (un)packer, mm7 to contain plugwidth for packed words |
| 84 |
* and we use mm5 to contain 0x0000ffff0000ffff */ |
| 85 |
__asm __volatile |
| 86 |
("\n\t movd %[plugw], %%mm6" |
| 87 |
"\n\t movd %[plugw], %%mm7" |
| 88 |
"\n\t movd %[aflag], %%mm5" |
| 89 |
"\n\t movd %[aflag], %%mm4" |
| 90 |
|
| 91 |
"\n\t psllq $32, %%mm6" |
| 92 |
"\n\t por %%mm6, %%mm7" |
| 93 |
|
| 94 |
"\n\t psllq $32, %%mm4" |
| 95 |
"\n\t por %%mm4, %%mm5" |
| 96 |
|
| 97 |
"\n\t pxor %%mm6, %%mm6" |
| 98 |
"\n\t pxor %%mm4, %%mm4" |
| 99 |
:: [plugw] "m" (plugwidth) |
| 100 |
, [aflag] "m" (aflag)); |
| 101 |
|
| 102 |
for (j = 0; j < priv->plugheight; j++) { |
| 103 |
for (i = 0; i < priv->plugwidth; i += 2) { |
| 104 |
int coords[2]; |
| 105 |
|
| 106 |
interpol = (vector_field += 2); // load 4 from. |
| 107 |
|
| 108 |
coords[0] = interpol[0].coord; |
| 109 |
coords[1] = interpol[1].coord; |
| 110 |
|
| 111 |
// return; |
| 112 |
// add_src = (interpol->coord & 0xFFFF) * priv->plugwidth + (interpol->coord >> 16); // pack and do in mmx! |
| 113 |
|
| 114 |
// ptr_pix = priv->surface1 + add_src; // pack and do in mmx! |
| 115 |
|
| 116 |
/* Do in two pieces! (first coords 0, 1, then 2, 3) */ |
| 117 |
__asm __volatile ( |
| 118 |
"\n\t movq %[interpol], %%mm0" /* Start loading in the babies */ |
| 119 |
"\n\t movq %[interpol], %%mm1" |
| 120 |
|
| 121 |
"\n\t psrld $16, %%mm0" /* the >> 16 coord elems */ |
| 122 |
"\n\t pand %%mm5, %%mm1" /* 0xffff coord elems */ |
| 123 |
"\n\t pmullw %%mm7, %%mm1" /* multiply with plugwidth */ |
| 124 |
"\n\t paddd %%mm0, %%mm1" /* Ading >> 16 coords. mm1 now contains the right addsrc offset */ |
| 125 |
|
| 126 |
"\n\t movd %[surf1], %%mm0" |
| 127 |
"\n\t movd %[surf1], %%mm4" |
| 128 |
"\n\t psllq $32, %%mm0" |
| 129 |
"\n\t por %%mm4, %%mm0" /* Contains the ptr_surf 2 times */ |
| 130 |
"\n\t paddd %%mm1, %%mm0" /* Now contains the right ptr_pix2 value */ |
| 131 |
|
| 132 |
"\n\t movq %%mm0, %[pix]" |
| 133 |
:: [interpol] "m" (*(coords)) |
| 134 |
, [addsrc2] "m" (add_src2) |
| 135 |
, [surf1] "m" (ptr_surf) |
| 136 |
, [pix] "m" (*ptr_pix2)); |
| 137 |
// printf ("%x %x\n", ptr_pix2[0], ptr_pix2[1]); |
| 138 |
// printf ("%p %p\n", ptr_pix2[0], ptr_pix2[1]); |
| 139 |
int color1 = 0, color2 = 0; |
| 140 |
/* int color1 = (*(ptr_pix2[0]) |
| 141 |
+ *(ptr_pix2[0] + 1) |
| 142 |
+ *(ptr_pix2[0] + priv->plugwidth) |
| 143 |
+ *(ptr_pix2[0] + priv->plugwidth + 1)); |
| 144 |
|
| 145 |
int color2 = (*(ptr_pix2[1]) |
| 146 |
+ *(ptr_pix2[1] + 1) |
| 147 |
+ *(ptr_pix2[1] + priv->plugwidth) |
| 148 |
+ *(ptr_pix2[1] + priv->plugwidth + 1)); |
| 149 |
*/ |
| 150 |
priv->surface2[add_dest] = color1; |
| 151 |
priv->surface2[add_dest + 1] = color2; |
| 152 |
|
| 153 |
add_dest += 2; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
__asm __volatile |
| 158 |
("emms"); |
| 159 |
#endif /* #if defined(VISUAL_ARCH_X86) || defined(VISUAL_ARCH_X86_64) */ |
| 160 |
#endif /* #if 0 */ |
| 161 |
} else { |
| 162 |
const uint8_t *ptr_pix_end = priv->surface1 + (priv->plugwidth * priv->plugheight); |
| 163 |
|
| 164 |
for (j=0;j<priv->plugheight;j++) { |
| 165 |
for (i=0;i<priv->plugwidth;i++) { |
| 166 |
interpol = &vector_field[add_dest]; |
| 167 |
const uint16_t y = interpol->coord & 0xffff; |
| 168 |
const uint16_t x = interpol->coord >> 16; |
| 169 |
ptr_pix = priv->surface1 + y * priv->plugwidth + x; |
| 170 |
|
| 171 |
color = ptr_pix[0] * (interpol->weight >> 24); |
| 172 |
|
| 173 |
// right neigbor |
| 174 |
if (ptr_pix + 1 < ptr_pix_end) |
| 175 |
color += ptr_pix[1] * ((interpol->weight >> 16) & 0xFF); |
| 176 |
|
| 177 |
// bottom neigbor |
| 178 |
if (ptr_pix + priv->plugwidth < ptr_pix_end) |
| 179 |
color += ptr_pix[priv->plugwidth] * ((interpol->weight >> 8) & 0xFF); |
| 180 |
|
| 181 |
// bottom right neigbor |
| 182 |
if (ptr_pix + priv->plugwidth + 1 < ptr_pix_end) |
| 183 |
color += ptr_pix[priv->plugwidth + 1] * (interpol->weight & 0xFF); |
| 184 |
|
| 185 |
color >>= 8; |
| 186 |
/* |
| 187 |
color= (*(ptr_pix) // * (interpol->weight >> 24) |
| 188 |
+*(ptr_pix + 1) // * ((interpol->weight & 0xFFFFFF) >> 16) |
| 189 |
+*(ptr_pix + priv->plugwidth) // * ((interpol->weight & 0xFFFF) >> 8) |
| 190 |
+*(ptr_pix + priv->plugwidth + 1)); // * (interpol->weight & 0xFF)) >> 8; |
| 191 |
|
| 192 |
*/ priv->surface2[add_dest]=color; |
| 193 |
add_dest++; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
ptr_swap=priv->surface1; |
| 200 |
priv->surface1=priv->surface2; |
| 201 |
priv->surface2=ptr_swap; |
| 202 |
} |
| 203 |
|
| 204 |
void _inf_display (InfinitePrivate *priv, uint8_t *surf, int pitch) |
| 205 |
{ |
| 206 |
int i; |
| 207 |
|
| 208 |
for (i = 0; i < priv->plugheight; i++) { |
| 209 |
|
| 210 |
visual_mem_copy (surf, priv->surface1 + (i * priv->plugwidth), priv->plugwidth); |
| 211 |
|
| 212 |
surf += pitch; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void _inf_blur(InfinitePrivate *priv, t_interpol* vector_field) |
| 217 |
{ |
| 218 |
_inf_compute_surface(priv, vector_field); |
| 219 |
} |
| 220 |
|
| 221 |
void _inf_plot1(InfinitePrivate *priv, int x,int y,int c) |
| 222 |
{ |
| 223 |
if (x>0 && x<priv->plugwidth-3 && y>0 && y<priv->plugheight-3) |
| 224 |
assign_max(&(priv->surface1)[x+y*priv->plugwidth],c); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
void _inf_plot2(InfinitePrivate *priv, int x,int y,int c) |
| 229 |
{ |
| 230 |
int ty; |
| 231 |
|
| 232 |
if (x>0 && x<priv->plugwidth-3 && y>0 && y<priv->plugheight-3) { |
| 233 |
ty = y*priv->plugwidth; |
| 234 |
assign_max(&(priv->surface1)[x+ty],c); |
| 235 |
assign_max(&(priv->surface1)[x+1+ty],c); |
| 236 |
assign_max(&(priv->surface1)[x+ty+priv->plugwidth],c); |
| 237 |
assign_max(&(priv->surface1)[x+1+ty+priv->plugwidth],c); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#define SWAP(x,y) \ |
| 242 |
x = x + y; \ |
| 243 |
y = x - y; \ |
| 244 |
x = x - y; |
| 245 |
|
| 246 |
void _inf_line(InfinitePrivate *priv, int x1, int y1, int x2, int y2, int c) { |
| 247 |
int dx, dy, cxy,dxy; |
| 248 |
/* calculate the distances */ |
| 249 |
dx = abs(x1 - x2); |
| 250 |
dy = abs(y1 - y2); |
| 251 |
|
| 252 |
cxy = 0; |
| 253 |
if (dy > dx) { |
| 254 |
/* Follow Y axis */ |
| 255 |
if (y1 > y2) { |
| 256 |
SWAP(y1, y2); |
| 257 |
SWAP(x1, x2); |
| 258 |
} |
| 259 |
|
| 260 |
if (x1 > x2) |
| 261 |
dxy = -1; |
| 262 |
else |
| 263 |
dxy = 1; |
| 264 |
|
| 265 |
for (y1=y1; y1<y2; y1++) { |
| 266 |
cxy += dx; |
| 267 |
if (cxy >= dy) { |
| 268 |
x1+= dxy; |
| 269 |
cxy -= dy; |
| 270 |
} |
| 271 |
_inf_plot1(priv, x1, y1, c); |
| 272 |
} |
| 273 |
} else { |
| 274 |
/* Follow X axis */ |
| 275 |
if (x1 > x2) { |
| 276 |
SWAP(x1, x2); |
| 277 |
SWAP(y1, y2); |
| 278 |
} |
| 279 |
|
| 280 |
if (y1 > y2) |
| 281 |
dxy = -1; |
| 282 |
else |
| 283 |
dxy = 1; |
| 284 |
|
| 285 |
for (x1=x1; x1<x2; x1++) { |
| 286 |
cxy += dy; |
| 287 |
if (cxy >= dx) { |
| 288 |
y1+=dxy; |
| 289 |
cxy -= dx; |
| 290 |
} |
| 291 |
_inf_plot1(priv, x1, y1, c); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
struct sincos { |
| 297 |
int i; |
| 298 |
float *f; |
| 299 |
}; |
| 300 |
|
| 301 |
/* Little optimization for cos/sin functions */ |
| 302 |
static struct sincos cosw = { 0, NULL }; |
| 303 |
static struct sincos sinw = { 0, NULL }; |
| 304 |
|
| 305 |
void _inf_spectral(InfinitePrivate *priv, t_effect* current_effect, float data[2][512]) |
| 306 |
{ |
| 307 |
int i, halfheight, halfwidth; |
| 308 |
float old_y1,old_y2; |
| 309 |
float y1=(((data[0][0]+data[1][0]) * 128)*current_effect->spectral_amplitude*priv->plugheight) * (1.0 / 4096.0); |
| 310 |
float y2=(((data[0][0]+data[1][0]) * 128)*current_effect->spectral_amplitude*priv->plugheight) * (1.0 / 4096.0); |
| 311 |
const int density_lines=5; |
| 312 |
const int step=4; |
| 313 |
const int shift=(current_effect->spectral_shift*priv->plugheight)>>8; |
| 314 |
|
| 315 |
if (cosw.i != priv->plugwidth || sinw.i != priv->plugwidth) { |
| 316 |
if (cosw.f != NULL) |
| 317 |
visual_mem_free(cosw.f); |
| 318 |
|
| 319 |
if (sinw.f != NULL) |
| 320 |
visual_mem_free(sinw.f); |
| 321 |
|
| 322 |
sinw.f = cosw.f = NULL; |
| 323 |
sinw.i = cosw.i = 0; |
| 324 |
} |
| 325 |
|
| 326 |
if (cosw.i == 0 || cosw.f == NULL) { |
| 327 |
float halfPI = (float)PI/2; |
| 328 |
cosw.i = priv->plugwidth; |
| 329 |
cosw.f = visual_mem_malloc0(sizeof(float)*priv->plugwidth); |
| 330 |
for (i=0; i<priv->plugwidth;i+=step) |
| 331 |
cosw.f[i] = cos((float)i/priv->plugwidth*PI+halfPI); |
| 332 |
} |
| 333 |
|
| 334 |
if (sinw.i == 0 || sinw.f == NULL) { |
| 335 |
float halfPI = (float)PI/2; |
| 336 |
sinw.i = priv->plugwidth; |
| 337 |
sinw.f = visual_mem_malloc0(sizeof(float)*priv->plugwidth); |
| 338 |
for (i=0; i<priv->plugwidth;i+=step) |
| 339 |
sinw.f[i] = sin((float)i/priv->plugwidth*PI+halfPI); |
| 340 |
} |
| 341 |
|
| 342 |
if (current_effect->mode_spectre==3) { |
| 343 |
if (y1<0) |
| 344 |
y1=0; |
| 345 |
if (y2<0) |
| 346 |
y2=0; |
| 347 |
} |
| 348 |
|
| 349 |
halfheight = priv->plugheight >> 1; |
| 350 |
halfwidth = priv->plugwidth >> 1; |
| 351 |
for (i=step;i<priv->plugwidth;i+=step) { |
| 352 |
old_y1=y1; |
| 353 |
old_y2=y2; |
| 354 |
y1=((data[1][(i<<9)/priv->plugwidth/density_lines] * 256)* |
| 355 |
current_effect->spectral_amplitude*priv->plugheight) * (1.0 / 4096.0); |
| 356 |
y2=((data[0][(i<<9)/priv->plugwidth/density_lines] * 256)* |
| 357 |
current_effect->spectral_amplitude*priv->plugheight) * (1.0 / 4096.0); |
| 358 |
|
| 359 |
switch (current_effect->mode_spectre) { |
| 360 |
case 0: |
| 361 |
_inf_line(priv, i-step,halfheight+shift+old_y2, |
| 362 |
i,halfheight+shift+y2, |
| 363 |
current_effect->spectral_color); |
| 364 |
break; |
| 365 |
case 1: |
| 366 |
_inf_line(priv, i-step,halfheight+shift+old_y1, |
| 367 |
i,halfheight+shift+y1, |
| 368 |
current_effect->spectral_color); |
| 369 |
_inf_line(priv, i-step,halfheight-shift+old_y2, |
| 370 |
i,halfheight-shift+y2, |
| 371 |
current_effect->spectral_color); |
| 372 |
break; |
| 373 |
case 2: |
| 374 |
_inf_line(priv, i-step,halfheight+shift+old_y1, |
| 375 |
i,halfheight+shift+y1, |
| 376 |
current_effect->spectral_color); |
| 377 |
_inf_line(priv, i-step,halfheight-shift+old_y1, |
| 378 |
i,halfheight-shift+y1, |
| 379 |
current_effect->spectral_color); |
| 380 |
_inf_line(priv, halfwidth+shift+old_y2,i-step, |
| 381 |
halfwidth+shift+y2,i, |
| 382 |
current_effect->spectral_color); |
| 383 |
_inf_line(priv, halfwidth-shift+old_y2,i-step, |
| 384 |
halfwidth-shift+y2,i, |
| 385 |
current_effect->spectral_color); |
| 386 |
break; |
| 387 |
case 3: |
| 388 |
if (y1<0) |
| 389 |
y1=0; |
| 390 |
if (y2<0) |
| 391 |
y2=0; |
| 392 |
case 4: |
| 393 |
_inf_line(priv, halfwidth + cosw.f[i-step] * (shift+old_y1), |
| 394 |
halfheight + sinw.f[i-step] * (shift+old_y1), |
| 395 |
halfwidth + cosw.f[i] * (shift+y1), |
| 396 |
halfheight + sinw.f[i] * (shift+y1), |
| 397 |
current_effect->spectral_color); |
| 398 |
_inf_line(priv, halfwidth - cosw.f[i-step] * (shift+old_y2), |
| 399 |
halfheight + sinw.f[i-step] * (shift+old_y2), |
| 400 |
halfwidth - cosw.f[i] * (shift+y2), |
| 401 |
halfheight + sinw.f[i] * (shift+y2), |
| 402 |
current_effect->spectral_color); |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (current_effect->mode_spectre==3 || current_effect->mode_spectre==4) |
| 408 |
_inf_line(priv, halfwidth + cosw.f[priv->plugwidth - step] * (shift+y1), |
| 409 |
halfheight + sinw.f[priv->plugwidth - step] * (shift+y1), |
| 410 |
halfwidth - cosw.f[priv->plugwidth - step] * (shift+y2), |
| 411 |
halfheight + sinw.f[priv->plugwidth - step] * (shift+y2), |
| 412 |
current_effect->spectral_color); |
| 413 |
} |
| 414 |
|
| 415 |
void _inf_curve(InfinitePrivate *priv, t_effect* current_effect) |
| 416 |
{ |
| 417 |
int i,j,k; |
| 418 |
float v,vr; |
| 419 |
float x,y; |
| 420 |
float amplitude=(float)current_effect->curve_amplitude/256; |
| 421 |
|
| 422 |
for (j=0;j<2;j++) { |
| 423 |
v=80; |
| 424 |
vr=0.001; |
| 425 |
k=current_effect->x_curve; |
| 426 |
for (i=0;i<64;i++) { |
| 427 |
x=cos((float)(k)/(v+v*j*1.34))*priv->plugheight*amplitude; |
| 428 |
y=sin((float)(k)/(1.756*(v+v*j*0.93)))*priv->plugheight*amplitude; |
| 429 |
_inf_plot2(priv, x*cos((float)k*vr)+y*sin((float)k*vr)+priv->plugwidth/2, |
| 430 |
x*sin((float)k*vr)-y*cos((float)k*vr)+priv->plugheight/2, |
| 431 |
current_effect->curve_color); |
| 432 |
k++; |
| 433 |
} |
| 434 |
} |
| 435 |
current_effect->x_curve=k; |
| 436 |
} |
| 437 |
|
| 438 |
void _inf_init_display(InfinitePrivate *priv) |
| 439 |
{ |
| 440 |
int allocsize; |
| 441 |
priv->plugwidth = priv->plugwidth; |
| 442 |
priv->plugheight = priv->plugheight; |
| 443 |
|
| 444 |
allocsize = priv->plugwidth * priv->plugheight; |
| 445 |
|
| 446 |
priv->surface1 = (uint8_t *) visual_mem_malloc0(allocsize); |
| 447 |
priv->surface2 = (uint8_t *) visual_mem_malloc0(allocsize); |
| 448 |
} |
| 449 |
|