Transparency and imageData:paste ?
Posted: Mon Jan 21, 2013 10:25 am
I need to paste imageData 1 onto another imageData 2, where imageData 1 has transparent parts. Using imageDate2:paste(...) it will replace the part of the image entirely. Instead, I want it to overlay the two (I want it to look the way it looks when using love.graphics.draw() over another love.graphics.draw() call).
I don't want to use Canvases.
So I coded my own function transparentPaste which uses getPixel and setPixel to combine the two:
Simplified, this is what it does for every pixel that overlaps:
However, this is extremely slow.
Any ideas for a better method without canvases?
Why are programs like the GIMP so fast when doing pretty much exactly the same thing (for example when merging two layers)...
I don't want to use Canvases.
So I coded my own function transparentPaste which uses getPixel and setPixel to combine the two:
Simplified, this is what it does for every pixel that overlaps:
Code: Select all
rDest,gDest,bDest,aDest = imgDataDest:getPixel(x,y)
rSource,gSource,bSource,aSource = imgDataSource:getPixel(x-posX,y-posY)
if aSource > 0 then
rSource,gSource,bSource,aSource = rSource/255,gSource/255,bSource/255,aSource/255
rDest,gDest,bDest,aDest = rDest/255,gDest/255,bDest/255,aDest/255
a = aSource + aDest*(1-aSource)
if a > 0 then
r = (rSource*aSource+rDest*aDest*(1-aSource))/a
g = (gSource*aSource+gDest*aDest*(1-aSource))/a
b = (bSource*aSource+bDest*aDest*(1-aSource))/a
else
r,g,b = 0,0,0
end
r,g,b,a = r*255,g*255,b*255,a*255
imgDataDest:setPixel(x, y, r, g, b, a)
end
Any ideas for a better method without canvases?
Why are programs like the GIMP so fast when doing pretty much exactly the same thing (for example when merging two layers)...