-
Notifications
You must be signed in to change notification settings - Fork 3
/
PV_Bitmap.pas
1028 lines (833 loc) · 24.5 KB
/
PV_Bitmap.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 PV_Bitmap;
//Lazzy Image Viewer
//github.com/PascalVault
//License: MIT
{$inline on}
interface
uses Classes, Graphics, SysUtils, DateUtils, Math, IntfGraphics, FPImage, Dialogs, PV_Streams;
const PaletteMono: array[0..1] of Cardinal = ($FFFFFFFF, $FF000000);
type
{$IFDEF WINDOWS}
TPix = packed record
case Byte of
1: (B,G,R,A: Byte);
2: (RGBA: Cardinal);
end;
{$ELSE}
TPix = packed record
case Byte of
1: (B,G,R,A: Byte);
2: (RGBA: Cardinal);
end;
{$ENDIF}
TPal = record
R,G,B: Byte;
end;
TPix3 = Cardinal;
PPixArray = ^TPixArray;
TPixArray = array[0..32766] of TPix;
PPix = ^TPix;
TPalArray = array of TPix;
TDither = (ddNone, ddFloyd, ddBurkes, ddStucki, ddJarvis, ddAtkinson, ddSierra2, ddSierra3, ddSierra4);
TPixInt = record
R,G,B: Integer;
end;
{ TPV_Bitmap }
TPV_Bitmap = class
public
function GetPixel(X,Y: Integer): TPix;
procedure SetPixel(X,Y: Integer; Val: TPix); inline;
private
FData: array of TPix;
FWidth: Integer;
FHeight: Integer;
function GetWidth: Integer;
procedure SetWidth(Val: Integer);
function GetHeight: Integer;
procedure SetHeight(Val: Integer);
function GetScanline(Y: Integer): Pointer;
public
FPalette: array of TPix;
PaletteLen: Integer;
FormatName: String;
procedure SetMono(X,Y: Integer; B: Byte); inline;
procedure SetRGBA(X,Y: Integer; R,G,B,A: Byte); inline;
procedure SetRGB(X,Y: Integer; R,G,B: Byte); inline;
procedure Set32(X,Y: Integer; Val: Cardinal); inline; overload;
procedure Set32(X,Y: Integer; Val: TPal); inline; overload;
procedure SetR(X,Y: Integer; R: Byte); inline;
procedure SetG(X,Y: Integer; G: Byte); inline;
procedure SetB(X,Y: Integer; B: Byte); inline;
procedure SetA(X,Y: Integer; A: Byte); inline;
procedure AddPal(R,G,B,A: Byte);
procedure SetPal(X,Y: Integer; Index: Byte); inline;
function GetPalIndex(X,Y: Integer): Integer;
procedure AddPalette(Pal: array of TPal; Len: Integer);
procedure ClearPalette;
property Scanline[Y: Integer]: Pointer read GetScanline;
property Pixel[X,Y: Integer]: TPix read GetPixel write SetPixel; default;
property Width: Integer read GetWidth write SetWidth;
property Height: Integer read GetHeight write SetHeight;
constructor Create;
procedure SetSize(AWidth, AHeight: Integer);
function LoadFromFile(Filename: String): Boolean;
procedure SaveToFile(Filename: String); overload;
procedure SaveToFile(Filename: String; Compression: Byte); overload;
procedure Draw(Bitmap: TPV_Bitmap; Left,Top, AWidth,AHeight: Integer);
procedure FlipH;
procedure FlipV;
procedure CopyFrom(Bitmap: TPV_Bitmap);
procedure CopyFrom(Bitmap: TBitmap);
procedure CopyFrom(Bitmap: TFPMemoryImage);
function GetPiece(AX,AY, AWidth,AHeight: Integer): TBitmap;
procedure ReduceColors(MaxColors: Byte; Dither: TDither = ddFloyd);
procedure Grayscale(MaxColors: Byte; Dither: TDither = ddFloyd);
procedure BlackWhite(Dither: TDither = ddFloyd);
procedure Highcolor(Bits: Integer = 16);
procedure Resize(AWidth, AHeight: Integer);
procedure ResizePercent(AWidth, AHeight: Integer);
procedure RemoveAlpha;
procedure Opaque;
function ToBitmap: TBitmap;
procedure DrawTo(X,Y: Integer; Canvas: TCanvas);
procedure DrawTo(Dest: TRect; Canvas: TCanvas); overload;
procedure Trim(RR,GG,BB: Byte);
function CountColors: Integer;
public
end;
TPV_BitmapReader = function(Bmp: TPV_Bitmap; Str: TStream): Boolean;
TPV_BitmapWriter = procedure(Bmp: TPV_Bitmap; Str: TStream; Compression: Byte);
TXBmpFormat = record
Ext: String;
Reader: TPV_BitmapReader;
Writer: TPV_BitmapWriter;
Name: String;
end;
{ TPV_BitmapFormat }
TPV_BitmapFormat = class
private
FList: array of TXBmpFormat;
FCount: Integer;
public
property Count: Integer read FCount;
procedure Item(Index: Integer; out Ext,Name: String; out Reader: TPV_BitmapReader; out Writer: TPV_BitmapWriter);
constructor Create;
function FindReader(Ext: String; out Format: String): TPV_BitmapReader;
function FindWriter(Ext: String): TPV_BitmapWriter;
function FindName(Ext: String): String;
procedure Add(Ext: String; Reader: TPV_BitmapReader; Writer: TPV_BitmapWriter; Name: String);
end;
function MakePix(R,G,B,A: Byte): TPix;
function Clip(V: Extended): Byte;
function SamePix(P,R: TPix; Threshold: Byte = 5): Boolean;
function Limit(Min,Max,Val: Integer): Integer;
procedure rgb2cmyk(R,G,B: Byte; out C,M,Y,K: Byte);
procedure cmyk2rgb(C,M,Y,K: Byte; out R,G,B: Byte);
procedure rgb2yuv(R,G,B: Byte; out Y,U,V: Byte);
procedure Unrle_AMI(Src: TStream; Dest: TStream; packedSize: Integer);
procedure Unrle_GG(Src: TStream; Dest: TStream; packedSize: Integer);
procedure Unrle_PAC(Src: TStream; Dest: TStream; idByte, packByte, specialByte: Byte);
procedure UnRle_PGC(src: TStream; dest: TStream; packedSize: Integer);
procedure UnRle_CPR(src: TStream; dest: TStream; packedSize: Integer);
procedure Unrle_TGA(Src: TStream; Dest: TStream; packedSize: Integer; unitSize: Integer);
procedure Unrle_RGB(Src: TStream; Dest: TStream; packedSize: Integer);
procedure Unrle_PCX(Src: TStream; Dest: TStream; packedSize: Integer);
procedure Unrle_CUT(Src: TStream; Dest: TStream; packedSize: Integer; unitSize: Integer);
procedure Unrle_DLP(Src: TStream; Dest: TStream; packedSize: Integer; escapeByte: Byte);
procedure Unrle_PSD(Src: TStream; Dest: TStream; packedSize: Integer; unitSize: Integer = 1); //psd,mac
procedure UnRle_LBM(Src: TStream; Dest: TStream; packedSize: Integer);
{
procedure UnRleBMP8(source: TQFile; out dest: TQFile; packedSize: Integer; width, height: Integer);
procedure UnRle4BT(source: TQFile; out dest: TQFile; packedSize: Integer);
}
function hexInt(hex: String): Integer;
var BitmapFormats: TPV_BitmapFormat;
implementation
uses PV_Grayscale, PV_Palette;
{$INCLUDE RLE.pas}
function MakePix(R, G, B, A: Byte): TPix;
begin
Result.RGBA := B + (G shl 8) + (R shl 16) + (A shl 24);
end;
function Clip(V: Extended): Byte;
begin
if V > 255 then Result := 255
else if V < 0 then Result := 0
else Result := Round(V);
end;
function SamePix(P, R: TPix; Threshold: Byte): Boolean;
begin
Result := False;
if abs(P.R - R.R) > Threshold then Exit;
if abs(P.G - R.G) > Threshold then Exit;
if abs(P.B - R.B) > Threshold then Exit;
Result := True;
end;
function Limit(Min, Max, Val: Integer): Integer;
begin
if Val < Min then Exit(Min);
if Val > Max then Exit(Max);
Exit(Val);
end;
procedure rgb2yuv(R,G,B: Byte; out Y,U,V: Byte);
var YY, UU, VV: Extended;
begin
YY := +0.2990 * R + 0.5870 * G + 0.1140 * B;
UU := 128 -0.1687 * R - 0.3313 * G + 0.5000 * B;
VV := 128 +0.5000 * R - 0.4187 * G - 0.0813 * B;
Y := floor(YY);
V := floor(VV);
U := floor(UU);
end;
procedure lab2rgb(L1,A1,B1: Byte; out R,G,B: Byte);
var LL,AA,BBB: Extended;
Y,X,Z: Extended;
Y3,X3,Z3: Extended;
RR,GG,BB: Extended;
begin
//http://www.easyrgb.com/
LL := L1 / 2.55;
AA := A1 - 128;
BBB := B1 - 128;
//CIELAB -> XYZ
Y := (LL + 16 ) / 116;
X := AA / 500 + Y;
Z := Y - BBB / 200;
Y3 := power(Y,3);
X3 := power(X,3);
Z3 := power(Z,3);
if ( Y3 > 0.008856 ) then Y := Y3
else Y := ( Y - 16 / 116 ) / 7.787;
if ( X3 > 0.008856 ) then X := X3
else X := ( X - 16 / 116 ) / 7.787;
if ( Z3 > 0.008856 ) then Z := Z3
else Z := ( Z - 16 / 116 ) / 7.787;
X := 95.047 * X;
Y := 100.000 * Y;
Z := 108.883 * Z;
//XYZ -> RGB
X := X / 100;
Y := Y / 100;
Z := Z / 100;
rr := X * 3.2406 + Y * -1.5372 + Z * -0.4986;
gg := X * -0.9689 + Y * 1.8758 + Z * 0.0415;
bb := X * 0.0557 + Y * -0.2040 + Z * 1.0570;
if ( rr > 0.0031308 ) then rr := 1.055 * power(rr, 1 / 2.4 ) - 0.055
else rr := 12.92 * rr;
if ( gg > 0.0031308 ) then gg := 1.055 * power(gg, 1 / 2.4 ) - 0.055
else gg := 12.92 * gg;
if ( bb > 0.0031308 ) then bb := 1.055 * power(bb, 1 / 2.4 ) - 0.055
else bb := 12.92 * bb;
rr := rr * 255;
gg := gg * 255;
bb := bb * 255;
r := clip(rr);
g := clip(gg);
b := clip(bb);
end;
procedure rgb2cmyk(R,G,B: Byte; out C,M,Y,K: Byte);
var RR,GG,BB,KK: Extended;
Temp: Extended;
begin
RR := R/255;
GG := G/255;
BB := B/255;
KK := 1-max(RR,max(GG,BB));
Temp := 1-KK;
if Temp = 0 then Temp := 0.01;
C := Round(100*(1-RR-KK) / Temp);
M := Round(100*(1-GG-KK) / Temp);
Y := Round(100*(1-BB-KK) / Temp);
if C>100 then C := 100;
if M>100 then M := 100;
if Y>100 then Y := 100;
K := Round(100*KK);
if K>100 then K := 100;
end;
procedure cmyk2rgb(C, M, Y, K: Byte; out R, G, B: Byte);
var Temp: Extended;
CC,MM,YY,KK: Extended;
begin
CC := C/100;
MM := M/100;
YY := Y/100;
KK := K/100;
Temp := (1-KK);
R := Clip(255* (1-CC) * Temp);
G := Clip(255* (1-MM) * Temp);
B := Clip(255* (1-YY) * Temp);
end;
procedure TPV_Bitmap.Draw(Bitmap: TPV_Bitmap; Left,Top, AWidth,AHeight: Integer);
begin
//FBitmap.Canvas.StretchDraw(Rect(Left,Top, Left+AWidth, Top+AHeight), Bitmap.FBitmap);
//TODO
end;
procedure TPV_Bitmap.FlipV;
var Old: TPV_Bitmap;
y: Integer;
begin
Old := TPV_Bitmap.Create;
Old.CopyFrom(Self);
for y:=0 to FHeight-1 do begin
Move(Old.Scanline[FHeight-y-1]^, Self.Scanline[y]^, FWidth*4);
end;
Old.Free;
end;
procedure TPV_Bitmap.FlipH;
var Old: TPV_Bitmap;
x,y: Integer;
begin
Old := TPV_Bitmap.Create;
Old.CopyFrom(Self);
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
Self[x,y] := Old[FWidth-x-1,y];
end;
Old.Free;
end;
procedure TPV_Bitmap.CopyFrom(Bitmap: TPV_Bitmap);
var x,y: Integer;
begin
SetSize(Bitmap.Width, Bitmap.Height);
for y:=0 to Height-1 do
Move(Bitmap.Scanline[y]^, Self.Scanline[y]^, 4*Bitmap.Width);
end;
{$IFDEF WINDOWS}
procedure TPV_Bitmap.CopyFrom(Bitmap: TBitmap);
var x,y: Integer;
R: TPix;
Bpp: TPixelFormat;
P: PByteArray;
begin
SetSize(Bitmap.Width, Bitmap.Height);
Bpp := Bitmap.PixelFormat;
for y:=0 to Bitmap.Height-1 do begin
P := Bitmap.Scanline[y];
case Bpp of
pf32bit: for x:=0 to Bitmap.Width-1 do
Self.SetRGBA(x,y, P^[4*x+2], P^[4*x+1], P^[4*x], P^[4*x+3]);
pf24bit: for x:=0 to Bitmap.Width-1 do
Self.SetRGB(x,y, P^[3*x+2], P^[3*x+1], P^[3*x]);
end;
end;
end;
{$ELSE}
procedure TPV_Bitmap.CopyFrom(Bitmap: TBitmap);
var x,y: Integer;
R: TPix;
Bpp: TPixelFormat;
P: PByteArray;
begin
SetSize(Bitmap.Width, Bitmap.Height);
Bpp := Bitmap.PixelFormat;
for y:=0 to Bitmap.Height-1 do begin
P := Bitmap.Scanline[y];
case Bpp of
pf32bit: for x:=0 to Bitmap.Width-1 do
Self.SetRGBA(x,y, P^[4*x+2], P^[4*x+1], P^[4*x], P^[4*x+3]);
pf24bit: for x:=0 to Bitmap.Width-1 do
Self.SetRGB(x,y, P^[3*x+2], P^[3*x+1], P^[3*x]);
end;
end;
end;
{$ENDIF}
procedure TPV_Bitmap.CopyFrom(Bitmap: TFPMemoryImage);
var x,y: Integer;
Col: TFPColor;
P: TPix;
begin
SetSize(Bitmap.Width, Bitmap.Height);
for y:=0 to Bitmap.Height-1 do
for x:=0 to Bitmap.Width-1 do begin
{if Bitmap.UsePalette then
Col := Bitmap.Palette[Bitmap.Palette[x, y]]
else }
Col := Bitmap.Colors[x, y];
P.R := Col.Red shr 8;
P.G := Col.Green shr 8;
P.B := Col.Blue shr 8;
P.A := Col.Alpha shr 8;
Self.SetRGBA(x,y, P.R, P.G, P.B, P.A);
end;
end;
procedure TPV_Bitmap.ReduceColors(MaxColors: Byte; Dither: TDither);
begin
PV_Palette.ReduceColors(Self, MaxColors, Dither);
end;
procedure TPV_Bitmap.Grayscale(MaxColors: Byte; Dither: TDither);
begin
PV_Grayscale.Grayscale(Self, MaxColors, Dither);
end;
procedure TPV_Bitmap.BlackWhite(Dither: TDither);
begin
PV_Grayscale.BlackWhite(Self, Dither);
end;
procedure TPV_Bitmap.Highcolor(Bits: Integer);
var x,y: Integer;
P: TPix;
begin
if Bits = 15 then begin
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
P := Self[x,y];
P.R := Byte(P.R shr 3) shl 3;
P.G := Byte(P.G shr 3) shl 3;
P.B := Byte(P.B shr 3) shl 3;
Self.SetRGBA(x,y, P.R, P.G, P.B, P.A);
end;
end
else begin
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
P := Self[x,y];
P.R := Byte(P.R shr 3) shl 3;
P.G := Byte(P.G shr 2) shl 2;
P.B := Byte(P.B shr 3) shl 3;
Self.SetRGBA(x,y, P.R, P.G, P.B, P.A);
end;
end;
end;
procedure TPV_Bitmap.Resize(AWidth, AHeight: Integer);
var Tmp,Tmp2: TBitmap;
begin
Tmp := Self.ToBitmap;
Tmp2 := TBitmap.Create;
Tmp2.PixelFormat := pf32bit;
Tmp2.SetSize(AWidth, AHeight);
Tmp2.Canvas.StretchDraw(Rect(0,0, AWidth, AHeight), Tmp);
Tmp.Free;
CopyFrom(Tmp2);
Tmp2.Free;
end;
procedure TPV_Bitmap.ResizePercent(AWidth, AHeight: Integer);
begin
Resize(Round(AWidth * FWidth/100), Round(AHeight * FHeight/100));
end;
procedure TPV_Bitmap.RemoveAlpha;
var x,y: Integer;
P: TPix;
begin
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
P := Self[x,y];
P.A := 255;
Self.SetRGB(x,y, P.R,P.G,P.B);
end;
end;
procedure TPV_Bitmap.Opaque;
var x,y: Integer;
P: TPix;
A,A2: Extended;
BG: TPix;
begin
Bg := MakePix(255,255,255,255);
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
P := Self[x,y];
A := P.A/255;
A2 := 1-A;
P.R := Clip(P.R * A + BG.R * A2);
P.G := Clip(P.G * A + BG.G * A2);
P.B := Clip(P.B * A + BG.B * A2);
Self.SetRGB(x,y, P.R,P.G,P.B);
end;
end;
{$IFDEF WINDOWS}
function TPV_Bitmap.ToBitmap: TBitmap;
var x,y: Integer;
P,R: PPixArray;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf32bit;
Result.SetSize(FWidth, FHeight);
for y:=0 to FHeight-1 do begin
P := Result.Scanline[y];
R := Scanline[y];
for x:=0 to FWidth-1 do begin
P^[x].RGBA := R^[x].RGBA;
end;
end;
end;
{$ELSE}
function TPV_Bitmap.ToBitmap: TBitmap;
var x,y: Integer;
R: PPixArray;
Color: TColor;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf32bit;
Result.SetSize(FWidth, FHeight);
for y:=0 to FHeight-1 do begin
R := Scanline[y];
for x:=0 to FWidth-1 do begin
Color := R^[x].R + (R^[x].G shl 8) + (R^[x].B shl 16);
Result.Canvas.Pixels[x,y] := Color;
end;
end;
end;
{$ENDIF}
{$IFDEF WINDOWS}
function TPV_Bitmap.GetPiece(AX, AY, AWidth, AHeight: Integer): TBitmap;
var x,y: Integer;
P,R: PPixArray;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf32bit;
Result.SetSize(AWidth, AHeight);
AWidth := Min(Width, AWidth);
AHeight := Min(Height, AHeight);
for y:=0 to AHeight-1 do begin
P := Result.Scanline[y];
R := Scanline[AY+y];
for x:=0 to AWidth-1 do begin
P^[x].RGBA := R^[AX+x].RGBA;
end;
end;
end;
{$ELSE}
function TPV_Bitmap.GetPiece(AX, AY, AWidth, AHeight: Integer): TBitmap;
var x,y: Integer;
P: PByteArray;
R: PPixArray;
Color: TColor;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf24bit;
Result.SetSize(AWidth, AHeight);
AWidth := Min(Width, AWidth);
AHeight := Min(Height, AHeight);
for y:=0 to AHeight-1 do begin
R := Scanline[AY+y];
for x:=0 to AWidth-1 do begin
Color := R^[AX+x].R + (R^[AX+x].G shl 8) + (R^[AX+x].B shl 16);
Result.Canvas.Pixels[x,y] := Color;
end;
end;
end;
{$ENDIF}
procedure TPV_Bitmap.DrawTo(X,Y: Integer; Canvas: TCanvas);
var Bmp: TBitmap;
begin
Bmp := Self.ToBitmap;
Canvas.Draw(X, Y, Bmp);
Bmp.Free;
end;
procedure TPV_Bitmap.DrawTo(Dest: TRect; Canvas: TCanvas);
var Bmp: TBitmap;
begin
Bmp := Self.ToBitmap;
Canvas.StretchDraw(Dest, Bmp);
Bmp.Free;
end;
procedure TPV_Bitmap.Trim(RR, GG, BB: Byte);
var L,T,R,B: Integer;
P: TPix;
Tmp: TBitmap;
procedure LeftMargin;
var x,y: Integer;
begin
L := FWidth-1;
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
P := Self[x,y];
if (P.R <> 255) or (P.G <> 255) or (P.B <> 255) then begin
L := Min(x, L);
break;
end;
end;
end;
procedure RightMargin;
var x,y: Integer;
begin
R := 0;
for y:=0 to FHeight-1 do
for x:=FWidth-1 downto 0 do begin
P := Self[x,y];
if (P.R <> 255) or (P.G <> 255) or (P.B <> 255) then begin
R := Max(x, R);
end;
end;
end;
procedure TopMargin;
var x,y: Integer;
begin
T := FHeight-1;
for x:=0 to FWidth-1 do
for y:=0 to FHeight-1 do begin
P := Self[x,y];
if (P.R <> 255) or (P.G <> 255) or (P.B <> 255) then begin
T := Min(y, T);
end;
end;
end;
procedure BottomMargin;
var x,y: Integer;
begin
B := 0;
for x:=0 to FWidth-1 do
for y:=FHeight-1 downto 0 do begin
P := Self[x,y];
if (P.R <> 255) or (P.G <> 255) or (P.B <> 255) then begin
B := Max(y, B);
end;
end;
end;
begin
LeftMargin;
RightMargin;
BottomMargin;
TopMargin;
Tmp := Self.GetPiece(L, T, R-L, B-T);
Self.CopyFrom(Tmp);
Tmp.Free;
end;
function TPV_Bitmap.CountColors: Integer;
var Map: array of array of array of Byte;
x,y: Integer;
P: TPix;
i,j,k: Integer;
begin
SetLength(Map, 256,256,256); //16 MB
for i:=0 to 255 do
for j:=0 to 255 do
for k:=0 to 255 do Map[i][j][k] := 0;
for y:=0 to FHeight-1 do
for x:=0 to FWidth-1 do begin
P := Self.Pixel[x,y];
Map[P.R][P.G][P.B] := 1;
end;
Result := 0;
for i:=0 to 255 do
for j:=0 to 255 do
for k:=0 to 255 do if Map[i][j][k] <> 0 then Inc(Result);
end;
function TPV_Bitmap.GetPixel(X,Y: Integer): TPix;
var P: PPixArray;
begin
P := Scanline[Y];
Result := P^[X];
end;
procedure TPV_Bitmap.SetPixel(X,Y: Integer; Val: TPix);
var P: PPixArray;
begin
P := Scanline[Y];
P^[X] := Val;
end;
function TPV_Bitmap.GetWidth: Integer;
begin
Result := FWidth;
end;
procedure TPV_Bitmap.SetWidth(Val: Integer);
begin
FWidth := Val;
SetLength(FData, FWidth*FHeight);
end;
function TPV_Bitmap.GetHeight: Integer;
begin
Result := FHeight;
end;
procedure TPV_Bitmap.SetHeight(Val: Integer);
begin
FHeight := Val;
SetLength(FData, FWidth*FHeight);
end;
function TPV_Bitmap.GetScanline(Y: Integer): Pointer;
begin
Result := @FData[Y* FWidth];
end;
procedure TPV_Bitmap.SetMono(X, Y: Integer; B: Byte);
begin
FData[Y* FWidth + X].RGBA := PaletteMono[B];
end;
{$IFDEF WINDOWS}
procedure TPV_Bitmap.SetRGBA(X, Y: Integer; R, G, B, A: Byte);
begin
FData[Y* FWidth + X].RGBA := B + (G shl 8) + (R shl 16) + (A shl 24);
end;
procedure TPV_Bitmap.SetRGB(X, Y: Integer; R, G, B: Byte);
begin
FData[Y* FWidth + X].RGBA := B + (G shl 8) + (R shl 16) + (255 shl 24);
end;
{$ELSE}
procedure TPV_Bitmap.SetRGBA(X, Y: Integer; R, G, B, A: Byte);
begin
FData[Y* FWidth + X].R := R;
FData[Y* FWidth + X].G := G;
FData[Y* FWidth + X].B := B;
FData[Y* FWidth + X].A := A;
end;
procedure TPV_Bitmap.SetRGB(X, Y: Integer; R, G, B: Byte);
begin
FData[Y* FWidth + X].R := R;
FData[Y* FWidth + X].G := G;
FData[Y* FWidth + X].B := B;
FData[Y* FWidth + X].A := 255;
end;
{$ENDIF}
procedure TPV_Bitmap.Set32(X, Y: Integer; Val: Cardinal);
begin
FData[Y* FWidth + X].RGBA := Val;
end;
procedure TPV_Bitmap.Set32(X, Y: Integer; Val: TPal);
begin
FData[Y* FWidth + X].RGBA := Val.B + (Val.G shl 8) + (Val.R shl 16) + (255 shl 24);
end;
procedure TPV_Bitmap.SetR(X, Y: Integer; R: Byte);
begin
FData[Y* FWidth + X].R := R;
end;
procedure TPV_Bitmap.SetG(X, Y: Integer; G: Byte);
begin
FData[Y* FWidth + X].G := G;
end;
procedure TPV_Bitmap.SetB(X, Y: Integer; B: Byte);
begin
FData[Y* FWidth + X].B := B;
end;
procedure TPV_Bitmap.SetA(X, Y: Integer; A: Byte);
begin
FData[Y* FWidth + X].A := A;
end;
procedure TPV_Bitmap.AddPal(R, G, B, A: Byte);
begin
FPalette[PaletteLen].RGBA := B + (G shl 8) + (R shl 16) + (A shl 24);
Inc(PaletteLen);
end;
procedure TPV_Bitmap.SetPal(X, Y: Integer; Index: Byte);
begin
FData[Y* FWidth + X].RGBA := FPalette[Index].RGBA;
end;
function TPV_Bitmap.GetPalIndex(X, Y: Integer): Integer;
var P,Pal: TPix;
i: Integer;
begin
P := Self.Pixel[x,y];
for i:=0 to PaletteLen-1 do begin
Pal := FPalette[i];
if (Pal.R = P.R) and (Pal.G = P.G) and (Pal.B = P.B) then Exit(i);
end;
Result := 0;
end;
procedure TPV_Bitmap.AddPalette(Pal: array of TPal; Len: Integer);
var i: Integer;
begin
for i:=0 to Len-1 do
AddPal(Pal[i].R, Pal[i].G, Pal[i].B, 255);
end;
procedure TPV_Bitmap.ClearPalette;
begin
PaletteLen := 0;
end;
constructor TPV_Bitmap.Create;
begin
inherited Create;
FWidth := 1;
FHeight := 1;
SetLength(FData, FWidth*FHeight);
SetLength(FPalette, 256);
PaletteLen := 0;
FormatName := '';
end;
procedure TPV_Bitmap.SetSize(AWidth, AHeight: Integer);
begin
FWidth := AWidth;
FHeight := AHeight;
SetLength(FData, FWidth*FHeight);
end;
function TPV_Bitmap.LoadFromFile(Filename: String): Boolean;
var Pic: TPicture;
Reader: TPV_BitmapReader;
F: TFileStream;
Ext: String;
Res: Boolean;
AFormat: String;
begin
Result := False;
Ext := Copy(ExtractFileExt(Filename), 2);
F := TFileStream.Create(Filename, fmOpenRead or fmShareDenyNone);
if F.Size < 50 then begin
F.Free;
Exit;
end;
Reader := BitmapFormats.FindReader(Ext, AFormat);
if Reader <> nil then begin
ClearPalette;
Res := Reader(Self, F);
if Res then begin
Result := True;
Self.FormatName := AFormat;
end;
end;
F.Free;
end;
procedure TPV_Bitmap.SaveToFile(Filename: String);
begin
SaveToFile(Filename, 0);
end;
procedure TPV_Bitmap.SaveToFile(Filename: String; Compression: Byte);
var Ext: String;
Writer: TPV_BitmapWriter;
F: TFileStream;
begin
Ext := Copy(ExtractFileExt(Filename), 2);
F := TFileStream.Create(Filename, fmCreate);
Writer := BitmapFormats.FindWriter(Ext);
if Writer <> nil then Writer(Self, F, Compression)
else raise Exception.Create('Unsupported format: ' + Ext);
F.Free;
end;
procedure TPV_BitmapFormat.Item(Index: Integer; out Ext,Name: String; out
Reader: TPV_BitmapReader; out Writer: TPV_BitmapWriter);
begin
Ext := FList[Index].Ext;
Name := FList[Index].Name;
Reader := FList[Index].Reader;
Writer := FList[Index].Writer;
end;
constructor TPV_BitmapFormat.Create;
begin
FCount := 0;
SetLength(FList, 200);
end;
function TPV_BitmapFormat.FindReader(Ext: String; out Format: String): TPV_BitmapReader;
var i: Integer;
begin
Result := nil;
Ext := LowerCase(Ext);
for i:=0 to FCount-1 do
if FList[i].Ext = Ext then begin
Format := FList[i].Name;
Result := FList[i].Reader;
Exit;
end;
end;
function TPV_BitmapFormat.FindWriter(Ext: String): TPV_BitmapWriter;
var i: Integer;
begin
Result := nil;
Ext := LowerCase(Ext);
for i:=0 to FCount-1 do
if FList[i].Ext = Ext then Exit(FList[i].Writer);
end;