-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpixelsort.c
376 lines (325 loc) · 10.9 KB
/
pixelsort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* pixelsort -- a GEGL operation that implements a pixel sorting effect
* Copyright (C) 2021 Evergreen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define GETTEXT_PACKAGE "pixelsort"
#include <glib/gi18n-lib.h>
#ifdef GEGL_PROPERTIES
enum_start (gegl_pixelsort_key)
enum_value (GEGL_PIXELSORT_KEY_LUMINANCE, "luminance", "Luminance")
enum_value (GEGL_PIXELSORT_KEY_RGB_MAX, "rgb-maximum", "RGB Maximum")
enum_value (GEGL_PIXELSORT_KEY_HUE, "hue", "Hue")
enum_value (GEGL_PIXELSORT_KEY_SATURATION, "saturation", "Saturation")
enum_value (GEGL_PIXELSORT_KEY_RED, "red", "Red")
enum_value (GEGL_PIXELSORT_KEY_GREEN, "green", "Green")
enum_value (GEGL_PIXELSORT_KEY_BLUE, "blue", "Blue")
enum_end (GeglPixelsortKey)
property_enum (sort_key, "Sort Key",
GeglPixelsortKey, gegl_pixelsort_key,
GEGL_PIXELSORT_KEY_LUMINANCE)
description ("Property used to sort the pixels")
property_enum (threshold_key, "Threshold Key",
GeglPixelsortKey, gegl_pixelsort_key,
GEGL_PIXELSORT_KEY_LUMINANCE)
description ("Property used to determine what to sort in the current row/column")
property_double (threshold, "Threshold", 0.1)
description ("Determines how much of each row/column is sorted")
value_range (0.0, 1.0)
property_boolean (under_threshold, "Sort under threshold", FALSE)
description ("Sort pixels under threshold value instead of above it")
property_enum (direction, "Sort direction",
GeglOrientation, gegl_orientation,
GEGL_ORIENTATION_HORIZONTAL)
property_boolean (reverse_order, "Reverse sort order", FALSE)
description ("Reverse sort order")
property_seed (seed, "Random seed", rand)
#else
#define GEGL_OP_FILTER
#define GEGL_OP_NAME pixelsort
#define GEGL_OP_C_SOURCE pixelsort.c
#include "gegl-op.h"
static gdouble
get_key (gfloat pixel[4], GeglPixelsortKey key)
{
gfloat max;
gfloat min;
switch (key)
{
case GEGL_PIXELSORT_KEY_LUMINANCE:
return 0.2126 * pixel[0] + 0.7152 * pixel[1] + 0.0722 * pixel[2];
case GEGL_PIXELSORT_KEY_RGB_MAX:
return MAX (MAX (pixel[0], pixel[1]), pixel[2]);
case GEGL_PIXELSORT_KEY_HUE:
min = MIN (MIN (pixel[0], pixel[1]), pixel[2]);
max = MAX (MAX (pixel[0], pixel[1]), pixel[2]);
if (min == max)
return 0;
gdouble hue = 0.0;
if (max == pixel[0])
{
hue = (pixel[1] - pixel[2]) / (max - min);
}
else if (max == pixel[1])
{
hue = 2 + (pixel[2] - pixel[0]) / (max - min);
}
else
{
hue = 4 + (pixel[0] - pixel[1]) / (max - min);
}
if (hue < 0)
hue += 6;
hue /= 6.0;
return hue;
case GEGL_PIXELSORT_KEY_SATURATION:
min = MIN (MIN (pixel[0], pixel[1]), pixel[2]);
max = MAX (MAX (pixel[0], pixel[1]), pixel[2]);
gdouble lightness = (min + max) / 2;
if (min == max)
return 0;
return (min - max) / (1 - ABS ((lightness * 2) - 1));
case GEGL_PIXELSORT_KEY_RED:
return pixel[0];
case GEGL_PIXELSORT_KEY_GREEN:
return pixel[1];
case GEGL_PIXELSORT_KEY_BLUE:
return pixel[2];
default:
return 0.0;
}
}
static inline void
swap_rgba_pixels(gfloat (*pixels)[4], gint a, gint b)
{
gfloat temp_r = pixels[a][0];
gfloat temp_g = pixels[a][1];
gfloat temp_b = pixels[a][2];
gfloat temp_a = pixels[a][3];
pixels[a][0] = pixels[b][0];
pixels[a][1] = pixels[b][1];
pixels[a][2] = pixels[b][2];
pixels[a][3] = pixels[b][3];
pixels[b][0] = temp_r;
pixels[b][1] = temp_g;
pixels[b][2] = temp_b;
pixels[b][3] = temp_a;
}
static void
merge (gfloat (*in)[4], gint left, gint right, gint end, gfloat (*out)[4],
gboolean reverse, GeglPixelsortKey key)
{
gint i = left;
gint j = right;
gboolean comp;
for (gint k = left; k < end; k++)
{
comp = reverse ^ (get_key (in[i], key) <= get_key (in[j], key));
if (i < right && (j >= end || comp))
{
out[k][0] = in[i][0];
out[k][1] = in[i][1];
out[k][2] = in[i][2];
out[k][3] = in[i][3];
i++;
}
else
{
out[k][0] = in[j][0];
out[k][1] = in[j][1];
out[k][2] = in[j][2];
out[k][3] = in[j][3];
j++;
}
}
}
static void
stable_sort (gfloat (*pixels)[4], gfloat (*work)[4], gint start, gint end,
gboolean reverse, GeglPixelsortKey key)
{
gint n = (end - start) + 1;
for (gint width = 1; width < n; width *= 2)
{
for (gint i = start; i < end; i += width * 2)
{
merge (pixels, i, MIN (i + width, end), MIN (i + width * 2, end),
work, reverse, key);
}
for (gint i = start; i < end; i++)
{
pixels[i][0] = work[i][0];
pixels[i][1] = work[i][1];
pixels[i][2] = work[i][2];
pixels[i][3] = work[i][3];
}
}
}
static void
prepare (GeglOperation *operation)
{
#if GEGL_MAJOR_VERSION >= 4
const Babl *space = gegl_operation_get_source_space (operation, "input");
gegl_operation_set_format (operation, "input", babl_format_with_space ("RGBA float", space));
gegl_operation_set_format (operation, "output", babl_format_with_space ("RGBA float", space));
#else
gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
#endif
}
static GeglRectangle
get_cached_region (GeglOperation *self,
const GeglRectangle *roi)
{
const GeglRectangle *in_rect
= gegl_operation_source_get_bounding_box (self, "input");
if (in_rect && !gegl_rectangle_is_infinite_plane (in_rect))
return *in_rect;
return *roi;
}
static GeglRectangle
get_required_for_output (GeglOperation *self,
const gchar *input_pad,
const GeglRectangle *roi)
{
return get_cached_region (self, roi);
}
static gboolean
process (GeglOperation *operation,
GeglBuffer *input,
GeglBuffer *output,
const GeglRectangle *result,
gint level)
{
GeglProperties *o = GEGL_PROPERTIES (operation);
const Babl *format = gegl_operation_get_format (operation, "output");
GeglPixelsortKey sort_key = o->sort_key;
GeglPixelsortKey threshold_key = o->threshold_key;
gdouble threshold = o->threshold;
gboolean under_threshold = o->under_threshold;
gint num_lines, length, line_num, j;
GeglRectangle line_rect;
if (o->direction == GEGL_ORIENTATION_HORIZONTAL)
{
num_lines = result->height;
length = result->width;
line_rect.width = length;
line_rect.height = 1;
}
else
{
num_lines = result->width;
length = result->height;
line_rect.width = 1;
line_rect.height = length;
}
gfloat (*line_buf)[4] = (gfloat (*)[4]) g_new (gfloat, length * 4);
gfloat (*work_buf)[4] = (gfloat (*)[4]) g_new (gfloat, length * 4);
line_rect.x = result->x;
line_rect.y = result->y;
for (line_num = 0; line_num < num_lines; line_num++)
{
gegl_buffer_get (input, &line_rect, 1.0, format, (gfloat *) line_buf,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
gint start = 0;
gboolean in_thresh = FALSE;
for (j = 0; j < length; j++)
{
gdouble key = get_key (line_buf[j], threshold_key);
gboolean comp = under_threshold ^ (key >= threshold);
if (comp && !in_thresh)
{
start = j;
in_thresh = TRUE;
}
else if ((!comp || j == length - 1) && in_thresh)
{
stable_sort (line_buf, work_buf, start, j + 1, o->reverse_order,
sort_key);
in_thresh = FALSE;
}
}
gegl_buffer_set (output, &line_rect, 0, format, (gfloat *) line_buf,
GEGL_AUTO_ROWSTRIDE);
if (o->direction == GEGL_ORIENTATION_HORIZONTAL)
{
line_rect.y++;
}
else
{
line_rect.x++;
}
}
g_free(line_buf);
g_free(work_buf);
return TRUE;
}
static gboolean
operation_process (GeglOperation *operation,
GeglOperationContext *context,
const gchar *output_prop,
const GeglRectangle *result,
gint level)
{
gboolean success = FALSE;
const GeglRectangle *in_rect
= gegl_operation_source_get_bounding_box (operation, "input");
if (in_rect && gegl_rectangle_is_infinite_plane (in_rect))
{
gpointer in = gegl_operation_context_get_object (context, "input");
gegl_operation_context_take_object (context, "output",
g_object_ref (G_OBJECT (in)));
return TRUE;
}
else
{
GeglOperationFilterClass *klass;
GeglBuffer *input;
GeglBuffer *output;
if (strcmp (output_prop, "output"))
{
g_warning ("requested processing of %s pad on a filter",
output_prop);
return FALSE;
}
input
= (GeglBuffer *)gegl_operation_context_dup_object (context, "input");
output = gegl_operation_context_get_output_maybe_in_place (
operation, context, input, result);
klass = GEGL_OPERATION_FILTER_GET_CLASS (operation);
success = klass->process (operation, input, output, result, level);
g_clear_object (&input);
}
return success;
}
static void
gegl_op_class_init (GeglOpClass *klass)
{
GeglOperationClass *operation_class;
GeglOperationFilterClass *filter_class;
operation_class = GEGL_OPERATION_CLASS (klass);
filter_class = GEGL_OPERATION_FILTER_CLASS (klass);
operation_class->prepare = prepare;
operation_class->process = operation_process;
operation_class->get_required_for_output = get_required_for_output;
operation_class->get_cached_region = get_cached_region;
filter_class->process = process;
gegl_operation_class_set_keys (operation_class,
"name", "gegl:pixelsort",
"title", "Pixel Sort",
"categories", "distort",
"license", "GPL3+",
"description", "Sorts pixels by different properties within a threshold",
NULL);
}
#endif