예제 #1
0
        public static void CompositeClear(this IMagickImage <byte> image, MagickColor color)
        {
            const int BufferSize = 50;

            using (var pixels = image.GetPixels())
            {
                var colorChannels = pixels.Channels;
                var colorArray    = color.ToByteArray();

                var buffer = new byte[BufferSize * BufferSize * colorChannels];

                // Create a buffer to reduce native calls and keep memory footprint low.
                for (var x = 0; x < BufferSize; x++)
                {
                    for (var y = 0; y < BufferSize; y++)
                    {
                        var destination = colorChannels * (x + (y * BufferSize));

                        Array.Copy(colorArray, 0, buffer, destination, colorChannels);
                    }
                }

                for (var x = 0; x < image.Width; x += BufferSize)
                {
                    for (var y = 0; y < image.Height; y += BufferSize)
                    {
                        var w = Math.Min(BufferSize, image.Width - x);
                        var h = Math.Min(BufferSize, image.Height - y);

                        var bufferLength = w * h * colorChannels;

                        var actualBuffer = buffer.AsSpan()[..bufferLength];