Code archives/Graphics/Flood fill

This code has been declared by its author to be Public Domain code.

Download source code

Flood fill by Matt Merkulov
(Posted 20 years ago)
From Blitz et cetera article
Image used:
;Simple flood fill (like QB Paint command) algorhytm example by Matt Merkulov

Type paint
 Field x, y, dir, move
End Type

Type paint2
 Field x1, x2, y
End Type

Const rad = 48 ^ 2

Graphics 640, 480

Global ib, ib2, p, r, g, b

i = LoadImage("image3.jpg")
ib = ImageBuffer(i)
DrawBlock i, 0, 0
LockBuffer ib

i2 = CreateImage(640, 480)
ib2 = ImageBuffer(i2)
Global zero = ReadPixel(0, 0, ib2)
LockBuffer ib2

For x = 0 To 639
 For y = 0 To 479
 p = ReadPixelFast(x, y, ib)
 r = (p Shr 16) And 255
 g = (p Shr 8) And 255
 b = p And 255
 paint x, y
 Next
Next
WaitKey

Function checkpixel(x, y)
If x < 0 Or x >= 640 Or y < 0 Or y >= 480 Then Return
If ReadPixelFast(x, y, ib2) <> zero Then Return
;Calculating difference of colors
pp = ReadPixelFast(x, y, ib)
r2 = r - ((pp Shr 16) And 255)
g2 = g - ((pp Shr 8) And 255)
b2 = b - (pp And 255)
rr = r2 * r2 + g2 * g2 + b2 * b2
If rr > rad Then Return
p1.paint = New paint
p1\x = x
p1\y = y
WritePixel x, y, p
;Marking pixel in auxillary buffer
WritePixelFast x, y,  - 1, ib2
End Function

Function paint(x, y)
checkpixel x, y
Repeat
 p1.paint = First paint
 If p1 = Null Then Exit
 x = p1\x
 y = p1\y
 ;Checking adjacent pixels
 checkpixel x + 1, y
 checkpixel x - 1, y
 checkpixel x, y - 1
 checkpixel x, y + 1
 Delete p1
Forever
End Function

Comments

Mortifer Nex
(Posted 20 years ago)
Illegal Memory Adress...

Matt Merkulov
(Posted 20 years ago)
You are probably forgot to upload/create "image3.jpg" and put it in the same folder as program's.

Mortifer Nex
(Posted 19 years ago)
Whoops...

Adam Novagen
(Posted 19 years ago)
Well, I created an image, and it did display properly; however, the program crashed with a "Memory access violation" error as soon as the checkpixel() function hit the line "pp = ReadPixelFast(x, y, ib)".

big10p
(Posted 19 years ago)
ReadPixelFast doesn't work on my PC unless I explicitly specify the buffer.

[edit]nm - I see the buffer IS being specified :)

Matt Merkulov
(Posted 19 years ago)
ADAM: Is your image's resolution 640x480?

Code Archives Forum