Any chance that you could implement using PNG instead of JPEG since in PNG you have full alpha transparency and wouldn't have to record a mask? This would also allow for smoother edges. I know that FPC supports PNG and I believe that there is support in the newer versions of Delphi (though I haven't tried them).