Add an allocation-free alternative to Image.Pixels #202
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.Pixels
It 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.Pixels
for the reasons stated above.