minor optimization, fix indenting

This commit is contained in:
veejay
2023-11-30 23:39:23 +01:00
parent a40828db3c
commit d22cb19e70

View File

@@ -27,17 +27,17 @@ vj_effect *swirl_init(int w, int h)
vj_effect *ve = (vj_effect *) vj_calloc(sizeof(vj_effect)); vj_effect *ve = (vj_effect *) vj_calloc(sizeof(vj_effect));
ve->num_params = 1; ve->num_params = 1;
ve->defaults = (int *) vj_calloc(sizeof(int) * ve->num_params); /* default values */ ve->defaults = (int *) vj_calloc(sizeof(int) * ve->num_params); /* default values */
ve->limits[0] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* min */ ve->limits[0] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* min */
ve->limits[1] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* max */ ve->limits[1] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* max */
ve->limits[0][0] = 1; ve->limits[0][0] = 1;
ve->limits[1][0] = 360; ve->limits[1][0] = 360;
ve->defaults[0] = 250; ve->defaults[0] = 250;
ve->description = "Swirl"; ve->description = "Swirl";
ve->sub_format = 1; ve->sub_format = 1;
ve->extra_frame = 0; ve->extra_frame = 0;
ve->has_user = 0; ve->has_user = 0;
ve->param_description = vje_build_param_list( ve->num_params, "Degrees" ); ve->param_description = vje_build_param_list( ve->num_params, "Degrees" );
return ve; return ve;
} }
@@ -51,38 +51,31 @@ typedef struct {
void *swirl_malloc(int w, int h) void *swirl_malloc(int w, int h)
{ {
int x,y; int x,y;
int h2=h/2; int h2=h/2;
int w2=w/2; int w2=w/2;
int p = 0; int p = 0;
swirl_t *s = (swirl_t*) vj_calloc( sizeof(swirl_t) ); swirl_t *s = (swirl_t*) vj_calloc( sizeof(swirl_t) );
if(!s) { if(!s) {
return NULL; return NULL;
} }
/* s->buf[0] = vj_malloc( sizeof(uint8_t) * (w*h*3));
if(!s->buf[0]) { s->polar_map = (double*) vj_calloc(sizeof(double) * (w * h) );
swirl_free(s); if(!s->polar_map) {
return NULL;
}
s->buf[1] = s->buf[0] + (w*h);
s->buf[2] = s->buf[1] + (w*h);
*/
s->polar_map = (double*) vj_calloc(sizeof(double) * (w * h) );
if(!s->polar_map) {
swirl_free(s); swirl_free(s);
return NULL; return NULL;
} }
s->fish_angle = (double*) vj_calloc(sizeof(double) * (w * h) ); s->fish_angle = (double*) vj_calloc(sizeof(double) * (w * h) );
if(!s->fish_angle) { if(!s->fish_angle) {
swirl_free(s); swirl_free(s);
return NULL; return NULL;
} }
s->cached_coords = (int*) vj_calloc(sizeof(int) * (w * h) ); s->cached_coords = (int*) vj_calloc(sizeof(int) * (w * h) );
if(!s->cached_coords) { if(!s->cached_coords) {
swirl_free(s); swirl_free(s);
return NULL; return NULL;
} }
@@ -90,26 +83,24 @@ void *swirl_malloc(int w, int h)
double *polar_map = s->polar_map; double *polar_map = s->polar_map;
double *fish_angle = s->fish_angle; double *fish_angle = s->fish_angle;
for(y=(-1 *h2); y < (h-h2); y++) for(y=(-1 *h2); y < (h-h2); y++)
{ {
for(x=(-1 * w2); x < (w-w2); x++) for(x=(-1 * w2); x < (w-w2); x++)
{ {
p = (h2+y) * w + (w2+x); p = (h2+y) * w + (w2+x);
polar_map[p] = sqrt( y*y + x*x ); polar_map[p] = sqrt( y*y + x*x );
fish_angle[p] = atan2( (float) y, x); fish_angle[p] = atan2( (float) y, x);
} }
} }
return (void*) s; return (void*) s;
} }
void swirl_free(void *ptr) void swirl_free(void *ptr)
{ {
swirl_t *s = (swirl_t*) ptr; swirl_t *s = (swirl_t*) ptr;
if(s) { if(s) {
// if( s->buf[0] )
// free(s->buf[0] );
if( s->polar_map ) if( s->polar_map )
free(s->polar_map); free(s->polar_map);
if( s->fish_angle ) if( s->fish_angle )
@@ -122,13 +113,13 @@ void swirl_free(void *ptr)
void swirl_apply(void *ptr, VJFrame *frame, int *args) void swirl_apply(void *ptr, VJFrame *frame, int *args)
{ {
int i; int i;
const unsigned int width = frame->width; const unsigned int width = frame->width;
const unsigned int height = frame->height; const unsigned int height = frame->height;
const int len = frame->len; const int len = frame->len;
uint8_t *Y = frame->data[0]; uint8_t *Y = frame->data[0];
uint8_t *Cb= frame->data[1]; uint8_t *Cb= frame->data[1];
uint8_t *Cr= frame->data[2]; uint8_t *Cr= frame->data[2];
int v = args[0]; int v = args[0];
swirl_t *s = (swirl_t*) ptr; swirl_t *s = (swirl_t*) ptr;
@@ -136,46 +127,41 @@ void swirl_apply(void *ptr, VJFrame *frame, int *args)
double *polar_map = s->polar_map; double *polar_map = s->polar_map;
double *fish_angle = s->fish_angle; double *fish_angle = s->fish_angle;
int *cached_coords = s->cached_coords; int *cached_coords = s->cached_coords;
// uint8_t **buf = s->buf;
if( s->v != v ) if( s->v != v )
{ {
const unsigned int R = width; const unsigned int R = width;
const double coeef = v; const double coeef = v;
/* pre calculate */ int px,py;
int px,py; double r,a;
double r,a; double si,co;
double si,co; const int w2 = width/2;
const int w2 = width/2; const int h2 = height/2;
const int h2 = height/2;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
r = polar_map[i]; r = polar_map[i];
a = fish_angle[i]; a = fish_angle[i];
sin_cos(co, si, (a + r / coeef)); sin_cos(co, si, (a + r / coeef));
px = (int)(r * co) + w2; px = (int)(r * co) + w2;
py = (int)(r * si) + h2; py = (int)(r * si) + h2;
px = (px < 0) ? 0 : ((px >= width) ? (width - 1) : px); px = (px < 0) ? 0 : ((px >= width) ? (width - 1) : px);
py = (py < 0) ? 0 : ((py >= height) ? (height - 1) : py); py = (py < 0) ? 0 : ((py >= height) ? (height - 1) : py);
cached_coords[i] = (py * width) + px; cached_coords[i] = (py * width) + px;
} }
s->v = v; s->v = v;
} }
// int strides[4] = { len, len, len , 0 }; for(i=0; i < len; i++)
// vj_frame_copy( frame->data, buf, strides ); {
int idx = cached_coords[i];
for(i=0; i < len; i++) Y[i] = Y[idx];
{ Cb[i] = Cb[idx];
int idx = (cached_coords[i] == -1) ? 0 : cached_coords[i]; Cr[i] = Cr[idx];
}
Y[i] = Y[idx];
Cb[i] = Cb[idx];
Cr[i] = Cr[idx];
}
} }