forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixmappaint.d
1475 lines (1296 loc) · 29.5 KB
/
pixmappaint.d
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
/+
== pixmappaint ==
Copyright Elias Batek (0xEAB) 2024.
Distributed under the Boost Software License, Version 1.0.
+/
/++
Pixmap image manipulation
$(WARNING
$(B Early Technology Preview.)
)
$(PITFALL
This module is $(B work in progress).
API is subject to changes until further notice.
)
+/
module arsd.pixmappaint;
import arsd.color;
import arsd.core;
private float hackyRound(float f) {
import std.math : round;
return round(f);
}
float round(float f) pure @nogc nothrow @trusted {
return (cast(float function(float) pure @nogc nothrow) &hackyRound)(f);
}
/*
## TODO:
- Refactoring the template-mess of blendPixel() & co.
- Scaling
- Cropping
- Rotating
- Skewing
- HSL
- Advanced blend modes (maybe)
*/
///
alias Color = arsd.color.Color;
///
alias ColorF = arsd.color.ColorF;
///
alias Pixel = Color;
///
alias Point = arsd.color.Point;
///
alias Rectangle = arsd.color.Rectangle;
///
alias Size = arsd.color.Size;
// verify assumption(s)
static assert(Pixel.sizeof == uint.sizeof);
@safe pure nothrow @nogc {
///
Pixel rgba(ubyte r, ubyte g, ubyte b, ubyte a = 0xFF) {
return Pixel(r, g, b, a);
}
///
Pixel rgba(ubyte r, ubyte g, ubyte b, float aPct)
in (aPct >= 0 && aPct <= 1) {
return Pixel(r, g, b, castTo!ubyte(aPct * 255));
}
///
Pixel rgb(ubyte r, ubyte g, ubyte b) {
return rgba(r, g, b, 0xFF);
}
}
/++
Pixel data container
+/
struct Pixmap {
/// Pixel data
Pixel[] data;
/// Pixel per row
int width;
@safe pure nothrow:
///
this(Size size) {
this.size = size;
}
///
this(int width, int height)
in (width > 0)
in (height > 0) {
this(Size(width, height));
}
///
this(Pixel[] data, int width) @nogc
in (data.length % width == 0) {
this.data = data;
this.width = width;
}
/++
Creates a $(I deep clone) of the Pixmap
+/
Pixmap clone() const {
auto c = Pixmap();
c.width = this.width;
c.data = this.data.dup;
return c;
}
// undocumented: really shouldn’t be used.
// carries the risks of `length` and `width` getting out of sync accidentally.
deprecated("Use `size` instead.")
void length(int value) {
data.length = value;
}
/++
Changes the size of the buffer
Reallocates the underlying pixel array.
+/
void size(Size value) {
data.length = value.area;
width = value.width;
}
/// ditto
void size(int totalPixels, int width)
in (totalPixels % width == 0) {
data.length = totalPixels;
this.width = width;
}
static {
/++
Creates a Pixmap wrapping the pixel data from the provided `TrueColorImage`.
Interoperability function: `arsd.color`
+/
Pixmap fromTrueColorImage(TrueColorImage source) @nogc {
return Pixmap(source.imageData.colors, source.width);
}
/++
Creates a Pixmap wrapping the pixel data from the provided `MemoryImage`.
Interoperability function: `arsd.color`
+/
Pixmap fromMemoryImage(MemoryImage source) {
return fromTrueColorImage(source.getAsTrueColorImage());
}
}
@safe pure nothrow @nogc:
/// Height of the buffer, i.e. the number of lines
int height() inout {
if (width == 0) {
return 0;
}
return castTo!int(data.length / width);
}
/// Rectangular size of the buffer
Size size() inout {
return Size(width, height);
}
/// Length of the buffer, i.e. the number of pixels
int length() inout {
return castTo!int(data.length);
}
/++
Number of bytes per line
Returns:
width × Pixel.sizeof
+/
int pitch() inout {
return (width * int(Pixel.sizeof));
}
/++
Retrieves a linear slice of the pixmap.
Returns:
`n` pixels starting at the top-left position `pos`.
+/
inout(Pixel)[] sliceAt(Point pos, int n) inout {
immutable size_t offset = linearOffset(width, pos);
immutable size_t end = (offset + n);
return data[offset .. end];
}
/// Clears the buffer’s contents (by setting each pixel to the same color)
void clear(Pixel value) {
data[] = value;
}
}
///
struct SpriteSheet {
private {
Pixmap _pixmap;
Size _spriteDimensions;
Size _layout; // pre-computed upon construction
}
@safe pure nothrow @nogc:
///
public this(Pixmap pixmap, Size spriteSize) {
_pixmap = pixmap;
_spriteDimensions = spriteSize;
_layout = Size(
_pixmap.width / _spriteDimensions.width,
_pixmap.height / _spriteDimensions.height,
);
}
///
inout(Pixmap) pixmap() inout {
return _pixmap;
}
///
Size spriteSize() inout {
return _spriteDimensions;
}
///
Size layout() inout {
return _layout;
}
///
Point getSpriteColumn(int index) inout {
immutable x = index % layout.width;
immutable y = (index - x) / layout.height;
return Point(x, y);
}
///
Point getSpritePixelOffset2D(int index) inout {
immutable col = this.getSpriteColumn(index);
return Point(
col.x * _spriteDimensions.width,
col.y * _spriteDimensions.height,
);
}
}
// Silly micro-optimization
private struct OriginRectangle {
Size size;
@safe pure nothrow @nogc:
int left() const => 0;
int top() const => 0;
int right() const => size.width;
int bottom() const => size.height;
bool intersect(const Rectangle b) const {
// dfmt off
return (
(b.right > 0 ) &&
(b.left < this.right ) &&
(b.bottom > 0 ) &&
(b.top < this.bottom)
);
// dfmt on
}
}
@safe pure nothrow @nogc:
// misc
private {
Point pos(Rectangle r) => r.upperLeft;
T max(T)(T a, T b) => (a >= b) ? a : b;
T min(T)(T a, T b) => (a <= b) ? a : b;
}
/++
Calculates the square root
of an integer number
as an integer number.
+/
ubyte intSqrt(const ubyte value) @safe pure nothrow @nogc {
switch (value) {
default:
// unreachable
assert(false, "ubyte != uint8");
case 0:
return 0;
case 1: .. case 2:
return 1;
case 3: .. case 6:
return 2;
case 7: .. case 12:
return 3;
case 13: .. case 20:
return 4;
case 21: .. case 30:
return 5;
case 31: .. case 42:
return 6;
case 43: .. case 56:
return 7;
case 57: .. case 72:
return 8;
case 73: .. case 90:
return 9;
case 91: .. case 110:
return 10;
case 111: .. case 132:
return 11;
case 133: .. case 156:
return 12;
case 157: .. case 182:
return 13;
case 183: .. case 210:
return 14;
case 211: .. case 240:
return 15;
case 241: .. case 255:
return 16;
}
}
///
unittest {
assert(intSqrt(4) == 2);
assert(intSqrt(9) == 3);
assert(intSqrt(10) == 3);
}
unittest {
import std.math : round, sqrt;
foreach (n; ubyte.min .. ubyte.max + 1) {
ubyte fp = sqrt(float(n)).round().castTo!ubyte;
ubyte i8 = intSqrt(n.castTo!ubyte);
assert(fp == i8);
}
}
/++
Calculates the square root
of the normalized value
representated by the input integer number.
Normalization:
`[0x00 .. 0xFF]` → `[0.0 .. 1.0]`
Returns:
sqrt(value / 255f) * 255
+/
ubyte intNormalizedSqrt(const ubyte value) {
switch (value) {
default:
// unreachable
assert(false, "ubyte != uint8");
case 0x00:
return 0x00;
case 0x01:
return 0x10;
case 0x02:
return 0x17;
case 0x03:
return 0x1C;
case 0x04:
return 0x20;
case 0x05:
return 0x24;
case 0x06:
return 0x27;
case 0x07:
return 0x2A;
case 0x08:
return 0x2D;
case 0x09:
return 0x30;
case 0x0A:
return 0x32;
case 0x0B:
return 0x35;
case 0x0C:
return 0x37;
case 0x0D:
return 0x3A;
case 0x0E:
return 0x3C;
case 0x0F:
return 0x3E;
case 0x10:
return 0x40;
case 0x11:
return 0x42;
case 0x12:
return 0x44;
case 0x13:
return 0x46;
case 0x14:
return 0x47;
case 0x15:
return 0x49;
case 0x16:
return 0x4B;
case 0x17:
return 0x4D;
case 0x18:
return 0x4E;
case 0x19:
return 0x50;
case 0x1A:
return 0x51;
case 0x1B:
return 0x53;
case 0x1C:
return 0x54;
case 0x1D:
return 0x56;
case 0x1E:
return 0x57;
case 0x1F:
return 0x59;
case 0x20:
return 0x5A;
case 0x21:
return 0x5C;
case 0x22:
return 0x5D;
case 0x23:
return 0x5E;
case 0x24:
return 0x60;
case 0x25:
return 0x61;
case 0x26:
return 0x62;
case 0x27:
return 0x64;
case 0x28:
return 0x65;
case 0x29:
return 0x66;
case 0x2A:
return 0x67;
case 0x2B:
return 0x69;
case 0x2C:
return 0x6A;
case 0x2D:
return 0x6B;
case 0x2E:
return 0x6C;
case 0x2F:
return 0x6D;
case 0x30:
return 0x6F;
case 0x31:
return 0x70;
case 0x32:
return 0x71;
case 0x33:
return 0x72;
case 0x34:
return 0x73;
case 0x35:
return 0x74;
case 0x36:
return 0x75;
case 0x37:
return 0x76;
case 0x38:
return 0x77;
case 0x39:
return 0x79;
case 0x3A:
return 0x7A;
case 0x3B:
return 0x7B;
case 0x3C:
return 0x7C;
case 0x3D:
return 0x7D;
case 0x3E:
return 0x7E;
case 0x3F:
return 0x7F;
case 0x40:
return 0x80;
case 0x41:
return 0x81;
case 0x42:
return 0x82;
case 0x43:
return 0x83;
case 0x44:
return 0x84;
case 0x45:
return 0x85;
case 0x46:
return 0x86;
case 0x47: .. case 0x48:
return 0x87;
case 0x49:
return 0x88;
case 0x4A:
return 0x89;
case 0x4B:
return 0x8A;
case 0x4C:
return 0x8B;
case 0x4D:
return 0x8C;
case 0x4E:
return 0x8D;
case 0x4F:
return 0x8E;
case 0x50:
return 0x8F;
case 0x51:
return 0x90;
case 0x52: .. case 0x53:
return 0x91;
case 0x54:
return 0x92;
case 0x55:
return 0x93;
case 0x56:
return 0x94;
case 0x57:
return 0x95;
case 0x58:
return 0x96;
case 0x59: .. case 0x5A:
return 0x97;
case 0x5B:
return 0x98;
case 0x5C:
return 0x99;
case 0x5D:
return 0x9A;
case 0x5E:
return 0x9B;
case 0x5F: .. case 0x60:
return 0x9C;
case 0x61:
return 0x9D;
case 0x62:
return 0x9E;
case 0x63:
return 0x9F;
case 0x64: .. case 0x65:
return 0xA0;
case 0x66:
return 0xA1;
case 0x67:
return 0xA2;
case 0x68:
return 0xA3;
case 0x69: .. case 0x6A:
return 0xA4;
case 0x6B:
return 0xA5;
case 0x6C:
return 0xA6;
case 0x6D: .. case 0x6E:
return 0xA7;
case 0x6F:
return 0xA8;
case 0x70:
return 0xA9;
case 0x71: .. case 0x72:
return 0xAA;
case 0x73:
return 0xAB;
case 0x74:
return 0xAC;
case 0x75: .. case 0x76:
return 0xAD;
case 0x77:
return 0xAE;
case 0x78:
return 0xAF;
case 0x79: .. case 0x7A:
return 0xB0;
case 0x7B:
return 0xB1;
case 0x7C:
return 0xB2;
case 0x7D: .. case 0x7E:
return 0xB3;
case 0x7F:
return 0xB4;
case 0x80: .. case 0x81:
return 0xB5;
case 0x82:
return 0xB6;
case 0x83: .. case 0x84:
return 0xB7;
case 0x85:
return 0xB8;
case 0x86:
return 0xB9;
case 0x87: .. case 0x88:
return 0xBA;
case 0x89:
return 0xBB;
case 0x8A: .. case 0x8B:
return 0xBC;
case 0x8C:
return 0xBD;
case 0x8D: .. case 0x8E:
return 0xBE;
case 0x8F:
return 0xBF;
case 0x90: .. case 0x91:
return 0xC0;
case 0x92:
return 0xC1;
case 0x93: .. case 0x94:
return 0xC2;
case 0x95:
return 0xC3;
case 0x96: .. case 0x97:
return 0xC4;
case 0x98:
return 0xC5;
case 0x99: .. case 0x9A:
return 0xC6;
case 0x9B: .. case 0x9C:
return 0xC7;
case 0x9D:
return 0xC8;
case 0x9E: .. case 0x9F:
return 0xC9;
case 0xA0:
return 0xCA;
case 0xA1: .. case 0xA2:
return 0xCB;
case 0xA3: .. case 0xA4:
return 0xCC;
case 0xA5:
return 0xCD;
case 0xA6: .. case 0xA7:
return 0xCE;
case 0xA8:
return 0xCF;
case 0xA9: .. case 0xAA:
return 0xD0;
case 0xAB: .. case 0xAC:
return 0xD1;
case 0xAD:
return 0xD2;
case 0xAE: .. case 0xAF:
return 0xD3;
case 0xB0: .. case 0xB1:
return 0xD4;
case 0xB2:
return 0xD5;
case 0xB3: .. case 0xB4:
return 0xD6;
case 0xB5: .. case 0xB6:
return 0xD7;
case 0xB7:
return 0xD8;
case 0xB8: .. case 0xB9:
return 0xD9;
case 0xBA: .. case 0xBB:
return 0xDA;
case 0xBC:
return 0xDB;
case 0xBD: .. case 0xBE:
return 0xDC;
case 0xBF: .. case 0xC0:
return 0xDD;
case 0xC1: .. case 0xC2:
return 0xDE;
case 0xC3:
return 0xDF;
case 0xC4: .. case 0xC5:
return 0xE0;
case 0xC6: .. case 0xC7:
return 0xE1;
case 0xC8: .. case 0xC9:
return 0xE2;
case 0xCA:
return 0xE3;
case 0xCB: .. case 0xCC:
return 0xE4;
case 0xCD: .. case 0xCE:
return 0xE5;
case 0xCF: .. case 0xD0:
return 0xE6;
case 0xD1: .. case 0xD2:
return 0xE7;
case 0xD3:
return 0xE8;
case 0xD4: .. case 0xD5:
return 0xE9;
case 0xD6: .. case 0xD7:
return 0xEA;
case 0xD8: .. case 0xD9:
return 0xEB;
case 0xDA: .. case 0xDB:
return 0xEC;
case 0xDC: .. case 0xDD:
return 0xED;
case 0xDE: .. case 0xDF:
return 0xEE;
case 0xE0:
return 0xEF;
case 0xE1: .. case 0xE2:
return 0xF0;
case 0xE3: .. case 0xE4:
return 0xF1;
case 0xE5: .. case 0xE6:
return 0xF2;
case 0xE7: .. case 0xE8:
return 0xF3;
case 0xE9: .. case 0xEA:
return 0xF4;
case 0xEB: .. case 0xEC:
return 0xF5;
case 0xED: .. case 0xEE:
return 0xF6;
case 0xEF: .. case 0xF0:
return 0xF7;
case 0xF1: .. case 0xF2:
return 0xF8;
case 0xF3: .. case 0xF4:
return 0xF9;
case 0xF5: .. case 0xF6:
return 0xFA;
case 0xF7: .. case 0xF8:
return 0xFB;
case 0xF9: .. case 0xFA:
return 0xFC;
case 0xFB: .. case 0xFC:
return 0xFD;
case 0xFD: .. case 0xFE:
return 0xFE;
case 0xFF:
return 0xFF;
}
}
unittest {
import std.math : round, sqrt;
foreach (n; ubyte.min .. ubyte.max + 1) {
ubyte fp = (sqrt(n / 255.0f) * 255).round().castTo!ubyte;
ubyte i8 = intNormalizedSqrt(n.castTo!ubyte);
assert(fp == i8);
}
}
/++
Limits a value to a maximum of 0xFF (= 255).
+/
ubyte clamp255(Tint)(const Tint value) {
pragma(inline, true);
return (value < 0xFF) ? value.castTo!ubyte : 0xFF;
}
/++
Fast 8-bit “percentage” function
This function optimizes its runtime performance by substituting
the division by 255 with an approximation using bitshifts.
Nonetheless, its result are as accurate as a floating point
division with 64-bit precision.
Params:
nPercentage = percentage as the number of 255ths (“two hundred fifty-fifths”)
value = base value (“total”)
Returns:
`round(value * nPercentage / 255.0)`
+/
ubyte n255thsOf(const ubyte nPercentage, const ubyte value) {
immutable factor = (nPercentage | (nPercentage << 8));
return (((value * factor) + 0x8080) >> 16);
}
@safe unittest {
// Accuracy verification
static ubyte n255thsOfFP64(const ubyte nPercentage, const ubyte value) {
return (double(value) * double(nPercentage) / 255.0).round().castTo!ubyte();
}
for (int value = ubyte.min; value <= ubyte.max; ++value) {
for (int percent = ubyte.min; percent <= ubyte.max; ++percent) {
immutable v = cast(ubyte) value;
immutable p = cast(ubyte) percent;
immutable approximated = n255thsOf(p, v);
immutable precise = n255thsOfFP64(p, v);
assert(approximated == precise);
}
}
}
/++
Sets the opacity of a [Pixmap].
This lossy operation updates the alpha-channel value of each pixel.
→ `alpha *= opacity`
See_Also:
Use [opacityF] with opacity values in percent (%).
+/
void opacity(Pixmap pixmap, const ubyte opacity) {
foreach (ref px; pixmap.data) {
px.a = opacity.n255thsOf(px.a);
}
}
/++
Sets the opacity of a [Pixmap].
This lossy operation updates the alpha-channel value of each pixel.
→ `alpha *= opacity`
See_Also:
Use [opacity] with 8-bit integer opacity values (in 255ths).
+/
void opacityF(Pixmap pixmap, const float opacity)
in (opacity >= 0)
in (opacity <= 1.0) {
immutable opacity255 = round(opacity * 255).castTo!ubyte;
pixmap.opacity = opacity255;
}
/++
Inverts a color (to its negative color).
+/
Pixel invert(const Pixel color) {
return Pixel(
0xFF - color.r,
0xFF - color.g,
0xFF - color.b,
color.a,
);
}
/++
Inverts all colors to produce a $(B negative image).
$(TIP
Develops a positive image when applied to a negative one.
)
+/
void invert(Pixmap pixmap) {
foreach (ref px; pixmap.data) {
px = invert(px);
}
}
// ==== Blending functions ====
/++
Alpha-blending accuracy level
$(TIP
This primarily exists for performance reasons.
In my tests LLVM manages to auto-vectorize the RGB-only codepath significantly better,
while the codegen for the accurate RGBA path is pretty conservative.
This provides an optimization opportunity for use-cases
that don’t require an alpha-channel on the result.
)
+/
enum BlendAccuracy {
/++
Only RGB channels will have the correct result.
A(lpha) channel can contain any value.
Suitable for blending into non-transparent targets (e.g. framebuffer, canvas)
where the resulting alpha-channel (opacity) value does not matter.
+/
rgb = false,
/++
All RGBA channels will have the correct result.
Suitable for blending into transparent targets (e.g. images)
where the resulting alpha-channel (opacity) value matters.
Use this mode for image manipulation.
+/
rgba = true,
}
/++
Blend modes
$(NOTE
As blending operations are implemented as integer calculations,
results may be slightly less precise than those from image manipulation
programs using floating-point math.
)
See_Also:
<https://www.w3.org/TR/compositing/#blending>
+/
enum BlendMode {
///
none = 0,
///
replace = none,
///
normal = 1,
///
alpha = normal,
///
multiply,
///
screen,
///
overlay,
///
hardLight,
///
softLight,
///
darken,
///
lighten,
///
colorDodge,
///
colorBurn,
///
difference,
///
exclusion,
///
subtract,
///
divide,
}
///
alias Blend = BlendMode;
// undocumented
enum blendNormal = BlendMode.normal;
///
alias BlendFn = ubyte function(const ubyte background, const ubyte foreground) pure nothrow @nogc;
/++
Blends `source` into `target`
with respect to the opacity of the source image (as stored in the alpha channel).
See_Also:
[alphaBlendRGBA] and [alphaBlendRGB] are shorthand functions
in cases where no special blending algorithm is needed.
+/
template alphaBlend(BlendFn blend = null, BlendAccuracy accuracy = BlendAccuracy.rgba) {
/// ditto
public void alphaBlend(scope Pixel[] target, scope const Pixel[] source) @trusted
in (source.length == target.length) {
foreach (immutable idx, ref pxTarget; target) {
alphaBlend(pxTarget, source.ptr[idx]);
}
}