Add an allocation-free alternative to Image.Pixels#202
Add an allocation-free alternative to Image.Pixels#202Slko wants to merge 1 commit intoSFML:masterfrom
Conversation
DemoXinMC
left a comment
There was a problem hiding this comment.
Anything that we can use as a stop-gap until we transition to having access to Span strikes me as a positive.
|
Upon further review (yes, this PR is 2 years old), I don't like how this is implemented. Slko is correct that I would rather see this as There's also an argument to be made that these functions should be working with This can also all be simplified once we have access to |
Currently, there are two ways to access the pixels of an
Image.The first one is
Image.GetPixel(), which does a call to a native function on each invocation, which is pretty bad for performance if you do it millions of times per second.The other one is
Image.PixelsIt allocates a new pixel buffer on each getter access. Property accessors should never do anything heavy inside of them (according to common sense and the official design guidelines), you can easily shoot yourself in the foot by writing a naïve code like this:
Which seems faster than calling
Image.GetPixel(), but is actuallyO(W * H)allocations ofO(W * H)bytes!This pull request adds a new allocation-free and more performant alternative, which copies the internal pixel buffer to a user-provided byte array, and is a safer alternative to #138.
And I would also recommend you to deprecate
Image.Pixelsfor the reasons stated above.