A version that checks for the global boundaries of the image.
rem
rem AGK Application
rem
// Known issues
// The point where you start filling should be in a closed shape otherwise there is a crash.
// I try to fix this, but it works for my purposes.
// There is an issue with the color used to fill(changed); I don´t get always the color I wish
setvirtualresolution(480,480)
global memblock as integer
global width as integer
global height as integer
global size as integer
global image as integer
global Nimage as integer
type point
x as integer
y as integer
endtype
dim stack[100000] as point
global sp as integer
sp = -1
// image = LoadImage("pijl.png")
image = createimage2()
memblock = CreateMemblockFromImage(image)
width = GetMemblockInt(memblock, 0)
height = GetMemblockInt(memblock, 4)
size = GetMemblockSize(memblock)
clr= GetPixel(memblock ,width ,60,45)
//floodfill(memblock,width,60,45,clr,intfromcolor(255,0,125,255))
floodfill(memblock,width,10,10,clr,intfromcolor(255,0,125,255))
// create a new image and sprite from the inverted memblock (the source image is unaffected)
NImage = CreateImageFromMemblock(memblock)
sprite = CreateSprite(NImage)
SetSpriteSize(sprite, 100, -1)
setspritedepth(sprite,100)
do
Sync()
loop
function GetPixel(memblock as integer ,width as integer,x as integer,y as integer)
offsetPix=12+((x+(y*width))*4)
color = GetMemblockInt(memblock,offsetPix)
endfunction color
function intFromColor(r as integer, g as integer, b as integer, a as integer)
res as integer
res = ((r * (256^3)) + (g *(256^2)) + (b* 256) + a)
endfunction res
function setPixel(memblock as integer,width as integer,x as integer,y as integer,color as integer)
offsetPix=12+((x+(y*width))*4)
setMemblockInt(memblock,offsetPix,color )
endfunction
function comparepixel(memblock as integer,width as integer, x as integer,y as integer,color as integer )
offsetPix=12+((x+(y*width))*4)
if (getMemblockInt(memblock,offsetPix) = color)
exitfunction 1
else exitfunction 0
endif
endfunction 0
// mb = the memblock; width= of a line: xx,yy = the point where you start filling; chosen = the color of the point where you start filling; changed = the color fill with
function floodfill( mb as integer, width as integer,xx as integer, yy as integer, chosen as integer,changed as integer )
res as integer
tx as integer
ty as integer
inc sp;
stack[sp].x =xx : stack[sp].y=yy
while (sp >= 0 and sp < 100000)
tx= stack[sp].x : ty= stack[sp].y
dec sp
res= comparepixel(mb,width,tx,ty,chosen)
if (res= 1)
setPixel(mb,width, tx,ty ,changed)
if ( ty + 1 < height)
inc sp:stack[sp].x =tx: stack[sp].y =ty+1
endif
if ( ty - 1 >= 0)
inc sp:stack[sp].x =tx: stack[sp].y =ty-1
endif
if ( tx + 1 < width)
inc sp:stack[sp].x =tx+1: stack[sp].y =ty
endif
if ( tx -1 >= 0)
inc sp:stack[sp].x =tx-1: stack[sp].y =ty
endif
endif
endwhile
endfunction
function createimage2()
swap()
DrawLine( 10, 50, 50, 10, 255, 255, 255 )
DrawLine( 50, 10, 60, 30, 255, 255, 255 )
DrawLine( 60, 30, 70, 10, 255, 255, 255 )
DrawLine( 70,10, 70, 50, 255, 255, 255 )
DrawLine( 70, 50, 10,50, 255, 255, 255 )
render()
lineimage = getimage(0,0,100,100)
endfunction lineimage