Bitmap/Flood fill: Difference between revisions

no edit summary
(→‎{{header|Lua}}: added Lua solution)
No edit summary
Line 1,842:
Import the test image and fill the region containing the pixel at coordinate 100,100 with red (RGB 100%,0%,0%) using a tolerance of 1%
<pre>floodFill[Import["http://rosettacode.org/mw/images/0/0f/Unfilledcirc.png"], {100, 100}, 0.01, {1, 0, 0}]</pre>
 
=={{header|Pascal}}==
{{trans|C#}}
<lang Pascal>
 
program FloodFillTest;
 
{$APPTYPE CONSOLE}
{$R *.res}
 
uses
Winapi.Windows,
System.SysUtils,
System.Generics.Collections,
vcl.Graphics;
 
procedure FloodFill(bmp: tBitmap; pt: TPoint; targetColor: TColor;
replacementColor: TColor);
var
q: TQueue<TPoint>;
n, w, e: TPoint;
begin
q := TQueue<TPoint>.Create;
 
q.Enqueue(pt);
 
while (q.Count > 0) do
begin
n := q.Dequeue;
if bmp.Canvas.Pixels[n.X, n.Y] <> targetColor then
Continue;
 
w := n;
e := TPoint.Create(n.X + 1, n.Y);
 
while ((w.X >= 0) and (bmp.Canvas.Pixels[w.X, w.Y] = targetColor)) do
begin
bmp.Canvas.Pixels[w.X, w.Y] := replacementColor;
if ((w.Y > 0) and (bmp.Canvas.Pixels[w.X, w.Y - 1] = targetColor)) then
q.Enqueue(TPoint.Create(w.X, w.Y - 1));
if ((w.Y < bmp.Height - 1) and (bmp.Canvas.Pixels[w.X, w.Y + 1] = targetColor)) then
q.Enqueue(TPoint.Create(w.X, w.Y + 1));
dec(w.X);
end;
 
while ((e.X <= bmp.Width - 1) and (bmp.Canvas.Pixels[e.X, e.Y] = targetColor)) do
begin
bmp.Canvas.Pixels[e.X, e.Y] := replacementColor;
if ((e.Y > 0) and (bmp.Canvas.Pixels[e.X, e.Y - 1] = targetColor)) then
q.Enqueue(TPoint.Create(e.X, e.Y - 1));
 
if ((e.Y < bmp.Height - 1) and (bmp.Canvas.Pixels[e.X, e.Y + 1] = targetColor)) then
q.Enqueue(TPoint.Create(e.X, e.Y + 1));
inc(e.X);
end;
end;
q.Free;
end;
 
var
bmp: TBitmap;
 
begin
bmp := TBitmap.Create;
try
bmp.LoadFromFile('Unfilledcirc.bmp');
FloodFill(bmp, TPoint.Create(200, 200), clWhite, clRed);
FloodFill(bmp, TPoint.Create(100, 100), clBlack, clBlue);
bmp.SaveToFile('Filledcirc.bmp');
finally
bmp.Free;
end;
 
end.
</lang>
 
=={{header|Perl}}==
478

edits