-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgrapolygon.pas
1619 lines (1453 loc) · 57 KB
/
bgrapolygon.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
996
997
998
999
1000
{ ***************************************************************************
* *
* 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 BGRAPolygon;
{$i bgrabitmap.inc}{$H+}
{.$POINTERMATH ON}
{ This unit contains polygon drawing functions and spline functions.
Shapes are drawn using a TBGRACustomFillInfo object, which calculates the
intersection of an horizontal line and the polygon.
Various shapes are handled :
- TFillPolyInfo : polygon scanned in any order
- TOnePassFillPolyInfo : polygon scanned from top to bottom
- TFillEllipseInfo : ellipse
- TFillBorderEllipseInfo : ellipse border
- TFillRoundRectangleInfo : round rectangle (or other corners)
- TFillBorderRoundRectInfo : round rectangle border
Various fill modes :
- Alternate : each time there is an intersection, it enters or go out of the polygon
- Winding : filled when the sum of ascending and descending intersection is non zero
- Color : fill with a color defined as a TBGRAPixel argument
- Erase : erase with an alpha in the TBGRAPixel argument
- Texture : draws a texture with the IBGRAScanner argument
Various border handling :
- aliased : one horizontal line intersection is calculated per pixel in the vertical loop
- antialiased : more lines are calculated and a density is computed by adding them together
- multi-polygon antialiasing and superposition (TBGRAMultiShapeFiller) : same as above but
by combining multiple polygons at the same time, and optionally subtracting top polygons
}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType,{$ENDIF} BGRAGraphics, BGRABitmapTypes, BGRAFillInfo, BGRAPath;
procedure FillShapeAntialias(bmp: TBGRACustomBitmap; shapeInfo: TBGRACustomFillInfo;
c: TBGRAPixel; EraseMode: boolean; scan: IBGRAScanner; NonZeroWinding: boolean; LinearBlend: boolean = false);
procedure FillShapeAntialiasWithTexture(bmp: TBGRACustomBitmap; shapeInfo: TBGRACustomFillInfo;
scan: IBGRAScanner; NonZeroWinding: boolean; LinearBlend: boolean = false);
procedure FillShapeAliased(bmp: TBGRACustomBitmap; shapeInfo: TBGRACustomFillInfo;
c: TBGRAPixel; EraseMode: boolean; scan: IBGRAScanner; NonZeroWinding: boolean; drawmode: TDrawMode; AliasingIncludeBottomRight: Boolean= false);
type
{ TBGRAMultishapeFiller }
TBGRAMultishapeFiller = class
protected
nbShapes: integer;
shapes: array of record
info: TBGRACustomFillInfo;
internalInfo: boolean;
texture: IBGRAScanner;
internalTexture: TObject;
color: TExpandedPixel;
bounds: TRect;
fillMode: TFillMode;
fillModeOverride: boolean;
end;
function AddShape(AInfo: TBGRACustomFillInfo; AInternalInfo: boolean; ATexture: IBGRAScanner; AInternalTexture: TObject; AColor: TBGRAPixel): integer; overload;
function CheckRectangleBorderBounds(var x1, y1, x2, y2: single; w: single): boolean;
procedure InternalAddStroke(const APoints: array of TPointF; AClosed: boolean; AData: Pointer);
public
FillMode : TFillMode;
PolygonOrder: TPolygonOrder;
Antialiasing: Boolean;
AliasingIncludeBottomRight: Boolean;
constructor Create;
destructor Destroy; override;
function AddShape(AShape: TBGRACustomFillInfo; AColor: TBGRAPixel): integer; overload;
function AddShape(AShape: TBGRACustomFillInfo; ATexture: IBGRAScanner): integer; overload;
function AddPolygon(const points: array of TPointF; AColor: TBGRAPixel): integer; overload;
function AddPolygon(const points: array of TPointF; ATexture: IBGRAScanner): integer; overload;
procedure AddPathStroke(APath: TBGRAPath; AColor: TBGRAPixel; AWidth: single; AStroker: TBGRACustomPenStroker); overload;
procedure AddPathStroke(APath: TBGRAPath; ATexture: IBGRAScanner; AWidth: single; AStroker: TBGRACustomPenStroker); overload;
procedure AddPathStroke(APath: TBGRAPath; AMatrix: TAffineMatrix; AColor: TBGRAPixel; AWidth: single; AStroker: TBGRACustomPenStroker); overload;
procedure AddPathStroke(APath: TBGRAPath; AMatrix: TAffineMatrix; ATexture: IBGRAScanner; AWidth: single; AStroker: TBGRACustomPenStroker); overload;
function AddPathFill(APath: TBGRAPath; AColor: TBGRAPixel): integer; overload;
function AddPathFill(APath: TBGRAPath; ATexture: IBGRAScanner): integer; overload;
function AddPathFill(APath: TBGRAPath; AMatrix: TAffineMatrix; AColor: TBGRAPixel): integer; overload;
function AddPathFill(APath: TBGRAPath; AMatrix: TAffineMatrix; ATexture: IBGRAScanner): integer; overload;
function AddPolylineStroke(const points: array of TPointF; AColor: TBGRAPixel; AWidth: single; AStroker: TBGRACustomPenStroker): integer; overload;
function AddPolylineStroke(const points: array of TPointF; ATexture: IBGRAScanner; AWidth: single; AStroker: TBGRACustomPenStroker): integer; overload;
function AddPolygonStroke(const points: array of TPointF; AColor: TBGRAPixel; AWidth: single; AStroker: TBGRACustomPenStroker): integer; overload;
function AddPolygonStroke(const points: array of TPointF; ATexture: IBGRAScanner; AWidth: single; AStroker: TBGRACustomPenStroker): integer; overload;
function AddTriangleLinearColor(pt1, pt2, pt3: TPointF; c1, c2, c3: TBGRAPixel): integer;
function AddTriangleLinearMapping(pt1, pt2, pt3: TPointF; texture: IBGRAScanner; tex1, tex2, tex3: TPointF): integer;
procedure AddQuadLinearColor(pt1, pt2, pt3, pt4: TPointF; c1, c2, c3, c4: TBGRAPixel);
procedure AddQuadLinearMapping(pt1, pt2, pt3, pt4: TPointF; texture: IBGRAScanner; tex1, tex2, {%H-}tex3, tex4: TPointF;
ACulling: TFaceCulling = fcNone);
procedure AddQuadPerspectiveMapping(pt1, pt2, pt3, pt4: TPointF; texture: IBGRAScanner; tex1, tex2, tex3, tex4: TPointF);
function AddEllipse(x, y, rx, ry: single; AColor: TBGRAPixel): integer; overload;
function AddEllipse(x, y, rx, ry: single; ATexture: IBGRAScanner): integer; overload;
function AddEllipseBorder(x, y, rx, ry, w: single; AColor: TBGRAPixel): integer; overload;
function AddEllipseBorder(x, y, rx, ry, w: single; ATexture: IBGRAScanner): integer; overload;
function AddRoundRectangle(x1, y1, x2, y2, rx, ry: single; AColor: TBGRAPixel; options: TRoundRectangleOptions= []): integer; overload;
function AddRoundRectangle(x1, y1, x2, y2, rx, ry: single; ATexture: IBGRAScanner; options: TRoundRectangleOptions= []): integer; overload;
function AddRoundRectangleBorder(x1, y1, x2, y2, rx, ry, w: single; AColor: TBGRAPixel; options: TRoundRectangleOptions= []): integer; overload;
function AddRoundRectangleBorder(x1, y1, x2, y2, rx, ry, w: single; ATexture: IBGRAScanner; options: TRoundRectangleOptions= []): integer; overload;
function AddRectangle(x1, y1, x2, y2: single; AColor: TBGRAPixel): integer; overload;
function AddRectangle(x1, y1, x2, y2: single; ATexture: IBGRAScanner): integer; overload;
function AddRectangleBorder(x1, y1, x2, y2, w: single; AColor: TBGRAPixel): integer; overload;
function AddRectangleBorder(x1, y1, x2, y2, w: single; ATexture: IBGRAScanner): integer; overload;
procedure OverrideFillMode(AShapeIndex: integer; AFillMode: TFillMode);
procedure Draw(dest: TBGRACustomBitmap; ADrawMode: TDrawMode = dmDrawWithTransparency);
property ShapeCount: integer read nbShapes;
end;
procedure FillPolyAliased(bmp: TBGRACustomBitmap; points: array of TPointF;
c: TBGRAPixel; EraseMode: boolean; NonZeroWinding: boolean; drawmode: TDrawMode; APixelCenteredCoordinates: boolean = true);
procedure FillPolyAliasedWithTexture(bmp: TBGRACustomBitmap; points: array of TPointF;
scan: IBGRAScanner; NonZeroWinding: boolean; drawmode: TDrawMode; APixelCenteredCoordinates: boolean = true);
procedure FillPolyAntialias(bmp: TBGRACustomBitmap; points: array of TPointF;
c: TBGRAPixel; EraseMode: boolean; NonZeroWinding: boolean; LinearBlend: boolean = false; APixelCenteredCoordinates: boolean = true);
procedure FillPolyAntialiasWithTexture(bmp: TBGRACustomBitmap; points: array of TPointF;
scan: IBGRAScanner; NonZeroWinding: boolean; LinearBlend: boolean = false; APixelCenteredCoordinates: boolean = true);
procedure FillEllipseAntialias(bmp: TBGRACustomBitmap; x, y, rx, ry: single;
c: TBGRAPixel; EraseMode: boolean; LinearBlend: boolean = false);
procedure FillEllipseAntialiasWithTexture(bmp: TBGRACustomBitmap; x, y, rx, ry: single;
scan: IBGRAScanner; LinearBlend: boolean = false);
procedure BorderEllipseAntialias(bmp: TBGRACustomBitmap; x, y, rx, ry, w: single;
c: TBGRAPixel; EraseMode: boolean; LinearBlend: boolean = false);
procedure BorderEllipseAntialiasWithTexture(bmp: TBGRACustomBitmap; x, y, rx, ry, w: single;
scan: IBGRAScanner; LinearBlend: boolean = false);
procedure FillRoundRectangleAntialias(bmp: TBGRACustomBitmap; x1, y1, x2, y2, rx, ry: single;
options: TRoundRectangleOptions; c: TBGRAPixel; EraseMode: boolean; LinearBlend: boolean = false; APixelCenteredCoordinates: boolean = true);
procedure FillRoundRectangleAntialiasWithTexture(bmp: TBGRACustomBitmap; x1, y1, x2, y2, rx, ry: single;
options: TRoundRectangleOptions; scan: IBGRAScanner; LinearBlend: boolean = false; APixelCenteredCoordinates: boolean = true);
procedure BorderRoundRectangleAntialias(bmp: TBGRACustomBitmap; x1, y1, x2, y2, rx, ry, w: single;
options: TRoundRectangleOptions; c: TBGRAPixel; EraseMode: boolean; LinearBlend: boolean = false; APixelCenteredCoordinates: boolean = true);
procedure BorderRoundRectangleAntialiasWithTexture(bmp: TBGRACustomBitmap; x1, y1, x2, y2, rx, ry, w: single;
options: TRoundRectangleOptions; scan: IBGRAScanner; LinearBlend: boolean = false; APixelCenteredCoordinates: boolean = true);
procedure BorderAndFillRoundRectangleAntialias(bmp: TBGRACustomBitmap; x1, y1, x2, y2, rx, ry, w: single;
options: TRoundRectangleOptions; bordercolor,fillcolor: TBGRAPixel; bordertexture,filltexture: IBGRAScanner; EraseMode: boolean; APixelCenteredCoordinates: boolean = true);
implementation
uses Math, BGRABlend, BGRAGradientScanner, BGRATransform, BGRADefaultBitmap;
type
TPathStrokeData = record
Stroker: TBGRACustomPenStroker;
Texture: IBGRAScanner;
Color: TBGRAPixel;
Width: Single;
end;
procedure FillShapeAntialias(bmp: TBGRACustomBitmap; shapeInfo: TBGRACustomFillInfo;
c: TBGRAPixel; EraseMode: boolean; scan: IBGRAScanner; NonZeroWinding: boolean; LinearBlend: boolean);
const oneOver512 = 1/512;
var
inter: {$IFDEF BDS}arrayofTIntersectionInfo{$ELSE}array of TIntersectionInfo{$ENDIF};
nbInter: integer;
firstScan, lastScan: record
inter: {$IFDEF BDS}arrayofTIntersectionInfo{$ELSE}array of TIntersectionInfo{$ENDIF};
nbInter: integer;
sliceIndex: integer;
end;
miny, maxy, minx, maxx,
densMinX, densMaxX: integer;
density: PDensity;
xb, yb, yc, i: integer;
tempDensity: UInt32or64;
x1, x2, x1b,x2b: single;
ix1, ix2: integer;
pdest: PBGRAPixel;
pdens: PDensity;
bAlpha : Byte;
curvedSeg,optimised: boolean;
ec: TExpandedPixel;
c2, cTemp :TBGRAPixel;
MemScanCopy,pscan: pbgrapixel;
ScanNextPixelProc: TScanNextPixelFunction;
temp: Single;
function GetYScan(num: integer): single; //@{$ifdef inline}inline;{$endif}
begin
result := yb + (num * 2 + 1) / (AntialiasPrecision * 2);
end;
procedure SubTriangleDensity(x1,density1, x2, density2: single);
var ix1,ix2,n: integer;
slope: single;
function densityAt(x: single): single; //@{$ifdef inline}inline;{$endif}
begin
result := (x-x1)*slope+density1;
end;
var
curdens: single;
pdens: pdensity;
newvalue: Int32or64;
begin
if (x1 <> x2) and (x1 < maxx + 1) and (x2 >= minx) then
begin
slope := (density2-density1)/(x2-x1);
if x1 < minx then
begin
density1 := densityAt(minx);
x1 := minx;
end;
if x2 >= maxx + 1 then
begin
density2 := densityAt(maxx+1);
x2 := maxx + 1;
end;
ix1 := floor(x1);
ix2 := floor(x2);
if ix1 = ix2 then
begin
newValue := (density + (ix1 - minx))^ - round((x2 - x1)*(density1+density2)/2);
if newValue < 0 then newValue := 0;
if newValue > 256 then newValue := 256;
(density + (ix1 - minx))^ := newValue
end
else
begin
newValue := (density + (ix1 - minx))^ - round((1 - (x1 - ix1))*(density1+densityAt(ix1+1))/2) ;
if newValue < 0 then newValue := 0;
if newValue > 256 then newValue := 256;
(density + (ix1 - minx))^ := newValue;
if (ix2 <= maxx) then
begin
newValue := (density + (ix2 - minx))^ - round((x2 - ix2)*(density2+densityAt(ix2))/2);
if newValue < 0 then newValue := 0;
if newValue > 256 then newValue := 256;
(density + (ix2 - minx))^ := newValue;
end;
end;
if ix2 > ix1 + 1 then
begin
curdens := densityAt(ix1+1.5);
pdens := density + (ix1+1 - minx);
for n := ix2-1-(ix1+1) downto 0 do
begin
newValue := pdens^ - round(curdens);
if newValue < 0 then newValue := 0;
if newValue > 256 then newValue := 256;
pdens^ := newValue;
curdens := curdens +slope;
inc(pdens);
end;
end;
end;
end;
begin
if (scan=nil) and (c.alpha=0) then exit;
If not BGRAShapeComputeMinMax(shapeInfo,minx,miny,maxx,maxy,bmp) then exit;
inter := shapeInfo.CreateIntersectionArray;
getmem(density, (maxx - minx + 2)*sizeof(TDensity)); //more for safety
ec := GammaExpansion(c);
c2 := c;
MemScanCopy := nil;
ScanNextPixelProc := nil;
if scan <> nil then
begin
if scan.IsScanPutPixelsDefined then
GetMem(MemScanCopy,(maxx-minx+1)*sizeof(TBGRAPixel));
{$IFDEF OBJ}
ScanNextPixelProc := @scan.ScanNextPixel;
{$ELSE}
ScanNextPixelProc := TBGRACustomScanner(scan.GetInstance).ScanNextPixel;
{$ENDIF}
end;
curvedSeg := shapeInfo.SegmentsCurved;
if not curvedSeg then
begin
firstScan.inter := shapeInfo.CreateIntersectionArray;
lastScan.inter := shapeInfo.CreateIntersectionArray;
end;
//vertical scan
for yb := miny to maxy do
begin
//mean density
fillchar(density^,(maxx-minx+1)*sizeof(TDensity),0);
densMinX := maxx+1;
densMaxX := minx-1;
if not curvedSeg then
begin
with firstScan do
begin
shapeInfo.ComputeAndSort(yb+1/256,inter,nbInter,NonZeroWinding);
sliceIndex:= shapeInfo.GetSliceIndex;
end;
with lastScan do
begin
shapeInfo.ComputeAndSort(yb+255/256,inter,nbInter,NonZeroWinding);
sliceIndex:= shapeInfo.GetSliceIndex;
end;
if (firstScan.sliceIndex = lastScan.sliceIndex) and (firstScan.nbInter = lastScan.nbInter) then
begin
optimised := true;
for i := 0 to firstScan.nbInter-1 do
if firstScan.inter[i].numSegment <> lastScan.inter[i].numSegment then
begin
optimised := false;
break;
end;
end else
optimised := false;
if optimised then
begin
for i := 0 to firstScan.nbinter div 2 - 1 do
begin
x1 := firstScan.inter[i+i].interX;
x1b := lastScan.inter[i+i].interX;
x2 := firstScan.inter[i+i+1].interX;
x2b := lastScan.inter[i+i+1].interX;
if (abs(x1-x1b)<oneOver512) and (abs(x2-x2b)<oneOver512) and
((i+i+2 >= firstScan.nbInter) or
((firstScan.inter[i+i+2].interX >= x2+1) and
(lastScan.inter[i+i+2].interX >= x2b+1))) then
begin
x1 := (x1+x1b)*0.5;
x2 := (x2+x2b)*0.5;
if x1 < minx then x1 := minx;
ix1 := floor(x1);
if x2 >= maxx+1 then
begin
x2 := maxx+1;
ix2 := maxx;
end else
ix2 := floor(x2);
if ix2 > maxx then ix2 := maxx;
if ix1>ix2 then continue;
if ix1=ix2 then
begin
tempDensity:= round((x2-x1)*256);
if scan <> nil then //with texture scan
begin
scan.ScanMoveTo(ix1,yb);
c := scan.ScanNextPixel;
c.alpha := c.alpha*tempDensity shr 8;
if linearBlend then
bmp.DrawPixel(ix1, yb, c, dmLinearBlend)
else
bmp.DrawPixel(ix1, yb, c, dmDrawWithTransparency);
end else
if EraseMode then //erase with alpha
bmp.ErasePixel(ix1,yb,c.alpha*tempDensity shr 8)
else
begin //solid color
c2.alpha := c.alpha*tempDensity shr 8;
if linearBlend then
bmp.DrawPixel(ix1, yb, c2, dmLinearBlend)
else
bmp.DrawPixel(ix1, yb, c2, dmDrawWithTransparency);
end;
end else
begin
tempDensity:= round((ix1+1-x1)*256);
if scan <> nil then scan.ScanMoveTo(ix1,yb);
if tempDensity < 256 then
begin
if scan <> nil then //with texture scan
begin
c := scan.ScanNextPixel;
c.alpha := c.alpha*tempDensity shr 8;
if linearBlend then
bmp.DrawPixel(ix1, yb, c, dmLinearBlend)
else
bmp.DrawPixel(ix1, yb, c, dmDrawWithTransparency);
end else
if EraseMode then //erase with alpha
bmp.ErasePixel(ix1,yb, c.alpha*tempDensity shr 8)
else
begin //solid color
c2.alpha := c.alpha*tempDensity shr 8;
if linearBlend then
bmp.DrawPixel(ix1, yb, c2, dmLinearBlend)
else
bmp.DrawPixel(ix1, yb, c2, dmDrawWithTransparency);
end;
inc(ix1);
end;
tempDensity:= round((x2-ix2)*256);
if tempDensity < 256 then dec(ix2);
if ix2 >= ix1 then
begin
if scan <> nil then //with texture scan
begin
if linearBlend then
ScannerPutPixels(scan, bmp.ScanLine[yb] + ix1, ix2-ix1+1, dmLinearBlend)
else
ScannerPutPixels(scan, bmp.ScanLine[yb] + ix1, ix2-ix1+1, dmDrawWithTransparency);
end else
if EraseMode then //erase with alpha
bmp.EraseLine(ix1,yb,ix2,yb,c.alpha,True)
else
begin //solid color
if LinearBlend then
bmp.HorizLine(ix1,yb,ix2,c,dmLinearBlend)
else
bmp.HorizLine(ix1,yb,ix2,c,dmDrawWithTransparency);
end;
end;
if tempDensity < 256 then
begin
inc(ix2);
if scan <> nil then //with texture scan
begin
c := scan.ScanNextPixel;
c.alpha := c.alpha*tempDensity shr 8;
if linearBlend then
bmp.DrawPixel(ix2, yb, c, dmLinearBlend)
else
bmp.DrawPixel(ix2, yb, c, dmDrawWithTransparency);
end else
if EraseMode then //erase with alpha
bmp.ErasePixel(ix2,yb,c.alpha*tempDensity shr 8)
else
begin //solid color
c2.alpha := c.alpha*tempDensity shr 8;
if linearBlend then
bmp.DrawPixel(ix2, yb, c2, dmLinearBlend)
else
bmp.DrawPixel(ix2, yb, c2, dmDrawWithTransparency);
end;
end;
end;
continue;
end else
begin
if (x1 > x1b) then
begin
temp := x1;
x1 := x1b;
x1b := temp;
end;
if (x2 < x2b) then
begin
temp := x2;
x2 := x2b;
x2b := temp;
end;
{$DEFINE INCLUDE_FILLDENSITY}
{$DEFINE PARAM_SINGLESEGMENT}
{$i density256.inc}
SubTriangleDensity(x1,256,x1b,0);
SubTriangleDensity(x2b,0,x2,256);
end;
end;
end else
begin
for yc := 0 to AntialiasPrecision - 1 do
begin
//find intersections
shapeInfo.ComputeAndSort(GetYScan(yc),inter,nbInter,NonZeroWinding);
{$DEFINE INCLUDE_FILLDENSITY}
{$i density256.inc}
end;
end;
end else
begin
optimised := false;
//precision scan
for yc := 0 to AntialiasPrecision - 1 do
begin
//find intersections
shapeInfo.ComputeAndSort(GetYScan(yc),inter,nbInter,NonZeroWinding);
{$DEFINE INCLUDE_FILLDENSITY}
{$i density256.inc}
end;
end;
if LinearBlend then
begin
if optimised then
{$DEFINE INCLUDE_RENDERDENSITY}
{$define PARAM_LINEARANTIALIASING}
{$i density256.inc}
else
{$DEFINE INCLUDE_RENDERDENSITY}
{$define PARAM_LINEARANTIALIASING}
{$define PARAM_ANTIALIASINGFACTOR}
{$i density256.inc}
end else
begin
if optimised then
{$DEFINE INCLUDE_RENDERDENSITY}
{$i density256.inc}
else
{$DEFINE INCLUDE_RENDERDENSITY}
{$define PARAM_ANTIALIASINGFACTOR}
{$i density256.inc}
end;
end;
freemem(MemScanCopy);
shapeInfo.FreeIntersectionArray(inter);
if not curvedSeg then
begin
with firstScan do
begin
for i := 0 to high(inter) do
inter[i].free;
end;
with lastScan do
begin
for i := 0 to high(inter) do
inter[i].free;
end;
end;
freemem(density);
bmp.InvalidateBitmap;
end;
procedure FillShapeAliased(bmp: TBGRACustomBitmap; shapeInfo: TBGRACustomFillInfo;
c: TBGRAPixel; EraseMode: boolean; scan: IBGRAScanner; NonZeroWinding: boolean; drawmode: TDrawMode; AliasingIncludeBottomRight: Boolean= false);
var
inter: {$IFDEF BDS}arrayofTIntersectionInfo{$ELSE}array of TIntersectionInfo{$ENDIF};
nbInter: integer;
miny, maxy, minx, maxx: integer;
xb,yb, i: integer;
x1, x2: single;
ix1, ix2: integer;
pdest: PBGRAPixel;
AliasingOfs: TPointF;
ec: TExpandedPixel;
begin
if (scan=nil) and (c.alpha=0) then exit;
If not BGRAShapeComputeMinMax(shapeInfo,minx,miny,maxx,maxy,bmp) then exit;
inter := shapeInfo.CreateIntersectionArray;
if AliasingIncludeBottomRight then
AliasingOfs := PointF(0,0) else
AliasingOfs := PointF(-0.0001,-0.0001);
ec := GammaExpansion(c);
if (scan = nil) and (c.alpha = 255) then drawmode := dmSet;
//vertical scan
for yb := miny to maxy do
begin
//find intersections
shapeInfo.ComputeAndSort( yb+0.5-AliasingOfs.Y, inter, nbInter, NonZeroWinding);
for i := 0 to nbinter div 2 - 1 do
begin
x1 := inter[i + i].interX+AliasingOfs.X;
x2 := inter[i + i+ 1].interX+AliasingOfs.X;
if x1 <> x2 then
begin
ComputeAliasedRowBounds(x1,x2, minx,maxx, ix1,ix2);
if ix1 <= ix2 then
begin
//render scanline
if scan <> nil then //with texture scan
begin
pdest := bmp.ScanLine[yb] + ix1;
scan.ScanMoveTo(ix1,yb);
ScannerPutPixels(scan,pdest,ix2-ix1+1,drawmode);
end else
if EraseMode then //erase with alpha
begin
pdest := bmp.ScanLine[yb] + ix1;
for xb := ix1 to ix2 do
begin
ErasePixelInline(pdest, c.alpha);
Inc(pdest);
end;
end
else
begin
case drawmode of
dmFastBlend: bmp.FastBlendHorizLine(ix1,yb,ix2, c);
dmDrawWithTransparency: bmp.DrawHorizLine(ix1,yb,ix2, ec);
dmSet: bmp.SetHorizLine(ix1,yb,ix2, c);
dmXor: bmp.XorHorizLine(ix1,yb,ix2, c);
end;
end;
end;
end;
end;
end;
shapeInfo.FreeIntersectionArray(inter);
bmp.InvalidateBitmap;
end;
procedure FillShapeAntialiasWithTexture(bmp: TBGRACustomBitmap;
shapeInfo: TBGRACustomFillInfo; scan: IBGRAScanner; NonZeroWinding: boolean; LinearBlend: boolean);
begin
FillShapeAntialias(bmp,shapeInfo,BGRAPixelTransparent,False,scan,NonZeroWinding,LinearBlend);
end;
procedure FillPolyAliased(bmp: TBGRACustomBitmap; points: array of TPointF;
c: TBGRAPixel; EraseMode: boolean; NonZeroWinding: boolean; drawmode: TDrawMode; APixelCenteredCoordinates: boolean);
var
info: TCustomFillPolyInfo;
begin
if length(points) < 3 then
exit;
info := TOnePassFillPolyInfo.Create(points, APixelCenteredCoordinates);
FillShapeAliased(bmp, info, c, EraseMode, nil, NonZeroWinding, drawmode);
info.Free;
end;
procedure FillPolyAliasedWithTexture(bmp: TBGRACustomBitmap;
points: array of TPointF; scan: IBGRAScanner; NonZeroWinding: boolean; drawmode: TDrawMode; APixelCenteredCoordinates: boolean);
var
info: TCustomFillPolyInfo;
begin
if length(points) < 3 then
exit;
info := TOnePassFillPolyInfo.Create(points, APixelCenteredCoordinates);
FillShapeAliased(bmp, info, BGRAPixelTransparent,False,scan, NonZeroWinding, drawmode);
info.Free;
end;
procedure FillPolyAntialias(bmp: TBGRACustomBitmap; points: array of TPointF;
c: TBGRAPixel; EraseMode: boolean; NonZeroWinding: boolean; LinearBlend: boolean; APixelCenteredCoordinates: boolean);
var
info: TCustomFillPolyInfo;
begin
if length(points) < 3 then
exit;
info := TOnePassFillPolyInfo.Create(points, APixelCenteredCoordinates);
FillShapeAntialias(bmp, info, c, EraseMode, nil, NonZeroWinding, LinearBlend);
info.Free;
end;
procedure FillPolyAntialiasWithTexture(bmp: TBGRACustomBitmap;
points: array of TPointF; scan: IBGRAScanner; NonZeroWinding: boolean; LinearBlend: boolean; APixelCenteredCoordinates: boolean);
var
info: TCustomFillPolyInfo;
begin
if length(points) < 3 then
exit;
info := TOnePassFillPolyInfo.Create(points, APixelCenteredCoordinates);
FillShapeAntialiasWithTexture(bmp, info, scan, NonZeroWinding, LinearBlend);
info.Free;
end;
procedure FillEllipseAntialias(bmp: TBGRACustomBitmap; x, y, rx, ry: single;
c: TBGRAPixel; EraseMode: boolean; LinearBlend: boolean);
var
info: TFillEllipseInfo;
begin
if (rx = 0) or (ry = 0) or (x = EmptySingle) or (y = EmptySingle) then
exit;
info := TFillEllipseInfo.Create(x, y, rx, ry);
FillShapeAntialias(bmp, info, c, EraseMode, nil, False, LinearBlend);
info.Free;
end;
procedure FillEllipseAntialiasWithTexture(bmp: TBGRACustomBitmap; x, y, rx,
ry: single; scan: IBGRAScanner; LinearBlend: boolean);
var
info: TFillEllipseInfo;
begin
if (rx = 0) or (ry = 0) or (x = EmptySingle) or (y = EmptySingle) then
exit;
info := TFillEllipseInfo.Create(x, y, rx, ry);
FillShapeAntialiasWithTexture(bmp, info, scan, False, LinearBlend);
info.Free;
end;
procedure BorderEllipseAntialias(bmp: TBGRACustomBitmap; x, y, rx, ry, w: single;
c: TBGRAPixel; EraseMode: boolean; LinearBlend: boolean);
var
info: TFillBorderEllipseInfo;
begin
if ((rx = 0) and (ry = 0)) or (w=0) or (x = EmptySingle) or (y = EmptySingle) then
exit;
info := TFillBorderEllipseInfo.Create(x, y, rx, ry, w);
FillShapeAntialias(bmp, info, c, EraseMode, nil, False, LinearBlend);
info.Free;
end;
procedure BorderEllipseAntialiasWithTexture(bmp: TBGRACustomBitmap; x, y, rx,
ry, w: single; scan: IBGRAScanner; LinearBlend: boolean);
var
info: TFillBorderEllipseInfo;
begin
if ((rx = 0) and (ry = 0)) or (w=0) or (x = EmptySingle) or (y = EmptySingle) then
exit;
info := TFillBorderEllipseInfo.Create(x, y, rx, ry, w);
FillShapeAntialiasWithTexture(bmp, info, scan, False, LinearBlend);
info.Free;
end;
{ TBGRAMultishapeFiller }
function TBGRAMultishapeFiller.AddShape(AInfo: TBGRACustomFillInfo; AInternalInfo: boolean; ATexture: IBGRAScanner; AInternalTexture: TObject; AColor: TBGRAPixel): integer;
begin
if length(shapes) = nbShapes then
setlength(shapes, (length(shapes)+1)*2);
result := nbShapes;
inc(nbShapes);
with shapes[result] do
begin
info := AInfo;
internalInfo:= AInternalInfo;
texture := ATexture;
internalTexture:= AInternalTexture;
color := GammaExpansion(AColor);
fillModeOverride:= false;
end;
end;
function TBGRAMultishapeFiller.CheckRectangleBorderBounds(var x1, y1, x2,
y2: single; w: single): boolean;
var temp: single;
begin
if x1 > x2 then
begin
temp := x1;
x1 := x2;
x2 := temp;
end;
if y1 > y2 then
begin
temp := y1;
y1 := y2;
y2 := temp;
end;
result := (x2-x1 > w) and (y2-y1 > w);
end;
procedure TBGRAMultishapeFiller.InternalAddStroke(
const APoints: array of TPointF; AClosed: boolean; AData: Pointer);
var pts: ArrayOfTPointF;
idxShape: Integer;
begin
with TPathStrokeData(AData^) do
begin
if AClosed then
pts := Stroker.ComputePolygon(APoints, Width)
else
pts := Stroker.ComputePolylineAutoCycle(APoints, Width);
if Texture <> nil then
idxShape := AddPolygon(pts, Texture)
else
idxShape := AddPolygon(pts, Color);
OverrideFillMode(idxShape, fmWinding);
end;
end;
constructor TBGRAMultishapeFiller.Create;
begin
nbShapes := 0;
shapes := nil;
PolygonOrder := poNone;
Antialiasing := True;
AliasingIncludeBottomRight := False;
end;
destructor TBGRAMultishapeFiller.Destroy;
var
i: Integer;
begin
for i := 0 to nbShapes-1 do
begin
if shapes[i].internalInfo then shapes[i].info.free;
shapes[i].texture := nil;
if shapes[i].internalTexture <> nil then shapes[i].internalTexture.Free;
end;
shapes := nil;
inherited Destroy;
end;
function TBGRAMultishapeFiller.AddShape(AShape: TBGRACustomFillInfo;
AColor: TBGRAPixel): integer;
begin
result := AddShape(AShape,False,nil,nil,AColor);
end;
function TBGRAMultishapeFiller.AddShape(AShape: TBGRACustomFillInfo;
ATexture: IBGRAScanner): integer;
begin
result := AddShape(AShape,False,ATexture,nil,BGRAPixelTransparent);
end;
function TBGRAMultishapeFiller.AddPolygon(const points: array of TPointF;
AColor: TBGRAPixel): integer;
begin
if length(points) <= 2 then exit(-1);
result := AddShape(TOnePassFillPolyInfo.Create(points),True,nil,nil,AColor);
end;
function TBGRAMultishapeFiller.AddPolygon(const points: array of TPointF;
ATexture: IBGRAScanner): integer;
begin
if length(points) <= 2 then exit(-1);
result := AddShape(TOnePassFillPolyInfo.Create(points),True,ATexture,nil,BGRAPixelTransparent);
end;
procedure TBGRAMultishapeFiller.AddPathStroke(APath: TBGRAPath;
AColor: TBGRAPixel; AWidth: single; AStroker: TBGRACustomPenStroker);
begin
AddPathStroke(APath,AffineMatrixIdentity,AColor,AWidth,AStroker);
end;
procedure TBGRAMultishapeFiller.AddPathStroke(APath: TBGRAPath;
ATexture: IBGRAScanner; AWidth: single; AStroker: TBGRACustomPenStroker);
begin
AddPathStroke(APath,AffineMatrixIdentity,ATexture,AWidth,AStroker);
end;
procedure TBGRAMultishapeFiller.AddPathStroke(APath: TBGRAPath;
AMatrix: TAffineMatrix; AColor: TBGRAPixel; AWidth: single;
AStroker: TBGRACustomPenStroker);
var data: TPathStrokeData;
begin
data.Stroker := AStroker;
data.Color := AColor;
data.Texture := nil;
data.Width := AWidth;
APath.stroke({$IFDEF OBJ}@{$ENDIF}InternalAddStroke, AMatrix, 0.1, @data);
end;
procedure TBGRAMultishapeFiller.AddPathStroke(APath: TBGRAPath;
AMatrix: TAffineMatrix; ATexture: IBGRAScanner; AWidth: single;
AStroker: TBGRACustomPenStroker);
var data: TPathStrokeData;
begin
data.Stroker := AStroker;
data.Color := BGRAPixelTransparent;
data.Texture := ATexture;
data.Width := AWidth;
APath.stroke({$IFDEF OBJ}@{$ENDIF}InternalAddStroke, AMatrix, 0.1, @data);
end;
function TBGRAMultishapeFiller.AddPathFill(APath: TBGRAPath; AColor: TBGRAPixel): integer;
begin
result := AddPolygon(APath.ToPoints, AColor);
end;
function TBGRAMultishapeFiller.AddPathFill(APath: TBGRAPath;
ATexture: IBGRAScanner): integer;
begin
result := AddPolygon(APath.ToPoints, ATexture);
end;
function TBGRAMultishapeFiller.AddPathFill(APath: TBGRAPath;
AMatrix: TAffineMatrix; AColor: TBGRAPixel): integer;
begin
result := AddPolygon(APath.ToPoints(AMatrix), AColor);
end;
function TBGRAMultishapeFiller.AddPathFill(APath: TBGRAPath;
AMatrix: TAffineMatrix; ATexture: IBGRAScanner): integer;
begin
result := AddPolygon(APath.ToPoints(AMatrix), ATexture);
end;
function TBGRAMultishapeFiller.AddPolylineStroke(
const points: array of TPointF; AColor: TBGRAPixel; AWidth: single;
AStroker: TBGRACustomPenStroker): integer;
begin
result := AddPolygon(AStroker.ComputePolyline(points,AWidth,AColor), AColor);
end;
function TBGRAMultishapeFiller.AddPolylineStroke(
const points: array of TPointF; ATexture: IBGRAScanner; AWidth: single;
AStroker: TBGRACustomPenStroker): integer;
begin
result := AddPolygon(AStroker.ComputePolyline(points,AWidth), ATexture);
end;
function TBGRAMultishapeFiller.AddPolygonStroke(const points: array of TPointF;
AColor: TBGRAPixel; AWidth: single; AStroker: TBGRACustomPenStroker): integer;
begin
result := AddPolygon(AStroker.ComputePolygon(points,AWidth), AColor);
end;
function TBGRAMultishapeFiller.AddPolygonStroke(const points: array of TPointF;
ATexture: IBGRAScanner; AWidth: single; AStroker: TBGRACustomPenStroker
): integer;
begin
result := AddPolygon(AStroker.ComputePolygon(points,AWidth), ATexture);
end;
function TBGRAMultishapeFiller.AddTriangleLinearColor(pt1, pt2, pt3: TPointF;
c1, c2, c3: TBGRAPixel): integer;
var grad: TBGRAGradientTriangleScanner;
begin
if BGRAPixelEqual(c1, {=} c2) and BGRAPixelEqual(c2, {=} c3) then
result := AddPolygon([pt1,pt2,pt3],c1)
else
begin
grad := TBGRAGradientTriangleScanner.Create(pt1,pt2,pt3, c1,c2,c3);
result := AddShape(TOnePassFillPolyInfo.Create([pt1,pt2,pt3]),True,grad,grad,BGRAPixelTransparent);
end;
end;
function TBGRAMultishapeFiller.AddTriangleLinearMapping(pt1, pt2, pt3: TPointF;
texture: IBGRAScanner; tex1, tex2, tex3: TPointF): integer;
var
mapping: TBGRATriangleLinearMapping;
begin
mapping := TBGRATriangleLinearMapping.Create(texture, pt1,pt2,pt3, tex1, tex2, tex3);
result := AddShape(TOnePassFillPolyInfo.Create([pt1,pt2,pt3]),True,mapping,mapping,BGRAPixelTransparent);
end;
procedure TBGRAMultishapeFiller.AddQuadLinearColor(pt1, pt2, pt3, pt4: TPointF;
c1, c2, c3, c4: TBGRAPixel);
var
center: TPointF;
centerColor: TBGRAPixel;
begin
if BGRAPixelEqual(c1, {=} c2) and BGRAPixelEqual(c2, {=} c3) and BGRAPixelEqual(c3, {=} c4) then
AddPolygon([pt1,pt2,pt3,pt4],c1)
else
begin
center := (pt1+pt2+pt3+pt4)*(1/4);
centerColor := GammaCompression( MergeBGRA(MergeBGRA(GammaExpansion(c1),GammaExpansion(c2)),
MergeBGRA(GammaExpansion(c3),GammaExpansion(c4))) );
AddTriangleLinearColor(pt1,pt2,center, c1,c2,centerColor);
AddTriangleLinearColor(pt2,pt3,center, c2,c3,centerColor);
AddTriangleLinearColor(pt3,pt4,center, c3,c4,centerColor);
AddTriangleLinearColor(pt4,pt1,center, c4,c1,centerColor);
end;
end;
procedure TBGRAMultishapeFiller.AddQuadLinearMapping(pt1, pt2, pt3,
pt4: TPointF; texture: IBGRAScanner; tex1, tex2, tex3, tex4: TPointF;
ACulling: TFaceCulling);
var
mapping: TBGRAQuadLinearScanner;
begin
mapping := TBGRAQuadLinearScanner.Create(texture,
[tex1,tex2,tex3,tex4],
[pt1,pt2,pt3,pt4]);
mapping.Culling := ACulling;
AddShape(TOnePassFillPolyInfo.Create([pt1,pt2,pt3,pt4]),True,mapping,mapping,BGRAPixelTransparent);