-
Notifications
You must be signed in to change notification settings - Fork 8
/
BitmapEx.pas
2300 lines (1980 loc) · 56.7 KB
/
BitmapEx.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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
// 1-bit and RGBA bitmap objects
unit BitmapEx;
interface
uses
LclIntf, LclType,
SysUtils, Types, Classes, bmExUtils, Graphics, Math, gfMath, fnvHash,
GraphType;
type
TBrushShape = (bshRounded, bshSquare, bshSlash, bshBackslash,
bshHoriz, bshVert, bshSpray);
TGradientKind = (gkLinear, gkRadial, gkConical, gkSpiral);
TGradientRep = (grNone, grSym, grAsym);
TBitmap1 = class;
// Generic virtual canvas with alpha channel support
// Descendants must implement some basic methods (pixel read/write, painting)
TBitmapEx = class(TPersistent)
private
FFont: TFont;
// Descendants must use these variables to store the current image extents
FWidth, FHeight: integer;
function GetFont: TFont;
public
constructor Create;
destructor Destroy; override;
// General
procedure Assign(Source: TPersistent); override;
procedure AssignTo(Dest: TPersistent); override;
function Hash(OnlyAlpha: boolean): TfnvKey; virtual;
// Setting and getting image size
property Width: integer read FWidth;
property Height: integer read FHeight;
procedure Resize(NewWidth, NewHeight: integer); virtual; abstract;
function ClientRect: TRect;
// Canvas functions
procedure FillTransparent; virtual; abstract; // fills with transparency
procedure FillColor(Color: TColor32); virtual; abstract;
function GetPixel(x, y: integer): TColor32; virtual; abstract;
procedure SetPixel(x, y: integer; Color: TColor32); virtual; abstract;
// These perform range-checking
function SafeGetPixel(x, y: integer): TColor32;
procedure SafeSetPixel(x, y: integer; Color: TColor32);
procedure PutPixel(x, y: integer; Color: TColor32); virtual; abstract;
// --- The following 5 functions must use RGBA blending,
// and they must erase if Color is transparent
procedure PutPixelErase(x, y: integer; Color: TColor32); virtual; abstract;
// These 3 functions must work with all x1-2, y1-2 values
procedure PaintHorizLine(x1, x2, y: integer; Color: TColor32); virtual; abstract;
procedure PaintHorizLineMasked(x1, x2, y: integer; Color: TColor32; Mask: TBitmap1); virtual; abstract;
procedure PaintVertLine(x, y1, y2: integer; Color: TColor32); virtual; abstract;
// Paints with the specified color according to Mask
procedure PaintMask(x, y: integer; Mask: TBitmap1; Color: TColor32); virtual;
// Whether the image has pixel with alpha other than $ff
function HasTransparency: boolean;
// Whether the image has the specified pixel value
function HasColor(Color: TColor32): boolean;
// Whether the image has only cl32Black and cl32White pixels
function IsBlackAndWhite: boolean;
// convert alpha values to 0 or $ff
procedure ThresholdAlpha;
// Get the smallest rectangle which contains all the non-transparent pixels
function GetAutoCropRect: TRect;
procedure Crop(const r: TRect);
procedure Draw(xPos, yPos: integer; bm: TBitmapEx);
procedure TransformDraw(const r: TRect; Angle: double; bm: TBitmapEx; Antialias: boolean);
procedure CopyRect(x, y: integer; bm: TBitmapEx; const rSrc: TRect);
procedure CopyRectOverwrite(x, y: integer; Src: TBitmapEx; const rSrc: TRect);
// Can be overridden for faster operation.
// Same as CopyRect[Overwrite], but no range checks are necessary.
procedure CopyRectImpl(x, y: integer; bm: TBitmapEx; const rSrc: TRect); virtual;
procedure CopyRectOverwriteImpl(x, y: integer; Src: TBitmapEx; const rSrc: TRect); virtual;
procedure CreateThumbnail(Src: TBitmapEx; MaxSize: integer; Antialias: boolean);
// Renders the mask of a brush stroke and returns the bounding box of it
// Mask can be nil
function StrokeBrush(Count: integer; pt: PPointArray; BrushSize: integer;
BrushShape: TBrushShape; Color: TColor32; Mask: TBitmap1): TRect;
// Returns bounding box
function Rectangle(r: TRect; Color: TColor32; Filled: boolean;
LineWidth: integer): TRect;
// Returns bounding box
function Ellipse(r: TRect; Color: TColor32; Filled: boolean;
LineWidth: integer): TRect;
// Returns bounding box
// Mask can be nil
function Line(x1, y1, x2, y2: integer; BrushSize: integer;
BrushShape: TBrushShape; Color: TColor32; Mask: TBitmap1): TRect;
procedure Polygon(Count: integer; pt: PPointArray; Color: TColor32;
Filled: boolean; LineWidth: integer);
property Font: TFont read GetFont;
function TextExtent(const Text: string): TPoint;
procedure TextOut(x, y: integer; const Text: string; Color: TColor32;
AntiAlias: boolean);
end;
TBitsCombineMode = (bcmCopy, bcmOr, bcmAnd, bcmAndNot);
// This implementation can store a 1-bit bitmap
// whose pixels can be either cl32White (true) or cl32Transparent (false)
TBitmap1 = class(TBitmapEx)
protected
FData: Pointer;
function GetBit(x, y: integer): boolean; inline;
procedure SetBit(x, y: integer; const Value: boolean); inline;
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Resize(NewWidth, NewHeight: integer); override;
function LineSize: integer; inline;
function ScanLine(y: integer): PByteArray; inline;
procedure FillTransparent; override;
procedure FillColor(Color: TColor32); override;
function GetPixel(x, y: integer): TColor32; override;
procedure SetPixel(x, y: integer; Color: TColor32); override;
procedure PutPixel(x, y: integer; Color: TColor32); override;
procedure PutPixelErase(x, y: integer; Color: TColor32); override;
procedure PaintHorizLine(x1, x2, y: integer; Color: TColor32); override;
procedure PaintHorizLineMasked(x1, x2, y: integer; Color: TColor32; Mask: TBitmap1); override;
procedure PaintVertLine(x, y1, y2: integer; Color: TColor32); override;
procedure PaintMask(x, y: integer; Mask: TBitmap1; Color: TColor32); override;
procedure Invert;
procedure Combine(bm: TBitmap1; Mode: TBitsCombineMode);
// Returns bounding box
function CreateBrushMask(Count: integer; pt: PPointArray;
BrushSize: integer; BrushShape: TBrushShape): TRect;
// Creates the mask for a filled polygon
procedure CreatePolygonMask(Count: integer; pt: PPointArray);
property Data: Pointer read FData;
property Bits[x, y: integer]: boolean read GetBit write SetBit; default;
end;
// This descendant is a fully-implemented 32-bit bitmap
TBitmap32 = class(TBitmapEx)
private
FData: Pointer;
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure ToBitmap(bm: TBitmap; BackColor: TColor);
procedure Resize(NewWidth, NewHeight: integer); override;
procedure FillTransparent; override;
procedure FillColor(Color: TColor32); override;
function IsOpaque: boolean;
function FastGetPixel(x, y: integer): TColor32; inline;
procedure FastSetPixel(x, y: integer; Color: TColor32); inline;
function GetPixel(x, y: integer): TColor32; override;
procedure SetPixel(x, y: integer; Color: TColor32); override;
procedure PutPixel(x, y: integer; Color: TColor32); override;
procedure PutPixelErase(x, y: integer; Color: TColor32); override;
procedure PaintHorizLine(x1, x2, y: integer; Color: TColor32); override;
procedure PaintHorizLineMasked(x1, x2, y: integer; Color: TColor32; Mask: TBitmap1); override;
procedure PaintVertLine(x, y1, y2: integer; Color: TColor32); override;
function ScanLine(y: integer): PColor32Array; inline;
function PixelAddr(x, y: integer): PColor32; inline;
procedure FillBehind(c: TColor32);
procedure Colorize(c: TColor); // preserves opacity
// Moves the contents of the rSrc region in Src
// to position (x, y)
procedure CopyRectImpl(x, y: integer; bm: TBitmapEx; const rSrc: TRect); override;
procedure CopyRectOverwriteImpl(x, y: integer; Src: TBitmapEx; const rSrc: TRect); override;
procedure Gradient(c1, c2: TColor32; p1, p2: TPoint; rClip: TRect;
Kind: TGradientKind; Repetition: TGradientRep);
// These floodfill functions return the bounding box of the drawn blob
function CreateFloodFillMask(Mask: TBitmap1;
x, y, Tolerance: integer; Contiguous: boolean): TRect;
// IsTransparent(Color) -> erases
// Mask can be nil
function FloodFill(x, y, Tolerance: integer; Contiguous: boolean;
Color: TColor32; Mask: TBitmap1): TRect;
procedure DrawTo24(bm: TBitmap; xPos, yPos: integer);
procedure DrawZoomedHatchedTo24(bm: TBitmap; xPos, yPos, xHatchOrigin, yHatchOrigin, Zoom: integer;
Hatch: THatchDesc);
// Draws only to pixels whose y is in the interval [yMin, yMax)
// Shrink can be at most 100.
procedure DrawShrunkHatchedTo24(bm: TBitmap; xPos, yPos, xHatchOrigin, yHatchOrigin, Shrink: integer;
Hatch: THatchDesc; Draft: boolean);
procedure DrawToCanvas(c: TCanvas; x, y: integer; const Hatch: THatchDesc);
property Data: Pointer read FData;
end;
TBitmapEx_Impl = TBitmap32; // for PascalScript import
implementation
uses
ImageTransform;
procedure CreateRoundedBrushRanges(BrushSize: integer; var xRanges: TArrayOfIntegerRange);
var
y, Sqr_R: integer;
q: double;
begin
SetLength(xRanges, BrushSize);
// this way the brush tip looks nicer, size = 3 is a special case
if BrushSize = 3 then
Sqr_R := Sqr(2)
else
Sqr_R := Sqr(BrushSize);
for y := 0 to BrushSize - 1 do
begin
q := Sqrt(Sqr_R - Sqr(2 * y - BrushSize + 1));
xRanges[y].Max := Floor((BrushSize - 1 + q) * 0.5);
xRanges[y].Min := Ceil((BrushSize - 1 - q) * 0.5);
end;
end;
// TBitmapEx
constructor TBitmapEx.Create;
begin
inherited;
FWidth := 0;
FHeight := 0;
end;
destructor TBitmapEx.Destroy;
begin
if FFont <> nil then
FFont.Free;
inherited;
end;
function TBitmapEx.GetFont: TFont;
begin
if FFont = nil then
FFont := TFont.Create;
Result := FFont;
end;
procedure TBitmapEx.Assign;
var
x, y: integer;
c: TColor32;
p: PByteArray;
bx: TBitmapEx;
riSrc: TRasterImage;
sl: TScanLines;
begin
if Source is TBitmapEx then
begin
bx := Source as TBitmapEx;
Resize(bx.Width, bx.Height);
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
SetPixel(x, y, bx.GetPixel(x, y));
end
else
if Source is TRasterImage then
begin
riSrc := Source as TRasterImage;
Resize(riSrc.Width, riSrc.Height);
// This is some workaround
// direct pixel access in Lazarus is a bit buggy...
if riSrc.PixelFormat = pf32bit then
begin
sl := TScanLines.Create(riSrc);
try
for y := 0 to Height - 1 do
begin
p := sl[y];
for x := 0 to Width - 1 do
begin
c := FlipColor32(PColor32(p)^);
// prevent inverted color
if c and cl32Opaque = 0 then
c := cl32Transparent;
SetPixel(x, y, c);
Inc(PColor32(p));
end;
end;
finally
sl.Free;
end;
end else // no alpha channel
if riSrc.PixelFormat = pf24bit then
begin
c := cl32Opaque;
sl := TScanLines.Create(riSrc);
try
for y := 0 to Height - 1 do
begin
p := sl[y];
for x := 0 to Width - 1 do
begin
Move3(p, @c);
SetPixel(x, y, FlipColor32(c));
Inc(PByte(p), sl.BytesPerPixel);
end;
end;
finally
sl.Free;
end;
end else // other pixel formats
begin
for y := 0 to Height - 1 do
for x := 0 to Width - 1 do
SetPixel(x, y, TColor32(riSrc.Canvas.Pixels[x, y]) or cl32Opaque);
end;
end
else
inherited;
end;
procedure TBitmapEx.AssignTo;
var
bm: TBitmap;
pic: TPicture;
sl: TScanLines;
x, y: integer;
p: PColor32;
begin
if Dest is TBitmap then
begin
bm := Dest as TBitmap;
bm.PixelFormat := pf32bit;
bm.Width := Width;
bm.Height := Height;
sl := TScanLines.Create(bm);
try
for y := 0 to Height - 1 do
begin
p := PColor32(sl[y]);
for x := 0 to Width - 1 do
begin
PColor32(p)^ := FlipColor32(GetPixel(x, y));
Inc(PByte(p), sl.BytesPerPixel);
end;
end;
finally
sl.Free;
end;
end
else if Dest is TPicture then
begin
pic := Dest as TPicture;
bm := TBitmap.Create;
try
AssignTo(bm);
pic.Assign(bm);
finally
bm.Free;
end;
end else
inherited;
end;
function TBitmapEx.Hash;
var
i, x, y: integer;
c: TColor32;
begin
Result := fnvNull;
i := Width;
Result := fnvAppend(Result, @i, SizeOf(i));
i := Height;
Result := fnvAppend(Result, @i, SizeOf(i));
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
begin
c := GetPixel(x, y);
if OnlyAlpha then
Result := fnvAppend(Result, PByte(@c) + 3, 1) else
Result := fnvAppend(Result, @c, SizeOf(c));
end;
end;
function TBitmapEx.ClientRect;
begin
Result := Rect(0, 0, Width, Height);
end;
function TBitmapEx.SafeGetPixel;
begin
if (x >= 0) and (x < Width) and (y >= 0) and (y < Height) then
Result := GetPixel(x, y)
else
Result := cl32Transparent;
end;
procedure TBitmapEx.SafeSetPixel;
begin
if (x >= 0) and (x < Width) and (y >= 0) and (y < Height) then
SetPixel(x, y, Color);
end;
procedure TBitmapEx.PaintMask;
var
i, j: integer;
begin
for i := Max(0, x) to Min(Width, x + Mask.Width) - 1 do
for j := Max(0, y) to Min(Height, y + Mask.Height) - 1 do
if Mask[i - x, j - y] then
PutPixelErase(i, j, Color);
end;
function TBitmapEx.HasTransparency;
var
x, y: integer;
begin
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
if GetPixel(x, y) shr 24 <> $ff then
begin
Result := True;
Exit;
end;
Result := False;
end;
function TBitmapEx.HasColor;
var
x, y: integer;
begin
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
if GetPixel(x, y) = Color then
begin
Result := True;
Exit;
end;
Result := False;
end;
function TBitmapEx.IsBlackAndWhite;
var
x, y: integer;
c: TColor32;
begin
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
begin
c := GetPixel(x, y);
if (c <> cl32Black) and (c <> cl32White) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
procedure TBitmapEx.ThresholdAlpha;
var
x, y: integer;
c: TColor32;
begin
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
begin
c := GetPixel(x, y);
if (c shr 24) and $80 <> 0 then
SetPixel(x, y, c or cl32Opaque)
else
if c <> cl32Inverted then
SetPixel(x, y, cl32Transparent);
end;
end;
function TBitmapEx.GetAutoCropRect;
var
x, y: integer;
label
Break_yMin, Break_yMax, Break_xMin, Break_xMax;
begin
Result := Rect(Width, Height, 0, 0);
for y := 0 to Height - 1 do
for x := 0 to Width - 1 do
if not IsTransparent(GetPixel(x, y)) then
begin
Result.Top := y;
goto Break_yMin;
end;
Break_yMin:
// is the whole image transparent?
if Result.Top = Height then
Exit;
for y := Height - 1 downto 0 do
for x := 0 to Width - 1 do
if not IsTransparent(GetPixel(x, y)) then
begin
Result.Bottom := y + 1;
goto Break_yMax;
end;
Break_yMax:
for x := 0 to Width - 1 do
for y := 0 to Height - 1 do
if not IsTransparent(GetPixel(x, y)) then
begin
Result.Left := x;
goto Break_xMin;
end;
Break_xMin:
for x := Width - 1 downto 0 do
for y := 0 to Height - 1 do
if not IsTransparent(GetPixel(x, y)) then
begin
Result.Right := x + 1;
goto Break_xMax;
end;
Break_xMax: ;
end;
procedure TBitmapEx.Crop;
var
bm: TBitmapEx;
begin
if (r.Width <= 0) or (r.Height <= 0) then
Exit;
bm := (ClassType.Create) as TBitmapEx;
try
bm.Resize(r.Width, r.Height);
bm.Draw(-r.Left, -r.Top, Self);
Assign(bm);
finally
bm.Free;
end;
end;
procedure TBitmapEx.Draw;
var
x, y, xMin, xMax: integer;
begin
xMin := Max(0, xPos);
xMax := Min(Width, xPos + bm.Width) - 1;
for y := Max(0, yPos) to Min(Height, yPos + bm.Height) - 1 do
for x := xMin to xMax do
PutPixel(x, y, bm.GetPixel(x - xPos, y - yPos));
end;
procedure TBitmapEx.TransformDraw;
var
bmTemp: TBitmap32;
NoRotation, NoStretch: boolean;
begin
if (bm.Width = 0) or (bm.Height = 0) then Exit;
NoRotation := IsZero(Angle, 1e-5);
NoStretch := (r.Width = bm.Width) and (r.Height = bm.Height);
if NoRotation then
StretchDraw(bm, Self, r, Antialias) else
begin // rotate
if NoStretch then
RotateDraw(bm, Self, r.xCenter, r.yCenter, Angle, Antialias) else
begin
// stretch and rotate
bmTemp := TBitmap32.Create;
try
bmTemp.Resize(r.Width, r.Height);
StretchDraw(bm, bmTemp, bmTemp.ClientRect, Antialias);
RotateDraw(bmTemp, Self, r.xCenter, r.yCenter, Angle, Antialias)
finally
bmTemp.Free;
end;
end; // if stretch
end; // if rotate
end;
procedure TBitmapEx.CopyRect;
var
r: TRect;
begin
if bm = Self then Exit;
// calculate a new rSrc
r.Left := Max(0, rSrc.Left);
r.Right := Min3i(rSrc.Left + Width - x, bm.Width, rSrc.Right);
if r.Right <= r.Left then Exit;
r.Top := Max(0, rSrc.Top);
r.Bottom := Min3i(rSrc.Top + Height - y, bm.Height, rSrc.Bottom);
if r.Bottom <= r.Top then Exit;
// calculate new x and y values
inc(x, r.Left - rSrc.Left);
inc(y, r.Top - rSrc.Top);
CopyRectImpl(x, y, bm, r);
end;
procedure TBitmapEx.CopyRectImpl;
var
i, j: integer;
begin
for i := rSrc.Left to rSrc.Right - 1 do
for j := rSrc.Top to rSrc.Bottom - 1 do
PutPixel(x + i - rSrc.Left, y + j - rSrc.Top, bm.GetPixel(i, j));
end;
procedure TBitmapEx.CopyRectOverwrite;
var
r: TRect;
begin
if Src = Self then Exit;
r.Left := Max(0, rSrc.Left);
r.Right := Min3i(rSrc.Left + Width - x, Src.Width, rSrc.Right);
if r.Width <= 0 then Exit;
r.Top := Max(0, rSrc.Top);
r.Bottom := Min3i(rSrc.Top + Height - y, Src.Height, rSrc.Bottom);
if r.Height <= 0 then Exit;
x += r.Left - rSrc.Left;
y += r.Top - rSrc.Top;
CopyRectOverwriteImpl(x, y, Src, r);
end;
procedure TBitmapEx.CopyRectOverwriteImpl;
var
x2, y2: integer;
begin
for y2 := rSrc.Top to rSrc.Bottom - 1 do
for x2 := rSrc.Left to rSrc.Right - 1 do
SetPixel(x2 + x - rSrc.Left, y2 + y - rSrc.Top, Src.GetPixel(x2, y2));
end;
procedure TBitmapEx.CreateThumbnail;
begin
if (Src.Width <= MaxSize) and (Src.Height <= MaxSize) then
Assign(Src)
else
begin
if Src.Width > Src.Height then
Resize(MaxSize, MaxSize * Src.Height div Src.Width)
else
Resize(MaxSize * Src.Width div Src.Height, MaxSize);
FillTransparent;
TransformDraw(ClientRect, 0, Src, Antialias);
end;
end;
function TBitmapEx.StrokeBrush;
var
BrushMask: TBitmap1;
x, y: integer;
begin
BrushMask := TBitmap1.Create;
try
// render the mask
BrushMask.Resize(Width, Height);
Result := BrushMask.CreateBrushMask(Count, pt, BrushSize, BrushShape);
if Assigned(Mask) then
BrushMask.Combine(Mask, bcmAnd);
// paint/recolor/... the area
for x := Result.Left to Result.Right - 1 do
for y := Result.Top to Result.Bottom - 1 do
if BrushMask[x, y] then
PutPixelErase(x, y, Color);
finally
BrushMask.Free;
end;
end;
function TBitmapEx.Rectangle;
var
rInner: TRect;
procedure DoFill(x1, y1, x2, y2: integer);
var
y: integer;
begin
for y := Max(0, y1) to Min(Height - 1, y2) do
PaintHorizLine(x1, x2, y, Color);
end;
begin
// change orientation to default
r := DefaultRectOrientation(r);
// too small rectangles act as if they were filled
if Filled or (r.Width + 1 <= 2 * LineWidth) or
(r.Height + 1 <= 2 * LineWidth) then
begin
DoFill(r.Left, r.Top, r.Right, r.Bottom);
end
else
begin
rInner := r.Inflate(-LineWidth, -LineWidth);
DoFill(r.Left, r.Top, r.Right, rInner.Top - 1);
DoFill(r.Left, rInner.Bottom + 1, r.Right, r.Bottom);
DoFill(r.Left, rInner.Top, rInner.Left - 1, rInner.Bottom);
DoFill(rInner.Right + 1, rInner.Top, r.Right, rInner.Bottom);
end;
Result := Rect(Max(0, r.Left), Max(0, r.Top), Min(Width, r.Right + 1),
Min(Height, r.Bottom + 1));
end;
function TBitmapEx.Ellipse;
var
a, b, x, y, x2, y2, xMin, xMax, xMin2, xMax2, xMinPrev, Count: integer;
Points: array of TPoint;
xMid, yMid, q: double;
begin
// change orientation to default
r := DefaultRectOrientation(r);
if not Filled then
begin
Count := 0;
SetLength(Points, (r.Width + r.Height + 2) * 2);
end;
// calculate sizes of two axes (a = Sqr(2*a))
a := Sqr(r.Width + 1);
b := Sqr(r.Height + 1);
// special case: looks nicer this way
if (a = 9) and (b = 9) then
begin
a := 4;
b := 4;
end;
// cache
xMid := (r.Left + r.Right) * 0.5;
yMid := (r.Top + r.Bottom) * 0.5;
xMinPrev := 0;
for y := r.Top to (r.Top + r.Bottom) div 2 do
begin
// determine two endpoints of intersection line
if b = 0 then
q := 0
else
q := Sqrt(a * (b - 4 * Sqr(y - yMid)) / (4 * b));
xMin := Ceil(xMid - q);
xMax := Floor(xMid + q);
y2 := r.Top + r.Bottom - y;
if Filled then
begin
xMin := Max(0, xMin);
xMax := Min(Width - 1, xMax);
// draw two segments
if (y >= 0) and (y < Height) then
PaintHorizLine(xMin, xMax, y, Color);
// y = y2 -> the segment would be drawn twice
if (y <> y2) and (y2 >= 0) and (y2 < Height) then
PaintHorizLine(xMin, xMax, y2, Color);
end
else
begin
if y = r.Top then
begin
xMin2 := xMin;
xMax2 := (r.Left + r.Right) div 2;
end
else
begin
xMin2 := xMin;
xMax2 := xMinPrev;
if xMinPrev > xMin then
Dec(xMax2);
end;
for x := xMin2 to xMax2 do
begin
x2 := r.Left + r.Right - x;
Inc(Count);
Points[Count - 1] := Point(x, y);
if x2 <> x then
begin
Inc(Count);
Points[Count - 1] := Point(x2, y);
end;
if y2 <> y then
begin
Inc(Count);
Points[Count - 1] := Point(x, y2);
if x2 <> x then
begin
Inc(Count);
Points[Count - 1] := Point(x2, y2);
end;
end;
end;
xMinPrev := xMin;
end; // if Filled
end; // for y
// paint the outline
if Filled then
Result := Rect(Max(0, r.Left), Max(0, r.Top), Min(Width, r.Right + 1),
Min(Height, r.Bottom + 1))
else
Result := StrokeBrush(Count, @Points[0], LineWidth, bshRounded, Color, nil);
end;
function TBitmapEx.Line(x1, y1, x2, y2: integer; BrushSize: integer;
BrushShape: TBrushShape; Color: TColor32; Mask: TBitmap1): TRect;
var
i, mid, x, y, yIndex, xLeft, yTop: integer;
Points: TDynPointArray;
brushXRanges: TArrayOfIntegerRange;
xSlices: TArrayOfIntegerRange; // x slices for each y (offset is yMin)
begin
if BrushSize <= 0 then
Exit(Rect(0, 0, 0, 0));
EnumLinePoints(x1, y1, x2, y2, Points);
if (BrushShape = bshSpray) then
Result := StrokeBrush(Length(Points), @Points[0], BrushSize, BrushShape, Color, Mask)
else
begin
mid := (BrushSize - 1) div 2;
Result := Rect(Max(0, Min(x1, x2) - mid), Max(0, Min(y1, y2) - mid),
Min(Width, Max(x1, x2) - mid + BrushSize), Min(Height, Max(y1, y2) - mid + BrushSize));
if (Result.Width <= 0) or (Result.Height <= 0) then
Exit;
if BrushSize = 1 then
begin
for i := 0 to Length(Points) - 1 do
if PtInRect(Result, Points[i]) then
begin
x := Points[i].X;
y := Points[i].Y;
if not Assigned(Mask) or Mask.Bits[x, y] then
PutPixelErase(x, y, Color);
end;
end else
begin
SetLength(xSlices, Result.Height);
for i := 0 to Length(xSlices) - 1 do
xSlices[i].Clear;
// create a brush tip image if needed
if BrushShape = bshRounded then
CreateRoundedBrushRanges(BrushSize, brushXRanges);
for i := 0 to Length(Points) - 1 do
begin
xLeft := Points[i].X - mid;
yTop := Points[i].Y - mid;
case BrushShape of
bshRounded: for y := 0 to BrushSize-1 do
begin
yIndex := yTop+y - Result.Top;
if (yIndex >= 0) and (yIndex < Length(xSlices)) then
xSlices[yIndex].IncludeRange(brushXRanges[y].Min + xLeft, brushXRanges[y].Max + xLeft);
end;
bshSquare: for y := 0 to BrushSize-1 do
begin
yIndex := yTop+y - Result.Top;
if (yIndex >= 0) and (yIndex < Length(xSlices)) then
xSlices[yIndex].IncludeRange(xLeft, xLeft+BrushSize-1);
end;
bshSlash: for y := 0 to BrushSize-1 do
begin
yIndex := yTop+y - Result.Top;
if (yIndex >= 0) and (yIndex < Length(xSlices)) then
xSlices[yIndex].Include(xLeft + BrushSize-1 - y);
end;
bshBackslash: for y := 0 to BrushSize-1 do
begin
yIndex := yTop+y - Result.Top;
if (yIndex >= 0) and (yIndex < Length(xSlices)) then
xSlices[yIndex].Include(xLeft + y);
end;
bshHoriz:
begin
yIndex := Points[i].Y - Result.Top;
if (yIndex >= 0) and (yIndex < Length(xSlices)) then
xSlices[yIndex].IncludeRange(xLeft, xLeft+BrushSize-1);
end;
bshVert: for y := 0 to BrushSize-1 do
begin
yIndex := yTop+y - Result.Top;
if (yIndex >= 0) and (yIndex < Length(xSlices)) then
xSlices[yIndex].Include(Points[i].X);
end;
end; // case shape
end; // for i (points)
for i := 0 to Length(xSlices) - 1 do
begin
if not xSlices[i].Empty then
begin
y := Result.Top + i;
PaintHorizLineMasked(xSlices[i].Min, xSlices[i].Max, y, Color, Mask);
end;
end;
end; // if brush greater than 1
end; // if not spray
end;
procedure TBitmapEx.Polygon;
var
i, i2: integer;
Mask: TBitmap1;
begin
Mask := TBitmap1.Create;
try
Mask.Resize(Width, Height);
if Filled then
Mask.CreatePolygonMask(Count, pt)
else
for i := 0 to Count - 1 do
begin
i2 := (i + 1) mod Count;
Mask.Line(pt[i].X, pt[i].Y, pt[i2].X, pt[i2].Y, LineWidth, bshRounded,
cl32White, nil);
end;
PaintMask(0, 0, Mask, Color);
finally
Mask.Free;
end;
end;
function TBitmapEx.TextExtent;
var