forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgragraphics.pas
2486 lines (2156 loc) · 68.2 KB
/
bgragraphics.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 | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAGraphics;
{=== Types imported from Graphics ===}
{$i bgrabitmap.inc}{$H+}
interface
{$IFDEF BGRABITMAP_USE_LCL}
uses Windows, Classes, Types, SysUtils, BGRATypes, {$IFDEF BDS} Generics.Collections, CommCtrl, ImgList, {$ENDIF} Graphics, GraphType, FPImage;
type
PColor = Graphics.PColor;
TColor = Graphics.TColor;
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
TAntialiasingMode = GraphType.TAntialiasingMode;
TFPPenJoinStyle = (
pjsRound,
pjsBevel,
pjsMiter
);
TPenJoinStyle = TFPPenJoinStyle;
TFPPenEndCap = (
pecRound,
pecSquare,
pecFlat
);
TPenEndCap = TFPPenEndCap;
TFPPenStyle = (psSolid, psDash, psDot, psDashDot, psDashDotDot, psinsideFrame, psPattern,psClear);
TFPPenStyleSet = set of TFPPenStyle;
TFPPenMode = (pmBlack, pmWhite, pmNop, pmNot, pmCopy, pmNotCopy,
pmMergePenNot, pmMaskPenNot, pmMergeNotPen, pmMaskNotPen, pmMerge,
pmNotMerge, pmMask, pmNotMask, pmXor, pmNotXor);
TPenPattern = BGRALongWord;
TPenStyle = TFPPenStyle;
TPenMode = TFPPenMode;
TFillStyle = Graphics.TFillStyle;
TGradientDirection = GraphType.TGradientDirection;
{$ELSE}
TAntialiasingMode = Graphics.TAntialiasingMode;
TGradientDirection = Graphics.TGradientDirection;
TPenEndCap = Graphics.TPenEndCap;
TPenJoinStyle = Graphics.TPenJoinStyle;
TPenStyle = Graphics.TPenStyle;
{$IFEND}
const
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
{$ELSE}
amDontCare = Graphics.amDontCare;
amOn = Graphics.amOn;
amOff = Graphics.amOff;
gdVertical = Graphics.gdVertical;
gdHorizontal = Graphics.gdHorizontal;
pecRound = Graphics.pecRound;
pecSquare = Graphics.pecSquare;
pecFlat = Graphics.pecFlat;
pjsRound = Graphics.pjsRound;
pjsBevel = Graphics.pjsBevel;
pjsMiter = Graphics.pjsMiter;
psPattern = Graphics.psPattern;
psSolid = Graphics.psSolid;
psDash = Graphics.psDash;
psDot = Graphics.psDot;
psDashDot = Graphics.psDashDot;
psDashDotDot = Graphics.psDashDotDot;
psClear = Graphics.psClear;
psInsideframe = Graphics.psInsideframe;
{$IFEND}
tmAuto = Graphics.tmAuto;
tmFixed = Graphics.tmFixed;
type
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
TAlignment = (taLeftJustify,taRightJustify,taCenter);
TTextLayout = (tlTop, tlCenter, tlBottom);
TTextStyle = packed record
Alignment : TAlignment;
Layout : TTextLayout;
SingleLine: boolean;
Clipping : boolean;
ExpandTabs: boolean;
ShowPrefix: boolean;
Wordbreak : boolean;
Opaque : boolean;
SystemFont: Boolean;
RightToLeft: Boolean;
EndEllipsis: Boolean;
end;
{$ELSE}
TPen = Graphics.TPen;
TTextLayout = Graphics.TTextLayout;
TTextStyle = Graphics.TTextStyle;
TFillStyle = Graphics.TFillStyle;
{$IFEND}
TFillMode = Graphics.TFillMode;
TBrushStyle = Graphics.TBrushStyle;
const
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
{$ELSE}
tlTop = Graphics.tlTop;
tlCenter = Graphics.tlCenter;
tlBottom = Graphics.tlBottom;
fsSurface = GraphType.fsSurface;
fsBorder = GraphType.fsBorder;
{$IFEND}
fmAlternate = Graphics.fmAlternate;
fmWinding = Graphics.fmWinding;
bsSolid = Graphics.bsSolid;
bsClear = Graphics.bsClear;
bsHorizontal = Graphics.bsHorizontal;
bsVertical = Graphics.bsVertical;
bsFDiagonal = Graphics.bsFDiagonal;
bsBDiagonal = Graphics.bsBDiagonal;
bsCross = Graphics.bsCross;
bsDiagCross = Graphics.bsDiagCross;
type
TBrush = Graphics.TBrush;
TCanvas = Graphics.TCanvas;
TGraphic = Graphics.TGraphic;
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
TBitmap = class(Graphics.TBitmap)
private
FRefreshRawImage : boolean;
FRawImage : TRawImage;
function GetRawImage : TRawImage;
procedure FreeData;
protected
procedure Changed(Sender: TObject); override;
procedure UpdateRawImage; virtual;
public
constructor Create; override;
Destructor Destroy; override;
Property RawImage : TRawImage read GetRawImage;
procedure AssignRawImage(ARawImage : TRawImage);
procedure ResetRawImage;
end;
TRasterImage = TBitmap;
TRawImage = GraphType.TRawImage;
TCanvasVar = GraphType.TCanvasVar;
{$ELSE}
TBitmap = Graphics.TBitmap;
TRasterImage = Graphics.TRasterImage;
TRawImage = GraphType.TRawImage;
{$IFEND}
TFontStyle = Graphics.TFontStyle;
TFontStyles = Graphics.TFontStyles;
TFontQuality = Graphics.TFontQuality;
TFont = Graphics.TFont;
const
fsBold = Graphics.fsBold;
fsItalic = Graphics.fsItalic;
fsStrikeOut = Graphics.fsStrikeOut;
fsUnderline = Graphics.fsUnderline;
fqDefault = Graphics.fqDefault;
fqDraft = Graphics.fqDraft;
fqProof = Graphics.fqProof;
fqNonAntialiased = Graphics.fqNonAntialiased;
fqAntialiased = Graphics.fqAntialiased;
fqCleartype = Graphics.fqCleartype;
fqCleartypeNatural = Graphics.fqCleartypeNatural;
clNone = Graphics.clNone;
clBlack = Graphics.clBlack;
clMaroon = Graphics.clMaroon;
clGreen = Graphics.clGreen;
clOlive = Graphics.clOlive;
clNavy = Graphics.clNavy;
clPurple = Graphics.clPurple;
clTeal = Graphics.clTeal;
clGray = Graphics.clGray;
clSilver = Graphics.clSilver;
clRed = Graphics.clRed;
clLime = Graphics.clLime;
clYellow = Graphics.clYellow;
clBlue = Graphics.clBlue;
clFuchsia = Graphics.clFuchsia;
clAqua = Graphics.clAqua;
clLtGray = Graphics.clLtGray; // clSilver alias
clDkGray = Graphics.clDkGray; // clGray alias
clWhite = Graphics.clWhite;
type
TGBRACustomGraphic = class(TGraphic)
public
function GetMimeType: string; {$IFDEF FPC}override;{$ELSE}virtual;{$ENDIF}
procedure Clear; {$IFDEF FPC}override;{$ELSE}virtual;{$ENDIF}
end;
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
TFontPitch = graphics.TFontPitch;
TFontName = graphics.TFontName;
TFontDataName = string[LF_FACESIZE -1];
TFontStylesbase = set of TFontStyle;
TFontCharSet = 0..255;
TFontData = graphics.TFontData;
//TDicBitmapRawImage = TDictionary<TBitmap,TRawImage>;
TDicCanvasVar = TDictionary<TCanvas,TCanvasVar>;
var
// DicBitmapRawImage : TDicBitmapRawImage;
DicCanvasVar : TDicCanvasVar;
type
TRGBAQuad = record
Blue: Byte;
Green: Byte;
Red: Byte;
Alpha: Byte;
end;
PRGBAQuad = ^TRGBAQuad;
TRGBAQuadArray = array of TRGBAQuad;
TCustomImageListHelper = class helper for TCustomImageList
procedure GetBitmapRaw(Index: Integer; Image: TBitmap); overload;
// procedure GetBitmapRaw(Index: Integer; Image: TBitmap); overload;
end;
{ TBitmapHelper = class helper for TBitmap
private
class var FRawImage : TRawImage;
function GetRawImage : TRawImage;
public
Property RawImage : TRawImage read GetRawImage;
function UpdateRawImage : boolean;
procedure AssignRawImage(ARawImage : TRawImage);
procedure ResetRawImage;
end;}
TStreamHelper = class helper for TStream
procedure WriteByte(b : Byte);
procedure WriteWord(w : BGRAWord);
procedure WriteDWord(d : BGRACardinal);
procedure WriteQWord(q: BGRAQWord);
function ReadDWord : BGRACardinal;
end;
TFontHelper = class helper for TFont
function GetColor: TColor;
end;
TCanvasHelper = class helper for TCanvas
private
class var FCanvasVar : TCanvasVar;
public
function TextFitInfo(const Text: string; MaxWidth: Integer): Integer;
function GetUpdatedHandle(ReqState: TCanvasState): HDC;
procedure GetTextSize (text:string; var w,h:integer);
function GetTextHeight (text:string) : integer;
function GetTextWidth (text:string) : integer;
procedure TextRect(ARect: TRect; X, Y: integer; const Text: string; const Style: TTextStyle);
function GetWidth: integer;
function GetHeight: integer;
property Height: integer read GetHeight;
property Width: integer read GetWidth;
function GetAntialiasingMode : TAntialiasingMode;
procedure SetAntialiasingMode(AValue : TAntialiasingMode);
function GetState : TCanvasState;
function GetCanvasVar : TCanvasVar;
Property CanvasVar : TCanvasVar read GetCanvasVar;
Property State : TCanvasState read GetState;
Property AntialiasingMode : TAntialiasingMode read GetAntialiasingMode write SetAntialiasingMode;
end;
{$IFEND}
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
function GetDeviceSize(DC: HDC; var p: TPoint): boolean;
procedure FillRawImageDescriptionColors(var ADesc: TRawImageDescription);
procedure FillRawImageDescription(const ABitmapInfo: Windows.TBitmap; out ADesc: TRawImageDescription);
function RawImage_CreateBitmaps(const ARawImage: TRawImage; out ABitmap, AMask: HBitmap; ASkipMask: Boolean): Boolean;
function RawImage_FromDevice(out ARawImage: TRawImage; ADC: HDC; const ARect: TRect): Boolean;
function RawImage_FromBitmap(out ARawImage: TRawImage; ABitmap, AMask: HBITMAP; ARect: PRect = nil): Boolean;
function TryStringToGUID(const S: Ansistring; out Guid: TGUID): Boolean;
function GetBitmapOrder(AWinBmp: Windows.TBitmap; ABitmap: HBITMAP): TRawImageLineOrder;
function GetBitmapBytes(AWinBmp: Windows.TBitmap; ABitmap: HBITMAP; const ARect: TRect; ALineEnd: TRawImageLineEnd; ALineOrder: TRawImageLineOrder; out AData: Pointer; out ADataSize: PtrUInt): Boolean;
function Blue(rgb: TColorRef): BYTE; // does not work on system color
function Green(rgb: TColorRef): BYTE; // does not work on system color
function Red(rgb: TColorRef): BYTE; // does not work on system color
function RGBToColor(R, G, B: Byte): TColor;
procedure RedGreenBlue(rgb: TColorRef; out Red, Green, Blue: Byte); // does not work on system color
function FPColorToTColorRef(const FPColor: TFPColor): TColorRef;
function FPColorToTColor(const FPColor: TFPColor): TColor;
function TColorToFPColor(const c: TColorRef): TFPColor; overload;
function TColorToFPColor(const c: TColor): TFPColor; overload; // does not work on system color
function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer;
function MathRound(AValue: ValReal): BGRAInt64;
function GetFontData(Font: HFont): TFontData;
function PtInRect(const Rect : TRect; const Point : TPoint) : Boolean;
function Point(AX, AY: Integer): TPoint;
function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
procedure SendKey(Key: BGRAWord); overload;
procedure SendKey(Key: BGRAWord; const Shift: TShiftState; SpecialKey: boolean = False); overload;
function CompareMethods(const m1, m2: TMethod): boolean;
{$IFEND}
function ColorToRGB(c: TColor): TColor;
function clRgbBtnHighlight: TColor;
function clRgbBtnShadow: TColor;
{$IFDEF FPC}
function FPColorToTColor(const FPColor: TFPColor): TColor;
function TColorToFPColor(const c: TColor): TFPColor;
function RGBToColor(R, G, B: Byte): TColor;
procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte);
{$ENDIF}
procedure Filldword_(var x;count : BGRACardinal;value : BGRADWord);
implementation
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
uses
BGRABitmapTypes;
{$IFEND}
procedure filldword_(var x;count : BGRACardinal; value : BGRADWord);assembler;
asm
PUSH EDI
MOV EDI, x
MOV EAX, value
MOV ECX, count
CLD
REP STOSD
POP EDI
end;
{$IF NOT DEFINED(FPC) AND DEFINED(BDS)}
function CompareMethods(const m1, m2: TMethod): boolean;
begin
Result:=(m1.Code=m2.Code) and (m1.Data=m2.Data);
end;
procedure SendKey(Key: BGRAWord);
begin
Keybd_Event(Key, 0, 0, 0);
end;
procedure SendKey(Key: BGRAWord; const Shift: TShiftState; SpecialKey: boolean);
type
TShiftKeyInfo = record
shift: Byte;
vkey: Byte;
end;
byteset = set of 0..7;
const
ShiftKeys: array[1..3] of TShiftKeyInfo = ((shift: Ord(ssCtrl); vkey: VK_CONTROL), (shift: Ord(ssShift); vkey: VK_SHIFT), (shift: Ord(ssAlt); vkey: VK_MENU));
var
Flag: BGRADWord;
bShift: ByteSet absolute shift;
i: Integer;
begin
for i := 1 to 3 do
begin
if shiftkeys[i].shift in bShift then
Keybd_Event(ShiftKeys[i].vkey, MapVirtualKey(ShiftKeys[i].vkey, 0), 0, 0);
end;
if SpecialKey then
Flag := KEYEVENTF_EXTENDEDKEY
else
Flag := 0;
Keybd_Event(Key, MapvirtualKey(Key, 0), Flag, 0);
Flag := Flag or KEYEVENTF_KEYUP;
Keybd_Event(Key, MapvirtualKey(Key, 0), Flag, 0);
for i := 3 downto 1 do
begin
if ShiftKeys[i].shift in bShift then
Keybd_Event(shiftkeys[i].vkey, MapVirtualKey(ShiftKeys[i].vkey, 0), KEYEVENTF_KEYUP, 0);
end;
end;
function Point(AX, AY: Integer): TPoint;
begin
with Result do
begin
X := AX;
Y := AY;
end;
end;
function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
begin
with Result do
begin
Left := ALeft;
Top := ATop;
Right := ARight;
Bottom := ABottom;
end;
end;
function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
begin
with Result do
begin
Left := ALeft;
Top := ATop;
Right := ALeft + AWidth;
Bottom := ATop + AHeight;
end;
end;
function PtInRect(const Rect : TRect; const Point : TPoint) : Boolean;
begin
Result := ((Point.X >= Rect.Left) and
(Point.X < Rect.Right) and
(Point.Y >= Rect.Top) and
(Point.Y < Rect.Bottom)
);
end;
function GetFontData(Font: HFont): TFontData;
var
ALogFont: TLogFont;
begin
Result := DefFontData;
if Font <> 0 then
begin
if GetObject(Font, SizeOf(ALogFont), @ALogFont) <> 0 then
with Result, ALogFont do
begin
Height := lfHeight;
if lfWeight >= FW_BOLD then
Include(Style, fsBold);
if lfItalic > 0 then
Include(Style, fsItalic);
if lfUnderline > 0 then
Include(Style, fsUnderline);
if lfStrikeOut > 0 then
Include(Style, fsStrikeOut);
Charset := TFontCharset(lfCharSet);
Name := TFontDataName(lfFaceName);
case lfPitchAndFamily and $F of
VARIABLE_PITCH: Pitch := fpVariable;
FIXED_PITCH: Pitch := fpFixed;
else
Pitch := fpDefault;
end;
Orientation := lfOrientation;
Handle := Font;
end;
end;
end;
function MathRound(AValue: ValReal): BGRAInt64;
begin
if AValue >= 0 then
Result := Trunc(AValue + 0.5)
else
Result := Trunc(AValue - 0.5);
end;
function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer;
begin
if nDenominator = 0 then
Result := -1
else
if nNumerator = nDenominator then
Result := nNumber
else
Result := MathRound(BGRAInt64(nNumber) * BGRAInt64(nNumerator) / nDenominator);
end;
function Blue(rgb: TColorRef): BYTE; // does not work on system color
begin
Result := (rgb shr 16) and $000000ff;
end;
function Green(rgb: TColorRef): BYTE; // does not work on system color
begin
Result := (rgb shr 8) and $000000ff;
end;
function Red(rgb: TColorRef): BYTE; // does not work on system color
begin
Result := rgb and $000000ff;
end;
function RGBToColor(R, G, B: Byte): TColor;
begin
Result := (B shl 16) or (G shl 8) or R;
end;
procedure RedGreenBlue(rgb: TColorRef; out Red, Green, Blue: Byte); // does not work on system color
begin
Red := rgb and $000000ff;
Green := (rgb shr 8) and $000000ff;
Blue := (rgb shr 16) and $000000ff;
end;
function FPColorToTColorRef(const FPColor: TFPColor): TColorRef;
begin
Result:=((FPColor.Red shr 8) and $ff)
or (FPColor.Green and $ff00)
or ((FPColor.Blue shl 8) and $ff0000);
end;
function FPColorToTColor(const FPColor: TFPColor): TColor;
begin
Result:=TColor(FPColorToTColorRef(FPColor));
end;
function TColorToFPColor(const c: TColorRef): TFPColor; overload;
begin
Result.Red:=(c and $ff);
Result.Red:=Result.Red+(Result.Red shl 8);
Result.Green:=(c and $ff00);
Result.Green:=Result.Green+(Result.Green shr 8);
Result.Blue:=(c and $ff0000) shr 8;
Result.Blue:=Result.Blue+(Result.Blue shr 8);
Result.Alpha:=FPImage.alphaOpaque;
end;
function TColorToFPColor(const c: TColor): TFPColor; overload; // does not work on system color
begin
Result:=TColorToFPColor(TColorRef(c));
end;
function TryStringToGUID(const S: Ansistring; out Guid: TGUID): Boolean;
var
e: Boolean;
p: PAnsiChar;
function rb: Byte;
begin
case p^ of
'0'..'9': Result := Byte(p^) - Byte('0');
'a'..'f': Result := Byte(p^) - Byte('a') + 10;
'A'..'F': Result := Byte(p^) - Byte('A') + 10;
else e := False;
end;
Inc(p);
end;
procedure nextChar(c: AnsiChar);
begin
if p^ <> c then
e := False;
Inc(p);
end;
begin
if Length(S)<>38 then Exit(False);
e := True;
p := PAnsiChar(S);
nextChar('{');
Guid.D1 := rb shl 28 or rb shl 24 or rb shl 20 or rb shl 16 or rb shl 12 or rb shl 8 or rb shl 4 or rb;
nextChar('-');
Guid.D2 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
nextChar('-');
Guid.D3 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
nextChar('-');
Guid.D4[0] := rb shl 4 or rb;
Guid.D4[1] := rb shl 4 or rb;
nextChar('-');
Guid.D4[2] := rb shl 4 or rb;
Guid.D4[3] := rb shl 4 or rb;
Guid.D4[4] := rb shl 4 or rb;
Guid.D4[5] := rb shl 4 or rb;
Guid.D4[6] := rb shl 4 or rb;
Guid.D4[7] := rb shl 4 or rb;
nextChar('}');
Result := e;
end;
{TCustomImageListHelper}
{procedure TCustomImageListHelper.GetBitmapRaw(Index: Integer; Image: TBitmap);
begin
GetBitmapRaw(Index, TBaseBitmap(Image));
end;}
procedure TCustomImageListHelper.GetBitmapRaw(Index: Integer; Image: TBitmap);
var
RawImg: TRawImage;
hbmImage : TImageInfo;
WinDIB: Windows.TDIBSection;
WinBmp: Windows.TBitmap absolute WinDIB.dsBm;
i, i2, iSize, iForIni, iForFin, iLineSize : Integer;
PixelFormat : TPixelFormat;
begin
if (Index < 0) or (Index >= Count) or (Image = nil) then Exit;
{ if not Image.HandleAllocated then
GetBitmap(Index, Image);
if not Image.UpdateRawImage then
raise Exception.Create('Error UpdateRawImage.'); }
PixelFormat := Image.PixelFormat;
Image.PixelFormat := pf32bit;
RawImg.Init;
ImageList_GetImageInfo(Handle, Index, hbmImage);
if not Image.HandleAllocated then
GetBitmap(Index, Image);
if RawImage_FromBitmap(RawImg, Image.Handle, Image.MaskHandle) then
Image.AssignRawImage(RawImg);
Image.PixelFormat := PixelFormat;
// Image.Handle := hbmImage.hbmImage;
// Image.MaskHandle := hbmImage.hbmImage;
// Image.SetSize(RawImg.Description.Width, RawImg.Description.Height);
// Image.TransparentColor := Self.BkColor;
// if Image.RawImage.Data <> nil then
// FreeMem(Image.RawImage.Data);
{ iSize := Trunc(RawImg.DataSize / (RawImg.Description.Height / Height));
GetMem(_PByte, iSize);
i2 := 0;
iLineSize := Trunc(iSize / Height);
iForIni := (Height * Index);
iForFin := iForIni + Height;
for i := iForIni to iForFin do
begin
system.Move(RawImg.Data[i],_PByte[i2], iLineSize);
inc(i2);
end;
// FreeMem(RawImg.Data);
// FreeMem(RawImg.Mask);
RawImg.DataSize := iSize;
RawImg.MaskSize := 0;
// RawImg.Data := nil;
// RawImg.Mask := nil;
// system.Move(RawImg.Data[i],_PByte[i2], iLineSize);
RawImg.data := _PByte;
// system.Move(_PByte,RawImg.data, iSize); }
end;
{TFontHelper}
function TFontHelper.GetColor: TColor;
begin
Result := Color;
// if (Result = clDefault) and (Canvas is TCanvas) then
// Result := TCanvas(Canvas).GetDefaultColor(dctFont);
end;
{TCanvasHelper}
function TCanvasHelper.GetState : TCanvasState;
begin
Result := CanvasVar.FState;
end;
function TCanvasHelper.GetCanvasVar : TCanvasVar;
var
NewCanvasVar : TCanvasVar;
begin
if DicCanvasVar.ContainsKey(Self) then
begin
Result := DicCanvasVar.Items[Self];
FCanvasVar := Result;
end
else
begin
NewCanvasVar.FCanvas := Self;
DicCanvasVar.Add(Self, NewCanvasVar);
Result := NewCanvasVar;
FCanvasVar := Result;
end;
end;
function TCanvasHelper.GetAntialiasingMode : TAntialiasingMode;
begin
Result := GetCanvasVar.FAntialiasingMode;
end;
procedure TCanvasHelper.SetAntialiasingMode(AValue : TAntialiasingMode);
begin
GetCanvasVar;
FCanvasVar.FAntialiasingMode := AValue;
end;
function TCanvasHelper.GetUpdatedHandle(ReqState: TCanvasState): HDC;
begin
RequiredState(ReqState+[csHandleValid]);
Result := Handle;
end;
procedure TCanvasHelper.TextRect(ARect: TRect; X, Y: integer; const Text: string; const Style: TTextStyle);
var
Options : BGRALongInt;
fRect : TRect;
DCIndex: Integer;
DC: HDC;
ReqState: TCanvasState;
procedure SaveState;
begin
if DCIndex<>0 then exit;
DCIndex:=SaveDC(DC);
end;
procedure RestoreState;
begin
if DCIndex=0 then exit;
RestoreDC(DC,DCIndex);
end;
begin
//debugln(['TCanvas.TextRect ',DbgSName(Self),' Text="',Text,'" ',dbgs(ARect),' X=',X,',Y=',Y]);
Changing;
Options := 0;
case Style.Alignment of
taRightJustify : Options := DT_RIGHT;
taCenter : Options := DT_CENTER;
end;
case Style.Layout of
tlCenter : Options := Options or DT_VCENTER;
tlBottom : Options := Options or DT_BOTTOM;
end;
if Style.EndEllipsis then
Options := Options or DT_END_ELLIPSIS;
if Style.WordBreak then begin
Options := Options or DT_WORDBREAK;
if Style.EndEllipsis then
Options := Options and not DT_END_ELLIPSIS;
end;
if Style.SingleLine then
Options := Options or DT_SINGLELINE;
if not Style.Clipping then
Options := Options or DT_NOCLIP;
if Style.ExpandTabs then
Options := Options or DT_EXPANDTABS;
if not Style.ShowPrefix then
Options := Options or DT_NOPREFIX;
if Style.RightToLeft then
Options := Options or DT_RTLREADING;
ReqState:=[csHandleValid];
if not Style.SystemFont then
Include(ReqState,csFontValid);
if Style.Opaque then
Include(ReqState,csBrushValid);
DC:=GetUpdatedHandle(ReqState);
DCIndex:=0;
if Style.SystemFont or Style.Clipping or (not Style.Opaque) then
SaveState;
// if Style.SystemFont then
// Windows.SelectObject(DC, OnGetSystemFont());
// calculate text rectangle
FRect := ARect;
if Style.Alignment = taLeftJustify then
fRect.Left := X;
if Style.Layout = tlTop then
fRect.Top := Y;
if (Style.Alignment in [taRightJustify,taCenter]) or
(Style.Layout in [tlCenter,tlBottom]) then
begin
DrawText(DC, (*{$IFDEF FPC}PChar{$ELSE}PAnsiChar{$ENDIF}*)PChar(Text), Length(Text), fRect, DT_CALCRECT or Options);
case Style.Alignment of
taRightJustify : OffsetRect(fRect, ARect.Right - fRect.Right, 0);
taCenter : OffsetRect(fRect, (ARect.Right - fRect.Right) div 2, 0);
end;
case Style.Layout of
tlCenter : OffsetRect(fRect, 0,
((ARect.Bottom - ARect.Top) - (fRect.Bottom - fRect.Top)) div 2);
tlBottom : OffsetRect(fRect, 0, ARect.Bottom - fRect.Bottom);
end;
end;
if Style.Clipping then
begin
with ARect do
InterSectClipRect(DC, Left, Top, Right, Bottom);
Options := Options or DT_NOCLIP; // no clipping as we are handling it here
end;
if Style.Opaque then
FillRect(fRect)
else
SetBkMode(DC, TRANSPARENT);
if Style.SystemFont then
SetTextColor(DC, TColorRef(Font.GetColor));
//debugln('TCanvas.TextRect DRAW Text="',Text,'" ',dbgs(fRect));
DrawText(DC, (*{$IFDEF FPC}PChar{$ELSE}PAnsiChar{$ENDIF}*)PChar(Text), Length(Text), fRect, Options);
if Style.Opaque and (csBrushValid in State) then
begin
if Brush.Style=bsSolid then // restore BKMode
SetBkMode(DC, OPAQUE)
end;
RestoreState;
Changed;
end;
function TCanvasHelper.TextFitInfo(const Text: string; MaxWidth: Integer): Integer;
var lSize: TSize;
begin
windows.GetTextExtentExPoint(Self.Handle, PChar(Text), Length(Text), MaxWidth, @Result, nil, lSize);
end;
procedure TCanvasHelper.GetTextSize (text:string; var w,h:integer);
var
TxtSize: TSize;
begin
TxtSize:=TextExtent(Text);
w:=TxtSize.cx;
h:=TxtSize.cy;
end;
function TCanvasHelper.GetTextHeight (text:string) : integer;
begin
Result:=TextHeight(Text);
end;
function TCanvasHelper.GetTextWidth (text:string) : integer;
begin
Result:=TextWidth(Text);
end;
{TStreamHelper}
procedure TStreamHelper.WriteByte(b : Byte);
begin
WriteBuffer(b,1);
end;
procedure TStreamHelper.WriteWord(w : BGRAWord);
begin
WriteBuffer(w,2);
end;
procedure TStreamHelper.WriteDWord(d : BGRACardinal);
begin
WriteBuffer(d,4);
end;
procedure TStreamHelper.WriteQWord(q: BGRAQWord);
begin
WriteBuffer(q,8);
end;
function TStreamHelper.ReadDWord : BGRACardinal;
var
d : BGRACardinal;
begin
ReadBuffer(d,4);
ReadDWord:=d;
end;
function TCanvasHelper.GetHeight: integer;
var
p: TPoint;
begin
if HandleAllocated then
begin
GetDeviceSize(Handle,p);
Result:=p.y;
end
else
Result:=0;
end;
function TCanvasHelper.GetWidth: integer;
var
p: TPoint;
begin
if HandleAllocated then
begin
GetDeviceSize(Handle,p);
Result:=p.x;
end
else
Result:=0;
end;
{TBitmap}
procedure TBitmap.Changed(Sender: TObject);
begin
inherited;
FRefreshRawImage := True;
end;
procedure TBitmap.FreeData;
begin
FreeMem(FRawImage.Data);
FreeMem(FRawImage.Mask);
FRawImage.Data := nil;
FRawImage.Mask := nil;
end;
procedure TBitmap.UpdateRawImage;
var
PixelFormat : TPixelFormat;
begin
PixelFormat := Self.PixelFormat;
if Self.PixelFormat <> pf32bit then
Self.PixelFormat := pf32bit;
FRawImage.Init;
if not RawImage_FromBitmap(FRawImage, Self.Handle, Self.MaskHandle) then
raise Exception.Create('Error in method RawImage_FromBitmap');
if Self.PixelFormat <> PixelFormat then
Self.PixelFormat := PixelFormat;
FRefreshRawImage := False;
end;
constructor TBitmap.Create;
begin
inherited;
FRawImage.Init;
end;
Destructor TBitmap.Destroy;
begin
FreeData;
inherited;
end;
procedure TBitmap.AssignRawImage(ARawImage : TRawImage);