analysing a bitmap



i'm trying to copy all closed pixels of the same color of a bitmap (only
black and white)

i got an example on how to do this:

#############################################################################
program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

const
N = 5;
type
TCoordinate = 1..N;
TColour = '0'..'1';
TImage = array [TCoordinate, TCoordinate] of TColour;

procedure Dump(const Image: TImage; const XX, YY: TCoordinate);
var X, Y: TCoordinate;
begin
for X:=Low(X) to High(X)
do begin
for Y:=Low(Y) to High(Y)
do begin
if (X=XX) and (Y=YY)
then Write('*')
else Write(Image[X, Y]);
end;
WriteLn;
end;
WriteLn;
end;

procedure Fill
(var Image: TImage;
const X, Y: TCoordinate;
const Colour: TColour);
var
OldColour: TColour;
begin
OldColour:=Image[X, Y];
Image[X, Y]:=Colour;

Dump(Image, X, Y);

{ Look left }
if (Low(X)<X) and (Image[Pred(X), Y]=OldColour)
then Fill(Image, Pred(X), Y, Colour);

{ Look right }
if (X<High(X)) and (Image[Succ(X), Y]=OldColour)
then Fill(Image, Succ(X), Y, Colour);

{ Look up }
if (Low(Y)<Y) and (Image[X, Pred(Y)]=OldColour)
then Fill(Image, X, Pred(Y), Colour);

{ Look down }
if (Y<High(Y)) and (Image[X, Succ(Y)]=OldColour)
then Fill(Image, X, Succ(Y), Colour);
end;

const
Image : TImage
= (('1', '1', '1', '0', '0'),
('1', '0', '0', '0', '1'),
('1', '0', '1', '1', '0'),
('0', '0', '1', '0', '0'),
('0', '1', '1', '0', '1')
);
var
NewImage: TImage;
begin
NewImage:=Image;
Fill(NewImage, (Pred(N+2)) div 2, (Pred(N+2)) div 2, '0');
end.

#############################################################################

now i'm trying to modify this for bitmaps, but i allways get a
stack-overflow in the fill() methode.
i have no clue whats the problem :(

#############################################################################

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TCoordinateX = 1..242;
TCoordinateY = 1..102;
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private-Deklarationen }
CacheBitmap: TBitmap;
MyImageMap: array [TCoordinateX, TCoordinateY] of integer;
procedure Fill(X: TCoordinateX; Y: TCoordinateY; Colour: integer);
procedure BuildImageMap;
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BuildImageMap;
var
TempBitmap: TBitmap;
x, y: Integer;
P: PRGBQuad; // definiert in Windows.pas
begin
TempBitmap:= TBitMap.create;
//MyImageMap:= MyImageMap;
try
TempBitmap.Assign(Image1.Picture);
TempBitmap.PixelFormat:= pf32bit;

for y:= 0 to TempBitmap.Height-1 do
begin
P:= TempBitmap.ScanLine[y];
for x:= 0 to TempBitmap.Width-1 do
begin
if (P^.rgbRed = 0) and (P^.rgbGreen = 0) and (P^.rgbBlue = 0) then
MyImageMap[X, Y]:= 1
else
MyImageMap[X, Y]:= 0;
end;
end;
finally
TempBitmap.Free;
end;
end;

procedure TForm1.Fill(X: TCoordinateX; Y: TCoordinateY; Colour: integer);
var OldColour: Integer;
begin
OldColour:=MyImageMap[X, Y];
MyImageMap[X, Y]:=Colour;

//Dump(Image, X, Y);

{ Look left }
if (Low(X)<X) and (MyImageMap[Pred(X), Y]=OldColour)
then Fill(Pred(X), Y, Colour);

{ Look right }
if (X<High(X)) and (MyImageMap[Succ(X), Y]=OldColour)
then Fill(Succ(X), Y, Colour);

{ Look up }
if (Low(Y)<Y) and (MyImageMap[X, Pred(Y)]=OldColour)
then Fill(X, Pred(Y), Colour);

{ Look down }
if (Y<High(Y)) and (MyImageMap[X, Succ(Y)]=OldColour)
then Fill(X, Succ(Y), Colour);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
BuildImageMap;
Fill((Pred(242+2)) div 2, (Pred(102+2)) div 2, 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
CacheBitmap:= TBitMap.create;
CacheBitmap.PixelFormat:= pf32bit;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
CacheBitmap.Free;
end;

end.
#############################################################################


.