Recently, I have been trying to think of a way of using the RGBSurface and pictures to FloodFill a fully transparent area. The RGBSurface only supports a depth of 32 and I was wondering if there was an easy way to get FloodFill to fill transparent areas.
I thought about FloodFilling the alpha mask but I cannot think how I could avoid pixel-by-pixel processing through Xojo code to get it to fill the colour on the actual image rather than the mask.
Make a new picture the same size as the original and filled with the color you want. Copy the mask from the original and invert it (see transform for more info). Set the mask of the new image to the inverted mask. Then draw the new image on top of the old one.
I have attempted this but it does not seem to make a difference to the picture.
Sub Fill(x as integer, y as integer, col as Color)
//if pixel is partially or fully opaque
if ImageCanvas1.Image.Mask.RGBSurface.Pixel(x, y) <> &cFFFFFF then
ImageCanvas1.Image.RGBSurface.FloodFill(x, y, col)
else
//fill image with desired colour
dim p1 as new picture(ImageCanvas1.Image.Width, ImageCanvas1.Image.Height)
p1.Graphics.ForeColor = col
p1.Graphics.FillRect(0, 0, p1.width, p1.height)
//apply inverted mask
dim p2 as picture = ImageCanvas1.Image.Mask.ClonePicture
ImagePlayEffectsLibrary.Invert(p2)
p1.ApplyMask(p2)
//draw new image over original
ImageCanvas1.Image.Graphics.DrawPicture(p1, 0, 0)
end if
Invalidate(false)
End Sub
' draw the unmasked original picture on the result picture
dim ResultPic as new Picture(320,240,32)
ResultPic.Graphics.DrawPicture origPic,0,0
' build the masked floodfill picture
dim FFPic as new Picture(320,240,32)
FFPic.Graphics.DrawPicture FloodfillMask,0,0
' and on the mask of the floodfill picture
FFPic.Mask.Graphics.DrawPicture FloodfillMask,0,0
' floodfill on this newly build floodfill picture
FFPic.RGBSurface.FloodFill 160,120, &cFF0000
' invert mask of floodfilled pic
Dim map(255) As Integer
For i As Integer = 0 To 255
map(i) = 255 - i
Next
FFpic.Mask.RGBSurface.Transform(map)
' draw floodfilled picture on result picture
ResultPic.Graphics.DrawPicture FFPic,0,0
[quote=114956:@Alain Bailleul]Just follow the steps Greg pointed out:
[code]
’ draw the unmasked original picture on the result picture
dim ResultPic as new Picture(320,240,32)
ResultPic.Graphics.DrawPicture origPic,0,0
’ build the masked floodfill picture
dim FFPic as new Picture(320,240,32)
FFPic.Graphics.DrawPicture FloodfillMask,0,0
’ and on the mask of the floodfill picture
FFPic.Mask.Graphics.DrawPicture FloodfillMask,0,0
’ floodfill on this newly build floodfill picture
FFPic.RGBSurface.FloodFill 160,120, &cFF0000
’ invert mask of floodfilled pic
Dim map(255) As Integer
For i As Integer = 0 To 255
map(i) = 255 - i
Next
FFpic.Mask.RGBSurface.Transform(map)
’ draw floodfilled picture on result picture
ResultPic.Graphics.DrawPicture FFPic,0,0
[/code][/quote]
Thanks. What does Floodfillmask represent? The mask for the original image?
Here is the code I got. I tried all sorts to get it to work:
Not exactly what I wrote but I changed it so that the method does not require any modules installed into your project. I tested this code anyway and all it seemed to do was invert the image.