| 1 | /* camerashake.cpp |
| 2 | * Copyright (C) 2026 PakeMPC |
| 3 | * This file is a Frei0r plugin. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #if defined(_MSC_VER) |
| 23 | #define _USE_MATH_DEFINES |
| 24 | #endif |
| 25 | #include <math.h> |
| 26 | |
| 27 | extern "C" { |
| 28 | #include <frei0r.h> |
| 29 | |
| 30 | struct CameraShakeInstance { |
| 31 | double amp_x, amp_y, rotation, zoom, speed, opacity, blur; |
| 32 | float bg_r, bg_g, bg_b; |
| 33 | unsigned int width, height; |
| 34 | }; |
| 35 | |
| 36 | int f0r_init(void) { return 1; } |
| 37 | void f0r_deinit(void) {} |
| 38 | |
| 39 | void f0r_get_plugin_info(f0r_plugin_info_t* info) { |
| 40 | info->name = "Camera Shake Ultimate"; |
| 41 | info->author = "PakeMPC"; |
| 42 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 43 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 44 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 45 | info->major_version = 2; |
| 46 | info->minor_version = 0; |
| 47 | info->num_params = 8; |
| 48 | info->explanation = "Camera movement effect with rotation, opacity, and blur."; |
| 49 | } |
| 50 | |
| 51 | void f0r_get_param_info(f0r_param_info_t *info, int param_index) { |
| 52 | switch (param_index) { |
| 53 | case 0: |
| 54 | info->name = "amplitude_x"; |
| 55 | info->type = F0R_PARAM_DOUBLE; |
| 56 | info->explanation = "Maximum horizontal shake amplitude"; |
| 57 | break; |
| 58 | case 1: |
| 59 | info->name = "amplitude_y"; |
| 60 | info->type = F0R_PARAM_DOUBLE; |
| 61 | info->explanation = "Maximum vertical shake amplitude"; |
| 62 | break; |
| 63 | case 2: |
| 64 | info->name = "rotation"; |
| 65 | info->type = F0R_PARAM_DOUBLE; |
| 66 | info->explanation = "Maximum rotation shake"; |
| 67 | break; |
| 68 | case 3: |
| 69 | info->name = "zoom"; |
| 70 | info->type = F0R_PARAM_DOUBLE; |
| 71 | info->explanation = "Zoom factor to hide black borders"; |
| 72 | break; |
| 73 | case 4: |
| 74 | info->name = "speed"; |
| 75 | info->type = F0R_PARAM_DOUBLE; |
| 76 | info->explanation = "Speed or frequency of the shake"; |
| 77 | break; |
| 78 | case 5: |
| 79 | info->name = "opacity"; |
| 80 | info->type = F0R_PARAM_DOUBLE; |
| 81 | info->explanation = "Opacity of the effect"; |
| 82 | break; |
| 83 | case 6: |
| 84 | info->name = "blur"; |
| 85 | info->type = F0R_PARAM_DOUBLE; |
| 86 | info->explanation = "Amount of motion blur"; |
| 87 | break; |
| 88 | case 7: |
| 89 | info->name = "bg_color"; |
| 90 | info->type = F0R_PARAM_COLOR; |
| 91 | info->explanation = "Background color for exposed borders"; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) { |
| 97 | CameraShakeInstance* inst = new CameraShakeInstance(); |
| 98 | inst->amp_x = 0.25; |
| 99 | inst->amp_y = 0.25; |
| 100 | inst->rotation = 0.0; |
| 101 | inst->zoom = 0.2; |
| 102 | inst->speed = 0.25; |
| 103 | inst->opacity = 1.0; |
| 104 | inst->blur = 0.0; |
| 105 | inst->bg_r = 0.0f; |
| 106 | inst->bg_g = 0.0f; |
| 107 | inst->bg_b = 0.0f; |
| 108 | inst->width = width; |
| 109 | inst->height = height; |
| 110 | return (f0r_instance_t)inst; |
| 111 | } |
| 112 | |
| 113 | void f0r_destruct(f0r_instance_t instance) { delete (CameraShakeInstance*)instance; } |
| 114 | |
| 115 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) { |
| 116 | CameraShakeInstance* inst = (CameraShakeInstance*)instance; |
| 117 | double val = *((double*)param); |
| 118 | if (param_index == 0) inst->amp_x = val; |
| 119 | else if (param_index == 1) inst->amp_y = val; |
| 120 | else if (param_index == 2) inst->rotation = val; |
| 121 | else if (param_index == 3) inst->zoom = val; |
| 122 | else if (param_index == 4) inst->speed = val; |
| 123 | else if (param_index == 5) inst->opacity = val; |
| 124 | else if (param_index == 6) inst->blur = val; |
| 125 | else if (param_index == 7) { |
| 126 | f0r_param_color_t* color = (f0r_param_color_t*)param; |
| 127 | inst->bg_r = color->r; inst->bg_g = color->g; inst->bg_b = color->b; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) { |
| 133 | CameraShakeInstance* inst = (CameraShakeInstance*)instance; |
| 134 | if (param_index == 0) *((double*)param) = inst->amp_x; |
| 135 | else if (param_index == 1) *((double*)param) = inst->amp_y; |
| 136 | else if (param_index == 2) *((double*)param) = inst->rotation; |
| 137 | else if (param_index == 3) *((double*)param) = inst->zoom; |
| 138 | else if (param_index == 4) *((double*)param) = inst->speed; |
| 139 | else if (param_index == 5) *((double*)param) = inst->opacity; |
| 140 | else if (param_index == 6) *((double*)param) = inst->blur; |
| 141 | else if (param_index == 7) { |
| 142 | f0r_param_color_t* color = (f0r_param_color_t*)param; |
| 143 | color->r = inst->bg_r; color->g = inst->bg_g; color->b = inst->bg_b; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe) { |
| 148 | CameraShakeInstance* inst = (CameraShakeInstance*)instance; |
| 149 | int w = inst->width; |
| 150 | int h = inst->height; |
| 151 | |
| 152 | double speed_factor = inst->speed * 100.0; |
| 153 | |
| 154 | // 1. Zoom (0.0-1.0) mapped to scale (1.0-5.0) |
| 155 | double scale = 1.0 + (inst->zoom * 4.0); |
| 156 | |
| 157 | // 2. Shake X and Y (using the direct values up to 500px) |
| 158 | double max_amp_x = inst->amp_x * 500.0; |
| 159 | double max_amp_y = inst->amp_y * 500.0; |
| 160 | double shake_x = sin(time * speed_factor) * cos(time * speed_factor * 0.73) * max_amp_x; |
| 161 | double shake_y = sin(time * speed_factor * 0.89) * cos(time * speed_factor * 1.1) * max_amp_y; |
| 162 | |
| 163 | // 3. Rotation |
| 164 | double max_angle = inst->rotation * (M_PI / 4.0); |
| 165 | double theta = sin(time * speed_factor * 1.15) * max_angle; |
| 166 | double cos_t = cos(-theta); |
| 167 | double sin_t = sin(-theta); |
| 168 | |
| 169 | // 4. Blur and Opacity Parameters |
| 170 | int blur_radius = (int)(inst->blur * 20.0); |
| 171 | double alpha_mult = inst->opacity; |
| 172 | |
| 173 | double cx = w / 2.0; |
| 174 | double cy = h / 2.0; |
| 175 | |
| 176 | for (int y = 0; y < h; ++y) { |
| 177 | for (int x = 0; x < w; ++x) { |
| 178 | |
| 179 | // Inverse mapping of rotation and scale matrix |
| 180 | double dx = (x - cx) / scale; |
| 181 | double dy = (y - cy) / scale; |
| 182 | |
| 183 | double rx = dx * cos_t - dy * sin_t; |
| 184 | double ry = dx * sin_t + dy * cos_t; |
| 185 | |
| 186 | int base_src_x = (int)(cx + rx + shake_x); |
| 187 | int base_src_y = (int)(cy + ry + shake_y); |
| 188 | |
| 189 | uint8_t *out_ptr = (uint8_t *)&outframe[y * w + x]; |
| 190 | |
| 191 | // Check limits |
| 192 | if (base_src_x < 0 || base_src_x >= w || base_src_y < 0 || base_src_y >= h) { |
| 193 | out_ptr[0] = (uint8_t)(inst->bg_r * 255.0f); |
| 194 | out_ptr[1] = (uint8_t)(inst->bg_g * 255.0f); |
| 195 | out_ptr[2] = (uint8_t)(inst->bg_b * 255.0f); |
| 196 | out_ptr[3] = (uint8_t)(255 * alpha_mult); |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | // If there's NO blur, paint directly |
| 201 | if (blur_radius == 0) { |
| 202 | uint8_t *in_ptr = (uint8_t *)&inframe[base_src_y * w + base_src_x]; |
| 203 | out_ptr[0] = in_ptr[0]; |
| 204 | out_ptr[1] = in_ptr[1]; |
| 205 | out_ptr[2] = in_ptr[2]; |
| 206 | out_ptr[3] = (uint8_t)(in_ptr[3] * alpha_mult); |
| 207 | } |
| 208 | // If there is blur, do a "Fast Cross-Blur" |
| 209 | else { |
| 210 | int sum_r = 0, sum_g = 0, sum_b = 0, sum_a = 0; |
| 211 | int samples = 0; |
| 212 | |
| 213 | // Horizontal and vertical sampling (cross shape for yield) |
| 214 | for (int i = -blur_radius; i <= blur_radius; i += 2) { |
| 215 | // Horizontal |
| 216 | int sx = base_src_x + i; |
| 217 | if (sx >= 0 && sx < w) { |
| 218 | uint8_t* p = (uint8_t*)&inframe[base_src_y * w + sx]; |
| 219 | sum_r += p[0]; sum_g += p[1]; sum_b += p[2]; sum_a += p[3]; |
| 220 | samples++; |
| 221 | } |
| 222 | // Vertical |
| 223 | int sy = base_src_y + i; |
| 224 | if (sy >= 0 && sy < h && i != 0) { // i!=0 to avoid adding the center twice |
| 225 | uint8_t* p = (uint8_t*)&inframe[sy * w + base_src_x]; |
| 226 | sum_r += p[0]; sum_g += p[1]; sum_b += p[2]; sum_a += p[3]; |
| 227 | samples++; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | out_ptr[0] = sum_r / samples; |
| 232 | out_ptr[1] = sum_g / samples; |
| 233 | out_ptr[2] = sum_b / samples; |
| 234 | out_ptr[3] = (uint8_t)((sum_a / samples) * alpha_mult); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |