Update masking method for transparent images

And also fix an calculation error that caused mask placement to be off by 1px