-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pixels.cs
3064 lines (2838 loc) · 105 KB
/
Pixels.cs
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
using System;
using System.Collections.Generic;
/// <summary>
/// Holds methods that operate on arrays of pixels held by images.
/// </summary>
public static class Pixels
{
#region Generic
/// <summary>
/// Creates a checker pattern in an array of pixels.
/// </summary>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <param name="a">first color/param>
/// <param name="b">second color/param>
/// <param name="cols">columns</param>
/// <param name="rows">rows</param>
/// <returns>checker pattern</returns>
public static T[] Checker<T>(
in T[] target,
in int w, in int h,
in T a, in T b,
in int cols = 8, in int rows = 8)
{
const int limit = 2;
int vCols = cols < 2 ? 2 : cols > w / limit ? w / limit : cols;
int vRows = rows < 2 ? 2 : rows > h / limit ? h / limit : rows;
int wch = w / vCols;
int hchw = w * h / vRows;
int trgLen = target.Length;
for (int i = 0; i < trgLen; ++i)
{
// % 2 can be replaced by & 1 for even or odd.
target[i] = (i % w / wch + i / hchw & 1) == 0 ? a : b;
}
return target;
}
/// <summary>
/// Expands the image to the nearest greater power of two. Centers the
/// source image within the expanded dimensions. Returns a tuple with the
/// expanded pixels and the new width and height.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <param name="uniform">use uniform size</param>
/// <returns>tuple</returns>
public static (T[] pixels, int w, int h) ExpandToPow2<T>(
in T[] source, in int w, in int h, in bool uniform = false)
{
int wTrg;
int hTrg;
if (uniform)
{
int u = Utils.NextPowerOf2(Utils.Max(w, h));
wTrg = u;
hTrg = u;
}
else
{
wTrg = Utils.NextPowerOf2(w);
hTrg = Utils.NextPowerOf2(h);
}
int trgLen = wTrg * hTrg;
T[] target = new T[trgLen];
int srcLen = source.Length;
if (w == wTrg && h == hTrg)
{
System.Array.Copy(source, 0, target, 0, srcLen);
return (pixels: target, w: wTrg, h: hTrg);
}
int xtl = (int)(wTrg * 0.5f + 0.5f) - (int)(w * 0.5f + 0.5f);
int ytl = (int)(hTrg * 0.5f + 0.5f) - (int)(h * 0.5f + 0.5f);
for (int i = 0; i < trgLen; ++i)
{
int xSrc = (i % wTrg) - xtl;
int ySrc = (i / hTrg) - ytl;
if (ySrc > -1 && ySrc < h && xSrc > -1 && xSrc < w)
{
target[i] = source[xSrc + ySrc * w];
}
else
{
target[i] = default;
}
}
return (pixels: target, w: wTrg, h: hTrg);
}
/// <summary>
/// Fills the pixels target array with a color.
/// </summary>
/// <param name="source">fill color</param>
/// <param name="target">target pixels</param>
/// <returns>filled array</returns>
public static T[] Fill<T>(in T source, in T[] target)
{
int len = target.Length;
for (int i = 0; i < len; ++i) { target[i] = source; }
return target;
}
/// <summary>
/// Transposes and flips the source array. The flipped transposition is
/// stored in the target pixel array. The transposed image's width and
/// height are swapped.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <returns>transposed pixels</returns>
public static T[] FlipAll<T>(
in T[] source, in T[] target,
in int w, in int h)
{
int srcLen = source.Length;
if (srcLen == target.Length)
{
int wn1 = w - 1;
int hn1 = h - 1;
for (int i = 0; i < srcLen; ++i)
{
// TODO: Multiply wn1 * h before the loop then just x * h within?
target[(wn1 - i % w) * h + hn1 - i / w] = source[i];
}
}
return target;
}
/// <summary>
/// Flips the pixels source array horizontally, on the x axis, and stores
/// the result in the target array.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <returns>flipped pixels</returns>
public static T[] FlipX<T>(in T[] source, in T[] target, in int w, in int h)
{
int srcLen = source.Length;
if (source == target)
{
int wd2 = w / 2;
int wn1 = w - 1;
int lenHalf = wd2 * h;
for (int i = 0; i < lenHalf; ++i)
{
int x = i % wd2;
int yw = w * (i / wd2);
int idxSrc = x + yw;
int idxTrg = yw + wn1 - x;
(source[idxTrg], source[idxSrc]) = (source[idxSrc], source[idxTrg]);
}
}
else if (srcLen == target.Length)
{
int wn1 = w - 1;
for (int i = 0; i < srcLen; ++i)
{
target[i / w * w + wn1 - i % w] = source[i];
}
}
return target;
}
/// <summary>
/// Flips the pixels source array vertically, on the y axis, and stores the
/// result in the target array.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <returns>flipped pixels</returns>
public static T[] FlipY<T>(in T[] source, in T[] target, in int w, in int h)
{
int srcLen = source.Length;
if (source == target)
{
int hd2 = h / 2;
int hn1 = h - 1;
int lenHalf = w * hd2;
for (int i = 0; i < lenHalf; ++i)
{
int j = i % w + w * (hn1 - i / w);
(source[j], source[i]) = (source[i], source[j]);
}
}
else if (srcLen == target.Length)
{
int hn1 = h - 1;
for (int i = 0; i < srcLen; ++i)
{
// TODO: Multiply hn1 * w before the loop then just y * w within?
target[(hn1 - i / w) * w + i % w] = source[i];
}
}
return target;
}
/// <summary>
/// Gets a region defined by a top left and bottom right corner. May return
/// an empty array if coordinates are invalid. Same as getting a cropped
/// copy of an image.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="wSrc">source image width</param>
/// <param name="hSrc">source image height</param>
/// <param name="xtl">top left corner x</param>
/// <param name="ytl">top left corner y</param>
/// <param name="xbr">bottom right corner x</param>
/// <param name="ybr">bottom right corner y</param>
/// <returns>cropped region</returns>
public static (T[] pixels, int w, int h, int x, int y) GetRegion<T>(
in T[] source,
in int wSrc, in int hSrc,
in int xtl, in int ytl,
in int xbr, in int ybr)
{
int xtlVrf = xtl < 0 ? 0 : xtl > wSrc - 1 ? wSrc - 1 : xtl;
int ytlVrf = ytl < 0 ? 0 : ytl > hSrc - 1 ? hSrc - 1 : ytl;
int xbrVrf = xbr < 0 ? 0 : xbr > wSrc - 1 ? wSrc - 1 : xbr;
int ybrVrf = ybr < 0 ? 0 : ybr > hSrc - 1 ? hSrc - 1 : ybr;
// Swap pixels if in wrong order.
xtlVrf = Math.Min(xtlVrf, xbrVrf);
ytlVrf = Math.Min(ytlVrf, ybrVrf);
xbrVrf = Math.Max(xtlVrf, xbrVrf);
ybrVrf = Math.Max(ytlVrf, ybrVrf);
int wTrg = 1 + xbrVrf - xtlVrf;
int hTrg = 1 + ybrVrf - ytlVrf;
if (wTrg > 0 && hTrg > 0)
{
int trgLen = wTrg * hTrg;
T[] target = new T[trgLen];
for (int i = 0; i < trgLen; ++i)
{
int xTrg = i % wTrg;
int yTrg = i / wTrg;
int xSrc = xtlVrf + xTrg;
int ySrc = ytlVrf + yTrg;
int j = xSrc + ySrc * wSrc;
target[i] = source[j];
}
return (pixels: target, w: wTrg, h: hTrg, x: xtlVrf, y: ytlVrf);
}
return (pixels: new T[0], w: 0, h: 0, x: 0, y: 0);
}
/// <summary>
/// Mirrors, or reflects, pixels from a source image horizontally
/// across a pivot. The pivot is expected to be in [-1, width + 1].
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="pivot">pivot</param>
/// <param name="flip">flip reflection flag</param>
/// <returns>mirrored pixels</returns>
public static T[] MirrorX<T>(
in T[] source, in T[] target, in int w,
in int pivot = -1, in bool flip = false)
{
int trgLen = target.Length;
int flipSign = flip ? 1 : -1;
for (int i = 0; i < trgLen; ++i)
{
int cross = i % w - pivot;
if (flipSign * cross < 0)
{
target[i] = source[i];
}
else
{
int pxOpp = pivot - cross;
if (pxOpp > -1 && pxOpp < w)
{
target[i] = source[i / w * w + pxOpp];
}
else
{
target[i] = default;
}
}
}
return target;
}
/// <summary>
/// Mirrors, or reflects, pixels from a source image vertically
/// across a pivot. The pivot is expected to be in [-1, height + 1].
/// Positive y points down to the bottom of the image.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <param name="pivot">pivot</param>
/// <param name="flip">flip reflection flag</param>
/// <param name="target">target pixels</param>
/// <returns>mirrored pixels</returns>
public static T[] MirrorY<T>(
in T[] source, in T[] target, in int w, in int h,
in int pivot = -1, in bool flip = false)
{
int trgLen = target.Length;
int flipSign = flip ? 1 : -1;
for (int i = 0; i < trgLen; ++i)
{
int cross = i / w - pivot;
if (flipSign * cross < 0)
{
target[i] = source[i];
}
else
{
int pyOpp = pivot - cross;
if (pyOpp > -1 && pyOpp < h)
{
target[i] = source[pyOpp * w + i % w];
}
else
{
target[i] = default;
}
}
}
return target;
}
/// <summary>
/// Rotates the source pixel array 90 degrees counter-clockwise. The
/// rotation is stored in the target pixel array. The rotated image's
/// width and height are swapped.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <returns>rotated pixels</returns>
public static T[] Rotate90<T>(in T[] source, in T[] target, in int w, in int h)
{
int srcLen = source.Length;
if (srcLen == target.Length)
{
int srcLennh = srcLen - h;
for (int i = 0; i < srcLen; ++i)
{
target[srcLennh + i / w - i % w * h] = source[i];
}
}
return target;
}
/// <summary>
/// Rotates the source pixel array 180 degrees counter-clockwise. The
/// rotation is stored in the target pixel array.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <returns>rotated pixels</returns>
public static T[] Rotate180<T>(in T[] source, in T[] target)
{
int srcLen = source.Length;
if (source == target)
{
int srcHalfLen = srcLen / 2;
for (int i = 0, j = srcLen - 1; i < srcHalfLen; ++i, --j)
{
(source[j], source[i]) = (source[i], source[j]);
}
}
else if (srcLen == target.Length)
{
for (int i = 0, j = srcLen - 1; i < srcLen; ++i, --j)
{
target[j] = source[i];
}
}
return target;
}
/// <summary>
/// Rotates the source pixel array 270 degrees counter-clockwise. The
/// rotation is stored in the target pixel array. The rotated image's
/// width and height are swapped.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <returns>rotated pixels</returns>
public static T[] Rotate270<T>(in T[] source, in T[] target, in int w, in int h)
{
int srcLen = source.Length;
if (srcLen == target.Length)
{
int hn1 = h - 1;
for (int i = 0; i < srcLen; ++i)
{
target[i % w * h + hn1 - i / w] = source[i];
}
}
return target;
}
/// <summary>
/// Sets a region of pixels on the target array sfrom the source.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">source pixels</param>
/// <param name="wSrc">source image width</param>
/// <param name="hSrc">source image height</param>
/// <param name="wTrg">source image width</param>
/// <param name="hTrg">source image height</param>
public static void SetRegion<T>(
in T[] source, in T[] target,
in int wSrc, in int hSrc,
in int wTrg, in int hTrg)
{
Pixels.SetRegion(
source, target,
wSrc, hSrc,
wTrg, hTrg,
0, 0, wTrg - 1, hTrg - 1);
}
/// <summary>
/// Sets a region defined by a "write" top left and bottom right corners on
/// the target array from the source.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">source pixels</param>
/// <param name="wSrc">source image width</param>
/// <param name="hSrc">source image height</param>
/// <param name="wTrg">source image width</param>
/// <param name="hTrg">source image height</param>
/// <param name="xtlWrite">top left corner x</param>
/// <param name="ytlWrite">top left corner y</param>
/// <param name="xbrWrite">bottom right corner x</param>
/// <param name="ybrWrite">bottom right corner y</param>
public static void SetRegion<T>(
in T[] source, in T[] target,
in int wSrc, in int hSrc,
in int wTrg, in int hTrg,
in int xtlWrite, in int ytlWrite,
in int xbrWrite, in int ybrWrite)
{
Pixels.SetRegion(
source, target,
wSrc, hSrc,
wTrg, hTrg,
xtlWrite, ytlWrite, xbrWrite, ybrWrite,
0, 0, wSrc - 1, hSrc - 1);
}
/// <summary>
/// Sets a region defined by a "write" top left and bottom right
/// corners on the target array from the source. The "read" corners
/// specify a region from the source image.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">source pixels</param>
/// <param name="wSrc">source image width</param>
/// <param name="hSrc">source image height</param>
/// <param name="wTrg">source image width</param>
/// <param name="hTrg">source image height</param>
/// <param name="xtlWrite">top left corner x</param>
/// <param name="ytlWrite">top left corner y</param>
/// <param name="xbrWrite">bottom right corner x</param>
/// <param name="ybrWrite">bottom right corner y</param>
/// <param name="xtlRead">top left corner x</param>
/// <param name="ytlRead">top left corner y</param>
/// <param name="xbrRead">bottom right corner x</param>
/// <param name="ybrRead">bottom right corner y</param>
public static void SetRegion<T>(
in T[] source, in T[] target,
in int wSrc, in int hSrc,
in int wTrg, in int hTrg,
in int xtlWrite, in int ytlWrite,
in int xbrWrite, in int ybrWrite,
in int xtlRead, in int ytlRead,
in int xbrRead, in int ybrRead)
{
// TODO: Test
int xtlWrVrf = xtlWrite < 0 ? 0 : xtlWrite > wTrg - 1 ? wTrg - 1 : xtlWrite;
int ytlWrVrf = ytlWrite < 0 ? 0 : ytlWrite > hTrg - 1 ? hTrg - 1 : ytlWrite;
int xbrWrVrf = xbrWrite < 0 ? 0 : xbrWrite > wTrg - 1 ? wTrg - 1 : xbrWrite;
int ybrWrVrf = ybrWrite < 0 ? 0 : ybrWrite > hTrg - 1 ? hTrg - 1 : ybrWrite;
xtlWrVrf = Math.Min(xtlWrVrf, xbrWrVrf);
ytlWrVrf = Math.Min(ytlWrVrf, ybrWrVrf);
xbrWrVrf = Math.Max(xtlWrVrf, xbrWrVrf);
ybrWrVrf = Math.Max(ytlWrVrf, ybrWrVrf);
int xtlRdVrf = xtlRead < 0 ? 0 : xtlRead > wSrc - 1 ? wSrc - 1 : xtlRead;
int ytlRdVrf = ytlRead < 0 ? 0 : ytlRead > hSrc - 1 ? hSrc - 1 : ytlRead;
int xbrRdVrf = xbrRead < 0 ? 0 : xbrRead > wSrc - 1 ? wSrc - 1 : xbrRead;
int ybrRdVrf = ybrRead < 0 ? 0 : ybrRead > hSrc - 1 ? hSrc - 1 : ybrRead;
xtlRdVrf = Math.Min(xtlRdVrf, xbrRdVrf);
ytlRdVrf = Math.Min(ytlRdVrf, ybrRdVrf);
xbrRdVrf = Math.Max(xtlRdVrf, xbrRdVrf);
ybrRdVrf = Math.Max(ytlRdVrf, ybrRdVrf);
int wReg = Math.Min(1 + xbrWrVrf - xtlWrVrf,
1 + xbrRdVrf - xtlRdVrf);
int hReg = Math.Min(1 + ybrWrVrf - ytlWrVrf,
1 + ybrRdVrf - ytlRdVrf);
if (wReg > 0 && hReg > 0)
{
int trgLen = wReg * hReg;
for (int i = 0; i < trgLen; ++i)
{
int xReg = i % wReg;
int yReg = i / wReg;
int xSrc = xtlWrVrf + xReg;
int ySrc = ytlWrVrf + yReg;
int j = xSrc + ySrc * wTrg;
int xTrg = xtlRdVrf + xReg;
int yTrg = ytlRdVrf + yReg;
int k = xTrg + yTrg * wSrc;
target[j] = source[k];
}
}
}
/// <summary>
/// Skews the pixels of a source image horizontally by an integer rise. The
/// run specifies the number of pixels to skip on the x axis for each rise.
/// If the rise or run are zero, copies the source array.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="wSrc">image width/param>
/// <param name="hSrc">image height</param>
/// <param name="rise">rise, or step/param>
/// <param name="run">run, or skip/param>
/// <returns>tuple</returns>
public static (T[] pixels, int w, int h) SkewXInteger<T>(
in T[] source,
in int wSrc, in int hSrc,
in int rise = 1, in int run = 1)
{
int srcLen = source.Length;
if (rise == 0 || run == 0)
{
T[] t0 = new T[srcLen];
System.Array.Copy(source, 0, t0, 0, srcLen);
return (pixels: t0, w: wSrc, h: hSrc);
}
int absRun = run < 0 ? -run : run;
int sgnRise = run < 0 ? -rise : rise;
int absRise = sgnRise < 0 ? -sgnRise : sgnRise;
int hn1Run = (hSrc - 1) / absRun;
int offset = sgnRise < 0 ? 0 : hn1Run * sgnRise;
int wTrg = wSrc + hn1Run * absRise;
T[] target = new T[wTrg * hSrc];
for (int i = 0; i < srcLen; ++i)
{
int xSrc = i % wSrc;
int ySrc = i / wSrc;
int shift = sgnRise * (ySrc / absRun);
int xTrg = xSrc + offset - shift;
target[xTrg + ySrc * wTrg] = source[i];
}
return (pixels: target, w: wTrg, h: hSrc);
}
/// <summary>
/// Skews the pixels of a source image vertically by an integer rise. The
/// run specifies the number of pixels to skip on the y axis for each rise.
/// If the rise or run are zero, copies the source array.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="wSrc">image width/param>
/// <param name="hSrc">image height</param>
/// <param name="rise">rise, or step/param>
/// <param name="run">run, or skip/param>
/// <returns>tuple</returns>
public static (T[] pixels, int w, int h) SkewYInteger<T>(
in T[] source,
in int wSrc, in int hSrc,
in int rise = 1, in int run = 1)
{
int srcLen = source.Length;
if (rise == 0 || run == 0)
{
T[] t0 = new T[srcLen];
System.Array.Copy(source, 0, t0, 0, srcLen);
return (pixels: t0, w: wSrc, h: hSrc);
}
int absRun = run < 0 ? -run : run;
int sgnRise = run < 0 ? -rise : rise;
int absRise = sgnRise < 0 ? -sgnRise : sgnRise;
int wn1Run = (wSrc - 1) / absRun;
int offset = sgnRise < 0 ? 0 : wn1Run * sgnRise;
int hTrg = hSrc + wn1Run * absRise;
T[] target = new T[wSrc * hTrg];
for (int i = 0; i < srcLen; ++i)
{
int xSrc = i % wSrc;
int ySrc = i / wSrc;
int shift = sgnRise * (xSrc / absRun);
int yTrg = ySrc + offset - shift;
target[xSrc + yTrg * wSrc] = source[i];
}
return (pixels: target, w: wSrc, h: hTrg);
}
/// <summary>
/// Transposes the source array. The transposition is stored in the target
/// pixel array. The transposed image's width and height are swapped.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="w">image width</param>
/// <param name="h">image height</param>
/// <returns>transposed pixels</returns>
public static T[] Transpose<T>(
in T[] source, in T[] target,
in int w, in int h)
{
int srcLen = source.Length;
if (srcLen == target.Length)
{
for (int i = 0; i < srcLen; ++i)
{
target[i % w * h + i / w] = source[i];
}
}
return target;
}
/// <summary>
/// Blits a source image's pixels onto a target image's pixels, using
/// integer floor modulo to wrap the source image. The source image can be
/// offset horizontally and/or vertically, creating the illusion of
/// parallax.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="wSrc">source image width</param>
/// <param name="hSrc">source image height</param>
/// <param name="wTrg">target image width</param>
/// <param name="dx">horizontal pixel offset</param>
/// <param name="dy">vertical pixel offset</param>
/// <returns>wrapped pixels</returns>
public static T[] Wrap<T>(
in T[] source, in T[] target,
in int wSrc, in int hSrc, in int wTrg,
in int dx, in int dy)
{
int trgLen = target.Length;
for (int i = 0; i < trgLen; ++i)
{
int yMod = (i / wTrg + dy) % hSrc;
if ((yMod ^ hSrc) < 0 && yMod != 0) { yMod += hSrc; }
int xMod = (i % wTrg - dx) % wSrc;
if ((xMod ^ wSrc) < 0 && xMod != 0) { xMod += wSrc; }
target[i] = source[xMod + wSrc * yMod];
}
return target;
}
#endregion
#region Specific
/// <summary>
/// Multiplies a source pixels array's alpha by the input alpha. If the
/// alpha is less than zero, sets the target array to clear black. If the
/// alpha is approximately one, copies the source pixels to the target.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="alpha">alpha</param>
/// <returns>adjusted pixels</returns>
public static Rgb[] AdjustAlpha(in Rgb[] source, in Rgb[] target, in float alpha)
{
int srcLen = source.Length;
if (srcLen == target.Length)
{
if (alpha <= 0.0f)
{
Pixels.Fill(Rgb.ClearBlack, target);
return target;
}
if (Utils.Approx(alpha, 1.0f))
{
System.Array.Copy(source, 0, target, 0, srcLen);
return target;
}
for (int i = 0; i < srcLen; ++i)
{
Rgb c = source[i];
target[i] = new Rgb(c.R, c.G, c.B, c.Alpha * alpha);
}
}
return target;
}
/// <summary>
/// Adjusts a source pixels array's colors in LCH. Assigns the results
/// to a target array.
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="adjust">adjustment</param>
/// <returns>adjusted pixels</returns>
public static Rgb[] AdjustSrLch(in Rgb[] source, in Rgb[] target, in Lch adjust)
{
// TODO: Make AdjustSrLab2 method?
int srcLen = source.Length;
if (srcLen == target.Length)
{
if (adjust.Alpha == 0.0f &&
adjust.L == 0.0f &&
adjust.C == 0.0f &&
adjust.H == 0.0f)
{
System.Array.Copy(source, 0, target, 0, srcLen);
return target;
}
Dictionary<Rgb, Rgb> dict = new();
for (int i = 0; i < srcLen; ++i)
{
Rgb c = source[i];
if (Rgb.Any(c) && !dict.ContainsKey(c))
{
Lch lch = Rgb.StandardToSrLch(c);
dict.Add(c, Rgb.SrLchToStandard(lch + adjust));
}
}
if (dict.Count > 0)
{
for (int j = 0; j < srcLen; ++j)
{
Rgb c = source[j];
target[j] = dict.GetValueOrDefault(c, Rgb.ClearBlack);
}
}
else
{
Pixels.Fill(Rgb.ClearBlack, target);
}
}
return target;
}
/// <summary>
/// Adjusts the contrast of colors from a source pixels array by a factor.
/// Uses the SR LAB 2 color space. The adjustment factor is expected to be
/// in [-1.0, 1.0].
/// </summary>
/// <param name="source">source pixels</param>
/// <param name="target">target pixels</param>
/// <param name="fac">factor</param>
/// <returns>adjusted pixels</returns>
public static Rgb[] AdjustContrast(in Rgb[] source, in Rgb[] target, in float fac)
{
int srcLen = source.Length;
if (srcLen == target.Length)
{
float valAdjust = 1.0f + Utils.Clamp(fac, -1.0f, 1.0f);
if (Utils.Approx(valAdjust, 1.0f))
{
System.Array.Copy(source, 0, target, 0, srcLen);
return target;
}
Dictionary<Rgb, Rgb> dict = new();
for (int i = 0; i < srcLen; ++i)
{
Rgb c = source[i];
if (Rgb.Any(c))
{
Rgb key = Rgb.Opaque(c);
if (!dict.ContainsKey(key))
{
Lab lab = Rgb.StandardToSrLab2(key);
Lab labAdj = new(
l: (lab.L - 50.0f) * valAdjust + 50.0f,
a: lab.A,
b: lab.B,
alpha: lab.Alpha);
dict.Add(key, Rgb.SrLab2ToStandard(labAdj));
}
}
}
if (dict.Count > 0)
{
Rgb clearBlack = Rgb.ClearBlack;
for (int j = 0; j < srcLen; ++j)
{
Rgb c = source[j];
target[j] = Rgb.CopyAlpha(
dict.GetValueOrDefault(Rgb.Opaque(c),
clearBlack), c);
}
}
else
{
Pixels.Fill(Rgb.ClearBlack, target);
}
}
return target;
}
/// <summary>
/// Blends backdrop and overlay pixels using a function provided by the
/// caller. Forms a union of the bounding area of the two inputs.
/// Returns a tuple containing the blended pixels, the image's width and
/// height, and the top left corner x and y.
/// </summary>
/// <param name="apx">under image pixels</param>
/// <param name="bpx">over image pixels</param>
/// <param name="aw">under width</param>
/// <param name="ah">under height</param>
/// <param name="bw">over width</param>
/// <param name="bh">over height</param>
/// <param name="func">blend function</param>
/// <returns>tuple</returns>
public static (Rgb[] pixels, int w, int h, int x, int y) Blend(
in Rgb[] apx, in Rgb[] bpx,
in int aw, in int ah, in int bw, in int bh,
in Func<Rgb, Rgb, Rgb> func)
{
int wLrg = aw > bw ? aw : bw;
int hLrg = ah > bh ? ah : bh;
// The 0.5 is to bias the rounding.
float cx = wLrg * 0.5f + 0.5f;
float cy = hLrg * 0.5f + 0.5f;
int ax = aw == wLrg ? 0 : (int)(cx - aw * 0.5f);
int ay = ah == hLrg ? 0 : (int)(cy - ah * 0.5f);
int bx = bw == wLrg ? 0 : (int)(cx - bw * 0.5f);
int by = bh == hLrg ? 0 : (int)(cy - bh * 0.5f);
return Pixels.Blend(apx, bpx,
aw, ah, bw, bh,
ax, ay, bx, by, func);
}
/// <summary>
/// Blends backdrop and overlay pixels using a function provided by the
/// caller. Forms a union of the bounding area of the two inputs.
/// Returns a tuple containing the blended pixels, the image's width and
/// height, and the top left corner x and y.
/// </summary>
/// <param name="apx">under image pixels</param>
/// <param name="bpx">over image pixels</param>
/// <param name="aw">under width</param>
/// <param name="ah">under height</param>
/// <param name="bw">over width</param>
/// <param name="bh">over height</param>
/// <param name="ax">under top left corner x</param>
/// <param name="ay">under top left corner y</param>
/// <param name="bx">over top left corner x</param>
/// <param name="by">over top left corner y</param>
/// <param name="func">blend function</param>
/// <returns>tuple</returns>
public static (Rgb[] pixels, int w, int h, int x, int y) Blend(
in Rgb[] apx, in Rgb[] bpx,
in int aw, in int ah, in int bw, in int bh,
in int ax, in int ay, in int bx, in int by,
in Func<Rgb, Rgb, Rgb> func)
{
int cx = ax < bx ? ax : bx;
int cy = ay < by ? ay : by;
int cw = aw > bw ? aw : bw;
int ch = ah > bh ? ah : bh;
int cLen = cw * ch;
int axud = ax - cx;
int ayud = ay - cy;
int bxud = bx - cx;
int byud = by - cy;
Rgb[] target = new Rgb[cLen];
for (int i = 0; i < cLen; ++i)
{
int x = i % cw;
int y = i / cw;
Rgb aClr = Rgb.ClearBlack;
int axs = x - axud;
int ays = y - ayud;
if (ays > -1 && ays < ah && axs > -1 && axs < aw)
{
aClr = apx[axs + ays * aw];
}
Rgb bClr = Rgb.ClearBlack;
int bxs = x - bxud;
int bys = y - byud;
if (bys > -1 && bys < bh && bxs > -1 && bxs < bw)
{
bClr = bpx[bxs + bys * bw];
}
target[i] = func(aClr, bClr);
}
return (pixels: target, w: cw, h: ch, x: cx, y: cy);
}
/// <summary>
/// Blends backdrop and overlay pixels in SR LAB 2. Forms a union of the
/// bounding area of the two inputs. Returns a tuple containing the blended
/// pixels, the image's width and height, and the top left corner x and y.
/// </summary>
/// <param name="apx">under image pixels</param>
/// <param name="bpx">over image pixels</param>
/// <param name="aw">under width</param>
/// <param name="ah">under height</param>
/// <param name="bw">over width</param>
/// <param name="bh">over height</param>
/// <returns>tuple</returns>
public static (Rgb[] pixels, int w, int h, int x, int y) BlendSrLab2(
in Rgb[] apx, in Rgb[] bpx,
in int aw, in int ah, in int bw, in int bh)
{
int wLrg = aw > bw ? aw : bw;
int hLrg = ah > bh ? ah : bh;
// The 0.5 is to bias the rounding.
float cx = wLrg * 0.5f + 0.5f;
float cy = hLrg * 0.5f + 0.5f;
int ax = aw == wLrg ? 0 : (int)(cx - aw * 0.5f);
int ay = ah == hLrg ? 0 : (int)(cy - ah * 0.5f);
int bx = bw == wLrg ? 0 : (int)(cx - bw * 0.5f);
int by = bh == hLrg ? 0 : (int)(cy - bh * 0.5f);
return Pixels.BlendSrLab2(apx, bpx,
aw, ah, bw, bh,
ax, ay, bx, by);
}
/// <summary>
/// Blends backdrop and overlay pixels in SR LAB 2. Forms a union of the
/// bounding area of the two inputs. Returns a tuple containing the blended
/// pixels, the image's width and height, and the top left corner x and y.
/// </summary>
/// <param name="apx">under image pixels</param>
/// <param name="bpx">over image pixels</param>
/// <param name="aw">under width</param>
/// <param name="ah">under height</param>
/// <param name="bw">over width</param>
/// <param name="bh">over height</param>
/// <param name="ax">under top left corner x</param>
/// <param name="ay">under top left corner y</param>
/// <param name="bx">over top left corner x</param>
/// <param name="by">over top left corner y</param>
/// <returns>tuple</returns>
public static (Rgb[] pixels, int w, int h, int x, int y) BlendSrLab2(
in Rgb[] apx, in Rgb[] bpx,
in int aw, in int ah, in int bw, in int bh,
in int ax, in int ay, in int bx, in int by)
{
// Find the bottom right corner for a and b.
int abrx = ax + aw - 1;
int abry = ay + ah - 1;
int bbrx = bx + bw - 1;
int bbry = by + bh - 1;