| 1 | #include <stdlib.h> |
| 2 | #include <math.h> |
| 3 | #include "frei0r.h" |
| 4 | #include "frei0r/math.h" |
| 5 | |
| 6 | |
| 7 | // let's try to walk through everything because i'm a moron |
| 8 | // this is the struct for the effect instance |
| 9 | typedef struct gateweave_instance |
| 10 | { |
| 11 | // image dimensions |
| 12 | unsigned int width; |
| 13 | unsigned int height; |
| 14 | |
| 15 | // parameters |
| 16 | double interval; |
| 17 | double max_move_x; |
| 18 | double max_move_y; |
| 19 | |
| 20 | // other variables that the effect uses to keep track of things |
| 21 | double next_key_x; |
| 22 | double next_key_y; |
| 23 | double prev_key_x; |
| 24 | double prev_key_y; |
| 25 | |
| 26 | } gateweave_instance_t; |
| 27 | |
| 28 | |
| 29 | // these functions are for the effect |
| 30 | static double gateweave_random_range(double range, double last) |
| 31 | { |
| 32 | if(range <= 0) |
| 33 | { |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | // the maximum shift is 10 pixels |
| 38 | // since range includes fractional values, we want to multiply it by 100 |
| 39 | // this will generate an integer between -100 and 100 for the shift |
| 40 | // and then we divide by 100 to receive a decimal answer between -10.00 and 10.00 |
| 41 | range *= 10; |
| 42 | int int_range = range * 100; |
| 43 | int_range = (rand() % (int_range * 2)) - int_range; |
| 44 | double ret = int_range / 100.0; |
| 45 | |
| 46 | // we don't want to generate a value similar to one we already generated twice in a row |
| 47 | ret = CLAMP(ret, -range, range); |
| 48 | if((ret > 0 && ret >= last - 0.12) || (ret < 0 && ret <= last + 0.12)) |
| 49 | { |
| 50 | ret *= -1; |
| 51 | } |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | static inline double gateweave_lerp(double v0, double v1, double t) |
| 56 | { |
| 57 | return v0 + t * (v1 - v0); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | // this function mixes the colors of two pixels |
| 62 | // a is the original pixel that will be changed |
| 63 | // b is the second pixel that will not be changed |
| 64 | // the amount is how much of the second pixel will be mixed |
| 65 | // an amount of 1 means pixel a will be equal to pixel b |
| 66 | // an amount of 0 means pixel a will be equal to pixel a (no change) |
| 67 | // an amount of 0.5 means pixel a will be equal to 0.5 * pixel a + 0.5 * pixel b |
| 68 | static uint32_t gateweave_blend_color(uint32_t a, uint32_t b, double amount) |
| 69 | { |
| 70 | uint8_t c_a; |
| 71 | uint8_t c_b; |
| 72 | uint8_t c_g; |
| 73 | uint8_t c_r; |
| 74 | uint32_t output; |
| 75 | |
| 76 | // RRGGBBAA |
| 77 | // 0xFFFFFFFF |
| 78 | c_a = CLAMP0255(((a & 0xFF000000) >> 24) * (1 - amount) + ((b & 0xFF000000) >> 24) * (amount)); |
| 79 | c_b = CLAMP0255(((a & 0x00FF0000) >> 16) * (1 - amount) + ((b & 0x00FF0000) >> 16) * (amount)); |
| 80 | c_g = CLAMP0255(((a & 0x0000FF00) >> 8) * (1 - amount) + ((b & 0x0000FF00) >> 8) * (amount)); |
| 81 | c_r = CLAMP0255(((a & 0x000000FF)) * (1 - amount) + ((b & 0x000000FF)) * (amount)); |
| 82 | |
| 83 | output = (output & 0xFFFFFF00) | (uint32_t)c_r; |
| 84 | output = (output & 0xFFFF00FF) | ((uint32_t)c_g << 8); |
| 85 | output = (output & 0xFF00FFFF) | ((uint32_t)c_b << 16); |
| 86 | output = (output & 0x00FFFFFF) | ((uint32_t)c_a << 24); |
| 87 | |
| 88 | return output; |
| 89 | } |
| 90 | |
| 91 | // this function is the one that ultimately manipulates the image |
| 92 | // it shifts the image and can do so to a value less than one pixel |
| 93 | static inline void gateweave_shift_picture(const uint32_t* in, uint32_t* out, double shift_x, double shift_y, int w, int h) |
| 94 | { |
| 95 | uint32_t* buf = (uint32_t*)calloc(w * h, sizeof(uint32_t)); |
| 96 | |
| 97 | // first we shift to the nearest whole pixel |
| 98 | int int_shift_x = shift_x; |
| 99 | int int_shift_y = shift_y; |
| 100 | int pixel_shift = int_shift_x + (int_shift_y * w); |
| 101 | for(unsigned int i = 0; i < w * h; i++) |
| 102 | { |
| 103 | if(i + pixel_shift < 0 || i + pixel_shift >= w * h) |
| 104 | { |
| 105 | *(buf + i) = 0; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | *(buf + i) = *(in + i + pixel_shift); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // then we shift the fractional component |
| 114 | // let's think about how this works |
| 115 | // let's suppose we shift left 1.5 pixels and shift down 1.25 pixels |
| 116 | // after shifting 1 pixel left and 1 pixel down, we still have the 0.5 and 0.25 shifts left to do |
| 117 | // to do this we want each pixel to equal 50% the current pixel and 50% the pixel to its left |
| 118 | // we also want each pixel to equal 75% the current pixel and 25% the pixel below it |
| 119 | // but this isn't actually possible to do |
| 120 | shift_x -= int_shift_x; |
| 121 | shift_y -= int_shift_y; |
| 122 | |
| 123 | int shift_component_x; |
| 124 | int shift_component_y; |
| 125 | |
| 126 | char larger_x_comp = 0; |
| 127 | if(fabs(shift_x) > fabs(shift_y)) |
| 128 | { |
| 129 | larger_x_comp = 1; |
| 130 | } |
| 131 | |
| 132 | if(shift_x < 0) |
| 133 | { |
| 134 | shift_component_x = -1; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | shift_component_x = 1; |
| 139 | } |
| 140 | if(shift_y < 0) |
| 141 | { |
| 142 | shift_component_y = -w; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | shift_component_y = w; |
| 147 | } |
| 148 | |
| 149 | uint32_t v = 0; |
| 150 | for(unsigned int i = 0; i < w * h; i++) |
| 151 | { |
| 152 | if(i + shift_component_x >= 0 && i + shift_component_x < w * h && |
| 153 | i + shift_component_y >= 0 && i + shift_component_y < w * h && |
| 154 | i + shift_component_x + shift_component_y >= 0 && i + shift_component_x + shift_component_y < w * h) |
| 155 | { |
| 156 | if(larger_x_comp) |
| 157 | { |
| 158 | v = gateweave_blend_color(*(buf + i + shift_component_x), *(buf + i + shift_component_x + shift_component_y), shift_y); |
| 159 | *(out + i) = gateweave_blend_color(*(buf + i), v, shift_x); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | v = gateweave_blend_color(*(buf + i + shift_component_y), *(buf + i + shift_component_x + shift_component_y), shift_x); |
| 164 | *(out + i) = gateweave_blend_color(*(buf + i), v, shift_y); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | free(buf); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | // these functions are for frei0r |
| 173 | // mostly copy/paste/slightly modified from the other frei0r effects |
| 174 | int f0r_init() |
| 175 | { |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | void f0r_deinit() |
| 180 | {} |
| 181 | |
| 182 | void f0r_get_plugin_info(f0r_plugin_info_t* info) |
| 183 | { |
| 184 | info->name = "Gate Weave"; |
| 185 | info->author = "esmane"; |
| 186 | info->explanation = "Randomly moves frame around to simulate film gate weave."; |
| 187 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 188 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 189 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 190 | info->major_version = 0; |
| 191 | info->minor_version = 2; |
| 192 | info->num_params = 3; |
| 193 | } |
| 194 | |
| 195 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 196 | { |
| 197 | switch(param_index) |
| 198 | { |
| 199 | case 0: |
| 200 | info->name = "Interval"; |
| 201 | info->explanation = "The amount of time before the position is randomized again. The larger the number the slower the picture will move."; |
| 202 | info->type = F0R_PARAM_DOUBLE; |
| 203 | break; |
| 204 | |
| 205 | case 1: |
| 206 | info->name = "Maximum Horizontal Movement"; |
| 207 | info->explanation = "The maximum distance the picture could move left or right. The larger the number the more the picture moves and the less subtle the effect."; |
| 208 | info->type = F0R_PARAM_DOUBLE; |
| 209 | break; |
| 210 | |
| 211 | case 2: |
| 212 | info->name = "Maximum Vertical Movement"; |
| 213 | info->explanation = "The maximum distance the picture could move up or down. The larger the number the more the picture moves and the less subtle the effect."; |
| 214 | info->type = F0R_PARAM_DOUBLE; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 220 | { |
| 221 | gateweave_instance_t* inst = (gateweave_instance_t*)calloc(1, sizeof(*inst)); |
| 222 | |
| 223 | inst->width = width; |
| 224 | inst->height = height; |
| 225 | inst->interval = 0.6; |
| 226 | inst->max_move_x = 0.2; |
| 227 | inst->max_move_y = 0.2; |
| 228 | |
| 229 | // other variables that the effect uses to keep track of things |
| 230 | inst->next_key_x = gateweave_random_range(inst->max_move_x, 0); |
| 231 | inst->next_key_y = gateweave_random_range(inst->max_move_y, 0); |
| 232 | inst->prev_key_x = 0; |
| 233 | inst->prev_key_y = 0; |
| 234 | |
| 235 | return (f0r_instance_t)inst; |
| 236 | } |
| 237 | |
| 238 | void f0r_destruct(f0r_instance_t instance) |
| 239 | { |
| 240 | gateweave_instance_t* inst = (gateweave_instance_t*)instance; |
| 241 | free(instance); |
| 242 | } |
| 243 | |
| 244 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 245 | { |
| 246 | gateweave_instance_t* inst = (gateweave_instance_t*)instance; |
| 247 | switch(param_index) |
| 248 | { |
| 249 | case 0: |
| 250 | inst->interval = *((double*)param); |
| 251 | break; |
| 252 | case 1: |
| 253 | inst->max_move_x = *((double*)param); |
| 254 | break; |
| 255 | case 2: |
| 256 | inst->max_move_y = *((double*)param); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 262 | { |
| 263 | gateweave_instance_t* inst = (gateweave_instance_t*)instance; |
| 264 | switch(param_index) |
| 265 | { |
| 266 | case 0: |
| 267 | *((double*)param) = inst->interval; |
| 268 | break; |
| 269 | case 1: |
| 270 | *((double*)param) = inst->max_move_x; |
| 271 | break; |
| 272 | case 2: |
| 273 | *((double*)param) = inst->max_move_y; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe) |
| 279 | { |
| 280 | gateweave_instance_t* inst = (gateweave_instance_t*)instance; |
| 281 | inst->next_key_x = gateweave_random_range(inst->max_move_x, inst->next_key_x); |
| 282 | inst->next_key_y = gateweave_random_range(inst->max_move_y, inst->next_key_y); |
| 283 | |
| 284 | inst->prev_key_x = gateweave_lerp(inst->next_key_x, inst->prev_key_x, inst->interval); |
| 285 | inst->prev_key_y = gateweave_lerp(inst->next_key_y, inst->prev_key_y, inst->interval); |
| 286 | |
| 287 | gateweave_shift_picture(inframe, outframe, inst->prev_key_x, inst->prev_key_y, inst->width, inst->height); |
| 288 | } |