Bitmap

A bitmap in Simba is simply a two dimensional field of colors. These colors can all be the same, or they can be of different colors. Simba features functions to create, manipulate and search for bitmaps.

The bitmaps are - just as files - represented as integer in Simba (they point to a list of bitmaps, and the value of the integer is the position in the list). So typically, when referring to bitmaps in Simba, you simply represent them as an integer:

var bmp, x, y: integer;
begin
    bmp := CreateBitmap(10, 10); // Create a bitmap of size (10, 10)
    if FindBitmapIn(bmp, x, y, 0, 0, 300, 300) then
      writeln('Found it!');
    FreeBitmap(bmp); // Don't forget to free it when we are done.
end;

Note that the previous example doesn’t make a lot of sense as the bitmap has only been created and not filled with any colors, they are as of yet, undefined. You can also create bitmaps from screenshots and load them when your script starts using the BitmapFromString function, or simple store them as files and load them using the LoadBitmap function.

Word of caution on bitmap creation

Bitmaps in Simba are internally all instances of TMufasaBitmap. Scripts should generally access bitmaps using their handle: an integer. All functions referenced here either require a bitmap handle or return one.

If you want to gain more access over a specific bitmap, see the GetMufasaBitmap function. It is highly unrecommended to create bitmaps like this, because Simba will not free them automatically for you. (There’s no problem doing it like this if you only want to perform operations on it and then free it again)

var bmp: TMufasaBitmap;
begin
    bmp := TMufasBitmap.Create;
end;

Because there is no way to get a handle to this bitmap; as it will not be managed by Simba internally. (All Bitmaps created by CreateBitmap are managed by Simba, if you don’t know what this means: you generally want Simba to manage the bitmaps)

If you still want access to the TMufasaBitmap, use GetMufasaBitmap, described below.

Bitmap Functions

GetMufasaBitmap

function GetMufasaBitmap(bmp : integer) : TMufasaBitmap;

Returns the TMufasaBitmap for the given bitmap. They both reference the same bitmap. TMufasaBitmap is a more advanced interface to bitmaps in Simba. There is no way to get a internal (integer) reference to a bitmap if you create it with TMufasaBitmap.Create; so the recommended way is to use CreateBitmap to get the integer reference/handle and then call this function to get the class reference.

var bmp: TMufasaBitmap;
    bmph: integer;
begin;
    bmph := CreateBitmap(100, 100);
    bmp := GetMufasaBitmap(bmph);

    bmp.SetSize(150,150); // also changes bmph, as they are the same bitmap.
end;

CreateBitmapString

function CreateBitmapString(bmp : integer) : string;

Creates a string for the given bitmap, you can use this to save a bitmap for later use, for example loading it again using BitmapFromString.

CreateBitmap

function CreateBitmap(w,h :integer) : integer;

Create a bitmap with width h and height h. Returns the reference to the created bitmap.

FreeBitmap

procedure FreeBitmap(Bmp : integer);

Free the bitmap. You should do this when you no longer need the bitmap. Be careful when working with bitmaps: not freeing it when you no longer need it leads to memory leaks, which will eventually make your script crash. (Unless you stop it in time, in which case Simba will free the bitmaps for you)

SaveBitmap

procedure SaveBitmap(Bmp : integer; path : string);

Save the given bitmap to the specified path.

BitmapFromString

function BitmapFromString(Width,Height : integer; Data : string): integer;

Load a bitmap from the given string. This command is usually generated with the Bitmap to String feature in Simba.

LoadBitmap

function LoadBitmap(Path : string) : integer;

Load a bitmap from a path to a file. Formats known to work are BMP and PNG images.

SetBitmapSize

procedure SetBitmapSize(Bmp,NewW,NewH : integer);

Change the size of the bitmap. Previous data will be preserved (if possible), so enlarging the bitmap won’t destroy the old data, but shrinking it will inevitably destroy some data. (Everything that falls out of the new bounds)

GetBitmapSize

procedure GetBitmapSize(Bmp : integer; var BmpW,BmpH : integer);

Returns the size of the bitmap in BmpW, BmpH.

SetPersistentMemoryBitmap

procedure SetPersistentMemoryBitmap(bmp: integer; mem: PtrUInt; awidth, aheight: integer);

Set the internal bitmap bmp data pointer to mem. Size is described by awidth and aheight. mem should be a pointer of four bytes.

Byte order: BGRA (Blue, Green, Red, Alpha).

Previous data in the bitmap will be lost.

ResetPersistentMemoryBitmap

procedure ResetPersistentMemoryBitmap(bmp: Integer);

Undo the effect of SetPersistentMemoryBitmap. Data in the bitmap before the SetPersistentMemoryBitmap will not be restored.

StretchBitmapResize

procedure StretchBitmapResize(Bmp,NewW,NewH : integer);

CreateMirroredBitmap

function CreateMirroredBitmap(Bmp : integer) : integer;

CreateMirroredBitmapEx

function CreateMirroredBitmapEx(Bmp : integer; MirrorStyle : TBmpMirrorStyle) : integer;

FastSetPixel

procedure FastSetPixel(bmp,x,y : integer; Color : TColor);

Set the pixel on the bitmap at position x, y to color.

FastSetPixels

procedure FastSetPixels(bmp : integer; TPA : TPointArray; Colors : TIntegerArray);

Set the pixels on the bitmap at position TPA[index] to Colors[index].

FastGetPixel

function FastGetPixel(bmp, x,y : integer) : TColor;

Return the color of pixel on the bitmap, position specified by x, y.

FastGetPixels

function FastGetPixels(Bmp : integer; TPA : TPointArray) : TIntegerArray;

Return an array of the colors on the bitmap; positions specified by TPA.

GetBitmapAreaColors

function GetBitmapAreaColors(bmp,xs, ys, xe, ye: Integer): T2DIntegerArray;

Returns all the colors in the area defined by (xs, xy, xe, ye) on the bitmap in a two dimensions integer array.

FastDrawClear

procedure FastDrawClear(bmp : integer; Color : TColor);

Draw Color on every pixel on the bitmap.

FastDrawTransparent

procedure FastDrawTransparent(x, y: Integer; SourceBitmap, TargetBitmap: Integer);

SetTransparentColor

procedure SetTransparentColor(bmp : integer; Color : TColor);

GetTransparentColor

function GetTransparentColor(bmp: integer) : TColor;

FastReplaceColor

procedure FastReplaceColor(Bmp : integer; OldColor,NewColor : TColor);

CopyClientToBitmap

procedure CopyClientToBitmap(bmp, xs, ys, xe, ye: Integer);

Copy client area xs, ys, xe, ye to specified bitmap.

BitmapFromClient

function BitmapFromClient(const xs, ys, xe, ye: Integer): Integer;

Create a bitmap from the client. Area specified by xs, ye, xe, ye.

SetBitmapName

procedure SetBitmapName(Bmp : integer; name : string);

Assign a name to the bitmap. Mainly for debugging purposes. (It will write the name of the bitmap if it hasn’t been freed.)

program new;

var bmp: integer;
begin
  bmp := CreateBitmap(10, 10);
  SetBitmapName(bmp, 'We will not free this bitmap');
end.
// Simba will print what bitmap has not been freed (along with his long
// name)

FindBitmap

function FindBitmap(bitmap: integer; var x, y: Integer): Boolean;

Searches for the Bitmap bmp on the entire client. Returns true if found. If found, x, y specifies the position where the bitmap was found.

FindBitmapIn

function FindBitmapIn(bitmap: integer; var x, y: Integer;  xs, ys, xe, ye: Integer): Boolean;

Searches for the Bitmap bmp on the client in the area defined by xs,ys,xe,ye. Returns true if found. If found, x, y specifies the position where the bitmap was found.

FindBitmapToleranceIn

function FindBitmapToleranceIn(bitmap: integer; var x, y: Integer; xs, ys, xe, ye: Integer; tolerance: Integer): Boolean;

Searches for the Bitmap bmp on the client in the area defined by xs,ys,xe,ye. Tolerance defines the tolerance per pixel when matching bitmaps. See Color tolerance for more information on tolerance. Returns true if found. If found, x, y specifies the position where the bitmap was found.

FindBitmapSpiral

function FindBitmapSpiral(bitmap: Integer; var x, y: Integer; xs, ys, xe, ye: Integer): Boolean;

Searches for the Bitmap bmp on the client in the area defined by xs,ys,xe,ye. Returns true if found. If found, x, y specifies the position where the bitmap was found. Search starts from a point defined by x, y.

FindBitmapsSpiralTolerance

function FindBitmapsSpiralTolerance(bitmap: integer; x, y: Integer; var Points : TPointArray; xs, ys, xe, ye,tolerance: Integer): Boolean;

Searches for the Bitmap bmp on the client in the area defined by xs,ys,xe,ye. Tolerance defines the tolerance per pixel when matching bitmaps. See Color tolerance for more information on tolerance. Search starts from a point defined by x, y. Returns true if found. If found, each point in TPA specifies a match.

FindBitmapSpiralTolerance

function FindBitmapSpiralTolerance(bitmap: integer; var x, y: Integer; xs, ys, xe, ye,tolerance : integer): Boolean;

Searches for the Bitmap bmp on the client in the area defined by xs,ys,xe,ye. Tolerance defines the tolerance per pixel when matching bitmaps. See Color tolerance for more information on tolerance. Search starts from a point defined by x, y. Returns true if found. If found, x, y specifies the position where the bitmap was found.

RotateBitmap

function RotateBitmap(bitmap: Integer; angle: Extended): Integer;

DesaturateBitmap

function DesaturateBitmap(Bitmap : integer) : integer;

InvertBitmap

procedure InvertBitmap(Bitmap : integer);

CopyBitmap

function CopyBitmap(Bitmap:  integer) : integer)

Creates a copy of the Bitmap. Returns the bitmap copy.

GreyScaleBitmap

function GreyScaleBitmap(bitmap : integer) : integer

Creates a copy of the bitmap, greyscaled.

BrightnessBitmap

function BrightnessBitmap(Bitmap,br : integer) : integer;

Changes the brightness of a bitmap, intensity defined by br. Returns a new bitmap with the brightness applied.

If you instead want to apply brightness to the current bitmap, see Applying a filter on the current bitmap

ContrastBitmap

function ContrastBitmap(bitmap : integer; co : extended) : integer;

Changes the constrast of a bitmap, returns a new bitmap with the contrast applied.

PosterizeBitmap

function PosterizeBitmap(Bitmap : integer; po : integer) : integer;

Posterizes a bitmap, intensity defined by po; returns a new bitmap with the posterisation applied.

Applying a filter on the current bitmap

var b: integer;
begin
    // Dummy bitmap. You'll want something that's not just a blank bitmap.
    B:=CreateBitmap(100,100);

    // Apply the filter (Posterize in this case) without making a copy.
    GetMufasaBitmap(b).Posterize(GetMufasaBitmap(b), 10);

    // Always free your bitmaps when you no longer use them. :)
    FreeBitmap(b);
end.

CreateMaskFromBitmap

function CreateMaskFromBitmap(Bitmap : integer) : TMask;

FindMaskTolerance

function FindMaskTolerance(const mask: TMask; var x, y: Integer; xs,ys, xe, ye: Integer; Tolerance, ContourTolerance: Integer): Boolean;

FindBitmapMaskTolerance

function FindBitmapMaskTolerance(mask: Integer; var x, y: Integer; xs, ys, xe, ye: Integer; Tolerance, ContourTolerance: Integer): Boolean;

FindDeformedBitmapToleranceIn

function FindDeformedBitmapToleranceIn(bitmap: integer; var x,y: Integer; xs, ys, xe, ye: Integer; tolerance: Integer; Range: Integer; AllowPartialAccuracy: Boolean; var accuracy: Extended): Boolean;

DrawTPABitmap

procedure DrawTPABitmap(bitmap: integer; TPA: TPointArray; Color: integer);

Draws a TPointArray on a bitmap. Each point in the TPointArray is painted on the bitmap by setting the pixel on the bitmap (position defined by tpa point) to color.

DrawATPABitmap

procedure DrawATPABitmap(bitmap: integer; ATPA: T2DPointArray);

Draws a Array of TPointArray on a bitmap. Each point in the TPointArray is painted on the bitmap by setting the pixel on the bitmap (position defined by tpa point) to a color. Colors differ per TPointArray (group).

DrawATPABitmapEx

procedure DrawATPABitmapEx(bitmap: integer; ATPA: T2DPointArray; Colors: TIntegerArray);

Draws a Array of TPointArray on a bitmap. Each point in the TPointArray is painted on the bitmap by setting the pixel on the bitmap (position defined by tpa point) to a color. Colors are defined by Colors.

DrawBitmap

procedure DrawBitmap(Bmp: Integer; Dest: TCanvas; x, y: Integer);

Draw the bitmap to a TCanvas.

RectangleBitmap

procedure RectangleBitmap(bitmap : integer; const box : TBox; Color : TColor);

FloodFillBitmap

procedure FloodFillBitmap(bitmap : integer; const StartPoint : TPoint; const SearchCol,ReplaceCol : TColor);

CalculatePixelShift

function CalculatePixelShift(Bmp1,Bmp2 : Integer; CompareBox : TBox) : integer;

CalculatePixelTolerance

function CalculatePixelTolerance(Bmp1,Bmp2 : Integer; CompareBox : TBox; CTS : integer) : extended;');

CropBitmap

procedure CropBitmap(const bmp: integer; const xs, ys, xe, ye: integer);

Crops the bitmap, removes all points outside (xs, ys, xe, ye).

var
  bmp: integer;
begin;
  bmp := BitmapFromClient(0, 0, 50, 50);
  CropBitmap(bmp, 10, 10, 40, 40);
end;

GetColorsBitmap

function GetColorsBitmap(const bmp: integer): TIntegerArray;

Returns a TIntegerArray of all colors on the bitmap, the result will be sorted row by row.

var
  bmp, i: integer;
  arr: TIntegerArray;
begin;
  bmp := BitmapFromClient(0, 0, 10, 10);
  arr := GetColorsBitmap(bmp);

  for i := 0 to high(arr) do
    writeln(intToStr(arr[i]));
end;

BitmapToMatrix

function BitmapToMatrix(const bmp: integer): T2DIntegerArray;

Returns a two dimensions integer array of all the colors on the bitmap.

var
 bmp, x, y: integer;
 matrix: T2DIntegerArray;
begin
  bmp := BitmapFromClient(0, 0, 10, 10);
  matrix := BitmapToMatrix(bmp);

  for y := 0 to 10 do
    for x := 0 to 10 do
      writeln(matrix[y][x]);
end;

DrawMatrixBitmap

procedure DrawMatrixBitmap(const bmp: integer; const matrix: T2DIntegerArray);

Draws a matrix onto the bitmap.

DrawMatrixBitmap(bmp, matrix);

ThresholdAdaptiveBitmap

procedure ThresholdAdaptiveBitmap(const bmp: integer; Alpha, Beta: Byte; Invert: Boolean; Method: TBmpThreshMethod; C: Integer);

Applys a ThresholdAdaptive filter onto the bitmap. Vaild TBmpThreshMethods are (TM_Mean, TM_MinMax);

ThresholdAdaptiveBitmap(bmp, 0, 255, false, TM_Mean, 0);

ThresholdAdaptiveMatrix

procedure ThresholdAdaptiveMatrix(var Matrix: T2DIntegerArray; Alpha, Beta: Byte; Invert: Boolean; Method: TBmpThreshMethod; C: Integer);

Applys a ThresholdAdaptive filter onto a image matrix (created by BitmapToMatrix). Vaild TBmpThreshMethods are (TM_Mean, TM_MinMax);

ThresholdAdaptiveBitmap(matrix, 0, 255, false, TM_Mean, 0);

ResizeBilinearMatrix

procedure ResizeBilinearMatrix(var Matrix: T2DIntegerArray; NewW, NewH: Integer);

Uses the Bilinear resize method to resize a image matrix (created by BitmapToMatrix) to width newW and height newH.

ResizeBilinearMatrix(matrix, 500, 500);

BitmapExists

function BitmapExists(Index: Integer): Boolean

BlurBitmap

procedure BlurBitmap(const Bitmap, block: Integer)

CalculatePixelShiftTPA

function CalculatePixelShiftTPA(Bitmap1, Bitmap2: Integer; CPoints: TPointArray): Integer

CalculatePixelToleranceTPA

function CalculatePixelToleranceTPA(Bitmap1, Bitmap2: Integer; CPoints: TPointArray; CTS: Integer): Extended

ConvoluteBitmap

function ConvoluteBitmap(Bitmap: Integer; matrix: T2DExtendedArray): Integer

Desaturate

function Desaturate(Bitmap: Integer): Integer

DrawSystemTextBitmap

procedure DrawSystemTextBitmap(const Bitmap: Integer; const Text, FontName: string; const FontSize: Integer; const pnt: TPoint; const Shadow: Boolean; const Color: Integer);

DrawTextBitmap

procedure DrawTextBitmap(const Bitmap: Integer; const Text, FontName: string; const pnt: TPoint; const Shadow: Boolean; const Color: Integer)

FindColorsBitmap

function FindColorsBitmap(Bitmap: Integer; var points: TPointArray; const Color: Integer): Boolean

RectangleBitmapEx

procedure RectangleBitmapEx(const Bitmap: Integer; const Box: TBox; const Color: Integer; const Transparency: Extended)

ResizeBitmapEx

procedure ResizeBitmapEx(const Bitmap: Integer; const Method: TBitmapResizeMethod; const NewWidth, NewHeight: Integer)

RotateBitmapEx

function RotateBitmapEx(Bitmap: Integer; Angle: Single; Expand: Boolean; Smooth: Boolean): Integer