forked from AngusJohnson/Image32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImg32.Extra.pas
3260 lines (3077 loc) · 97.8 KB
/
Img32.Extra.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
unit Img32.Extra;
(*******************************************************************************
* Author : Angus Johnson *
* Version : 4.4 *
* Date : 21 February 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2019-2023 *
* Purpose : Miscellaneous routines that don't belong in other modules. *
* License : http://www.boost.org/LICENSE_1_0.txt *
*******************************************************************************)
interface
{$I Img32.inc}
uses
SysUtils, Classes, Math, Types,
Img32, Img32.Draw, Img32.Vector;
type
TButtonShape = (bsRound, bsSquare, bsDiamond);
TButtonAttribute = (baShadow, ba3D, baEraseBeneath);
TButtonAttributes = set of TButtonAttribute;
type
PPt = ^TPt;
TPt = record
pt : TPointD;
vec : TPointD;
len : double;
next : PPt;
prev : PPt;
end;
TFitCurveContainer = class
private
ppts : PPt;
solution : TPathD;
tolSqrd : double;
function Count(first, last: PPt): integer;
function AddPt(const pt: TPointD): PPt;
procedure Clear;
function ComputeLeftTangent(p: PPt): TPointD;
function ComputeRightTangent(p: PPt): TPointD;
function ComputeCenterTangent(p: PPt): TPointD;
function ChordLengthParameterize(
first: PPt; cnt: integer): TArrayOfDouble;
function GenerateBezier(first, last: PPt; cnt: integer;
const u: TArrayOfDouble; const firstTan, lastTan: TPointD): TPathD;
function Reparameterize(first: PPt; cnt: integer;
const u: TArrayOfDouble; const bezier: TPathD): TArrayOfDouble;
function NewtonRaphsonRootFind(const q: TPathD;
const pt: TPointD; u: double): double;
function ComputeMaxErrorSqrd(first, last: PPt;
const bezier: TPathD; const u: TArrayOfDouble;
out SplitPoint: PPt): double;
function FitCubic(first, last: PPt;
firstTan, lastTan: TPointD): Boolean;
procedure AppendSolution(const bezier: TPathD);
public
function FitCurve(const path: TPathD; closed: Boolean;
tolerance: double; minSegLength: double): TPathD;
end;
procedure DrawEdge(img: TImage32; const rec: TRect;
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0); overload;
procedure DrawEdge(img: TImage32; const rec: TRectD;
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0); overload;
procedure DrawEdge(img: TImage32; const path: TPathD;
topLeftColor, bottomRightColor: TColor32;
penWidth: double = 1.0; closePath: Boolean = true); overload;
//DrawShadowRect: is **much** faster than DrawShadow
procedure DrawShadowRect(img: TImage32; const rec: TRect; depth: double;
angle: double = angle45; color: TColor32 = $80000000);
procedure DrawShadow(img: TImage32; const polygon: TPathD;
fillRule: TFillRule; depth: double; angleRads: double = angle45;
color: TColor32 = $80000000; cutoutInsideShadow: Boolean = false); overload;
procedure DrawShadow(img: TImage32; const polygons: TPathsD;
fillRule: TFillRule; depth: double; angleRads: double = angle45;
color: TColor32 = $80000000; cutoutInsideShadow: Boolean = false); overload;
procedure DrawGlow(img: TImage32; const polygon: TPathD;
fillRule: TFillRule; color: TColor32; blurRadius: integer); overload;
procedure DrawGlow(img: TImage32; const polygons: TPathsD;
fillRule: TFillRule; color: TColor32; blurRadius: integer); overload;
procedure TileImage(img: TImage32; const rec: TRect; tile: TImage32); overload;
procedure TileImage(img: TImage32; const rec: TRect; tile: TImage32; const tileRec: TRect); overload;
//FloodFill: If no CompareFunc is provided, FloodFill will fill whereever
//adjoining pixels exactly match the starting pixel - Point(x,y).
procedure FloodFill(img: TImage32; x, y: Integer; newColor: TColor32;
tolerance: Byte = 0; compareFunc: TCompareFunctionEx = nil);
procedure FastGaussianBlur(img: TImage32;
const rec: TRect; stdDev: integer; repeats: integer = 2); overload;
procedure FastGaussianBlur(img: TImage32;
const rec: TRect; stdDevX, stdDevY: integer; repeats: integer); overload;
procedure GaussianBlur(img: TImage32; rec: TRect; radius: Integer);
//Emboss: A smaller radius is sharper. Increasing depth increases contrast.
//Luminance changes grayscale balance (unless preserveColor = true)
procedure Emboss(img: TImage32; radius: Integer = 1; depth: Integer = 10;
luminance: Integer = 75; preserveColor: Boolean = false);
//Sharpen: Radius range is 1 - 10; amount range is 1 - 50.<br>
//see https://en.wikipedia.org/wiki/Unsharp_masking
procedure Sharpen(img: TImage32; radius: Integer = 2; amount: Integer = 10);
//HatchBackground: Assumes the current image is semi-transparent.
procedure HatchBackground(img: TImage32; color1: TColor32 = clWhite32;
color2: TColor32= $FFE8E8E8; hatchSize: Integer = 10); overload;
procedure HatchBackground(img: TImage32; const rec: TRect;
color1: TColor32 = clWhite32; color2: TColor32= $FFE8E8E8;
hatchSize: Integer = 10); overload;
procedure GridBackground(img: TImage32; majorInterval, minorInterval: integer;
fillColor: TColor32 = clWhite32;
majColor: TColor32 = $30000000; minColor: TColor32 = $20000000);
procedure ReplaceExactColor(img: TImage32; oldColor, newColor: TColor32);
//EraseColor: Removes the specified color from the image, even from
//pixels that are a blend of colors including the specified color.<br>
//see https://stackoverflow.com/questions/9280902/
procedure EraseColor(img: TImage32; color: TColor32);
//FilterOnColor: Removes everything not nearly matching 'color'
//This uses an algorithm that's very similar to the one in EraseColor.
procedure FilterOnColor(img: TImage32; color: TColor32);
//RedEyeRemove: Removes 'red eye' from flash photo images.
procedure RedEyeRemove(img: TImage32; const rect: TRect);
procedure PencilEffect(img: TImage32; intensity: integer = 0);
procedure TraceContours(img: TImage32; intensity: integer);
procedure EraseInsidePath(img: TImage32;
const path: TPathD; fillRule: TFillRule);
procedure EraseInsidePaths(img: TImage32;
const paths: TPathsD; fillRule: TFillRule);
procedure EraseOutsidePath(img: TImage32; const path: TPathD;
fillRule: TFillRule; const outsideBounds: TRect);
procedure EraseOutsidePaths(img: TImage32; const paths: TPathsD;
fillRule: TFillRule; const outsideBounds: TRect);
procedure Draw3D(img: TImage32; const polygon: TPathD;
fillRule: TFillRule; height, blurRadius: double;
colorLt: TColor32 = $DDFFFFFF; colorDk: TColor32 = $80000000;
angleRads: double = angle225); overload;
procedure Draw3D(img: TImage32; const polygons: TPathsD;
fillRule: TFillRule; height, blurRadius: double;
colorLt: TColor32 = $DDFFFFFF; colorDk: TColor32 = $80000000;
angleRads: double = angle225); overload;
function RainbowColor(fraction: double; luminance: byte = 128): TColor32;
function GradientColor(color1, color2: TColor32; frac: single): TColor32;
function MakeDarker(color: TColor32; percent: cardinal): TColor32;
function MakeLighter(color: TColor32; percent: cardinal): TColor32;
function DrawButton(img: TImage32; const pt: TPointD;
size: double; color: TColor32 = clNone32;
buttonShape: TButtonShape = bsRound;
buttonAttributes: TButtonAttributes = [baShadow, ba3D, baEraseBeneath]): TPathD;
//Vectorize: convert an image into polygon vectors
function Vectorize(img: TImage32; compareColor: TColor32;
compareFunc: TCompareFunction; colorTolerance: Integer;
roundingTolerance: integer = 2): TPathsD;
function VectorizeMask(const mask: TArrayOfByte; maskWidth: integer): TPathsD;
// RamerDouglasPeucker: simplifies paths, recursively removing vertices where
// they deviate no more than 'epsilon' from their adjacent vertices.
function RamerDouglasPeucker(const path: TPathD;
epsilon: double): TPathD; overload;
function RamerDouglasPeucker(const paths: TPathsD;
epsilon: double): TPathsD; overload;
// SimplifyPath: Better than RDP when simplifying closed paths
function SimplifyPath(const path: TPathD;
epsilon: double; isOpenPath: Boolean = false): TPathD;
function SimplifyPaths(const paths: TPathsD;
epsilon: double; isOpenPath: Boolean = false): TPathsD;
// SmoothToCubicBezier - produces a series of cubic bezier control points.
// This function is very useful in the following combination:
// RamerDouglasPeucker(), SmoothToCubicBezier(), FlattenCBezier().
function SmoothToCubicBezier(const path: TPathD;
pathIsClosed: Boolean; maxOffset: integer = 0): TPathD; overload;
function SmoothToCubicBezier(const paths: TPathsD;
pathIsClosed: Boolean; maxOffset: integer = 0): TPathsD; overload;
//InterpolatePoints: smooths a simple line chart.
//Points should be left to right and equidistant along the X axis
function InterpolatePoints(const points: TPathD; tension: integer = 0): TPathD;
function GetFloodFillMask(imgIn, imgMaskOut: TImage32; x, y: Integer;
tolerance: Byte; compareFunc: TCompareFunctionEx): Boolean;
procedure SymmetricCropTransparent(img: TImage32);
//3 additional blend functions (see TImage32.CopyBlend)
function BlendAverage(bgColor, fgColor: TColor32): TColor32;
function BlendLinearBurn(bgColor, fgColor: TColor32): TColor32;
function BlendColorDodge(bgColor, fgColor: TColor32): TColor32;
//CurveFit: this function is based on -
//"An Algorithm for Automatically Fitting Digitized Curves"
//by Philip J. Schneider in "Graphics Gems", Academic Press, 1990
//Smooths out many very closely positioned points
//tolerance range: 1..10 where 10 == max tolerance.
function CurveFit(const path: TPathD; closed: Boolean;
tolerance: double; minSegLength: double = 2): TPathD; overload;
function CurveFit(const paths: TPathsD; closed: Boolean;
tolerance: double; minSegLength: double = 2): TPathsD; overload;
implementation
uses
{$IFNDEF MSWINDOWS}
{$IFNDEF FPC}
Img32.FMX,
{$ENDIF}
{$ENDIF}
Img32.Transform;
const
FloodFillDefaultRGBTolerance: byte = 64;
MaxBlur = 100;
type
PColor32Array = ^TColor32Array;
TColor32Array = array [0.. maxint div SizeOf(TColor32) -1] of TColor32;
PWeightedColorArray = ^TWeightedColorArray;
TWeightedColorArray = array [0.. $FFFFFF] of TWeightedColor;
//------------------------------------------------------------------------------
// Miscellaneous functions
//------------------------------------------------------------------------------
function GetSymmetricCropTransparentRect(img: TImage32): TRect;
var
w,h, x,y, x1,y1: Integer;
p1,p2: PARGB;
opaquePxlFound: Boolean;
begin
Result := img.Bounds;
w := img.Width;
y1 := 0;
opaquePxlFound := false;
for y := 0 to (img.Height div 2) -1 do
begin
p1 := PARGB(img.PixelRow[y]);
p2 := PARGB(img.PixelRow[img.Height - y -1]);
for x := 0 to w -1 do
begin
if (p1.A > 0) or (p2.A > 0) then
begin
y1 := y;
opaquePxlFound := true;
break;
end;
inc(p1); inc(p2);
end;
if opaquePxlFound then break;
end;
// probably safeset not to resize empty images
if not opaquePxlFound then Exit;
if y1 > 0 then
begin
inc(Result.Top, y1);
dec(Result.Bottom, y1);
end;
x1 := 0;
h := RectHeight(Result);
opaquePxlFound := false;
for x := 0 to (w div 2) -1 do
begin
p1 := PARGB(@img.Pixels[Result.Top * w + x]);
p2 := PARGB(@img.Pixels[Result.Top * w + (w -1) - x]);
for y := 0 to h -1 do
begin
if (p1.A > 0) or (p2.A > 0) then
begin
x1 := x;
opaquePxlFound := true;
break;
end;
inc(p1, w); inc(p2, w);
end;
if opaquePxlFound then break;
end;
if not opaquePxlFound then Exit;
inc(Result.Left, x1);
dec(Result.Right, x1);
end;
//------------------------------------------------------------------------------
//SymmetricCropTransparent: after cropping, the image's midpoint
//will be the same pixel as before cropping. (Important for rotating.)
procedure SymmetricCropTransparent(img: TImage32);
var
rec: TRect;
begin
rec := GetSymmetricCropTransparentRect(img);
if (rec.Top > 0) or (rec.Left > 0) then img.Crop(rec);
end;
//------------------------------------------------------------------------------
procedure DrawEdge(img: TImage32; const rec: TRect;
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0);
begin
DrawEdge(img, RectD(rec), topLeftColor, bottomRightColor, penWidth);
end;
//------------------------------------------------------------------------------
procedure DrawEdge(img: TImage32; const rec: TRectD;
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0);
var
p: TPathD;
c: TColor32;
begin
if penWidth = 0 then Exit
else if penWidth < 0 then
begin
c := topLeftColor;
topLeftColor := bottomRightColor;
bottomRightColor := c;
penWidth := -penWidth;
end;
if topLeftColor <> bottomRightColor then
begin
with rec do
begin
p := Img32.Vector.MakePath([left, bottom, left, top, right, top]);
DrawLine(img, p, penWidth, topLeftColor, esButt);
p := Img32.Vector.MakePath([right, top, right, bottom, left, bottom]);
DrawLine(img, p, penWidth, bottomRightColor, esButt);
end;
end else
DrawLine(img, Rectangle(rec), penWidth, topLeftColor, esPolygon);
end;
//------------------------------------------------------------------------------
procedure DrawEdge(img: TImage32; const path: TPathD;
topLeftColor, bottomRightColor: TColor32;
penWidth: double = 1.0; closePath: Boolean = true);
var
i, highI, deg: integer;
frac: double;
c: TColor32;
p: TPathD;
const
RadToDeg = 180/PI;
begin
if penWidth = 0 then Exit
else if penWidth < 0 then
begin
c := topLeftColor;
topLeftColor := bottomRightColor;
bottomRightColor := c;
penWidth := -penWidth;
end;
highI := high(path);
if highI < 2 then Exit;
p := path;
if closePath and not PointsNearEqual(p[0], p[highI], 0.01) then
begin
AppendPath(p, p[0]);
inc(highI);
end;
for i := 1 to highI do
begin
deg := Round(GetAngle(p[i-1], p[i]) * RadToDeg);
case deg of
-180..-136: frac := (-deg-135)/45;
-135..0 : frac := 0;
1..44 : frac := deg/45;
else frac := 1;
end;
c := GradientColor(topLeftColor, bottomRightColor, frac);
DrawLine(img, p[i-1], p[i], penWidth, c);
end;
end;
//------------------------------------------------------------------------------
procedure FillColorHorz(img: TImage32; x, endX, y: integer; color: TColor32);
var
i,dx: integer;
p: PColor32;
begin
if (x < 0) or (x >= img.Width) then Exit;
if (y < 0) or (y >= img.Height) then Exit;
p := img.PixelRow[y]; inc(p, x);
if endX >= img.Width then endX := img.Width -1
else if endX < 0 then endX := 0;
if endX < x then dx := -1 else dx := 1;
for i := 0 to Abs(x-endX) do
begin
p^ := color;
inc(p, dx);
end;
end;
//------------------------------------------------------------------------------
procedure FillColorVert(img: TImage32; x, y, endY: integer; color: TColor32);
var
i, dy: integer;
p: PColor32;
begin
if (x < 0) or (x >= img.Width) then Exit;
if (y < 0) or (y >= img.Height) then Exit;
p := img.PixelRow[y]; inc(p, x);
if endY >= img.Height then
endY := img.Height -1 else if endY < 0 then endY := 0;
dy := img.Width;
if endY < y then dy := -dy;
for i := 0 to Abs(y - endY) do
begin
p^ := color;
inc(p, dy);
end;
end;
//------------------------------------------------------------------------------
procedure DrawShadowRect(img: TImage32; const rec: TRect; depth: double;
angle: double = angle45; color: TColor32 = $80000000);
var
i,j, sX,sY: integer;
l,t,r,b: integer;
tmpImg: TImage32;
tmpRec: TRect;
xx,yy: double;
ss: TPointD;
c: TColor32;
begin
GetSinCos(angle, yy, xx);
ss.X := depth * xx;
ss.Y := depth * yy;
sX := Abs(Round(ss.X));
sY := Abs(Round(ss.Y));
if rec.Left + ss.X < 0 then ss.X := -rec.Left
else if rec.Right + ss.X > img.Width then ss.X := img.Width - rec.Right -1;
if rec.Top + ss.Y < 0 then ss.Y := -rec.Top
else if rec.Bottom + ss.Y > img.Height then ss.Y := img.Height -rec.Bottom -1;
tmpImg := TImage32.Create(sX*3 +1, sY*3 +1);
try
i := sX div 2; j := sY div 2;
DrawPolygon(tmpImg, Rectangle(i,j,i+sX*2,j+sY*2), frNonZero, color);
FastGaussianBlur(tmpImg, tmpImg.Bounds, Round(sX/4),Round(sY/4), 1);
// t-l corner
if (ss.X < 0) or (ss.Y < 0) then
begin
tmpRec := Rect(0, 0, sX, sY);
l := rec.Left; t := rec.Top;
if ss.X < 0 then dec(l, sX);
if ss.Y < 0 then dec(t, sY);
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY));
end;
// t-r corner
if (ss.X > 0) or (ss.Y < 0) then
begin
tmpRec := Rect(sX*2+1, 0, sX*3+1, sY);
l := rec.Right; t := rec.Top;
if ss.X < 0 then dec(l, sX);
if ss.Y < 0 then dec(t, sY);
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY));
end;
// b-l corner
if (ss.X < 0) or (ss.Y > 0) then
begin
tmpRec := Rect(0, sY*2+1, sX, sY*3+1);
l := rec.Left; t := rec.Bottom;
if ss.X < 0 then dec(l, sX);
if ss.Y < 0 then dec(t, sY);
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY));
end;
// b-r corner
if (ss.X > 0) or (ss.Y > 0) then
begin
tmpRec := Rect(sX*2+1, sY*2+1, sX*3+1, sY*3+1);
l := rec.Right; t := rec.Bottom;
if ss.X < 0 then dec(l, sX);
if ss.Y < 0 then dec(t, sY);
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY));
end;
// l-edge
if (ss.X < 0) then
begin
l := rec.Left; t := rec.Top+sY; b := rec.Bottom-1;
if ss.Y < 0 then begin dec(t, sY); dec(b,sY); end;
for i := 1 to sX do
begin
c := tmpImg.Pixel[sX-i, sY+1];
FillColorVert(img, l-i, t, b, c);
end;
end;
// t-edge
if (ss.Y < 0) then
begin
l := rec.Left+sX; r := rec.Right-1; t := rec.Top;
if ss.X < 0 then begin dec(l, sX); dec(r,sX); end;
for i := 1 to sY do
begin
c := tmpImg.Pixel[sX+1, sY-i];
FillColorHorz(img, l, r, t-i, c);
end;
end;
// r-edge
if (ss.X > 0) then
begin
r := rec.Right-1; t := rec.Top+sY; b := rec.Bottom-1;
if ss.Y < 0 then begin dec(t, sY); dec(b,sY); end;
for i := 1 to sX do
begin
c := tmpImg.Pixel[sX*2+i, sY+1];
FillColorVert(img, r+i, t, b, c);
end;
end;
// b-edge
if (ss.Y > 0) then
begin
l := rec.Left+sX; r := rec.Right-1; b := rec.Bottom-1;
if ss.X < 0 then begin dec(l, sX); dec(r,sX); end;
for i := 1 to sY do
begin
c := tmpImg.Pixel[sX+1, sY*2+i];
FillColorHorz(img, l, r, b+i, c);
end;
end;
finally
tmpImg.Free;
end;
end;
//------------------------------------------------------------------------------
procedure DrawShadow(img: TImage32; const polygon: TPathD;
fillRule: TFillRule; depth: double; angleRads: double;
color: TColor32; cutoutInsideShadow: Boolean);
var
polygons: TPathsD;
begin
setlength(polygons, 1);
polygons[0] := polygon;
DrawShadow(img, polygons, fillRule, depth,
angleRads, color, cutoutInsideShadow);
end;
//------------------------------------------------------------------------------
procedure DrawShadow(img: TImage32; const polygons: TPathsD;
fillRule: TFillRule; depth: double; angleRads: double;
color: TColor32; cutoutInsideShadow: Boolean);
var
x, y: double;
blurSize, w,h: integer;
rec: TRect;
polys, shadowPolys: TPathsD;
shadowImg: TImage32;
begin
rec := GetBounds(polygons);
if IsEmptyRect(rec) or (depth < 1) then Exit;
if not ClockwiseRotationIsAnglePositive then angleRads := -angleRads;
NormalizeAngle(angleRads);
GetSinCos(angleRads, y, x);
depth := depth * 0.5;
x := depth * x;
y := depth * y;
blurSize := Max(1,Round(depth / 2));
Img32.Vector.InflateRect(rec, Ceil(depth*2), Ceil(depth*2));
polys := OffsetPath(polygons, -rec.Left, -rec.Top);
shadowPolys := OffsetPath(polys, x, y);
RectWidthHeight(rec, w, h);
shadowImg := TImage32.Create(w, h);
try
DrawPolygon(shadowImg, shadowPolys, fillRule, color);
FastGaussianBlur(shadowImg, shadowImg.Bounds, blurSize, 1);
if cutoutInsideShadow then EraseInsidePaths(shadowImg, polys, fillRule);
img.CopyBlend(shadowImg, shadowImg.Bounds, rec, BlendToAlpha);
finally
shadowImg.Free;
end;
end;
//------------------------------------------------------------------------------
procedure DrawGlow(img: TImage32; const polygon: TPathD;
fillRule: TFillRule; color: TColor32; blurRadius: integer);
var
polygons: TPathsD;
begin
setlength(polygons, 1);
polygons[0] := polygon;
DrawGlow(img, polygons, fillRule, color, blurRadius);
end;
//------------------------------------------------------------------------------
procedure DrawGlow(img: TImage32; const polygons: TPathsD;
fillRule: TFillRule; color: TColor32; blurRadius: integer);
var
w,h: integer;
rec: TRect;
glowPolys: TPathsD;
glowImg: TImage32;
begin
rec := GetBounds(polygons);
glowPolys := OffsetPath(polygons,
blurRadius -rec.Left +1, blurRadius -rec.Top +1);
Img32.Vector.InflateRect(rec, blurRadius +1, blurRadius +1);
RectWidthHeight(rec, w, h);
glowImg := TImage32.Create(w, h);
try
DrawPolygon(glowImg, glowPolys, fillRule, color);
FastGaussianBlur(glowImg, glowImg.Bounds, blurRadius, 2);
glowImg.ScaleAlpha(4);
img.CopyBlend(glowImg, glowImg.Bounds, rec, BlendToAlpha);
finally
glowImg.Free;
end;
end;
//------------------------------------------------------------------------------
procedure TileImage(img: TImage32; const rec: TRect; tile: TImage32);
begin
TileImage(img, rec, tile, tile.Bounds);
end;
//------------------------------------------------------------------------------
procedure TileImage(img: TImage32;
const rec: TRect; tile: TImage32; const tileRec: TRect);
var
i, dstW, dstH, srcW, srcH, cnt: integer;
dstRec, srcRec: TRect;
begin
if tile.IsEmpty or IsEmptyRect(tileRec) then Exit;
RectWidthHeight(rec, dstW,dstH);
RectWidthHeight(tileRec, srcW, srcH);
cnt := Ceil(dstW / srcW);
dstRec := Img32.Vector.Rect(rec.Left, rec.Top,
rec.Left + srcW, rec.Top + srcH);
for i := 1 to cnt do
begin
img.Copy(tile, tileRec, dstRec);
Types.OffsetRect(dstRec, srcW, 0);
end;
cnt := Ceil(dstH / srcH) -1;
srcRec := Img32.Vector.Rect(rec.Left, rec.Top,
rec.Right, rec.Top + srcH);
dstRec := srcRec;
for i := 1 to cnt do
begin
Types.OffsetRect(dstRec, 0, srcH);
img.Copy(img, srcRec, dstRec);
end;
end;
//------------------------------------------------------------------------------
procedure Sharpen(img: TImage32; radius: Integer; amount: Integer);
var
i: Integer;
amt: double;
weightAmount: array [-255 .. 255] of Integer;
bmpBlur: TImage32;
pColor, pBlur: PARGB;
begin
if radius = 0 then Exit;
amt := ClampRange(amount/10, 0.1, 5);
radius := ClampRange(radius, 1, 10);
for i := -255 to 255 do
weightAmount[i] := Round(amt * i);
bmpBlur := TImage32.Create(img); // clone self
try
pColor := PARGB(img.pixelBase);
FastGaussianBlur(bmpBlur, bmpBlur.Bounds, radius, 2);
pBlur := PARGB(bmpBlur.pixelBase);
for i := 1 to img.Width * img.Height do
begin
if (pColor.A > 0) then
begin
pColor.R := ClampByte(pColor.R + weightAmount[pColor.R - pBlur.R]);
pColor.G := ClampByte(pColor.G + weightAmount[pColor.G - pBlur.G]);
pColor.B := ClampByte(pColor.B + weightAmount[pColor.B - pBlur.B]);
end;
Inc(pColor); Inc(pBlur);
end;
finally
bmpBlur.Free;
end;
end;
//------------------------------------------------------------------------------
procedure HatchBackground(img: TImage32; const rec: TRect;
color1: TColor32 = clWhite32; color2: TColor32= $FFE8E8E8;
hatchSize: Integer = 10); overload;
var
i,j: Integer;
pc: PColor32;
colors: array[boolean] of TColor32;
hatch: Boolean;
begin
colors[false] := color1;
colors[true] := color2;
img.BeginUpdate;
try
for i := rec.Top to rec.Bottom -1 do
begin
pc := img.PixelRow[i];
inc(pc, rec.Left);
hatch := Odd(i div hatchSize);
for j := rec.Left to rec.Right -1 do
begin
if (j + 1) mod hatchSize = 0 then hatch := not hatch;
pc^ := BlendToOpaque(pc^, colors[hatch]);
inc(pc);
end;
end;
finally
img.EndUpdate;
end;
end;
//------------------------------------------------------------------------------
procedure HatchBackground(img: TImage32;
color1: TColor32; color2: TColor32; hatchSize: Integer);
begin
HatchBackground(img, img.Bounds, color1, color2, hatchSize);
end;
//------------------------------------------------------------------------------
procedure GridBackground(img: TImage32; majorInterval, minorInterval: integer;
fillColor: TColor32; majColor: TColor32; minColor: TColor32);
var
i, x,y, w,h: integer;
path: TPathD;
begin
img.Clear(fillColor);
w := img.Width; h := img.Height;
SetLength(path, 2);
if minorInterval > 0 then
begin
x := minorInterval;
path[0] := PointD(x, 0); path[1] := PointD(x, h);;
for i := 1 to (w div minorInterval) do
begin
Img32.Draw.DrawLine(img, path, 1, minColor, esSquare);
path[0].X := path[0].X + minorInterval;
path[1].X := path[1].X + minorInterval;
end;
y := minorInterval;
path[0] := PointD(0, y); path[1] := PointD(w, y);
for i := 1 to (h div minorInterval) do
begin
Img32.Draw.DrawLine(img, path, 1, minColor, esSquare);
path[0].Y := path[0].Y + minorInterval;
path[1].Y := path[1].Y + minorInterval;
end;
end;
if majorInterval > minorInterval then
begin
x := majorInterval;
path[0] := PointD(x, 0); path[1] := PointD(x, h);;
for i := 1 to (w div majorInterval) do
begin
Img32.Draw.DrawLine(img, path, 1, majColor, esSquare);
path[0].X := path[0].X + majorInterval;
path[1].X := path[1].X + majorInterval;
end;
y := majorInterval;
path[0] := PointD(0, y); path[1] := PointD(w, y);
for i := 1 to (h div majorInterval) do
begin
Img32.Draw.DrawLine(img, path, 1, majColor, esSquare);
path[0].Y := path[0].Y + majorInterval;
path[1].Y := path[1].Y + majorInterval;
end;
end;
end;
//------------------------------------------------------------------------------
function ColorDifference(color1, color2: TColor32): cardinal;
{$IFDEF INLINE} inline; {$ENDIF}
var
c1: TARGB absolute color1;
c2: TARGB absolute color2;
begin
result := Abs(c1.R - c2.R) + Abs(c1.G - c2.G) + Abs(c1.B - c2.B);
result := (result * 341) shr 10; // divide by 3
end;
//------------------------------------------------------------------------------
procedure ReplaceExactColor(img: TImage32; oldColor, newColor: TColor32);
var
color: PColor32;
i: Integer;
begin
color := img.PixelBase;
for i := 0 to img.Width * img.Height -1 do
begin
if color^ = oldColor then color^ := newColor;
inc(color);
end;
end;
//------------------------------------------------------------------------------
procedure EraseColor(img: TImage32; color: TColor32);
var
fg: TARGB absolute color;
bg: PARGB;
i: Integer;
Q: byte;
begin
if fg.A = 0 then Exit;
bg := PARGB(img.PixelBase);
for i := 0 to img.Width * img.Height -1 do
begin
if bg.A > 0 then
begin
// red
if (bg.R > fg.R) then
Q := DivTable[bg.R - fg.R, not fg.R]
else if (bg.R < fg.R) then
Q := DivTable[fg.R - bg.R, fg.R]
else
Q := 0;
// green
if (bg.G > fg.G) then
Q := Max(Q, DivTable[bg.G - fg.G, not fg.G])
else if (bg.G < fg.G) then
Q := Max(Q, DivTable[fg.G - bg.G, fg.G]);
// blue
if (bg.B > fg.B) then
Q := Max(Q, DivTable[bg.B - fg.B, not fg.B])
else if (bg.B < fg.B) then
Q := Max(Q, DivTable[fg.B - bg.B, fg.B]);
// Q = 255 - (erase frac * 255)
if (Q > 0) then
begin
bg.A := MulTable[bg.A, Q];
bg.R := DivTable[bg.R - MulTable[fg.R, not Q], Q];
bg.G := DivTable[bg.G - MulTable[fg.G, not Q], Q];
bg.B := DivTable[bg.B - MulTable[fg.B, not Q], Q];
end else
bg.Color := clNone32;
end;
inc(bg);
end;
end;
//------------------------------------------------------------------------------
procedure FilterOnColor(img: TImage32; color: TColor32);
var
fg: TARGB absolute color;
bg: PARGB;
i: Integer;
Q: byte;
begin
if fg.A = 0 then Exit;
bg := PARGB(img.PixelBase);
for i := 0 to img.Width * img.Height -1 do
begin
if bg.A > 0 then
begin
// red
if (bg.R > fg.R) then
Q := DivTable[bg.R - fg.R, not fg.R]
else if (bg.R < fg.R) then
Q := DivTable[fg.R - bg.R, fg.R]
else
Q := 0;
// green
if (bg.G > fg.G) then
Q := Max(Q, DivTable[bg.G - fg.G, not fg.G])
else if (bg.G < fg.G) then
Q := Max(Q, DivTable[fg.G - bg.G, fg.G]);
// blue
if (bg.B > fg.B) then
Q := Max(Q, DivTable[bg.B - fg.B, not fg.B])
else if (bg.B < fg.B) then
Q := Max(Q, DivTable[fg.B - bg.B, fg.B]);
Q := MulTable[bg.A, not Q];
bg.Color := color;
bg.A := Q; // note: fg.A is ignored
end;
inc(bg);
end;
end;
//------------------------------------------------------------------------------
procedure RedEyeRemove(img: TImage32; const rect: TRect);
var
k: integer;
cutout, mask: TImage32;
path: TPathD;
cutoutRec, rect3: TRect;
radGrad: TRadialGradientRenderer;
begin
k := RectWidth(rect) * RectHeight(rect);
if k < 120 then k := 2
else if k < 230 then k := 3
else k := 4;
cutoutRec := rect;
Img32.Vector.InflateRect(cutoutRec, k, k);
cutout := TImage32.Create(img, cutoutRec);
mask := TImage32.Create(cutout.Width, cutout.Height);
radGrad := TRadialGradientRenderer.Create;
try
// fill behind the cutout with black also
// blurring the fill to soften its edges
rect3 := cutout.Bounds;
Img32.Vector.InflateRect(rect3, -k, -k);
path := Ellipse(rect3);
DrawPolygon(mask, path, frNonZero, clBlack32);
// given the very small area and small radius of the blur, the
// speed improvement of BoxBlur over GaussianBlur is inconsequential.
GaussianBlur(mask, mask.Bounds, k);
img.CopyBlend(mask, mask.Bounds, cutoutRec, BlendToOpaque);
// gradient fill to clNone32 a mask to soften cutout's edges
path := Ellipse(cutoutRec);
radGrad.SetParameters(rect3, clBlack32, clNone32);
DrawPolygon(mask, path, frNonZero, radGrad);
cutout.CopyBlend(mask, mask.Bounds, cutout.Bounds, BlendMask);
// now remove red from the cutout
EraseColor(cutout, clRed32);
// finally replace the cutout ...
img.CopyBlend(cutout, cutout.Bounds, cutoutRec, BlendToOpaque);
finally
mask.Free;
cutout.Free;
radGrad.Free;
end;
end;
//------------------------------------------------------------------------------
procedure EraseInsidePath(img: TImage32; const path: TPathD; fillRule: TFillRule);
begin
if assigned(path) then
ErasePolygon(img, path, fillRule);
end;
//------------------------------------------------------------------------------
procedure EraseInsidePaths(img: TImage32; const paths: TPathsD; fillRule: TFillRule);
begin
if assigned(paths) then
ErasePolygon(img, paths, fillRule);
end;
//------------------------------------------------------------------------------
procedure EraseOutsidePath(img: TImage32; const path: TPathD;
fillRule: TFillRule; const outsideBounds: TRect);
var
mask: TImage32;
p: TPathD;
w,h: integer;
begin
if not assigned(path) then Exit;
RectWidthHeight(outsideBounds, w,h);
mask := TImage32.Create(w, h);
try
p := OffsetPath(path, -outsideBounds.Left, -outsideBounds.top);
DrawPolygon(mask, p, fillRule, clBlack32);
img.CopyBlend(mask, mask.Bounds, outsideBounds, BlendMask);
finally
mask.Free;
end;
end;
//------------------------------------------------------------------------------
procedure EraseOutsidePaths(img: TImage32; const paths: TPathsD;
fillRule: TFillRule; const outsideBounds: TRect);
var
mask: TImage32;
pp: TPathsD;
w,h: integer;
begin
if not assigned(paths) then Exit;
RectWidthHeight(outsideBounds, w,h);
mask := TImage32.Create(w, h);
try
pp := OffsetPath(paths, -outsideBounds.Left, -outsideBounds.top);
DrawPolygon(mask, pp, fillRule, clBlack32);
img.CopyBlend(mask, mask.Bounds, outsideBounds, BlendMask);
finally
mask.Free;
end;
end;
//------------------------------------------------------------------------------
procedure Draw3D(img: TImage32; const polygon: TPathD;
fillRule: TFillRule; height, blurRadius: double;
colorLt: TColor32; colorDk: TColor32; angleRads: double);