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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
{
/***************************************************************************
RGBGraphics.pas
***************************************************************************/
*****************************************************************************
* *
* See the file COPYING.modifiedLGPL, 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. *
* *
*****************************************************************************
Author: Tom Gregorovic (_tom_@centrum.cz)
Abstract:
TRGB32Bitmap is a memory image which allows fast pixel manipulations.
TRGB32Canvas is a TRGB32Bitmap canvas for drawing primitives and
drawing bitmap image into TCanvas.
}
unit RGBGraphics;
{$ifdef fpc}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils, LCLIntf, FPWriteBMP,
LCLType, LCLProc, FPImage, LResources, IntfGraphics,
GraphType, Graphics, Forms, Math, Clipbrd,
RGBTypes, RGBRoutines, RGBUtils;
type
TEraseMode = (ermNone, ermErase, ermReplace);
TDrawMode = (dmFillAndOutline, dmOutline, dmFill);
TRandomDensity = Word;
const
MAXRANDOMDENSITY = $FFFF;
type
TMaskFillMode = (mfAdd, mfRemove, mfXOR);
{ TRGBMask }
TRGBMask = class(TRGB8BitmapCore)
private
FBGPen: TPen;
FFGPen: TPen;
FFillMode: TMaskFillMode;
FMaskedPixels: Integer;
protected
procedure AddPixel(X, Y: Integer);
procedure RemovePixel(X, Y: Integer);
procedure XORPixel(X, Y: Integer);
procedure CreatePens; virtual;
function GetFillProcedure: TDrawPixelProcedure; virtual;
function GetMaskedPixelsCount: Integer;
public
constructor Create(AWidth, AHeight: Integer); override;
constructor CreateAsCopy(ABitmap: TRGBBitmapCore); override;
destructor Destroy; override;
procedure LoadFromLazIntfImageAlpha(AImage: TLazIntfImage); override;
procedure SwapWith(ABitmap: TRGBBitmapCore); override;
procedure Assign(Source: TPersistent); override;
procedure UpdateMaskedPixels;
procedure Draw(X, Y: Integer; AMask: TRGBMask);
procedure DrawShapeTo(ACanvas: TCanvas; X, Y: Integer);
procedure StretchDrawShapeTo(ACanvas: TCanvas; DstX, DstY, DstWidth, DstHeight: Integer);
procedure StretchDrawShapePortionTo(ACanvas: TCanvas; DstX, DstY, DstWidth, DstHeight: Integer;
DX, DY, DW, DH: Integer);
procedure DrawTo(ACanvas: TCanvas; X, Y: Integer);
procedure StretchTrunc(AWidth, AHeight: Integer); virtual;
procedure Rectangle(X1, Y1, X2, Y2: Integer);
procedure Ellipse(X1, Y1, X2, Y2: Integer);
procedure Clear; override;
procedure ClearWhite; override;
procedure Invert; override;
public
function IsEmpty: Boolean;
function GetMaskedRect: TRect;
property BackgroundPen: TPen read FBGPen;
property ForegroundPen: TPen read FFGPen;
property FillMode: TMaskFillMode read FFillMode write FFillMode;
end;
{ TRGB32Canvas }
TRGB32Canvas = class
private
FDrawMode: TDrawMode;
FEraseMode: TEraseMode;
FFillColor: TRGB32Pixel;
FFloodFillTolerance: TPixelDifference;
FOutlineColor: TRGB32Pixel;
FOwner: TRGB32BitmapCore;
FPaperColor: TRGB32Pixel;
FRandomDensity: TRandomDensity;
FRandomEnabled: Boolean;
FRectangleRoundness: Integer;
function GetFillColor: TColor;
function GetOutlineColor: TColor;
function GetPaperColor: TColor;
procedure SetFillColor(const AValue: TColor);
procedure SetOutlineColor(const AValue: TColor);
procedure SetPaperColor(const AValue: TColor);
protected
function PixelMasked(X, Y: Integer): Boolean;
function SamePixelUnsafe(X, Y: Integer; Value: TRGB32Pixel): Boolean;
function SamePixelUnmasked(X, Y: Integer; Value: TRGB32Pixel): Boolean;
procedure DrawOutlinePixel(X, Y: Integer);
procedure DrawFillPixel(X, Y: Integer);
procedure DrawPaperPixel(X, Y: Integer);
procedure DrawReplacePixel(X, Y: Integer);
procedure DrawRandomOutlinePixel(X, Y: Integer);
procedure DrawRandomFillPixel(X, Y: Integer);
procedure DrawRandomPaperPixel(X, Y: Integer);
procedure DrawEmptyPixel(X, Y: Integer);
function GetOutlineProcedure: TDrawPixelProcedure; virtual;
function GetFillProcedure: TDrawPixelProcedure; virtual;
public
constructor Create(AOwner: TRGB32BitmapCore);
procedure SetColor(X, Y: Integer; Value: TColor);
function GetColor(X, Y: Integer): TColor;
procedure Fill(Color: TColor);
procedure FillRect(X1, Y1, X2, Y2: Integer);
procedure FillEllipse(X1, Y1, X2, Y2: Integer);
procedure Line(X1, Y1, X2, Y2: Integer);
procedure Rectangle(X1, Y1, X2, Y2: Integer);
procedure Ellipse(X1, Y1, X2, Y2: Integer);
procedure FloodFill(X, Y: Integer);
procedure MaskFloodFill(X, Y: Integer);
public
procedure DrawTo(ACanvas: TCanvas; X, Y: Integer);
procedure StretchDrawTo(ACanvas: TCanvas; DstX, DstY, DstWidth, DstHeight: Integer);
public
property EraseMode: TEraseMode read FEraseMode write FEraseMode;
property DrawMode: TDrawMode read FDrawMode write FDrawMode;
property FloodFillTolerance: TPixelDifference read FFloodFillTolerance
write FFloodFillTolerance;
property FillColor: TColor read GetFillColor write SetFillColor;
property OutlineColor: TColor read GetOutlineColor write SetOutlineColor;
property PaperColor: TColor read GetPaperColor write SetPaperColor;
property RandomEnabled: Boolean read FRandomEnabled write FRandomEnabled;
property RandomDensity: TRandomDensity read FRandomDensity write FRandomDensity;
property RectangleRoundness: Integer read FRectangleRoundness write FRectangleRoundness;
end;
TSmoothMethod = (smAreaPixel, smBilinear, smBicubic);
{ TRGB32Bitmap }
TRGB32Bitmap = class(TRGB32BitmapCore)
private
FCanvas: TRGB32Canvas;
FMask: TRGBMask;
protected
function CreateDefaultLazIntfImage: TLazIntfImage;
public
constructor Create(AWidth, AHeight: Integer); override;
constructor CreateAsCopy(ABitmap: TRGBBitmapCore); override;
constructor CreateFromLazIntfImage(AImage: TLazIntfImage); override;
constructor CreateFromFile(const FileName: String); virtual;
constructor CreateFromBitmap(ABitmap: TRasterImage); virtual;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure SwapWith(ABitmap: TRGBBitmapCore); override;
procedure SaveToLazIntfImage(AImage: TLazIntfImage; const ARect: TRect); override;
procedure SaveToStream(Stream: TStream); virtual;
procedure SaveToStream(Stream: TStream; AWriterClass: TFPCustomImageWriterClass); virtual;
procedure SaveToStream(Stream: TStream; const ARect: TRect;
AWriterClass: TFPCustomImageWriterClass); virtual;
procedure SaveToFile(const FileName: String); virtual;
procedure SaveToLazarusResource(const FileName, Name: String); virtual;
public
procedure Draw(X, Y: Integer; ABitmap: TRGB32Bitmap);
procedure StretchTrunc(AWidth, AHeight: Integer); virtual;
procedure StretchSmooth(AWidth, AHeight: Integer; Method: TSmoothMethod); virtual;
procedure Grayscale; virtual;
procedure Disable; virtual;
procedure CutToClipboard; virtual;
procedure CopyToClipboard; virtual;
procedure Delete; virtual;
procedure FlipHorz; override;
procedure FlipVert; override;
procedure Rotate90; override;
procedure Rotate180; override;
procedure Rotate270; override;
public
property Canvas: TRGB32Canvas read FCanvas;
property Mask: TRGBMask read FMask write FMask;
end;
implementation
function AbsByte(Src: Integer): Byte; inline;
begin
if Src >= 0 then Result := Src
else Result := -Src;
end;
function RGB32PixelDifference(A, B: TRGB32Pixel): TPixelDifference; inline;
begin
Result := AbsByte(((A shr 16) and $FF) - ((B shr 16) and $FF))
+ AbsByte(((A shr 8) and $FF) - ((B shr 8) and $FF))
+ AbsByte((A and $FF) - (B and $FF));
end;
{ TRGB32Bitmap }
function TRGB32Bitmap.CreateDefaultLazIntfImage: TLazIntfImage;
var
RID: TRawImageDescription;
DC: HDC;
begin
DC := GetDC(0);
try
RawImage_DescriptionFromDevice(DC, RID);
finally
ReleaseDC(0, DC);
end;
Result := TLazIntfImage.Create(0, 0);
Result.DataDescription := RID;
end;
constructor TRGB32Bitmap.Create(AWidth, AHeight: Integer);
begin
inherited;
FCanvas := TRGB32Canvas.Create(Self);
FMask := TRGBMask.Create(AWidth, AHeight);
end;
constructor TRGB32Bitmap.CreateAsCopy(ABitmap: TRGBBitmapCore);
begin
inherited;
FCanvas := TRGB32Canvas.Create(Self);
if ABitmap is TRGB32Bitmap then
FMask := TRGBMask.CreateAsCopy((ABitmap as TRGB32Bitmap).Mask)
else
FMask := TRGBMask.Create(ABitmap.Width, ABitmap.Height);
end;
constructor TRGB32Bitmap.CreateFromLazIntfImage(AImage: TLazIntfImage);
begin
inherited CreateFromLazIntfImage(AImage);
FMask.LoadFromLazIntfImageAlpha(AImage);
end;
constructor TRGB32Bitmap.CreateFromFile(const FileName: String);
var
Image: TLazIntfImage;
begin
Image := CreateDefaultLazIntfImage;
try
Image.LoadFromFile(FileName);
CreateFromLazIntfImage(Image);
finally
Image.Free;
end;
end;
constructor TRGB32Bitmap.CreateFromBitmap(ABitmap: TRasterImage);
var
Image: TLazIntfImage;
begin
Image := ABitmap.CreateIntfImage;
try
CreateFromLazIntfImage(Image);
finally
Image.Free;
end;
end;
destructor TRGB32Bitmap.Destroy;
begin
FCanvas.Free;
FMask.Free;
inherited;
end;
procedure TRGB32Bitmap.Assign(Source: TPersistent);
begin
inherited Assign(Source);
if Source is TRGB32Bitmap then
begin
Mask.Assign((Source as TRGB32Bitmap).Mask);
end;
end;
procedure TRGB32Bitmap.SwapWith(ABitmap: TRGBBitmapCore);
begin
inherited SwapWith(ABitmap);
if ABitmap is TRGB32Bitmap then
begin
Mask.SwapWith((ABitmap as TRGB32Bitmap).Mask);
end;
end;
procedure TRGB32Bitmap.SaveToLazIntfImage(AImage: TLazIntfImage; const ARect: TRect);
begin
inherited SaveToLazIntfImage(AImage, ARect);
if not Mask.IsEmpty then FMask.SaveToLazIntfImageAlpha(AImage, ARect);
end;
procedure TRGB32Bitmap.SaveToStream(Stream: TStream);
begin
SaveToStream(Stream, Bounds(0, 0, Width, Height), TLazWriterXPM);
end;
procedure TRGB32Bitmap.SaveToStream(Stream: TStream;
AWriterClass: TFPCustomImageWriterClass);
begin
SaveToStream(Stream, Bounds(0, 0, Width, Height), AWriterClass);
end;
procedure TRGB32Bitmap.SaveToStream(Stream: TStream; const ARect: TRect;
AWriterClass: TFPCustomImageWriterClass);
var
Image: TLazIntfImage;
Writer: TFPCustomImageWriter;
begin
Image := CreateDefaultLazIntfImage;
Writer := AWriterClass.Create;
try
SaveToLazIntfImage(Image, ARect);
Image.SaveToStream(Stream, Writer);
finally
Writer.Free;
Image.Free;
end;
end;
procedure TRGB32Bitmap.SaveToFile(const FileName: String);
var
Image: TLazIntfImage;
begin
Image := CreateDefaultLazIntfImage;
try
inherited SaveToLazIntfImage(Image);
Image.SaveToFile(FileName);
finally
Image.Free;
end;
end;
procedure TRGB32Bitmap.SaveToLazarusResource(const FileName, Name: String);
var
PixmapStream, ResourceStream: TMemoryStream;
FileStream: TFileStream;
begin
PixmapStream := TMemoryStream.Create;
ResourceStream := TMemoryStream.Create;
try
SaveToStream(PixmapStream);
PixmapStream.Position := 0;
BinaryToLazarusResourceCode(PixmapStream, ResourceStream, Name, 'XPM');
ResourceStream.Position := 0;
FileStream := TFileStream.Create(FileName, fmCreate);
try
FileStream.CopyFrom(ResourceStream, ResourceStream.Size);
finally
FileStream.Free;
end;
finally
PixmapStream.Free;
ResourceStream.Free;
end;
end;
procedure TRGB32Bitmap.Draw(X, Y: Integer; ABitmap: TRGB32Bitmap);
begin
DrawRGB32Bitmap(Self, X, Y, ABitmap);
end;
procedure TRGB32Bitmap.StretchTrunc(AWidth, AHeight: Integer);
var
Result: TRGB32Bitmap;
begin
if (AWidth = Width) and (AHeight = Height) then Exit;
Result := TRGB32Bitmap.Create(AWidth, AHeight);
try
StretchRGB32BitmapTrunc(Result, Self);
inherited SwapWith(Result);
Mask.StretchTrunc(AWidth, AHeight);
finally
FreeAndNil(Result);
end;
end;
procedure TRGB32Bitmap.StretchSmooth(AWidth, AHeight: Integer; Method: TSmoothMethod);
begin
//
end;
procedure TRGB32Bitmap.Grayscale;
begin
GrayscaleRGB32Bitmap(Self);
end;
procedure TRGB32Bitmap.Disable;
begin
DisableRGB32Bitmap(Self);
end;
procedure TRGB32Bitmap.CutToClipboard;
begin
CopyToClipboard;
Delete;
end;
procedure TRGB32Bitmap.CopyToClipboard;
var
PixmapStream, BitmapStream: TMemoryStream;
PixmapWriter, BitmapWriter: TFPCustomImageWriter;
Image: TLazIntfImage;
R: TRect;
begin
PixmapStream := TMemoryStream.Create;
BitmapStream := TMemoryStream.Create;
Image := CreateDefaultLazIntfImage;
PixmapWriter := TLazWriterXPM.Create;
BitmapWriter := TFPWriterBMP.Create;
try
R := Mask.GetMaskedRect;
SaveToLazIntfImage(Image, R);
Clipboard.Open;
try
Clipboard.Clear;
Image.SaveToStream(PixmapStream, PixmapWriter);
Clipboard.AddFormat(PredefinedClipboardFormat(pcfPixmap), PixmapStream);
Image.SaveToStream(BitmapStream, BitmapWriter);
Clipboard.AddFormat(PredefinedClipboardFormat(pcfBitmap), BitmapStream);
finally
Clipboard.Close;
end;
finally
PixmapStream.Free;
BitmapStream.Free;
Image.Free;
PixmapWriter.Free;
BitmapWriter.Free;
end;
end;
procedure TRGB32Bitmap.Delete;
begin
Canvas.Fill(Canvas.PaperColor);
end;
procedure TRGB32Bitmap.FlipHorz;
begin
inherited FlipHorz;
Mask.FlipHorz;
end;
procedure TRGB32Bitmap.FlipVert;
begin
inherited FlipVert;
Mask.FlipVert;
end;
procedure TRGB32Bitmap.Rotate90;
begin
inherited Rotate90;
Mask.Rotate90;
end;
procedure TRGB32Bitmap.Rotate180;
begin
inherited Rotate180;
Mask.Rotate180;
end;
procedure TRGB32Bitmap.Rotate270;
begin
inherited Rotate270;
Mask.Rotate270;
end;
{ TRGB32Canvas }
constructor TRGB32Canvas.Create(AOwner: TRGB32BitmapCore);
begin
inherited Create;
FOwner := AOwner;
FRandomDensity := MAXRANDOMDENSITY;
FFloodFillTolerance := 0;
FRectangleRoundness := 0;
end;
procedure TRGB32Canvas.SetColor(X, Y: Integer; Value: TColor);
begin
FOwner.Set32Pixel(X, Y, ColorToRGB32Pixel(Value));
end;
function TRGB32Canvas.GetColor(X, Y: Integer): TColor;
var
P: PRGB32Pixel;
begin
P := FOwner.Get32PixelPtr(X, Y);
if P <> nil then Result := RGB32PixelToColor(P^)
else Result := clNone;
end;
function TRGB32Canvas.GetFillColor: TColor;
begin
Result := RGB32PixelToColor(FFillColor);
end;
function TRGB32Canvas.GetOutlineColor: TColor;
begin
Result := RGB32PixelToColor(FOutlineColor);
end;
function TRGB32Canvas.GetPaperColor: TColor;
begin
Result := RGB32PixelToColor(FPaperColor);
end;
procedure TRGB32Canvas.SetFillColor(const AValue: TColor);
begin
FFillColor := ColorToRGB32Pixel(AValue);
end;
procedure TRGB32Canvas.SetOutlineColor(const AValue: TColor);
begin
FOutlineColor := ColorToRGB32Pixel(AValue);
end;
procedure TRGB32Canvas.SetPaperColor(const AValue: TColor);
begin
FPaperColor := ColorToRGB32Pixel(AValue);
end;
function TRGB32Canvas.PixelMasked(X, Y: Integer): Boolean;
var
P: PRGB8Pixel;
begin
if not (FOwner is TRGB32Bitmap) then Result := True
else
if (FOwner as TRGB32Bitmap).Mask.IsEmpty then Result := True
else
begin
P := (FOwner as TRGB32Bitmap).Mask.Get8PixelPtr(X, Y);
Result := (P <> nil) and (P^ = $FF);
end;
end;
function TRGB32Canvas.SamePixelUnsafe(X, Y: Integer; Value: TRGB32Pixel): Boolean;
begin
Result := PixelMasked(X, Y) and (RGB32PixelDifference(FOwner.Get32PixelUnsafe(X, Y), Value)
<= FFloodFillTolerance);
end;
function TRGB32Canvas.SamePixelUnmasked(X, Y: Integer; Value: TRGB32Pixel): Boolean;
begin
Result := RGB32PixelDifference(FOwner.Get32PixelUnsafe(X, Y), Value)
<= FFloodFillTolerance;
end;
procedure TRGB32Canvas.DrawOutlinePixel(X, Y: Integer);
begin
if PixelMasked(X, Y) then FOwner.Set32Pixel(X, Y, FOutlineColor);
end;
procedure TRGB32Canvas.DrawFillPixel(X, Y: Integer);
begin
if PixelMasked(X, Y) then FOwner.Set32Pixel(X, Y, FFillColor);
end;
procedure TRGB32Canvas.DrawPaperPixel(X, Y: Integer);
begin
if PixelMasked(X, Y) then FOwner.Set32Pixel(X, Y, FPaperColor);
end;
procedure TRGB32Canvas.DrawReplacePixel(X, Y: Integer);
var
P: PRGB32Pixel;
begin
if not PixelMasked(X, Y) then Exit;
P := FOwner.Get32PixelPtr(X, Y);
if (P <> nil) and (P^ = FFillColor) then P^ := FPaperColor;
end;
procedure TRGB32Canvas.DrawRandomOutlinePixel(X, Y: Integer);
begin
if PixelMasked(X, Y) and (Random(MAXRANDOMDENSITY) < FRandomDensity) then
FOwner.Set32Pixel(X, Y, FOutlineColor);
end;
procedure TRGB32Canvas.DrawRandomFillPixel(X, Y: Integer);
begin
if PixelMasked(X, Y) and (Random(MAXRANDOMDENSITY) < FRandomDensity) then
FOwner.Set32Pixel(X, Y, FFillColor);
end;
procedure TRGB32Canvas.DrawRandomPaperPixel(X, Y: Integer);
begin
if PixelMasked(X, Y) and (Random(MAXRANDOMDENSITY) < FRandomDensity) then
FOwner.Set32Pixel(X, Y, FPaperColor);
end;
procedure TRGB32Canvas.DrawEmptyPixel(X, Y: Integer);
begin
//
end;
function TRGB32Canvas.GetOutlineProcedure: TDrawPixelProcedure;
begin
if not FRandomEnabled then
begin
case DrawMode of
dmFillAndOutline, dmOutline:
begin
case EraseMode of
ermNone: Result := @DrawOutlinePixel;
ermErase: Result := @DrawPaperPixel;
ermReplace: Result := @DrawReplacePixel;
end;
end;
else
Result := @DrawEmptyPixel;
end;
end
else
begin
case EraseMode of
ermNone: Result := @DrawRandomFillPixel;
ermErase: Result := @DrawRandomPaperPixel;
ermReplace: Result := @DrawRandomFillPixel;
end;
end;
end;
function TRGB32Canvas.GetFillProcedure: TDrawPixelProcedure;
begin
if not FRandomEnabled then
begin
case DrawMode of
dmFillAndOutline, dmFill:
begin
case EraseMode of
ermNone: Result := @DrawFillPixel;
ermErase: Result := @DrawPaperPixel;
ermReplace: Result := @DrawReplacePixel;
end;
end;
else
Result := @DrawEmptyPixel;
end;
end
else
begin
case EraseMode of
ermNone: Result := @DrawRandomFillPixel;
ermErase: Result := @DrawRandomPaperPixel;
ermReplace: Result := @DrawRandomFillPixel;
end;
end;
end;
procedure TRGB32Canvas.Fill(Color: TColor);
var
P: PRGB32Pixel;
C: TRGB32Pixel;
PM: PRGB8Pixel;
I, J: Integer;
begin
C := ColorToRGB32Pixel(Color);
for J := 0 to Pred(FOwner.Height) do
begin
P := FOwner.Get32PixelPtr(0, J);
if (FOwner is TRGB32Bitmap) and not (FOwner as TRGB32Bitmap).Mask.IsEmpty then
begin
PM := (FOwner as TRGB32Bitmap).Mask.Get8PixelPtr(0, J);
for I := 0 to Pred(FOwner.Width) do
begin
if PM^ = $FF then P^ := C;
Inc(P);
Inc(PM);
end;
end
else FillDWord(P^, FOwner.Width, C);
end;
end;
procedure TRGB32Canvas.FillRect(X1, Y1, X2, Y2: Integer);
begin
FillPixelRect(X1, Y1, X2, Y2, GetFillProcedure);
end;
procedure TRGB32Canvas.FillEllipse(X1, Y1, X2, Y2: Integer);
begin
EllipticRectangle(X1, Y1, X2, Y2, 0, 0, GetFillProcedure, GetFillProcedure);
end;
procedure TRGB32Canvas.Line(X1, Y1, X2, Y2: Integer);
begin
LineBresenham(X1, Y1, X2, Y2, GetOutlineProcedure);
end;
procedure TRGB32Canvas.Rectangle(X1, Y1, X2, Y2: Integer);
var
R1, R2: Integer;
begin
R1 := Max(0, Succ(Abs(X2 - X1)) - RectangleRoundness);
R2 := Max(0, Succ(Abs(Y2 - Y1)) - RectangleRoundness);
EllipticRectangle(X1, Y1, X2, Y2, R1, R2, GetOutlineProcedure, GetFillProcedure);
end;
procedure TRGB32Canvas.Ellipse(X1, Y1, X2, Y2: Integer);
begin
EllipticRectangle(X1, Y1, X2, Y2, 0, 0, GetOutlineProcedure, GetFillProcedure);
end;
procedure TRGB32Canvas.FloodFill(X, Y: Integer);
begin
case EraseMode of
ermNone: FloodFillScanLine(X, Y, FOwner.Width, FOwner.Height, @FOwner.Get32PixelUnsafe,
@SamePixelUnsafe, @DrawFillPixel);
ermErase: FloodFillScanLine(X, Y, FOwner.Width, FOwner.Height, @FOwner.Get32PixelUnsafe,
@SamePixelUnsafe, @DrawPaperPixel);
end;
end;
procedure TRGB32Canvas.MaskFloodFill(X, Y: Integer);
begin
if not (FOwner is TRGB32Bitmap) then Exit;
FloodFillScanLine(X, Y, FOwner.Width, FOwner.Height, @FOwner.Get32PixelUnsafe,
@SamePixelUnmasked, (FOwner as TRGB32Bitmap).Mask.GetFillProcedure);;
end;
procedure TRGB32Canvas.DrawTo(ACanvas: TCanvas; X, Y: Integer);
begin
if ACanvas <> nil then
with FOwner do
DrawRGB32Bitmap(ACanvas.Handle, X, Y, 0, 0, Width, Height, FOwner);
end;
procedure TRGB32Canvas.StretchDrawTo(ACanvas: TCanvas; DstX, DstY, DstWidth,
DstHeight: Integer);
begin
if ACanvas <> nil then
with FOwner do
StretchDrawRGB32Bitmap(ACanvas.Handle, DstX, DstY, DstWidth, DstHeight,
0, 0, Width, Height, FOwner);
end;
{ TRGBMask }
procedure TRGBMask.AddPixel(X, Y: Integer);
var
P: PRGB8Pixel;
begin
P := Get8PixelPtr(X, Y);
if P <> nil then
begin
if P^ <> $FF then Inc(FMaskedPixels);
P^ := $FF;
end;
end;
procedure TRGBMask.RemovePixel(X, Y: Integer);
var
P: PRGB8Pixel;
begin
P := Get8PixelPtr(X, Y);
if P <> nil then
begin
if P^ = $FF then Dec(FMaskedPixels);
P^ := 0;
end;
end;
procedure TRGBMask.XORPixel(X, Y: Integer);
var
P: PRGB8Pixel;
begin
P := Get8PixelPtr(X, Y);
if P <> nil then
begin
if P^ = 0 then Inc(FMaskedPixels);
if P^ = $FF then Dec(FMaskedPixels);
P^ := $FF - P^;
end;
end;
procedure TRGBMask.CreatePens;
begin
FBGPen := TPen.Create;
FBGPen.Color := clYellow;
FFGPen := TPen.Create;
FFGPen.Color := clBlue;
//FFGPen.Style := psDot;
end;
function TRGBMask.GetFillProcedure: TDrawPixelProcedure;
begin
case FillMode of
mfAdd: Result := @AddPixel;
mfRemove: Result := @RemovePixel;
mfXOR: Result := @XORPixel;
end;
end;
function TRGBMask.GetMaskedPixelsCount: Integer;
var
I, J: Integer;
P: PRGB8Pixel;
begin
Result := 0;
for J := 0 to Pred(Height) do
begin
P := Get8PixelPtr(0, J);
for I := 0 to Pred(Width) do
begin
if P^ = $FF then Inc(Result);
Inc(P);
end;
end;
end;
constructor TRGBMask.Create(AWidth, AHeight: Integer);
begin
inherited Create(AWidth, AHeight);
Clear;
CreatePens;
end;
constructor TRGBMask.CreateAsCopy(ABitmap: TRGBBitmapCore);
begin
inherited CreateAsCopy(ABitmap);
UpdateMaskedPixels;
CreatePens;
end;
procedure TRGBMask.SwapWith(ABitmap: TRGBBitmapCore);
begin
inherited SwapWith(ABitmap);
UpdateMaskedPixels;
end;
procedure TRGBMask.Assign(Source: TPersistent);
begin
inherited Assign(Source);
UpdateMaskedPixels;
end;
procedure TRGBMask.UpdateMaskedPixels;
begin
FMaskedPixels := GetMaskedPixelsCount;
end;
procedure TRGBMask.Draw(X, Y: Integer; AMask: TRGBMask);
begin
DrawRGB8Bitmap(Self, X, Y, AMask);
UpdateMaskedPixels;
end;
destructor TRGBMask.Destroy;
begin
FBGPen.Free;
FFGPen.Free;
inherited Destroy;
end;
procedure TRGBMask.LoadFromLazIntfImageAlpha(AImage: TLazIntfImage);
begin
inherited LoadFromLazIntfImageAlpha(AImage);
UpdateMaskedPixels;
end;
procedure TRGBMask.DrawShapeTo(ACanvas: TCanvas; X, Y: Integer);
begin
StretchDrawShapeTo(ACanvas, X, Y, Width, Height);
end;
procedure TRGBMask.StretchDrawShapeTo(ACanvas: TCanvas; DstX, DstY, DstWidth,
DstHeight: Integer);
begin
StretchDrawShapePortionTo(ACanvas, DstX, DstY, DstWidth, DstHeight,
0, 0, Width, Height);
end;
procedure TRGBMask.StretchDrawShapePortionTo(ACanvas: TCanvas; DstX, DstY,
DstWidth, DstHeight: Integer; DX, DY, DW, DH: Integer);
begin
if ACanvas <> nil then
StretchDrawRGBMaskShapePortion(ACanvas.Handle, DstX, DstY, DstWidth, DstHeight,
Self, DX, DY, DW, DH, FBGPen.Reference.Handle, FFGPen.Reference.Handle);
end;
procedure TRGBMask.DrawTo(ACanvas: TCanvas; X, Y: Integer);
begin
if ACanvas <> nil then
DrawRGB8Bitmap(ACanvas.Handle, X, Y, 0, 0, Width, Height, Self);
end;
procedure TRGBMask.StretchTrunc(AWidth, AHeight: Integer);
var
Result: TRGBMask;
begin
if (AWidth = Width) and (AHeight = Height) then Exit;
Result := TRGBMask.Create(AWidth, AHeight);
try
StretchRGB8BitmapTrunc(Result, Self);
SwapWith(Result);
FMaskedPixels := GetMaskedPixelsCount;
finally
FreeAndNil(Result);
end;
end;
procedure TRGBMask.Rectangle(X1, Y1, X2, Y2: Integer);
begin
FillPixelRect(X1, Y1, X2, Y2, GetFillProcedure);
end;
procedure TRGBMask.Ellipse(X1, Y1, X2, Y2: Integer);
begin
EllipticRectangle(X1, Y1, X2, Y2, 0, 0, GetFillProcedure, GetFillProcedure);
end;
procedure TRGBMask.Clear;
begin
inherited Clear;
FMaskedPixels := 0;
end;
procedure TRGBMask.ClearWhite;
begin
inherited ClearWhite;
FMaskedPixels := Width * Height;
end;
procedure TRGBMask.Invert;
begin
inherited Invert;
FMaskedPixels := Width * Height - FMaskedPixels;
end;
function TRGBMask.IsEmpty: Boolean;
begin
Result := FMaskedPixels = 0;
end;
function TRGBMask.GetMaskedRect: TRect;
var
I, J: Integer;
LineMasked: Boolean;
P: PRGB8Pixel;
begin
Result := Rect(Width, Height, 0, 0);
for J := 0 to Pred(Height) do
begin
P := Get8PixelPtr(0, J);
LineMasked := False;
for I := 0 to Pred(Width) do
begin
if P^ = $FF then
begin
LineMasked := True;
if I < Result.Left then Result.Left := I;
if Succ(I) > Result.Right then Result.Right := Succ(I);
end;
Inc(P);
end;
if LineMasked then
begin
if J < Result.Top then Result.Top := J;
if Succ(J) > Result.Bottom then Result.Bottom := Succ(J);
end;
end;
SortRect(Result);
end;
end.
|