Skip to content

How to use a greyscale image as an alpha mask for a fill operation? #2872

Answered by jez9999
jez9999 asked this question in Q&A
Discussion options

You must be logged in to vote

I ended up doing it with the following, but I think an ApplyMask extension method should be added so it can be done in Mutate without having to ProcessPixelRows:

// Apply alpha mask
imgPreviewSquare.ProcessPixelRows(imgMask, (imgAccessor, maskAccessor) => {
	for (int y = 0; y < imgAccessor.Height; y++) {
		var rowImg = imgAccessor.GetRowSpan(y);
		var rowMask = maskAccessor.GetRowSpan(y);
		for (int x = 0; x < imgAccessor.Width; x++) {
			var alpha = rowMask[x].R == rowMask[x].G && rowMask[x].G == rowMask[x].B ? rowMask[x].R : throw new Exception("Image mask is not fully grayscale!");
			rowImg[x].A = alpha;
		}
	}
});

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jez9999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant