Debian Codegraph @

src/filter/pixels0rt/pixels0rt.c

raw symbols SCIP
1 /* pixels0rt.c
2 * Copyright (C) 2026 Asterleen ~ https://asterleen.com
3 * Implements a pixel sorting effect
4 *
5 * This file is a Frei0r plugin.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #include "frei0r.h"
28
29 #define S0RT_PARAM_THRESHOLD 0
30 #define S0RT_PARAM_DIRECTION 1
31 #define S0RT_PARAM_REVERSED 2
32 #define S0RT_PARAM_ALPHA_THR 3
33
34 typedef enum pixels0rt_direction
35 {
36 S0RT_DIRECTION_VER_TOP, // vertical, from top to bottom
37 S0RT_DIRECTION_VER_BOTTOM, // vertical, from bottom to top
38 S0RT_DIRECTION_HOR_LEFT, // horizontal, from left to right
39 S0RT_DIRECTION_HOR_RIGHT, // horizontal, from right to left
40
41 S0RT_DIRECTION_COUNT // total number of possible directions
42
43 } pixels0rt_direction_t;
44
45
46 typedef struct pixels0rt_instance
47 {
48 unsigned int width;
49 unsigned int height;
50
51 uint8_t *grayscale_px; // the frame in grayscale, will be used for radix sort
52
53 float threshold;
54 float alpha_threshold;
55 short int reversed;
56 pixels0rt_direction_t direction;
57
58 } pixels0rt_instance_t;
59
60 inline static uint8_t pixel_grayscale(uint32_t px)
61 {
62 uint8_t *pxSplit = (uint8_t *)&px;
63
64 return ((pxSplit[0] & 0xFF) +
65 (pxSplit[1] & 0xFF) +
66 (pxSplit[2] & 0xFF)) / 3;
67 }
68
69 inline static short pixel_alpha_threshold(uint32_t px, float threshold)
70 {
71 uint8_t *pxSplit = (uint8_t *)&px;
72
73 return pxSplit[3] / 0xFF >= threshold;
74 }
75
76 inline static double pixel_diff(uint32_t px1, uint32_t px2)
77 {
78 uint8_t *px1_split = (uint8_t *)&px1;
79 uint8_t *px2_split = (uint8_t *)&px2;
80
81 return (
82 abs(px1_split[0] - px2_split[0]) +
83 abs(px1_split[1] - px2_split[1]) +
84 abs(px1_split[2] - px2_split[2])
85 ) / 765.0;
86 }
87
88 int f0r_init()
89 {
90 return 1;
91 }
92
93 void f0r_deinit()
94 { /* no initialization required */ }
95
96 void f0r_get_plugin_info(f0r_plugin_info_t* pixels0rtInfo)
97 {
98 pixels0rtInfo->name = "pixels0rt";
99 pixels0rtInfo->author = "Asterleen";
100 pixels0rtInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER;
101 pixels0rtInfo->color_model = F0R_COLOR_MODEL_RGBA8888;
102 pixels0rtInfo->frei0r_version = FREI0R_MAJOR_VERSION;
103 pixels0rtInfo->major_version = 1;
104 pixels0rtInfo->minor_version = 0;
105 pixels0rtInfo->num_params = 4;
106 pixels0rtInfo->explanation = "Pixel sorting effect";
107 }
108
109 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
110 {
111 switch(param_index) {
112 case S0RT_PARAM_THRESHOLD:
113 {
114 info->name = "Threshold";
115 info->type = F0R_PARAM_DOUBLE;
116 info->explanation = "Relative size of the line that will be sorted";
117 break;
118 }
119
120 case S0RT_PARAM_DIRECTION:
121 {
122 info->name = "Direction";
123 info->type = F0R_PARAM_DOUBLE;
124 info->explanation = "Sorting direction";
125 break;
126 }
127
128 case S0RT_PARAM_REVERSED:
129 {
130 info->name = "Reversed";
131 info->type = F0R_PARAM_BOOL;
132 info->explanation = "Should be pixels sorted in reversed order";
133 break;
134 }
135
136 case S0RT_PARAM_ALPHA_THR:
137 {
138 info->name = "Transparency threshold";
139 info->type = F0R_PARAM_DOUBLE;
140 info->explanation = "What transparency level should be bypassed by the filter. 0 processes all pixels, 1 ignores all transparent pixels";
141 break;
142 }
143 }
144 }
145
146 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
147 {
148 pixels0rt_instance_t* inst = (pixels0rt_instance_t*)calloc(1, sizeof(*inst));
149 inst->width = width;
150 inst->height = height;
151 inst->threshold = 0.3;
152 inst->alpha_threshold = 1.0;
153 inst->direction = S0RT_DIRECTION_VER_TOP;
154 inst->reversed = 0;
155
156 inst->grayscale_px = malloc(width * height * sizeof(uint8_t));
157
158 return (f0r_instance_t)inst;
159 }
160
161 void f0r_destruct(f0r_instance_t instance)
162 {
163 pixels0rt_instance_t *inst = (pixels0rt_instance_t*)instance;
164
165 free(inst->grayscale_px);
166
167 free(instance);
168 }
169
170 void f0r_set_param_value(f0r_instance_t instance,
171 f0r_param_t param, int param_index)
172 {
173 assert(instance);
174 pixels0rt_instance_t *inst = (pixels0rt_instance_t*)instance;
175
176 switch (param_index)
177 {
178 case S0RT_PARAM_THRESHOLD:
179 {
180 inst->threshold = *((double*)param);
181 break;
182 }
183
184 case S0RT_PARAM_DIRECTION:
185 {
186 inst->direction = (pixels0rt_direction_t)(*((double*)param) * S0RT_DIRECTION_COUNT);
187 break;
188 }
189
190 case S0RT_PARAM_REVERSED:
191 {
192 inst->reversed = *((double*)param) >= 0.5;
193 break;
194 }
195
196 case S0RT_PARAM_ALPHA_THR:
197 {
198 inst->alpha_threshold = *((double*)param);
199 break;
200 }
201 }
202 }
203
204 void f0r_get_param_value(f0r_instance_t instance,
205 f0r_param_t param, int param_index)
206 {
207
208 assert(instance);
209 pixels0rt_instance_t *inst = (pixels0rt_instance_t*)instance;
210
211 switch (param_index)
212 {
213 case S0RT_PARAM_THRESHOLD:
214 {
215 *((double*)param) = inst->threshold;
216 break;
217 }
218
219 case S0RT_PARAM_DIRECTION:
220 {
221 *((double*)param) = inst->direction / S0RT_DIRECTION_COUNT;
222 break;
223 }
224
225 case S0RT_PARAM_REVERSED:
226 {
227 *((double*)param) = inst->reversed ? 1. : 0.;
228 break;
229 }
230
231 case S0RT_PARAM_ALPHA_THR:
232 {
233 *((double*)param) = inst->alpha_threshold;
234 break;
235 }
236 }
237
238 }
239
240 inline static void s0rt_line_horizontal(
241 const uint32_t *src,
242 uint32_t *dst,
243 uint8_t *gray,
244 unsigned int line,
245 unsigned int start,
246 unsigned int end)
247 {
248 unsigned int length = end - start;
249 unsigned int line_start = line + start;
250
251 uint32_t histogram[256] = {0};
252
253 for (int i = 0; i < length; i++)
254 histogram[gray[line_start + i]]++;
255
256 for (int i = 1; i < 256; i++)
257 histogram[i] += histogram[i - 1];
258
259 for (int i = length; i-- > 0;)
260 {
261 uint8_t key = gray[line_start + i];
262 dst[line_start + (--histogram[key])] = src[line_start + i];
263 }
264 }
265
266 inline static void s0rt_line_vertical(
267 const uint32_t *src,
268 uint32_t *dst,
269 uint8_t *gray,
270 unsigned int width,
271 unsigned int column,
272 unsigned int start,
273 unsigned int end)
274 {
275 unsigned int length = end - start;
276
277 uint32_t histogram[256] = {0};
278
279 for (int i = 0; i < length; i++)
280 histogram[gray[(start + i) * width + column]]++;
281
282 for (int i = 1; i < 256; i++)
283 histogram[i] += histogram[i - 1];
284
285 for (int i = length; i-- > 0;)
286 {
287 uint8_t key = gray[(start + i) * width + column];
288 dst[(start + (--histogram[key])) * width + column] = src[(start + i) * width + column];
289 }
290 }
291
292
293 void f0r_update(f0r_instance_t instance, double time,
294 const uint32_t* src, uint32_t* dst)
295 {
296 assert(instance);
297 pixels0rt_instance_t* inst = (pixels0rt_instance_t*)instance;
298
299 unsigned int size = inst->width * inst->height;
300
301 double px_diff = 0.0;
302 unsigned int line_idx;
303 unsigned int next_pos = 0;
304
305 if (inst->reversed) {
306 for (int i = 0; i < size; i++)
307 inst->grayscale_px[i] = 255 - pixel_grayscale(src[i]);
308 } else {
309 for (int i = 0; i < size; i++)
310 inst->grayscale_px[i] = pixel_grayscale(src[i]);
311 }
312
313 switch (inst->direction) {
314 case S0RT_DIRECTION_VER_TOP:
315 {
316 for (int column = 0; column < inst->width; column++) {
317 next_pos = 0;
318
319 for (int i = 0; i < inst->height; i++) {
320 if (pixel_alpha_threshold(src[i * inst->width + column], inst->alpha_threshold)) {
321 px_diff = pixel_diff(src[next_pos * inst->width + column], src[i * inst->width + column]);
322
323 if (px_diff > inst->threshold ||
324 (i < inst->height - 2 && !pixel_alpha_threshold(src[(i + 1) * inst->width + column], inst->alpha_threshold)) ||
325 i == inst->height - 1) {
326 s0rt_line_vertical(src, dst, inst->grayscale_px, inst->width, column, next_pos, i);
327 next_pos = i;
328 }
329 } else {
330 dst[i * inst->width + column] = src[i * inst->width + column];
331 next_pos = i;
332 }
333 }
334 }
335
336 break;
337 }
338
339 case S0RT_DIRECTION_VER_BOTTOM:
340 {
341 for (int column = 0; column < inst->width; column++) {
342 next_pos = inst->height - 1;
343
344 for (int i = inst->height - 1; i >= 0; i--) {
345
346 if (pixel_alpha_threshold(src[i * inst->width + column], inst->alpha_threshold)) {
347 px_diff = pixel_diff(src[next_pos * inst->width + column], src[i * inst->width + column]);
348
349 if (px_diff > inst->threshold ||
350 (i > 0 && !pixel_alpha_threshold(src[(i - 1) * inst->width + column], inst->alpha_threshold)) ||
351 i == 0) {
352 s0rt_line_vertical(src, dst, inst->grayscale_px, inst->width, column, i, next_pos);
353 next_pos = i;
354 }
355 } else {
356 dst[i * inst->width + column] = src[i * inst->width + column];
357 next_pos = i;
358 }
359 }
360 }
361
362 break;
363 }
364
365
366 case S0RT_DIRECTION_HOR_LEFT:
367 {
368 for (int line = 0; line < inst->height; line++) {
369 next_pos = 0;
370 line_idx = line * inst->width;
371
372 for (int i = 0; i < inst->width; i++) {
373 if (pixel_alpha_threshold(src[line_idx + i], inst->alpha_threshold)) {
374 px_diff = pixel_diff(src[line_idx + next_pos], src[line_idx + i]);
375
376 if (px_diff > inst->threshold ||
377 (i < inst->width - 2 && !pixel_alpha_threshold(src[line_idx + i + 1], inst->alpha_threshold)) ||
378 i == inst->width - 1) {
379 s0rt_line_horizontal(src, dst, inst->grayscale_px, line_idx, next_pos, i);
380 next_pos = i;
381 }
382 } else {
383 dst[line_idx + i] = src[line_idx + i];
384 next_pos = i;
385 }
386 }
387 }
388
389 break;
390 }
391
392 case S0RT_DIRECTION_HOR_RIGHT:
393 {
394 for (int line = 0; line < inst->height; line++) {
395 next_pos = inst->width - 1;
396 line_idx = line * inst->width;
397
398 for (int i = inst->width - 1; i >= 0; i--) {
399 if (pixel_alpha_threshold(src[line_idx + i], inst->alpha_threshold)) {
400 px_diff = pixel_diff(src[line_idx + next_pos], src[line_idx + i]);
401
402 if (px_diff > inst->threshold ||
403 (i > 0 && !pixel_alpha_threshold(src[line_idx + i - 1], inst->alpha_threshold)) ||
404 i == 0) {
405 s0rt_line_horizontal(src, dst, inst->grayscale_px, line_idx, i, next_pos);
406 next_pos = i;
407 }
408 } else {
409 dst[line_idx + i] = src[line_idx + i];
410 next_pos = i;
411 }
412 }
413 }
414
415 break;
416 }
417 }
418 }