forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgrablend.pas
996 lines (900 loc) · 34.5 KB
/
bgrablend.pas
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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BGRABlend;
{ This unit contains pixel blending functions. They take a destination adress as parameter,
and draw pixels at this address with different blending modes. These functions are used
by many functions in BGRABitmap library to do the low level drawing. }
{$i bgrabitmap.inc}{$H+}
interface
uses
{$IFNDEF FPC}Types, GraphType, {$ENDIF} BGRATypes, BGRAGraphics, BGRABitmapTypes;
{ Draw one pixel with alpha blending }
procedure DrawPixelInlineWithAlphaCheck(dest: PBGRAPixel; const c: TBGRAPixel); {$ifdef inline}inline;{$endif} overload;
procedure DrawPixelInlineWithAlphaCheck(dest: PBGRAPixel; c: TBGRAPixel; appliedOpacity: byte); {$ifdef inline}inline;{$endif} overload;
procedure DrawExpandedPixelInlineWithAlphaCheck(dest: PBGRAPixel; const ec: TExpandedPixel); {$ifdef inline}inline;{$endif} overload;
procedure DrawPixelInlineExpandedOrNotWithAlphaCheck(dest: PBGRAPixel; const ec: TExpandedPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif} overload; //alpha in 'c' parameter
procedure DrawPixelInlineNoAlphaCheck(dest: PBGRAPixel; const c: TBGRAPixel); {$ifdef inline}inline;{$endif} overload;
procedure DrawExpandedPixelInlineNoAlphaCheck(dest: PBGRAPixel; const ec: TExpandedPixel; calpha: byte); {$ifdef inline}inline;{$endif} overload;
procedure ClearTypeDrawPixel(pdest: PBGRAPixel; Cr, Cg, Cb: byte; Color: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure InterpolateBilinear(pUpLeft,pUpRight,pDownLeft,pDownRight: PBGRAPixel;
iFactX,iFactY: Integer; ADest: PBGRAPixel);
procedure CopyPixelsWithOpacity(dest,src: PBGRAPixel; opacity: byte; Count: integer); {$ifdef inline}inline;{$endif}
function ApplyOpacity(opacity1,opacity2: byte): byte; {$ifdef inline}inline;{$endif}
function FastRoundDiv255(value: BGRACardinal): BGRACardinal; {$ifdef inline}inline;{$endif}
{ Draw a series of pixels with alpha blending }
procedure PutPixels(pdest: PBGRAPixel; psource: PBGRAPixel; copycount: integer; mode: TDrawMode; AOpacity:byte);
procedure DrawPixelsInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer); {$ifdef inline}inline;{$endif} overload;
procedure DrawExpandedPixelsInline(dest: PBGRAPixel; ec: TExpandedPixel; Count: integer); {$ifdef inline}inline;{$endif} overload;
procedure DrawPixelsInlineExpandedOrNot(dest: PBGRAPixel; ec: TExpandedPixel; c: TBGRAPixel; Count: integer); {$ifdef inline}inline;{$endif} overload; //alpha in 'c' parameter
{ Draw one pixel with linear alpha blending }
procedure FastBlendPixelInline(dest: PBGRAPixel; const c: TBGRAPixel); {$ifdef inline}inline;{$endif} overload;
procedure FastBlendPixelInline(dest: PBGRAPixel; c: TBGRAPixel; appliedOpacity: byte); {$ifdef inline}inline;{$endif} overload;
{ Draw a series of pixels with linear alpha blending }
procedure FastBlendPixelsInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer); {$ifdef inline}inline;{$endif}
{ Replace a series of pixels }
procedure FillInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer); {$ifdef inline}inline;{$endif}
{ Xor a series of pixels }
procedure XorInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer); {$ifdef inline}inline;{$endif}
procedure XorPixels(pdest, psrc: PBGRAPixel; count: integer);
{ Set alpha value for a series of pixels }
procedure AlphaFillInline(dest: PBGRAPixel; alpha: byte; Count: integer); {$ifdef inline}inline;{$endif}
{ Erase a series of pixels, i.e. decrease alpha value }
procedure ErasePixelInline(dest: PBGRAPixel; alpha: byte); {$ifdef inline}inline;{$endif}
{ Draw a pixel to the extent the current pixel is close enough to compare value.
It should not be called on pixels that have not been checked to be close enough }
procedure DrawPixelInlineDiff(dest: PBGRAPixel; c, compare: TBGRAPixel;
maxDiff: byte); {$ifdef inline}inline;{$endif}
{ Draw a series of pixel to the extent the current pixel is close enough to compare value }
procedure DrawPixelsInlineDiff(dest: PBGRAPixel; c: TBGRAPixel;
Count: integer; compare: TBGRAPixel; maxDiff: byte); {$ifdef inline}inline;{$endif}
{ Blend pixels with scanner content }
procedure ScannerPutPixels(scan: IBGRAScanner; pdest: PBGRAPixel; count: integer; mode: TDrawMode);
{ Perform advanced blending operation }
procedure BlendPixels(pdest: PBGRAPixel; psrc: PBGRAPixel;
blendOp: TBlendOperation; Count: integer);
{ Perform blending operation and merge over destination }
procedure BlendPixelsOver(pdest: PBGRAPixel; psrc: PBGRAPixel;
blendOp: TBlendOperation; Count: integer; opacity: byte; linearBlend: boolean = false);
//layer blend modes
//- http://www.pegtop.net/delphi/articles/blendmodes/
//- http://www.w3.org/TR/2009/WD-SVGCompositing-20090430/#comp-op
//- http://docs.gimp.org/en/gimp-concepts-layer-modes.html
procedure LinearMultiplyPixelInline(dest: PBGRAPixel; c: TBGRAPixel); //@{$ifdef inline}inline;{$endif}
procedure AddPixelInline(dest: PBGRAPixel; c: TBGRAPixel); //@{$ifdef inline}inline;{$endif}
procedure LinearAddPixelInline(dest: PBGRAPixel; c: TBGRAPixel); //@{$ifdef inline}inline;{$endif}
procedure ColorBurnPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure ColorDodgePixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure DividePixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure ReflectPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure NonLinearReflectPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure GlowPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure NiceGlowPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure OverlayPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LinearOverlayPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure DifferencePixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LinearDifferencePixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure ExclusionPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LinearExclusionPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LinearSubtractPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LinearSubtractInversePixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure SubtractPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure SubtractInversePixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure NegationPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LinearNegationPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure LightenPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure DarkenPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure ScreenPixelInline(dest: PBGRAPixel; c: TBGRAPixel); //@{$ifdef inline}inline;{$endif}
procedure SoftLightPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure SvgSoftLightPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure HardLightPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure BlendXorPixelInline(dest: PBGRAPixel; c: TBGRAPixel); {$ifdef inline}inline;{$endif}
procedure BGRAFillClearTypeMask(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner; RGBOrder: boolean);
procedure BGRAFillClearTypeRGBMask(dest: TBGRACustomBitmap; x, y: integer;
mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner;
KeepRGBOrder: boolean);
procedure BGRAFillClearTypeMaskPtr(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; maskData: PByte; maskPixelSize: BGRANativeInt; maskRowSize: BGRANativeInt; maskWidth,maskHeight: integer; color: TBGRAPixel; texture: IBGRAScanner; RGBOrder: boolean);
type
TscanNextFunc = function(): TBGRAPixel of object;
implementation
procedure BGRAFillClearTypeMaskPtr(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; maskData: PByte; maskPixelSize: BGRANativeInt; maskRowSize: BGRANativeInt; maskWidth,maskHeight: integer; color: TBGRAPixel; texture: IBGRAScanner; RGBOrder: boolean);
var
pdest: PBGRAPixel;
ClearTypePixel: array[0..2] of byte;
curThird: integer;
procedure OutputPixel; //@{$ifdef inline}inline;{$endif}
begin
if texture <> nil then
color := texture.ScanNextPixel;
if RGBOrder then
ClearTypeDrawPixel(pdest, ClearTypePixel[0],ClearTypePixel[1],ClearTypePixel[2], color)
else
ClearTypeDrawPixel(pdest, ClearTypePixel[2],ClearTypePixel[1],ClearTypePixel[0], color);
end;
procedure NextAlpha(alphaValue: byte); //@{$ifdef inline}inline;{$endif}
begin
ClearTypePixel[curThird] := alphaValue;
inc(curThird);
if curThird = 3 then
begin
OutputPixel;
curThird := 0;
Fillchar(ClearTypePixel, sizeof(ClearTypePixel),0);
inc(pdest);
end;
end;
procedure EndRow; //@{$ifdef inline}inline;{$endif}
begin
if curThird > 0 then OutputPixel;
end;
var
yMask,n: integer;
a: byte;
pmask: PByte;
dx:integer;
miny,maxy,minx,minxThird,maxx,alphaMinX,alphaMaxX,alphaLineLen: integer;
leftOnSide, rightOnSide: boolean;
countBetween: integer;
v1,v2,v3: byte;
procedure StartRow; //@{$ifdef inline}inline;{$endif}
begin
pdest := dest.Scanline[yMask+y]+minx;
if texture <> nil then
texture.ScanMoveTo(minx,yMask+y);
curThird := minxThird;
ClearTypePixel[0] := 0;
ClearTypePixel[1] := 0;
ClearTypePixel[2] := 0;
end;
begin
alphaLineLen := maskWidth+2;
xThird := xThird -1; //for first subpixel
if xThird >= 0 then dx := xThird div 3
else dx := -((-xThird+2) div 3);
x := x +dx;
xThird := xThird -(dx*3);
if y >= dest.ClipRect.Top then miny := 0
else miny := dest.ClipRect.Top-y;
if y+maskHeight-1 < dest.ClipRect.Bottom then
maxy := maskHeight-1 else
maxy := dest.ClipRect.Bottom-1-y;
if x >= dest.ClipRect.Left then
begin
minx := x;
minxThird := xThird;
alphaMinX := 0;
leftOnSide := false;
end else
begin
minx := dest.ClipRect.Left;
minxThird := 0;
alphaMinX := (dest.ClipRect.Left-x)*3 - xThird;
leftOnSide := true;
end;
if x*3+xThird+maskWidth-1 < dest.ClipRect.Right*3 then
begin
maxx := (x*3+xThird+maskWidth-1) div 3;
alphaMaxX := alphaLineLen-1;
rightOnSide := false;
end else
begin
maxx := dest.ClipRect.Right-1;
alphaMaxX := maxx*3+2 - (x*3+xThird);
rightOnSide := true;
end;
countBetween := alphaMaxX-alphaMinX-1;
if (alphaMinX <= alphaMaxX) then
begin
for yMask := miny to maxy do
begin
StartRow;
if leftOnSide then
begin
pmask := maskData + (yMask*maskRowSize)+ (alphaMinX-1)*maskPixelSize;
a := pmask^ div 3;
v1 := a+a;
v2 := a;
v3 := 0;
inc(pmask, maskPixelSize);
end else
begin
pmask := maskData + (yMask*maskRowSize);
v1 := 0;
v2 := 0;
v3 := 0;
end;
for n := countBetween-1 downto 0 do
begin
a := pmask^ div 3;
v1 := v1 +a;
v2 := v2 +a;
v3 := v3 +a;
inc(pmask, maskPixelSize);
NextAlpha(v1);
v1 := v2;
v2 := v3;
v3 := 0;
end;
if rightOnSide then
begin
a := pmask^ div 3;
v1 := v1 +a;
v2 := v2 +a+a;
end;
NextAlpha(v1);
NextAlpha(v2);
EndRow;
end;
end;
end;
procedure BGRAFillClearTypeMask(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner; RGBOrder: boolean);
var delta: BGRANativeInt;
begin
delta := mask.Width*sizeof(TBGRAPixel);
if mask.LineOrder = riloBottomToTop then
delta := -delta;
BGRAFillClearTypeMaskPtr(dest,x,y,xThird,pbyte(mask.Scanline[0])+1,sizeof(TBGRAPixel),delta,mask.Width,mask.Height,color,texture,RGBOrder);
end;
procedure BGRAFillClearTypeRGBMask(dest: TBGRACustomBitmap; x, y: integer;
mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner;
KeepRGBOrder: boolean);
var
minx,miny,maxx,maxy,countx,n,yb: integer;
pdest,psrc: PBGRAPixel;
begin
if y >= dest.ClipRect.Top then miny := 0
else miny := dest.ClipRect.Top-y;
if y+mask.Height-1 < dest.ClipRect.Bottom then
maxy := mask.Height-1 else
maxy := dest.ClipRect.Bottom-1-y;
if x >= dest.ClipRect.Left then minx := 0
else minx := dest.ClipRect.Left-x;
if x+mask.Width-1 < dest.ClipRect.Right then
maxx := mask.Width-1 else
maxx := dest.ClipRect.Right-1-x;
countx := maxx-minx+1;
if countx <= 0 then exit;
for yb := miny to maxy do
begin
pdest := dest.Scanline[y+yb]+(x+minx);
psrc := mask.Scanline[yb]+minx;
if texture <> nil then
texture.ScanMoveTo(x+minx, y+yb);
if KeepRGBOrder then
begin
for n := countx-1 downto 0 do
begin
if texture <> nil then color := texture.ScanNextPixel;
ClearTypeDrawPixel(pdest, psrc^.red, psrc^.green, psrc^.blue, color);
inc(pdest);
inc(psrc);
end;
end else
begin
for n := countx-1 downto 0 do
begin
if texture <> nil then color := texture.ScanNextPixel;
ClearTypeDrawPixel(pdest, psrc^.blue, psrc^.green, psrc^.red, color);
inc(pdest);
inc(psrc);
end;
end;
end;
end;
procedure ClearTypeDrawPixel(pdest: PBGRAPixel; Cr, Cg, Cb: byte; Color: TBGRAPixel);
var merge,mergeClearType: TBGRAPixel;
acc: BGRAWord;
keep,dont_keep: byte;
begin
Cr := ApplyOpacity(Cr,color.alpha);
Cg := ApplyOpacity(Cg,color.alpha);
Cb := ApplyOpacity(Cb,color.alpha);
acc := Cr+Cg+Cb;
if acc = 0 then exit;
merge := pdest^;
mergeClearType.red := GammaCompressionTab[(GammaExpansionTab[merge.red] * (not byte(Cr)) +
GammaExpansionTab[color.red] * Cr + 128) div 255];
mergeClearType.green := GammaCompressionTab[(GammaExpansionTab[merge.green] * (not byte(Cg)) +
GammaExpansionTab[color.green] * Cg + 128) div 255];
mergeClearType.blue := GammaCompressionTab[(GammaExpansionTab[merge.blue] * (not byte(Cb)) +
GammaExpansionTab[color.blue] * Cb + 128) div 255];
mergeClearType.alpha := merge.alpha;
if (mergeClearType.alpha = 255) then
pdest^:= mergeClearType
else
begin
if Cg <> 0 then
DrawPixelInlineWithAlphaCheck(@merge, color, Cg);
dont_keep := mergeClearType.alpha;
if dont_keep > 0 then
begin
keep := not dont_keep;
merge.red := GammaCompressionTab[(GammaExpansionTab[merge.red] * keep + GammaExpansionTab[mergeClearType.red] * dont_keep) div 255];
merge.green := GammaCompressionTab[(GammaExpansionTab[merge.green] * keep + GammaExpansionTab[mergeClearType.green] * dont_keep) div 255];
merge.blue := GammaCompressionTab[(GammaExpansionTab[merge.blue] * keep + GammaExpansionTab[mergeClearType.blue] * dont_keep) div 255];
merge.alpha := mergeClearType.alpha + ApplyOpacity(merge.alpha, not mergeClearType.alpha);
end;
pdest^ := merge;
end;
end;
procedure InterpolateBilinear(pUpLeft, pUpRight, pDownLeft,
pDownRight: PBGRAPixel; iFactX,iFactY: Integer; ADest: PBGRAPixel);
var
w1,w2,w3,w4,alphaW: BGRACardinal;
rSum, gSum, bSum: BGRACardinal; //rgbDiv = aSum
aSum, aDiv: BGRACardinal;
begin
rSum := 0;
gSum := 0;
bSum := 0;
aSum := 0;
aDiv := 0;
w4 := (iFactX*iFactY+127) shr 8;
w3 := iFactY-w4;
{$IFDEF FPC}{$PUSH}{$ENDIF}{$HINTS OFF}
w1 := (256-iFactX)-w3;
{$IFDEF FPC}{$POP}{$ENDIF}
w2 := iFactX-w4;
{ For each pixel around the coordinate, compute
the weight for it and multiply values by it before
adding to the sum }
if pUpLeft <> nil then
with pUpLeft^ do
begin
alphaW := alpha * w1;
aDiv := aDiv +w1;
aSum := aSum +alphaW;
rSum := rSum +(red * alphaW);
gSum := gSum +(green * alphaW);
bSum := bSum +(blue * alphaW);
end;
if pUpRight <> nil then
with pUpRight^ do
begin
alphaW := alpha * w2;
aDiv := aDiv +w2;
aSum := aSum +alphaW;
rSum := rSum +(red * alphaW);
gSum := gSum +(green * alphaW);
bSum := bSum +(blue * alphaW);
end;
if pDownLeft <> nil then
with pDownLeft^ do
begin
alphaW := alpha * w3;
aDiv := aDiv +w3;
aSum := aSum +alphaW;
rSum := rSum +(red * alphaW);
gSum := gSum +(green * alphaW);
bSum := bSum +(blue * alphaW);
end;
if pDownRight <> nil then
with pDownRight^ do
begin
alphaW := alpha * w4;
aDiv := aDiv +w4;
aSum := aSum +alphaW;
rSum := rSum +(red * alphaW);
gSum := gSum +(green * alphaW);
bSum := bSum +(blue * alphaW);
end;
if aSum < 128 then //if there is no alpha
ADest^ := BGRAPixelTransparent
else
with ADest^ do
begin
red := (rSum + aSum shr 1) div aSum;
green := (gSum + aSum shr 1) div aSum;
blue := (bSum + aSum shr 1) div aSum;
if aDiv = 256 then
alpha := (aSum + 128) shr 8
else
alpha := (aSum + aDiv shr 1) div aDiv;
end;
end;
procedure ScannerPutPixels(scan: IBGRAScanner; pdest: PBGRAPixel; count: integer; mode: TDrawMode);
var c : TBGRAPixel;
i: Integer;
scanNextFunc : TscanNextFunc;
begin
if scan.IsScanPutPixelsDefined then
scan.ScanPutPixels(pdest,count,mode) else
begin
{$IFDEF OBJ}
@scan.ScanNextPixel;
{$ELSE}
scanNextFunc := TBGRACustomBitmap(scan.GetInstance).ScanNextPixel;
{$ENDIF}
case mode of
dmLinearBlend:
for i := 0 to count-1 do
begin
FastBlendPixelInline(pdest, scanNextFunc());
inc(pdest);
end;
dmDrawWithTransparency:
for i := 0 to count-1 do
begin
DrawPixelInlineWithAlphaCheck(pdest, scanNextFunc());
inc(pdest);
end;
dmSet:
for i := 0 to count-1 do
begin
pdest^ := scanNextFunc();
inc(pdest);
end;
dmXor:
for i := 0 to count-1 do
begin
PByte(pdest)^ := PBGRADWord(pdest)^ xor BGRADWord(scanNextFunc());
inc(pdest);
end;
dmSetExceptTransparent:
for i := 0 to count-1 do
begin
c := scanNextFunc();
if c.alpha = 255 then pdest^ := c;
inc(pdest);
end;
end;
end;
end;
procedure XorInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer);
{$IFDEF BDS}var _BGRADWord : BGRADWord;{$ENDIF}//#
begin
while Count > 0 do
begin
{$IFDEF BDS}move(c , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
PBGRADWord(dest)^ := PBGRADWord(dest)^ xor {$IFDEF BDS}_BGRADWord{$ELSE}BGRADWord(c){$ENDIF};
Inc(dest);
Dec(Count);
end;
end;
procedure XorPixels(pdest, psrc: PBGRAPixel; count: integer);
{$IFDEF BDS}var _BGRADWord : BGRADWord;{$ENDIF}//#
begin
while Count > 0 do
begin
{$IFDEF BDS}move(pdest , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
PBGRADWord(pdest)^ := PBGRADWord(psrc)^ xor {$IFDEF BDS}_BGRADWord{$ELSE}PBGRADWord(pdest)^{$ENDIF};
Inc(pdest);
Inc(psrc);
Dec(Count);
end;
end;
{$i blendpixels.inc}
procedure AlphaFillInline(dest: PBGRAPixel; alpha: byte; Count: integer); {$ifdef inline}inline;{$endif}
begin
while Count > 0 do
begin
dest^.alpha := alpha;
Inc(dest);
Dec(Count);
end;
end;
procedure FillInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer); {$ifdef inline}inline;{$endif}
// i : integer;
{$IFDEF BDS}var _BGRADWord : BGRADWord;{$ENDIF}
begin
{ for I := 0 to Count -1 do // run without asm
begin
dest^.blue := c.blue;
dest^.green := c.green;
dest^.red := c.red;
dest^.alpha := c.alpha;
inc(dest);
end;}
{$IFDEF BDS}
{$IFDEF FPC}
FillDWord_(dest^, Count, BGRADWord(c));
{$ELSE}
Move(c , _BGRADWord, sizeof(BGRADWord));
FillDWord_(dest^, Count, _BGRADWord);
{$ENDIF}
{$ELSE}
FillDWord(dest^, Count, BGRADWord(c));
{$ENDIF}
end;
procedure FastBlendPixelsInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer);
var
n: integer;
begin
if c.alpha = 0 then exit;
for n := Count - 1 downto 0 do
begin
FastBlendPixelInline(dest, c);
Inc(dest);
end;
end;
procedure PutPixels(pdest: PBGRAPixel; psource: PBGRAPixel; copycount: integer;
mode: TDrawMode; AOpacity: byte);
var i: integer; tempPixel: TBGRAPixel;
{$IFDEF BDS}_BGRADWord, _BGRADWord2, _BGRADWord3 : BGRADWord;{$ENDIF}//#
begin
case mode of
dmSet:
begin
if AOpacity <> 255 then
CopyPixelsWithOpacity(pdest, psource, AOpacity, copycount)
else
begin
copycount := copycount * sizeof(TBGRAPixel);
move(psource^, pdest^, copycount);
end;
end;
dmSetExceptTransparent:
begin
if AOpacity <> 255 then
begin
for i := copycount - 1 downto 0 do
begin
if psource^.alpha = 255 then
begin
tempPixel := psource^;
tempPixel.alpha := ApplyOpacity(tempPixel.alpha,AOpacity);
FastBlendPixelInline(pdest,tempPixel);
end;
Inc(pdest);
Inc(psource);
end;
end else
for i := copycount - 1 downto 0 do
begin
if psource^.alpha = 255 then
pdest^ := psource^;
Inc(pdest);
Inc(psource);
end;
end;
dmDrawWithTransparency:
begin
if AOpacity <> 255 then
begin
for i := copycount - 1 downto 0 do
begin
DrawPixelInlineWithAlphaCheck(pdest, psource^, AOpacity);
Inc(pdest);
Inc(psource);
end;
end
else
for i := copycount - 1 downto 0 do
begin
DrawPixelInlineWithAlphaCheck(pdest, psource^);
Inc(pdest);
Inc(psource);
end;
end;
dmFastBlend:
begin
if AOpacity <> 255 then
begin
for i := copycount - 1 downto 0 do
begin
FastBlendPixelInline(pdest, psource^, AOpacity);
Inc(pdest);
Inc(psource);
end;
end else
for i := copycount - 1 downto 0 do
begin
FastBlendPixelInline(pdest, psource^);
Inc(pdest);
Inc(psource);
end;
end;
dmXor:
begin
if AOpacity <> 255 then
begin
for i := copycount - 1 downto 0 do
begin
{$IFDEF BDS}
move(pdest , _BGRADWord , sizeof(BGRADWord));
move(psource , _BGRADWord2, sizeof(BGRADWord));
_BGRADWord3 := _BGRADWord xor _BGRADWord2;
move(_BGRADWord3 , tempPixel, sizeof(BGRADWord));
{$ENDIF}
FastBlendPixelInline(pdest,{$IFDEF BDS}tempPixel{$ELSE}TBGRAPixel(PBGRADWord(pdest)^ xor PBGRADWord(psource)^){$ENDIF}, AOpacity);//#
Inc(pdest);
Inc(psource);
end;
end else
XorPixels(pdest, psource, copycount);
end;
end;
end;
procedure DrawPixelsInline(dest: PBGRAPixel; c: TBGRAPixel; Count: integer);
var
n: integer;
ec: TExpandedPixel;
{$IFDEF BDS} _BGRADWord : BGRADWord;{$ENDIF}//#
begin
if c.alpha = 0 then exit;
if c.alpha = 255 then
begin
{$IFDEF BDS}move(dest , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
FillDWord_(dest^,count, {$IFDEF BDS}_BGRADWord{$ELSE}BGRALongWord(c){$ENDIF});
exit;
end;
ec := GammaExpansion(c);
for n := Count - 1 downto 0 do
begin
DrawExpandedPixelInlineNoAlphaCheck(dest, ec,c.alpha);
Inc(dest);
end;
end;
procedure DrawExpandedPixelsInline(dest: PBGRAPixel; ec: TExpandedPixel;
Count: integer);
var
n: integer;
c: TBGRAPixel;
{$IFDEF BDS}_BGRADWord : BGRADWord;{$ENDIF}//#
begin
if ec.alpha < $0100 then exit;
if ec.alpha >= $FF00 then
begin
c := GammaCompression(ec);
{$IFDEF BDS}move(c , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
FillDWord_(dest^,count, {$IFDEF BDS}_BGRADWord{$ELSE}BGRALongWord(c){$ENDIF});
exit;
end;
for n := Count - 1 downto 0 do
begin
DrawExpandedPixelInlineNoAlphaCheck(dest, ec, ec.alpha shr 8);
Inc(dest);
end;
end;
procedure DrawPixelsInlineExpandedOrNot(dest: PBGRAPixel; ec: TExpandedPixel; c: TBGRAPixel; Count: integer);
var
n: integer;
{$IFDEF BDS}_BGRADWord : BGRADWord;{$ENDIF}//#
begin
if c.alpha = 0 then exit;
if c.alpha = 255 then
begin
{$IFDEF BDS}move(c , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
FillDWord_(dest^,count, {$IFDEF BDS}_BGRADWord{$ELSE}BGRALongWord(c){$ENDIF});
exit;
end;
for n := Count - 1 downto 0 do
begin
DrawExpandedPixelInlineNoAlphaCheck(dest, ec, c.alpha);
Inc(dest);
end;
end;
procedure DrawPixelsInlineDiff(dest: PBGRAPixel; c: TBGRAPixel;
Count: integer; compare: TBGRAPixel; maxDiff: byte); {$ifdef inline}inline;{$endif}
var
n: integer;
begin
for n := Count - 1 downto 0 do
begin
DrawPixelInlineDiff(dest, c, compare, maxDiff);
Inc(dest);
end;
end;
procedure DrawPixelInlineWithAlphaCheck(dest: PBGRAPixel; const c: TBGRAPixel);
begin
case c.alpha of
0: ;
255: {$IFDEF BDS}move(C, dest^, sizeof(BGRADWord));{$ELSE} dest^ := c;{$ENDIF}
else
DrawPixelInlineNoAlphaCheck(dest,c);
end;
end;
procedure DrawPixelInlineWithAlphaCheck(dest: PBGRAPixel; c: TBGRAPixel; appliedOpacity: byte);
begin
c.alpha := ApplyOpacity(c.alpha,appliedOpacity);
DrawPixelInlineWithAlphaCheck(dest, c);
end;
procedure CopyPixelsWithOpacity(dest, src: PBGRAPixel; opacity: byte;
Count: integer);
begin
while count > 0 do
begin
dest^ := MergeBGRAWithGammaCorrection(src^,opacity,dest^,not opacity);
inc(src);
inc(dest);
dec(count);
end;
end;
function ApplyOpacity(opacity1, opacity2: byte): byte;
begin
result := opacity1*(opacity2+1) shr 8;
end;
function FastRoundDiv255(value: BGRACardinal): BGRACardinal; {$ifdef inline}inline;{$endif}
begin
result := (value + (value shr 7)) shr 8;
end;
procedure DrawExpandedPixelInlineWithAlphaCheck(dest: PBGRAPixel; const ec: TExpandedPixel);
var
calpha: byte;
begin
calpha := ec.alpha shr 8;
case calpha of
0: ;
255: dest^ := GammaCompression(ec);
else
DrawExpandedPixelInlineNoAlphaCheck(dest,ec,calpha);
end;
end;
procedure DrawPixelInlineExpandedOrNotWithAlphaCheck(dest: PBGRAPixel; const ec: TExpandedPixel; c: TBGRAPixel);
begin
case c.alpha of
0: ;
255: {$IFDEF BDS}move(C, dest^, sizeof(BGRADWord));{$ELSE} dest^ := c;{$ENDIF}
else
DrawExpandedPixelInlineNoAlphaCheck(dest,ec,c.alpha);
end;
end;
procedure DrawPixelInlineNoAlphaCheck(dest: PBGRAPixel; const c: TBGRAPixel);
var
a1f, a2f, a12, a12m, alphaCorr: BGRANativeUInt;
{$IFDEF BDS} _BGRADWord : BGRADWord;{$ENDIF}//#
begin
case dest^.alpha of
0: {$IFDEF BDS}move(C, dest^, sizeof(BGRADWord));{$ELSE} dest^ := c;{$ENDIF}
255:
begin
alphaCorr := c.alpha;
if alphaCorr >= 128 then alphaCorr := alphaCorr +1;
dest^.red := GammaCompressionTab[(GammaExpansionTab[dest^.red] * BGRANativeUInt(256-alphaCorr) + GammaExpansionTab[c.red]*alphaCorr) shr 8];
dest^.green := GammaCompressionTab[(GammaExpansionTab[dest^.green] * BGRANativeUInt(256-alphaCorr) + GammaExpansionTab[c.green]*alphaCorr) shr 8];
dest^.blue := GammaCompressionTab[(GammaExpansionTab[dest^.blue] * BGRANativeUInt(256-alphaCorr) + GammaExpansionTab[c.blue]*alphaCorr) shr 8];
end;
else
begin
{$HINTS OFF}
a12 := 65025 - (not dest^.alpha) * (not c.alpha);
{$HINTS ON}
a12m := a12 shr 1;
a1f := dest^.alpha * (not c.alpha);
a2f := (c.alpha shl 8) - c.alpha;
{$IFDEF BDS}_BGRADWord{$ELSE}PBGRADWord(dest)^{$ENDIF} := ((GammaCompressionTab[(GammaExpansionTab[dest^.red] * a1f +
GammaExpansionTab[c.red] * a2f + a12m) div a12]) shl TBGRAPixel_RedShift) or
((GammaCompressionTab[(GammaExpansionTab[dest^.green] * a1f +
GammaExpansionTab[c.green] * a2f + a12m) div a12]) shl TBGRAPixel_GreenShift) or
((GammaCompressionTab[(GammaExpansionTab[dest^.blue] * a1f +
GammaExpansionTab[c.blue] * a2f + a12m) div a12]) shl TBGRAPixel_BlueShift) or
(((a12 + a12 shr 7) shr 8) shl TBGRAPixel_AlphaShift);
{$IFDEF BDS}move(_BGRADWord, dest^, sizeof(BGRADWord));{$ENDIF}
end;
end;
end;
procedure DrawExpandedPixelInlineNoAlphaCheck(dest: PBGRAPixel;
const ec: TExpandedPixel; calpha: byte);
var
a1f, a2f, a12, a12m, alphaCorr: BGRANativeUInt;
{$IFDEF BDS} _BGRADWord : BGRADWord;{$ENDIF}//#
begin
case dest^.alpha of
0: begin
dest^.red := GammaCompressionTab[ec.red];
dest^.green := GammaCompressionTab[ec.green];
dest^.blue := GammaCompressionTab[ec.blue];
dest^.alpha := calpha;
end;
255:
begin
alphaCorr := calpha;
if alphaCorr >= 128 then alphaCorr := alphaCorr +1;
dest^.red := GammaCompressionTab[(GammaExpansionTab[dest^.red] * BGRANativeUInt(256-alphaCorr) + ec.red*alphaCorr) shr 8];
dest^.green := GammaCompressionTab[(GammaExpansionTab[dest^.green] * BGRANativeUInt(256-alphaCorr) + ec.green*alphaCorr) shr 8];
dest^.blue := GammaCompressionTab[(GammaExpansionTab[dest^.blue] * BGRANativeUInt(256-alphaCorr) + ec.blue*alphaCorr) shr 8];
end;
else
begin
{$HINTS OFF}
a12 := 65025 - (not dest^.alpha) * (not calpha);
{$HINTS ON}
a12m := a12 shr 1;
a1f := dest^.alpha * (not calpha);
a2f := (calpha shl 8) - calpha;
{$IFDEF BDS}_BGRADWord{$ELSE}PBGRADWord(dest)^{$ENDIF} := ((GammaCompressionTab[(GammaExpansionTab[dest^.red] * a1f +
ec.red * a2f + a12m) div a12]) shl TBGRAPixel_RedShift) or
((GammaCompressionTab[(GammaExpansionTab[dest^.green] * a1f +
ec.green * a2f + a12m) div a12]) shl TBGRAPixel_GreenShift) or
((GammaCompressionTab[(GammaExpansionTab[dest^.blue] * a1f +
ec.blue * a2f + a12m) div a12]) shl TBGRAPixel_BlueShift) or
(((a12 + a12 shr 7) shr 8) shl TBGRAPixel_AlphaShift);
{$IFDEF BDS}move(_BGRADWord, dest^, sizeof(BGRADWord));{$ENDIF}
end;
end;
end;
procedure FastBlendPixelInline(dest: PBGRAPixel; const c: TBGRAPixel);
var
a1f, a2f, a12, a12m, alphaCorr: BGRANativeUInt;
{$IFDEF BDS} _BGRADWord : BGRADWord;{$ENDIF}//#
begin
case c.alpha of
0: ;
255: {$IFDEF BDS}move(c , dest^, sizeof(TBGRAPixel));{$ELSE}dest^ := c;{$ENDIF}
else
begin
case dest^.alpha of
0: {$IFDEF BDS}move(c , dest^, sizeof(TBGRAPixel));{$ELSE}dest^ := c;{$ENDIF}
255:
begin
alphaCorr := c.alpha;
if alphaCorr >= 128 then
alphaCorr := alphaCorr +1;
dest^.red := (dest^.red * BGRANativeUInt(256-alphaCorr) + c.red*(alphaCorr+1)) shr 8;
dest^.green := (dest^.green * BGRANativeUInt(256-alphaCorr) + c.green*(alphaCorr+1)) shr 8;
dest^.blue := (dest^.blue * BGRANativeUInt(256-alphaCorr) + c.blue*(alphaCorr+1)) shr 8;
end;
else
begin
{$HINTS OFF}
a12 := 65025 - (not dest^.alpha) * (not c.alpha);
{$HINTS ON}
a12m := a12 shr 1;
a1f := dest^.alpha * (not c.alpha);
a2f := (c.alpha shl 8) - c.alpha;
{$IFDEF BDS}_BGRADWord{$ELSE}PBGRADWord(dest)^{$ENDIF} := (((dest^.red * a1f + c.red * a2f + a12m) div a12) shl TBGRAPixel_RedShift) or
(((dest^.green * a1f + c.green * a2f + a12m) div a12) shl TBGRAPixel_GreenShift) or
(((dest^.blue * a1f + c.blue * a2f + a12m) div a12) shl TBGRAPixel_BlueShift) or
(((a12 + a12 shr 7) shr 8) shl TBGRAPixel_AlphaShift);
{$IFDEF BDS}move(_BGRADWord, dest^, sizeof(BGRADWord));{$ENDIF}
end;
end;
end;
end;
end;
procedure FastBlendPixelInline(dest: PBGRAPixel; c: TBGRAPixel;
appliedOpacity: byte);
begin
c.alpha := ApplyOpacity(c.alpha,appliedOpacity);
FastBlendPixelInline(dest,c);
end;
procedure DrawPixelInlineDiff(dest: PBGRAPixel; c, compare: TBGRAPixel;
maxDiff: byte); {$ifdef inline}inline;{$endif}
var alpha: BGRANativeInt;
c2: TBGRAPixel;
begin
alpha := (c.alpha * (maxDiff + 1 - BGRADiff(dest^, compare)) + (maxDiff + 1) shr 1) div
(maxDiff + 1);
if alpha > 0 then
begin
c2 := BGRA(c.red, c.green, c.blue, alpha);
DrawPixelInlineWithAlphaCheck(dest, c2);
end;
end;
procedure ErasePixelInline(dest: PBGRAPixel; alpha: byte); {$ifdef inline}inline;{$endif}
var
newAlpha: byte;
begin
newAlpha := ApplyOpacity(dest^.alpha, not alpha);
if newAlpha = 0 then
dest^ := BGRAPixelTransparent
else
dest^.alpha := newAlpha;
end;
{$i blendpixelsover.inc}
{$i blendpixelinline.inc}
end.