| 1 | /* squigglevision.cpp |
| 2 | * Copyright (C) 2026 Gabriel Lobo (https://olobo.xyz) |
| 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 "frei0r.hpp"cxx . . $ `<file>/include/frei0r.hpp`/ |
| 21 | #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/ |
| 22 | |
| 23 | #include <cmath> |
| 24 | #include <cstdint> |
| 25 | #include <vector> |
| 26 | |
| 27 | #ifndef NO_FUTURE |
| 28 | #include <future> |
| 29 | #include <thread> |
| 30 | #endif |
| 31 | |
| 32 | #ifndef M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`! |
| 33 | #define M_PI 3.14159265358979323846 |
| 34 | #endif |
| 35 | |
| 36 | #ifndef M_Ecxx . . $ `<external>/11263917310783833665/math.h:1284:10`! |
| 37 | #define M_E 2.71828182845904523536 |
| 38 | #endif |
| 39 | |
| 40 | namespacecxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/ |
| 41 | { |
| 42 | |
| 43 | float hash2cxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/hash2(ac70f22789a9917c).(int x, int y) |
| 44 | { |
| 45 | unsigned int h = (unsigned int)(x * 374761393 + y * 668265263); |
| 46 | h = (h ^ (h >> 13)) * 1274126177u; |
| 47 | h ^= h >> 16; |
| 48 | return (h & 0xffffffu) / (float)0x1000000; |
| 49 | } |
| 50 | |
| 51 | float value_noisecxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/value_noise(d59a45cd3bfd2eda).(float x, float y) |
| 52 | { |
| 53 | int xi = (int)stdcxx . . $ std/::floor(x); |
| 54 | int yi = (int)stdcxx . . $ std/::floor(y); |
| 55 | float fx = x - xi; |
| 56 | float fy = y - yi; |
| 57 | |
| 58 | float a = hash2cxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/hash2(ac70f22789a9917c).(xi, yi); |
| 59 | float b = hash2cxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/hash2(ac70f22789a9917c).(xi + 1, yi); |
| 60 | float c = hash2cxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/hash2(ac70f22789a9917c).(xi, yi + 1); |
| 61 | float d = hash2cxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/hash2(ac70f22789a9917c).(xi + 1, yi + 1); |
| 62 | |
| 63 | float ab = a + (b - a) * fx; |
| 64 | float cd = c + (d - c) * fx; |
| 65 | return ab + (cd - ab) * fy; |
| 66 | } |
| 67 | |
| 68 | void sample_bilinearcxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/sample_bilinear(1300f42e0f80f9ac).(const unsigned char *s, int w, int h, |
| 69 | float fx, float fy, unsigned char *o) |
| 70 | { |
| 71 | if (fx < 0) |
| 72 | fx = 0; |
| 73 | if (fy < 0) |
| 74 | fy = 0; |
| 75 | if (fx > w - 1) |
| 76 | fx = w - 1; |
| 77 | if (fy > h - 1) |
| 78 | fy = h - 1; |
| 79 | |
| 80 | int x0 = (int)fx; |
| 81 | int y0 = (int)fy; |
| 82 | int x1 = x0 + 1 < w ? x0 + 1 : x0; |
| 83 | int y1 = y0 + 1 < h ? y0 + 1 : y0; |
| 84 | float dx = fx - x0; |
| 85 | float dy = fy - y0; |
| 86 | |
| 87 | const unsigned char *corners[4] = { |
| 88 | s + ((size_t)y0 * w + x0) * 4, |
| 89 | s + ((size_t)y0 * w + x1) * 4, |
| 90 | s + ((size_t)y1 * w + x0) * 4, |
| 91 | s + ((size_t)y1 * w + x1) * 4}; |
| 92 | float weights[4] = { |
| 93 | (1 - dx) * (1 - dy), |
| 94 | dx * (1 - dy), |
| 95 | (1 - dx) * dy, |
| 96 | dx * dy}; |
| 97 | |
| 98 | float inv255 = 1.0f / 255.0f; |
| 99 | float pr = 0, pg = 0, pb = 0, pa = 0; |
| 100 | |
| 101 | for (int k = 0; k < 4; k++) |
| 102 | { |
| 103 | const unsigned char *p = corners[k]; |
| 104 | float a = p[3] * inv255; |
| 105 | float wk = weights[k]; |
| 106 | pr += p[0] * a * wk; |
| 107 | pg += p[1] * a * wk; |
| 108 | pb += p[2] * a * wk; |
| 109 | pa += p[3] * wk; |
| 110 | } |
| 111 | |
| 112 | o[3] = (unsigned char)(pa + 0.5f); |
| 113 | |
| 114 | if (pa > 0.0f) |
| 115 | { |
| 116 | float inv = 255.0f / pa; |
| 117 | o[0] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)(pr * inv + 0.5f)); |
| 118 | o[1] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)(pg * inv + 0.5f)); |
| 119 | o[2] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)(pb * inv + 0.5f)); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | o[0] = 0; |
| 124 | o[1] = 0; |
| 125 | o[2] = 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } // namespace |
| 130 | |
| 131 | class squigglevisioncxx . . $ squigglevision# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter# |
| 132 | { |
| 133 | public: |
| 134 | squigglevisioncxx . . $ squigglevision#squigglevision(b4d55b1286ff73e8).(unsigned int width, unsigned int height) |
| 135 | { |
| 136 | (void)width; |
| 137 | (void)height; |
| 138 | |
| 139 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(m_strengthcxx . . $ squigglevision#m_strength., "Strength"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 140 | "Displacement amount, normalized 0..1 mapped to 0..100 percent (8 percent is a moderate default)."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 141 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(m_fpscxx . . $ squigglevision#m_fps., "FPS"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 142 | "Squiggle updates per second, normalized 0..1 mapped to 0..30 fps (0 = frozen; 6 fps is a moderate default)."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 143 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(m_scalecxx . . $ squigglevision#m_scale., "Scale"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 144 | "Noise cell size, normalized 0..1 mapped to 4..128 px (about 23 px is a moderate default)."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 145 | |
| 146 | m_strengthcxx . . $ squigglevision#m_strength. = 0.08; |
| 147 | m_fpscxx . . $ squigglevision#m_fps. = 0.2; |
| 148 | m_scalecxx . . $ squigglevision#m_scale. = 0.15; |
| 149 | } |
| 150 | |
| 151 | void updatecxx . . $ squigglevision#update(da7b4a17ceb4e697).(double time, uint32_t *out, const uint32_t *in) |
| 152 | { |
| 153 | const int w = (int)widthcxx . . $ frei0r/fx#width.; |
| 154 | const int h = (int)heightcxx . . $ frei0r/fx#height.; |
| 155 | |
| 156 | const double disp = m_strengthcxx . . $ squigglevision#m_strength. * 5.0 * 0.005; |
| 157 | const double fps = m_fpscxx . . $ squigglevision#m_fps. * 30.0; |
| 158 | const double cell = 4.0 + m_scalecxx . . $ squigglevision#m_scale. * 124.0; |
| 159 | |
| 160 | m_inv_cellcxx . . $ squigglevision#m_inv_cell. = (float)(1.0 / cell); |
| 161 | m_dispxcxx . . $ squigglevision#m_dispx. = (float)(disp * w); |
| 162 | m_dispycxx . . $ squigglevision#m_dispy. = (float)(disp * h); |
| 163 | |
| 164 | const double frame = stdcxx . . $ std/::floor(time * fps); |
| 165 | build_gridcxx . . $ squigglevision#build_grid(aef2b8561d239819).(w, h, (float)(frame * M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!), (float)(frame * M_Ecxx . . $ `<external>/11263917310783833665/math.h:1284:10`!)); |
| 166 | |
| 167 | dispatchcxx . . $ squigglevision#dispatch(31ec06a0e995072d).((const unsigned char *)in, (unsigned char *)out, w, h); |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | void build_gridcxx . . $ squigglevision#build_grid(aef2b8561d239819).(int w, int h, float offx, float offy) |
| 172 | { |
| 173 | m_gwcxx . . $ squigglevision#m_gw. = (int)(w * m_inv_cellcxx . . $ squigglevision#m_inv_cell.) + 2; |
| 174 | m_ghcxx . . $ squigglevision#m_gh. = (int)(h * m_inv_cellcxx . . $ squigglevision#m_inv_cell.) + 2; |
| 175 | m_gxcxx . . $ squigglevision#m_gx..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).((size_t)m_gwcxx . . $ squigglevision#m_gw. * m_ghcxx . . $ squigglevision#m_gh.); |
| 176 | m_gycxx . . $ squigglevision#m_gy..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).((size_t)m_gwcxx . . $ squigglevision#m_gw. * m_ghcxx . . $ squigglevision#m_gh.); |
| 177 | |
| 178 | for (int j = 0; j < m_ghcxx . . $ squigglevision#m_gh.; j++) |
| 179 | { |
| 180 | for (int i = 0; i < m_gwcxx . . $ squigglevision#m_gw.; i++) |
| 181 | { |
| 182 | float angle = value_noisecxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/value_noise(d59a45cd3bfd2eda).(i + offx, j + offy) * 4.0f * (float)M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!; |
| 183 | m_gxcxx . . $ squigglevision#m_gx.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).(size_t)j * m_gwcxx . . $ squigglevision#m_gw. + i] = stdcxx . . $ std/::cos(angle) * m_dispxcxx . . $ squigglevision#m_dispx.; |
| 184 | m_gycxx . . $ squigglevision#m_gy.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).(size_t)j * m_gwcxx . . $ squigglevision#m_gw. + i] = stdcxx . . $ std/::sin(angle) * m_dispycxx . . $ squigglevision#m_dispy.; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void render_rowscxx . . $ squigglevision#render_rows(5736d0456fc3aabc).(int y0, int y1, const unsigned char *src, unsigned char *dst, |
| 190 | int w, int h) const |
| 191 | { |
| 192 | const float *gx = m_gxcxx . . $ squigglevision#m_gx..datacxx . . $ std/vector#data(1016ee5406d91ab9).(); |
| 193 | const float *gy = m_gycxx . . $ squigglevision#m_gy..datacxx . . $ std/vector#data(1016ee5406d91ab9).(); |
| 194 | const int gw = m_gwcxx . . $ squigglevision#m_gw.; |
| 195 | const float inv_cell = m_inv_cellcxx . . $ squigglevision#m_inv_cell.; |
| 196 | |
| 197 | for (int y = y0; y < y1; y++) |
| 198 | { |
| 199 | const float v = y * inv_cell; |
| 200 | const int j = (int)v; |
| 201 | const float fv = v - j; |
| 202 | |
| 203 | for (int x = 0; x < w; x++) |
| 204 | { |
| 205 | const float u = x * inv_cell; |
| 206 | const int i = (int)u; |
| 207 | const float fu = u - i; |
| 208 | const size_t g = (size_t)j * gw + i; |
| 209 | |
| 210 | float xa = gx[g] + (gx[g + 1] - gx[g]) * fu; |
| 211 | float xb = gx[g + gw] + (gx[g + gw + 1] - gx[g + gw]) * fu; |
| 212 | float ddx = xa + (xb - xa) * fv; |
| 213 | |
| 214 | float ya = gy[g] + (gy[g + 1] - gy[g]) * fu; |
| 215 | float yb = gy[g + gw] + (gy[g + gw + 1] - gy[g + gw]) * fu; |
| 216 | float ddy = ya + (yb - ya) * fv; |
| 217 | |
| 218 | unsigned char px[4]; |
| 219 | sample_bilinearcxx . . $ `$anonymous_namespace_src/filter/squigglevision/squigglevision.cpp`/sample_bilinear(1300f42e0f80f9ac).(src, w, h, x + ddx, y + ddy, px); |
| 220 | |
| 221 | const size_t idx = ((size_t)y * w + x) * 4; |
| 222 | const unsigned char *orig = src + idx; |
| 223 | unsigned char *d = dst + idx; |
| 224 | |
| 225 | if (px[3] >= orig[3]) |
| 226 | { |
| 227 | d[0] = px[0]; |
| 228 | d[1] = px[1]; |
| 229 | d[2] = px[2]; |
| 230 | d[3] = px[3]; |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | d[0] = orig[0]; |
| 235 | d[1] = orig[1]; |
| 236 | d[2] = orig[2]; |
| 237 | d[3] = orig[3]; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void dispatchcxx . . $ squigglevision#dispatch(31ec06a0e995072d).(const unsigned char *src, unsigned char *dst, int w, int h) |
| 244 | { |
| 245 | #ifndef NO_FUTURE |
| 246 | unsigned int nthreads = stdcxx . . $ std/::threadcxx . . $ std/thread#::hardware_concurrencycxx . . $ std/thread#hardware_concurrency(db52760e79bf9054).(); |
| 247 | const int min_band = 64; |
| 248 | |
| 249 | if (nthreads > 1 && h >= 2 * min_band) |
| 250 | { |
| 251 | if (nthreads > (unsigned int)(h / min_band)) |
| 252 | { |
| 253 | nthreads = (unsigned int)(h / min_band); |
| 254 | } |
| 255 | |
| 256 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::futurecxx . . $ std/future#<void>> futurescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 257 | futures.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(nthreads); |
| 258 | |
| 259 | const int band = h / nthreads; |
| 260 | int y0 = 0; |
| 261 | for (unsigned int t = 0; t < nthreads; t++) |
| 262 | { |
| 263 | int y1 = (t == nthreads - 1) ? h : y0 + band; |
| 264 | futures.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(stdcxx . . $ std/::asynccxx . . $ std/async(198f4be7f5e581d8).(stdcxx . . $ std/::launchcxx . . $ std/launch#::asynccxx . . $ std/launch#async., |
| 265 | &squigglevisioncxx . . $ squigglevision#::render_rowscxx . . $ squigglevision#render_rows(5736d0456fc3aabc)., this, y0, y1, src, dst, w, h)); |
| 266 | y0 = y1; |
| 267 | } |
| 268 | for (size_t k = 0; k < futures.sizecxx . . $ std/vector#size(fe4d2d262d4164a8).(); k++) |
| 269 | { |
| 270 | futures[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).k].waitcxx . . $ std/__basic_future#wait(83f5f38750778ac8).(); |
| 271 | } |
| 272 | return; |
| 273 | } |
| 274 | #endif |
| 275 | render_rowscxx . . $ squigglevision#render_rows(5736d0456fc3aabc).(0, h, src, dst, w, h); |
| 276 | } |
| 277 | |
| 278 | double m_strengthcxx . . $ squigglevision#m_strength.; |
| 279 | double m_fpscxx . . $ squigglevision#m_fps.; |
| 280 | double m_scalecxx . . $ squigglevision#m_scale.; |
| 281 | |
| 282 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<float> m_gxcxx . . $ squigglevision#m_gx.; |
| 283 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<float> m_gycxx . . $ squigglevision#m_gy.; |
| 284 | int m_gwcxx . . $ squigglevision#m_gw. = 0; |
| 285 | int m_ghcxx . . $ squigglevision#m_gh. = 0; |
| 286 | float m_inv_cellcxx . . $ squigglevision#m_inv_cell. = 0; |
| 287 | float m_dispxcxx . . $ squigglevision#m_dispx. = 0; |
| 288 | float m_dispycxx . . $ squigglevision#m_dispy. = 0; |
| 289 | }; |
| 290 | |
| 291 | frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<squigglevisioncxx . . $ squigglevision#> plugincxx . . $ plugin.( |
| 292 | "squigglevision"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 293 | "Hand-drawn wobble via time-quantized noise displacement"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 294 | "Gabriel Lobo (https://olobo.xyz)"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 295 | 1, 0, |
| 296 | F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!); |