Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Feb 5, 2024
1 parent 8b8d1ee commit 355d8eb
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 112 deletions.
1 change: 0 additions & 1 deletion Source/codetools/simba.ide_codetools_parser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ TCodeParser = class(TmwSimplePasPar)
constructor Create; override;
destructor Destroy; override;
end;
TPluginParser = class(TCodeParser);

TCodeParserArray = array of TCodeParser;
TCodeParserList = specialize TSimbaObjectList<TCodeParser>;
Expand Down
4 changes: 3 additions & 1 deletion Source/colormath/simba.colormath.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface
{$POP}

PChannelMultipliers = ^TChannelMultipliers;
TChannelMultipliers = array [0..2] of Single;
TChannelMultipliers = array[0..2] of Single;

const
DefaultMultipliers: TChannelMultipliers = (1, 1, 1);
Expand All @@ -38,6 +38,8 @@ interface
function AsString: String;
end;

// note: TColor stuff should not include alpha

TColor = Graphics.TColor;
PColor = ^TColor;

Expand Down
8 changes: 4 additions & 4 deletions Source/colormath/simba.colormath_conversion.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Project: Simba (https://github.com/MerlijnWajer/Simba)
License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0)
Color space converting.
Color space converting, includes lots of code from: https://github.com/slackydev/colorlib
Lots of code from: https://github.com/slackydev/colorlib
note: TColor stuff should not include alpha
}
unit simba.colormath_conversion;

Expand Down Expand Up @@ -63,12 +63,12 @@ class function TSimbaColorConversion.ColorToBGRA(const Color: TColor): TColorBGR
Result.B := Color shr B_BIT and $FF;
Result.G := Color shr G_BIT and $FF;
Result.R := Color shr R_BIT and $FF;
Result.A := Color shr A_BIT and $FF;
Result.A := 0;
end;

class function TSimbaColorConversion.BGRAToColor(const BGRA: TColorBGRA): TColor;
begin
Result := TColor(BGRA.R or BGRA.G shl G_BIT or BGRA.B shl B_BIT or BGRA.A shl A_BIT);
Result := TColor(BGRA.R or BGRA.G shl G_BIT or BGRA.B shl B_BIT);
end;

class function TSimbaColorConversion.ColorToRGB(const Color: TColor): TColorRGB;
Expand Down
151 changes: 72 additions & 79 deletions Source/image/simba.image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface
uses
Classes, SysUtils, Graphics,
simba.baseclass, simba.base, simba.image_textdrawer, simba.image_utils,
simba.colormath, simba.colormath_distance, simba.matchtemplate;
simba.colormath, simba.colormath_distance;

type
{$PUSH}
Expand Down Expand Up @@ -229,7 +229,6 @@ TSimbaImage = class(TSimbaBaseClass)
procedure LoadFromLazBitmap(LazBitmap: TBitmap);

function ToGreyMatrix: TByteMatrix;
function ToMatrixBGR: TIntegerMatrix;
function ToMatrix: TIntegerMatrix; overload;
function ToMatrix(X1, Y1, X2, Y2: Integer): TIntegerMatrix; overload;

Expand All @@ -252,9 +251,6 @@ TSimbaImage = class(TSimbaBaseClass)
function PixelDifference(Other: TSimbaImage; Tolerance: Single): Integer; overload;
function PixelDifferenceTPA(Other: TSimbaImage): TPointArray; overload;
function PixelDifferenceTPA(Other: TSimbaImage; Tolerance: Single): TPointArray; overload;

function MatchTemplate(Template: TSimbaImage; Formula: ETMFormula): TSingleMatrix;
function MatchTemplateMask(Template: TSimbaImage; Formula: ETMFormula): TSingleMatrix;
end;

TSimbaImageArray = array of TSimbaImage;
Expand Down Expand Up @@ -393,15 +389,6 @@ function TSimbaImage.ToGreyMatrix: TByteMatrix;
Result[Y, X] := Round(R * 0.3 + G * 0.59 + B * 0.11);
end;

function TSimbaImage.ToMatrixBGR: TIntegerMatrix;
var
Y: Integer;
begin
Result.SetSize(FWidth, FHeight);
for Y := 0 to FHeight - 1 do
Move(FData[Y * FWidth], Result[Y, 0], FWidth * SizeOf(Integer));
end;

function TSimbaImage.ToMatrix: TIntegerMatrix;
var
X, Y, W, H: Integer;
Expand All @@ -413,7 +400,8 @@ function TSimbaImage.ToMatrix: TIntegerMatrix;

for Y := 0 to H do
for X := 0 to W do
Result[Y][X] := FData[Y * FWidth + X].ToColor();
with FData[Y * FWidth + X] do
Result[Y, X] := TColor(R or G shl G_BIT or B shl B_BIT);
end;

function TSimbaImage.ToMatrix(X1, Y1, X2, Y2: Integer): TIntegerMatrix;
Expand All @@ -427,21 +415,26 @@ function TSimbaImage.ToMatrix(X1, Y1, X2, Y2: Integer): TIntegerMatrix;

for Y := Y1 to Y2 do
for X := X1 to X2 do
Result[Y-Y1, X-X1] := FData[Y * FWidth + X].ToColor();
with FData[Y * FWidth + X] do
Result[Y-Y1, X-X1] := TColor(R or G shl G_BIT or B shl B_BIT);
end;

procedure TSimbaImage.DrawMatrix(Matrix: TIntegerMatrix);
var
X, Y, W, H: Integer;
Color: Integer;
begin
SetSize(Matrix.Width, Matrix.Height);

W := FWidth - 1;
H := FHeight - 1;

for Y := 0 to H do
for X := 0 to W do
FData[Y * FWidth + X] := TColor(Matrix[Y, X]).ToBGRA();
begin
Color := Matrix[Y, X];

FData[Y * FWidth + X] := TColorBGRA((Color shr B_BIT and $FF) or (Color shr G_BIT and $FF) shl G_BIT or (Color shr R_BIT and $FF) shl B_BIT);
end;
end;

procedure TSimbaImage.DrawMatrix(Matrix: TSingleMatrix; ColorMapID: Integer = 0);
Expand Down Expand Up @@ -624,29 +617,28 @@ function TSimbaImage.Compare(Other: TSimbaImage): Single;
function TSimbaImage.PixelDifference(Other: TSimbaImage): Integer;
var
I: Integer;
Ptr, OtherPtr: PColorBGRA;
P1, P2: PColorBGRA;
begin
Result := 0;
if (FWidth <> Other.Width) or (FHeight <> Other.Height) then
SimbaException('TSimbaImage.PixelDifference: Both images must be equal dimensions');

Ptr := Data;
OtherPtr := Other.Data;

P1 := Data;
P2 := Other.Data;
for I := 0 to FWidth * FHeight - 1 do
begin
if not Ptr^.EqualsIgnoreAlpha(OtherPtr^) then
if not P1^.EqualsIgnoreAlpha(P2^) then
Inc(Result);

Inc(Ptr);
Inc(OtherPtr);
Inc(P1);
Inc(P2);
end;
end;

function TSimbaImage.PixelDifference(Other: TSimbaImage; Tolerance: Single): Integer;
var
I: Integer;
Ptr, OtherPtr: PColorBGRA;
P1, P2: PColorBGRA;
begin
Result := 0;
if (FWidth <> Other.Width) or (FHeight <> Other.Height) then
Expand All @@ -656,38 +648,40 @@ function TSimbaImage.PixelDifference(Other: TSimbaImage; Tolerance: Single): Int

Tolerance := Sqr(Tolerance);

Ptr := Data;
OtherPtr := Other.Data;

P1 := Data;
P2 := Other.Data;
for I := 0 to FWidth * FHeight - 1 do
begin
if (not SimilarColors(Ptr^.ToColor(), OtherPtr^.ToColor(), Tolerance)) then
if (not SimilarColors(TColor(P1^.R or P1^.G shl G_BIT or P1^.B shl B_BIT), TColor(P2^.R or P2^.G shl G_BIT or P2^.B shl B_BIT), Tolerance)) then
Inc(Result);

Inc(Ptr);
Inc(OtherPtr);
Inc(P1);
Inc(P2);
end;
end;

function TSimbaImage.PixelDifferenceTPA(Other: TSimbaImage): TPointArray;
var
X, Y, W, H: Integer;
Index: Integer;
P1, P2: PColorBGRA;
Buffer: TSimbaPointBuffer;
begin
if (FWidth <> Other.Width) or (FHeight <> Other.Height) then
SimbaException('TSimbaImage.PixelDifference: Both images must be equal dimensions');
Buffer.Init();
SimbaException('TSimbaImage.PixelDifferenceTPA: Both images must be equal dimensions');

W := FWidth - 1;
H := FHeight - 1;

P1 := Data;
P2 := Other.Data;
for Y := 0 to H do
for X := 0 to W do
begin
Index := Y * FWidth + X;
if not FData[Index].EqualsIgnoreAlpha(Other.FData[Index]) then
Buffer.Add(TPoint.Create(X, Y));
if (not P1^.EqualsIgnoreAlpha(P2^)) then
Buffer.Add(X, Y);

Inc(P1);
Inc(P2);
end;

Result := Buffer.ToArray(False);
Expand All @@ -696,27 +690,28 @@ function TSimbaImage.PixelDifferenceTPA(Other: TSimbaImage): TPointArray;
function TSimbaImage.PixelDifferenceTPA(Other: TSimbaImage; Tolerance: Single): TPointArray;
var
X, Y, W, H: Integer;
Index: Integer;
P1, P2: PColorBGRA;
Buffer: TSimbaPointBuffer;
begin
if (FWidth <> Other.Width) or (FHeight <> Other.Height) then
SimbaException('TSimbaImage.PixelDifference: Both images must be equal dimensions');
SimbaException('TSimbaImage.PixelDifferenceTPA: Both images must be equal dimensions');
if (Tolerance = 0) then
Exit(PixelDifferenceTPA(Other));

Buffer.Init();

Tolerance := Sqr(Tolerance);

W := FWidth - 1;
H := FHeight - 1;

P1 := Data;
P2 := Other.Data;
for Y := 0 to H do
for X := 0 to W do
begin
Index := Y * FWidth + X;
if (not (SimilarColors(FData[Index].ToColor(), Other.FData[Index].ToColor(), Tolerance))) then
Buffer.Add(TPoint.Create(X, Y));
if (not SimilarColors(TColor(P1^.R or P1^.G shl G_BIT or P1^.B shl B_BIT), TColor(P2^.R or P2^.G shl G_BIT or P2^.B shl B_BIT), Tolerance)) then
Buffer.Add(X, Y);

Inc(P1);
Inc(P2);
end;

Result := Buffer.ToArray(False);
Expand Down Expand Up @@ -1148,7 +1143,8 @@ function TSimbaImage.GetColors: TColorArray;
begin
SetLength(Result, FHeight * FWidth);
for I := 0 to High(Result) do
Result[I] := FData[I].ToColor();
with FData[I] do
Result[I] := TColor(R or G shl G_BIT or B shl B_BIT);
end;

function TSimbaImage.Equals(Other: TObject): Boolean;
Expand Down Expand Up @@ -1603,7 +1599,7 @@ function TSimbaImage.BoxBlur(Radius: Integer): TSimbaImage;
end;
Ptr: PColorBGRA;
begin
Result := TSimbaImage.Create(FWidth, FHeight);
Result := Copy();
if (Radius <= 1) or (not Odd(Radius)) then
SimbaException('TSimbaImage.BlockBlur: Radius(%d) must be odd (1,3,5 etc).', [Radius]);

Expand All @@ -1615,29 +1611,35 @@ function TSimbaImage.BoxBlur(Radius: Integer): TSimbaImage;
for Y := 0 to H do
for X := 0 to W do
begin
B.X1 := Max(X-Radius, 0);
B.Y1 := Max(Y-Radius, 0);
B.X2 := Min(X+Radius, W);
B.Y2 := Min(Y+Radius, H);
if (FData[Y * FWidth + X].A > 0) then
begin
B.X1 := Max(X-Radius, 0);
B.Y1 := Max(Y-Radius, 0);
B.X2 := Min(X+Radius, W);
B.Y2 := Min(Y+Radius, H);

Size := 0;
Sum.R := 0; Sum.G := 0; Sum.B := 0;
Size := 0;
Sum.R := 0; Sum.G := 0; Sum.B := 0;

for YY := B.Y1 to B.Y2 - 1 do
for XX := B.X1 to B.X2 - 1 do
begin
Ptr := @FData[YY * FWidth + XX];
for YY := B.Y1 to B.Y2 - 1 do
for XX := B.X1 to B.X2 - 1 do
begin
Ptr := @FData[YY * FWidth + XX];
if (Ptr^.A > 0) then
begin
Sum.R += Ptr^.R;
Sum.G += Ptr^.G;
Sum.B += Ptr^.B;

Sum.R += Ptr^.R;
Sum.G += Ptr^.G;
Sum.B += Ptr^.B;
Size += 1;
end;
Size += 1;
end;
end;

Ptr := @Result.Data[Y * FWidth + X];
Ptr^.R := Sum.R div Size;
Ptr^.G := Sum.G div Size;
Ptr^.B := Sum.B div Size;
Ptr := @Result.Data[Y * FWidth + X];
Ptr^.R := Sum.R div Size;
Ptr^.G := Sum.G div Size;
Ptr^.B := Sum.B div Size;
end;
end;
end;

Expand Down Expand Up @@ -2181,12 +2183,13 @@ function TSimbaImage.GetPixels(Points: TPointArray): TColorArray;
I: Integer;
begin
SetLength(Result, Length(Points));

for I := 0 to High(Points) do
with Points[I] do
begin
AssertInImage('GetPixels', X, Y);

Result[I] := FData[Y * FWidth + X].ToColor();
with FData[Y * FWidth + X] do
Result[I] := TColor(R or G shl G_BIT or B shl B_BIT);
end;
end;

Expand Down Expand Up @@ -2802,16 +2805,6 @@ procedure TSimbaImage.AssertInImage(const Method: String; const X, Y: Integer);
SimbaException('TSimbaImage.%s: %d,%d is outside the image bounds: %d,%d,%d,%d', [Method, X, Y, 0,0,FWidth-1,FHeight-1]);
end;

function TSimbaImage.MatchTemplate(Template: TSimbaImage; Formula: ETMFormula): TSingleMatrix;
begin
Result := simba.matchtemplate.MatchTemplate(Self.ToMatrixBGR(), Template.ToMatrixBGR(), Formula);
end;

function TSimbaImage.MatchTemplateMask(Template: TSimbaImage; Formula: ETMFormula): TSingleMatrix;
begin
Result := simba.matchtemplate.MatchTemplateMask(Self.ToMatrixBGR(), Template.ToMatrixBGR(), Formula);
end;

procedure TSimbaImage.DrawLineAA(Start, Stop: TPoint; Color: TColor; Thickness: Single);
var
RGB: TColorBGRA;
Expand Down
Loading

0 comments on commit 355d8eb

Please sign in to comment.