Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations using ImageSharp #2

Open
JimBobSquarePants opened this issue May 25, 2018 · 0 comments
Open

Reduce allocations using ImageSharp #2

JimBobSquarePants opened this issue May 25, 2018 · 0 comments

Comments

@JimBobSquarePants
Copy link

Image<TPixel> comes with an indexer so you do not need to create a new array. You are also not using tha alpha component of the Rgba32 struct so you might as well save another 25% memory by ignoring it and using the Rgb24 struct instead.

https://github.com/MaitreDede/RaspberryPi.Libs/blob/758a849be16ade461ef1e4be8218aaf78627ac6b/RaspberryPi.LibLedMatrix.ImageSharp/LedCanvasExtensions.cs#L20

Could be:

        public static void UpdateCanvasAsImageSharp(this LedMatrix matrix, Action<IImageProcessingContext<Rgb24>> method)
        {
            matrix.UpdateCanvas(canvas =>
            {
                using (Image<Rgb24> img = new Image<Rgb24>(canvas.Width, canvas.Height))
                {
                    img.Mutate(method);
                    for (int x = 0; x < canvas.Width; x++)
                    {
                        for (int y = 0; y < canvas.Height; y++)
                        {
                            Rgb24 rgb = img[x,y];
                            canvas.SetPixel(x, y, rgb.R, rgb.G, rgb.B);
                        }
                    }
                }
            });
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant