-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIB.pas
3239 lines (2890 loc) · 83.7 KB
/
DIB.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 DIB;
interface
// {$INCLUDE DelphiXcfg.inc}
uses
Windows, SysUtils, Classes, Graphics, Controls;
type
TRGBQuads = array[0..255] of TRGBQuad;
TPaletteEntries = array[0..255] of TPaletteEntry;
PBGR = ^TBGR;
TBGR = packed record
B, G, R: Byte;
end;
PArrayBGR = ^TArrayBGR;
TArrayBGR = array[0..10000] of TBGR;
PArrayByte = ^TArrayByte;
TArrayByte = array[0..10000] of Byte;
PArrayWord = ^TArrayWord;
TArrayWord = array[0..10000] of Word;
PArrayDWord = ^TArrayDWord;
TArrayDWord = array[0..10000] of DWord;
{ TDIB }
TDIBPixelFormat = record
RBitMask, GBitMask, BBitMask: DWORD;
RBitCount, GBitCount, BBitCount: DWORD;
RShift, GShift, BShift: DWORD;
RBitCount2, GBitCount2, BBitCount2: DWORD;
end;
TDIBSharedImage = class(TSharedImage)
private
FBitCount: Integer;
FBitmapInfo: PBitmapInfo;
FBitmapInfoSize: Integer;
FChangePalette: Boolean;
FColorTable: TRGBQuads;
FColorTablePos: Integer;
FCompressed: Boolean;
FDC: THandle;
FHandle: THandle;
FHeight: Integer;
FMemoryImage: Boolean;
FNextLine: Integer;
FOldHandle: THandle;
FPalette: HPalette;
FPaletteCount: Integer;
FPBits: Pointer;
FPixelFormat: TDIBPixelFormat;
FSize: Integer;
FTopPBits: Pointer;
FWidth: Integer;
FWidthBytes: Integer;
constructor Create;
procedure NewImage(AWidth, AHeight, ABitCount: Integer;
const PixelFormat: TDIBPixelFormat; const ColorTable: TRGBQuads; MemoryImage, Compressed: Boolean);
procedure Duplicate(Source: TDIBSharedImage; MemoryImage: Boolean);
procedure Compress(Source: TDIBSharedImage);
procedure Decompress(Source: TDIBSharedImage; MemoryImage: Boolean);
procedure ReadData(Stream: TStream; MemoryImage: Boolean);
function GetPalette: THandle;
procedure SetColorTable(const Value: TRGBQuads);
protected
procedure FreeHandle; override;
public
destructor Destroy; override;
end;
TDIB = class(TGraphic)
private
FCanvas: TCanvas;
FImage: TDIBSharedImage;
FProgressName: string;
FProgressOldY: DWORD;
FProgressOldTime: DWORD;
FProgressOld: DWORD;
FProgressY: DWORD;
{ For speed-up }
FBitCount: Integer;
FHeight: Integer;
FNextLine: Integer;
FNowPixelFormat: TDIBPixelFormat;
FPBits: Pointer;
FSize: Integer;
FTopPBits: Pointer;
FWidth: Integer;
FWidthBytes: Integer;
procedure AllocHandle;
procedure CanvasChanging(Sender: TObject);
procedure Changing(MemoryImage: Boolean);
procedure ConvertBitCount(ABitCount: Integer);
function GetBitmapInfo: PBitmapInfo;
function GetBitmapInfoSize: Integer;
function GetCanvas: TCanvas;
function GetHandle: THandle;
function GetPaletteCount: Integer;
function GetPixel(X, Y: Integer): DWORD;
function GetPBits: Pointer;
function GetPBitsReadOnly: Pointer;
function GetScanLine(Y: Integer): Pointer;
function GetScanLineReadOnly(Y: Integer): Pointer;
function GetTopPBits: Pointer;
function GetTopPBitsReadOnly: Pointer;
procedure SetBitCount(Value: Integer);
procedure SetImage(Value: TDIBSharedImage);
procedure SetNowPixelFormat(const Value: TDIBPixelFormat);
procedure SetPixel(X, Y: Integer; Value: DWORD);
procedure StartProgress(const Name: string);
procedure EndProgress;
procedure UpdateProgress(PercentY: Integer);
protected
procedure DefineProperties(Filer: TFiler); override;
function GetEmpty: Boolean; override;
function GetHeight: Integer; override;
function GetPalette: HPalette; override;
function GetWidth: Integer; override;
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;
procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
public
ColorTable: TRGBQuads;
PixelFormat: TDIBPixelFormat;
constructor Create; override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Clear;
procedure Compress;
procedure Decompress;
procedure FreeHandle;
procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
APalette: HPALETTE); override;
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
var APalette: HPALETTE); override;
procedure SaveToStream(Stream: TStream); override;
procedure SetSize(AWidth, AHeight, ABitCount: Integer);
procedure UpdatePalette;
{ Special effect }
procedure Blur(ABitCount: Integer; Radius: Integer);
procedure Greyscale(ABitCount: Integer);
procedure Mirror(MirrorX, MirrorY: Boolean);
procedure Negative;
property BitCount: Integer read FBitCount write SetBitCount;
property BitmapInfo: PBitmapInfo read GetBitmapInfo;
property BitmapInfoSize: Integer read GetBitmapInfoSize;
property Canvas: TCanvas read GetCanvas;
property Handle: THandle read GetHandle;
property Height: Integer read FHeight write SetHeight;
property NextLine: Integer read FNextLine;
property NowPixelFormat: TDIBPixelFormat read FNowPixelFormat write SetNowPixelFormat;
property PaletteCount: Integer read GetPaletteCount;
property PBits: Pointer read GetPBits;
property PBitsReadOnly: Pointer read GetPBitsReadOnly;
property Pixels[X, Y: Integer]: DWORD read GetPixel write SetPixel;
property ScanLine[Y: Integer]: Pointer read GetScanLine;
property ScanLineReadOnly[Y: Integer]: Pointer read GetScanLineReadOnly;
property Size: Integer read FSize;
property TopPBits: Pointer read GetTopPBits;
property TopPBitsReadOnly: Pointer read GetTopPBitsReadOnly;
property Width: Integer read FWidth write SetWidth;
property WidthBytes: Integer read FWidthBytes;
end;
TDIBitmap = class(TDIB) end;
{ TCustomDXDIB }
TCustomDXDIB = class(TComponent)
private
FDIB: TDIB;
procedure SetDIB(Value: TDIB);
public
constructor Create(AOnwer: TComponent); override;
destructor Destroy; override;
property DIB: TDIB read FDIB write SetDIB;
end;
{ TDXDIB }
TDXDIB = class(TCustomDXDIB)
published
property DIB;
end;
{ TCustomDXPaintBox }
TCustomDXPaintBox = class(TGraphicControl)
private
FAutoStretch: Boolean;
FCenter: Boolean;
FDIB: TDIB;
FKeepAspect: Boolean;
FStretch: Boolean;
FViewWidth: Integer;
FViewHeight: Integer;
procedure SetAutoStretch(Value: Boolean);
procedure SetCenter(Value: Boolean);
procedure SetDIB(Value: TDIB);
procedure SetKeepAspect(Value: Boolean);
procedure SetStretch(Value: Boolean);
procedure SetViewWidth(Value: Integer);
procedure SetViewHeight(Value: Integer);
protected
function GetPalette: HPALETTE; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
property AutoStretch: Boolean read FAutoStretch write SetAutoStretch;
property Canvas;
property Center: Boolean read FCenter write SetCenter;
property DIB: TDIB read FDIB write SetDIB;
property KeepAspect: Boolean read FKeepAspect write SetKeepAspect;
property Stretch: Boolean read FStretch write SetStretch;
property ViewWidth: Integer read FViewWidth write SetViewWidth;
property ViewHeight: Integer read FViewHeight write SetViewHeight;
end;
{ TDXPaintBox }
TDXPaintBox = class(TCustomDXPaintBox)
published
{$IFDEF DelphiX_Spt4}property Anchors;{$ENDIF}
property AutoStretch;
property Center;
{$IFDEF DelphiX_Spt4}property Constraints;{$ENDIF}
property DIB;
property KeepAspect;
property Stretch;
property ViewWidth;
property ViewHeight;
property Align;
property DragCursor;
property DragMode;
property Enabled;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
function MakeDIBPixelFormat(RBitCount, GBitCount, BBitCount: Integer): TDIBPixelFormat;
function MakeDIBPixelFormatMask(RBitMask, GBitMask, BBitMask: Integer): TDIBPixelFormat;
function pfRGB(const PixelFormat: TDIBPixelFormat; R, G, B: Byte): DWORD;
procedure pfGetRGB(const PixelFormat: TDIBPixelFormat; Color: DWORD; var R, G, B: Byte);
function pfGetRValue(const PixelFormat: TDIBPixelFormat; Color: DWORD): Byte;
function pfGetGValue(const PixelFormat: TDIBPixelFormat; Color: DWORD): Byte;
function pfGetBValue(const PixelFormat: TDIBPixelFormat; Color: DWORD): Byte;
function GreyscaleColorTable: TRGBQuads;
function RGBQuad(R, G, B: Byte): TRGBQuad;
function PaletteEntryToRGBQuad(const Entry: TPaletteEntry): TRGBQuad;
function PaletteEntriesToRGBQuads(const Entries: TPaletteEntries): TRGBQuads;
function RGBQuadToPaletteEntry(const RGBQuad: TRGBQuad): TPaletteEntry;
function RGBQuadsToPaletteEntries(const RGBQuads: TRGBQuads): TPaletteEntries;
implementation
uses DXConsts;
function Max(B1, B2: Integer): Integer;
begin
if B1>=B2 then Result := B1 else Result := B2;
end;
function MakeDIBPixelFormat(RBitCount, GBitCount, BBitCount: Integer): TDIBPixelFormat;
begin
Result.RBitMask := ((1 shl RBitCount)-1) shl (GBitCount+BBitCount);
Result.GBitMask := ((1 shl GBitCount)-1) shl (BBitCount);
Result.BBitMask := (1 shl BBitCount)-1;
Result.RBitCount := RBitCount;
Result.GBitCount := GBitCount;
Result.BBitCount := BBitCount;
Result.RBitCount2 := 8-RBitCount;
Result.GBitCount2 := 8-GBitCount;
Result.BBitCount2 := 8-BBitCount;
Result.RShift := (GBitCount+BBitCount)-(8-RBitCount);
Result.GShift := BBitCount-(8-GBitCount);
Result.BShift := 8-BBitCount;
end;
function MakeDIBPixelFormatMask(RBitMask, GBitMask, BBitMask: Integer): TDIBPixelFormat;
function GetBitCount(b: Integer): Integer;
var
i: Integer;
begin
i := 0;
while (i<31) and (((1 shl i) and b)=0) do Inc(i);
Result := 0;
while ((1 shl i) and b)<>0 do
begin
Inc(i);
Inc(Result);
end;
end;
begin
Result := MakeDIBPixelFormat(GetBitCount(RBitMask), GetBitCount(GBitMask),
GetBitCount(BBitMask));
end;
function pfRGB(const PixelFormat: TDIBPixelFormat; R, G, B: Byte): DWORD;
begin
with PixelFormat do
Result := ((R shl RShift) and RBitMask) or ((G shl GShift) and GBitMask) or
((B shr BShift) and BBitMask);
end;
procedure pfGetRGB(const PixelFormat: TDIBPixelFormat; Color: DWORD; var R, G, B: Byte);
begin
with PixelFormat do
begin
R := (Color and RBitMask) shr RShift;
R := R or (R shr RBitCount2);
G := (Color and GBitMask) shr GShift;
G := G or (G shr GBitCount2);
B := (Color and BBitMask) shl BShift;
B := B or (B shr BBitCount2);
end;
end;
function pfGetRValue(const PixelFormat: TDIBPixelFormat; Color: DWORD): Byte;
begin
with PixelFormat do
begin
Result := (Color and RBitMask) shr RShift;
Result := Result or (Result shr RBitCount);
end;
end;
function pfGetGValue(const PixelFormat: TDIBPixelFormat; Color: DWORD): Byte;
begin
with PixelFormat do
begin
Result := (Color and GBitMask) shr GShift;
Result := Result or (Result shr GBitCount);
end;
end;
function pfGetBValue(const PixelFormat: TDIBPixelFormat; Color: DWORD): Byte;
begin
with PixelFormat do
begin
Result := (Color and BBitMask) shl BShift;
Result := Result or (Result shr BBitCount);
end;
end;
function GreyscaleColorTable: TRGBQuads;
var
i: Integer;
begin
for i:=0 to 255 do
with Result[i] do
begin
rgbRed := i;
rgbGreen := i;
rgbBlue := i;
rgbReserved := 0;
end;
end;
function RGBQuad(R, G, B: Byte): TRGBQuad;
begin
with Result do
begin
rgbRed := R;
rgbGreen := G;
rgbBlue := B;
rgbReserved := 0;
end;
end;
function PaletteEntryToRGBQuad(const Entry: TPaletteEntry): TRGBQuad;
begin
with Result do
with Entry do
begin
rgbRed := peRed;
rgbGreen := peGreen;
rgbBlue := peBlue;
rgbReserved := 0;
end;
end;
function PaletteEntriesToRGBQuads(const Entries: TPaletteEntries): TRGBQuads;
var
i: Integer;
begin
for i:=0 to 255 do
Result[i] := PaletteEntryToRGBQuad(Entries[i]);
end;
function RGBQuadToPaletteEntry(const RGBQuad: TRGBQuad): TPaletteEntry;
begin
with Result do
with RGBQuad do
begin
peRed := rgbRed;
peGreen := rgbGreen;
peBlue := rgbBlue;
peFlags := 0;
end;
end;
function RGBQuadsToPaletteEntries(const RGBQuads: TRGBQuads): TPaletteEntries;
var
i: Integer;
begin
for i:=0 to 255 do
Result[i] := RGBQuadToPaletteEntry(RGBQuads[i]);
end;
{ TDIBSharedImage }
type
PLocalDIBPixelFormat = ^TLocalDIBPixelFormat;
TLocalDIBPixelFormat = packed record
RBitMask, GBitMask, BBitMask: DWORD;
end;
TPaletteItem = class(TCollectionItem)
private
ID: Integer;
Palette: HPalette;
RefCount: Integer;
ColorTable: TRGBQuads;
ColorTableCount: Integer;
destructor Destroy; override;
procedure AddRef;
procedure Release;
end;
TPaletteManager = class
private
FList: TCollection;
constructor Create;
destructor Destroy; override;
function CreatePalette(const ColorTable: TRGBQuads; ColorTableCount: Integer): HPalette;
procedure DeletePalette(var Palette: HPalette);
end;
destructor TPaletteItem.Destroy;
begin
DeleteObject(Palette);
inherited Destroy;
end;
procedure TPaletteItem.AddRef;
begin
Inc(RefCount);
end;
procedure TPaletteItem.Release;
begin
Dec(RefCount);
if RefCount<=0 then Free;
end;
constructor TPaletteManager.Create;
begin
inherited Create;
FList := TCollection.Create(TPaletteItem);
end;
destructor TPaletteManager.Destroy;
begin
FList.Free;
inherited Destroy;
end;
function TPaletteManager.CreatePalette(const ColorTable: TRGBQuads; ColorTableCount: Integer): HPalette;
type
TMyLogPalette = record
palVersion: Word;
palNumEntries: Word;
palPalEntry: TPaletteEntries;
end;
var
i, ID: Integer;
Item: TPaletteItem;
LogPalette: TMyLogPalette;
begin
{ Hash key making }
ID := ColorTableCount;
for i:=0 to ColorTableCount-1 do
with ColorTable[i] do
begin
Inc(ID, rgbRed);
Inc(ID, rgbGreen);
Inc(ID, rgbBlue);
end;
{ Does the same palette already exist? }
for i:=0 to FList.Count-1 do
begin
Item := TPaletteItem(FList.Items[i]);
if (Item.ID=ID) and (Item.ColorTableCount=ColorTableCount) and
CompareMem(@Item.ColorTable, @ColorTable, ColorTableCount*SizeOf(TRGBQuad)) then
begin
Item.AddRef; Result := Item.Palette;
Exit;
end;
end;
{ New palette making }
Item := TPaletteItem.Create(FList);
Item.ID := ID;
Move(ColorTable, Item.ColorTable, ColorTableCount*SizeOf(TRGBQuad));
Item.ColorTableCount := ColorTableCount;
with LogPalette do
begin
palVersion := $300;
palNumEntries := ColorTableCount;
palPalEntry := RGBQuadsToPaletteEntries(ColorTable);
end;
Item.Palette := Windows.CreatePalette(PLogPalette(@LogPalette)^);
Item.AddRef; Result := Item.Palette;
end;
procedure TPaletteManager.DeletePalette(var Palette: HPalette);
var
i: Integer;
Item: TPaletteItem;
begin
if Palette=0 then Exit;
for i:=0 to FList.Count-1 do
begin
Item := TPaletteItem(FList.Items[i]);
if (Item.Palette=Palette) then
begin
Palette := 0;
Item.Release;
Exit;
end;
end;
end;
var
FPaletteManager: TPaletteManager;
function PaletteManager: TPaletteManager;
begin
if FPaletteManager=nil then
FPaletteManager := TPaletteManager.Create;
Result := FPaletteManager;
end;
constructor TDIBSharedImage.Create;
begin
inherited Create;
FMemoryImage := True;
SetColorTable(GreyscaleColorTable);
FColorTable := GreyscaleColorTable;
FPixelFormat := MakeDIBPixelFormat(8, 8, 8);
end;
procedure TDIBSharedImage.NewImage(AWidth, AHeight, ABitCount: Integer;
const PixelFormat: TDIBPixelFormat; const ColorTable: TRGBQuads; MemoryImage, Compressed: Boolean);
var
InfoOfs: Integer;
UsePixelFormat: Boolean;
begin
Create;
{ Pixel format check }
case ABitCount of
1 : if not ((PixelFormat.RBitMask=$FF0000) and (PixelFormat.GBitMask=$00FF00) and (PixelFormat.BBitMask=$0000FF)) then
raise EInvalidGraphicOperation.Create(SInvalidDIBPixelFormat);
4 : if not ((PixelFormat.RBitMask=$FF0000) and (PixelFormat.GBitMask=$00FF00) and (PixelFormat.BBitMask=$0000FF)) then
raise EInvalidGraphicOperation.Create(SInvalidDIBPixelFormat);
8 : if not ((PixelFormat.RBitMask=$FF0000) and (PixelFormat.GBitMask=$00FF00) and (PixelFormat.BBitMask=$0000FF)) then
raise EInvalidGraphicOperation.Create(SInvalidDIBPixelFormat);
16: begin
if not (((PixelFormat.RBitMask=$7C00) and (PixelFormat.GBitMask=$03E0) and (PixelFormat.BBitMask=$001F)) or
((PixelFormat.RBitMask=$F800) and (PixelFormat.GBitMask=$07E0) and (PixelFormat.BBitMask=$001F))) then
raise EInvalidGraphicOperation.Create(SInvalidDIBPixelFormat);
end;
24: begin
if not ((PixelFormat.RBitMask=$FF0000) and (PixelFormat.GBitMask=$00FF00) and (PixelFormat.BBitMask=$0000FF)) then
raise EInvalidGraphicOperation.Create(SInvalidDIBPixelFormat);
end;
32: begin
if not ((PixelFormat.RBitMask=$FF0000) and (PixelFormat.GBitMask=$00FF00) and (PixelFormat.BBitMask=$0000FF)) then
raise EInvalidGraphicOperation.Create(SInvalidDIBPixelFormat);
end;
else
raise EInvalidGraphicOperation.CreateFmt(SInvalidDIBBitCount, [ABitCount]);
end;
FBitCount := ABitCount;
FHeight := AHeight;
FWidth := AWidth;
FWidthBytes := (((AWidth*ABitCount)+31) shr 5) * 4;
FNextLine := -FWidthBytes;
FSize := FWidthBytes*FHeight;
UsePixelFormat := ABitCount in [16, 32];
FPixelFormat := PixelFormat;
FPaletteCount := 0;
if FBitCount<=8 then
FPaletteCount := 1 shl FBitCount;
FBitmapInfoSize := SizeOf(TBitmapInfoHeader);
if UsePixelFormat then
Inc(FBitmapInfoSize, SizeOf(TLocalDIBPixelFormat));
Inc(FBitmapInfoSize, SizeOf(TRGBQuad)*FPaletteCount);
GetMem(FBitmapInfo, FBitmapInfoSize);
FillChar(FBitmapInfo^, FBitmapInfoSize, 0);
{ BitmapInfo setting. }
with FBitmapInfo^.bmiHeader do
begin
biSize := SizeOf(TBitmapInfoHeader);
biWidth := FWidth;
biHeight := FHeight;
biPlanes := 1;
biBitCount := FBitCount;
if UsePixelFormat then
biCompression := BI_BITFIELDS
else
begin
if (FBitCount=4) and (Compressed) then
biCompression := BI_RLE4
else if (FBitCount=8) and (Compressed) then
biCompression := BI_RLE8
else
biCompression := BI_RGB;
end;
biSizeImage := FSize;
biXPelsPerMeter := 0;
biYPelsPerMeter := 0;
biClrUsed := 0;
biClrImportant := 0;
end;
InfoOfs := SizeOf(TBitmapInfoHeader);
if UsePixelFormat then
begin
with PLocalDIBPixelFormat(Integer(FBitmapInfo)+InfoOfs)^ do
begin
RBitMask := PixelFormat.RBitMask;
GBitMask := PixelFormat.GBitMask;
BBitMask := PixelFormat.BBitMask;
end;
Inc(InfoOfs, SizeOf(TLocalDIBPixelFormat));
end;
FColorTablePos := InfoOfs;
FColorTable := ColorTable;
Move(FColorTable, Pointer(Integer(FBitmapInfo)+FColorTablePos)^, SizeOf(TRGBQuad)*FPaletteCount);
FCompressed := FBitmapInfo^.bmiHeader.biCompression in [BI_RLE4, BI_RLE8];
FMemoryImage := MemoryImage or FCompressed;
{ DIB making. }
if not Compressed then
begin
if MemoryImage then
begin
FPBits := Pointer(GlobalAlloc(GMEM_FIXED, FSize));
if FPBits=nil then
OutOfMemoryError;
end else
begin
FDC := CreateCompatibleDC(0);
FHandle := CreateDIBSection(FDC, FBitmapInfo^, DIB_RGB_COLORS, FPBits, 0, 0);
if FHandle=0 then
raise EOutOfResources.CreateFmt(SCannotMade, ['DIB']);
FOldHandle := SelectObject(FDC, FHandle);
end;
end;
FTopPBits := Pointer(Integer(FPBits)+(FHeight-1)*FWidthBytes);
end;
procedure TDIBSharedImage.Duplicate(Source: TDIBSharedImage; MemoryImage: Boolean);
begin
if Source.FSize=0 then
begin
Create;
FMemoryImage := MemoryImage;
end else
begin
NewImage(Source.FWidth, Source.FHeight, Source.FBitCount,
Source.FPixelFormat, Source.FColorTable, MemoryImage, Source.FCompressed);
if FCompressed then
begin
FBitmapInfo.bmiHeader.biSizeImage := Source.FBitmapInfo.bmiHeader.biSizeImage;
GetMem(FPBits, FBitmapInfo.bmiHeader.biSizeImage);
Move(Source.FPBits^, FPBits^, FBitmapInfo.bmiHeader.biSizeImage);
end else
begin
Move(Source.FPBits^, FPBits^, FBitmapInfo.bmiHeader.biSizeImage);
end;
end;
end;
procedure TDIBSharedImage.Compress(Source: TDIBSharedImage);
procedure EncodeRLE4;
var
Size: Integer;
function AllocByte: PByte;
begin
if Size mod 4096=0 then
ReAllocMem(FPBits, Size+4095);
Result := Pointer(Integer(FPBits)+Size);
Inc(Size);
end;
var
B1, B2, C: Byte;
PB1, PB2: Integer;
Src: PByte;
X, Y: Integer;
function GetPixel(x: Integer): Integer;
begin
if X and 1=0 then
Result := PArrayByte(Src)[X shr 1] shr 4
else
Result := PArrayByte(Src)[X shr 1] and $0F;
end;
begin
Size := 0;
for y:=0 to Source.FHeight-1 do
begin
x := 0;
Src := Pointer(Integer(Source.FPBits)+y*FWidthBytes);
while x<Source.FWidth do
begin
if (Source.FWidth-x>3) and (GetPixel(x)=GetPixel(x+2)) then
begin
{ Encoding mode }
B1 := 2;
B2 := (GetPixel(x) shl 4) or GetPixel(x+1);
Inc(x, 2);
C := B2;
while (x<Source.FWidth) and (C and $F=GetPixel(x)) and (B1<255) do
begin
Inc(B1);
Inc(x);
C := (C shr 4) or (C shl 4);
end;
AllocByte^ := B1;
AllocByte^ := B2;
end else
if (Source.FWidth-x>5) and ((GetPixel(x)<>GetPixel(x+2)) or (GetPixel(x+1)<>GetPixel(x+3))) and
((GetPixel(x+2)=GetPixel(x+4)) and (GetPixel(x+3)=GetPixel(x+5))) then
begin
{ Encoding mode }
AllocByte^ := 2;
AllocByte^ := (GetPixel(x) shl 4) or GetPixel(x+1);
Inc(x, 2);
end else
begin
if (Source.FWidth-x<4) then
begin
{ Encoding mode }
while Source.FWidth-x>=2 do
begin
AllocByte^ := 2;
AllocByte^ := (GetPixel(x) shl 4) or GetPixel(x+1);
Inc(x, 2);
end;
if Source.FWidth-x=1 then
begin
AllocByte^ := 1;
AllocByte^ := GetPixel(x) shl 4;
Inc(x);
end;
end else
begin
{ Absolute mode }
PB1 := Size; AllocByte;
PB2 := Size; AllocByte;
B1 := 0;
B2 := 4;
AllocByte^ := (GetPixel(x) shl 4) or GetPixel(x+1);
AllocByte^ := (GetPixel(x+2) shl 4) or GetPixel(x+3);
Inc(x, 4);
while (x+1<Source.FWidth) and (B2<254) do
begin
if (Source.FWidth-x>3) and (GetPixel(x)=GetPixel(x+2)) and (GetPixel(x+1)=GetPixel(x+3)) then
Break;
AllocByte^ := (GetPixel(x) shl 4) or GetPixel(x+1);
Inc(B2, 2);
Inc(x, 2);
end;
PByte(Integer(FPBits)+PB1)^ := B1;
PByte(Integer(FPBits)+PB2)^ := B2;
end;
end;
if Size and 1=1 then AllocByte;
end;
{ End of line }
AllocByte^ := 0;
AllocByte^ := 0;
end;
{ End of bitmap }
AllocByte^ := 0;
AllocByte^ := 1;
FBitmapInfo.bmiHeader.biSizeImage := Size;
FSize := Size;
end;
procedure EncodeRLE8;
var
Size: Integer;
function AllocByte: PByte;
begin
if Size mod 4096=0 then
ReAllocMem(FPBits, Size+4095);
Result := Pointer(Integer(FPBits)+Size);
Inc(Size);
end;
var
B1, B2: Byte;
PB1, PB2: Integer;
Src: PByte;
X, Y: Integer;
begin
Size := 0;
for y:=0 to Source.FHeight-1 do
begin
x := 0;
Src := Pointer(Integer(Source.FPBits)+y*FWidthBytes);
while x<Source.FWidth do
begin
if (Source.FWidth-x>2) and (Src^=PByte(Integer(Src)+1)^) then
begin
{ Encoding mode }
B1 := 2;
B2 := Src^;
Inc(x, 2);
Inc(Src, 2);
while (x<Source.FWidth) and (Src^=B2) and (B1<255) do
begin
Inc(B1);
Inc(x);
Inc(Src);
end;
AllocByte^ := B1;
AllocByte^ := B2;
end else
if (Source.FWidth-x>2) and (Src^<>PByte(Integer(Src)+1)^) and (PByte(Integer(Src)+1)^=PByte(Integer(Src)+2)^) then
begin
{ Encoding mode }
AllocByte^ := 1;
AllocByte^ := Src^; Inc(Src);
Inc(x);
end else
begin
if (Source.FWidth-x<4) then
begin
{ Encoding mode }
if Source.FWidth-x=2 then
begin
AllocByte^ := 1;
AllocByte^ := Src^; Inc(Src);
AllocByte^ := 1;
AllocByte^ := Src^; Inc(Src);
Inc(x, 2);
end else
begin
AllocByte^ := 1;
AllocByte^ := Src^; Inc(Src);
Inc(x);
end;
end else
begin
{ Absolute mode }
PB1 := Size; AllocByte;
PB2 := Size; AllocByte;
B1 := 0;
B2 := 3;
Inc(x, 3);
AllocByte^ := Src^; Inc(Src);
AllocByte^ := Src^; Inc(Src);
AllocByte^ := Src^; Inc(Src);
while (x<Source.FWidth) and (B2<255) do
begin
if (Source.FWidth-x>3) and (Src^=PByte(Integer(Src)+1)^) and (Src^=PByte(Integer(Src)+2)^) and (Src^=PByte(Integer(Src)+3)^) then
Break;
AllocByte^ := Src^; Inc(Src);
Inc(B2);
Inc(x);
end;
PByte(Integer(FPBits)+PB1)^ := B1;
PByte(Integer(FPBits)+PB2)^ := B2;
end;
end;
if Size and 1=1 then AllocByte;
end;
{ End of line }
AllocByte^ := 0;
AllocByte^ := 0;
end;
{ End of bitmap }
AllocByte^ := 0;
AllocByte^ := 1;
FBitmapInfo.bmiHeader.biSizeImage := Size;
FSize := Size;
end;
begin
if Source.FCompressed then
Duplicate(Source, Source.FMemoryImage)
else begin
NewImage(Source.FWidth, Source.FHeight, Source.FBitCount,
Source.FPixelFormat, Source.FColorTable, True, True);
case FBitmapInfo.bmiHeader.biCompression of
BI_RLE4: EncodeRLE4;
BI_RLE8: EncodeRLE8;
else
Duplicate(Source, Source.FMemoryImage);
end;
end;
end;
procedure TDIBSharedImage.Decompress(Source: TDIBSharedImage; MemoryImage: Boolean);
procedure DecodeRLE4;
var
B1, B2, C: Byte;
Dest, Src, P: PByte;
X, Y, i: Integer;