-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.cpp
More file actions
658 lines (595 loc) · 24.7 KB
/
Copy pathfilters.cpp
File metadata and controls
658 lines (595 loc) · 24.7 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
// ReSharper disable once CppUnusedIncludeDirective
#include <pybind11/stl.h>
#include <array>
#include <stdexcept>
#include <utility>
#include <numbers>
#include <random>
#ifdef _OPENMP
// ReSharper disable once CppUnusedIncludeDirective
#include <omp.h>
#endif
using namespace pybind11::literals;
namespace py = pybind11;
#ifndef SSIZE_T_DEFINED
using ssize_t = Py_ssize_t;
#define SSIZE_T_DEFINED
#endif
int THREADS = 0;
long long PROGRESS = 0;
long long MAX_PROGRESS = 0;
class Image { // Made by ChatGPT
public:
explicit Image(py::array_t<uint8_t> im, const bool require_write=false)
{
if (require_write) {
const py::object np = py::module_::import("numpy");
if (!py::cast<py::bool_>(im.attr("flags").attr("writeable"))) {
py::print("[C++] Array not writeable - making a copy. Consider making copy=True");
im = im.attr("copy")();
}
}
im_ = std::move(im);
const auto buf = im_.request();
strides_ = {buf.strides[0], buf.strides[1]};
if (buf.ndim != 3)
throw std::runtime_error("Expected 3D array (H, W, C)");
height_ = buf.shape[0];
width_ = buf.shape[1];
channels_ = buf.shape[2];
data_ = static_cast<uint8_t*>(buf.ptr);
}
[[nodiscard]] ssize_t width() const { return width_; }
[[nodiscard]] ssize_t height() const { return height_; }
[[nodiscard]] ssize_t channels() const { return channels_; }
[[nodiscard]] std::array<uint8_t, 4> get_pixel(const ssize_t y, const ssize_t x) const {
if (y < 0 || y >= height_ || x < 0 || x >= width_)
throw std::runtime_error("Invalid pixel coordinates");
const auto* ptr = reinterpret_cast<const uint8_t*>(
reinterpret_cast<const char*>(data_) + y * strides_[0] + x * strides_[1]
);
std::array<uint8_t, 4> pixel{0, 0, 0, 255}; // default alpha = 255
for (ssize_t c = 0; c < std::min<ssize_t>(channels_, 4); ++c)
pixel[c] = ptr[c];
return pixel;
}
void set_pixel(const ssize_t y, const ssize_t x, const std::array<uint8_t, 4>& rgba) const {
if (y < 0 || y >= height_ || x < 0 || x >= width_)
throw std::runtime_error("Invalid pixel coordinates");
auto* ptr = reinterpret_cast<uint8_t*>(
reinterpret_cast<char*>(data_) + y * strides_[0] + x * strides_[1]
);
// Only modify up to the available number of channels
const ssize_t n = std::min<ssize_t>(channels_, 4);
for (ssize_t c = 0; c < n; ++c)
ptr[c] = rgba[c];
}
void crop(const ssize_t left, const ssize_t top, const ssize_t right, const ssize_t bottom) {
if (top < 0 || left < 0 || bottom > height_ || right > width_ || bottom <= top || right <= left)
throw std::runtime_error("Invalid crop coordinates");
const auto buf = im_.request();
const auto* base_ptr = static_cast<uint8_t*>(buf.ptr);
const auto* cropped_ptr = base_ptr + (top * width_ + left) * channels_;
const std::vector<ssize_t> shape = {bottom - top, right - left, channels_};
const std::vector<ssize_t> strides = buf.strides;
strides_ = {strides[0], strides[1]};
auto view = py::array_t<uint8_t>(
shape,
strides,
cropped_ptr,
im_
);
try {
view.attr("setflags")(py::arg("write") = true);
} catch (...) {
py::print("[C++] Could not set writeable flag; proceeding anyway");
}
im_ = std::move(view);
width_ = shape[1];
height_ = shape[0];
data_ = static_cast<uint8_t*>(im_.mutable_data());
}
[[nodiscard]] py::array_t<uint8_t> array() const { return im_; }
[[nodiscard]] Image copy() const {
// Create a full NumPy array copy
py::array_t<uint8_t> im_copy = im_.attr("copy")();
// Construct a new Image from it (writeable guaranteed)
return Image(std::move(im_copy), /*require_write=*/false);
}
private:
py::array_t<uint8_t> im_;
ssize_t width_, height_, channels_;
uint8_t* data_;
std::array<ssize_t, 2> strides_{};
}; // Made by ChatGPT
py::array_t<uint8_t> gray(py::array_t<uint8_t> im, const bool use_alpha=false) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgba = img.get_pixel(i, j);
if (use_alpha) {
const auto g = static_cast<uint8_t>((rgba[0] + rgba[1] + rgba[2] + rgba[3]) / 4);
img.set_pixel(i, j, {g, g, g, g});
} else {
const auto g = static_cast<uint8_t>((rgba[0] + rgba[1] + rgba[2]) / 3);
img.set_pixel(i, j, {g, g, g, rgba[3]});
}
}
}
return im;
}
py::array_t<uint8_t> black_and_white(py::array_t<uint8_t> im, const uint8_t threshold=127, const bool reverse=false) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgba = img.get_pixel(i, j);
uint8_t g;
if ((rgba[0] + rgba[1] + rgba[2]) / 3 > threshold) g = 255;
else g = 0;
if (reverse) g = g == 255 ? 0 : 255;
img.set_pixel(i, j, {g, g, g, 255});
}
}
return im;
}
py::array_t<uint8_t> reverse(py::array_t<uint8_t> im, const bool use_alpha=false) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgba = img.get_pixel(i, j);
rgba[0] = 255 - rgba[0];
rgba[1] = 255 - rgba[1];
rgba[2] = 255 - rgba[2];
if (use_alpha)
rgba[3] = 255 - rgba[3];
img.set_pixel(i, j, rgba);
}
}
return im;
}
py::array_t<uint8_t> brightness_noise(py::array_t<uint8_t> im, const int level=100, const bool use_alpha=false) {
const Image img(im);
static thread_local std::mt19937 gen(std::random_device{}());
std::uniform_int_distribution<int> dist(-level, level);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgba = img.get_pixel(i, j);
const auto add = dist(gen);
const uint8_t r = std::max(std::min(rgba[0] + add, 255), 0);
const uint8_t g = std::max(std::min(rgba[1] + add, 255), 0);
const uint8_t b = std::max(std::min(rgba[2] + add, 255), 0);
uint8_t a = rgba[3];
if (use_alpha)
a = std::max(std::min(rgba[3] + add, 255), 0);
img.set_pixel(i, j, {r, g, b, a});
}
}
return im;
}
py::array_t<uint8_t> color_noise(py::array_t<uint8_t> im, const int level=100, const bool use_alpha=false) {
const Image img(im);
static thread_local std::mt19937 gen(std::random_device{}());
std::uniform_int_distribution<int> dist(-level, level);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgba = img.get_pixel(i, j);
const uint8_t r = std::max(std::min(rgba[0] + dist(gen), 255), 0);
const uint8_t g = std::max(std::min(rgba[1] + dist(gen), 255), 0);
const uint8_t b = std::max(std::min(rgba[2] + dist(gen), 255), 0);
uint8_t a = rgba[3];
if (use_alpha)
a = std::max(std::min(rgba[3] + dist(gen), 255), 0);
img.set_pixel(i, j, {r, g, b, a});
}
}
return im;
}
py::array_t<uint8_t> corners(py::array_t<uint8_t> im, const std::array<std::array<uint8_t, 4>, 4> colors) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgb = img.get_pixel(i, j);
std::array<uint8_t, 4> color; // NOLINT(*-pro-type-member-init)
if (j < img.width() / 2) {
if (i < img.height() / 2) color = colors[0];
else color = colors[1];
} else {
if (i < img.height() / 2) color = colors[2];
else color = colors[3];
}
const uint8_t r = std::min(rgb[0] + color[0], 255);
const uint8_t g = std::min(rgb[1] + color[1], 255);
const uint8_t b = std::min(rgb[2] + color[2], 255);
const uint8_t a = std::min(rgb[3] + color[3], 255);
img.set_pixel(i, j, {r, g, b, a});
}
}
return im;
}
py::array_t<uint8_t> mirror(py::array_t<uint8_t> im) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = img.width() / 2 + img.width() % 2; j < img.width(); ++j) {
img.set_pixel(i, j, img.get_pixel(i, img.width() - j));
}
}
return im;
}
py::array_t<uint8_t> lines(const py::array_t<uint8_t> &im, const ssize_t width=10) {
Image img(im, true);
img.crop(0, 0, img.width() / width * width, img.height());
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width() / width / 2 * width; ++j) {
const ssize_t k = img.width() - j / width * width - width + j % width - img.width() % width;
const auto rgb = img.get_pixel(i, j);
img.set_pixel(i, j, img.get_pixel(i, k));
img.set_pixel(i, k, rgb);
}
}
return img.array();
}
py::array_t<uint8_t> turn(py::array_t<uint8_t> im, const int count) {
const Image img(im);
for (ssize_t i = 0; i < img.height() / 2; ++i) {
for (ssize_t j = 0; j < img.width() / 2; ++j) {
if (const ssize_t i1 = i / (img.height() / count / 2), j1 = j / (img.width() / count / 2);
(i1 % 2 == 0 && i1 <= j1) || (j1 % 2 == 0 && j1 <= i1)) {
auto rgb = img.get_pixel(i, j);
img.set_pixel(i, j, img.get_pixel(img.height() - i - 1, img.width() - j - 1));
img.set_pixel(img.height() - i - 1, img.width() - j - 1, rgb);
rgb = img.get_pixel(img.height() - i - 1, j);
img.set_pixel(img.height() - i - 1, j, img.get_pixel(i, img.width() - j - 1));
img.set_pixel(i, img.width() - j - 1, rgb);
}
}
}
return im;
}
py::array_t<uint8_t> curtain(py::array_t<uint8_t> im) {
const Image img(im);
for (ssize_t i = img.height() - 2; i >= 0; --i) {
for (ssize_t j = 0; j < img.width(); ++j) {
img.set_pixel(i, j, img.get_pixel(i + 1, j));
}
}
return im;
}
py::array_t<uint8_t> dots(py::array_t<uint8_t> im, py::array_t<uint8_t> im2) {
const Image img(im), img2(std::move(im2));
for (ssize_t i = 0; i < std::min(img.height(), img2.height()); ++i) {
for (ssize_t j = 0; j < std::min(img.width(), img2.width()); ++j) {
if (i % 2 != 0 && j % 2 != 0) {
img.set_pixel(i, j, img2.get_pixel(i, j));
}
}
}
return im;
}
py::array_t<uint8_t> channel_swap(py::array_t<uint8_t> im, const int first, const int second, const bool reverse=false) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
auto rgba = img.get_pixel(i, j);
std::swap(rgba[first], rgba[second]);
if (reverse) {
rgba[first] = 255 - rgba[first];
rgba[second] = 255 - rgba[second];
}
img.set_pixel(i, j, rgba);
}
}
return im;
}
py::array_t<uint8_t> matrix(py::array_t<uint8_t> im, const std::vector<std::vector<double>> &m) {
#ifdef _OPENMP
omp_set_num_threads(THREADS);
#endif
const Image orig(im);
const auto img = orig.copy();
py::gil_scoped_release release;
MAX_PROGRESS = img.height();
PROGRESS = 0;
#pragma omp parallel for schedule(dynamic)
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
double r = 0, g = 0, b = 0, a = 0, c = 0;
for (ssize_t i1 = 0; i1 < m.size(); ++i1) {
const ssize_t idx = i1 - static_cast<ssize_t>(m.size()) / 2;
if (i + idx < 0 || i + idx >= img.height())
continue;
for (ssize_t j1 = 0; j1 < m[i1].size(); ++j1) {
const ssize_t jdx = j1 - static_cast<ssize_t>(m[i1].size()) / 2;
if (i + idx < 0 || i + idx >= img.height() || j + jdx < 0 || j + jdx >= img.width())
continue;
const auto rgba = img.get_pixel(i + idx, j + jdx);
r += rgba[0] * m[i1][j1];
g += rgba[1] * m[i1][j1];
b += rgba[2] * m[i1][j1];
a += rgba[3] * m[i1][j1];
c += m[i1][j1];
}
}
r = std::max(std::min(r / c, 255.), 0.);
g = std::max(std::min(g / c, 255.), 0.);
b = std::max(std::min(b / c, 255.), 0.);
a = std::max(std::min(a / c, 255.), 0.);
orig.set_pixel(i, j, {static_cast<uint8_t>(r), static_cast<uint8_t>(g), static_cast<uint8_t>(b),
static_cast<uint8_t>(a)});
}
#pragma omp atomic
++PROGRESS;
}
MAX_PROGRESS = 0;
PROGRESS = 0;
py::gil_scoped_acquire acquire;
return im;
}
py::array_t<uint8_t> blur(py::array_t<uint8_t> im, const double level=1) {
const ssize_t size = static_cast<int>(level * 6 + 1);
std::vector<std::vector<double>> m;
double summ = 0;
for (ssize_t i = -(size / 2); i < size / 2 + 1; ++i) {
std::vector<double> v;
for (ssize_t j = -(size / 2); j < size / 2 + 1; ++j) {
const double n = 1 / (level * std::sqrt(2 * std::numbers::pi)) *
exp(static_cast<double>(-(i * i + j * j)) / (2 * level * level));
summ += n;
v.push_back(n);
}
m.push_back(v);
}
for (auto &i : m) {
for (auto &j : i) {
j /= summ;
}
}
matrix(im, m);
return im;
}
py::array_t<uint8_t> sharp(py::array_t<uint8_t> im, const double level=9) {
const std::vector<std::vector<double>> m = {
{-1, -1, -1},
{-1, level, -1},
{-1, -1, -1}
};
matrix(im, m);
return im;
}
py::array_t<uint8_t> median(py::array_t<uint8_t> im, const ssize_t size) {
#ifdef _OPENMP
omp_set_num_threads(THREADS);
#endif
const Image orig(im);
const auto img = orig.copy();
py::gil_scoped_release release;
MAX_PROGRESS = img.height();
PROGRESS = 0;
#pragma omp parallel for schedule(dynamic)
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
int c = 0;
std::vector<uint8_t> r(size * size, 255), g(size * size, 255), b(size * size, 255), a(size * size, 255);
for (ssize_t i1 = 0; i1 < size; ++i1) {
const ssize_t idx = i1 - size / 2;
if (i + idx < 0 || i + idx >= img.height())
continue;
for (ssize_t j1 = 0; j1 < size; ++j1) {
const ssize_t jdx = j1 - size / 2;
if (i + idx < 0 || i + idx >= img.height() || j + jdx < 0 || j + jdx >= img.width())
continue;
const auto rgba = img.get_pixel(i + idx, j + jdx);
r[c] = rgba[0];
g[c] = rgba[1];
b[c] = rgba[2];
a[c] = rgba[3];
c += 1;
}
}
std::ranges::sort(r);
std::ranges::sort(g);
std::ranges::sort(b);
std::ranges::sort(a);
orig.set_pixel(i, j, {r[(c - 1) / 2], g[(c - 1) / 2], b[(c - 1) / 2], a[(c - 1) / 2]});
}
#pragma omp atomic
++PROGRESS;
}
MAX_PROGRESS = 0;
PROGRESS = 0;
py::gil_scoped_acquire acquire;
return im;
}
py::array_t<uint8_t> bright(py::array_t<uint8_t> im, const ssize_t size) {
#ifdef _OPENMP
omp_set_num_threads(THREADS);
#endif
const Image orig(im);
const auto img = orig.copy();
py::gil_scoped_release release;
MAX_PROGRESS = img.height();
PROGRESS = 0;
#pragma omp parallel for schedule(dynamic)
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
int c = 0;
std::vector<uint8_t> r(size * size), g(size * size), b(size * size), a(size * size);
for (ssize_t i1 = 0; i1 < size; ++i1) {
const ssize_t idx = i1 - size / 2;
if (i + idx < 0 || i + idx >= img.height())
continue;
for (ssize_t j1 = 0; j1 < size; ++j1) {
const ssize_t jdx = j1 - size / 2;
if (i + idx < 0 || i + idx >= img.height() || j + jdx < 0 || j + jdx >= img.width())
continue;
const auto rgba = img.get_pixel(i + idx, j + jdx);
r[c] = rgba[0];
g[c] = rgba[1];
b[c] = rgba[2];
a[c] = rgba[3];
c += 1;
}
}
orig.set_pixel(i, j, {*std::ranges::max_element(r), *std::ranges::max_element(g),
*std::ranges::max_element(b), *std::ranges::max_element(a)});
}
#pragma omp atomic
++PROGRESS;
}
MAX_PROGRESS = 0;
PROGRESS = 0;
py::gil_scoped_acquire acquire;
return im;
}
py::array_t<uint8_t> dim(py::array_t<uint8_t> im, const ssize_t size) {
#ifdef _OPENMP
omp_set_num_threads(THREADS);
#endif
const Image orig(im);
const auto img = orig.copy();
py::gil_scoped_release release;
MAX_PROGRESS = img.height();
PROGRESS = 0;
#pragma omp parallel for schedule(dynamic)
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
int c = 0;
std::vector<uint8_t> r(size * size, 255), g(size * size, 255), b(size * size, 255), a(size * size, 255);
for (ssize_t i1 = 0; i1 < size; ++i1) {
const ssize_t idx = i1 - size / 2;
if (i + idx < 0 || i + idx >= img.height())
continue;
for (ssize_t j1 = 0; j1 < size; ++j1) {
const ssize_t jdx = j1 - size / 2;
if (i + idx < 0 || i + idx >= img.height() || j + jdx < 0 || j + jdx >= img.width())
continue;
const auto rgba = img.get_pixel(i + idx, j + jdx);
r[c] = rgba[0];
g[c] = rgba[1];
b[c] = rgba[2];
a[c] = rgba[3];
c += 1;
}
}
orig.set_pixel(i, j, {*std::ranges::min_element(r), *std::ranges::min_element(g),
*std::ranges::min_element(b), *std::ranges::min_element(a)});
}
#pragma omp atomic
++PROGRESS;
}
MAX_PROGRESS = 0;
PROGRESS = 0;
py::gil_scoped_acquire acquire;
return im;
}
py::array_t<uint8_t> selection(py::array_t<uint8_t> im, int frame=0, const ssize_t length=10, const ssize_t width=1,
const ssize_t left=0, const ssize_t top=0, ssize_t right=-1, ssize_t bottom=-1) {
const Image img(im);
if (right == -1)
right = img.width() - 1;
if (bottom == -1)
bottom = img.height() - 1;
for (ssize_t j = left; j <= right - width; ++j) {
for (ssize_t i = top; i < top + width; ++i) {
if ((j - left + frame) / length % 2 == 0)
img.set_pixel(i, j, {0, 0, 0, 255});
else
img.set_pixel(i, j, {255, 255, 255, 255});
}
}
frame = static_cast<int>((frame + static_cast<int>(right - left - width)) % (length * 2));
for (ssize_t i = top; i <= bottom - width; ++i) {
for (ssize_t j = right; j > right - width; --j) {
if ((i - top + frame) / length % 2 == 0)
img.set_pixel(i, j, {0, 0, 0, 255});
else
img.set_pixel(i, j, {255, 255, 255, 255});
}
}
frame = static_cast<int>((frame + static_cast<int>(bottom - top - width)) % (length * 2));
for (ssize_t j = left + width; j <= right; ++j) {
for (ssize_t i = bottom; i > bottom - width; --i) {
if ((right - j + frame) / length % 2 == 0)
img.set_pixel(i, j, {0, 0, 0, 255});
else
img.set_pixel(i, j, {255, 255, 255, 255});
}
}
frame = static_cast<int>((frame + static_cast<int>(right - left - width)) % (length * 2));
for (ssize_t i = top + width; i <= bottom; ++i) {
for (ssize_t j = left; j < left + width; ++j) {
if ((bottom - i + frame) / length % 2 == 0)
img.set_pixel(i, j, {0, 0, 0, 255});
else
img.set_pixel(i, j, {255, 255, 255, 255});
}
}
return im;
}
py::array_t<uint8_t> transparent(py::array_t<uint8_t> im, const ssize_t size, const uint8_t gray=239) {
const Image img(im);
for (ssize_t i = 0; i < img.height(); ++i) {
for (ssize_t j = 0; j < img.width(); ++j) {
if ((i / size + j / size) % 2 == 0) {
img.set_pixel(i, j, {255, 255, 255, 255});
} else {
img.set_pixel(i, j, {gray, gray, gray, 255});
}
}
}
return im;
}
void set_threads(const int n) { THREADS = n; }
int get_threads() { return THREADS; }
double get_progress() {
if (MAX_PROGRESS > 0)
return static_cast<double>(PROGRESS) * 100.0 / static_cast<double>(MAX_PROGRESS);
return -1;
}
PYBIND11_MODULE(filters, m) {
m.doc() = "Filters, written in C++";
m.def("gray", &gray, "Turns an image to shades of gray", "im"_a, "use_alpha"_a=false,
py::return_value_policy::reference_internal);
m.def("black_and_white", &black_and_white, "Turns an image to B&W", "im"_a, "threshold"_a = 127,
"reverse"_a = false, py::return_value_policy::reference_internal);
m.def("reverse", &reverse, "Reverse the color", "im"_a, "use_alpha"_a=false,
py::return_value_policy::reference_internal);
m.def("brightness_noise", &brightness_noise, "Makes brightness noise", "im"_a, "level"_a=100, "use_alpha"_a=false,
py::return_value_policy::reference_internal);
m.def("color_noise", &color_noise, "Makes color noise", "im"_a, "level"_a=100, "use_alpha"_a=false,
py::return_value_policy::reference_internal);
m.def("corners", &corners, "Tint four corners", "im"_a, "colors"_a,
py::return_value_policy::reference_internal);
m.def("mirror", &mirror, "Mirrors the left half on the right", "im"_a,
py::return_value_policy::reference_internal);
m.def("lines", &lines, "Rotate each line, then rotate the image", "im"_a, "width"_a=10,
py::return_value_policy::reference_internal);
m.def("turn", &turn, "Turn each inner rectangle 180", "im"_a, "count"_a,
py::return_value_policy::reference_internal);
m.def("curtain", &curtain, "Copies the bottom column until the center", "im"_a,
py::return_value_policy::reference_internal);
m.def("dots", &dots, "Place a dotted image on top of an image", "im"_a, "im2"_a,
py::return_value_policy::reference_internal);
m.def("channel_swap", &channel_swap, "Swap channels", "im"_a, "first"_a, "second"_a, "reverse"_a=false,
py::return_value_policy::reference_internal);
m.def("matrix", &matrix, "Apply a matrix filter", "im"_a, "m"_a,
py::return_value_policy::reference_internal);
m.def("blur", &blur, "Blur an image", "im"_a, "level"_a=1,
py::return_value_policy::reference_internal);
m.def("sharp", &sharp, "Sharpen an image", "im"_a, "level"_a=9,
py::return_value_policy::reference_internal);
m.def("median", &median, "Apply a median filter", "im"_a, "size"_a,
py::return_value_policy::reference_internal);
m.def("bright", &bright, "Brightness", "im"_a, "size"_a,
py::return_value_policy::reference_internal);
m.def("dim", &dim, "Dimness", "im"_a, "size"_a,
py::return_value_policy::reference_internal);
m.def("selection", &selection, "Selection", "im"_a, "frame"_a=0, "length"_a=10, "width"_a=1,
"left"_a=0, "top"_a=0, "right"_a=-1, "bottom"_a=-1, py::return_value_policy::reference_internal);
m.def("transparent", &transparent, "Transparent background", "im"_a, "size"_a, "gray"_a=239,
py::return_value_policy::reference_internal);
m.def("set_threads", &set_threads, "Sets the thread count", "n"_a);
m.def("get_threads", &get_threads, "Gets the thread count");
m.def("get_progress", &get_progress, "Gets the current progress");
}