Repository navigation
Expand file tree
/
Copy pathSCBitmap.pas
More file actions
1963 lines (1826 loc) · 55.4 KB
/
Copy pathSCBitmap.pas
File metadata and controls
1963 lines (1826 loc) · 55.4 KB
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
{*******************************************************}
{ }
{ CA SweetControls Component Library }
{ }
{ Copyright (c) 1996,2004 CodeAccelerate Corporation }
{ }
{*******************************************************}
unit SCBitmap;
{$I SweetControls.inc}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, SCCommon;
type
TSCBitmapImage = class(TSharedImage)
private
FHandle: HBITMAP;
FMaskHandle: HBITMAP;
FPalette: HPALETTE;
FDIBHandle: HBITMAP;
FDIB: TDIBSection;
FSaveStream: TMemoryStream;
FOS2Format: Boolean;
FHalftone: Boolean;
protected
procedure FreeHandle; override;
public
destructor Destroy; override;
end;
TSCBitmap = class(TGraphic)
private
FImage: TSCBitmapImage;
FCanvas: TCanvas;
FIgnorePalette: Boolean;
FMaskBitsValid: Boolean;
FMaskValid: Boolean;
FTransparentColor: TColor;
FTransparentMode: TTransparentMode;
procedure Changing(Sender: TObject);
procedure CopyImage(AHandle: HBITMAP; APalette: HPALETTE; DIB: TDIBSection);
procedure DIBNeeded;
procedure FreeContext;
function GetCanvas: TCanvas;
function GetHandle: HBITMAP; virtual;
function GetHandleType: TBitmapHandleType;
function GetMaskHandle: HBITMAP; virtual;
function GetMonochrome: Boolean;
function GetPixelFormat: TPixelFormat;
function GetScanline(Row: Integer): Pointer;
function GetTransparentColor: TColor;
procedure NewImage(NewHandle: HBITMAP; NewPalette: HPALETTE;
const NewDIB: TDIBSection; OS2Format: Boolean; RLEStream: TStream = nil);
procedure ReadStream(Stream: TStream; Size: Longint);
procedure ReadDIB(Stream: TStream; ImageSize: LongWord);
procedure SetHandle(Value: HBITMAP);
procedure SetHandleType(Value: TBitmapHandleType); virtual;
procedure SetMaskHandle(Value: HBITMAP);
procedure SetMonochrome(Value: Boolean);
procedure SetPixelFormat(Value: TPixelFormat);
procedure SetTransparentColor(Value: TColor);
procedure SetTransparentMode(Value: TTransparentMode);
function TransparentColorStored: Boolean;
procedure WriteStream(Stream: TStream; WriteSize: Boolean);
protected
procedure Changed(Sender: TObject); override;
procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
function GetEmpty: Boolean; override;
function GetHeight: Integer; override;
function GetPalette: HPALETTE; override;
function GetWidth: Integer; override;
procedure HandleNeeded;
procedure MaskHandleNeeded;
procedure PaletteNeeded;
procedure ReadData(Stream: TStream); override;
procedure SetHeight(Value: Integer); override;
procedure SetPalette(Value: HPALETTE); override;
procedure SetWidth(Value: Integer); override;
procedure WriteData(Stream: TStream); override;
public
constructor Create; override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Dormant;
procedure FreeImage;
procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
APalette: HPALETTE); override;
procedure LoadFromStream(Stream: TStream); override;
procedure LoadFromResourceName(Instance: THandle; const ResName: String);
procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
procedure Mask(TransparentColor: TColor);
function ReleaseHandle: HBITMAP;
function ReleaseMaskHandle: HBITMAP;
function ReleasePalette: HPALETTE;
procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
var APalette: HPALETTE); override;
procedure SaveToStream(Stream: TStream); override;
procedure SetBounds(AWidth, AHeight: Integer); dynamic;
property Canvas: TCanvas read GetCanvas;
property Handle: HBITMAP read GetHandle write SetHandle;
property HandleType: TBitmapHandleType read GetHandleType write SetHandleType;
property IgnorePalette: Boolean read FIgnorePalette write FIgnorePalette;
property MaskHandle: HBITMAP read GetMaskHandle write SetMaskHandle;
property Monochrome: Boolean read GetMonochrome write SetMonochrome;
property PixelFormat: TPixelFormat read GetPixelFormat write SetPixelFormat;
property ScanLine[Row: Integer]: Pointer read GetScanLine;
property TransparentColor: TColor read GetTransparentColor
write SetTransparentColor stored TransparentColorStored;
property TransparentMode: TTransparentMode read FTransparentMode
write SetTransparentMode default tmAuto;
end;
implementation
uses Consts;
var
BitmapImageLock: TRTLCriticalSection;
BitmapCanvasList: TThreadList = nil;
{ TSCBitmap }
type
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array [Byte] of TRGBTriple;
PRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = array [Byte] of TRGBQuad;
TSCFakeCanvas = class(TCanvas);
{ TSCBitmapCanvas }
{ Create a canvas that gets its DC from the memory DC cache }
TSCBitmapCanvas = class(TCanvas)
private
FHandle: HDC;
FBitmap: TSCBitmap;
FOldBitmap: HBITMAP;
FOldPalette: HPALETTE;
procedure FreeContext;
protected
procedure CreateHandle; override;
public
constructor Create(ABitmap: TSCBitmap);
destructor Destroy; override;
end;
const
csAllValid = [csHandleValid..csBrushValid];
procedure InvalidOperation(const Str: String);
begin
raise EInvalidGraphicOperation.Create(Str);
end;
procedure InvalidGraphic(const Str: String);
begin
raise EInvalidGraphic.Create(Str);
end;
procedure OutOfResources;
begin
raise EOutOfResources.Create(SOutOfResources);
end;
procedure InvalidBitmap;
begin
InvalidGraphic(SInvalidBitmap);
end;
function GetDInColors(BitCount: Word): Integer;
begin
case BitCount of
1, 4, 8: Result := 1 shl BitCount;
else
Result := 0;
end;
end;
function BytesPerScanline(PixelsPerScanline, BitsPerPixel, Alignment: Longint): Longint;
begin
Dec(Alignment);
Result := ((PixelsPerScanline * BitsPerPixel) + Alignment) and not Alignment;
Result := Result div 8;
end;
procedure InternalDeletePalette(Pal: HPalette);
begin
if (Pal <> 0) and (Pal <> SystemPalette16) then
DeleteObject(Pal);
end;
procedure InitializeBitmapInfoHeader(Bitmap: HBITMAP; var BI: TBitmapInfoHeader;
Colors: Integer);
var
DS: TDIBSection;
Bytes: Integer;
begin
DS.dsbmih.biSize := 0;
Bytes := GetObject(Bitmap, SizeOf(DS), @DS);
if Bytes = 0 then InvalidBitmap
else if (Bytes >= (sizeof(DS.dsbm) + sizeof(DS.dsbmih))) and
(DS.dsbmih.biSize >= DWORD(sizeof(DS.dsbmih))) then
BI := DS.dsbmih
else
begin
FillChar(BI, sizeof(BI), 0);
with BI, DS.dsbm do
begin
biSize := SizeOf(BI);
biWidth := bmWidth;
biHeight := bmHeight;
end;
end;
case Colors of
2: BI.biBitCount := 1;
3..16:
begin
BI.biBitCount := 4;
BI.biClrUsed := Colors;
end;
17..256:
begin
BI.biBitCount := 8;
BI.biClrUsed := Colors;
end;
else
BI.biBitCount := DS.dsbm.bmBitsPixel * DS.dsbm.bmPlanes;
end;
BI.biPlanes := 1;
if BI.biClrImportant > BI.biClrUsed then
BI.biClrImportant := BI.biClrUsed;
if BI.biSizeImage = 0 then
BI.biSizeImage := BytesPerScanLine(BI.biWidth, BI.biBitCount, 32) * Abs(BI.biHeight);
end;
procedure InternalGetDIBSizes(Bitmap: HBITMAP; var InfoHeaderSize: DWORD;
var ImageSize: DWORD; Colors: Integer);
var
BI: TBitmapInfoHeader;
begin
InitializeBitmapInfoHeader(Bitmap, BI, Colors);
if BI.biBitCount > 8 then
begin
InfoHeaderSize := SizeOf(TBitmapInfoHeader);
if (BI.biCompression and BI_BITFIELDS) <> 0 then
Inc(InfoHeaderSize, 12);
end
else
if BI.biClrUsed = 0 then
InfoHeaderSize := SizeOf(TBitmapInfoHeader) +
SizeOf(TRGBQuad) * (1 shl BI.biBitCount)
else
InfoHeaderSize := SizeOf(TBitmapInfoHeader) +
SizeOf(TRGBQuad) * BI.biClrUsed;
ImageSize := BI.biSizeImage;
end;
{ RGBTripleToQuad performs in-place conversion of an OS2 color
table into a DIB color table. }
procedure RGBTripleToQuad(var ColorTable);
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;
{ RGBQuadToTriple performs the inverse of RGBTripleToQuad. }
procedure RGBQuadToTriple(var ColorTable; var ColorCount: Integer);
var
I: Integer;
P3: PRGBTripleArray;
P4: PRGBQuadArray;
begin
P3 := PRGBTripleArray(@ColorTable);
P4 := Pointer(P3);
for I := 1 to ColorCount-1 do // don't move zeroth item
with P4^[I], P3^[I] do
begin
rgbtRed := rgbRed;
rgbtGreen := rgbGreen;
rgbtBlue := rgbBlue;
end;
if ColorCount < 256 then
begin
FillChar(P3^[ColorCount], (256 - ColorCount) * sizeof(TRGBTriple), 0);
ColorCount := 256; // OS2 color tables always have 256 entries
end;
end;
procedure ByteSwapColors(var Colors; Count: Integer);
var // convert RGB to BGR and vice-versa. TRGBQuad <-> TPaletteEntry
SysInfo: TSystemInfo;
begin
GetSystemInfo(SysInfo);
asm
MOV EDX, Colors
MOV ECX, Count
DEC ECX
JS @@END
LEA EAX, SysInfo
CMP [EAX].TSystemInfo.wProcessorLevel, 3
JE @@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 SystemPaletteOverride(var Pal: TMaxLogPalette): Boolean;
var
DC: HDC;
SysPalSize: Integer;
begin
Result := False;
if SystemPalette16 <> 0 then
begin
DC := GetDC(0);
try
SysPalSize := GetDeviceCaps(DC, SIZEPALETTE);
if SysPalSize >= 16 then
begin
{ Ignore the disk image of the palette for 16 color bitmaps.
Replace with the first and last 8 colors of the system palette }
GetPaletteEntries(SystemPalette16, 0, 8, Pal.palPalEntry);
GetPaletteEntries(SystemPalette16, 8, 8, Pal.palPalEntry[Pal.palNumEntries - 8]);
Result := True;
end
finally
ReleaseDC(0,DC);
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) or not SystemPaletteOverride(Pal) then
ByteSwapColors(Pal.palPalEntry, Pal.palNumEntries);
Result := CreatePalette(PLogPalette(@Pal)^);
end;
function PaletteToDIBColorTable(Pal: HPalette;
var ColorTable: array of TRGBQuad): Integer;
begin
Result := 0;
if (Pal = 0) or
(GetObject(Pal, sizeof(Result), @Result) = 0) or
(Result = 0) then Exit;
if Result > High(ColorTable)+1 then Result := High(ColorTable)+1;
GetPaletteEntries(Pal, 0, Result, ColorTable);
ByteSwapColors(ColorTable, Result);
end;
procedure UpdateDIBColorTable(DIBHandle: HBITMAP; Pal: HPalette;
const DIB: TDIBSection);
var
ScreenDC, DC: HDC;
OldBM: HBitmap;
ColorCount: Integer;
Colors: array [Byte] of TRGBQuad;
begin
if (DIBHandle <> 0) and (DIB.dsbmih.biBitCount <= 8) then
begin
ColorCount := PaletteToDIBColorTable(Pal, Colors);
if ColorCount = 0 then Exit;
ScreenDC := GetDC(0);
DC := CreateCompatibleDC(ScreenDC);
OldBM := SelectObject(DC, DIBHandle);
try
SetDIBColorTable(DC, 0, ColorCount, Colors);
finally
SelectObject(DC, OldBM);
DeleteDC(DC);
ReleaseDC(0, ScreenDC);
end;
end;
end;
procedure FixupBitFields(var DIB: TDIBSection);
begin
if (DIB.dsbmih.biCompression and BI_BITFIELDS <> 0) and
(DIB.dsBitFields[0] = 0) then
if DIB.dsbmih.biBitCount = 16 then
begin
// fix buggy 16 bit color drivers
DIB.dsBitFields[0] := $F800;
DIB.dsBitFields[1] := $07E0;
DIB.dsBitFields[2] := $001F;
end else if DIB.dsbmih.biBitCount = 32 then
begin
// fix buggy 32 bit color drivers
DIB.dsBitFields[0] := $00FF0000;
DIB.dsBitFields[1] := $0000FF00;
DIB.dsBitFields[2] := $000000FF;
end;
end;
{ DeselectBitmap is called to ensure that a bitmap handle is not
selected into any memory DC anywhere in the system. If the bitmap
handle is in use by a locked canvas, DeselectBitmap must wait for
the canvas to unlock. }
procedure DeselectBitmap(AHandle: HBITMAP);
var
I: Integer;
begin
if AHandle = 0 then Exit;
with BitmapCanvasList.LockList do
try
for I := Count - 1 downto 0 do
with TSCBitmapCanvas(Items[I]) do
if (FBitmap <> nil) and (FBitmap.FImage.FHandle = AHandle) then
FreeContext;
finally
BitmapCanvasList.UnlockList;
end;
end;
procedure GDIError;
var
ErrorCode: Integer;
Buf: array [Byte] of Char;
begin
ErrorCode := GetLastError;
if (ErrorCode <> 0) and (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil,
ErrorCode, LOCALE_USER_DEFAULT, Buf, sizeof(Buf), nil) <> 0) then
raise EOutOfResources.Create(Buf)
else
OutOfResources;
end;
function GDICheck(Value: Integer): Integer;
begin
if Value = 0 then GDIError;
Result := Value;
end;
function CopyBitmap(Handle: HBITMAP; OldPalette, NewPalette: HPALETTE;
var DIB: TDIBSection; Canvas: TCanvas): HBITMAP;
var
OldScr, NewScr: HBITMAP;
ScreenDC, NewImageDC, OldImageDC: HDC;
BI: PBitmapInfo;
BitsMem: Pointer;
SrcDIB: TDIBSection;
MonoColors: array [0..1] of Integer;
Pal1, Pal2: HPalette;
begin
Result := 0;
with DIB, dsbm, dsbmih do
begin
if (biSize <> 0) and ((biWidth = 0) or (biHeight = 0)) then Exit;
if (biSize = 0) and ((bmWidth = 0) or (bmHeight = 0)) then Exit;
end;
DeselectBitmap(Handle);
SrcDIB.dsbmih.biSize := 0;
if Handle <> 0 then
if GetObject(Handle, sizeof(SrcDIB), @SrcDIB) < sizeof(SrcDIB.dsbm) then
InvalidBitmap;
ScreenDC := GDICheck(GetDC(0));
NewImageDC := GDICheck(CreateCompatibleDC(ScreenDC));
with DIB.dsbm do
try
if DIB.dsbmih.biSize < DWORD(sizeof(DIB.dsbmih)) then
if (bmPlanes or bmBitsPixel) = 1 then // monochrome
Result := GDICheck(CreateBitmap(bmWidth, bmHeight, 1, 1, nil))
else // Create DDB
Result := GDICheck(CreateCompatibleBitmap(ScreenDC, bmWidth, bmHeight))
else // Create DIB
begin
GetMem(BI, sizeof(TBitmapInfo) + 256 * sizeof(TRGBQuad));
with DIB.dsbmih do
try
biSize := sizeof(BI.bmiHeader);
biPlanes := 1;
if biBitCount = 0 then
biBitCount := GetDeviceCaps(ScreenDC, BITSPIXEL) * GetDeviceCaps(ScreenDC, PLANES);
BI.bmiHeader := DIB.dsbmih;
bmWidth := biWidth;
bmHeight := biHeight;
if (biBitCount <= 8) then
begin
if (biBitCount = 1) and (SrcDIB.dsbm.bmBits = nil) then
begin // set mono DIB to white/black when converting from DDB.
Integer(BI^.bmiColors[0]) := 0;
PInteger(Integer(@BI^.bmiColors) + sizeof(Integer))^ := $FFFFFF;
end
else if (NewPalette <> 0) then
PaletteToDIBColorTable(NewPalette, PRGBQuadArray(@BI.bmiColors)^)
else if Handle <> 0 then
begin
NewScr := SelectObject(NewImageDC, Handle);
if (SrcDIB.dsbmih.biSize > 0) and (SrcDIB.dsbm.bmBits <> nil) then
biClrUsed := GetDIBColorTable(NewImageDC, 0, 256, BI^.bmiColors)
else
GetDIBits(NewImageDC, Handle, 0, Abs(biHeight), nil, BI^, DIB_RGB_COLORS);
SelectObject(NewImageDC, NewScr);
end;
end
else if ((biBitCount = 16) or (biBitCount = 32)) and
((biCompression and BI_BITFIELDS) <> 0) then
begin
FixupBitFields(DIB);
Move(DIB.dsBitFields, BI.bmiColors, sizeof(DIB.dsBitFields));
end;
Result := GDICheck(CreateDIBSection(ScreenDC, BI^, DIB_RGB_COLORS, BitsMem, 0, 0));
if (BitsMem = nil) then GDIError;
if (Handle <> 0) and (SrcDIB.dsbm.bmWidth = biWidth) and
(SrcDIB.dsbm.bmHeight = biHeight) and (biBitCount > 8) then
begin // shortcut bitblt steps
GetDIBits(NewImageDC, Handle, 0, Abs(biHeight), BitsMem, BI^, DIB_RGB_COLORS);
Exit;
end;
finally
FreeMem(BI);
end;
end;
GDICheck(Result);
NewScr := GDICheck(SelectObject(NewImageDC, Result));
try
try
Pal1 := 0;
Pal2 := 0;
if NewPalette <> 0 then
begin
Pal1 := SelectPalette(NewImageDC, NewPalette, False);
RealizePalette(NewImageDC);
end;
try
if Canvas <> nil then
begin
FillRect(NewImageDC, Rect(0, 0, bmWidth, bmHeight),
Canvas.Brush.Handle);
SetTextColor(NewImageDC, ColorToRGB(Canvas.Font.Color));
SetBkColor(NewImageDC, ColorToRGB(Canvas.Brush.Color));
if (DIB.dsbmih.biBitCount = 1) and (DIB.dsbm.bmBits <> nil) then
begin
MonoColors[0] := ColorToRGB(Canvas.Font.Color);
MonoColors[1] := ColorToRGB(Canvas.Brush.Color);
SetDIBColorTable(NewImageDC, 0, 2, MonoColors);
end;
end
else
PatBlt(NewImageDC, 0, 0, bmWidth, bmHeight, WHITENESS);
if Handle <> 0 then
begin
OldImageDC := GDICheck(CreateCompatibleDC(ScreenDC));
try
OldScr := GDICheck(SelectObject(OldImageDC, Handle));
if OldPalette <> 0 then
begin
Pal2 := SelectPalette(OldImageDC, OldPalette, False);
RealizePalette(OldImageDC);
end;
if Canvas <> nil then
begin
SetTextColor(OldImageDC, ColorToRGB(Canvas.Font.Color));
SetBkColor(OldImageDC, ColorToRGB(Canvas.Brush.Color));
end;
BitBlt(NewImageDC, 0, 0, bmWidth, bmHeight, OldImageDC, 0, 0, SRCCOPY);
if OldPalette <> 0 then
SelectPalette(OldImageDC, Pal2, True);
GDICheck(SelectObject(OldImageDC, OldScr));
finally
DeleteDC(OldImageDC);
end;
end;
finally
if NewPalette <> 0 then
SelectPalette(NewImageDC, Pal1, True);
end;
finally
SelectObject(NewImageDC, NewScr);
end;
except
DeleteObject(Result);
raise;
end;
finally
DeleteDC(NewImageDC);
ReleaseDC(0, ScreenDC);
if (Result <> 0) then GetObject(Result, sizeof(DIB), @DIB);
end;
end;
function CopyPalette(Palette: HPALETTE): HPALETTE;
var
PaletteSize: Integer;
LogPal: TMaxLogPalette;
begin
Result := 0;
if Palette = 0 then Exit;
PaletteSize := 0;
if GetObject(Palette, SizeOf(PaletteSize), @PaletteSize) = 0 then Exit;
if PaletteSize = 0 then Exit;
with LogPal do
begin
palVersion := $0300;
palNumEntries := PaletteSize;
GetPaletteEntries(Palette, 0, PaletteSize, palPalEntry);
end;
Result := CreatePalette(PLogPalette(@LogPal)^);
end;
function CopyBitmapAsMask(Handle: HBITMAP; Palette: HPALETTE;
TransparentColor: TColorRef): HBITMAP;
var
DIB: TDIBSection;
ScreenDC, BitmapDC, MonoDC: HDC;
BkColor: TColorRef;
Remove: Boolean;
SaveBitmap, SaveMono: HBITMAP;
begin
Result := 0;
if (Handle <> 0) and (GetObject(Handle, SizeOf(DIB), @DIB) <> 0) then
begin
DeselectBitmap(Handle);
ScreenDC := 0;
MonoDC := 0;
try
ScreenDC := GDICheck(GetDC(0));
MonoDC := GDICheck(CreateCompatibleDC(ScreenDC));
with DIB, dsBm do
begin
Result := CreateBitmap(bmWidth, bmHeight, 1, 1, nil);
if Result <> 0 then
begin
SaveMono := SelectObject(MonoDC, Result);
if TransparentColor = TColorRef(clNone) then
PatBlt(MonoDC, 0, 0, bmWidth, bmHeight, Blackness)
else
begin
BitmapDC := GDICheck(CreateCompatibleDC(ScreenDC));
try
{ Convert DIB to DDB }
if bmBits <> nil then
begin
Remove := True;
DIB.dsbmih.biSize := 0;
Handle := CopyBitmap(Handle, Palette, Palette, DIB, nil);
end
else Remove := False;
SaveBitmap := SelectObject(BitmapDC, Handle);
if Palette <> 0 then
begin
SelectPalette(BitmapDC, Palette, False);
RealizePalette(BitmapDC);
SelectPalette(MonoDC, Palette, False);
RealizePalette(MonoDC);
end;
BkColor := SetBkColor(BitmapDC, TransparentColor);
BitBlt(MonoDC, 0, 0, bmWidth, bmHeight, BitmapDC, 0, 0, SrcCopy);
SetBkColor(BitmapDC, BkColor);
if SaveBitmap <> 0 then SelectObject(BitmapDC, SaveBitmap);
if Remove then DeleteObject(Handle);
finally
DeleteDC(BitmapDC);
end;
end;
if SaveMono <> 0 then SelectObject(MonoDC, SaveMono);
end;
end;
finally
if MonoDC <> 0 then DeleteDC(MonoDC);
if ScreenDC <> 0 then ReleaseDC(0, ScreenDC);
end;
end;
end;
procedure TSCBitmap.SetBounds(AWidth, AHeight: Integer);
var
DIB: TDIBSection;
begin
with FImage do
if (FDIB.dsbm.bmWidth <> AWidth) or (FDIB.dsbm.bmHeight <> AHeight) then
begin
HandleNeeded;
DIB := FDIB;
DIB.dsbm.bmWidth := AWidth;
DIB.dsbmih.biWidth := AWidth;
DIB.dsbm.bmHeight := AHeight;
DIB.dsbmih.biHeight := AHeight;
CopyImage(FHandle, FPalette, DIB);
Changed(Self);
end;
end;
{ TSCBitmapImage }
destructor TSCBitmapImage.Destroy;
begin
if FDIBHandle <> 0 then
begin
DeselectBitmap(FDIBHandle);
DeleteObject(FDIBHandle);
FDIBHandle := 0;
end;
FreeHandle;
if FDIB.dshSection <> 0 then CloseHandle(FDIB.dshSection);
FreeAndNil(FSaveStream);
inherited Destroy;
end;
procedure TSCBitmapImage.FreeHandle;
begin
if (FHandle <> 0) and (FHandle <> FDIBHandle) then
begin
DeselectBitmap(FHandle);
DeleteObject(FHandle);
end;
if FMaskHandle <> 0 then
begin
DeselectBitmap(FMaskHandle);
DeleteObject(FMaskHandle);
FMaskHandle := 0;
end;
InternalDeletePalette(FPalette);
FHandle := 0;
FPalette := 0;
end;
{ TSCBitmapCanvas }
constructor TSCBitmapCanvas.Create(ABitmap: TSCBitmap);
begin
inherited Create;
FBitmap := ABitmap;
end;
destructor TSCBitmapCanvas.Destroy;
begin
FreeContext;
inherited Destroy;
end;
procedure TSCBitmapCanvas.FreeContext;
var
H: HBITMAP;
begin
if FHandle <> 0 then
begin
Lock;
try
if FOldBitmap <> 0 then SelectObject(FHandle, FOldBitmap);
if FOldPalette <> 0 then SelectPalette(FHandle, FOldPalette, True);
H := FHandle;
Handle := 0;
DeleteDC(H);
BitmapCanvasList.Remove(Self);
finally
Unlock;
end;
end;
end;
procedure TSCBitmapCanvas.CreateHandle;
var
H: HBITMAP;
begin
if FBitmap <> nil then
begin
Lock;
try
FBitmap.HandleNeeded;
DeselectBitmap(FBitmap.FImage.FHandle);
FBitmap.PaletteNeeded;
H := CreateCompatibleDC(0);
if FBitmap.FImage.FHandle <> 0 then
FOldBitmap := SelectObject(H, FBitmap.FImage.FHandle) else
FOldBitmap := 0;
if FBitmap.FImage.FPalette <> 0 then
begin
FOldPalette := SelectPalette(H, FBitmap.FImage.FPalette, True);
RealizePalette(H);
end
else
FOldPalette := 0;
Handle := H;
FHandle := Handle;
BitmapCanvasList.Add(Self);
finally
Unlock;
end;
end;
end;
{ TSCBitmap }
constructor TSCBitmap.Create;
begin
inherited Create;
FTransparentColor := clDefault;
FImage := TSCBitmapImage.Create;
FImage.Reference;
if DDBsOnly then HandleType := bmDDB;
end;
destructor TSCBitmap.Destroy;
begin
FreeContext;
FImage.Release;
FCanvas.Free;
inherited Destroy;
end;
procedure TSCBitmap.Assign(Source: TPersistent);
var
DIB: TDIBSection;
begin
if (Source = nil) or (Source is TSCBitmap) then
begin
EnterCriticalSection(BitmapImageLock);
try
if Source <> nil then
begin
TSCBitmap(Source).FImage.Reference;
FImage.Release;
FImage := TSCBitmap(Source).FImage;
Transparent := TSCBitmap(Source).Transparent;
FTransparentColor := TSCBitmap(Source).FTransparentColor;
FTransparentMode := TSCBitmap(Source).FTransparentMode;
end
else
begin
FillChar(DIB, Sizeof(DIB), 0);
NewImage(0, 0, DIB, False);
end;
finally
LeaveCriticalSection(BitmapImageLock);
end;
PaletteModified := Palette <> 0;
Changed(Self);
end
else inherited Assign(Source);
end;
procedure TSCBitmap.CopyImage(AHandle: HBITMAP; APalette: HPALETTE; DIB: TDIBSection);
var
NewHandle, NewPalette: THandle;
begin
FreeContext;
NewHandle := 0;
NewPalette := 0;
try
if APalette = SystemPalette16 then
NewPalette := APalette
else
NewPalette := CopyPalette(APalette);
NewHandle := CopyBitmap(AHandle, APalette, NewPalette, DIB, FCanvas);
NewImage(NewHandle, NewPalette, DIB, FImage.FOS2Format);
except
InternalDeletePalette(NewPalette);
if NewHandle <> 0 then DeleteObject(NewHandle);
raise;
end;
end;
{ Called by the FCanvas whenever an operation is going to be performed on the
bitmap that would modify it. Since modifications should only affect this
TSCBitmap, the handle needs to be 'cloned' if it is being refered to by more
than one TSCBitmap }
procedure TSCBitmap.Changing(Sender: TObject);
begin
FreeImage;
FImage.FDIB.dsbmih.biClrUsed := 0;
FImage.FDIB.dsbmih.biClrImportant := 0;
FreeAndNil(FImage.FSaveStream);
end;
procedure TSCBitmap.Changed(Sender: TObject);
begin
FMaskBitsValid := False;
inherited Changed(Sender);
end;
procedure TSCBitmap.Dormant;
begin
FreeContext; // !! InternalDeletePalette fails without this
DIBNeeded;
FImage.FreeHandle;
end;
procedure TSCBitmap.Draw(ACanvas: TCanvas; const Rect: TRect);
var
OldPalette: HPalette;
RestorePalette: Boolean;
DoHalftone: Boolean;
Pt: TPoint;
BPP: Integer;
MaskDC: HDC;
Save: THandle;
begin
with Rect, FImage do
begin
TSCFakeCanvas(ACanvas).RequiredState(csAllValid);
PaletteNeeded;
OldPalette := 0;
RestorePalette := False;
if FPalette <> 0 then
begin
OldPalette := SelectPalette(ACanvas.Handle, FPalette, True);
RealizePalette(ACanvas.Handle);
RestorePalette := True;
end;
BPP := GetDeviceCaps(ACanvas.Handle, BITSPIXEL) *
GetDeviceCaps(ACanvas.Handle, PLANES);
DoHalftone := (BPP <= 8) and (BPP < (FDIB.dsbm.bmBitsPixel * FDIB.dsbm.bmPlanes));
if DoHalftone then
begin
GetBrushOrgEx(ACanvas.Handle, pt);
SetStretchBltMode(ACanvas.Handle, HALFTONE);
SetBrushOrgEx(ACanvas.Handle, pt.x, pt.y, @pt);
end else if not Monochrome then
SetStretchBltMode(ACanvas.Handle, STRETCH_DELETESCANS);
try
{ Call MaskHandleNeeded prior to creating the canvas handle since
it causes FreeContext to be called. }
if Transparent then MaskHandleNeeded;
TSCFakeCanvas(Canvas).RequiredState(csAllValid);
if Transparent then
begin
Save := 0;
MaskDC := 0;
try
MaskDC := GDICheck(CreateCompatibleDC(0));
Save := SelectObject(MaskDC, FMaskHandle);
TransparentStretchBlt(ACanvas.Handle, Left, Top, Right - Left,
Bottom - Top, Canvas.Handle, 0, 0, FDIB.dsbm.bmWidth,
FDIB.dsbm.bmHeight, MaskDC, 0, 0);
finally
if Save <> 0 then SelectObject(MaskDC, Save);
if MaskDC <> 0 then DeleteDC(MaskDC);