Bitmap/Flood fill: Difference between revisions

Content added Content deleted
(add ocaml)
m (syntax highlighting fixup automation)
Line 12: Line 12:
In the following solution a simple implementation of queue has been used.
In the following solution a simple implementation of queue has been used.
{{libheader|Action! Bitmap tools}}
{{libheader|Action! Bitmap tools}}
<lang Action!>INCLUDE "H6:RGBCIRCL.ACT" ;from task Midpoint circle algorithm
<syntaxhighlight lang=Action!>INCLUDE "H6:RGBCIRCL.ACT" ;from task Midpoint circle algorithm


RGB black,white,yellow,blue
RGB black,white,yellow,blue
Line 182: Line 182:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Flood_fill.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Flood_fill.png Screenshot from Atari 8-bit computer]


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>procedure Flood_Fill
<syntaxhighlight lang=ada>procedure Flood_Fill
( Picture : in out Image;
( Picture : in out Image;
From : Point;
From : Point;
Line 292: Line 292:
Column (From);
Column (From);
end if;
end if;
end Flood_Fill;</lang>
end Flood_Fill;</syntaxhighlight>
The procedure has the following parameters. ''Picture'' is the image to change. ''From'' is the point to start at. ''Fill'' is the color to fill with. ''Replace'' is the color to replace. ''Distance'' defines the range of color around ''Replace'' to replace as well. The distance is defined as a maximum of the differences of stimuli. The following code snippet reads the test file, fills the area between two circles red, and writes the result:
The procedure has the following parameters. ''Picture'' is the image to change. ''From'' is the point to start at. ''Fill'' is the color to fill with. ''Replace'' is the color to replace. ''Distance'' defines the range of color around ''Replace'' to replace as well. The distance is defined as a maximum of the differences of stimuli. The following code snippet reads the test file, fills the area between two circles red, and writes the result:
<lang ada>declare
<syntaxhighlight lang=ada>declare
File : File_Type;
File : File_Type;
begin
begin
Line 312: Line 312:
Close (File);
Close (File);
end;
end;
end;</lang>
end;</syntaxhighlight>
=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 100 GR:GOSUB 330"DRAW THE DEATH STAR"
<syntaxhighlight lang=gwbasic> 100 GR:GOSUB 330"DRAW THE DEATH STAR"
110 COLOR= 12
110 COLOR= 12
120 X = 20:Y = 30: GOSUB 140"FLOOD FILL"
120 X = 20:Y = 30: GOSUB 140"FLOOD FILL"
Line 341: Line 341:
350 F = 1 - R:X = 0:Y = R:DX = 0:DY = - 2 * R:PLOT CX,CY + R:PLOT CX,CY - R:HLIN CX - R,CX + R AT CY: IF X > = Y THEN RETURN
350 F = 1 - R:X = 0:Y = R:DX = 0:DY = - 2 * R:PLOT CX,CY + R:PLOT CX,CY - R:HLIN CX - R,CX + R AT CY: IF X > = Y THEN RETURN
360 FOR I = 0 TO 1:IF F > = 0 THEN Y = Y - 1:DY = DY + 2:F = F + DY
360 FOR I = 0 TO 1:IF F > = 0 THEN Y = Y - 1:DY = DY + 2:F = F + DY
370 X = X + 1:DX = DX + 2:F = F + DX + 1:HLIN CX - X,CX + X AT CY + Y:HLIN CX - X,CX + X AT CY - Y:HLIN CX - Y,CX + Y AT CY + X:HLIN CX - Y,CX + Y AT CY - X: I = X > = Y : NEXT I : RETURN</lang>
370 X = X + 1:DX = DX + 2:F = F + DX + 1:HLIN CX - X,CX + X AT CY + Y:HLIN CX - X,CX + X AT CY - Y:HLIN CX - Y,CX + Y AT CY + X:HLIN CX - Y,CX + Y AT CY - X: I = X > = Y : NEXT I : RETURN</syntaxhighlight>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
* <code>x</code>, <code>y</code> are the initial coords (relative to screen unless the <code>relative</code> parameter is true).
* <code>x</code>, <code>y</code> are the initial coords (relative to screen unless the <code>relative</code> parameter is true).
Line 351: Line 351:
=== Recursive ===
=== Recursive ===
This is limited to %StackSize% pixels.
This is limited to %StackSize% pixels.
<lang AutoHotkey>SetBatchLines, -1
<syntaxhighlight lang=AutoHotkey>SetBatchLines, -1
CoordMode, Mouse
CoordMode, Mouse
CoordMode, Pixel
CoordMode, Pixel
Line 391: Line 391:
FloodFill(x-1, y-1, target, replacement, key)
FloodFill(x-1, y-1, target, replacement, key)
}
}
}</lang>
}</syntaxhighlight>


=== Iterative ===
=== Iterative ===
<lang AutoHotkey>#NoEnv
<syntaxhighlight lang=AutoHotkey>#NoEnv
#SingleInstance, Force
#SingleInstance, Force


Line 448: Line 448:
DllCall("ReleaseDC", UInt, 0, UInt, hDC)
DllCall("ReleaseDC", UInt, 0, UInt, hDC)
DllCall("DeleteObject", UInt, hBrush)
DllCall("DeleteObject", UInt, hBrush)
}</lang>
}</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
BBC BASIC has a built-in flood fill statement, but to satisfy the terms of the task it is not used in this example.
BBC BASIC has a built-in flood fill statement, but to satisfy the terms of the task it is not used in this example.
<lang bbcbasic> MODE 8
<syntaxhighlight lang=bbcbasic> MODE 8
GCOL 15
GCOL 15
CIRCLE FILL 640, 512, 500
CIRCLE FILL 640, 512, 500
Line 475: Line 475:
PROCflood(X%, Y%-2, C%)
PROCflood(X%, Y%-2, C%)
NEXT
NEXT
ENDPROC</lang>
ENDPROC</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
===Simple and complete example in C89===
===Simple and complete example in C89===
<lang C>/*
<syntaxhighlight lang=C>/*
* RosettaCode: Bitmap/Flood fill, language C, dialects C89, C99, C11.
* RosettaCode: Bitmap/Flood fill, language C, dialects C89, C99, C11.
*
*
Line 585: Line 585:
writePortableBitMap(stdout);
writePortableBitMap(stdout);
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


===Second example ===
===Second example ===
<syntaxhighlight lang=c>
<lang c>
// http://commons.wikimedia.org/wiki/File:Julia_immediate_basin_1_3.png
// http://commons.wikimedia.org/wiki/File:Julia_immediate_basin_1_3.png


Line 696: Line 696:
}
}
</syntaxhighlight>
</lang>


===Third example===
===Third example===
Line 703: Line 703:
The <code>sys/queue.h</code> is not POSIX. (See [[FIFO#C|FIFO]])
The <code>sys/queue.h</code> is not POSIX. (See [[FIFO#C|FIFO]])


<lang c>/* #include <sys/queue.h> */
<syntaxhighlight lang=c>/* #include <sys/queue.h> */
typedef struct {
typedef struct {
color_component red, green, blue;
color_component red, green, blue;
Line 711: Line 711:
void floodfill(image img, int px, int py,
void floodfill(image img, int px, int py,
rgb_color_p bankscolor,
rgb_color_p bankscolor,
rgb_color_p rcolor);</lang>
rgb_color_p rcolor);</syntaxhighlight>


<lang c>#include "imglib.h"
<syntaxhighlight lang=c>#include "imglib.h"


typedef struct _ffill_node {
typedef struct _ffill_node {
Line 805: Line 805:
}
}
return pixelcount;
return pixelcount;
}</lang>
}</syntaxhighlight>


The '''pixelcount''' could be used to know the area of the filled region. The ''internal'' parameter <code>tolerance</code> can be tuned to cope with antialiasing, bringing "sharper" resuts.
The '''pixelcount''' could be used to know the area of the filled region. The ''internal'' parameter <code>tolerance</code> can be tuned to cope with antialiasing, bringing "sharper" resuts.
Line 813: Line 813:
(Comments show changes to fill the white area instead of the black circle)
(Comments show changes to fill the white area instead of the black circle)


<lang c>#include <stdio.h>
<syntaxhighlight lang=c>#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include "imglib.h"
#include "imglib.h"
Line 839: Line 839:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 847: Line 847:
This implementation matches exact colours only. Since the example image has grey pixels around the edges of the circles, these will remain grey after the interiors are filled.
This implementation matches exact colours only. Since the example image has grey pixels around the edges of the circles, these will remain grey after the interiors are filled.


<lang csharp>
<syntaxhighlight lang=csharp>
using System;
using System;
using System.Collections.Generic;
using System.Collections.Generic;
Line 901: Line 901:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 909: Line 909:


'''Interface'''
'''Interface'''
<lang cpp>#ifndef PROCESSING_FLOODFILLALGORITHM_H_
<syntaxhighlight lang=cpp>#ifndef PROCESSING_FLOODFILLALGORITHM_H_
#define PROCESSING_FLOODFILLALGORITHM_H_
#define PROCESSING_FLOODFILLALGORITHM_H_


Line 936: Line 936:


#endif /* PROCESSING_FLOODFILLALGORITHM_H_ */
#endif /* PROCESSING_FLOODFILLALGORITHM_H_ */
</syntaxhighlight>
</lang>
'''Implementation'''
'''Implementation'''
<lang cpp>#include "FloodFillAlgorithm.h"
<syntaxhighlight lang=cpp>#include "FloodFillAlgorithm.h"


FloodFillAlgorithm::~FloodFillAlgorithm() {
FloodFillAlgorithm::~FloodFillAlgorithm() {
Line 982: Line 982:
}
}


</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
This version uses the bitmap module from the Bitmap Task, matches exact colours only, and is derived from the Go version (to avoid stack overflow because unlike Go the D stack is not segmented).
This version uses the bitmap module from the Bitmap Task, matches exact colours only, and is derived from the Go version (to avoid stack overflow because unlike Go the D stack is not segmented).


<lang d>import std.array, bitmap;
<syntaxhighlight lang=d>import std.array, bitmap;


void floodFill(Color)(Image!Color img, in uint x, in uint y,
void floodFill(Color)(Image!Color img, in uint x, in uint y,
Line 1,015: Line 1,015:
img.floodFill(200, 200, RGB(127, 0, 0));
img.floodFill(200, 200, RGB(127, 0, 0));
img.savePPM6("unfilled_circ_flooded.ppm");
img.savePPM6("unfilled_circ_flooded.ppm");
}</lang>
}</syntaxhighlight>
=={{header|Delphi}}==
=={{header|Delphi}}==
See [[#Pascal]].
See [[#Pascal]].
Line 1,022: Line 1,022:
Using the image type from [[Basic bitmap storage#E]].
Using the image type from [[Basic bitmap storage#E]].


<lang e>def floodFill(image, x, y, newColor) {
<syntaxhighlight lang=e>def floodFill(image, x, y, newColor) {
def matchColor := image[x, y]
def matchColor := image[x, y]
def w := image.width()
def w := image.width()
Line 1,090: Line 1,090:


fillScan(x, y)
fillScan(x, y)
}</lang>
}</syntaxhighlight>


[[File:Filledcirc-E.png|128px|thumb|right|Filled sample image]]Note that this does not make any attempt to smoothly fill 'banks' or have a tolerance; it matches exact colors only. This will fill the example image with red inside green, and there will be black/white fringes:
[[File:Filledcirc-E.png|128px|thumb|right|Filled sample image]]Note that this does not make any attempt to smoothly fill 'banks' or have a tolerance; it matches exact colors only. This will fill the example image with red inside green, and there will be black/white fringes:


<syntaxhighlight lang=e>{
<lang e>{
println("Read")
println("Read")
def i := readPPM(<import:java.io.makeFileInputStream>(<file:Unfilledcirc.ppm>))
def i := readPPM(<import:java.io.makeFileInputStream>(<file:Unfilledcirc.ppm>))
Line 1,104: Line 1,104:
i.writePPM(<import:java.io.makeFileOutputStream>(<file:Filledcirc.ppm>))
i.writePPM(<import:java.io.makeFileOutputStream>(<file:Filledcirc.ppm>))
println("Done")
println("Done")
}</lang>
}</syntaxhighlight>


=={{header|ERRE}}==
=={{header|ERRE}}==
In "PC.LIB" library there is a FILL procedure that do the job, but the example program implements the algorithm in ERRE language using an iterative method. This program is taken from the distribution disk and works in 320x200 graphics.
In "PC.LIB" library there is a FILL procedure that do the job, but the example program implements the algorithm in ERRE language using an iterative method. This program is taken from the distribution disk and works in 320x200 graphics.
<lang ERRE>
<syntaxhighlight lang=ERRE>
PROGRAM MYFILL_DEMO
PROGRAM MYFILL_DEMO


Line 1,196: Line 1,196:
FLOOD_FILL(100,100,0,1)
FLOOD_FILL(100,100,0,1)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>
Note: I haven't an "Upload files" item, so I can't show the resulting image!
Note: I haven't an "Upload files" item, so I can't show the resulting image!


Line 1,203: Line 1,203:
Using an emulated stack. EMT's recursive stack space is limited. For the notebook with images see [http://www.euler-math-toolbox.de/renegrothmann/Flood%20Fill.html this page].
Using an emulated stack. EMT's recursive stack space is limited. For the notebook with images see [http://www.euler-math-toolbox.de/renegrothmann/Flood%20Fill.html this page].


<lang>
<syntaxhighlight lang=text>
>file="test.png";
>file="test.png";
>A=loadrgb(file); ...
>A=loadrgb(file); ...
Line 1,240: Line 1,240:
>B=floodfill(B,200,200,rgb(0,0,0.5),0.5);
>B=floodfill(B,200,200,rgb(0,0,0.5),0.5);
>insrgb(B);
>insrgb(B);
</syntaxhighlight>
</lang>


=={{header|FBSL}}==
=={{header|FBSL}}==
'''Using pure FBSL's built-in graphics functions:'''
'''Using pure FBSL's built-in graphics functions:'''
<lang qbasic>#DEFINE WM_LBUTTONDOWN 513
<syntaxhighlight lang=qbasic>#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
#DEFINE WM_CLOSE 16


Line 1,280: Line 1,280:
CIRCLE(FBSL.GETDC, Breadth / 2, Height / 2, 85, &HFFFFFF, 0, 360, 1, TRUE) _ ' White
CIRCLE(FBSL.GETDC, Breadth / 2, Height / 2, 85, &HFFFFFF, 0, 360, 1, TRUE) _ ' White
(FBSL.GETDC, Breadth / 3, Height / 3, 30, 0, 0, 360, 1, TRUE) ' Black
(FBSL.GETDC, Breadth / 3, Height / 3, 30, 0, 0, 360, 1, TRUE) ' Black
END SUB</lang>
END SUB</syntaxhighlight>
'''Output:''' [[File:FBSLFlood.PNG]]
'''Output:''' [[File:FBSLFlood.PNG]]


=={{header|Forth}}==
=={{header|Forth}}==
This simple recursive algorithm uses routines from [[Basic bitmap storage]].
This simple recursive algorithm uses routines from [[Basic bitmap storage]].
<lang forth>: third 2 pick ;
<syntaxhighlight lang=forth>: third 2 pick ;
: 3dup third third third ;
: 3dup third third third ;
: 4dup 2over 2over ;
: 4dup 2over 2over ;
Line 1,312: Line 1,312:
swap 1- swap
swap 1- swap
then
then
r> drop ;</lang>
r> drop ;</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 1,319: Line 1,319:
Here the ''target color'' paradigm is used. Again the <code>matchdistance</code> parameter can be tuned to ignore small differences that could come because of antialiasing.
Here the ''target color'' paradigm is used. Again the <code>matchdistance</code> parameter can be tuned to ignore small differences that could come because of antialiasing.


<lang fortran>module RCImageArea
<syntaxhighlight lang=fortran>module RCImageArea
use RCImageBasic
use RCImageBasic
use RCImagePrimitive
use RCImagePrimitive
Line 1,424: Line 1,424:
end subroutine floodfill
end subroutine floodfill


end module RCImageArea</lang>
end module RCImageArea</syntaxhighlight>


Usage example excerpt (which on the test image will fill with green the inner black circle):
Usage example excerpt (which on the test image will fill with green the inner black circle):


<lang fortran> call floodfill(animage, point(100,100), rgb(0,0,0), rgb(0,255,0))</lang>
<syntaxhighlight lang=fortran> call floodfill(animage, point(100,100), rgb(0,0,0), rgb(0,255,0))</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
<lang freebasic>' version 04-11-2016
<syntaxhighlight lang=freebasic>' version 04-11-2016
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,486: Line 1,486:
Sleep 2000
Sleep 2000
If InKey <> "" OrElse InKey = Chr(255) + "k" Then End
If InKey <> "" OrElse InKey = Chr(255) + "k" Then End
Loop</lang>
Loop</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
An addition to code from the bitmap task:
An addition to code from the bitmap task:
<lang go>package raster
<syntaxhighlight lang=go>package raster


func (b *Bitmap) Flood(x, y int, repl Pixel) {
func (b *Bitmap) Flood(x, y int, repl Pixel) {
Line 1,506: Line 1,506:
}
}
ff(x, y)
ff(x, y)
}</lang>
}</syntaxhighlight>
And a test program. Works with code from read ppm and write ppm to pipe tasks. For input, it uses a version of the test file converted by the Go solution to "Read an image through a pipe". For output it uses the trick from "PPM conversion through a pipe" to write the .png suitable for uploading to RC.
And a test program. Works with code from read ppm and write ppm to pipe tasks. For input, it uses a version of the test file converted by the Go solution to "Read an image through a pipe". For output it uses the trick from "PPM conversion through a pipe" to write the .png suitable for uploading to RC.
[[File:Go_flood.png|right]]
[[File:Go_flood.png|right]]
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 1,537: Line 1,537:
log.Fatal(err)
log.Fatal(err)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
This code uses the Bitmap and Bitmap.RGB modules defined [[Bitmap#Haskell|here]].
This code uses the Bitmap and Bitmap.RGB modules defined [[Bitmap#Haskell|here]].
<lang Haskell>import Data.Array.ST
<syntaxhighlight lang=Haskell>import Data.Array.ST
import Data.STRef
import Data.STRef
import Control.Monad
import Control.Monad
Line 1,651: Line 1,651:
setSpanRight p False
setSpanRight p False
scanWhileX b st p oldC newC (w, h) (Pixel (x, y + 1))
scanWhileX b st p oldC newC (w, h) (Pixel (x, y + 1))
</syntaxhighlight>
</lang>


=={{header|HicEst}}==
=={{header|HicEst}}==
HicEst color fill is via the [http://www.HicEst.com/DeCoRation.htm decoration option of WRITE()]
HicEst color fill is via the [http://www.HicEst.com/DeCoRation.htm decoration option of WRITE()]
<lang HicEst>WINDOW(WINdowhandle=wh, BaCkcolor=0, TItle="Rosetta test image")
<syntaxhighlight lang=HicEst>WINDOW(WINdowhandle=wh, BaCkcolor=0, TItle="Rosetta test image")


WRITE(WIN=wh, DeCoRation="EL=14, BC=14") ! color 14 == bright yellow
WRITE(WIN=wh, DeCoRation="EL=14, BC=14") ! color 14 == bright yellow
Line 1,662: Line 1,662:
WRITE(WIN=wh, DeCoRation="L=1/4, R=1/2, T=1/4, B=1/2, EL=25, BC=25")
WRITE(WIN=wh, DeCoRation="L=1/4, R=1/2, T=1/4, B=1/2, EL=25, BC=25")


WINDOW(Kill=wh)</lang>
WINDOW(Kill=wh)</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
'''Solution:'''<br>
'''Solution:'''<br>
Uses <code>getPixels</code> and <code>setPixels</code> from [[Basic bitmap storage#J|Basic bitmap storage]].
Uses <code>getPixels</code> and <code>setPixels</code> from [[Basic bitmap storage#J|Basic bitmap storage]].
<lang j>NB. finds and labels contiguous areas with the same numbers
<syntaxhighlight lang=j>NB. finds and labels contiguous areas with the same numbers
NB. ref: http://www.jsoftware.com/pipermail/general/2005-August/023886.html
NB. ref: http://www.jsoftware.com/pipermail/general/2005-August/023886.html
findcontig=: (|."1@|:@:>. (* * 1&(|.!.0)))^:4^:_@(* >:@i.@$)
findcontig=: (|."1@|:@:>. (* * 1&(|.!.0)))^:4^:_@(* >:@i.@$)
Line 1,676: Line 1,676:
NB.*floodFill v Floods area, defined by point and color (x), of image (y)
NB.*floodFill v Floods area, defined by point and color (x), of image (y)
NB. x is: 2-item list of (y x) ; (color)
NB. x is: 2-item list of (y x) ; (color)
floodFill=: (1&({::)@[ ;~ 0&({::)@[ getFloodpoints ]) setPixels ]</lang>
floodFill=: (1&({::)@[ ;~ 0&({::)@[ getFloodpoints ]) setPixels ]</syntaxhighlight>


'''Example Usage:'''<br>
'''Example Usage:'''<br>
The following draws the same image as for the [[Flood fill#Tcl|Tcl example image]] below.<br>
The following draws the same image as for the [[Flood fill#Tcl|Tcl example image]] below.<br>
Uses definitions from [[Basic bitmap storage#J|Basic bitmap storage]], [[Bresenham's line algorithm#J|Bresenham's line algorithm]] and [[Midpoint circle algorithm#J|Midpoint circle algorithm]].
Uses definitions from [[Basic bitmap storage#J|Basic bitmap storage]], [[Bresenham's line algorithm#J|Bresenham's line algorithm]] and [[Midpoint circle algorithm#J|Midpoint circle algorithm]].
<lang j>'white blue yellow black orange red'=: 255 255 255,0 0 255,255 255 0,0 0 0,255 165 0,:255 0 0
<syntaxhighlight lang=j>'white blue yellow black orange red'=: 255 255 255,0 0 255,255 255 0,0 0 0,255 165 0,:255 0 0
myimg=: white makeRGB 50 70
myimg=: white makeRGB 50 70
lines=: _2]\^:2 ] 0 0 25 0 , 25 0 25 35 , 25 35 0 35 , 0 35 0 0
lines=: _2]\^:2 ] 0 0 25 0 , 25 0 25 35 , 25 35 0 35 , 0 35 0 0
Line 1,689: Line 1,689:
myimg=: (5 34;orange) floodFill myimg
myimg=: (5 34;orange) floodFill myimg
myimg=: (5 36;red) floodFill myimg
myimg=: (5 36;red) floodFill myimg
viewRGB myimg</lang>
viewRGB myimg</syntaxhighlight>


'''Alternative findcontig:'''<br>
'''Alternative findcontig:'''<br>
The following alternative version of <code>findcontig</code> is less concise but is leaner, faster, works for n-dimensions and is not restricted to numerical arrays.
The following alternative version of <code>findcontig</code> is less concise but is leaner, faster, works for n-dimensions and is not restricted to numerical arrays.
<lang j>NB. ref: http://www.jsoftware.com/pipermail/general/2005-August/024174.html
<syntaxhighlight lang=j>NB. ref: http://www.jsoftware.com/pipermail/general/2005-August/024174.html
eq=:[:}:"1 [:($$[:([:+/\1:,}:~:}.),) ,&_"1 NB. equal numbers for atoms of y connected in first direction
eq=:[:}:"1 [:($$[:([:+/\1:,}:~:}.),) ,&_"1 NB. equal numbers for atoms of y connected in first direction
eq_nd=: i.@#@$(<"0@[([:, |:^:_1"0 _)&> [:EQ&.> <@|:"0 _)] NB. n-dimensional eq, gives an #@$,*/@$ shaped matrix
eq_nd=: i.@#@$(<"0@[([:, |:^:_1"0 _)&> [:EQ&.> <@|:"0 _)] NB. n-dimensional eq, gives an #@$,*/@$ shaped matrix
Line 1,699: Line 1,699:
cnnct=: [: |:@({."1<.//.]) [: ; <@(,.<./)/.~
cnnct=: [: |:@({."1<.//.]) [: ; <@(,.<./)/.~


findcontig_nd=: 3 : '($y)${. ([:({.,~}:) ([ repl cnnct)/\.)^:([:+./@(~:/)2&{.)^:_ (,{.) eq_nd (i.~ ~.@,) y'</lang>
findcontig_nd=: 3 : '($y)${. ([:({.,~}:) ([ repl cnnct)/\.)^:([:+./@(~:/)2&{.)^:_ (,{.) eq_nd (i.~ ~.@,) y'</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Input is the image, the starting node (x, y), the target color we want to fill, and the replacement color that will replace the target color. It implements a 4-way flood fill algorithm. For large images, the performance can be improved by drawing the scanlines instead of setting each pixel to the replacement color, or by working directly on the databuffer.
Input is the image, the starting node (x, y), the target color we want to fill, and the replacement color that will replace the target color. It implements a 4-way flood fill algorithm. For large images, the performance can be improved by drawing the scanlines instead of setting each pixel to the replacement color, or by working directly on the databuffer.
<lang java>import java.awt.Color;
<syntaxhighlight lang=java>import java.awt.Color;
import java.awt.Point;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImage;
Line 1,744: Line 1,744:
}
}
}
}
}</lang>
}</syntaxhighlight>
And here is an example of how to replace the white color with red from the sample image (with starting node (50, 50)):
And here is an example of how to replace the white color with red from the sample image (with starting node (50, 50)):
<lang java>import java.io.IOException;
<syntaxhighlight lang=java>import java.io.IOException;
import java.awt.Color;
import java.awt.Color;
import java.awt.Point;
import java.awt.Point;
Line 1,763: Line 1,763:
new Test();
new Test();
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Line 1,769: Line 1,769:
Inspired to [[#Python | Python]] version.
Inspired to [[#Python | Python]] version.


<lang julia>using Images, FileIO
<syntaxhighlight lang=julia>using Images, FileIO


function floodfill!(img::Matrix{<:Color}, initnode::CartesianIndex{2}, target::Color, replace::Color)
function floodfill!(img::Matrix{<:Color}, initnode::CartesianIndex{2}, target::Color, replace::Color)
Line 1,814: Line 1,814:
img = Gray{Bool}.(load("data/unfilledcircle.png"))
img = Gray{Bool}.(load("data/unfilledcircle.png"))
floodfill!(img, CartesianIndex(100, 100), Gray(false), Gray(true))
floodfill!(img, CartesianIndex(100, 100), Gray(false), Gray(true))
save("data/filledcircle.png", img)</lang>
save("data/filledcircle.png", img)</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>// version 1.1.4-3
<syntaxhighlight lang=scala>// version 1.1.4-3


import java.awt.Color
import java.awt.Color
Line 1,877: Line 1,877:
ImageIO.write(image, "png", File(title))
ImageIO.write(image, "png", File(title))
JOptionPane.showMessageDialog(null, JLabel(ImageIcon(image)), title, JOptionPane.PLAIN_MESSAGE)
JOptionPane.showMessageDialog(null, JLabel(ImageIcon(image)), title, JOptionPane.PLAIN_MESSAGE)
}</lang>
}</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>'This example requires the Windows API
<syntaxhighlight lang=lb>'This example requires the Windows API
NoMainWin
NoMainWin
WindowWidth = 267.5
WindowWidth = 267.5
Line 1,968: Line 1,968:
result = FloodFill(mouseXX, (mouseYY - 1), targetColor)
result = FloodFill(mouseXX, (mouseYY - 1), targetColor)
End If
End If
End Function</lang>
End Function</syntaxhighlight>


=={{header|Lingo}}==
=={{header|Lingo}}==
Lingo has built-in flood fill for image objects, so a custom implementation would be pointless:
Lingo has built-in flood fill for image objects, so a custom implementation would be pointless:
<lang lingo>img.floodFill(x, y, rgb(r,g,b))</lang>
<syntaxhighlight lang=lingo>img.floodFill(x, y, rgb(r,g,b))</syntaxhighlight>




Line 1,979: Line 1,979:


Preprocess with ImageMagick to simplify loading:
Preprocess with ImageMagick to simplify loading:
<lang lua>$ magick unfilledcirc.png -depth 8 unfilledcirc.ppm</lang>
<syntaxhighlight lang=lua>$ magick unfilledcirc.png -depth 8 unfilledcirc.ppm</syntaxhighlight>
Some rudimentary PPM support:
Some rudimentary PPM support:
<lang lua>function Bitmap:loadPPM(filename)
<syntaxhighlight lang=lua>function Bitmap:loadPPM(filename)
local fp = io.open( filename, "rb" )
local fp = io.open( filename, "rb" )
if fp == nil then return false end
if fp == nil then return false end
Line 2,006: Line 2,006:
end
end
fp:close()
fp:close()
end</lang>
end</syntaxhighlight>
The task itself:
The task itself:
<lang lua>function Bitmap:floodfill(x, y, c)
<syntaxhighlight lang=lua>function Bitmap:floodfill(x, y, c)
local b = self:get(x, y)
local b = self:get(x, y)
if not b then return end
if not b then return end
Line 2,025: Line 2,025:
end
end
ff(x, y)
ff(x, y)
end</lang>
end</syntaxhighlight>
Demo:
Demo:
<lang lua>bitmap = Bitmap(0, 0)
<syntaxhighlight lang=lua>bitmap = Bitmap(0, 0)
bitmap:loadPPM("unfilledcirc.ppm")
bitmap:loadPPM("unfilledcirc.ppm")
bitmap:floodfill( 1, 1, { 255,0,0 }) -- fill exterior (except bottom right) with red
bitmap:floodfill( 1, 1, { 255,0,0 }) -- fill exterior (except bottom right) with red
bitmap:floodfill( 50, 50, { 0,255,0 })-- fill larger circle with green
bitmap:floodfill( 50, 50, { 0,255,0 })-- fill larger circle with green
bitmap:floodfill( 100, 100, { 0,0,255 })-- fill smaller circle with blue
bitmap:floodfill( 100, 100, { 0,0,255 })-- fill smaller circle with blue
bitmap:savePPM("filledcirc.ppm")</lang>
bitmap:savePPM("filledcirc.ppm")</syntaxhighlight>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>createMask[img_, pos_, tol_] :=
<syntaxhighlight lang=Mathematica>createMask[img_, pos_, tol_] :=
RegionBinarize[img, Image[SparseArray[pos -> 1, ImageDimensions[img]]], tol];
RegionBinarize[img, Image[SparseArray[pos -> 1, ImageDimensions[img]]], tol];
floodFill[img_Image, pos_List, tol_Real, color_List] :=
floodFill[img_Image, pos_List, tol_Real, color_List] :=
Line 2,043: Line 2,043:
Dilation[createMask[img, pos, tol],1]
Dilation[createMask[img, pos, tol],1]
]
]
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,051: Line 2,051:
=={{header|Nim}}==
=={{header|Nim}}==
{{Trans|Python}}
{{Trans|Python}}
<lang Nim>import bitmap
<syntaxhighlight lang=Nim>import bitmap


proc floodFill*(img: Image; initPoint: Point; targetColor, replaceColor: Color) =
proc floodFill*(img: Image; initPoint: Point; targetColor, replaceColor: Color) =
Line 2,099: Line 2,099:
var img = readPPM("Unfilledcirc.ppm")
var img = readPPM("Unfilledcirc.ppm")
img.floodFill((30, 122), White, color(255, 0, 0))
img.floodFill((30, 122), White, color(255, 0, 0))
img.writePPM("Unfilledcirc_red.ppm")</lang>
img.writePPM("Unfilledcirc_red.ppm")</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
{{Trans|C}}
{{Trans|C}}
<lang ocaml>
<syntaxhighlight lang=ocaml>
let floodFill ~img (i, j) newColor =
let floodFill ~img (i, j) newColor =
let oldColor = get_pixel ~img ~pt:(i, j) in
let oldColor = get_pixel ~img ~pt:(i, j) in
Line 2,120: Line 2,120:
end;
end;
in
in
aux (i, j)</lang>
aux (i, j)</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
{{trans|C#}}
{{trans|C#}}
<lang Pascal>
<syntaxhighlight lang=Pascal>


program FloodFillTest;
program FloodFillTest;
Line 2,195: Line 2,195:


end.
end.
</syntaxhighlight>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
Line 2,203: Line 2,203:
The <tt>fill</tt> of the Perl package Image::Imlib2 is a flood fill (so the documentatin of Image::Imlib2 says). The target colour is the one of the starting point pixel; the color set with <tt>set_color</tt> is the fill colour.
The <tt>fill</tt> of the Perl package Image::Imlib2 is a flood fill (so the documentatin of Image::Imlib2 says). The target colour is the one of the starting point pixel; the color set with <tt>set_color</tt> is the fill colour.


<lang perl>#! /usr/bin/perl
<syntaxhighlight lang=perl>#! /usr/bin/perl


use strict;
use strict;
Line 2,212: Line 2,212:
$img->fill(100,100);
$img->fill(100,100);
$img->save("filledcirc.jpg");
$img->save("filledcirc.jpg");
exit 0;</lang>
exit 0;</syntaxhighlight>


A homemade implementation can be:
A homemade implementation can be:


<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use Image::Imlib2;
use Image::Imlib2;


Line 2,263: Line 2,263:
floodfill($img, 100,100, 0, 0, 0);
floodfill($img, 100,100, 0, 0, 0);
$img->save("filledcirc1.jpg");
$img->save("filledcirc1.jpg");
exit 0;</lang>
exit 0;</syntaxhighlight>


This fills better than the Image::Imlib2 <tt>fill</tt> function the inner circle, since because of JPG compression and thanks to the <tt>$distparameter</tt>, it "sees" as black also pixel that are no more exactly black.
This fills better than the Image::Imlib2 <tt>fill</tt> function the inner circle, since because of JPG compression and thanks to the <tt>$distparameter</tt>, it "sees" as black also pixel that are no more exactly black.
Line 2,271: Line 2,271:
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read a PPM file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write a PPM file]]. <br>
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read a PPM file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write a PPM file]]. <br>
Uses the output of [[Bitmap/Midpoint_circle_algorithm#Phix|Midpoint circle algorithm]] (Circle.ppm), results may be verified with demo\rosetta\viewppm.exw
Uses the output of [[Bitmap/Midpoint_circle_algorithm#Phix|Midpoint circle algorithm]] (Circle.ppm), results may be verified with demo\rosetta\viewppm.exw
<lang Phix>-- demo\rosetta\Bitmap_FloodFill.exw (runnable version)
<syntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_FloodFill.exw (runnable version)
include ppm.e -- blue, green, read_ppm(), write_ppm() (covers above requirements)
include ppm.e -- blue, green, read_ppm(), write_ppm() (covers above requirements)


Line 2,296: Line 2,296:
write_ppm("FloodIn.ppm",img)
write_ppm("FloodIn.ppm",img)
img = FloodFill(img, 10, 10, green)
img = FloodFill(img, 10, 10, green)
write_ppm("FloodOut.ppm",img)</lang>
write_ppm("FloodOut.ppm",img)</syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Using the format of [[Bitmap#PicoLisp|Bitmap]], a minimal recursive solution:
Using the format of [[Bitmap#PicoLisp|Bitmap]], a minimal recursive solution:
<lang PicoLisp>(de ppmFloodFill (Ppm X Y Color)
<syntaxhighlight lang=PicoLisp>(de ppmFloodFill (Ppm X Y Color)
(let Target (get Ppm Y X)
(let Target (get Ppm Y X)
(recur (X Y)
(recur (X Y)
Line 2,309: Line 2,309:
(recurse X (dec Y))
(recurse X (dec Y))
(recurse X (inc Y)) ) ) )
(recurse X (inc Y)) ) ) )
Ppm )</lang>
Ppm )</syntaxhighlight>
Test using 'ppmRead' from [[Bitmap/Read a PPM file#PicoLisp]] and 'ppmWrite' from [[Bitmap/Write a PPM file#PicoLisp]], filling the white area with red:
Test using 'ppmRead' from [[Bitmap/Read a PPM file#PicoLisp]] and 'ppmWrite' from [[Bitmap/Write a PPM file#PicoLisp]], filling the white area with red:
<pre>(ppmWrite
<pre>(ppmWrite
Line 2,316: Line 2,316:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>fill: procedure (x, y, fill_color) recursive; /* 12 May 2010 */
<syntaxhighlight lang=PL/I>fill: procedure (x, y, fill_color) recursive; /* 12 May 2010 */
declare (x, y) fixed binary;
declare (x, y) fixed binary;
declare fill_color bit (24) aligned;
declare fill_color bit (24) aligned;
Line 2,341: Line 2,341:
if pixel_color = area_color then call fill (x, y+1, fill_color);
if pixel_color = area_color then call fill (x, y+1, fill_color);


end fill;</lang>
end fill;</syntaxhighlight>
The following PL/I statements change the color of the white area
The following PL/I statements change the color of the white area
of the sample image to red, and the central orb to green.
of the sample image to red, and the central orb to green.
<lang>
<syntaxhighlight lang=text>
/* Fill the white area of the suggested image with red color. */
/* Fill the white area of the suggested image with red color. */
area_color = (24)'1'b;
area_color = (24)'1'b;
Line 2,352: Line 2,352:
area_color = '0'b;
area_color = '0'b;
call fill (125, 125, '000000001111111100000000'b );
call fill (125, 125, '000000001111111100000000'b );
</syntaxhighlight>
</lang>


=={{header|Processing}}==
=={{header|Processing}}==
<lang java>import java.awt.Point;
<syntaxhighlight lang=java>import java.awt.Point;
import java.util.Queue;
import java.util.Queue;
import java.util.LinkedList;
import java.util.LinkedList;
Line 2,434: Line 2,434:
img.pixels[pixel_position(x, y)] = fill_color;
img.pixels[pixel_position(x, y)] = fill_color;
return true;
return true;
}</lang>
}</syntaxhighlight>


==={{header|Processing Python mode}}===
==={{header|Processing Python mode}}===
<lang Python>from collections import deque
<syntaxhighlight lang=Python>from collections import deque


image_file = "image.png"
image_file = "image.png"
Line 2,510: Line 2,510:
return False
return False
img.pixels[pixel_position(x, y)] = fill_color
img.pixels[pixel_position(x, y)] = fill_color
return True</lang>
return True</syntaxhighlight>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
=== built-in ===
=== built-in ===
<lang PureBasic>FillArea(0,0,-1,$ff)
<syntaxhighlight lang=PureBasic>FillArea(0,0,-1,$ff)
; Fills an Area in red</lang>
; Fills an Area in red</syntaxhighlight>


=== Iterative ===
=== Iterative ===
<lang PureBasic> Procedure Floodfill(x,y,new_color)
<syntaxhighlight lang=PureBasic> Procedure Floodfill(x,y,new_color)
old_color = Point(x,y)
old_color = Point(x,y)
NewList stack.POINT()
NewList stack.POINT()
Line 2,547: Line 2,547:
Event = WaitWindowEvent()
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Until Event = #PB_Event_CloseWindow
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang=python>
import Image
import Image
def FloodFill( fileName, initNode, targetColor, replaceColor ):
def FloodFill( fileName, initNode, targetColor, replaceColor ):
Line 2,595: Line 2,595:
break
break
return img
return img
</syntaxhighlight>
</lang>


===Usage example===
===Usage example===
<lang python>
<syntaxhighlight lang=python>
# "FloodFillClean.png" is name of input file
# "FloodFillClean.png" is name of input file
# [55,55] the x,y coordinate where fill starts
# [55,55] the x,y coordinate where fill starts
Line 2,606: Line 2,606:
#The resulting image is saved as Filled.png
#The resulting image is saved as Filled.png
img.save( "Filled.png" )
img.save( "Filled.png" )
</syntaxhighlight>
</lang>


=={{header|R}}==
=={{header|R}}==
'''Stack-based recursive version'''
'''Stack-based recursive version'''
<syntaxhighlight lang=R>
<lang R>
library(png)
library(png)
img <- readPNG("Unfilledcirc.png")
img <- readPNG("Unfilledcirc.png")
Line 2,634: Line 2,634:


image(M, col = c(1, 0, 2))
image(M, col = c(1, 0, 2))
</syntaxhighlight>
</lang>
'''Queue-based version (Forest Fire algorithm)'''
'''Queue-based version (Forest Fire algorithm)'''
<syntaxhighlight lang=R>
<lang R>
library(png)
library(png)
img <- readPNG("Unfilledcirc.png")
img <- readPNG("Unfilledcirc.png")
Line 2,674: Line 2,674:


image(M, col = c(1, 0, 2, 3))
image(M, col = c(1, 0, 2, 3))
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang=racket>
#lang racket
#lang racket


Line 2,760: Line 2,760:
;; ... and after:
;; ... and after:
bm
bm
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,767: Line 2,767:
Using bits and pieces from various other bitmap tasks.
Using bits and pieces from various other bitmap tasks.


<lang perl6>class Pixel { has Int ($.R, $.G, $.B) }
<syntaxhighlight lang=raku line>class Pixel { has Int ($.R, $.G, $.B) }
class Bitmap {
class Bitmap {
has Int ($.width, $.height);
has Int ($.width, $.height);
Line 2,839: Line 2,839:


$outfile.write: $b.P6;
$outfile.write: $b.P6;
</syntaxhighlight>
</lang>


See output image [https://github.com/thundergnat/rc/blob/master/img/Bitmap-flood-perl6.png Bitmap-flood-perl6 ] (offsite image file, converted to PNG for ease of viewing)
See output image [https://github.com/thundergnat/rc/blob/master/img/Bitmap-flood-perl6.png Bitmap-flood-perl6 ] (offsite image file, converted to PNG for ease of viewing)
Line 2,845: Line 2,845:
=={{header|REXX}}==
=={{header|REXX}}==
{{trans|PL/I}}
{{trans|PL/I}}
<lang rexx>/*REXX program demonstrates a method to perform a flood fill of an area. */
<syntaxhighlight lang=rexx>/*REXX program demonstrates a method to perform a flood fill of an area. */
black= '000000000000000000000000'b /*define the black color (using bits).*/
black= '000000000000000000000000'b /*define the black color (using bits).*/
red = '000000000000000011111111'b /* " " red " " " */
red = '000000000000000011111111'b /* " " red " " " */
Line 2,869: Line 2,869:
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg $x,$y; return image.$x.$y /*return with color of the X,Y pixel.*/</lang>
@: parse arg $x,$y; return image.$x.$y /*return with color of the X,Y pixel.*/</syntaxhighlight>
<br><br>
<br><br>


Line 2,876: Line 2,876:
Uses [[Raster graphics operations/Ruby]]
Uses [[Raster graphics operations/Ruby]]


<lang ruby># frozen_string_literal: true
<syntaxhighlight lang=ruby># frozen_string_literal: true


require_relative 'raster_graphics'
require_relative 'raster_graphics'
Line 2,932: Line 2,932:
bitmap.draw_circle(Pixel[200, 100], 40, RGBColour::BLACK)
bitmap.draw_circle(Pixel[200, 100], 40, RGBColour::BLACK)
bitmap.flood_fill(Pixel[140, 160], RGBColour::BLUE)
bitmap.flood_fill(Pixel[140, 160], RGBColour::BLUE)
bitmap.save_as_png('flood_fill.png')</lang>
bitmap.save_as_png('flood_fill.png')</syntaxhighlight>


{{libheader|RubyGems}}
{{libheader|RubyGems}}
Line 2,938: Line 2,938:
JRubyArt is a port of Processing to the ruby language
JRubyArt is a port of Processing to the ruby language


<lang ruby># holder for pixel coords
<syntaxhighlight lang=ruby># holder for pixel coords
Pixel = Struct.new(:x, :y)
Pixel = Struct.new(:x, :y)


Line 2,990: Line 2,990:
size(256, 256)
size(256, 256)
end
end
</syntaxhighlight>
</lang>


=={{header|Rust}}==
=={{header|Rust}}==


<lang rust>
<syntaxhighlight lang=rust>
/* Naive Rust implementation of RosettaCode's Bitmap/Flood fill excercise.
/* Naive Rust implementation of RosettaCode's Bitmap/Flood fill excercise.
*
*
Line 3,082: Line 3,082:
write_image(data);
write_image(data);


}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
Line 3,090: Line 3,090:
See [[Basic_bitmap_storage#Scala|Basic Bitmap Storage]] for RgbBitmap class.
See [[Basic_bitmap_storage#Scala|Basic Bitmap Storage]] for RgbBitmap class.


<lang scala>import java.awt.Color
<syntaxhighlight lang=scala>import java.awt.Color
import scala.collection.mutable
import scala.collection.mutable


Line 3,132: Line 3,132:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==
Line 3,139: Line 3,139:
data structures instead.
data structures instead.


<lang sml>(* For simplicity, we're going to fill black-and-white images. Nothing
<syntaxhighlight lang=sml>(* For simplicity, we're going to fill black-and-white images. Nothing
* fundamental would change if we used more colors. *)
* fundamental would change if we used more colors. *)
datatype color = Black | White
datatype color = Black | White
Line 3,187: Line 3,187:


(* Fill the image with black starting at the center. *)
(* Fill the image with black starting at the center. *)
val () = fill test Black (3,3)</lang>
val () = fill test Black (3,3)</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 3,193: Line 3,193:
{{tcllib|struct::queue}}
{{tcllib|struct::queue}}
Using code from [[Basic bitmap storage#Tcl|Basic bitmap storage]], [[Bresenham's line algorithm#Tcl|Bresenham's line algorithm]] and [[Midpoint circle algorithm#Tcl|Midpoint circle algorithm]]
Using code from [[Basic bitmap storage#Tcl|Basic bitmap storage]], [[Bresenham's line algorithm#Tcl|Bresenham's line algorithm]] and [[Midpoint circle algorithm#Tcl|Midpoint circle algorithm]]
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang=tcl>package require Tcl 8.5
package require Tk
package require Tk
package require struct::queue
package require struct::queue
Line 3,266: Line 3,266:
toplevel .flood
toplevel .flood
label .flood.l -image $img
label .flood.l -image $img
pack .flood.l</lang>
pack .flood.l</syntaxhighlight>
Results in:
Results in:


Line 3,278: Line 3,278:


When the up arrow is pressed, the red square changes to blue and when the down arrow is pressed the blue square turns back to red.
When the up arrow is pressed, the red square changes to blue and when the down arrow is pressed the blue square turns back to red.
<lang ecmascript>import "graphics" for Canvas, ImageData, Color
<syntaxhighlight lang=ecmascript>import "graphics" for Canvas, ImageData, Color
import "dome" for Window
import "dome" for Window
import "input" for Keyboard
import "input" for Keyboard
Line 3,348: Line 3,348:
}
}


var Game = Bitmap.new("Bitmap - flood fill", 600)</lang>
var Game = Bitmap.new("Bitmap - flood fill", 600)</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
[[File:FloodXPL0.gif|right|Output]]
[[File:FloodXPL0.gif|right|Output]]
<lang XPL0>include c:\cxpl\codes;
<syntaxhighlight lang=XPL0>include c:\cxpl\codes;


proc Flood(X, Y, C, C0); \Fill an area of color C0 with color C
proc Flood(X, Y, C, C0); \Fill an area of color C0 with color C
Line 3,411: Line 3,411:
if ChIn(1) then []; \wait for keystroke
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text mode
SetVid(3); \restore normal text mode
]</lang>
]</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Line 3,418: Line 3,418:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl


<lang zkl>fcn flood(pixmap, x,y, repl){ // slow!
<syntaxhighlight lang=zkl>fcn flood(pixmap, x,y, repl){ // slow!
targ,h,w:=pixmap[x,y], pixmap.h,pixmap.w;
targ,h,w:=pixmap[x,y], pixmap.h,pixmap.w;
stack:=List(T(x,y));
stack:=List(T(x,y));
Line 3,431: Line 3,431:
}
}
}
}
}</lang>
}</syntaxhighlight>
<lang zkl>pixmap:=PPM(250,302,0xFF|FF|FF);
<syntaxhighlight lang=zkl>pixmap:=PPM(250,302,0xFF|FF|FF);
pixmap.circle(101,200,100,0); pixmap.circle(75,100,25,0);
pixmap.circle(101,200,100,0); pixmap.circle(75,100,25,0);


Line 3,439: Line 3,439:
flood(pixmap, 75,100, 0x00|00|F0);
flood(pixmap, 75,100, 0x00|00|F0);


pixmap.writeJPGFile("flood.zkl.jpg");</lang>
pixmap.writeJPGFile("flood.zkl.jpg");</syntaxhighlight>


{{omit from|Computer/zero Assembly|this language doesn't support video output and only has 32 bytes of RAM}}
{{omit from|Computer/zero Assembly|this language doesn't support video output and only has 32 bytes of RAM}}