SRL Archive - OSR Help

Counting bitmaps in an area


Flight

2011-01-23 19:57:13

I'm having some trouble counting bitmaps within a specified X/Y region and I've tried everything I could think of, searched the forums for hours on end and still to no avail.

1. I use Simba, not SCAR
2. This is not for Runescape

Here is a method by lordsaturn:
function FindBitmapsIn(bmp, x1, y1, x2, y2, Tol: integer): TPointArray;
var
x, y, bitmap, w, h, L, R, G, B: integer;
begin
w := x2 - x1;
h := y2 - y1;
bitmap := BitmapFromString(w, h, '');
CopyClientToBitmap(bitmap, x1, y1, x2, y2);
SetTargetBitmap(bitmap);
while FindBitmapToleranceIn(bmp, x, y, 0, 0, w, h, Tol) do
begin
try
if PointInTPA(Point(x, y), Result) then Break;
except end;
SetLength(Result, L+1);
Result[L] := Point(x, y);
Inc(L);
ColortoRGB(FastGetPixel(bitmap, x, y), R, G, B);
FastSetPixel(bitmap, x, y, RGBtoColor(iAbs(R-255), iAbs(G-255), iAbs(B-255)));
end;
FreeBitmap(bitmap);
ResetDc;
end;

Maybe all I simply need is the Simba-equivalent to SCAR's 'ResetDc' method. Even with commenting out ResetDc I still have a flood of un-freed bitmaps, and to convert the result from this method to an integer I simply use something like this in another method: (This is just an example, not using real values)
BitmapCount := High(FindBitmapsIn(BitMapID,500,300,600,400,25)):
It's probably an obvious problem and an easy fix to most of you, but I'm still learning as I go so any ideas you can throw my way would be much appreciated. :)

Nava2

2011-01-23 21:08:45

Why not just do:
CountBitmaps: Integer;
begin
Result := Length(FindBitmaps);
end;

I left some parameters out though haha.

Boreas

2011-01-23 21:30:36

FindBitmapsSpiralTolerance?
http://wizzup.org/simbadoc/scriptref/bitmaps.html#findbitmapsspiraltolerance

Flight

2011-01-23 22:43:19

Do you have the source for the FindBitmapsSpiralTolerance function?

I don't see how you could call upon this method to return an integer, because this method itself returns as a statement?

Has anyone used this particular method before? I'd sure love to see how they were able to use it.

Boreas

2011-01-23 22:55:28

https://github.com/MerlijnWajer/Simba/blob/master/Units/MMLCore/finder.pas
It doesn't return an integer, but you feed it a TPA, call the function, and then get the length of the TPA. Use the middle of x1,y1,x2,y2 for the x,y, unless you want to rewrite it to not use a spiral.

Flight

2011-01-24 23:10:44

Ha yeah I guess I still have alot to learn about Scar and Simba procedures, so thanks anyways for the help guys but I'm just not getting it and after hours of trying I'm giving up on using a simple formula, at this point I'll just make my own sloppy bitmap counter.

Thanks for trying to help anyways.

Boreas

2011-01-25 00:07:30

Does this work?
if FindBitmapsSpiralTolerance(TheBitmap, (x1+x2)/2, (y1+y2)/2, TheTPA, x1, y1, x2, y2, TheTolerance) then 
BitmapCount := Length(TheTPA);

Flight

2011-01-25 00:52:10

TPA would need to be the "Point(x,y)" in the center of the area you're searching for bitmaps, yes?

Boreas

2011-01-25 01:04:34

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

bitmap: bitmap
x, y : start of spiral (usually center of box) eg (x1+x2)/2, (y1+y2)/2
Points: notice the var here (see also GetMousePos(var x, y)) this means you pass the function a variable, and it will write data to it. In this case, give it a blank TPA (declared without a length), and the function put in the top left corner of each occurrence of the bitmap it finds (ordered by distance from start of spiral).
xs….. : box
tol : bitmap tol

Flight

2011-01-25 01:33:17

Yes that did the trick. Boreas thank you so much for taking the time to help me out, it's very much appreciated!

Before while trying to use FindBitmapsSpiralTolerance I had assumed the x/y were the x/y coordinates of each bitmap that was found within the area, therefore I defined the x and y as blank coordinates assuming they'd be defined when the bitmap(s) was found. From all the other usage of TPA's I've only seen them being used as already pre-defined, but using a blank TPA and letting the function find it according to the bitmaps's coordinates… that really saved my life. :D

Boreas

2011-01-25 02:06:17

You're welcome. More info on 'var' if your're interested:

function Foo(var Bar:integer; OtherParam: boolean): string;

MyStr := Foo(MyInt, MyBool);

Foo can take input from MyInt and MyBool. It can give output to MyStr and MyInt.

So var indicates you can read and write to a parameter. Usually supplied as a blank in finder functions like FindBitmapsSpiralTolerance (also important to declare arrays with dynamic length), or supplied as a full array in functions that rearrange the order*.

*However some sorting functions do not use var so that they don't touch the supplied array, and instead return a new one, which is better in some situations.
function SortArr(TheTPA: TPointArray): TPointarray;
NewArr := SortArr(OldArr);