Skip to content

Reduce allocations using ImageSharp #2

@JimBobSquarePants

Description

@JimBobSquarePants

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);
                        }
                    }
                }
            });
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions