This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Vcl.Graphics.pas
1703 lines (1526 loc) · 44.4 KB
/
EvilWorks.Vcl.Graphics.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit EvilWorks.Vcl.Graphics;
interface
uses
Winapi.Windows,
System.Classes,
System.SysUtils,
Vcl.Graphics,
Vcl.Imaging.PNGImage,
EvilWorks.System.SysUtils;
type
// These first two structs represent how the icon information is stored
// when it is bound into a EXE or DLL file. Structure members are WORD
// aligned and the last member of the structure is the ID instead of
// the imageoffset.
{$ALIGN 2}
TGrpIconDirEntry = record
bWidth: BYTE; // Width of the image
bHeight: BYTE; // Height of the image (times 2)
bColorCount: BYTE; // Number of colors in image (0 if >=8bpp)
bReserved: BYTE; // Reserved
wPlanes: WORD; // Color Planes
wBitCount: WORD; // Bits per pixel
dwBytesInRes: DWORD; // how many bytes in this resource?
nID: WORD; // the ID
end;
PGrpIconDirEntry = ^TGrpIconDirEntry;
TGrpIconDir = record
idReserved: WORD; // Reserved
idType: WORD; // resource type (1 for icons)
idCount: WORD; // how many images?
idEntries: array [0 .. 0] of TGrpIconDirEntry; // the entries for each image
end;
PGrpIconDir = ^TGrpIconDir;
{$ALIGN 8}
// These next two structs represent how the icon information is stored
// in an ICO file.
TIconDirEntry = record
bWidth: BYTE; // Width of the image
bHeight: BYTE; // Height of the image (times 2)
bColorCount: BYTE; // Number of colors in image (0 if >=8bpp)
bReserved: BYTE; // Reserved
wPlanes: WORD; // Color Planes
wBitCount: WORD; // Bits per pixel
dwBytesInRes: DWORD; // how many bytes in this resource?
dwImageOffset: DWORD; // where in the file is this image
end;
PIconDirEntry = ^TIconDirEntry;
TIconDir = record
idReserved: WORD; // Reserved
idType: WORD; // resource type (1 for icons)
idCount: WORD; // how many images?
idEntries: array [0 .. 0] of TIconDirEntry; // the entries for each image
end;
PIconDir = TIconDir;
{ Forward declarations }
TIconImage = class;
TIconDirectory = class;
TIconList = class;
{ TIconImage }
{ Contains one bitmap for an icon file. }
TIconImage = class
private
FHandle : HICON;
FWidth : integer;
FBitDepth: WORD;
FHeight : integer;
FSize : cardinal;
public
constructor Create;
destructor Destroy; override;
procedure Draw(const aDC: HDC; const aX, aY: integer);
function Duplicate: HICON;
function To32BitBitmap: HBITMAP;
property Handle: HICON read FHandle;
property Width: integer read FWidth;
property Height: integer read FHeight;
property BitDepth: WORD read FBitDepth;
property Size: cardinal read FSize;
end;
{ TIconDirectory }
{ Contains all bitmaps for a complete icon file. }
TIconDirectory = class
private
FImages: array of TIconImage;
FCount : integer;
function GetImage(aIndex: integer): TIconImage;
protected
function ReadIconHeader(const aFile: HFILE): cardinal;
function Add: TIconImage;
procedure Clear;
public
constructor Create;
destructor Destroy; override;
function GetFormat(const aWidth, aHeight, aBitDepth: integer): HICON;
function To32BitBitmap(const aWidth, aHeight, aBitDepth: integer): HBITMAP;
function LoadFromIco(const aFileName: string): boolean;
property Images[aIndex: integer]: TIconImage read GetImage; default;
property Count: integer read FCount;
end;
{ TIconList }
{ Extracts icons from executables and contains a list of extracted icons. }
TIconList = class
private
FResourceNames: array of string;
FResourceCount: integer;
FIcons: array of TIconDirectory;
FCount: integer;
function GetIcon(aIndex: integer): TIconDirectory;
protected
class function EnumResourceNamesProc(aModule: HINST; aType, aName: PChar; aLParam: LPARAM): BOOL; stdcall; static;
function Add: TIconDirectory;
procedure Clear;
public
constructor Create; overload;
destructor Destroy; override;
function LoadFromIco(const aFileName: string): boolean;
function LoadFromExe(const aFileName: string; const aIconIndex, aCount: integer): boolean;
property Icons[aIndex: integer]: TIconDirectory read GetIcon; default;
property Count: integer read FCount;
end;
{ Icon functions }
function IconTo32BitBitmap(const aIcon: HICON; const aSize: cardinal): HBITMAP;
{ Bitmap functions }
function LoadBitmapFromBuffer(const aBuffer: pointer; const aBufferSize: cardinal; const aPremultiplyAlpha: boolean = False): HBITMAP;
function LoadBitmapFromFile(const aFileName: string; const aPremultiplyAlpha: boolean = False): HBITMAP;
function SaveBitmapToBuffer(const aBitmap: HBITMAP; out aBuffer: pointer): cardinal;
function SaveBitmapToFile(const aBitmap: HBITMAP; const aFileName: string): boolean;
function IsBitmapValid(const aBitmap: HBITMAP): boolean;
function DuplicateBitmap(const aBitmap: HBITMAP): HBITMAP;
function GetBitmapSize(const aBitmap: HBITMAP; var aSize: TSize): boolean;
function GetBitmapBitDepth(const aBitmap: HBITMAP): WORD;
function GetBitmapInfo(const aBitmap: HBITMAP; var aBitmapInfo: TBitmapInfoHeader): boolean;
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY: integer; const aBlend: boolean = False): boolean; overload;
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY, aDestCX, aDestCY: integer; const aBlend: boolean = False): boolean; overload;
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY, aDestCX, aDestCY, aSrcX, aSrcY: integer; const aBlend: boolean = False): boolean; overload;
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY, aDestCX, aDestCY, aSrcX, aSrcY, aSrcCX, aSrcCY: integer; const aBlend: boolean = False): boolean; overload;
function CreateSpectrumPalette: HPALETTE;
function PremultiplyBitmapAlpha(const aBitmap: HBITMAP; const aFloat: boolean = False): boolean;
function PremultiplyBitmapBits(const aBits: pointer; const aBitsCount: cardinal; const aFloat: boolean = False): boolean;
function CreateBitmap(const aWidth, aHeight: cardinal; const aBits: WORD): HBITMAP;
function Create32BitBitmap(aWidth, aHeight: integer): HBITMAP; overload;
function Create32BitBitmap(const aWidth, aHeight: integer; out aBits: pointer): HBITMAP; overload;
function GetBitmapBits(const aBitmap: HBITMAP; var aBits: pointer): integer;
function PNGtoBitmap(aPNG: TPNGImage): HBITMAP; overload;
function PNGtoBitmap(const aFileName: string): HBITMAP; overload;
procedure PremultiplyBitmap(aBitmap: TBitmap);
implementation
procedure _RGBTripleToQuad(var ColorTable);
type
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array [BYTE] of TRGBTriple;
PRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = array [BYTE] of TRGBQuad;
var
I : integer;
P3: PRGBTripleArray;
P4: PRGBQuadArray;
begin
P3 := PRGBTripleArray(@ColorTable);
P4 := pointer(P3);
for I := 255 downto 1 do // don't move zeroth item
with P4^[I], P3^[I] do
begin // order is significant for last item moved
rgbRed := rgbtRed;
rgbGreen := rgbtGreen;
rgbBlue := rgbtBlue;
rgbReserved := 0;
end;
P4^[0].rgbReserved := 0;
end;
// convert RGB to BGR and vice-versa. TRGBQuad <-> TPaletteEntry
procedure _ByteSwapColors(var Colors; Count: integer);
var
localCPU: integer;
begin
localCPU := Test8086;
asm
MOV EDX, Colors
MOV ECX, Count
DEC ECX
JS @@END
CMP localCPU, CPUi386
JLE @@386
@@1: MOV EAX, [EDX+ECX*4]
BSWAP EAX
SHR EAX,8
MOV [EDX+ECX*4],EAX
DEC ECX
JNS @@1
JMP @@END
@@386:
PUSH EBX
@@2: XOR EBX,EBX
MOV EAX, [EDX+ECX*4]
MOV BH, AL
MOV BL, AH
SHR EAX,16
SHL EBX,8
MOV BL, AL
MOV [EDX+ECX*4],EBX
DEC ECX
JNS @@2
POP EBX
@@END:
end;
end;
function _PaletteFromDIBColorTable(DIBHandle: THandle; ColorTable: pointer; ColorCount: integer): HPALETTE;
var
DC : HDC;
Save: THandle;
Pal : TMaxLogPalette;
begin
Result := 0;
Pal.palVersion := $300;
if DIBHandle <> 0 then
begin
DC := CreateCompatibleDC(0);
Save := SelectObject(DC, DIBHandle);
Pal.palNumEntries := GetDIBColorTable(DC, 0, 256, Pal.palPalEntry);
SelectObject(DC, Save);
DeleteDC(DC);
end
else
begin
Pal.palNumEntries := ColorCount;
Move(ColorTable^, Pal.palPalEntry, ColorCount * 4);
end;
if Pal.palNumEntries = 0 then
Exit;
if (Pal.palNumEntries <> 16) then
_ByteSwapColors(Pal.palPalEntry, Pal.palNumEntries);
Result := CreatePalette(PLogPalette(@Pal)^);
end;
function _BytesPerScanline(PixelsPerScanline, BitsPerPixel, Alignment: Longint): Longint;
begin
DEC(Alignment);
Result := ((PixelsPerScanline * BitsPerPixel) + Alignment) and not Alignment;
Result := Result div 8;
end;
function _BytesPerLine(const aWidth, aBPP: DWORD): WORD;
begin
Result := WORD(((DWORD(aWidth) * DWORD(aBPP) + 31) shr 5) shl 2);
end;
function _BitmapImageBitsSize(const aBitmapInfo: PBitmapInfo): cardinal;
begin
case aBitmapInfo^.bmiHeader.biCompression of
BI_RLE4, BI_RLE8:
begin
Result := aBitmapInfo^.bmiHeader.biSizeImage;
end;
BI_RGB, BI_BITFIELDS:
begin
Result := _BytesPerLine(aBitmapInfo^.bmiHeader.biWidth, aBitmapInfo^.bmiHeader.biBitCount * aBitmapInfo^.bmiHeader.biPlanes) * aBitmapInfo^.bmiHeader.biHeight;
end;
else
begin
Result := 0;
end;
end; { case }
end;
function _BitmapColorTableSize(const aBitmapInfo: PBitmapInfo): DWORD;
var
Colors: DWORD;
begin
if (aBitmapInfo^.bmiHeader.biClrUsed <> 0) then
Colors := aBitmapInfo^.bmiHeader.biClrUsed
else
if (aBitmapInfo^.bmiHeader.biBitCount > 8) then
Colors := 0
else
Colors := 1 shl (aBitmapInfo^.bmiHeader.biBitCount * aBitmapInfo^.bmiHeader.biPlanes);
if (aBitmapInfo^.bmiHeader.biCompression = BI_BITFIELDS) then
Exit((SizeOf(DWORD) * 3) + (Colors * SizeOf(TRGBQuad)));
Result := Colors * SizeOf(TRGBQuad);
end;
function IconTo32BitBitmap(const aIcon: HICON; const aSize: cardinal): HBITMAP;
var
MainDC : HDC;
TempDC : HDC;
OutBits: pointer;
function HasAlphaInternal(const aBits: pointer; const aBitsCount: cardinal): boolean;
var
P: PRGBQuad;
c: cardinal;
begin
Result := False;
P := aBits;
c := aBitsCount;
while (c > 0) do
begin
if (P.rgbReserved <> 0) then
begin
Result := True;
Exit;
end
else
begin
Inc(P);
DEC(c);
end;
end;
end;
var
MaskBitmap : HBITMAP;
MaskBitmapBits: pointer;
c : cardinal;
PC : PRGBQuad;
PM : PRGBQuad;
begin
Result := 0;
if (aIcon = 0) then
Exit;
MainDC := GetDC(0);
if (MainDC <> 0) then
begin
TempDC := CreateCompatibleDC(MainDC);
ReleaseDC(0, MainDC);
end
else
Exit;
if (TempDC <> 0) then
begin
Result := Create32BitBitmap(aSize, aSize, OutBits);
SelectObject(TempDC, Result);
DrawIconEx(TempDC, 0, 0, aIcon, aSize, aSize, 0, 0, DI_NORMAL);
SelectObject(TempDC, 0);
if (HasAlphaInternal(OutBits, aSize * aSize) = False) then
begin
MaskBitmap := Create32BitBitmap(aSize, aSize, MaskBitmapBits);
if (MaskBitmap <> 0) then
begin
SelectObject(TempDC, MaskBitmap);
DrawIconEx(TempDC, 0, 0, aIcon, aSize, aSize, 0, 0, DI_MASK);
SelectObject(TempDC, 0);
PC := OutBits;
PM := MaskBitmapBits;
c := aSize * aSize;
while (c > 0) do
begin
if (cardinal(PM^) and $FFFFFF00 <> 0) then
RGBQUAD(PC^).rgbReserved := 0
else
RGBQUAD(PC^).rgbReserved := 255;
Inc(PC);
Inc(PM);
DEC(c);
end;
DeleteObject(MaskBitmap);
end;
end;
DeleteDC(TempDC);
end;
end;
function LoadBitmapFromBuffer(const aBuffer: pointer; const aBufferSize: cardinal; const aPremultiplyAlpha: boolean): HBITMAP;
const
DIBPalSizes: array [boolean] of BYTE = (SizeOf(TRGBQuad), SizeOf(TRGBTriple));
var
P : PByte;
ImageSize : cardinal;
DC, MemDC : HDC;
BitsMem : pointer;
OS2Header : TBitmapCoreHeader;
PBI : PBitmapInfo;
ColorTable: pointer;
HeaderSize: DWORD;
OS2Format : boolean;
OldBMP : HBITMAP;
Pal : HPALETTE;
OldPal : HPALETTE;
BFH : TBitmapFileHeader;
begin
// Initialize variables.
Result := 0;
P := aBuffer;
ImageSize := aBufferSize;
// Read in Bitmap file header if present.
CopyMemory(@BFH, P, SizeOf(TBitmapFileHeader));
if (BFH.bfType = $4D42) then
Inc(P, SizeOf(TBitmapFileHeader));
// Get Bitmap header size.
HeaderSize := TBitmapCoreHeader(pointer(P)^).bcSize;
// Determine type of bitmap.
OS2Format := (HeaderSize = SizeOf(OS2Header));
if (OS2Format) then
HeaderSize := SizeOf(TBitmapInfoHeader);
// Get enough memory for Bitmap header and palette entries
// no matter what the Bitmap bit depth or format is.
// 3 quads for bitfields and 256 quads for palette.
GetMem(PBI, HeaderSize + (3 * SizeOf(TRGBQuad)) + (256 * SizeOf(TRGBQuad)));
if (PBI <> nil) then
begin
if (OS2Format) then
begin
// Convert OS2 DIB to Win DIB.
CopyMemory(@OS2Header, P, SizeOf(OS2Header));
Inc(P, SizeOf(OS2Header));
FillChar(PBI^.bmiHeader, SizeOf(PBI^.bmiHeader), 0);
PBI^.bmiHeader.biWidth := OS2Header.bcWidth;
PBI^.bmiHeader.biHeight := OS2Header.bcHeight;
PBI^.bmiHeader.biPlanes := OS2Header.bcPlanes;
PBI^.bmiHeader.biBitCount := OS2Header.bcBitCount;
DEC(ImageSize, SizeOf(OS2Header));
end
else
begin
// Support bitmap headers larger than TBitmapInfoHeader
// such as PBitmapV4Header, PBitmapV5Header.
CopyMemory(PBI, P, HeaderSize);
Inc(P, HeaderSize);
DEC(ImageSize, HeaderSize);
end;
PBI^.bmiHeader.biSize := HeaderSize;
ColorTable := PByte(PBI) + HeaderSize;
// Check number of planes. DIBs must be 1 color plane (packed pixels).
if (PBI^.bmiHeader.biPlanes = 1) then
begin
// 3 DWORD color element bit masks (ie 888 or 565) can precede colors
// TBitmapInfoHeader sucessors include these masks in the headersize
if (HeaderSize = SizeOf(TBitmapInfoHeader)) and ((PBI^.bmiHeader.biBitCount = 16) or (PBI^.bmiHeader.biBitCount = 32)) and (PBI^.bmiHeader.biCompression = BI_BITFIELDS) then
begin
CopyMemory(ColorTable, P, (3 * SizeOf(DWORD)));
Inc(Longint(ColorTable), 3 * SizeOf(DWORD));
DEC(ImageSize, (3 * SizeOf(DWORD)));
end;
// Read the color palette.
if (PBI^.bmiHeader.biClrUsed = 0) then
begin
if (PBI^.bmiHeader.biBitCount in [1, 4, 8]) then
PBI^.bmiHeader.biClrUsed := (1 shl PBI^.bmiHeader.biBitCount)
else
PBI^.bmiHeader.biClrUsed := 0;
end;
CopyMemory(ColorTable, P, PBI^.bmiHeader.biClrUsed * DIBPalSizes[OS2Format]);
Inc(P, PBI^.bmiHeader.biClrUsed * DIBPalSizes[OS2Format]);
DEC(ImageSize, PBI^.bmiHeader.biClrUsed * DIBPalSizes[OS2Format]);
// biSizeImage can be zero. If zero or RGB, compute the size.
// top-down DIBs have negative height
if (PBI^.bmiHeader.biSizeImage = 0) or (PBI^.bmiHeader.biCompression = BI_RGB) then
PBI^.bmiHeader.biSizeImage := _BytesPerScanline(PBI^.bmiHeader.biWidth, PBI^.bmiHeader.biBitCount, 32) * Abs(PBI^.bmiHeader.biHeight);
if (PBI^.bmiHeader.biSizeImage < ImageSize) then
ImageSize := PBI^.bmiHeader.biSizeImage;
// convert OS2 color table to DIB color table.
if (OS2Format) then
_RGBTripleToQuad(ColorTable^);
// Create Bitmap.
DC := GetDC(0);
if (DC <> 0) then
begin
if ((PBI^.bmiHeader.biCompression <> BI_RGB) and (PBI^.bmiHeader.biCompression <> BI_BITFIELDS)) then
begin
GetMem(BitsMem, ImageSize);
if (BitsMem <> nil) then
begin
CopyMemory(BitsMem, P, ImageSize);
MemDC := CreateCompatibleDC(DC);
if (MemDC <> 0) then
begin
OldBMP := SelectObject(MemDC, CreateCompatibleBitmap(DC, 1, 1));
OldPal := 0;
if (PBI^.bmiHeader.biClrUsed > 0) then
begin
Pal := _PaletteFromDIBColorTable(0, ColorTable, PBI^.bmiHeader.biClrUsed);
OldPal := SelectPalette(MemDC, Pal, False);
RealizePalette(MemDC);
end;
Result := CreateDIBitmap(MemDC, PBI^.bmiHeader, CBM_INIT, BitsMem, PBI^, DIB_RGB_COLORS);
if (OldPal <> 0) then
SelectPalette(MemDC, OldPal, True);
DeleteObject(SelectObject(MemDC, OldBMP));
DeleteDC(MemDC);
end;
FreeMem(BitsMem);
end;
end
else
begin
Result := CreateDIBSection(DC, PBI^, DIB_RGB_COLORS, BitsMem, 0, 0);
CopyMemory(BitsMem, P, ImageSize);
if (PBI^.bmiHeader.biBitCount = 32) and (aPremultiplyAlpha) then
PremultiplyBitmapBits(BitsMem, ImageSize);
end;
ReleaseDC(0, DC);
end;
end;
FreeMem(PBI);
end;
end;
// Loads a aBitmap form a aFileName.
function LoadBitmapFromFile(const aFileName: string; const aPremultiplyAlpha: boolean): HBITMAP;
var
F: TFileStream;
H: TBitmapFileHeader;
P: pointer;
B: PByte;
begin
Result := 0;
F := TFileStream.Create(aFileName, fmOpenRead);
try
if (F.Read(H, SizeOf(TBitmapFileHeader)) = SizeOf(TBitmapFileHeader)) then
begin
P := GetMemory(H.bfSize);
if (P <> nil) then
begin
B := P;
CopyMemory(B, @H, SizeOf(TBitmapFileHeader));
Inc(B, SizeOf(TBitmapFileHeader));
if (F.Read(B^, F.Size - SizeOf(TBitmapFileHeader)) = F.Size - SizeOf(TBitmapFileHeader)) then
Result := LoadBitmapFromBuffer(P, cardinal(F.Size), aPremultiplyAlpha);
FreeMemory(P);
end;
end;
finally
F.Free;
end;
end;
// Saves a aBitmap to a aBuffer. Function will return the allocated buffer in aBuffer. When you are done, call
// FreeMem() on it. aBuffer might return nil if allocation failed. Result is the size of allocation on aBuffer
// or 0 if an error occurs.
function SaveBitmapToBuffer(const aBitmap: HBITMAP; out aBuffer: pointer): cardinal;
var
BFH : TBitmapFileHeader;
PBI : PBitmapInfo;
DS : DIBSECTION;
PRGB : PRGBQuad;
DC : HDC;
OldBM : HBITMAP;
Pal : HPALETTE;
PE : array [0 .. 255] of TPaletteEntry;
I : integer;
TotalBytes: DWORD;
P : PByte;
Size : cardinal;
TempQuad : array [0 .. 255] of TRGBQuad;
begin
aBuffer := nil;
Result := 0;
if (IsBitmapValid(aBitmap) = False) then
Exit;
// Get the BITMAPINFO for the DIBSection
GetObject(aBitmap, SizeOf(DS), @DS);
// load the header and the bitmasks if present
// per function comments above, we allocate space for a color
// table even if it is not needed
if (DS.dsBmih.biCompression = BI_BITFIELDS) then
begin
// has a bitmask - be sure to allocate for and copy them
PBI := GetMemory(SizeOf(TBitmapInfoHeader) + (3 * SizeOf(DWORD)) + (256 * SizeOf(TRGBQuad)));
CopyMemory(@PBI^.bmiHeader, @DS.dsBmih, SizeOf(TBitmapInfoHeader) + (3 * SizeOf(DWORD)));
PRGB := PRGBQuad(@PBI^.bmiColors[0]);
Inc(PRGB, 3);
end
else
begin
// no bitmask - just the header and color table
PBI := GetMemory(SizeOf(TBitmapInfoHeader) + (256 * SizeOf(TRGBQuad)));
CopyMemory(@PBI^.bmiHeader, @DS.dsBmih, SizeOf(TBitmapInfoHeader));
PRGB := @PBI^.bmiColors[0];
end;
// at this point, prgb points to the color table, even
// if bitmasks are present
// Now for the color table
if ((DS.dsBm.bmBitsPixel * DS.dsBm.bmPlanes) <= 8) then
begin
// the DIBSection is 256 color or less (has color table)
DC := CreateCompatibleDC(0);
OldBM := SelectObject(DC, aBitmap);
ZeroMemory(@TempQuad[0], (256 * SizeOf(TRGBQuad)));
Size := GetDIBColorTable(DC, 0, 1 shl (DS.dsBm.bmBitsPixel * DS.dsBm.bmPlanes), TempQuad);
if (Size > 0) then
CopyMemory(@PBI^.bmiColors[0], @TempQuad[0], Size * SizeOf(TRGBQuad));
SelectObject(DC, OldBM);
DeleteDC(DC);
end
else
begin
// the DIBSection is >8bpp (has no color table) so make one up
// where are we going to get the colors? from a spectrum palette
Pal := CreateSpectrumPalette;
GetPaletteEntries(Pal, 0, 256, PE);
for I := 0 to 255 do
begin
PRGB^.rgbRed := PE[I].peRed;
PRGB^.rgbGreen := PE[I].peGreen;
PRGB^.rgbBlue := PE[I].peBlue;
PRGB^.rgbReserved := 0;
Inc(PRGB);
end;
DeleteObject(Pal);
end;
// What's the total size of the DIB information (not counting file header)?
TotalBytes := _BitmapImageBitsSize(PBI) + SizeOf(TBitmapInfoHeader) + _BitmapColorTableSize(PBI);
// Construct the file header
ZeroMemory(@BFH, SizeOf(TBitmapFileHeader));
BFH.bfType := $4D42;
BFH.bfSize := TotalBytes + SizeOf(TBitmapFileHeader);
BFH.bfReserved1 := 0;
BFH.bfReserved2 := 0;
BFH.bfOffBits := SizeOf(TBitmapFileHeader) + PBI^.bmiHeader.biSize + _BitmapColorTableSize(PBI);
aBuffer := GetMemory(BFH.bfSize);
if (aBuffer <> nil) then
begin
Result := BFH.bfSize;
P := aBuffer;
CopyMemory(P, @BFH, SizeOf(TBitmapFileHeader));
Inc(P, SizeOf(TBitmapFileHeader));
Size := SizeOf(TBitmapInfoHeader) + _BitmapColorTableSize(PBI);
CopyMemory(P, PBI, Size);
Inc(P, Size);
Size := _BitmapImageBitsSize(PBI);
CopyMemory(P, DS.dsBm.bmBits, Size);
end;
FreeMem(PBI);
end;
// Saves a aBitmap to a aFileName.
function SaveBitmapToFile(const aBitmap: HBITMAP; const aFileName: string): boolean;
var
P: pointer;
c: cardinal;
F: TFileStream;
begin
Result := False;
c := SaveBitmapToBuffer(aBitmap, P);
if (c <> 0) then
begin
F := TFileStream.Create(aFileName, fmOpenWrite);
try
if (P <> nil) then
begin
if (F.Write(P^, c) = integer(c)) then
Result := True;
FreeMem(P);
end;
finally
F.Free;
end;
end;
end;
// Checks if bitmap handle is valid.
function IsBitmapValid(const aBitmap: HBITMAP): boolean;
begin
Result := (GetObjectType(aBitmap) = OBJ_BITMAP);
end;
// Duplicates aBitmap. You need to free the returned bitmap yourself.
function DuplicateBitmap(const aBitmap: HBITMAP): HBITMAP;
var
c: cardinal;
P: pointer;
begin
Result := 0;
c := SaveBitmapToBuffer(aBitmap, P);
if (c <> 0) then
begin
Result := LoadBitmapFromBuffer(P, c);
FreeMem(P);
end;
end;
// Gets aBitmap size in pixels.
function GetBitmapSize(const aBitmap: HBITMAP; var aSize: TSize): boolean;
var
BIH: TBitmapInfoHeader;
begin
Result := GetBitmapInfo(aBitmap, BIH);
if (Result) then
begin
aSize.cx := BIH.biWidth;
aSize.cy := BIH.biHeight;
end;
end;
// Gets aBitmap bit depth.
function GetBitmapBitDepth(const aBitmap: HBITMAP): WORD;
var
BIH: TBitmapInfoHeader;
begin
if (GetBitmapInfo(aBitmap, BIH)) then
Result := BIH.biBitCount
else
Result := 0;
end;
// Gets aBitmap info.
function GetBitmapInfo(const aBitmap: HBITMAP; var aBitmapInfo: TBitmapInfoHeader): boolean;
var
DC : HDC;
BI : TBitmapInfo;
Res: integer;
begin
Result := False;
DC := GetDC(0);
if (DC <> 0) then
begin
ZeroMemory(@BI, SizeOf(BI));
BI.bmiHeader.biSize := SizeOf(TBitmapInfoHeader);
Res := GetDIBits(DC, aBitmap, 0, 0, nil, BI, DIB_RGB_COLORS);
if (Res <> 0) then
begin
aBitmapInfo := BI.bmiHeader;
Result := True;
end;
ReleaseDC(0, DC);
end;
end;
// Draws aBitmap to a DeviceContext.
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY: integer; const aBlend: boolean = False): boolean; overload;
var
SrcSZ: TSize;
begin
Result := GetBitmapSize(aBitmap, SrcSZ);
if (Result) then
Result := DrawBitmap(aDC, aBitmap, aDestX, aDestY, SrcSZ.cx, SrcSZ.cy, 0, 0, SrcSZ.cx, SrcSZ.cy, aBlend);
end;
// Draws aBitmap to a DeviceContext.
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY, aDestCX, aDestCY: integer; const aBlend: boolean = False): boolean; overload;
var
SrcSZ: TSize;
begin
Result := GetBitmapSize(aBitmap, SrcSZ);
if (Result) then
Result := DrawBitmap(aDC, aBitmap, aDestX, aDestY, aDestCX, aDestCY, 0, 0, SrcSZ.cx, SrcSZ.cy, aBlend);
end;
// Draws aBitmap to a DeviceContext.
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY, aDestCX, aDestCY, aSrcX, aSrcY: integer; const aBlend: boolean = False): boolean; overload;
var
SrcSZ: TSize;
begin
Result := GetBitmapSize(aBitmap, SrcSZ);
if (Result) then
Result := DrawBitmap(aDC, aBitmap, aDestX, aDestY, aDestCX, aDestCY, aSrcX, aSrcY, SrcSZ.cx - aSrcX, SrcSZ.cy - aSrcY, aBlend);
end;
// Draws aBitmap to a DeviceContext.
function DrawBitmap(const aDC: HDC; const aBitmap: HBITMAP; const aDestX, aDestY, aDestCX, aDestCY, aSrcX, aSrcY, aSrcCX, aSrcCY: integer; const aBlend: boolean = False): boolean; overload;
var
DC: HDC;
BF: TBlendFunction;
begin
Result := False;
if (aDC = 0) or (aBitmap = 0) then
begin
SetLastError(ERROR_INVALID_PARAMETER);
Exit;
end;
DC := CreateCompatibleDC(aDC);
if (DC <> 0) then
begin
SelectObject(DC, aBitmap);
if (aBlend) and (GetBitmapBitDepth(aBitmap) = 32) then
begin
BF.BlendOp := AC_SRC_OVER;
BF.BlendFlags := 0;
BF.SourceConstantAlpha := 255;
BF.AlphaFormat := AC_SRC_ALPHA;
Result := AlphaBlend(aDC, aDestX, aDestY, aDestCX, aDestCY, DC, aSrcX, aSrcY, aSrcCX, aSrcCY, BF);
end
else
begin
Result := StretchBlt(aDC, aDestX, aDestY, aDestCX, aDestCY, DC, aSrcX, aSrcY, aSrcCX, aSrcCY, SRCCOPY);
end;
SelectObject(DC, 0);
DeleteDC(DC);
end;
end;
// Pre-multiplies aBitmap bits with its Alpha channel for proper alpha-blending drawing.
function PremultiplyBitmapAlpha(const aBitmap: HBITMAP; const aFloat: boolean = False): boolean;
var
BIH : TBitmapInfoHeader;
Bits : pointer;
Count: integer;
I : integer;
Q : PRGBQuad;
begin
Result := False;
if (aBitmap = 0) then
begin
SetLastError(ERROR_INVALID_PARAMETER);
Exit;
end;
if (GetBitmapInfo(aBitmap, BIH) = False) then
Exit;
if (BIH.biBitCount < 32) then
begin
SetLastError(ERROR_INVALID_DATATYPE);
Exit;
end;
Bits := GetMemory(BIH.biSizeImage);
if (Bits = nil) then
Exit;
Count := Winapi.Windows.GetBitmapBits(aBitmap, BIH.biSizeImage, Bits);
if (Count = 0) then
begin
FreeMemory(Bits);
Exit;
end;
Q := Bits;
I := 0;
while (I < Count) do
begin
if (aFloat) then
begin
Q^.rgbBlue := Round(Q^.rgbBlue * Q^.rgbReserved / 255);
Q^.rgbGreen := Round(Q^.rgbGreen * Q^.rgbReserved / 255);
Q^.rgbRed := Round(Q^.rgbRed * Q^.rgbReserved / 255);
end
else
begin
Q^.rgbBlue := Q^.rgbBlue * Q^.rgbReserved div 255;
Q^.rgbGreen := Q^.rgbGreen * Q^.rgbReserved div 255;
Q^.rgbRed := Q^.rgbRed * Q^.rgbReserved div 255;
end;
Inc(Q);
Inc(I, 4);
end;
Result := (SetBitmapBits(aBitmap, BIH.biSizeImage, Bits) <> 0);
FreeMemory(Bits);
end;
// Pre-multiplies Bitmap aBits with its Alpha channel for proper alpha-blending drawing.
function PremultiplyBitmapBits(const aBits: pointer; const aBitsCount: cardinal; const aFloat: boolean = False): boolean;
var
I: cardinal;
Q: PRGBQuad;
begin
Result := False;
if (aBits = nil) then
begin
SetLastError(ERROR_INVALID_PARAMETER);
Exit;
end;
Q := aBits;
I := 0;
while (I < aBitsCount) do
begin
if (aFloat) then
begin
Q^.rgbBlue := Round(Q^.rgbBlue * Q^.rgbReserved / 255);
Q^.rgbGreen := Round(Q^.rgbGreen * Q^.rgbReserved / 255);
Q^.rgbRed := Round(Q^.rgbRed * Q^.rgbReserved / 255);
end
else
begin
Q^.rgbBlue := Q^.rgbBlue * Q^.rgbReserved div 255;
Q^.rgbGreen := Q^.rgbGreen * Q^.rgbReserved div 255;
Q^.rgbRed := Q^.rgbRed * Q^.rgbReserved div 255;
end;
Inc(Q);
Inc(I, 4);
end;
Result := True;
end;
// Creates a 256 coloe default spectrum palette for 8 bit bitmaps.
function CreateSpectrumPalette: HPALETTE;
var
hPal : HPALETTE;
LogPal: PLogPalette;
Red : cardinal;
Green : cardinal;
Blue : cardinal;
I : integer;
Entry : PPaletteEntry;
begin
LogPal := GetMemory(SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
if (LogPal = nil) then
Exit(0);
LogPal^.palVersion := $300;
LogPal^.palNumEntries := 256;
Red := 0;
Green := 0;
Blue := 0;
Entry := @LogPal^.palPalEntry;
for I := 0 to 255 do
begin
Entry^.peRed := Red;
Entry^.peGreen := Green;
Entry^.peBlue := Blue;
Entry.peFlags := 0;
Inc(Entry);
{
if (!(red += 32))
if (!(green += 32))
blue += 64;
}
Red := (Red + 32) mod 256;
if (Red = 0) then
begin
Green := (Green + 32) mod 256;
if (Green = 0) then
begin
Blue := (Blue + 64) mod 256;
end;
end;
end;
hPal := CreatePalette(LogPal^);
FreeMem(LogPal);
Result := hPal;
end;