3

I am converting from VCL to FMX. In VCL, there was a function in a TBitmap's TCanvas called FloodFill, which allowed for a TBitmap's canvas to be flooded with a specific color, until another specific color was reached on the bitmap's canvas.

Is there an equivalent to this function in FMX?

3 Answers 3

3

Base on RRUZ's answer and Anthony's reply, I made this code:

Procedure TForm1.FloodFill(BitmapData:TBitmapData; X, Y:Integer;  OldColor, NewColor: TAlphaColor);
var
  Current: TAlphaColor;
begin
  Current := BitmapData.GetPixel(X, Y);
  if Current = OldColor then begin
    BitmapData.SetPixel(X,Y,NewColor);
    FloodFill(BitmapData, X+1, Y, OldColor, NewColor);
    FloodFill(BitmapData, X-1, Y, OldColor, NewColor);
    FloodFill(BitmapData, X, Y+1, OldColor, NewColor);
    FloodFill(BitmapData, X, Y-1, OldColor, NewColor);
  end;
end;

And a usage sample:

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Single);
var
  MyBmpData: TBitmapData;
  OldColor, NewColor: TAlphaColor;
  ix, iy: integer;
begin
  Image1.Bitmap.Canvas.Bitmap.Map(TMapAccess.ReadWrite, MyBmpData);

  ix := round(X); iy := Round(Y);
  OldColor := MyBmpData.GetPixel(ix, iy);
  NewColor := ColorComboBox1.Color; // or use some other source for a new color
  FloodFill(MyBmpData, ix, iy, OldColor, NewColor) ;

  Image1.Bitmap.Canvas.Bitmap.Unmap(MyBmpData);
end;
3
  • 1
    Welcome to StackOverflow! Please add some text to your code where you describe what it is doing. Users who are stuck here will copy, paste and forget, but it would be better to learn something :)
    – Hille
    Apr 4, 2018 at 6:54
  • If I am reading this function right, it does a flood fill of a certain color with a replacement color, stopping whenever it reaches a different color or the edges of the bitmap? Apr 6, 2018 at 23:09
  • Yes it does, I think what is called as floodfill. Floodfill with FsSurface specifically Apr 7, 2018 at 8:57
1

There is not a FloodFill function equivalent in FireMonkey, but you can use a path(TPathData) which can be filled. So you could define a path with the shape to fill, and then use the FMX.Graphics.TCanvas.FillPath to do the interior painting.

1
  • To replicate similar, relevant (in my case) functionality to the VCL FloodFill, I wrote a simple 4-way FloodFill function using a TBitmapData to get and set pixels. Apr 3, 2014 at 12:15
1

I converted this old floodfill code (posted by Jon Lennart Berg) to support Firemonkey bitmaps:
https://groups.google.com/forum/#!topic/borland.public.delphi.graphics/84pyiclLTvg

procedure Bitmap_FloodFill(fBitmap: TBitmap; StartX,StartY : Integer; FillColor: TAlphaColor);
var
  fBitmapData  : TBitmapData;
  X, Y         : Integer;
  ReplaceColor : TAlphaColor;
  Stack        : Array of TPoint;
  fHeight      : Integer;
  fWidth       : Integer;

  procedure PutInStack(X, Y: Integer);
  begin
    SetLength(Stack, Length(Stack)+1);
    Stack[Length(Stack)-1] := Point(X, Y);
  end;

  procedure GetFromStack(var X, Y: Integer);
  begin
    X := Stack[Length(Stack)-1].X;
    Y := Stack[Length(Stack)-1].Y;
    SetLength(Stack, Length(Stack)-1);
  end;

begin
  X := StartX;
  Y := StartY;
  fHeight := fBitmap.Height;
  fWidth  := fBitmap.Width;
  if (X >= fWidth) or (Y >= fHeight) then Exit;

  If fBitmap.Map(TMapAccess.ReadWrite,fBitmapData) then
  Try
    ReplaceColor := fBitmapData.GetPixel(X,Y);
    If ReplaceColor <> FillColor then
    Begin
      PutInStack(X,Y);
      While Length(Stack) > 0 do
      Begin
        GetFromStack(X,Y);
        While (X >      0) and (fBitmapData.GetPixel(X-1, Y) = ReplaceColor) do Dec(X);
        While (X < fWidth) and (fBitmapData.GetPixel(X  , Y) = ReplaceColor) do
        Begin
          if Y   >       0 then If fBitmapData.GetPixel(X, Y-1) = ReplaceColor then PutInStack(X, Y-1);
          if Y+1 < fHeight then If fBitmapData.GetPixel(X, Y+1) = ReplaceColor then PutInStack(X, Y+1);
          fBitmapData.SetPixel(X,Y,FillColor);
          Inc(X);
        End;
      End;
    End;
  Finally
    fBitmap.Canvas.Bitmap.Unmap(fBitmapData);
  End;
end;

This code could be optimized by replacing the GetPixel/SetPixel functions with scanlines and direct memory access, but even as-is, it's fast enough for most operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.