namespace Febe.Core.Codecs.Images; /// Grey levels of RGBA pixels. public static class ImageGray { /// /// The grey level 0-255 of each pixel of an RGBA row seen over white, with the weights of ITU-R BT.601 /// (0.299 R + 0.587 G + 0.114 B): a transparent pixel is white, a half-transparent one halfway to white. /// public static void LumaOverWhite(ReadOnlySpan rgba, Span output) { if (output.Length * 4 > rgba.Length) { throw new ArgumentException("The row has fewer pixels than the output.", nameof(rgba)); } for (var x = 0; x < output.Length; x++) { var i = x * 4; int a = rgba[i + 3]; if (a == 255) { output[x] = (0.299 * rgba[i]) + (0.587 * rgba[i + 1]) + (0.114 * rgba[i + 2]); continue; } // Over white each channel becomes c·α + 255·(1 − α), with α = a / 255. var alpha = a / 255.0; var white = 255 - a; output[x] = (0.299 * ((rgba[i] * alpha) + white)) + (0.587 * ((rgba[i + 1] * alpha) + white)) + (0.114 * ((rgba[i + 2] * alpha) + white)); } } }