Debian Codegraph @

src/filter/squigglevision/squigglevision.cpp

raw symbols SCIP
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"
21 #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_PI
33 #define M_PI 3.14159265358979323846
34 #endif
35
36 #ifndef M_E
37 #define M_E 2.71828182845904523536
38 #endif
39
40 namespace
41 {
42
43 float hash2(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_noise(float x, float y)
52 {
53 int xi = (int)std::floor(x);
54 int yi = (int)std::floor(y);
55 float fx = x - xi;
56 float fy = y - yi;
57
58 float a = hash2(xi, yi);
59 float b = hash2(xi + 1, yi);
60 float c = hash2(xi, yi + 1);
61 float d = hash2(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_bilinear(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] = CLAMP0255((int32_t)(pr * inv + 0.5f));
118 o[1] = CLAMP0255((int32_t)(pg * inv + 0.5f));
119 o[2] = CLAMP0255((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 squigglevision : public frei0r::filter
132 {
133 public:
134 squigglevision(unsigned int width, unsigned int height)
135 {
136 (void)width;
137 (void)height;
138
139 register_param(m_strength, "Strength",
140 "Displacement amount, normalized 0..1 mapped to 0..100 percent (8 percent is a moderate default).");
141 register_param(m_fps, "FPS",
142 "Squiggle updates per second, normalized 0..1 mapped to 0..30 fps (0 = frozen; 6 fps is a moderate default).");
143 register_param(m_scale, "Scale",
144 "Noise cell size, normalized 0..1 mapped to 4..128 px (about 23 px is a moderate default).");
145
146 m_strength = 0.08;
147 m_fps = 0.2;
148 m_scale = 0.15;
149 }
150
151 void update(double time, uint32_t *out, const uint32_t *in)
152 {
153 const int w = (int)width;
154 const int h = (int)height;
155
156 const double disp = m_strength * 5.0 * 0.005;
157 const double fps = m_fps * 30.0;
158 const double cell = 4.0 + m_scale * 124.0;
159
160 m_inv_cell = (float)(1.0 / cell);
161 m_dispx = (float)(disp * w);
162 m_dispy = (float)(disp * h);
163
164 const double frame = std::floor(time * fps);
165 build_grid(w, h, (float)(frame * M_PI), (float)(frame * M_E));
166
167 dispatch((const unsigned char *)in, (unsigned char *)out, w, h);
168 }
169
170 private:
171 void build_grid(int w, int h, float offx, float offy)
172 {
173 m_gw = (int)(w * m_inv_cell) + 2;
174 m_gh = (int)(h * m_inv_cell) + 2;
175 m_gx.resize((size_t)m_gw * m_gh);
176 m_gy.resize((size_t)m_gw * m_gh);
177
178 for (int j = 0; j < m_gh; j++)
179 {
180 for (int i = 0; i < m_gw; i++)
181 {
182 float angle = value_noise(i + offx, j + offy) * 4.0f * (float)M_PI;
183 m_gx[(size_t)j * m_gw + i] = std::cos(angle) * m_dispx;
184 m_gy[(size_t)j * m_gw + i] = std::sin(angle) * m_dispy;
185 }
186 }
187 }
188
189 void render_rows(int y0, int y1, const unsigned char *src, unsigned char *dst,
190 int w, int h) const
191 {
192 const float *gx = m_gx.data();
193 const float *gy = m_gy.data();
194 const int gw = m_gw;
195 const float inv_cell = 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_bilinear(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 dispatch(const unsigned char *src, unsigned char *dst, int w, int h)
244 {
245 #ifndef NO_FUTURE
246 unsigned int nthreads = std::thread::hardware_concurrency();
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 std::vector<std::future<void>> futures;
257 futures.reserve(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_back(std::async(std::launch::async,
265 &squigglevision::render_rows, this, y0, y1, src, dst, w, h));
266 y0 = y1;
267 }
268 for (size_t k = 0; k < futures.size(); k++)
269 {
270 futures[k].wait();
271 }
272 return;
273 }
274 #endif
275 render_rows(0, h, src, dst, w, h);
276 }
277
278 double m_strength;
279 double m_fps;
280 double m_scale;
281
282 std::vector<float> m_gx;
283 std::vector<float> m_gy;
284 int m_gw = 0;
285 int m_gh = 0;
286 float m_inv_cell = 0;
287 float m_dispx = 0;
288 float m_dispy = 0;
289 };
290
291 frei0r::construct<squigglevision> plugin(
292 "squigglevision",
293 "Hand-drawn wobble via time-quantized noise displacement",
294 "Gabriel Lobo (https://olobo.xyz)",
295 1, 0,
296 F0R_COLOR_MODEL_RGBA8888);