-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathmormot.lib.gdiplus.pas
2169 lines (2002 loc) · 72.5 KB
/
mormot.lib.gdiplus.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
/// low-level access to the GDI+ API for Win32/Win64
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.lib.gdiplus;
{
*****************************************************************************
Windows GDI+ Graphics Device Interface Support
- GDI+ Shared Types
- GDI+ TImageAttributes wrapper
- TGdiPlus class for Direct Access to the GDI+ Library
- AntiAliased Rendering of GDI MetaFile
See mormot.ui.gdiplus.pas for high-level LCL/VCL pictures support.
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
{$ifdef OSPOSIX}
// do-nothing-unit on non Windows system
implementation
{$else}
uses
Windows,
ActiveX,
sysutils,
classes,
mormot.core.base,
mormot.core.os;
{.$define GDIPLUS_USEENCODERS}
// if defined, the GDI+ encoder list will be used - seems not necessary
// - should be defined here and in mormot.ui.gdiplus (i.e. Projects options)
{.$define GDIPLUS_USEDPI}
// if defined, the DrawAt() method is available, which respect dpi on drawing
// - should not be useful on most applications
// - should be defined here and in mormot.ui.gdiplus (i.e. Projects options)
{ ****************** GDI+ Shared Types }
{$MINENUMSIZE 4}
type
/// exception class raised during GDI+ process
EGdiPlus = class(ExceptionWithProps);
/// GDI+ line drawing smoothing types
TSmoothingMode = (
smDefault,
smHighSpeed,
smHighQuality,
smNone,
smAntiAlias
);
/// GDI+ text rendering smoothing types
TTextRenderingHint = (
trhDefault,
trhSingleBitPerPixelGridFit,
trhSingleBitPerPixel,
trhAntiAliasGridFit,
trhAntiAlias,
trhClearTypeGridFit
);
/// GDI+ available coordinates units
TUnit = (
uWorld,
uDisplay,
uPixel,
uPoint,
uInch,
uDocument,
uMillimeter,
uGdi
);
/// GDI+ types of conversion from EMF to EMF+
TEmfType = (
etEmf0, etEmf1, etEmf2, { unused }
etEmfOnly,
etEmfPlusOnly,
etEmfPlusDual
);
/// GDI+ available filling modes
TFillMode = (
fmAlternate,
fmWinding
);
/// GDI+ lock mode for GdipFull.BitmapLockBits
TLockModeOption = (
lmRead,
lmWrite,
lmUserInputBuf
);
/// GDI+ lock mode settings for GdipFull.BitmapLockBits
TLockModeOptions = set of TLockModeOption;
/// GDI+ error codes
TGdipStatus = (
stOk,
stGenericError,
stInvalidParameter,
stOutOfMemory,
stObjectBusy,
stInsufficientBuffer,
stNotImplemented,
stWin32Error,
stWrongState,
stAborted,
stFileNotFound,
stValueOverflow,
stAccessDenied,
stUnknownImageFormat,
stFontFamilyNotFound,
stFontStyleNotFound,
stNotTrueTypeFont,
stUnsupportedGdiplusVersion,
stGdiplusNotInitialized,
stPropertyNotFound,
stPropertyNotSupported);
/// GDI+ integer coordinates rectangles
// - use width and height instead of right and bottom
TGdipRect = record
X, Y, Width, Height: integer;
end;
PGdipRect = ^TGdipRect;
/// GDI+ floating point coordinates rectangles
// - use width and height instead of right and bottom
TGdipRectF = record
X, Y, Width, Height: Single;
end;
PGdipRectF = ^TGdipRectF;
/// GDI+ floating point coordinates for a point
TGdipPointF = record
X, Y: Single;
end;
PGdipPointF = ^TGdipPointF;
PGdipPointFArray = ^TGdipPointFArray;
/// GDI+ floating point coordinates for an array of points
TGdipPointFArray = array[0..1000] of TGdipPointF;
/// data as retrieved by GdipFull.BitmapLockBits
TGdipBitmapData = record
Width: cardinal;
Height: cardinal;
Stride: integer;
PixelFormat: integer;
Scan0: pointer;
Reserved: cardinal;
end;
PGdipBitmapData = ^TGdipBitmapData;
TGpipImageAttributes = pointer;
TColorAdjustType = (
ColorAdjustTypeDefault,
ColorAdjustTypeBitmap,
ColorAdjustTypeBrush,
ColorAdjustTypePen,
ColorAdjustTypeText,
ColorAdjustTypeCount,
ColorAdjustTypeAny
);
TColorMatrix = packed array[0..4, 0..4] of Single;
PColorMatrix = ^TColorMatrix;
TColorMatrixFlags = (
ColorMatrixFlagsDefault,
ColorMatrixFlagsSkipGrays,
ColorMatrixFlagsAltGray
);
TColorChannelFlags = (
ColorChannelFlagsC,
ColorChannelFlagsM,
ColorChannelFlagsY,
ColorChannelFlagsK,
ColorChannelFlagsLast
);
TColorMap = packed record
oldColor: cardinal;
newColor: cardinal;
end;
PColorMap = ^TColorMap;
TWrapMode = (
WrapModeTile,
WrapModeTileFlipX,
WrapModeTileFlipY,
WrapModeTileFlipXY,
WrapModeClamp
);
TColorPalette = packed record
Flags: UINT;
Count: UINT;
Entries: array [0..0] of cardinal;
end;
PColorPalette = ^TColorPalette;
// Region combine mode (used by SetClipRegion, etc.)
TGdipCombineMode = (
cmReplace,
cmIntersect,
cmUnion,
cmXor,
cmExclude,
cmComplement
);
/// allowed types for image saving
TGdipPictureType = (
gptGIF,
gptPNG,
gptJPG,
gptBMP,
gptTIF
);
/// the optional TIFF compression levels
// - use e.g. ord(evCompressionCCITT4) to save a TIFF picture as CCITT4
TGdipPEncoderValue = (
evColorTypeCMYK,
evColorTypeYCCK,
evCompressionLZW,
evCompressionCCITT3,
evCompressionCCITT4,
evCompressionRle,
evCompressionNone,
evScanMethodInterlaced,
evScanMethodNonInterlaced,
evVersionGif87,
evVersionGif89,
evRenderProgressive,
evRenderNonProgressive,
evTransformRotate90,
evTransformRotate180,
evTransformRotate270,
evTransformFlipHorizontal,
evTransformFlipVertical,
evMultiFrame,
evLastFrame,
evFlush,
evFrameDimensionTime,
evFrameDimensionResolution,
evFrameDimensionPage
);
TEncoderParameter = record
Guid : TGuid; // GUID of the parameter
NumberOfValues : ULONG; // number of values for this parameter
Type_ : ULONG; // value type, like ValueTypeLONG etc.
Value : pointer; // a pointer to the parameter values
end;
PEncoderParameter = ^TEncoderParameter;
TEncoderParameters = record
Count : UINT; // number of parameters in this structure
Parameter : array[0..0] of TEncoderParameter; // parameter values
end;
PEncoderParameters = ^TEncoderParameters;
TGdiPlusHookProc = function(out token: THandle): integer; stdcall;
TGdiPlusUnhookProc = procedure(token: THandle); stdcall;
const
EncoderParameterValueTypeLong = 4; // 32-bit unsigned int
EncoderQuality: TGuid = '{1d5be4b5-fa4a-452d-9cdd-5db35105e7eb}';
EncoderCompression: TGuid = '{e09d739d-ccd4-44ee-8eba-3fbf8be4fc58}';
FrameDimensionPage: TGuid = '{7462dc86-6180-4c7e-8e3f-ee7333a7a483}';
const
/// the corresponding file extension for every saving format type
GdipPictureExt: array [TGdipPictureType] of TFileName = (
'.gif',
'.png',
'.jpg',
'.bmp',
'.tif'
);
{$ifdef GDIPLUS_USEENCODERS}
PICTURE_MIME_TYPES: array[TGdipPictureType] of PAnsiChar = (
'image/gif',
'image/png',
'image/jpeg',
'image/bmp',
'image/tiff'
);
var
ENCODERS_GUID: array[TGdipPictureType] of TGuid;
{$else}
const
ENCODERS_GUID: array[TGdipPictureType] of TGuid = (
'{557CF402-1A04-11D3-9A73-0000F81EF32E}',
'{557CF406-1A04-11D3-9A73-0000F81EF32E}',
'{557CF401-1A04-11D3-9A73-0000F81EF32E}',
'{557CF400-1A04-11D3-9A73-0000F81EF32E}',
'{557CF405-1A04-11D3-9A73-0000F81EF32E}'
);
{$endif GDIPLUS_USEENCODERS}
{ ****************** GDI+ TImageAttributes wrapper }
type
/// an object wrapper to handle gdi+ image attributes
TImageAttributes = class
protected
fAttr: TGpipImageAttributes;
public
constructor Create; overload;
constructor Create(clone: TImageAttributes); overload;
destructor Destroy; override;
function SetToIdentity(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function Reset(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetColorMatrix(const colormatrix: TColorMatrix;
flags: TColorMatrixFlags = ColorMatrixFlagsDefault;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearColorMatrix(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetThreshold(threshold: Single;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearThreshold(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetGamma(gamma: Single;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearGamma(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetNoOp(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearNoOp(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetColorKey(colorLow, colorHigh: cardinal;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearColorKey(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetOutputChannel(channelFlags: TColorChannelFlags;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearOutputChannel(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetOutputChannelColorProfile(colorProfileName: PWideChar;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearOutputChannelColorProfile(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetRemapTable(mapSize: cardinal; map: PColorMap;
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function ClearRemapTable(
adjusttype: TColorAdjustType = ColorAdjustTypeDefault): TGdipStatus;
function SetWrapMode(wrap: TWrapMode; color: cardinal = $ff000000;
clamp: Boolean = false): TGdipStatus;
function GetAdjustedPalette(colorPalette: PColorPalette;
colortype: TColorAdjustType): TGdipStatus;
property Attr: TGpipImageAttributes
read fAttr;
end;
{ ****************** TGdiPlus class for Direct Access to the GDI+ Library }
type
/// Direct access to the Windows GDI+ library
// - wrap most used GDI+ library calls
// - is dynamically loaded, so application could start e.g. on plain XP
TGdiPlus = class(TSynLibrary)
protected
fToken: THandle;
fStartupHook: record
Hook: TGdiPlusHookProc;
Unhook: TGdiPlusUnhookProc;
end;
fStartupHookToken: THandle;
fLock: TRTLCriticalSection;
public
// Picture related API calls of the GDI+ class hierarchy
Startup: function(var Token: THandle; var Input, Output): TGdipStatus; stdcall;
Shutdown: procedure(Token: THandle); stdcall;
DeleteGraphics: function(graphics: THandle): TGdipStatus; stdcall;
CreateFromHDC: function(hdc: HDC; out Graphics: THandle): TGdipStatus; stdcall;
LoadImageFromStream: function(stream: IStream; out image: THandle): TGdipStatus; stdcall;
LoadImageFromFile: function(filename: PWideChar; out image: THandle): TGdipStatus; stdcall;
DrawImageRect: function(graphics, image: THandle; x,y,width,height: integer): TGdipStatus; stdcall;
DrawImageRectRect: function(graphics, image: THandle; xd,yd,wd,hd, xs,ys,ws,hs: integer;
u: TUnit=uPixel; imageAttributes: TGpipImageAttributes=nil; callback: pointer=nil;
calldata: pointer=nil): TGdipStatus; stdcall;
{$ifdef GDIPLUS_USEDPI}
DrawImage: function(graphics, image: THandle; x,y: integer): TGdipStatus; stdcall;
{$endif GDIPLUS_USEDPI}
DisposeImage: function(image: THandle): TGdipStatus; stdcall;
GetImageRawFormat: function(image: THandle; var format: TGuid): TGdipStatus; stdcall;
GetImageWidth: function(image: THandle; var width: cardinal): TGdipStatus; stdcall;
GetImageHeight: function(image: THandle; var height: cardinal): TGdipStatus; stdcall;
SaveImageToStream: function(image: THandle; stream: IStream;
clsidEncoder: PGuid; encoderParams: pointer): TGdipStatus; stdcall;
{$ifdef GDIPLUS_USEENCODERS}
GetImageEncodersSize: function(out numEncoders: cardinal;
out size: cardinal): TGdipStatus; stdcall;
GetImageEncoders: function(numEncoders, size: cardinal;
encoders: pointer): TGdipStatus; stdcall;
{$endif GDIPLUS_USEENCODERS}
CreateBitmapFromHBITMAP: function(hbm: HBITMAP; hpal: HPALETTE;
out bitmap: THandle): TGdipStatus; stdcall;
CreateBitmapFromGdiDib: function(bmi, bits: pointer; out bitmap: THandle): TGdipStatus; stdcall;
BitmapSetResolution: function(bitmap: THandle; XDPI,YDPI: single): TGdipStatus; stdcall;
GetFrameCount: function(image: THandle; dimensionID: PGuid; var count: UINT): TGdipStatus; stdcall;
SelectActiveFrame: function(image: THandle; dimensionID: PGuid; frameIndex: UINT): TGdipStatus; stdcall;
CreateImageAttributes: function(out imageattr: TGpipImageAttributes): TGdipStatus; stdcall;
CloneImageAttributes: function(imageattr: TGpipImageAttributes; out cloneImageattr: TGpipImageAttributes): TGdipStatus; stdcall;
DisposeImageAttributes: function(imageattr: TGpipImageAttributes): TGdipStatus; stdcall;
SetImageAttributesToIdentity: function(imageattr: TGpipImageAttributes; adjusttype: TColorAdjustType): TGdipStatus; stdcall;
ResetImageAttributes: function(imageattr: TGpipImageAttributes; adjusttype: TColorAdjustType): TGdipStatus; stdcall;
SetImageAttributesColorMatrix: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; colorMatrix: PColorMatrix; grayMatrix: PColorMatrix; flags: TColorMatrixFlags): TGdipStatus; stdcall;
SetImageAttributesThreshold: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; threshold: Single): TGdipStatus; stdcall;
SetImageAttributesGamma: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; gamma: Single): TGdipStatus; stdcall;
SetImageAttributesNoOp: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool): TGdipStatus; stdcall;
SetImageAttributesColorKeys: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; colorLow, colorHigh: cardinal): TGdipStatus; stdcall;
SetImageAttributesOutputChannel: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; channelFlags: TColorChannelFlags): TGdipStatus; stdcall;
SetImageAttributesOutputChannelColorProfile: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; const colorProfileFilename: PWideChar): TGdipStatus; stdcall;
SetImageAttributesRemapTable: function(imageattr: TGpipImageAttributes; colortype: TColorAdjustType; enableFlag: Bool; mapSize: UINT; const map: PColorMap): TGdipStatus; stdcall;
SetImageAttributesWrapMode: function(imageattr: TGpipImageAttributes; wrap: TWrapMode; argb: cardinal; clamp: Bool): TGdipStatus; stdcall;
GetImageAttributesAdjustedPalette: function(imageattr: TGpipImageAttributes; out colorPalette: PColorPalette; colortype: TColorAdjustType): TGdipStatus; stdcall;
// drawing/extended API calls of the GDI+ class hierarchy
DrawLine: function(graphics, pen: THandle; x1,y1,x2,y2: integer): TGdipStatus; stdcall;
CreatePen: function(color: cardinal; width: Single; units: TUnit; out pen: THandle): TGdipStatus; stdcall;
DeletePen: function(pen: THandle): TGdipStatus; stdcall;
Flush: function(graphics: THandle; intention: integer=0): TGdipStatus; stdcall;
SetSmoothingMode: function(graphics: THandle; mode: TSmoothingMode): TGdipStatus; stdcall;
SetTextRenderingHint: function(graphics: THandle; mode: TTextRenderingHint): TGdipStatus; stdcall;
SetPenBrushFill: function(Pen, Brush: THandle): TGdipStatus; stdcall;
SetPenColor: function(Pen: THandle; Color: cardinal): TGdipStatus; stdcall;
SetPenWidth: function(Pen: THandle; Width: Single): TGdipStatus; stdcall;
DeleteBrush: function(brush: THandle): TGdipStatus; stdcall;
CreateSolidFill: function(color: cardinal; var brush: THandle): TGdipStatus; stdcall;
FillRectangle: function(graphics, brush: THandle; x, y, width, height: integer): TGdipStatus; stdcall;
FillEllipse: function(graphics, brush: THandle; x, y, width, height: integer): TGdipStatus; stdcall;
DrawEllipse: function(graphics, pen: THandle; x, y, width, height: integer): TGdipStatus; stdcall;
DrawCurve: function(graphics, pen: THandle; Points: pointer; Count: integer): TGdipStatus; stdcall;
GraphicsClear: function(Graphics: THandle; Color: cardinal): TGdipStatus; stdcall;
SetPageUnit: function(Graphics: THandle; units: TUnit): TGdipStatus; stdcall;
DrawRectangle: function(Graphics, Pen: THandle; X, Y, Width, Height: integer): TGdipStatus; stdcall;
SetPenDashStyle: function(Pen: THandle; dashStyle: integer): TGdipStatus; stdcall;
DrawPolygon: function(graphics, pen: THandle; points: pointer; count: integer): TGdipStatus; stdcall;
FillPolygon: function(graphics, brush: THandle; points: pointer; count: integer; fillMode: TFillMode): TGdipStatus; stdcall;
SetWorldTransform: function(graphics, matrix: THandle): TGdipStatus; stdcall;
GetWorldTransform: function(graphics, matrix: THandle): TGdipStatus; stdcall;
CreateMatrix: function(out matrix: THandle): TGdipStatus; stdcall;
CreateMatrix2: function(m11,m12,m21,m22,dx,dy: Single; out matrix: THandle): TGdipStatus; stdcall;
DeleteMatrix: function(matrix: THandle): TGdipStatus; stdcall;
SetMatrixElements: function(matrix: THandle; m11,m12,m21,m22,dx,dy: Single): TGdipStatus; stdcall;
MultiplyMatrix: function(matrix, matrix2: THandle; order: integer=0): TGdipStatus; stdcall;
ScaleMatrix: function(matrix: THandle; scaleX, scaleY: Single; order: integer=0): TGdipStatus; stdcall;
TranslateMatrix: function(matrix: THandle; offsetX, offsetY: Single; order: integer=0): TGdipStatus; stdcall;
DrawLines: function(Graphics, Pen: THandle; Points: pointer; Count: integer): TGdipStatus; stdcall;
RecordMetafile: function (DC: HDC; emfType: TEmfType; frameRect: PGdipRect;
frameUnit: TUnit; description: PWideChar; var out_metafile: THandle): TGdipStatus; stdcall;
RecordMetafileStream: function (strm: IStream; DC: HDC; emfType: TEmfType; const frameRect: TGdipRect;
frameUnit: TUnit; description: PWideChar; var out_metafile: THandle): TGdipStatus; stdcall;
PlayRecord: function(metafile: THandle; RecType, flags, RecSize: cardinal; Rec: pointer): TGdipStatus; stdcall;
EnumerateMetaFile: function(graphics, metafile: THandle; Dest: PGdipRect;
callback, data: pointer; imageAttributes: TGpipImageAttributes=nil): TGdipStatus; stdcall;
ResetWorldTransform: function(graphics: THandle): TGdipStatus; stdcall;
RotateTransform: function(graphics: THandle; angle: Single; order: integer=0): TGdipStatus; stdcall;
TranslateTransform: function(graphics: THandle; dx,dy: Single; order: integer=0): TGdipStatus; stdcall;
CreateFromImage: function(image: THandle; out graphics: THandle): TGdipStatus; stdcall;
CreateFontFrom: function(aHDC: HDC; out font: THandle): TGdipStatus; stdcall;
DeleteFont: function(font: THandle): TGdipStatus; stdcall;
CreateFontFromLogfont: function(hdc: HDC; logfont: PLOGFONTW; out font: THandle): TGdipStatus; stdcall;
DrawString: function(graphics: THandle; text: PWideChar; length: integer; font: THandle;
Dest: PGdipRectF; format, brush: THandle): TGdipStatus; stdcall;
MeasureString: function(graphics: THandle; text: PWideChar; length: integer; font: THandle;
Dest: PGdipRectF; format: THandle; bound: PGdipRectF;
codepointsFitted, linesFilled: PInteger): TGdipStatus; stdcall;
DrawDriverString: function(graphics: THandle; text: PWideChar;
length: integer; font, brush: THandle; positions: PGdipPointFArray; flag: integer; matrix: THandle): TGdipStatus; stdcall;
CreatePath: function(brushmode: TFillMode; var path: THandle): TGdipStatus; stdcall;
DeletePath: function(path: THandle): TGdipStatus; stdcall;
DrawPath: function(graphics, pen, path: THandle): TGdipStatus; stdcall;
FillPath: function(graphics, brush, path: THandle): TGdipStatus; stdcall;
AddPathLine: function(path: THandle; X1,Y1,X2,Y2: integer): TGdipStatus; stdcall;
AddPathLines: function(path: THandle; Points: pointer; Count: integer): TGdipStatus; stdcall;
AddPathArc: function(path: THandle; X,Y,width,height: integer; StartAndle, SweepAngle: single): TGdipStatus; stdcall;
AddPathCurve: function(path: THandle; Points: pointer; Count: integer): TGdipStatus; stdcall;
AddPathClosedCurve: function(): TGdipStatus; stdcall;
AddPathEllipse: function(path: THandle; X,Y,width,height: integer): TGdipStatus; stdcall;
AddPathPolygon: function(path: THandle; Points: pointer; Count: integer): TGdipStatus; stdcall;
AddPathRectangle: function(path: THandle; X,Y,width,height: integer): TGdipStatus; stdcall;
ClosePath: function(path: THandle): TGdipStatus; stdcall;
DrawArc: function(graphics, pen: THandle; X,Y,width,height: integer; StartAndle, SweepAngle: single): TGdipStatus; stdcall;
DrawBezier: function(graphics, pen: THandle; X1,Y1,X2,Y2,X3,Y3,X4,Y4: integer): TGdipStatus; stdcall;
DrawPie: function(graphics, pen: THandle; X,Y,width,height: integer; StartAndle, SweepAngle: single): TGdipStatus; stdcall;
CreateBitmapFromScan0: function(width, height, stride, format: integer; scan0: PByte;
out bitmap: THandle): TGdipStatus; stdcall;
BitmapLockBits: function(Bitmap: THandle; const Rect: PGdipRect;
Flags: TLockModeOptions; Format: integer; out LockedBitmapData: TGdipBitmapData): TGdipStatus; stdcall;
BitmapUnlockBits: function(Bitmap: THandle; const LockedBitmapData: TGdipBitmapData): TGdipStatus; stdcall;
GetClip: function (graphics: THandle; Region: THandle): TGdipStatus; stdcall;
SetClipRegion: function(graphics: THandle; region: THandle; CombineMode: TGdipCombineMode): TGdipStatus; stdcall;
SetClipRectI: function(graphics: THandle; X,Y,width,height: integer; CombineMode: TGdipCombineMode): TGdipStatus; stdcall;
ResetClip: function(graphics: THandle): TGdipStatus; stdcall;
CreateRegion: function(out Region: THandle): TGdipStatus; stdcall;
DeleteRegion: function(Region: THandle): TGdipStatus; stdcall;
/// this function is available only with GDI+ version 1.1 from MSOffice
ConvertToEmfPlus11: function(graphics, image: THandle; flag: pointer;
emftype: TEmfType; description: PWideChar; var out_metafile: THandle): TGdipStatus; stdcall;
{$ifdef GDIPLUS_USEENCODERS}
function GetEncoderClsid(format: PAnsiChar; out pClsid: TGuid): PtrInt;
{$endif GDIPLUS_USEENCODERS}
public
/// load the GDI+ library and all needed procedures
// - returns true on success
// - if no DLL file name is supplied, will search for system-available GDI+,
// starting from Microsoft Office GDI+ 1.1 with ConvertToEmfPlus API
// - library is loaded dynamically, therefore the executable is able
// to launch before Windows XP, but with no GDI + functions in such case
constructor Create(aDllFileName: TFileName = ''); reintroduce;
/// unload the GDI+ library
destructor Destroy; override;
/// GDI+ is not thread-safe, so use this mutex for proper multi-threading
procedure Lock;
/// GDI+ is not thread-safe, so use this mutex for proper multi-threading
procedure UnLock;
end;
var
/// internal singleton pointer called when inlining Gdip global function
// - you may free and override this instance with your own instance if needed
_Gdip: TGdiPlus;
/// internal function called when inlining Gdip global function
function _GdipLoad: TGdiPlus;
/// raise an EGdiPlus if the GDI+ library was not successfully loaded
procedure EnsureGdipExists(const caller: shortstring);
/// raise an EGdiPlus if no GDI+ library is available, or call Gdip.Lock
// - the GDI+ API is not thread-safe, so Gdip.Lock/UnLock is mandatory
procedure EnsureGdipExistsAndLock(const caller: shortstring);
/// access the GDI+ library instance
// - will try to load it if needed
// - Gdip.Exists return FALSE if the GDI+ library is not available in this
// operating system (e.g. on Windows 2000) nor the current executable folder
function Gdip: TGdiPlus;
{$ifdef HASINLINE} inline; {$endif}
{ *************** AntiAliased Rendering of GDI MetaFile }
type
/// define how ConvertToEmfPlus/DrawAntiAliased draw using GDI+
TEmfConvertOption = (
ecoNoGdiPlus,
ecoDrawString,
ecoInternalConvert
);
/// ConvertToEmfPlus/DrawAntiAliased drawing options
TEmfConvertOptions = set of TEmfConvertOption;
/// conversion of an EMF metafile handle into a EMF+ image
// - i.e. allows antialiased drawing of the EMF metafile
// - if GDI+ 1.1 (from Office 2007+ or Vista+) is available, will use it -
// otherwise, it will fallback to our own converter, enumerating the EMF records
// - return 0 if GDI+ is not available or conversion failed
// - return an EMF+ metafile handle, to be drawing via gdip.DrawImageRect(), and
// to be eventually released after use by gdip.DisposeImage()
// - this procedure is thread-safe (protected by Gdip.Lock/UnLock)
function ConvertToEmfPlus(Source: HENHMETAFILE; Width, Height: integer;
Dest: HDC; ConvertOptions: TEmfConvertOptions = [];
Smoothing: TSmoothingMode = smAntiAlias;
TextRendering: TTextRenderingHint = trhClearTypeGridFit): THandle;
/// draw an EMF metafile handle using GDI+ anti-aliased rendering
// - will fallback to plain GDI drawing if GDI+ is not available
// - this procedure is thread-safe (protected by Gdip.Lock/UnLock)
procedure DrawAntiAliased(Source: HENHMETAFILE; Width, Height: integer;
Dest: HDC; DestRect: TRect; ConvertOptions: TEmfConvertOptions = [];
Smoothing: TSmoothingMode = smAntiAlias;
TextRendering: TTextRenderingHint = trhClearTypeGridFit); overload;
/// draw an EMF metafile handle using GDI+ anti-aliased rendering
// - will fallback to plain GDI drawing if GDI+ is not available
// - this procedure is thread-safe (protected by Gdip.Lock/UnLock)
procedure DrawAntiAliased(Source: HENHMETAFILE; SourceRect: TRect;
Dest: HDC; DestRect: TRect; ConvertOptions: TEmfConvertOptions = [];
Smoothing: TSmoothingMode = smAntiAlias;
TextRendering: TTextRenderingHint = trhClearTypeGridFit;
u: TUnit = uPixel; attributes: TImageAttributes = nil); overload;
/// low-level internal function used e.g. by ConvertToEmfPlus()
function MetaFileToIStream(Source: HENHMETAFILE): IStream;
/// low-level internal function used e.g. by BitmapToRawByteString()
function IStreamSize(const S: IStream): Int64;
implementation
{ ****************** GDI+ TImageAttributes wrapper }
{ TImageAttributes }
constructor TImageAttributes.Create;
begin
EnsureGdipExists('TImageAttributes.Create'); // check it once (paranoid)
_Gdip.CreateImageAttributes(fAttr);
end;
constructor TImageAttributes.Create(clone: TImageAttributes);
begin
inherited Create;
_Gdip.CloneImageAttributes(clone.fAttr, fAttr)
end;
destructor TImageAttributes.Destroy;
begin
_Gdip.DisposeImageAttributes(fAttr);
inherited;
end;
function TImageAttributes.SetToIdentity(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesToIdentity(fAttr, adjusttype);
end;
function TImageAttributes.Reset(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.ResetImageAttributes(fAttr, adjusttype);
end;
function TImageAttributes.SetColorMatrix(const colormatrix: TColorMatrix;
flags: TColorMatrixFlags; adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesColorMatrix(
fAttr, adjusttype, true, @colormatrix, nil, flags);
end;
function TImageAttributes.ClearColorMatrix(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesColorMatrix(
fAttr, adjusttype, false, nil, nil, ColorMatrixFlagsDefault);
end;
function TImageAttributes.SetThreshold(threshold: Single;
adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesThreshold(
fAttr, adjusttype, true, threshold);
end;
function TImageAttributes.ClearThreshold(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesThreshold(
fAttr, adjusttype, false, 0.0);
end;
function TImageAttributes.SetGamma(gamma: Single;
adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesGamma(
fAttr, adjusttype, true, gamma);
end;
function TImageAttributes.ClearGamma(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesGamma(fAttr, adjusttype, false, 0.0);
end;
function TImageAttributes.SetNoOp(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesNoOp(fAttr, adjusttype, true);
end;
function TImageAttributes.ClearNoOp(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesNoOp(fAttr, adjusttype, false);
end;
function TImageAttributes.SetColorKey(colorLow, colorHigh: cardinal; adjusttype:
TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesColorKeys(fAttr, adjusttype, true,
colorLow, colorHigh);
end;
function TImageAttributes.ClearColorKey(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesColorKeys(
fAttr, adjusttype, false, 0, 0);
end;
function TImageAttributes.SetOutputChannel(channelFlags: TColorChannelFlags;
adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesOutputChannel(
fAttr, adjusttype, true, channelFlags);
end;
function TImageAttributes.ClearOutputChannel(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesOutputChannel(
fAttr, adjusttype, false, ColorChannelFlagsLast);
end;
function TImageAttributes.SetOutputChannelColorProfile(
colorProfileName: PWideChar; adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesOutputChannelColorProfile(
fAttr, adjusttype, true, colorProfileName);
end;
function TImageAttributes.ClearOutputChannelColorProfile(
adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesOutputChannelColorProfile(
fAttr, adjusttype, false, nil);
end;
function TImageAttributes.SetRemapTable(mapSize: cardinal; map: PColorMap;
adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesRemapTable(
fAttr, adjusttype, true, mapSize, map);
end;
function TImageAttributes.ClearRemapTable(adjusttype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.SetImageAttributesRemapTable(
fAttr, adjusttype, false, 0, nil);
end;
function TImageAttributes.SetWrapMode(wrap: TWrapMode; color: cardinal;
clamp: Boolean): TGdipStatus;
begin
result := _Gdip.SetImageAttributesWrapMode(fAttr, wrap, color, clamp);
end;
function TImageAttributes.GetAdjustedPalette(colorPalette: PColorPalette;
colortype: TColorAdjustType): TGdipStatus;
begin
result := _Gdip.GetImageAttributesAdjustedPalette(
fAttr, colorPalette, colortype);
end;
{ ****************** TGdiPlus class for Direct Access to the GDI+ Library }
{ TGdiPlus }
{$ifdef GDIPLUS_USEENCODERS}
type
ImageCodecInfo = record
Clsid: TGuid;
FormatID: TGuid;
CodecName: PWCHAR;
DllName: PWCHAR;
FormatDescription: PWCHAR;
FilenameExtension: PWCHAR;
MimeType: PWCHAR;
flags: DWORD;
Version: DWORD;
SigCount: DWORD;
SigSize: DWORD;
SigPattern: PBYTE;
SigMask: PBYTE;
end;
TImageCodecInfo = ImageCodecInfo;
PImageCodecInfo = ^TImageCodecInfo;
TImageCodecInfoArray = array[byte] of TImageCodecInfo;
function StrWideAnsiComp(W: PWord; A: PByte): integer;
begin
// to avoid widestring usage + compatibility with Delphi 2009/2010/XE
if pointer(W) <> pointer(A) then
if W <> nil then
if A <> nil then
begin
if W^ = A^ then
repeat
if W^ = 0 then
break;
inc(W);
inc(A);
until W^ <> A^;
result := W^ - A^;
exit;
end
else
result := 1
else // A=''
result := -1
else // W=''
result := 0; // W=A
end;
function TGdiPlus.GetEncoderClsid(format: PAnsiChar; out pClsid: TGuid): PtrInt;
var
num, size: cardinal;
ImageCodecInfo: AnsiString;
P: ^TImageCodecInfoArray;
begin
num := 0; // number of image encoders
size := 0; // size of the image encoder array in bytes
result := -1;
if (GetImageEncodersSize(num, size) <> stOk) or
(size = 0) then
exit;
SetLength(ImageCodecInfo, size);
P := pointer(ImageCodecInfo);
if GetImageEncoders(num, size, P) <> stOk then
exit;
for result := 0 to num - 1 do
if StrWideAnsiComp(pointer(P^[result].MimeType), pointer(format)) = 0 then
begin
pClsid := P^[result].Clsid;
exit;
end;
result := -1;
end;
{$endif GDIPLUS_USEENCODERS}
const
GDIP_API_NAME: array[ 0..103
{$ifdef GDIPLUS_USEDPI} + 1 {$endif GDIPLUS_USEDPI}
{$ifdef GDIPLUS_USEENCODERS} + 2 {$endif GDIPLUS_USEENCODERS} ] of PAnsiChar = (
'GdiplusStartup',
'GdiplusShutdown',
'GdipDeleteGraphics',
'GdipCreateFromHDC',
'GdipLoadImageFromStream',
'GdipLoadImageFromFile',
'GdipDrawImageRectI',
'GdipDrawImageRectRectI',
{$ifdef GDIPLUS_USEDPI}
'GdipDrawImageI',
{$endif GDIPLUS_USEDPI}
'GdipDisposeImage',
'GdipGetImageRawFormat',
'GdipGetImageWidth',
'GdipGetImageHeight',
'GdipSaveImageToStream',
{$ifdef GDIPLUS_USEENCODERS}
'GdipGetImageEncodersSize',
'GdipGetImageEncoders',
{$endif GDIPLUS_USEENCODERS}
'GdipCreateBitmapFromHBITMAP',
'GdipCreateBitmapFromGdiDib',
'GdipBitmapSetResolution',
'GdipImageGetFrameCount',
'GdipImageSelectActiveFrame',
'GdipCreateImageAttributes',
'GdipCloneImageAttributes',
'GdipDisposeImageAttributes',
'GdipSetImageAttributesToIdentity',
'GdipResetImageAttributes',
'GdipSetImageAttributesColorMatrix',
'GdipSetImageAttributesThreshold',
'GdipSetImageAttributesGamma',
'GdipSetImageAttributesNoOp',
'GdipSetImageAttributesColorKeys',
'GdipSetImageAttributesOutputChannel',
'GdipSetImageAttributesOutputChannelColorProfile',
'GdipSetImageAttributesRemapTable',
'GdipSetImageAttributesWrapMode',
'GdipGetImageAttributesAdjustedPalette',
'GdipDrawLineI',
'GdipCreatePen1',
'GdipDeletePen',
'GdipFlush',
'GdipSetSmoothingMode',
'GdipSetTextRenderingHint',
'GdipSetPenBrushFill',
'GdipSetPenColor',
'GdipSetPenWidth',
'GdipDeleteBrush',
'GdipCreateSolidFill',
'GdipFillRectangleI',
'GdipFillEllipseI',
'GdipDrawEllipseI',
'GdipDrawCurveI',
'GdipGraphicsClear',
'GdipSetPageUnit',
'GdipDrawRectangleI',
'GdipSetPenDashStyle',
'GdipDrawPolygonI',
'GdipFillPolygonI',
'GdipSetWorldTransform',
'GdipGetWorldTransform',
'GdipCreateMatrix',
'GdipCreateMatrix2',
'GdipDeleteMatrix',
'GdipSetMatrixElements',
'GdipMultiplyMatrix',
'GdipScaleMatrix',
'GdipTranslateMatrix',
'GdipDrawLinesI',
'GdipRecordMetafileI',
'GdipRecordMetafileStreamI',
'GdipPlayMetafileRecord',
'GdipEnumerateMetafileDestRectI',
'GdipResetWorldTransform',
'GdipRotateWorldTransform',
'GdipTranslateWorldTransform',
'GdipGetImageGraphicsContext',
'GdipCreateFontFromDC',
'GdipDeleteFont',
'GdipCreateFontFromLogfontW',
'GdipDrawString',
'GdipMeasureString',
'GdipDrawDriverString',
'GdipCreatePath',
'GdipDeletePath',
'GdipDrawPath',
'GdipFillPath',
'GdipAddPathLineI',
'GdipAddPathLine2I',
'GdipAddPathArcI',
'GdipAddPathCurveI',
'GdipAddPathClosedCurveI',
'GdipAddPathEllipseI',
'GdipAddPathPolygonI',
'GdipAddPathRectangleI',
'GdipClosePathFigure',
'GdipDrawArcI',
'GdipDrawBezierI',
'GdipDrawPieI',
'GdipCreateBitmapFromScan0',
'GdipBitmapLockBits',
'GdipBitmapUnlockBits',
'GdipGetClip',
'GdipSetClipRegion',
'GdipSetClipRectI',
'GdipResetClip',
'GdipCreateRegion',
'GdipDeleteRegion',
nil);
Office2003Version = $B0000; // Office 2003 = Office 11 ($B)
constructor TGdiPlus.Create(aDllFileName: TFileName);
var
Input: record
Version: integer; // Must be one
DebugEventCallback: pointer; // Only for debug builds
SuppressBackgroundThread: Bool; // true if replacing GDI+ background processing
SuppressExternalCodecs: Bool; // true if only using internal codecs
end;
{$ifndef WIN64}
i: PtrInt;
{$endif WIN64}
{$ifdef GDIPLUS_USEENCODERS}
fmt: TGdipPictureType;
{$endif GDIPLUS_USEENCODERS}
begin
InitializeCriticalSection(fLock);
// first try and search the best library name
if (aDllFileName = '') or
not FileExists(aDllFileName) then
begin
// first try and search gdiplus11.dll / gdiplus.dll in the same directory
aDllFileName := Executable.ProgramFilePath + 'gdiplus11.dll';
if not FileExists(aDllFileName) then
aDllFileName := Executable.ProgramFilePath + 'gdiplus.dll';