Skip to content

Commit

Permalink
TImage.SetExternalData
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Oct 20, 2024
1 parent 6e309c3 commit 785813d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
33 changes: 32 additions & 1 deletion Source/script/imports/simba.import_image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,35 @@ procedure _LapeImage_SetSize(const Params: PParamArray); LAPE_WRAPPER_CALLING_CO
PSimbaImage(Params^[0])^.SetSize(PInteger(Params^[1])^, PInteger(Params^[2])^);
end;

(*
TImage.SetExternalData
----------------------
```
procedure TImage.SetExternalData(NewData: PColorBGRA; DataWidth, DataHeight: Integer);
```
Point the image data to external data (ie. not data allocated by the image itself).
*)
procedure _LapeImage_SetExternalData(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
begin
PSimbaImage(Params^[0])^.SetExternalData(PPointer(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^);
end;

(*
TImage.ResetExternalData
------------------------
```
procedure TImage.ResetExternalData;
```
Remove the effects of `SetExternalData`.
*)
procedure _LapeImage_ResetExternalData(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
begin
PSimbaImage(Params^[0])^.ResetExternalData(PInteger(Params^[1])^, PInteger(Params^[2])^);
end;


(*
TImage.Fill
-----------
Expand Down Expand Up @@ -1852,7 +1881,9 @@ procedure ImportSimbaImage(Compiler: TSimbaScript_Compiler);

addGlobalFunc('function TImage.InImage(X, Y: Integer): Boolean', @_LapeImage_InImage);

addGlobalFunc('procedure TImage.SetSize(AWidth, AHeight: Integer);', @_LapeImage_SetSize);
addGlobalFunc('procedure TImage.SetSize(NewWidth, NewHeight: Integer);', @_LapeImage_SetSize);
addGlobalFunc('procedure TImage.SetExternaData(Data: PColorBGRA; DataWidth, DataHeight: Integer);', @_LapeImage_SetExternalData);
addGlobalFunc('procedure TImage.ResetExternaData(NewWidth, NewHeight: Integer);', @_LapeImage_ResetExternalData);

addGlobalFunc('procedure TImage.Fill(Color: TColor);', @_LapeImage_Fill);
addGlobalFunc('procedure TImage.FillWithAlpha(Value: Byte);', @_LapeImage_FillWithAlpha);
Expand Down
34 changes: 33 additions & 1 deletion Source/simba.image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ TSimbaImage = class(TSimbaBaseClass)

FData: PColorBGRA;
FDataSize: SizeUInt;
FDataOwner: Boolean;

FLineStarts: TSimbaImageLineStarts;

Expand Down Expand Up @@ -95,6 +96,7 @@ TSimbaImage = class(TSimbaBaseClass)

property Data: PColorBGRA read FData;
property DataSize: SizeUInt read FDataSize;
property DataOwner: Boolean read FDataOwner;
property Width: Integer read FWidth;
property Height: Integer read FHeight;
property Center: TPoint read FCenter;
Expand All @@ -118,6 +120,8 @@ TSimbaImage = class(TSimbaBaseClass)
function InImage(const X, Y: Integer): Boolean;

procedure SetSize(NewWidth, NewHeight: Integer);
procedure SetExternalData(NewData: PColorBGRA; DataWidth, DataHeight: Integer);
procedure ResetExternalData(NewWidth, NewHeight: Integer);

procedure Fill(Color: TColor);
procedure FillWithAlpha(Value: Byte);
Expand Down Expand Up @@ -1803,6 +1807,9 @@ procedure TSimbaImage.SetSize(NewWidth, NewHeight: Integer);
NewData: PColorBGRA;
I, MinW, MinH: Integer;
begin
if not FDataOwner then
SimbaException('Cannot resize image with external data.');

if (NewWidth <> FWidth) or (NewHeight <> FHeight) then
begin
if (NewWidth * NewHeight <> 0) then
Expand Down Expand Up @@ -1835,6 +1842,29 @@ procedure TSimbaImage.SetSize(NewWidth, NewHeight: Integer);
end;
end;

procedure TSimbaImage.SetExternalData(NewData: PColorBGRA; DataWidth, DataHeight: Integer);
begin
SetSize(0, 0);

FDataOwner := False;
FData := NewData;
FWidth := DataWidth;
FHeight := DataHeight;
end;

procedure TSimbaImage.ResetExternalData(NewWidth, NewHeight: Integer);
begin
if FDataOwner then
Exit;

FDataOwner := True;
FData := nil;
FWidth := 0;
FHeight := 0;

SetSize(NewWidth, NewHeight);
end;

function TSimbaImage.Resize(Algo: EImageResizeAlgo; NewWidth, NewHeight: Integer): TSimbaImage;
begin
case Algo of
Expand Down Expand Up @@ -2076,6 +2106,7 @@ constructor TSimbaImage.Create;
begin
inherited Create();

FDataOwner := True;
FTextDrawer := TSimbaTextDrawer.Create(Self);

DefaultPixel.AsInteger := 0;
Expand Down Expand Up @@ -2159,7 +2190,8 @@ constructor TSimbaImage.CreateFromMatrix(Mat: TSingleMatrix; ColorMapType: Integ

destructor TSimbaImage.Destroy;
begin
SetSize(0, 0);
if FDataOwner then
SetSize(0, 0);

FreeAndNil(FTextDrawer);

Expand Down
26 changes: 26 additions & 0 deletions Tests/image_externaldata.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{$assertions on}

var
testData: PColorBGRA;
img: TImage;
begin
testData := GetMem(50*50*SizeOf(TColorBGRA));
// white, and alpha=255
FillMem(testData^, 50*50*SizeOf(TColorBGRA), 255);

img := TImage.Create(100,100);
img.SetExternaData(testData, 50, 50);

Assert(img.Pixel[10,10] = Colors.WHITE);
img.Pixel[10,10] := Colors.RED;
Assert(img.Pixel[10,10] = Colors.RED);
Assert(testData[10*50+10]^ = [0,0,255,255]);

// test reseting works
img.ResetExternaData(100,100);
img.SetSize(200,200);
Assert(img.Pixel[10,10] = Colors.BLACK);

img.Free();
FreeMem(testData);
end.

0 comments on commit 785813d

Please sign in to comment.