| 1 | #include <stdlib.h> |
| 2 | #include <assert.h> |
| 3 | #if defined(_MSC_VER) |
| 4 | #define _USE_MATH_DEFINES |
| 5 | #endif |
| 6 | #include <math.h> |
| 7 | |
| 8 | #include "frei0r.h" |
| 9 | |
| 10 | |
| 11 | #define GRID_SIZE_LOG 3 |
| 12 | #define GRID_SIZE (1<<GRID_SIZE_LOG) |
| 13 | |
| 14 | typedef struct grid_point |
| 15 | { |
| 16 | int32_t u; |
| 17 | int32_t v; |
| 18 | } grid_point_t; |
| 19 | |
| 20 | |
| 21 | typedef struct distorter_instance |
| 22 | { |
| 23 | unsigned int width, height; |
| 24 | double amplitude, frequency, change_speed; |
| 25 | grid_point_t* grid; |
| 26 | double time_stack; |
| 27 | double mode; |
| 28 | } distorter_instance_t; |
| 29 | |
| 30 | //const double AMPLTUDE_SCALE = 10.0; |
| 31 | const double FREQUENCY_SCALE = 200.0; |
| 32 | const double SPEED_SCALE = 2.0; |
| 33 | |
| 34 | void interpolateGrid(grid_point_t* grid, unsigned int w, unsigned int h, |
| 35 | const uint32_t* src, uint32_t* dst); |
| 36 | |
| 37 | int f0r_init() |
| 38 | { |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | void f0r_deinit() |
| 43 | {} |
| 44 | |
| 45 | void f0r_get_plugin_info(f0r_plugin_info_t* distorterInfo) |
| 46 | { |
| 47 | distorterInfo->name = "Distort0r"; |
| 48 | distorterInfo->author = "Gephex crew"; |
| 49 | distorterInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 50 | distorterInfo->color_model = F0R_COLOR_MODEL_PACKED32; |
| 51 | distorterInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
| 52 | distorterInfo->major_version = 0; |
| 53 | distorterInfo->minor_version = 10; |
| 54 | distorterInfo->num_params = 4; |
| 55 | distorterInfo->explanation = "Plasma"; |
| 56 | } |
| 57 | |
| 58 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 59 | { |
| 60 | switch(param_index) |
| 61 | { |
| 62 | case 0: |
| 63 | info->name = "Amplitude"; |
| 64 | info->type = F0R_PARAM_DOUBLE; |
| 65 | info->explanation = "The amplitude of the plasma signal"; |
| 66 | break; |
| 67 | case 1: |
| 68 | info->name = "Frequency"; |
| 69 | info->type = F0R_PARAM_DOUBLE; |
| 70 | info->explanation = "The frequency of the plasma signal"; |
| 71 | break; |
| 72 | case 2: |
| 73 | info->name = "Use Velocity"; |
| 74 | info->type = F0R_PARAM_BOOL; |
| 75 | info->explanation = "\'Time Based\' or \'Adjustable Velocity\'"; |
| 76 | break; |
| 77 | case 3: |
| 78 | info->name = "Velocity"; |
| 79 | info->type = F0R_PARAM_DOUBLE; |
| 80 | info->explanation = "Changing speed of the plasma signal"; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 86 | { |
| 87 | distorter_instance_t* inst = (distorter_instance_t*)calloc(1, sizeof(*inst)); |
| 88 | inst->width = width; inst->height = height; |
| 89 | inst->grid = |
| 90 | (grid_point_t*)malloc(sizeof(grid_point_t)* |
| 91 | ((width/GRID_SIZE)+1)*((height/GRID_SIZE)+1)); |
| 92 | inst->amplitude = 1.0; |
| 93 | inst->frequency = 1.0; |
| 94 | inst->change_speed = 1.0; |
| 95 | inst->time_stack = 0.0; |
| 96 | inst->mode = 0.0; |
| 97 | return (f0r_instance_t)inst; |
| 98 | } |
| 99 | |
| 100 | void f0r_destruct(f0r_instance_t instance) |
| 101 | { |
| 102 | distorter_instance_t* inst = (distorter_instance_t*)instance; |
| 103 | free(inst->grid); |
| 104 | free(inst); |
| 105 | } |
| 106 | |
| 107 | void f0r_set_param_value(f0r_instance_t instance, |
| 108 | f0r_param_t param, int param_index) |
| 109 | { |
| 110 | assert(instance); |
| 111 | distorter_instance_t* inst = (distorter_instance_t*)instance; |
| 112 | |
| 113 | switch(param_index) |
| 114 | { |
| 115 | case 0: |
| 116 | // don't scale |
| 117 | inst->amplitude = *((double*)param); |
| 118 | break; |
| 119 | case 1: |
| 120 | // scale |
| 121 | inst->frequency = *((double*)param) * FREQUENCY_SCALE; |
| 122 | break; |
| 123 | case 2: |
| 124 | inst->mode = *((f0r_param_bool *)param); |
| 125 | break; |
| 126 | case 3: |
| 127 | // scale |
| 128 | inst->change_speed = *((double*)param) * SPEED_SCALE; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void f0r_get_param_value(f0r_instance_t instance, |
| 134 | f0r_param_t param, int param_index) |
| 135 | { |
| 136 | double scaled; |
| 137 | |
| 138 | assert(instance); |
| 139 | distorter_instance_t* inst = (distorter_instance_t*)instance; |
| 140 | |
| 141 | |
| 142 | switch(param_index) |
| 143 | { |
| 144 | case 0: |
| 145 | // don't scale |
| 146 | *((double*)param) = inst->amplitude; |
| 147 | break; |
| 148 | case 1: |
| 149 | // scale to [0..1] |
| 150 | scaled = inst->frequency / FREQUENCY_SCALE; |
| 151 | *((double*)param) = scaled; |
| 152 | break; |
| 153 | case 2: |
| 154 | *((f0r_param_bool *)param) = inst->mode; |
| 155 | break; |
| 156 | case 3: |
| 157 | // scale to [0..1] |
| 158 | scaled = inst->change_speed / SPEED_SCALE; |
| 159 | *((double*)param) = scaled; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | } |
| 164 | |
| 165 | /* this will compute a displacement value such that |
| 166 | 0<=x_retval<xsize and 0<=y_retval<ysize. */ |
| 167 | static inline void plasmaFunction |
| 168 | (int32_t* x_retval, int32_t* y_retval, |
| 169 | unsigned int x, unsigned int y, |
| 170 | unsigned int w, unsigned int h, |
| 171 | double amp, double freq, double t) |
| 172 | { |
| 173 | double time = fmod(t, 2*M_PI); |
| 174 | double h_ = (double)h -1; double w_ = (double)w-1; |
| 175 | double dx = (-4./(w_*w_)*x + 4./w_)*x; |
| 176 | double dy = (-4./(h_*h_)*y + 4./h_)*y; |
| 177 | *x_retval = (int32_t)(65536.0*((double)x+amp*(w/4)*dx*sin(freq*y/h + time))); |
| 178 | *y_retval = (int32_t)(65536.0*((double)y+amp*(h/4)*dy*sin(freq*x/w + time))); |
| 179 | |
| 180 | } |
| 181 | |
| 182 | void f0r_update(f0r_instance_t instance, double time, |
| 183 | const uint32_t* inframe, uint32_t* outframe) |
| 184 | { |
| 185 | assert(instance); |
| 186 | distorter_instance_t* inst = (distorter_instance_t*)instance; |
| 187 | unsigned int w = inst->width; |
| 188 | unsigned int h = inst->height; |
| 189 | unsigned int x,y; |
| 190 | |
| 191 | inst->time_stack+=inst->change_speed; |
| 192 | |
| 193 | grid_point_t* pt = inst->grid; |
| 194 | for(y=0;y<=h;y+= GRID_SIZE) |
| 195 | for(x=0;x<=w;x+=GRID_SIZE,++pt) |
| 196 | { |
| 197 | plasmaFunction(&pt->u, &pt->v, x, y, w, h, |
| 198 | inst->amplitude, inst->frequency, inst->mode?inst->time_stack:time); |
| 199 | } |
| 200 | |
| 201 | interpolateGrid(inst->grid, w, h, inframe, outframe); |
| 202 | } |
| 203 | |
| 204 | void interpolateGrid(grid_point_t* grid, unsigned int w, unsigned int h, |
| 205 | const uint32_t* src, uint32_t* dst) |
| 206 | { |
| 207 | unsigned int x, y, block_x, block_y; |
| 208 | unsigned int tex_x = 0, tex_y = 0; |
| 209 | unsigned int grid_x = (w / GRID_SIZE); |
| 210 | unsigned int grid_y = (h / GRID_SIZE); |
| 211 | for(y=0, tex_y=0; y < grid_y; y++) |
| 212 | { |
| 213 | for(x=0, tex_x=0; x < grid_x; x++) |
| 214 | { |
| 215 | unsigned int offset = x + y*(grid_x+1); |
| 216 | |
| 217 | grid_point_t* upper_left = grid + offset; |
| 218 | grid_point_t* lower_left = grid + offset + grid_x + 1; |
| 219 | grid_point_t* upper_right = grid + offset + 1; |
| 220 | grid_point_t* lower_right = grid + offset + grid_x + 2; |
| 221 | |
| 222 | int32_t u_left, u_right, v_left, v_right; |
| 223 | |
| 224 | int32_t start_col_uu = upper_left->u; |
| 225 | int32_t start_col_vv = upper_left->v; |
| 226 | int32_t end_col_uu = upper_right->u; |
| 227 | int32_t end_col_vv = upper_right->v; |
| 228 | |
| 229 | int32_t step_start_col_u = (lower_left->u - upper_left->u) |
| 230 | >> GRID_SIZE_LOG; |
| 231 | int32_t step_start_col_v = (lower_left->v - upper_left->v) |
| 232 | >> GRID_SIZE_LOG; |
| 233 | int32_t step_end_col_u = (lower_right->u - upper_right->u) |
| 234 | >> GRID_SIZE_LOG; |
| 235 | int32_t step_end_col_v = (lower_right->v - upper_right->v) |
| 236 | >> GRID_SIZE_LOG; |
| 237 | |
| 238 | int32_t u_line_index, v_line_index; |
| 239 | int32_t step_line_u, step_line_v; |
| 240 | |
| 241 | |
| 242 | uint32_t* pos = dst+ (y<<GRID_SIZE_LOG)*w + (x<<GRID_SIZE_LOG); |
| 243 | |
| 244 | for(block_y = 0; block_y < GRID_SIZE; ++block_y) |
| 245 | { |
| 246 | u_left = start_col_uu; |
| 247 | u_right = end_col_uu; |
| 248 | v_left = start_col_vv; |
| 249 | v_right = end_col_vv; |
| 250 | |
| 251 | u_line_index = start_col_uu; |
| 252 | v_line_index = start_col_vv; |
| 253 | |
| 254 | step_line_u = (int32_t) ((u_right-u_left) >> GRID_SIZE_LOG); |
| 255 | step_line_v = (int32_t) ((v_right-v_left) >> GRID_SIZE_LOG); |
| 256 | |
| 257 | for(block_x=0; block_x < GRID_SIZE; ++block_x) |
| 258 | { |
| 259 | int uu = u_line_index >> 16; |
| 260 | int vv = v_line_index >> 16; |
| 261 | |
| 262 | u_line_index += step_line_u; |
| 263 | v_line_index += step_line_v; |
| 264 | |
| 265 | // Bounds checking to prevent buffer overrun |
| 266 | if (uu >= 0 && uu < (int)w && vv >= 0 && vv < (int)h) { |
| 267 | *pos++ = src[uu + vv * w]; |
| 268 | } else { |
| 269 | *pos++ = 0; // Black pixel for out-of-bounds access |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | start_col_uu += step_start_col_u; |
| 274 | end_col_uu += step_end_col_u; |
| 275 | start_col_vv += step_start_col_v; |
| 276 | end_col_vv += step_end_col_v; |
| 277 | |
| 278 | pos += (w - GRID_SIZE); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |