Dear All,
I'm having some issues with PNGs and alpha. Have you ever noticed that Photoshop often magically leaves RGB data behind the alpha mask. This can cause issues with the use of these PNGs in some game engines.
I've written some code as part of the image editor for Forester Pro (a tree generator that exports to X, Collada and FBX) that strips out the RGB data in masked areas in PNGs, however, I can only seem to get it to write rgb(0,0,0) in these areas. I would like to be able to write whatever background colour I would like. Here is the code:
function replaceMaskBackground(fileName$)
local colour as dword
if bitmap exist(1) then delete bitmap 1
currentinfile$=""
load bitmap fileName$, 1
set current bitmap 1
bwidth = bitmap width()
bheight = bitmap height()
lock pixels
For i=0 to bwidth
For j=0 to bheight
colour = point(i,j)
alpha = colour >> 24
if alpha=0
colour = rgb(0,0,0)
endif
colour=setAlpha(alpha,colour)
dot i, j, colour
Next j
Next i
unlock pixels
get image editorimage+1,0,0,bwidth,bheight,3 //texture flag..no stretching with alpha
set current bitmap 0
delete bitmap 1
save image "data\tempimage.png", editorimage+1
Endfunction
You can see this goes through pixel by pixel and in masked areas (alpha=0) writes the colour as rgb(0,0,0). The strange thing is if I set the colour as something different, it still gets set to rgb(0,0,0). All I can think is is my function setAlpha is at fault. I found this on the forum (shown below) and I am not to familiar with bitwise operands.
function setalpha(alpha,incolour)
colourtoreturn as dword
colourtoreturn=(incolour && %00000000111111111111111111111111) || (alpha<<%11000)
ENDFUNCTION colourtoreturn
Any suggestions are welcome.
p.s. Pretty picture of Forester Pro trees below. PNG editing would mean I can improve the quality of leaf textures even more.
GrumpyOne - the natural state of the programmer