-
Notifications
You must be signed in to change notification settings - Fork 5
/
blurry.cpp
3060 lines (2542 loc) · 82.9 KB
/
blurry.cpp
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// +build ignore.
#define DEBUG 0
#include "blurry.hpp"
const uint8_t GRAY_R = 76, GRAY_G = 152, GRAY_B = 28;
const Expr CANNY_SIGMA = 5.0f;
const Expr acos_v = -1.0f;
const Expr pi = acos(acos_v);
const Expr ui8_0 = cast<uint8_t>(0);
const Expr ui8_1 = cast<uint8_t>(1);
const Expr ui8_128 = cast<uint8_t>(128);
const Expr ui8_255 = cast<uint8_t>(255);
const Expr f05 = cast<float>(0.5f);
const Expr float_0 = cast<float>(0.f);
const Expr float_16 = cast<float>(16.f);
const Expr float_128 = cast<float>(128.f);
const Expr float_255 = cast<float>(255.f);
const Expr degree0 = cast<uint8_t>(0);
const Expr degree45 = cast<uint8_t>(45);
const Expr degree90 = cast<uint8_t>(90);
const Expr degree135 = cast<uint8_t>(135);
const Expr pcm16_max = cast<float>(32768.0f); // 2^15
const Expr ln10 = cast<float>(2.30258509299404568401799145468436420760110148862877297603332790f); // https://oeis.org/A002392
const Func kernel_sobel_x = kernel_sobel3x3_x();
const Func kernel_sobel_y = kernel_sobel3x3_y();
const Func kernel_emboss = kernel_emboss3x3();
const Func kernel_laplacian = kernel_laplacian3x3();
const Func kernel_highpass = kernel_highpass3x3();
const Func kernel_gradient = kernel_gradient3x3();
const RDom rd_kernel = RDom(-1, 3, -1, 3, "rd_kernel");
Func kernel_sobel3x3_x() {
Var x("x"), y("y");
Func kernel_x = Func("kernel_sobel_x");
kernel_x(x, y) = 0;
kernel_x(-1, -1) = -1; kernel_x(0, -1) = 0; kernel_x(1, -1) = 1;
kernel_x(-1, 0) = -2; kernel_x(0, 0) = 0; kernel_x(1, 0) = 2;
kernel_x(-1, 1) = -1; kernel_x(0, 1) = 0; kernel_x(1, 1) = 1;
return kernel_x;
}
Func kernel_sobel3x3_y() {
Var x("x"), y("y");
Func kernel_y = Func("kernel_sobel_y");
kernel_y(x, y) = 0;
kernel_y(-1, -1) = -1; kernel_y(0, -1) = -2; kernel_y(1, -1) = -1;
kernel_y(-1, 0) = 0; kernel_y(0, 0) = 0; kernel_y(1, 0) = 0;
kernel_y(-1, 1) = 1; kernel_y(0, 1) = 2; kernel_y(1, 1) = 1;
return kernel_y;
}
Func kernel_emboss3x3() {
Var x("x"), y("y");
Func kernel = Func("kernel_emboss");
kernel(x, y) = 0;
kernel(-1, -1) = -1; kernel(0, -1) = -1; kernel(1, -1) = 0;
kernel(-1, 0) = -1; kernel(0, 0) = 0; kernel(1, 0) = 1;
kernel(-1, 1) = 0; kernel(0, 1) = 1; kernel(1, 1) = 1;
return kernel;
}
Func kernel_laplacian3x3() {
Var x("x"), y("y");
Func kernel = Func("kernel_laplacian");
kernel(x, y) = 0;
kernel(-1, -1) = 0; kernel(0, -1) = 1; kernel(1, -1) = 0;
kernel(-1, 0) = 1; kernel(0, 0) = -4; kernel(1, 0) = 1;
kernel(-1, 1) = 0; kernel(0, 1) = 1; kernel(1, 1) = 0;
return kernel;
}
Func kernel_highpass3x3() {
Var x("x"), y("y");
Func kernel = Func("kernel_highpass");
kernel(x, y) = 0;
kernel(-1, -1) = -1; kernel(0, -1) = -1; kernel(1, -1) = -1;
kernel(-1, 0) = -1; kernel(0, 0) = 8; kernel(1, 0) = -1;
kernel(-1, 1) = -1; kernel(0, 1) = -1; kernel(1, 1) = -1;
return kernel;
}
Func kernel_gradient3x3() {
Var x("x"), y("y");
Func kernel = Func("kernel_gradient");
kernel(x, y) = 0;
kernel(-1, -1) = 1; kernel(0, -1) = 1; kernel(1, -1) = 1;
kernel(-1, 0) = 0; kernel(0, 0) = 0; kernel(1, 0) = 0;
kernel(-1, 1) = -1; kernel(0, 1) = -1; kernel(1, 1) = -1;
return kernel;
}
Func wrapFunc(Buffer<uint8_t> buf, const char* name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = buf(x, y, ch);
return f;
}
Func wrapFunc(Buffer<float> buf, const char* name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = buf(x, y, ch);
return f;
}
Func wrapFunc(Buffer<double> buf, const char* name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = buf(x, y, ch);
return f;
}
Func wrapFunc_xy(Buffer<uint8_t> buf, const char* name) {
Var x("x"), y("y");
Func f = Func(name);
f(x, y) = buf(x, y);
return f;
}
Func wrapFunc_xy(Buffer<float> buf, const char* name) {
Var x("x"), y("y");
Func f = Func(name);
f(x, y) = buf(x, y);
return f;
}
Func wrapFunc_xy(Buffer<double> buf, const char* name) {
Var x("x"), y("y");
Func f = Func(name);
f(x, y) = buf(x, y);
return f;
}
Func wrapFunc_x(Buffer<uint8_t> buf, const char* name) {
Var x("x");
Func f = Func(name);
f(x) = buf(x);
return f;
}
Func wrapFunc_x(Buffer<int16_t> buf, const char* name) {
Var x("x");
Func f = Func(name);
f(x) = buf(x);
return f;
}
Func wrapFunc_x(Buffer<float> buf, const char* name) {
Var x("x");
Func f = Func(name);
f(x) = buf(x);
return f;
}
Func wrapFunc_x(Buffer<double> buf, const char* name) {
Var x("x");
Func f = Func(name);
f(x) = buf(x);
return f;
}
Func read(Func clamped, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = cast<int16_t>(clamped(x, y, ch));
return read;
}
Func readUI8(Func clamped, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = cast<uint8_t>(clamped(x, y, ch));
return read;
}
Func readFloat(Func clamped, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = cast<float>(clamped(x, y, ch));
return read;
}
// ABGR to RGBA
Func read_from_abgr(Func in, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = select(
ch == 0, in(x, y, 3), // A
ch == 1, in(x, y, 2), // B
ch == 2, in(x, y, 1), // G
likely(in(x, y, 0)) // R
);
return f;
}
// ARGB to RGBA
Func read_from_argb(Func in, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = select(
ch == 0, in(x, y, 3), // A
ch == 1, in(x, y, 0), // R
ch == 2, in(x, y, 1), // G
likely(in(x, y, 2)) // B
);
return f;
}
// RABG to RGBA
Func read_from_rabg(Func in, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = select(
ch == 0, in(x, y, 0), // R
ch == 1, in(x, y, 3), // A
ch == 2, in(x, y, 2), // B
likely(in(x, y, 1)) // G
);
return f;
}
// BGRA to RGBA
Func read_from_bgra(Func in, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
f(x, y, ch) = select(
ch == 0, in(x, y, 2), // B
ch == 1, in(x, y, 1), // G
ch == 2, in(x, y, 0), // R
likely(in(x, y, 3)) // A
);
return f;
}
// BT.601 limited range
Func read_from_yuv_bt601_limited(Func yf, Func uf, Func vf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yy = (yf(x, y) - float_16) * 1.164f;
Expr uu = uf(x, y) - float_128;
Expr vv = vf(x, y) - float_128;
Expr r = yy + (1.596f * vv);
Expr g = yy - (0.391f * uu) - (0.813f * vv);
Expr b = yy + (2.018f * uu);
Expr rr = clamp(r, float_0, float_255);
Expr gg = clamp(g, float_0, float_255);
Expr bb = clamp(b, float_0, float_255);
Expr v = select(
ch == 0, rr, // R
ch == 1, gg, // G
ch == 2, bb, // B
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
// BT.601 full range
Func read_from_yuv_bt601_fullrange(Func yf, Func uf, Func vf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yy = yf(x, y);
Expr uu = uf(x, y) - float_128;
Expr vv = vf(x, y) - float_128;
Expr r = yy + (1.40200f * vv);
Expr g = yy - (0.34414f * uu) - (0.71414f * vv);
Expr b = yy + (1.77200f * uu);
Expr rr = clamp(r, float_0, float_255);
Expr gg = clamp(g, float_0, float_255);
Expr bb = clamp(b, float_0, float_255);
Expr v = select(
ch == 0, rr, // R
ch == 1, gg, // G
ch == 2, bb, // B
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
// BT.709 limited range
Func read_from_yuv_bt709_limited(Func yf, Func uf, Func vf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yy = (yf(x, y) - float_16) * 1.164f;
Expr uu = uf(x, y) - float_128;
Expr vv = vf(x, y) - float_128;
Expr r = yy + (1.793f * vv);
Expr g = yy - (0.213f * uu) - (0.533f * vv);
Expr b = yy + (2.112f * uu);
Expr rr = clamp(r, float_0, float_255);
Expr gg = clamp(g, float_0, float_255);
Expr bb = clamp(b, float_0, float_255);
Expr v = select(
ch == 0, rr, // R
ch == 1, gg, // G
ch == 2, bb, // B
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
// BT.2020 full range
Func read_from_yuv_bt2020_fullrange(Func yf, Func uf, Func vf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yy = yf(x, y);
Expr uu = uf(x, y) - float_128;
Expr vv = vf(x, y) - float_128;
Expr r = yy + (1.474600f * vv);
Expr g = yy - (0.164553f * uu) - (0.571353f * vv);
Expr b = yy + (1.881400f * uu);
Expr rr = clamp(r, float_0, float_255);
Expr gg = clamp(g, float_0, float_255);
Expr bb = clamp(b, float_0, float_255);
Expr v = select(
ch == 0, rr, // R
ch == 1, gg, // G
ch == 2, bb, // B
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
// BT.2020 limited range
Func read_from_yuv_bt2020_limited(Func yf, Func uf, Func vf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yy = (yf(x, y) - float_16) * 1.164384f;
Expr uu = uf(x, y) - float_128;
Expr vv = vf(x, y) - float_128;
Expr r = yy + (1.67867f * vv);
Expr g = yy - (0.187326f * uu) - (0.65042f * vv);
Expr b = yy + (2.14177f * uu);
Expr rr = clamp(r, float_0, float_255);
Expr gg = clamp(g, float_0, float_255);
Expr bb = clamp(b, float_0, float_255);
Expr v = select(
ch == 0, rr, // R
ch == 1, gg, // G
ch == 2, bb, // B
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
Func read_from_yuv_444(Func in_y, Func in_u, Func in_v, const char *name) {
Var x("x"), y("y");
Func yf = Func("y_float");
Func uf = Func("u_float");
Func vf = Func("v_float");
yf(x, y) = cast<float>(in_y(x, y));
uf(x, y) = cast<float>(in_u(x, y));
vf(x, y) = cast<float>(in_v(x, y));
return read_from_yuv_bt2020_limited(yf, uf, vf, name);
}
Func read_from_yuv_420(Func in_y, Func in_u, Func in_v, const char *name) {
Var x("x"), y("y");
Func yf = Func("y_float");
Func uf = Func("u_float");
Func vf = Func("v_float");
yf(x, y) = cast<float>(in_y(x, y));
uf(x, y) = cast<float>(in_u(x / 2, y / 2));
vf(x, y) = cast<float>(in_v(x / 2, y / 2));
return read_from_yuv_bt2020_limited(yf, uf, vf, name);
}
// BT.601
Func rgb_to_yuv_bt601(Func rf, Func gf, Func bf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yuv_y = ((rf(x, y) * 0.257f) + (gf(x, y) * 0.504f) + (bf(x, y) * 0.098f)) + float_16;
Expr yuv_u = ((rf(x, y) * -0.148f) - (gf(x, y) * 0.291f) + (bf(x, y) * 0.439f)) + float_128;
Expr yuv_v = ((rf(x, y) * 0.439f) - (gf(x, y) * 0.368f) - (bf(x, y) * 0.071f)) + float_128;
Expr yy = clamp(yuv_y, float_0, float_255);
Expr uu = clamp(yuv_u, float_0, float_255);
Expr vv = clamp(yuv_v, float_0, float_255);
Expr v = select(
ch == 0, yy, // Y
ch == 1, uu, // U
ch == 2, vv, // V
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
// BT.2020 full range
Func rgb_to_yuv_bt2020_fullrange(Func rf, Func gf, Func bf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yuv_y = ((rf(x, y) * 0.26270f) + (gf(x, y) * 0.67800f) + (bf(x, y) * 0.05930f)) + float_16;
Expr yuv_u = ((rf(x, y) * -0.13963f) - (gf(x, y) * 0.36037f) + (bf(x, y) * 0.50000f)) + float_128;
Expr yuv_v = ((rf(x, y) * 0.50000f) - (gf(x, y) * 0.45979f) - (bf(x, y) * 0.04021f)) + float_128;
Expr yy = clamp(yuv_y, float_0, float_255);
Expr uu = clamp(yuv_u, float_0, float_255);
Expr vv = clamp(yuv_v, float_0, float_255);
Expr v = select(
ch == 0, yy, // Y
ch == 1, uu, // U
ch == 2, vv, // V
likely(float_255) // A always 0xff
);
f(x, y, ch) = cast<uint8_t>(v);
return f;
}
// BT.2020 limited range
Func rgb_to_yuv_bt2020_limited(Func rf, Func gf, Func bf, const char *name) {
Var x("x"), y("y"), ch("ch");
Func f = Func(name);
Expr yuv_y = ((rf(x, y) * 0.22564f) + (gf(x, y) * 0.59558f) + (bf(x, y) * 0.05209f)) + float_16;
Expr yuv_u = ((rf(x, y) * -0.11992f) - (gf(x, y) * 0.31656f) + (bf(x, y) * 0.43922f)) + float_128;
Expr yuv_v = ((rf(x, y) * 0.42941f) - (gf(x, y) * 0.40389f) - (bf(x, y) * 0.03533f)) + float_128;
Expr yy = clamp(yuv_y, float_0, float_255);
Expr uu = clamp(yuv_u, float_0, float_255);
Expr vv = clamp(yuv_v, float_0, float_255);
Expr v = select(
ch == 0, yy, // Y
ch == 1, uu, // U
ch == 2, vv, // V
likely(float_255) // A always 0xff
);
f(x, y, ch) = v;
return f;
}
Func rgb_to_yuv444(Func in, const char *name) {
Var x("x"), y("y");
Func rf = Func("r_float");
Func gf = Func("g_float");
Func bf = Func("b_float");
rf(x, y) = cast<float>(in(x, y, 0));
gf(x, y) = cast<float>(in(x, y, 1));
bf(x, y) = cast<float>(in(x, y, 2));
return rgb_to_yuv_bt2020_limited(rf, gf, bf, name);
}
Func gray_xy(Func in) {
Var x("x"), y("y");
Func gray = Func("gray");
gray(x, y) = in(x, y, 0); // rgba(r) for grayscale
gray.compute_root();
return gray;
}
Func gray_xy_uint8(Func in, const char *name) {
Var x("x"), y("y");
Func gray = Func(name);
gray(x, y) = cast<uint8_t>(in(x, y, 0)); // rgba(r) for grayscale
gray.compute_root();
return gray;
}
Func rotate90(Func in, Expr width, Expr height, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = in(y, height - x, ch);
return read;
}
Func rotate90(Func clamped, Param<int32_t> width, Param<int32_t> height, const char *name) {
Expr w = width - 1;
Expr h = height - 1;
return rotate90(clamped, w, h, name);
}
Func rotate180(Func in, Expr width, Expr height, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = in(width - x, height - y, ch);
return read;
}
Func rotate180(Func clamped, Param<int32_t> width, Param<int32_t> height, const char *name) {
Expr w = width - 1;
Expr h = height - 1;
return rotate180(clamped, w, h, name);
}
Func rotate270(Func in, Expr width, Expr height, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = in(width - y, x, ch);
return read;
}
Func rotate270(Func clamped, Param<int32_t> width, Param<int32_t> height, const char *name) {
Expr w = width - 1;
Expr h = height - 1;
return rotate270(clamped, w, h, name);
}
// Func flip = flipV(in, width, height, "myFlip")
// f(x, y, ch) = flip(x, y, ch);
Func flipV(Func in, Expr width, Expr height, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = in(width - x, y, ch);
return read;
}
// Func foo(Func input, Param<int32_t> w, Param<int32_t> h) {
// Func in = flipV(input, w, h);
// ...
// }
Func flipV(Func clamped, Param<int32_t> width, Param<int32_t> height, const char *name) {
Expr w = width - 1;
Expr h = height - 1;
return flipV(clamped, w, h, name);
}
// Func flip = flipH(in, width, height, "myFlip")
// f(x, y, ch) = flip(x, y, ch);
Func flipH(Func in, Expr width, Expr height, const char *name) {
Var x("x"), y("y"), ch("ch");
Func read = Func(name);
read(x, y, ch) = in(x, height - y, ch);
return read;
}
// Func foo(Func input, Param<int32_t> w, Param<int32_t> h) {
// Func in = flipH(input, w, h);
// ...
// }
Func flipH(Func clamped, Param<int32_t> width, Param<int32_t> height, const char *name) {
Expr w = width - 1;
Expr h = height - 1;
return flipH(clamped, w, h, name);
}
Func convolve(Func in, Func kernel, RDom rd_kernel) {
Var x("x"), y("y"), ch("ch");
Func convolve = Func("convolve");
Expr in_val = in(x + rd_kernel.x, y + rd_kernel.y, ch);
Expr k_val = kernel(rd_kernel.x, rd_kernel.y);
Expr val = in_val * k_val;
convolve(x, y, ch) += cast<uint8_t>(val);
return convolve;
}
Func convolve_xy(Func in, Func kernel, RDom rd_kernel) {
Var x("x"), y("y");
Func convolve = Func("convolve_xy");
Expr in_val = in(x + rd_kernel.x, y + rd_kernel.y);
Expr k_val = kernel(rd_kernel.x, rd_kernel.y);
convolve(x, y) += in_val * k_val;
return convolve;
}
Func filter2d_gray(
Func input,
Region bounds,
Func kernel,
RDom rd_kernel,
const char *name
) {
Var x("x"), y("y"), ch("ch");
Var xo("xo"), xi("xi");
Var yo("yo"), yi("yi");
Var ti("ti");
Func in = gray_xy_uint8(BoundaryConditions::repeat_edge(input, bounds), "in");
Func conv = convolve_xy(in, kernel, rd_kernel);
Func gradient = Func(name);
gradient(x, y, ch) = select(
ch < 3, cast<uint8_t>(conv(x, y) & 128),
likely(ui8_255)
);
conv.compute_at(gradient, yi)
.vectorize(x)
.update(0).unscheduled();
gradient.compute_at(in, ti)
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ch)
.parallel(ti, 8)
.vectorize(xi, 32);
return gradient;
}
Func gauss_kernel(Expr sigma) {
Var x("x"), y("y"), ch("ch");
Expr sig2 = 2 * sigma * sigma;
Expr sigR = sigma * sqrt(2 * pi);
Func kernel = Func("kernel");
kernel(x) = fast_exp(-((x * x)) / sig2 / sigR);
return kernel;
}
Expr erode(Func in, RDom rd_erode) {
Var x("x"), y("y");
Expr val = in(x + rd_erode.x, y + rd_erode.y);
return minimum(val);
}
Expr dilate(Func in, RDom rd_dilate) {
Var x("x"), y("y");
Expr val = in(x + rd_dilate.x, y + rd_dilate.y);
return maximum(val);
}
Func morphology_open(Func in, Param<int32_t> size) {
Var x("x"), y("y");
RDom rd_morph = RDom(-1 * size, (size * 2) + 1, -1 * size, (size * 2) + 1, "rd_morph_open");
Func erode_tmp = Func("erode_tmp");
erode_tmp(x, y) = erode(in, rd_morph);
Func dilate_tmp = Func("dilate_tmp");
dilate_tmp(x, y) = dilate(erode_tmp, rd_morph);
Func morph = Func("morph_open");
morph(x, y) = dilate_tmp(x, y);
erode_tmp.compute_root()
.parallel(y, 8)
.vectorize(x, 32);
dilate_tmp.compute_root()
.parallel(y, 8)
.vectorize(x, 32);
return morph;
}
Func morphology_close(Func in, Param<int32_t> size) {
Var x("x"), y("y");
RDom rd_morph = RDom(-1 * size, (size * 2) + 1, -1 * size, (size * 2) + 1, "rd_morph_close");
Func dilate_tmp = Func("dilate_tmp");
dilate_tmp(x, y) = dilate(in, rd_morph);
Func erode_tmp = Func("erode_tmp");
erode_tmp(x, y) = erode(dilate_tmp, rd_morph);
Func morph = Func("morph_close");
morph(x, y) = erode_tmp(x, y);
dilate_tmp.compute_root()
.parallel(y, 8)
.vectorize(x, 32);
erode_tmp.compute_root()
.parallel(y, 8)
.vectorize(x, 32);
return morph;
}
Func gaussian(Func in, Expr sigma, RDom rd, const char *name) {
Var x("x"), y("y");
Func kernel = gauss_kernel(sigma);
Func sum_kernel = Func("sum_kernel");
Expr kernel_val = kernel(rd);
sum_kernel(x) += kernel_val;
Func gaussian = Func(name);
Expr center_val = sum_kernel(0);
Expr in_val = in(x + rd.x, y);
Expr val = in_val * kernel(rd);
gaussian(x, y) += cast<uint8_t>(val / center_val);
sum_kernel.compute_at(gaussian, y)
.vectorize(x)
.update(0).unscheduled();
return gaussian;
}
Func canny(Func in, Param<int32_t> threshold_max, Param<int32_t> threshold_min) {
Var x("x"), y("y"), ch("ch");
Var xo("xo"), xi("xi");
Var yo("yo"), yi("yi");
Var ti("ti");
//
// gaussian
//
RDom gauss_rd = RDom(-1, 3, "gaussian_rdom");
Func gauss = gaussian(in, CANNY_SIGMA, gauss_rd, "gaussian3x3");
Func gx = convolve_xy(gauss, kernel_sobel_x, rd_kernel);
Func gy = convolve_xy(gauss, kernel_sobel_y, rd_kernel);
//
// sobel x/y
//
Func sobel = Func("sobel");
Expr pow_gy = fast_pow(abs(gy(x, y)), 2);
Expr pow_gx = fast_pow(abs(gx(x, y)), 2);
Expr magnitude = ceil(sqrt(pow_gy + pow_gx));
sobel(x, y) = magnitude;
//
// non max supression
//
Func nms = Func("nonmax_supression");
Expr angle = (atan2(gy(x, y), gx(x, y)) * 180) / pi;
Expr approx = select(
angle >= -22.5f && angle < 22.5f, degree0,
angle >= 157.5f && angle < 180.0f, degree0,
angle >= 180.0f && angle < -157.5f, degree0,
angle >= 22.5f && angle < 67.5f, degree45,
angle >= -157.5f && angle < -112.5f, degree45,
angle >= 67.5f && angle < 112.5f, degree90,
angle >= -112.5f && angle < -67.5f, degree90,
angle >= 112.5f && angle < 157.5f, degree135,
angle >= -67.5f && angle < -22.5f, degree135,
likely(degree0)
);
nms(x, y) = select(
approx == degree0 && sobel(x, y) < sobel(x + 1, y), ui8_0,
approx == degree0 && sobel(x, y) < sobel(x - 1, y), ui8_0,
approx == degree45 && sobel(x, y) < sobel(x + 1, y - 1), ui8_0,
approx == degree45 && sobel(x, y) < sobel(x - 1, y + 1), ui8_0,
approx == degree90 && sobel(x, y) < sobel(x, y + 1), ui8_0,
approx == degree90 && sobel(x, y) < sobel(x, y - 1), ui8_0,
sobel(x, y) < sobel(x + 1, y + 1), ui8_0,
sobel(x, y) < sobel(x - 1, y - 1), ui8_0,
cast<uint8_t>(sobel(x, y))
);
//
// hysteresis threshold
//
RDom rd_nb = RDom(-1, 3, -1, 3, "rd_neighbors");
Func hy = Func("hysteresis");
Expr value = nms(x, y);
Expr nb_val = maximum(nms(x + rd_nb.x, y + rd_nb.y));
Expr th_val = select(
value < threshold_min, ui8_0,
value > threshold_max, ui8_255,
nb_val > threshold_max, ui8_255,
value
);
hy(x, y) = th_val;
gauss.compute_at(hy, ti)
.vectorize(y)
.vectorize(x)
.update(0).unscheduled();
gy.compute_at(hy, ti)
.vectorize(x)
.update(0).unscheduled();
gx.compute_at(hy, ti)
.vectorize(x)
.update(0).unscheduled();
nms.compute_at(hy, ti)
.vectorize(x);
sobel.compute_at(hy, ti)
.unroll(y, 8)
.vectorize(x);
hy.compute_root()
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ti)
.vectorize(xi, 32);
return hy;
}
Func cloneimg_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
Var x("x"), y("y"), ch("ch");
Region src_bounds = {{0, width},{0, height},{0, 4}};
Func in = readUI8(BoundaryConditions::repeat_edge(input, src_bounds), "in");
//
// RGBA interleave test
//
Func cloneimg = Func("cloneimg");
cloneimg(x, y, ch) = in(x, y, ch);
cloneimg.compute_at(in, x)
.parallel(ch)
.vectorize(x, 16);
return cloneimg;
}
Func convert_from(Func in, Param<int32_t> width, Param<int32_t> height, const char *name) {
Var x("x"), y("y"), ch("ch");
Var xo("xo"), xi("xi");
Var yo("yo"), yi("yi");
Var ti("ti");
Func convert = Func(name);
convert(x, y, ch) = in(x, y, ch);
convert.compute_at(in, xi)
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ch)
.parallel(ti, 16)
.vectorize(xi, 32);
in.compute_at(convert, xi)
.parallel(ch)
.vectorize(x, 32);
return convert;
}
// ABGR
Func convert_from_abgr_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
return convert_from(
read_from_abgr(input, "in"),
width,
height,
"convert_from_abgr"
);
}
// ARGB
Func convert_from_argb_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
return convert_from(
read_from_argb(input, "in"),
width,
height,
"convert_from_argb"
);
}
// BGRA
Func convert_from_bgra_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
return convert_from(
read_from_bgra(input, "in"),
width,
height,
"convert_from_bgra"
);
}
// BGRA little endian (argb in memory) at libyuv
Func convert_from_rabg_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
return convert_from(
read_from_rabg(input, "in"),
width,
height,
"convert_from_rabg"
);
}
Func convert_from_yuv_444_fn(Func in_y, Func in_u, Func in_v, Param<int32_t> width, Param<int32_t> height) {
return convert_from(
read_from_yuv_444(in_y, in_u, in_v, "in"),
width,
height,
"convert_from_yuv_444"
);
}
Func convert_from_yuv_420_fn(Func in_y, Func in_u, Func in_v, Param<int32_t> width, Param<int32_t> height) {
return convert_from(
read_from_yuv_420(in_y, in_u, in_v, "in"),
width,
height,
"convert_from_yuv_420"
);
}
Pipeline convert_to_yuv_420_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
Var x("x"), y("y"), ch("ch");
Var xo("xo"), xi("xi");
Var yo("yo"), yi("yi");
Var ti("ti");
Region src_bounds = {{0, width},{0, height},{0, 3}};
Func in = readUI8(BoundaryConditions::constant_exterior(input, 0, src_bounds), "in");
Expr y_max_w = width;
Expr y_max_h = height;
Expr y_height = height;
Expr y_width = width;
Expr uv_width = width / 2;
Expr uv_height = height / 2;
Expr u_max_h = y_height + uv_height;
Expr v_max_h = u_max_h + uv_height;
Func yuv = rgb_to_yuv444(in, "rgb_to_yuv444");
Func yuv444to420 = Func("yuv444to420");
Expr kx = x * 2;
Expr ky = y * 2;
yuv444to420(x, y, ch) = cast<float>(
yuv(kx, ky, ch) +
yuv(kx + 1, ky, ch) +
yuv(kx, ky + 1, ch) +
yuv(kx + 1, ky + 1, ch)
) / 4.f;
Func fn_y = Func("fn_y");
Func fn_u = Func("fn_u");
Func fn_v = Func("fn_v");
fn_y(x, y) = cast<uint8_t>(yuv(x, y, 0));
fn_u(x, y) = cast<uint8_t>(yuv444to420(x, y, 1));
fn_v(x, y) = cast<uint8_t>(yuv444to420(x, y, 2));
in.compute_root();
fn_y.compute_at(yuv, ti)
.store_at(yuv, ti)
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ti)
.vectorize(xi, 32);
fn_u.compute_at(yuv444to420, ti)
.store_at(yuv444to420, ti)
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ti)
.vectorize(xi, 32);
fn_v.compute_at(yuv444to420, ti)
.store_at(yuv444to420, ti)
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ti)
.vectorize(xi, 32);
return Pipeline({fn_y, fn_u, fn_v});
}
Pipeline convert_to_yuv_444_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
Var x("x"), y("y");