Ok, here you go. This works with your supplied image. You will also need to supply image 2, which will be your clean background color. You could also do this in code for a simple solid color.
does this help? It uses 3d because of loadtexture, but that allows me to read the alpha. I'm not sure about writing the alpha back out, but you could write the RGB from here with a clean background.
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
img_comp = compimage_mask("test_png_image.png", "image2.png")
cube = CreateCube()
camera = CreateCamera()
PositionEntity camera,0,0,-4
EntityTexture cube,img_comp
While Not KeyHit(1)
TurnEntity cube,.2,.2,.2
UpdateWorld
RenderWorld
Flip
Cls
Wend ; end main loop
End ;end program
Function minmax#(value#,min#,max#)
If value > max Return max
If value < min Return min
Return value
End Function
Function compimage_mask(image1$, image2$)
Local img1,img2,maskimg,r,g,b,r2,g2,b2,r3,g3,b3,m#
;load the images and do error checks
img1 = LoadTexture (image1,2)
If img1 = 0 Then RuntimeError "Image " + image1$ + " not found."
img2 = LoadTexture (image2)
If img2 = 0 Then RuntimeError "Image " + image2$ + " not found."
maskimg = LoadTexture (image1,2)
;lock the buffers for the pixel operations
LockBuffer (TextureBuffer(img1))
LockBuffer (TextureBuffer(img2))
LockBuffer (TextureBuffer(maskimg))
;loop through the pixels
For y = 0 To TextureHeight(img1) -1
For x = 0 To TextureWidth(img1) -1
;read the RGB value of image1 and split the RGB values
rgb=ReadPixelFast(x,y,TextureBuffer(img1))
;a = ((rgb Shr 24) And $ff )
r=((rgb Shr 16) And $ff )
g=((rgb Shr 8) And $ff )
b=(rgb And $ff)
;read the RGB value of image2 and split the RGB values
rgb2=ReadPixelFast(x,y,TextureBuffer(img2))
r2=(rgb2 Shr 16) And $ff
g2=(rgb2 Shr 8) And $ff
b2=rgb2 And $ff
;read the alpha value of the mask
m =((rgb Shr 24) And $ff )
m= m /256 ;convert matte value from 0-255 to 0-1
;now blend img1 and img2 with the mask value
r3 = minmax ( r2 + ((r - r2)*m) ,0,255)
g3 = minmax ( g2 + ((g -g2)*m) ,0,255)
b3 = minmax ( b2 + ((b -b2)*m) ,0,255)
rgb=(b3 Or (g3 Shl 8) Or (r3 Shl 16) Or ($ff000000))
WritePixelFast(x,y,rgb,TextureBuffer(img2))
Next
Next
UnlockBuffer (TextureBuffer(img1))
UnlockBuffer (TextureBuffer(img2))
UnlockBuffer (TextureBuffer(maskimg))
Return img2
End Function