forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinarBitmap.pas
1771 lines (1634 loc) · 49.9 KB
/
LinarBitmap.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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// LinarBitmap.pas - Bitmap handling
// ---------------------------------
// Version: 2004-03-10
// Maintain: Michael Vinther | mv@logicnet·dk
//
// Last changes:
// 16 bit grayscale bitmap support
//
unit LinarBitmap;
interface
uses Windows, Classes, SysUtils, Graphics, Streams, DelphiStream, MemUtils,
Math, Monitor, MathUtils;
resourcestring
rsInvalidPixelFormat = 'Unsupported pixel format';
rsInvalidBitmapSize = 'Invalid bitmap size';
rsUnsupportedFileFormat = 'Unsupported file format';
rsErrorInBitmapData = 'Error in bitmap data';
rsUnsupportedBitmapFormat = 'Unsupported bitmap format';
type
RGBRec = packed record
B, G, R : Byte;
end;
PRGBRec = ^RGBRec;
TRGBArray = packed array[0..0] of RGBRec;
PRGBArray = ^TRGBArray;
TPalette = packed array[0..255] of RGBRec;
PPalette = ^TPalette;
TPalEntries = array[0..255] of TPaletteEntry;
TBitmapLoader = class;
TLinearBitmap = class(TAssignObject)
protected
fWidth, fHeight : Integer;
fPixelFormat : TPixelFormat;
fBytesPerLine, fPixelSize : Integer;
fSize : LongInt;
fPresent : Boolean;
function GetScanLine(Y: Integer): Pointer;
function GetPixel(X,Y: Integer): Pointer;
function GetPixelColor(X,Y: Integer): TColor;
procedure SetPixelColor(X,Y: Integer; Color: TColor);
procedure SetPixelFormat(PixFormat: TPixelFormat);
public
Map : PByteArray;
Palette : PPalette;
property Width: Integer read fWidth;
property Height: Integer read fHeight;
property BytesPerLine : Integer read fBytesPerLine;
property PixelFormat : TPixelFormat read fPixelFormat write SetPixelFormat;
property PixelSize : Integer read fPixelSize; // Bytes per pixel
property ScanLine[Y: Integer]: Pointer read GetScanLine;
property Pixel[X,Y: Integer]: Pointer read GetPixel; default;
property PixelColor[X,Y: Integer]: TColor read GetPixelColor write SetPixelColor;
property Size : Integer read fSize;
property Present : Boolean read fPresent;
constructor Create; overload;
constructor Create(Width,Height: Integer; PixFormat: TPixelFormat); overload;
constructor Create(Other: TObject); overload; // Create copy
destructor Destroy; override;
// Create new image
procedure New(Width,Height: Integer; PixFormat: TPixelFormat);
// Release image memory
procedure Dispose; virtual;
// Clear image with color
procedure Clear(Value: Byte=0);
procedure ClearColor(Color: TColor); // Red is LSB
// Copy image to Other
procedure AssignTo(Other: TObject); override;
procedure Assign(Other: TObject); override;
procedure TakeOver(Other: TLinearBitmap);
// Make sure BytesPerLine=Width*PixelSize so no memory is wasted
procedure OptimizeMem; virtual;
// Copy image to/from clipboard
procedure CopyToClipboard;
function GetFromClipboard: Boolean;
procedure PasteImage(Source: TLinearBitmap; X,Y: Integer);
// Load/save, TBitmapLoaders must be assigned
function LoadFromFile(const FileName: string): TBitmapLoader;
procedure SaveToFile(const FileName: string);
procedure LoadFromStream(Stream: TSeekableStream; const FormatExt: string); overload;
procedure LoadFromStream(Stream: TStream; const FormatExt: string); overload; // FormatExt must be UPPERCASE
procedure SaveToStream(Stream: TSeekableStream; const FormatExt: string); overload;
procedure SaveToStream(Stream: TStream; const FormatExt: string); overload; // FormatExt must be UPPERCASE
// Return true if image is grayscale
function IsGrayScale: Boolean;
// Resize image canvas, old image is positioned at XPos,YPos
procedure ResizeCanvas(XSiz,YSiz,XPos,YPos: Integer; Color: TColor=0);
// Get image from Windows DIB
procedure GetFromDIB(var DIB: TBitmapInfo);
procedure GetFromHDIB(HDIB: HBitmap);
// Make Windows DIB, return total size in bytes
function MakeDIB(out Bitmap: PBitmapInfo): Integer;
// Stretch and paint to TCanvas,
procedure PaintToCanvas(Canvas: TCanvas; const Dest: TRect; HalftoneStretch: Boolean=False);
procedure StretchDIBits(DC: THandle; const Dest: TRect; HalftoneStretch: Boolean=False);
// Paint to TBitmap (target size and palette assumed correct)
procedure PaintToTBitmap(Target: TBitmap);
end;
TLinearBitmapArray = array of TLinearBitmap;
TLinarBitmap = TLinearBitmap; // Spelling bug correction
TBitmapLoader = class(TMonitorObject)
public
function CanLoad(const Ext: string): Boolean; virtual; // Ext must be UPPERCASE
function CanSave(const Ext: string): Boolean; virtual; // Ext must be UPPERCASE
function GetLoadFilter: string; virtual;
function GetSaveFilter: string; virtual;
procedure LoadFromStream(Stream: TSeekableStream; const Ext: string; Bitmap: TLinearBitmap); virtual;
procedure SaveToStream(Stream: TSeekableStream; const Ext: string; Bitmap: TLinearBitmap); virtual;
procedure LoadFromFile(const FileName,FileType: string; Bitmap: TLinearBitmap); virtual;
procedure SaveToFile(const FileName,FileType: string; Bitmap: TLinearBitmap); virtual;
end;
TLinearGraphic = class(TGraphic)
protected
FImage : TLinearBitmap;
procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
function GetEmpty: Boolean; override;
function GetHeight: Integer; override;
function GetWidth: Integer; override;
procedure SetHeight(Value: Integer); override;
procedure SetWidth(Value: Integer); override;
procedure AssignTo(Dest: TPersistent); override;
public
property Bitmap : TLinearBitmap read FImage;
constructor Create; override;
destructor Destroy; override;
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToStream(Stream: TStream); override;
procedure LoadFromFile(const FileName: string); override;
procedure SaveToFile(const FileName: string); override;
procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle; APalette: HPALETTE); override;
procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle; var APalette: HPALETTE); override;
end;
TLinarGraphic = TLinearGraphic; // Spelling bug correction
TBitmapLoaderList = array[0..0] of TBitmapLoader;
PBitmapLoaderList = ^TBitmapLoaderList;
TBitmapLoaders = class(TMonitorObject)
Loaders : PBitmapLoaderList;
Count, MemCount : Integer;
procedure AddLoader(Loader: TBitmapLoader);
function GetLoader(Ext: string): TBitmapLoader;
function GetSaver(Ext: string): TBitmapLoader;
function GetLoadFilter: string; // Filter for open dialog
function GetSaveFilter: string; // Filter for save dialog
destructor Destroy; override;
end;
ELinearBitmap = class(Exception);
EUnsupportedFileFormat = class(Exception);
TProgressUpdate = procedure(Done: Integer) of object;
var
BitmapLoaders : TBitmapLoaders;
ProgressUpdate : TProgressUpdate = nil;
GrayPal: TPalette; // Grayscale palette
const
BlackPix24 : RGBRec = (B:0;G:0;R:0);
WhitePix24 : RGBRec = (B:255;G:255;R:255);
function GrayPix24(Level: Byte): RGBRec;
// Swap red and blue color components
function RGB2BGR(const Color: TColor): TColor;
function RGB2TColor(R,G,B: Byte): TColor;
function GetRGBRec(R,G,B: Byte): RGBRec; overload;
function GetRGBRec(Color: TColor): RGBRec; overload;
procedure MakeLogPalette(const Pal: TPalette; var PalEntries; ColorCount: Integer = 256);
function MakeHPalette(const Pal: TPalette; ColorCount: Integer = 256): HPALETTE;
function GetFromRGBPalette(var Pal): TPalette;
function GetFromHPalette(const HPal: HPALETTE; ColorCount: Integer=256): TPalette;
function BestColorMatch(Color: TColor; const Palette: TPalette): Byte;
procedure AddLoader(Loader: TBitmapLoader);
procedure DrawHDIBToTBitmap(HDIB: THandle; Bitmap: TBitmap);
procedure DrawDIBToTBitmap(var DIB: TBitmapInfo; Bitmap: TBitmap);
procedure MakeGrayPal(var Palette; ColorCount: Integer);
procedure DeInterleave(Image: TLinearBitmap; Source: PByteArray=nil; Planes: Integer=3);
// Copied from FileUtils to avoid including Forms unit
function ExtractFileExtNoDotUpper(const FileName: string): string;
implementation
function ExtractFileExtNoDotUpper(const FileName: string): string;
var I : Integer;
begin
I := LastDelimiter('.\:',FileName);
if (I>0) and (FileName[I]='.') then Result:=UpperCase(Copy(FileName,I+1,MaxInt))
else Result:='';
end;
function GrayPix24(Level: Byte): RGBRec;
begin
Result.R:=Level;
Result.G:=Level;
Result.B:=Level;
end;
function RGB2TColor(R,G,B: Byte): TColor;
begin
Result:=R or (Integer(G) shl 8) or (Integer(B) shl 16);
end;
function GetRGBRec(R,G,B: Byte): RGBRec;
begin
Result.R:=R; Result.G:=G; Result.B:=B;
end;
function GetRGBRec(Color: TColor): RGBRec;
begin
Result.R:=TPaletteEntry(Color).peRed;
Result.G:=TPaletteEntry(Color).peGreen;
Result.B:=TPaletteEntry(Color).peBlue;
end;
function RGB2BGR(const Color: TColor): TColor;
begin
Result:=Color;
RGBQuad(Result).rgbRed:=RGBQuad(Color).rgbBlue;
RGBQuad(Result).rgbBlue:=RGBQuad(Color).rgbRed;
end;
procedure DrawHDIBToTBitmap(HDIB: THandle; Bitmap: TBitmap);
var BitmapInfo : ^TBitmapInfo;
begin
BitmapInfo:=GlobalLock(HDIB);
if Assigned(BitmapInfo) then
try
DrawDIBToTBitmap(BitmapInfo^,Bitmap);
finally
GlobalUnlock(HDIB);
end;
end;
procedure DrawDIBToTBitmap(var DIB: TBitmapInfo; Bitmap: TBitmap); // BitmapInfo
function DibNumColors(pv: Pointer): Integer;
begin
if PBitmapInfoHeader(pv)^.biSize=sizeof(TBITMAPCOREHEADER) then
Result:=1 shl PBITMAPCOREHEADER(pv)^.bcBitCount
else if PBitmapInfoHeader(pv)^.biClrUsed=0 then
Result:=1 shl PBitmapInfoHeader(pv)^.biBitCount
else Result:=PBitmapInfoHeader(pv)^.biClrUsed;
if (Result>256) then Result:=0;
end;
var
Map : ^Byte;
I : Integer;
BytesPerLine, Colors : DWord;
LogPal: PLogPalette;
begin
with DIB.bmiHeader do
begin
case biBitCount of
1 : Bitmap.PixelFormat:=pf1bit;
4 : Bitmap.PixelFormat:=pf4bit;
8 : Bitmap.PixelFormat:=pf8bit;
16 : Bitmap.PixelFormat:=pf16bit;
24 : Bitmap.PixelFormat:=pf24bit;
32 : Bitmap.PixelFormat:=pf32bit;
else
raise Exception.Create(rsInvalidPixelFormat);
end;
Bitmap.Width:=biWidth;
Bitmap.Height:=biHeight;
Colors:=0;
if (biBitCount<=8) then
begin
Colors:=DibNumColors(@DIB);
GetMem(LogPal,SizeOf(TLogPalette)+SizeOf(TPaletteEntry)*Colors);
try
LogPal^.PalVersion:=$300;
LogPal^.PalNumEntries:=Colors;
for I:=0 to Colors-1 do with LogPal^.palPalEntry[I] do
begin
peRed:=DIB.bmiColors[I].rgbRed;
peGreen:=DIB.bmiColors[I].rgbGreen;
peBlue:=DIB.bmiColors[I].rgbBlue;
peFlags:=PC_RESERVED;
end;
Bitmap.Palette:=CreatePalette(LogPal^);
finally
FreeMem(LogPal);
end;
end;
Map:=Pointer(DWord(@DIB)+biSize+Colors*SizeOf(TRGBQUAD));
if biCompression=BI_RGB then
begin
BytesPerLine:=(biWidth*biBitCount div 8+3) and $fffffffc;
if biHeight<0 then
for I:=0 to -biHeight-1 do
begin
Move(Map^,Bitmap.ScanLine[I]^,BytesPerLine);
Inc(Map,BytesPerLine);
end
else
for I:=biHeight-1 downto 0 do
begin
Move(Map^,Bitmap.ScanLine[I]^,BytesPerLine);
Inc(Map,BytesPerLine);
end;
end
else
begin
if (biBitCount in [16,32]) and (biCompression=BI_BITFIELDS) then Inc(Map,12);
StretchDIBits(Bitmap.Canvas.Handle,
0,0,biWidth,biHeight,
0,0,biWidth,biHeight,
Map,DIB,
DIB_RGB_COLORS,SRCCOPY);
//raise Exception.Create(rsUnsupportedBitmapFormat);
end;
end;
end;
procedure MakeGrayPal(var Palette; ColorCount: Integer);
var
I, C : Integer;
begin
Dec(ColorCount);
For I:=0 to ColorCount do
begin
C:=I*255 div ColorCount;
TPalette(Palette)[I].B:=C;
TPalette(Palette)[I].G:=C;
TPalette(Palette)[I].R:=C;
end;
end;
function BestColorMatch(Color: TColor; const Palette: TPalette): Byte;
var
I, BestDist, Dist : Integer;
begin
Result:=0;
BestDist:=High(BestDist);
with TPaletteEntry(Color) do
for I:=0 to 255 do with Palette[I] do
begin
Dist:=Sqr(R-peRed)+Sqr(G-peGreen)+Sqr(B-peBlue);
if Dist<BestDist then
begin
BestDist:=Dist;
Result:=I;
end;
end;
end;
//==================================================================================================
// TLinearBitmap
//==================================================================================================
constructor TLinearBitmap.Create;
begin
inherited Create;
fPixelFormat:=pfCustom;
end;
constructor TLinearBitmap.Create(Width,Height: Integer; PixFormat: TPixelFormat);
begin
inherited Create;
fPixelFormat:=pfCustom;
New(Width,Height,PixFormat);
end;
constructor TLinearBitmap.Create(Other: TObject);
begin
inherited Create;
fPixelFormat:=pfCustom;
Assign(Other);
end;
procedure TLinearBitmap.New(Width,Height: Integer; PixFormat: TPixelFormat);
begin
if FPresent and (FWidth=Width) and (FHeight=Height) and (PixelFormat=PixFormat) then Exit;
if not (PixFormat in [pf8bit,pf16bit,pf24bit,pf32bit]) then raise ELinearBitmap.Create(rsInvalidPixelFormat);
if (Width<1) or (Height<1) then raise ELinearBitmap.Create(rsInvalidBitmapSize);
if Assigned(Map) then FreeMem(Map);
fWidth:=Width; fHeight:=Height;
if PixFormat=pf8bit then
begin
fPixelSize:=1;
if Palette=nil then GetMem(Palette,SizeOf(TPalette));
end
else
begin
if PixFormat=pf24bit then fPixelSize:=3
else if PixFormat=pf16bit then fPixelSize:=2
else fPixelSize:=4;
if Assigned(Palette) then
begin
FreeMem(Palette);
Palette:=nil;
end;
end;
fBytesPerLine:=Width*PixelSize;
fPixelFormat:=PixFormat;
fSize:=BytesPerLine*Height;
GetMem(Map,Size+1);
fPresent:=True;
end;
procedure TLinearBitmap.Dispose;
begin
if Assigned(Map) then
begin
FreeMem(Map);
Map:=nil;
end;
if Assigned(Palette) then
begin
FreeMem(Palette);
Palette:=nil;
end;
fPresent:=False;
fWidth:=0; fHeight:=0; fSize:=0; fPixelFormat:=pfCustom; fPixelSize:=0;
end;
procedure TLinearBitmap.Assign(Other: TObject);
procedure GetFromTBitmap(Bitmap: TBitmap);
var Y : Integer;
begin
if (Bitmap.Width=0) or (Bitmap.Height=0) then Dispose
else
begin
New(Bitmap.Width,Bitmap.Height,Bitmap.PixelFormat);
for Y:=0 to Height-1 do Move(Bitmap.ScanLine[Y]^,ScanLine[Y]^,BytesPerLine);
if PixelFormat=pf8bit then Palette^:=GetFromHPalette(Bitmap.Palette);
end
end;
var
Bitmap : TBitmap;
Y : Integer;
begin
if Other is TLinearBitmap then
begin
if TLinearBitmap(Other).Present then
begin
New(TLinearBitmap(Other).Width,TLinearBitmap(Other).Height,TLinearBitmap(Other).PixelFormat);
if BytesPerLine=TLinearBitmap(Other).BytesPerLine then Move(TLinearBitmap(Other).Map^,Map^,Size)
else for Y:=0 to Height-1 do Move(TLinearBitmap(Other).ScanLine[Y]^,ScanLine[Y]^,BytesPerLine);
if PixelFormat=pf8bit then Palette^:=TLinearBitmap(Other).Palette^;
end
else Dispose;
end
else if (Other is TBitmap) and (TBitmap(Other).PixelFormat in [pf8bit,pf24bit,pf32bit]) then GetFromTBitmap(TBitmap(Other))
else if Other is TGraphic then
begin
Bitmap:=TBitmap.Create;
try
try
Bitmap.Assign(TPersistent(Other));
if not (Bitmap.PixelFormat in [pf8bit,pf24bit,pf32bit]) then Bitmap.PixelFormat:=pf24bit;
except
// Assign didn't work, try Draw
Bitmap.PixelFormat:=pf24bit;
Bitmap.Width:=TGraphic(Other).Width;
Bitmap.Height:=TGraphic(Other).Height;
Bitmap.Canvas.Draw(0,0,TGraphic(Other));
end;
GetFromTBitmap(Bitmap)
finally
Bitmap.Free;
end
end
else if Other is TPersistent then
begin
Bitmap:=TBitmap.Create;
try
Bitmap.Assign(TPersistent(Other));
if not (Bitmap.PixelFormat in [pf8bit,pf24bit]) then Bitmap.PixelFormat:=pf24bit;
GetFromTBitmap(Bitmap)
finally
Bitmap.Free;
end
end
else inherited;
end;
procedure TLinearBitmap.AssignTo(Other: TObject);
begin
if Other is TBitmap then
begin
if not Present then Exit;
if PixelFormat=pf16bit then TBitmap(Other).PixelFormat:=pf8bit
else TBitmap(Other).PixelFormat:=PixelFormat;
if PixelFormat=pf8bit then TBitmap(Other).Palette:=MakeHPalette(Palette^)
else if PixelFormat=pf16bit then TBitmap(Other).Palette:=MakeHPalette(GrayPal);
TBitmap(Other).Width:=Width; TBitmap(Other).Height:=Height;
PaintToTBitmap(TBitmap(Other));
end
else if Other is TPicture then AssignTo(TPicture(Other).Bitmap)
else inherited;
end;
procedure TLinearBitmap.TakeOver(Other: TLinearBitmap);
begin
Dispose;
fWidth:=Other.fWidth;
fHeight:=Other.fHeight;
fPixelFormat:=Other.fPixelFormat;
fPixelSize:=Other.fPixelSize;
fBytesPerLine:=Other.fBytesPerLine;
fSize:=Other.fSize;
fPresent:=Other.Present; Other.FPresent:=False;
Map:=Other.Map; Other.Map:=nil;
Palette:=Other.Palette; Other.Palette:=nil;
end;
procedure TLinearBitmap.OptimizeMem;
var
Y, NewBytesPerLine : Integer;
NewMap : PByteArray;
begin
NewBytesPerLine:=Width*PixelSize;
if Present and (BytesPerLine<>NewBytesPerLine) then
begin
fSize:=Height*NewBytesPerLine;
GetMem(NewMap,Size+1);
for Y:=0 to Height-1 do Move(Map^[Y*BytesPerLine],NewMap^[Y*NewBytesPerLine],NewBytesPerLine);
FreeMem(Map);
Map:=NewMap;
fBytesPerLine:=NewBytesPerLine;
end;
end;
procedure TLinearBitmap.Clear(Value: Byte);
begin
FillChar(Map^,Size,Value);
end;
procedure TLinearBitmap.ClearColor(Color: TColor);
var
P : Integer;
Pix24 : PRGBRec;
begin
if Color=0 then ZeroMem(Map^,Size)
else if PixelFormat=pf24bit then
begin
Color:=RGB2BGR(Color);
Pix24:=@Map^;
for P:=1 to Width*Height-1 do
begin
PDWord(Pix24)^:=Color;
Inc(Pix24);
end;
Pix24^.R:=(Color shr 16) and $ff;
Pix24^.G:=(Color shr 8) and $ff;
Pix24^.B:=Color and $ff;
end
else Clear(((Color and $ff)*2+((Color shr 8) and $ff)*3+((Color shr 16) and $ff)+3) div 6);
end;
function TLinearBitmap.GetScanLine(Y: Integer): Pointer;
begin
Result:=@Map^[Y*BytesPerLine];
end;
function TLinearBitmap.GetPixel(X,Y: Integer): Pointer;
begin
Result:=@Map^[Y*BytesPerLine+X*PixelSize];
end;
function TLinearBitmap.GetPixelColor(X,Y: Integer): TColor;
begin
case PixelFormat of
pf8bit : with Palette^[Map^[Y*BytesPerLine+X]] do
Result:=RGB2TColor(R,G,B);
pf24bit : with PRGBRec(@Map^[Y*BytesPerLine+X*3])^ do
Result:=RGB2TColor(R,G,B);
pf32bit : with PRGBRec(@Map^[Y*BytesPerLine+X*4])^ do
Result:=RGB2TColor(R,G,B);
else raise Exception.Create(rsInvalidPixelFormat);
end
end;
procedure TLinearBitmap.SetPixelColor(X,Y: Integer; Color: TColor);
begin
case PixelFormat of
pf8bit : Map^[Y*BytesPerLine+X]:=BestColorMatch(Color,Palette^);
pf24bit : with PRGBRec(@Map^[Y*BytesPerLine+X*3])^ do
begin
R:=Color;
G:=Color shr 8;
B:=Color shr 16;
end;
pf32bit : with PRGBRec(@Map^[Y*BytesPerLine+X*4])^ do
begin
R:=Color;
G:=Color shr 8;
B:=Color shr 16;
end;
else raise Exception.Create(rsInvalidPixelFormat);
end;
end;
function TLinearBitmap.IsGrayScale: Boolean;
var
F, C : Integer;
begin
if Present then
case PixelFormat of
pf8bit : begin
IsGrayScale:=True;
for F:=0 to 255 do if (Palette^[F].R<>F) or (Palette^[F].G<>F) or (Palette^[F].B<>F) then
begin
IsGrayScale:=False; Break;
end;
end;
pf16bit : IsGrayScale:=True;
pf24bit : begin
IsGrayScale:=True;
C:=0;
for F:=0 to Width*Height-1 do
begin
if (Map^[C]<>Map^[C+1]) or (Map^[C]<>Map^[C+2]) then
begin
IsGrayScale:=False; Break;
end;
Inc(C,3);
end;
end;
else IsGrayScale:=False;
end
else IsGrayScale:=False;
end;
destructor TLinearBitmap.Destroy;
begin
if Present then Dispose;
inherited Destroy;
end;
function TLinearBitmap.LoadFromFile(const FileName: string): TBitmapLoader;
var
Ext : string;
begin
Ext:=ExtractFileExtNoDotUpper(FileName);
Result:=BitmapLoaders.GetLoader(Ext);
Dispose;
Result.LoadFromFile(FileName,Ext,Self);
end;
procedure TLinearBitmap.SaveToFile(const FileName: string);
var
Ext : string;
begin
Ext:=ExtractFileExtNoDotUpper(FileName);
BitmapLoaders.GetSaver(Ext).SaveToFile(FileName,Ext,Self)
end;
procedure TLinearBitmap.LoadFromStream(Stream: TSeekableStream; const FormatExt: string);
var
BitmapLoader : TBitmapLoader;
begin
BitmapLoader:=BitmapLoaders.GetLoader(FormatExt);
Dispose;
BitmapLoader.LoadFromStream(Stream,FormatExt,Self);
end;
procedure TLinearBitmap.LoadFromStream(Stream: TStream; const FormatExt: string);
var
TmpStream : TDelphiFilterStream;
begin
TmpStream:=TDelphiFilterStream.Create(Stream);
try
LoadFromStream(TmpStream,FormatExt);
finally
TmpStream.Free;
end;
end;
procedure TLinearBitmap.SaveToStream(Stream: TStream; const FormatExt: string);
var
TmpStream : TDelphiFilterStream;
begin
TmpStream:=TDelphiFilterStream.Create(Stream);
try
SaveToStream(TmpStream,FormatExt);
finally
TmpStream.Free;
end;
end;
procedure TLinearBitmap.SaveToStream(Stream: TSeekableStream; const FormatExt: string);
begin
BitmapLoaders.GetSaver(FormatExt).SaveToStream(Stream,FormatExt,Self);
end;
// Return size
const ReserveForPalette = 256*SizeOf(TRGBQuad);
function TLinearBitmap.MakeDIB(out Bitmap: PBitmapInfo): Integer;
var
Pix : ^Byte;
LinLen, Y : Integer;
begin
case PixelFormat of
pf8bit : begin
LinLen:=(Width+3) and $fffffffc;
Result:=SizeOf(TBitmapInfoHeader)+ReserveForPalette+LinLen*Height;
GetMem(Bitmap,Result);
ZeroMem(Bitmap^.bmiHeader,SizeOf(TBitmapInfoHeader));
with Bitmap^.bmiHeader do // BITMAPINFOHEADER
begin
biSize:=SizeOf(TBitmapInfoHeader);
biWidth:=Width;
biHeight:=Height;
biPlanes:=1;
biBitCount:=8;
biClrUsed:=256;
biClrImportant:=256;
biCompression:=BI_RGB;
biSizeImage:=LinLen*Height;
end;
for Y:=0 to 255 do with Bitmap^.bmiColors[Y] do
begin
rgbRed:=Palette^[Y].R; rgbGreen:=Palette^[Y].G; rgbBlue:=Palette^[Y].B;
rgbReserved:=0;
end;
Pix:=Pointer(Integer(Bitmap)+SizeOf(TBitmapInfoHeader)+ReserveForPalette);
for Y:=Height-1 downto 0 do
begin
Move(ScanLine[Y]^,Pix^,BytesPerLine);
Inc(Pix,LinLen);
end;
end;
pf24bit : begin
LinLen:=(Width*3+3) and $fffffffc;
Result:=SizeOf(TBitmapInfoHeader)+LinLen*Height;
GetMem(Bitmap,Result);
ZeroMem(Bitmap^.bmiHeader,SizeOf(TBitmapInfoHeader));
with Bitmap^.bmiHeader do // BITMAPINFO HEADER
begin
biSize:=SizeOf(TBitmapInfoHeader);
biWidth:=Width;
biHeight:=Height;
biPlanes:=1;
biBitCount:=24;
biCompression:=BI_RGB;
biSizeImage:=LinLen*Height;
end;
Pix:=Pointer(Integer(Bitmap)+SizeOf(TBitmapInfoHeader));
for Y:=Height-1 downto 0 do
begin
Move(ScanLine[Y]^,Pix^,BytesPerLine);
Inc(Pix,LinLen);
end;
end;
else
begin
Bitmap:=nil;
MakeDIB:=0;
end;
end;
end;
procedure TLinearBitmap.GetFromDIB(var DIB: TBitmapInfo); // Windows BITMAPINFO struct
var
C, Y, LinInc, ColorCount : Integer;
OPix, NPix : ^Byte;
PalEntry : ^TRGBQuad;
Bitmap : TBitmap;
begin
with DIB.bmiHeader do
if (biBitCount in [8,24]) and (biCompression=BI_RGB) then
begin
if biBitCount=8 then New(biWidth,Abs(biHeight),pf8bit)
else New(biWidth,Abs(biHeight),pf24bit);
case biBitCount of
8 : begin
if biClrUsed=0 then ColorCount:=256
else ColorCount:=biClrUsed;
PalEntry:=Pointer(DWord(@DIB)+biSize);
for C:=0 to ColorCount-1 do
begin
Palette^[C].R:=PalEntry^.rgbRed; Palette^[C].G:=PalEntry^.rgbGreen; Palette^[C].B:=PalEntry^.rgbBlue;
Inc(PalEntry);
end;
for C:=ColorCount to 255 do Palette^[C]:=BlackPix24;
LinInc:=(Width+3) and $fffffffc; // Rund op til nærmeste 4 bytes
OPix:=Pointer(PalEntry);
end;
24 : begin
LinInc:=(Width*3+3) and $fffffffc; // Rund op til nærmeste 4 bytes
OPix:=Pointer(DWord(@DIB)+biSize);
end;
else
raise ELinearBitmap.Create(rsInvalidPixelFormat);
end;
if biHeight<0 then
for Y:=0 to Height-1 do
begin
NPix:=ScanLine[Y];
Move(OPix^,NPix^,BytesPerLine);
Inc(OPix,LinInc);
end
else
for Y:=Height-1 downto 0 do // Upside-down
begin
NPix:=ScanLine[Y];
Move(OPix^,NPix^,BytesPerLine);
Inc(OPix,LinInc);
end;
end
else
begin
Bitmap:=TBitmap.Create;
try
DrawDIBToTBitmap(DIB,Bitmap);
{if Bitmap.PixelFormat in [pf1bit,pf4bit] then
begin
GetFromTBitmap(Bitmap);
Palette^:=GetFromHPalette(Bitmap.Palette);
Bitmap.PixelFormat:=pf8bit;
end
else} if not (Bitmap.PixelFormat in [pf8bit,pf24bit]) then
begin
Bitmap.PixelFormat:=pf24bit;
Assign(Bitmap);
end
else Assign(Bitmap);
finally
Bitmap.Free;
end;
end;
end;
procedure TLinearBitmap.GetFromHDIB(HDIB: HBitmap);
var
Bitmap : ^TBitmapInfo;
begin
if HDIB<>0 then
try
Bitmap:=GlobalLock(HDIB);
if Bitmap=nil then Exit;
GetFromDIB(Bitmap^);
finally
GlobalUnLock(HDIB);
end;
end;
function TLinearBitmap.GetFromClipboard;
var
HDIB : HBitmap;
Bitmap : TBitmap;
Metafile : TMetafile;
begin
if OpenClipboard(0) then
try
if IsClipboardFormatAvailable(CF_DIB) then // Get as bitmap
begin
HDIB:=GetClipboardData(CF_DIB);
GetFromHDIB(HDIB);
Result:=(HDIB<>0) and Present;
end
else if IsClipboardFormatAvailable(CF_ENHMETAFILE) then // Get as Windows metafile
begin
Metafile:=TMetafile.Create;
try
Metafile.LoadFromClipboardFormat(0,0,0);
Bitmap:=TBitmap.Create;
try
Bitmap.PixelFormat:=pf24bit;
Bitmap.Width:=Metafile.Width;
Bitmap.Height:=Metafile.Height;
Bitmap.Canvas.Draw(0,0,Metafile);
Assign(Bitmap);
finally
Bitmap.Free;
end;
finally
Metafile.Free;
end;
Result:=Present;
end
else Result:=False;
finally
CloseClipboard;
end
else Result:=False;
end;
procedure TLinearBitmap.CopyToClipboard;
var
PDIB : Pointer;
BitmapInfo : PBitmapInfo;
HDIB : THandle;
MemSize : Integer;
begin
if not Present then Exit;
if OpenClipboard(0) then
try
MemSize:=MakeDIB(BitmapInfo);
try
HDIB:=GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE,MemSize);
PDIB:=GlobalLock(HDIB);
try
Move(BitmapInfo^,PDIB^,MemSize);
finally
GlobalUnLock(HDIB);
end;
EmptyClipboard;
SetClipboardData(CF_DIB,HDIB);
finally
FreeMem(BitmapInfo);
end;
finally
CloseClipboard;
end
end;
procedure TLinearBitmap.PasteImage(Source: TLinearBitmap; X,Y: Integer);
var
CopySize : Integer;
DestRect : TRect;
SrcLine, DestLine : PByte;
begin
if PixelFormat<>Source.PixelFormat then raise ELinearBitmap.Create(rsInvalidPixelFormat);
if not IntersectRect(DestRect,Rect(0,0,Width,Height),Bounds(X,Y,Source.Width,Source.Height)) then Exit;
SrcLine:=Source.Pixel[Max(0,-X),Max(0,-Y)];
DestLine:=Pixel[DestRect.Left,DestRect.Top];
CopySize:=(DestRect.Right-DestRect.Left)*PixelSize;
for Y:=DestRect.Top to DestRect.Bottom-1 do
begin
Move(SrcLine^,DestLine^,CopySize);
Inc(SrcLine,Source.BytesPerLine);
Inc(DestLine,BytesPerLine);
end;
end;
procedure TLinearBitmap.ResizeCanvas(XSiz,YSiz,XPos,YPos: Integer; Color: TColor);
var
OldMap : PByteArray;
Y, XStart, OldBytesPerLine, LineMove, Bottom : Integer;
begin
if (XSiz<1) or (YSiz<1) then
begin
Dispose;
Exit;
end;
if not Present then
begin
New(XSiz,YSiz,pf8bit);
Palette^:=GrayPal;
ClearColor(Color);
Exit;
end;
if (XSiz=Width) and (YSiz=Height) and (XPos=0) and (YPos=0) then Exit;
OldBytesPerLine:=BytesPerLine;
fBytesPerLine:=XSiz*PixelSize;
fSize:=BytesPerLine*YSiz;
OldMap:=Map;
GetMem(Map,Size+1);
if XPos>0 then XStart:=0
else
begin
XStart:=-XPos*PixelSize;
XPos:=0;
end;
XPos:=XPos*PixelSize;
LineMove:=OldBytesPerLine-XStart;
if LineMove+XPos>BytesPerLine then LineMove:=BytesPerLine-XPos;
Bottom:=Min(Height-1,YSiz-YPos-1);
fWidth:=XSiz; fHeight:=YSiz;