-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmaps.pas
1665 lines (1506 loc) · 47.5 KB
/
bitmaps.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
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
Bitmaps class for the Mufasa Macro Library
}
unit bitmaps;
{$mode objfpc}{$H+}
{$Inline on}
interface
uses
Classes, SysUtils, FPImage,IntfGraphics,graphtype,MufasaTypes,MufasaBase,graphics;
type
TMBitmaps = class;
{ TMufasaBitmap }
PMufasaBitmap = ^TMufasaBitmap;
TMufasaBitmap = class(TObject)
private
w,h : integer;
FTransparentColor : TRGB32;
FTransparentSet : boolean;
FIndex : integer;
FName : string;
FList: TMBitmaps;
{ True if we do not own FData }
FExternData: boolean;
public
OnDestroy : procedure(Bitmap : TMufasaBitmap) of object;
//FakeData : array of TRGB32;
FData : PRGB32;
property Name : string read FName write FName;
property Index : integer read FIndex write FIndex;
procedure SetSize(AWidth,AHeight : integer);
procedure StretchResize(AWidth,AHeight : integer);
property Width : Integer read w;
property Height : Integer read h;
procedure SetPersistentMemory(mem: PtrUInt; awidth, aheight: integer);
procedure ResetPersistentMemory;
function PointInBitmap(x,y : integer) : boolean;
procedure ValidatePoint(x,y : integer);
function SaveToFile(const FileName : string) :boolean;
procedure LoadFromFile(const FileName : string);
procedure Rectangle(const Box : TBox;FillCol : TColor);
procedure FloodFill(const StartPT : TPoint; const SearchCol, ReplaceCol : TColor);
procedure FastSetPixel(x,y : integer; Color : TColor);
procedure FastSetPixels(Points : TPointArray; Colors : TIntegerArray);
procedure DrawATPA(ATPA : T2DPointArray; Colors : TIntegerArray);overload;
procedure DrawATPA(ATPA : T2DPointArray);overload;
procedure DrawTPA(Points : TPointArray; Color : TColor);
procedure DrawToCanvas(x,y : integer; Canvas : TCanvas);
procedure LineTo(Src,Dst: TPoint;Color: TColor);
function FindColors(var points: TPointArray; const color: integer): boolean;
function FastGetPixel(x,y : integer) : TColor;
function FastGetPixels(Points : TPointArray) : TIntegerArray;
function GetAreaColors(xs,ys,xe,ye : integer) : T2DIntArray;
function GetHSLValues(xs, ys, xe, ye: integer): T2DHSLArray;
procedure FastDrawClear(Color : TColor);
procedure FastDrawTransparent(x, y: Integer; TargetBitmap: TMufasaBitmap);
procedure FastReplaceColor(OldColor, NewColor: TColor);
procedure CopyClientToBitmap(MWindow : TObject;Resize : boolean; xs, ys, xe, ye: Integer);overload;
procedure CopyClientToBitmap(MWindow : TObject;Resize : boolean;x,y : integer; xs, ys, xe, ye: Integer);overload;
procedure RotateBitmap(angle: Extended;TargetBitmap : TMufasaBitmap );
procedure Desaturate(TargetBitmap : TMufasaBitmap); overload;
procedure Desaturate;overload;
procedure GreyScale(TargetBitmap : TMufasaBitmap);overload;
procedure GreyScale;
procedure Brightness(TargetBitmap : TMufasaBitmap; br : integer); overload;
procedure Brightness(br: integer);overload;
procedure Contrast(TargetBitmap : TMufasaBitmap; co : Extended);overload;
procedure Contrast(co: Extended);overload;
procedure Invert(TargetBitmap : TMufasaBitmap);overload;
procedure Invert;overload;
procedure Posterize(TargetBitmap : TMufasaBitmap; Po : integer);overload;
procedure Posterize(Po : integer);overload;
procedure Convolute(TargetBitmap : TMufasaBitmap; Matrix : T2DExtendedArray);
function Copy(const xs,ys,xe,ye : integer) : TMufasaBitmap; overload;
function Copy: TMufasaBitmap;overload;
function ToTBitmap: TBitmap;
function ToString : string;
function RowPtrs : TPRGB32Array;
procedure LoadFromTBitmap(bmp: TBitmap);
procedure LoadFromRawImage(RawImage: TRawImage);
function CreateTMask : TMask;
procedure SetTransparentColor(Col : TColor);
function GetTransparentColor : TColor;
property TransparentColorSet : boolean read FTransparentSet;
property List: TMBitmaps read FList write FList;
procedure SetAlphaValue(const value : byte);
constructor Create;
destructor Destroy;override;
end;
TMufasaBmpArray = Array of TMufasaBitmap;
{ TMBitmaps }
TMBitmaps = class(TObject)
protected
Client: TObject;
FreeSpots: Array of integer;
BmpArray: TMufasaBmpArray;
BmpsCurr, BmpsHigh, FreeSpotsHigh, FreeSpotsLen: integer;
function GetNewIndex : integer;
public
function GetBMP(Index : integer) : TMufasaBitmap;
property Bmp[Index : integer]: TMufasaBitmap read GetBMP; default;
function CreateBMP(w, h: integer): Integer;
function ExistsBMP(Index : integer) : boolean;
function AddBMP(_bmp: TMufasaBitmap): Integer;
function CopyBMP( Bitmap : integer) : Integer;
function CreateMirroredBitmap(bitmap: Integer; MirrorStyle : TBmpMirrorStyle): Integer;
function CreateBMPFromFile(const Path : string) : integer;
function CreateBMPFromString(width,height : integer; Data : string) : integer;overload;
function CreateBMPFromString(BmpName: string; width, height: integer; Data: string) : integer;overload;
function RemoveBMP(Number: Integer): TMufasaBitmap;
constructor Create(Owner: TObject);
destructor Destroy;override;
end;
Procedure ArrDataToRawImage(Ptr: PRGB32; Size: TPoint; out RawImage: TRawImage);
function CalculatePixelShift(Bmp1,Bmp2 : TMufasaBitmap; CompareBox : TBox) : integer;
function CalculatePixelShiftTPA(Bmp1, Bmp2: TMufasaBitmap; CPoints: TPointArray): integer;
function CalculatePixelTolerance(Bmp1,Bmp2 : TMufasaBitmap; CompareBox : TBox; CTS : integer) : extended;
function CalculatePixelToleranceTPA(Bmp1, Bmp2: TMufasaBitmap; CPoints: TPointArray; CTS: integer): extended;
implementation
uses
paszlib,DCPbase64,math, client,tpa,
colour_conv,IOManager,mufasatypesutil,FileUtil;
// Needs more fixing. We need to either copy the memory ourself, or somehow
// find a TRawImage feature to skip X bytes after X bytes read. (Most likely a
// feature)
Procedure ArrDataToRawImage(Ptr: PRGB32; Size: TPoint; out RawImage: TRawImage);
Begin
RawImage.Init; { Calls raw.Description.Init as well }
RawImage.Description.PaletteColorCount:=0;
RawImage.Description.MaskBitsPerPixel:=0;
RawImage.Description.Width := Size.X;
RawImage.Description.Height:= Size.Y;
RawImage.Description.Format := ricfRGBA;
RawImage.Description.ByteOrder := riboLSBFirst;
RawImage.Description.BitOrder:= riboBitsInOrder; // should be fine
RawImage.Description.Depth:=24;
RawImage.Description.BitsPerPixel:=32;
RawImage.Description.LineOrder:=riloTopToBottom;
RawImage.Description.LineEnd := rileDWordBoundary;
RawImage.Description.RedPrec := 8;
RawImage.Description.GreenPrec:= 8;
RawImage.Description.BluePrec:= 8;
RawImage.Description.AlphaPrec:=0;
RawImage.Description.RedShift:=16;
RawImage.Description.GreenShift:=8;
RawImage.Description.BlueShift:=0;
RawImage.DataSize := RawImage.Description.Width * RawImage.Description.Height
* (RawImage.Description.bitsperpixel shr 3);
RawImage.Data := PByte(Ptr);
End;
function CalculatePixelShift(Bmp1, Bmp2: TMufasaBitmap; CompareBox: TBox): integer;
var
x,y : integer;
w1,w2 : integer;
begin
Bmp1.ValidatePoint(comparebox.x1,comparebox.y1);
Bmp1.ValidatePoint(comparebox.x2,comparebox.y2);
Bmp2.ValidatePoint(comparebox.x1,comparebox.y1);
Bmp2.ValidatePoint(comparebox.x2,comparebox.y2);
Bmp1.SetAlphaValue(0);
Bmp2.SetAlphaValue(0);
w1 := bmp1.Width;
w2 := bmp2.width;
result := 0;
for y := CompareBox.y1 to CompareBox.y2 do
for x := CompareBox.x1 to CompareBox.x2 do
if LongWord(Bmp1.FData[y * w1 + x]) <> LongWord(Bmp2.Fdata[y * w2 + x]) then
inc(result);
end;
function CalculatePixelShiftTPA(Bmp1, Bmp2: TMufasaBitmap; CPoints: TPointArray): integer;
var
i : integer;
bounds: TBox;
w1,w2 : integer;
begin
bounds := GetTPABounds(CPoints);
Bmp1.ValidatePoint(bounds.x1,bounds.y1);
Bmp1.ValidatePoint(bounds.x2,bounds.y2);
Bmp2.ValidatePoint(bounds.x1,bounds.y1);
Bmp2.ValidatePoint(bounds.x2,bounds.y2);
Bmp1.SetAlphaValue(0);
Bmp2.SetAlphaValue(0);
w1 := bmp1.width;
w2 := bmp2.width;
result := 0;
for i := 0 to High(CPoints) do
if LongWord(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x]) <>
LongWord(Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x]) then
inc(result);
end;
//CTS 0 counts the average difference in R,G,B per pixel
//CTS 1 counts the average difference using SQRT(Sqr(r) + sqr(g)+sqr(b));
function CalculatePixelTolerance(Bmp1, Bmp2: TMufasaBitmap; CompareBox: TBox;
CTS: integer): extended;
var
x,y : integer;
w1,w2 : integer;
Diff : int64;
begin
Bmp1.ValidatePoint(comparebox.x1,comparebox.y1);
Bmp1.ValidatePoint(comparebox.x2,comparebox.y2);
Bmp2.ValidatePoint(comparebox.x1,comparebox.y1);
Bmp2.ValidatePoint(comparebox.x1,comparebox.y1);
Bmp1.SetAlphaValue(0);
Bmp2.SetAlphaValue(0);
w1 := bmp1.Width;
w2 := bmp2.width;
result := 0;
if not InRange(CTS,0,1) then
raise exception.CreateFmt('CTS Passed to CalculateTolerance must be in [0..1], it currently is %d',[CTS]);
case CTS of
0 : begin
Diff := 0;
for y := CompareBox.y1 to CompareBox.y2 do
for x := CompareBox.x1 to CompareBox.x2 do
begin
Diff := Diff + abs(Bmp1.FData[y * w1 + x].r-Bmp2.Fdata[y * w2 + x].r) +
abs(Bmp1.FData[y * w1 + x].g-Bmp2.Fdata[y * w2 + x].g) +
abs(Bmp1.FData[y * w1 + x].b-Bmp2.Fdata[y * w2 + x].b);
end;
Result := Diff / (3 * (CompareBox.x2 - CompareBox.x1 + 1) * (CompareBox.y2-CompareBox.y1 + 1)); //We want the value for the whole Pixel; so divide by 3 (RGB)
end;
1 : begin
for y := CompareBox.y1 to CompareBox.y2 do
for x := CompareBox.x1 to CompareBox.x2 do
Result := Result + Sqrt(Sqr(Bmp1.FData[y * w1 + x].r-Bmp2.Fdata[y * w2 + x].r) +
Sqr(Bmp1.FData[y * w1 + x].g-Bmp2.Fdata[y * w2 + x].g) +
Sqr(Bmp1.FData[y * w1 + x].b-Bmp2.Fdata[y * w2 + x].b));
Result := Result / ((CompareBox.x2 - CompareBox.x1 + 1) * (CompareBox.y2-CompareBox.y1 + 1)); //We want the value for the whole Pixel;
end;
end;
end;
function CalculatePixelToleranceTPA(Bmp1, Bmp2: TMufasaBitmap; CPoints: TPointArray;
CTS: integer): extended;
var
i : integer;
bounds: TBox;
w1,w2 : integer;
Diff : int64;
begin
bounds := GetTPABounds(CPoints);
Bmp1.ValidatePoint(bounds.x1,bounds.y1);
Bmp1.ValidatePoint(bounds.x2,bounds.y2);
Bmp2.ValidatePoint(bounds.x1,bounds.y1);
Bmp2.ValidatePoint(bounds.x2,bounds.y2);
Bmp1.SetAlphaValue(0);
Bmp2.SetAlphaValue(0);
w1 := bmp1.Width;
w2 := bmp2.width;
result := 0;
if not InRange(CTS,0,1) then
raise exception.CreateFmt('CTS Passed to CalculateTolerance must be in [0..1], it currently is %d',[CTS]);
case CTS of
0 : begin
Diff := 0;
for i := 0 to High(CPoints) do
begin
Diff := Diff + abs(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x].r-Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x].r) +
abs(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x].g-Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x].g) +
abs(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x].b-Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x].b);
end;
Result := Diff / (3 * (bounds.x2 - bounds.x1 + 1) * (bounds.y2-bounds.y1 + 1)); //We want the value for the whole Pixel; so divide by 3 (RGB)
end;
1 : begin
for i := 0 to High(CPoints) do
Result := Result + Sqrt(Sqr(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x].r-Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x].r) +
Sqr(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x].g-Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x].g) +
Sqr(Bmp1.FData[CPoints[i].y * w1 + CPoints[i].x].b-Bmp2.Fdata[CPoints[i].y * w2 + CPoints[i].x].b));
Result := Result / ((bounds.x2 - bounds.x1 + 1) * (bounds.y2-bounds.y1 + 1)); //We want the value for the whole Pixel;
end;
end;
end;
function Min(a,b:integer) : integer;
begin
if a < b then
result := a
else
result := b;
end;
{ TMBitmaps }
function TMBitmaps.GetNewIndex: integer;
begin
if BmpsCurr < BmpsHigh then
begin;
inc(BmpsCurr);
Result := BmpsCurr;
end else if (FreeSpotsHigh > -1) then
begin;
Result := FreeSpots[FreeSpotsHigh];
dec(FreeSpotsHigh);
end else
begin;
SetLength(BmpArray, BmpsHigh + 6);
BmpsHigh := BmpsHigh + 5;
inc(BmpsCurr);
Result := BmpsCurr;
end;
end;
function TMBitmaps.GetBMP(Index: integer): TMufasaBitmap;
begin
Result := nil;
if (Index >= 0) and (Index <= BmpsCurr) then
if BmpArray[Index] <> nil then
Result := BmpArray[Index];
if Result = nil then
raise Exception.CreateFmt('The bitmap[%d] does not exist',[Index]);
end;
function TMBitmaps.CreateBMP(w,h : integer): Integer;
var
Bitmap: TMufasaBitmap;
begin
Bitmap := TMufasaBitmap.Create;
Bitmap.SetSize(w,h);
addBMP(Bitmap);
end;
function TMBitmaps.AddBMP(_bmp: TMufasaBitmap): Integer;
begin
Result := GetNewIndex;
BmpArray[Result] := _bmp;
BmpArray[Result].Index := Result;
BmpArray[Result].List := Self;
end;
function TMBitmaps.CopyBMP(Bitmap: integer): Integer;
var
InputBMP : TMufasaBitmap;
OutputBMP : TMUfasaBitmap;
begin
InputBMP := GetBMP(Bitmap);
Result := CreateBMP(InputBmp.w,InputBMP.h);
OutputBMP := GetBMP(Result);
Move(InputBMP.FData[0],OutPutBMP.FData[0],InputBMP.w * InputBMP.h * SizeOf(TRGB32));
end;
function TMBitmaps.CreateMirroredBitmap(bitmap: Integer;
MirrorStyle: TBmpMirrorStyle): Integer;
var
w,h : integer;
y,x : integer;
Source,Dest : PRGB32;
begin
Source := Bmp[Bitmap].FData;
w := BmpArray[Bitmap].Width;
h := BmpArray[Bitmap].Height;
if MirrorStyle = MirrorLine then
Result := CreateBMP(h,w)
else
Result := CreateBMP(w,h);
Dest := BmpArray[Result].FData;
case MirrorStyle of
MirrorWidth : for y := (h-1) downto 0 do
for x := (w-1) downto 0 do
Dest[y*w+x] := Source[y*w+w-1-x];
MirrorHeight : for y := (h-1) downto 0 do
Move(Source[y*w],Dest[(h-1 - y) * w],w*SizeOf(TRGB32));
MirrorLine : for y := (h-1) downto 0 do
for x := (w-1) downto 0 do
Dest[x*h+y] := Source[y*w+x];
end;
//Can be optmized, this is just proof of concept
end;
function TMBitmaps.CreateBMPFromFile(const Path: string): integer;
begin
Result := CreateBMP(0,0);
try
BmpArray[Result].LoadFromFile(Path);
except
BmpArray[Result].Free();
Result := -1; // meh
raise;
end;
end;
function HexToInt(HexNum: string): LongInt;inline;
begin
Result:=StrToInt('$' + HexNum);
end;
function TMBitmaps.ExistsBMP(Index : integer) : boolean;
begin
result := false;
if (Index >= 0) and (Index <= BmpsCurr) then
result := Assigned(BmpArray[Index]);
end;
function TMBitmaps.CreateBMPFromString(width, height: integer; Data: string): integer;
var
I,II: LongWord;
DestLen : LongWord;
Dest,Source : string;
DestPoint, Point : PByte;
MufRaw : PRGB24;
MufDest : PRGB32;
begin
Result := CreateBMP(width,height);
if (Data <> '') and (Length(Data) <> 6) then
begin;
Point := Pointer(BmpArray[Result].FData);
if (Data[1] = 'b') or (Data[1] = 'm') then
begin;
Source := Base64DecodeStr(Copy(Data,2,Length(Data) - 1));
Destlen := Width * Height * 3;
Setlength(Dest,DestLen);
if uncompress(PChar(Dest),Destlen,pchar(Source), Length(Source)) = Z_OK then
begin;
if data[1] = 'm' then //Our encrypted bitmap! Winnor.
begin
MufRaw:= @Dest[1];
MufDest:= PRGB32(Point);
for i := width * height - 1 downto 0 do
begin
MufDest[i].R:= MufRaw[i].R;
MufDest[i].G := MufRaw[i].G;
MufDest[i].B := MufRaw[i].B;
end;
end else
if Data[1] = 'b'then
begin
DestPoint := @Dest[1];
i := 0;
ii := 2;
Dec(DestLen);
if DestLen > 2 then
begin;
while (ii < DestLen) do
Begin;
Point[i]:= DestPoint[ii+2];
Point[i+1]:= DestPoint[ii+1];
Point[i+2]:= DestPoint[ii];
ii := ii + 3;
i := i + 4;
end;
Point[i] := DestPoint[1];
Point[i+1] := DestPoint[0];
Point[i+2] := DestPoint[ii];
end else if (Width = 1) and (Height =1 ) then
begin;
Point[0] := DestPoint[1];
Point[1] := DestPoint[0];
Point[2] := DestPoint[2];
end;
end;
end;
end else if Data[1] = 'z' then
begin;
Destlen := Width * Height * 3 *2;
Setlength(Dest,DestLen);
ii := (Length(Data) - 1) div 2;
SetLength(Source,ii);
for i := 1 to ii do
Source[i] := Chr(HexToInt(Data[i * 2] + Data[i * 2+1]));
if uncompress(PChar(Dest),Destlen,pchar(Source), ii) = Z_OK then
begin;
ii := 1;
i := 0;
while (II < DestLen) do
begin;
Point[i+2]:= HexToInt(Dest[ii] + Dest[ii + 1]);
Point[i+1]:= HexToInt(Dest[ii+2] + Dest[ii + 3]);
Point[i]:= HexToInt(Dest[ii+4] + Dest[ii + 5]);
ii := ii + 6;
i := i + 4;
end;
end;
end else if LongWord(Length(Data)) = LongWord((Width * Height * 3 * 2)) then
begin;
ii := 1;
i := 0;
Destlen := Width * Height * 3 * 2;
while (II < DestLen) do
begin;
Point[i+2]:= HexToInt(Data[ii] + Data[ii + 1]);
Point[i+1]:= HexToInt(Data[ii+2] + Data[ii + 3]);
Point[i]:= HexToInt(Data[ii+4] + Data[ii + 5]);
ii := ii + 6;
i := i + 4;
end;
end;
end else
begin;
if Length(data) = 6 then
BmpArray[Result].FastDrawClear(HexToInt(Data));
// else
// FastDrawClear(Result,clBlack);
end;
end;
function TMBitmaps.CreateBMPFromString(BmpName: string; width, height: integer;
Data: string): integer;
begin
Result := Self.CreateBMPFromString(width,height,data);
Bmp[Result].Name:= BmpName;
end;
function TMBitmaps.RemoveBMP(Number: Integer): TMufasaBitmap;
begin
Result := GetBMP(Number);
if (Number < BmpsCurr) then
begin
Inc(FreeSpotsHigh);
if (FreeSpotsHigh = FreeSpotsLen) then
begin
Inc(FreeSpotsLen);
SetLength(FreeSpots, FreeSpotsLen);
end;
FreeSpots[FreeSpotsHigh] := Number;
end else
Dec(BmpsCurr);
BMPArray[Number] := nil;
Result.Index := -1;
Result.List := nil;
end;
{
Saves a bitmap to a file.
If Filename ends with 'png' it will save as a PNG file,
if Filename ends with 'jpg' or 'jpeg' it will save as JPG,
otherwise BMP is assumed.
}
function TMufasaBitmap.SaveToFile(const FileName: string): boolean;
var
rawImage : TRawImage;
Bmp : TLazIntfImage;
png: TPortableNetworkGraphic;
jpg: TJPEGImage;
begin
ArrDataToRawImage(FData,Point(w,h),RawImage);
result := true;
try
if RightStr(Filename, 3) = 'png' then
begin
png := TPortableNetworkGraphic.Create;
png.LoadFromRawImage(RawImage, False);
png.SaveToFile(UTF8ToSys(Filename));
png.Free;
end else if (RightStr(Filename, 3) = 'jpg') or (RightStr(Filename, 4) = 'jpeg') then
begin
jpg := TJPEGImage.Create;
jpg.LoadFromRawImage(RawImage, False);
jpg.SaveToFile(UTF8ToSys(Filename));
jpg.Free;
end else // Assume .bmp
begin
Bmp := TLazIntfImage.Create(RawImage,false);
Bmp.SaveToFile(UTF8ToSys(FileName));
Bmp.Free;
end;
except
result := false;
end;
end;
procedure TMufasaBitmap.LoadFromFile(const FileName: string);
var
LazIntf : TLazIntfImage;
RawImageDesc : TRawImageDescription;
begin
try
LazIntf := TLazIntfImage.Create(0,0);
RawImageDesc.Init_BPP32_B8G8R8_BIO_TTB(LazIntf.Width,LazIntf.Height);
LazIntf.DataDescription := RawImageDesc;
LazIntf.LoadFromFile(UTF8ToSys(FileName));
if Assigned(FData) then
Freemem(FData);
Self.W := LazIntf.Width;
Self.H := LazIntf.Height;
FData := GetMem(Self.W*Self.H*SizeOf(TRGB32));
Move(LazIntf.PixelData[0],FData[0],w*h*sizeOf(TRGB32));
finally
LazIntf.Free;
end;
end;
function RGBToBGR(Color : TColor) : TRGB32; inline;
begin;
Result.R := Color and $ff;
Result.G := Color shr 8 and $ff;
Result.B := Color shr 16 and $ff;
Result.A := 0;
end;
procedure TMufasaBitmap.Rectangle(const Box: TBox;FillCol: TColor);
var
y : integer;
Col : Longword;
Size : longword;
begin
if (Box.x1 < 0) or (Box.y1 < 0) or (Box.x2 >= self.w) or (Box.y2 >= self.h) then
raise exception.Create('The Box you passed to Rectangle exceed the bitmap''s bounds');
if (box.x1 > box.x2) or (Box.y1 > box.y2) then
raise exception.CreateFmt('The Box you passed to Rectangle doesn''t have normal bounds: (%d,%d) : (%d,%d)',
[Box.x1,box.y1,box.x2,box.y2]);
col := Longword(RGBToBGR(FillCol));
Size := Box.x2 - box.x1 + 1;
for y := Box.y1 to Box.y2 do
FillDWord(FData[y * self.w + Box.x1],size,Col);
end;
procedure TMufasaBitmap.FloodFill(const StartPT: TPoint; const SearchCol,
ReplaceCol: TColor);
var
Stack : TPointArray;
SIndex : Integer;
CurrX,CurrY : integer;
Search,Replace : LongWord;
procedure AddToStack(x,y : integer);inline;
begin
if LongWord(FData[y * w + x]) = Search then
begin
LongWord(FData[y * w + x]) := Replace;
Stack[SIndex].x := x;
Stack[SIndex].y := y;
inc(SIndex);
end;
end;
begin
ValidatePoint(StartPT.x,StartPT.y);
Search := LongWord(RGBToBGR(SearchCol));
Replace := LongWord(RGBToBGR(ReplaceCol));
SetAlphaValue(0);
if LongWord(FData[StartPT.y * w + StartPT.x]) <> Search then //Only add items to the stack that are the searchcol.
Exit;
SetLength(Stack,w * h);
SIndex := 0;
AddToStack(StartPT.x,StartPT.y);
SIndex := 0;
while (SIndex >= 0) do
begin;
CurrX := Stack[SIndex].x;
Curry := Stack[SIndex].y;
if (CurrX > 0) and (CurrY > 0) then AddToStack(CurrX - 1, CurrY - 1);
if (CurrX > 0) then AddToStack(CurrX - 1, CurrY);
if (CurrX > 0) and (CurrY + 1 < h) then AddToStack(CurrX - 1, CurrY + 1);
if (CurrY + 1 < h) then AddToStack(CurrX , CurrY + 1);
if (CurrX + 1 < w) and (CurrY + 1 < h) then AddToStack(CurrX + 1, CurrY + 1);
if (CurrX + 1 < w) then AddToStack(CurrX + 1, CurrY );
if (CurrX + 1 < w) and (CurrY > 0) then AddToStack(CurrX + 1, CurrY - 1);
if (CurrY > 0) then AddToStack(CurrX , CurrY - 1);
Dec(SIndex);
end;
end;
function TMufasaBitmap.Copy: TMufasaBitmap;
begin
Result := TMufasaBitmap.Create;
Result.SetSize(self.Width, self.Height);
Move(self.FData[0], Result.FData[0],self.w * self.h * SizeOf(TRGB32));
end;
function TMufasaBitmap.Copy(const xs, ys, xe, ye: integer): TMufasaBitmap;
var
i : integer;
begin
ValidatePoint(xs,ys);
ValidatePoint(xe,ye);
Result := TMufasaBitmap.Create;
Result.SetSize(xe-xs+1, ye-ys+1);
for i := ys to ye do
Move(self.FData[i * self.w + xs], Result.FData[(i-ys) * result.w],result.Width * SizeOf(TRGB32));
end;
function TMufasaBitmap.ToTBitmap: TBitmap;
var
tr:TRawImage;
begin
Result := TBitmap.Create;
ArrDataToRawImage(Self.Fdata, point(self.width,self.height), tr);
Result.LoadFromRawImage(tr, false);
end;
function TMufasaBitmap.ToString: string;
var
i : integer;
DestLen : longword;
DataStr : string;
CorrectData : PRGB24;
begin
SetLength(DataStr,w*h*3);
CorrectData:= PRGB24(@DataStr[1]);
for i := w*h - 1 downto 0 do
begin
CorrectData[i].R := FData[i].R;
CorrectData[i].G := FData[i].G;
CorrectData[i].B := FData[i].B;
end;
DestLen := BufferLen;
if compress(BufferString,destlen,PChar(DataStr),w*h*3) = Z_OK then
begin;
SetLength(DataStr,DestLen);
move(bufferstring[0],dataStr[1],DestLen);
result := 'm' + Base64EncodeStr(datastr);
SetLength(datastr,0);
end;
end;
function TMufasaBitmap.RowPtrs: TPRGB32Array;
var
I : integer;
begin;
setlength(result,h);
for i := 0 to h - 1 do
result[i] := FData + w * i;
end;
procedure TMufasaBitmap.LoadFromRawImage(RawImage: TRawImage);
var
x,y: integer;
_24_old_p: PByte;
rs,gs,bs:byte;
data: PRGB32;
begin
// clear data
Self.SetSize(0,0);
if (RawImage.Description.BitsPerPixel <> 24) and (RawImage.Description.BitsPerPixel <> 32) then
raise Exception.CreateFMT('TMufasaBitmap.LoadFromRawImage - BitsPerPixel is %d', [RawImage.Description.BitsPerPixel]);
{writeln('Bits per pixel: ' + Inttostr(RawImage.Description.BitsPerPixel)); }
if RawImage.Description.LineOrder <> riloTopToBottom then
raise Exception.Create('TMufasaBitmap.LoadFromRawImage - LineOrder is not riloTopToBottom');
{ writeln(format('LineOrder: theirs: %d, ours: %d', [RawImage.Description.LineOrder, riloTopToBottom])); }
// Todo, add support for other alignments.
{ if RawImage.Description.LineEnd <> rileDWordBoundary then
raise Exception.Create('TMufasaBitmap.LoadFromRawImage - LineEnd is not rileDWordBoundary'); }
//writeln(format('LineEnd: t', [RawImage.Description.LineEnd]));
if RawImage.Description.Format<>ricfRGBA then
raise Exception.Create('TMufasaBitmap.LoadFromRawImage - Format is not ricfRGBA');
// Set w,h and alloc mem.
Self.SetSize(RawImage.Description.Width, RawImage.Description.Height);
{writeln(format('Image size: %d, %d', [w,h])); }
rs := RawImage.Description.RedShift shr 3;
gs := RawImage.Description.GreenShift shr 3;
bs := RawImage.Description.BlueShift shr 3;
{ writeln(format('Shifts(R,G,B): %d, %d, %d', [rs,gs,bs]));
writeln(format('Bits per line %d, expected: %d',
[RawImage.Description.BitsPerLine, RawImage.Description.BitsPerPixel * self.w]));
}
if RawImage.Description.BitsPerPixel = 32 then
Move(RawImage.Data[0], Self.FData[0], self.w * self.h * SizeOf(TRGB32))
else
begin
//FillChar(Self.FData[0], self.w * self.h * SizeOf(TRGB32), 0);
data := self.FData;
_24_old_p := RawImage.Data;
for y := 0 to self.h -1 do
begin
for x := 0 to self.w -1 do
begin
// b is the first byte in the record.
data^.b := _24_old_p[bs];
data^.g := _24_old_p[gs];
data^.r := _24_old_p[rs];
data^.a := 0;
inc(_24_old_p, 3);
inc(data);
end;
case RawImage.Description.LineEnd of
rileTight, rileByteBoundary: ; // do nothing
rileWordBoundary:
while (_24_old_p - RawImage.Data) mod 2 <> 0 do
inc(_24_old_p);
rileDWordBoundary:
while (_24_old_p - RawImage.Data) mod 4 <> 0 do
inc(_24_old_p);
rileQWordBoundary:
while (_24_old_p - RawImage.Data) mod 4 <> 0 do
inc(_24_old_p);
rileDQWordBoundary:
while (_24_old_p - RawImage.Data) mod 8 <> 0 do
inc(_24_old_p);
end;
end;
end;
end;
procedure TMufasaBitmap.LoadFromTBitmap(bmp: TBitmap);
begin
// bmp.BeginUpdate();
LoadFromRawImage(bmp.RawImage);
// bmp.EndUpdate();
end;
procedure TMufasaBitmap.FastSetPixel(x, y: integer; Color: TColor);
begin
ValidatePoint(x,y);
FData[y*w+x] := RGBToBGR(Color);
end;
procedure TMufasaBitmap.FastSetPixels(Points: TPointArray; Colors: TIntegerArray);
var
i,len : integer;
Box : TBox;
begin
len := High(Points);
if Len <> High(colors) then
Raise Exception.CreateFMT('TPA/Colors Length differ',[]);
Box := GetTPABounds(Points);
if (Box.x1 < 0) or (Box.y1 < 0) or (Box.x2 >= self.w) or (Box.y2 >= self.h) then
raise exception.Create('The Points you passed to FastSetPixels exceed the bitmap''s bounds')
else
for i := 0 to len do
FData[Points[i].y * w + Points[i].x] := RGBToBGR(Colors[i]);
end;
procedure TMufasaBitmap.DrawATPA(ATPA: T2DPointArray; Colors: TIntegerArray);
var
lenTPA,lenATPA : integer;
i,ii : integer;
Color : TRGB32;
Box : TBox;
begin
lenATPA := High(ATPA);
if LenATPA <> High(colors) then
Raise Exception.CreateFMT('TPA/Colors Length differ -> %d : %d',[LenATPA + 1,High(Colors) + 1]);
for i := 0 to lenATPA do
begin;
lenTPA := High(ATPA[i]);
Color := RGBToBGR(Colors[i]);
Box := GetTPABounds(ATPA[i]);
if (Box.x1 < 0) or (Box.y1 < 0) or (Box.x2 >= self.w) or (Box.y2 >= self.h) then
raise exception.Create('The Points you passed to DrawATPA exceed the bitmap''s bounds')
else
for ii := 0 to lenTPA do
FData[ATPA[i][ii].y * w + ATPA[i][ii].x] := Color;
end;
end;
procedure TMufasaBitmap.DrawATPA(ATPA: T2DPointArray);
var
Colors : TIntegerArray;
i,len : integer;
begin
len := high(ATPA);
SetLength(colors,len+1);
for i := 0 to len do
Colors[i] := Random(clwhite);
DrawATPA(ATPA,Colors);
end;
procedure TMufasaBitmap.DrawTPA(Points: TPointArray; Color: TColor);
begin
DrawATPA(ConvArr([Points]),ConvArr([Color]));
end;
procedure TMufasaBitmap.DrawToCanvas(x,y : integer; Canvas: TCanvas);
var
Bitmap : Graphics.TBitmap;
begin
Bitmap := Self.ToTBitmap;
Canvas.Draw(x,y,Bitmap);
Bitmap.free;
end;
procedure TMufasaBitmap.LineTo(Src, Dst: TPoint;Color: TColor);
var
TPA: TPointArray;
begin
TPA:=TPAFromLine(src.x,src.y,dst.x,dst.y);
// if (not Assigned(TPA)) or (Length(TPA)< 2) then exit;
Self.DrawTPA(TPA,Color);
end;
function TMufasaBitmap.FindColors(var points: TPointArray; const color: integer): boolean;
var
x, y, i, bmpW, bmpH: integer;
begin
bmpW := width;
bmpH := height;
i := 0;
SetLength(points, bmpW * bmpH);
for x := 0 to (bmpW - 1) do
for y := 0 to (bmpH - 1) do
if (fastGetPixel(x, y) = color) then
begin
points[i].x := x;
points[i].y := y;
inc(i);
end;
setLength(points, i);
result := length(points) > 0;
end;
function TMufasaBitmap.FastGetPixel(x, y: integer): TColor;
begin
ValidatePoint(x,y);
Result := BGRToRGB(FData[y*w+x]);
end;
function TMufasaBitmap.FastGetPixels(Points: TPointArray): TIntegerArray;
var
i,len : integer;
Box : TBox;
begin
len := high(Points);
Box := GetTPABounds(Points);
if (Box.x1 < 0) or (Box.y1 < 0) or (Box.x2 >= self.w) or (Box.y2 >= self.h) then
raise exception.Create('The Points you passed to FastGetPixels exceed the bitmap''s bounds');
SetLength(result,len+1);
for i := 0 to len do
Result[i] := BGRToRGB(FData[Points[i].y*w + Points[i].x]);
end;
function TMufasaBitmap.GetAreaColors(xs, ys, xe, ye : integer): T2DIntArray;
var
x,y : integer;
begin
ValidatePoint(xs,ys);
ValidatePoint(xe,ye);
setlength(result,xe-xs+1,ye-ys+1);
for x := xs to xe do
for y := ys to ye do
result[x-xs][y-ys] := BGRToRGB(FData[y*w+x]);
end;
function TMufasaBitmap.GetHSLValues(xs, ys, xe, ye: integer): T2DHSLArray;
var
x, y: integer;
R, G, B, C: integer;
begin
ValidatePoint(xs,ys);
ValidatePoint(xe,ye);