Add Buffer::pixel_rows and Buffer::pixels_iter iterators#312
Merged
Conversation
MarijnS95
approved these changes
Jan 19, 2026
Comment on lines
+329
to
+333
| let width = self.width().get() as usize; | ||
| let pixels = self.buffer_impl.pixels_mut(); | ||
| assert_eq!(pixels.len() % width, 0, "buffer must be multiple of width"); | ||
| // NOTE: This won't panic because `width` is `NonZeroU32` | ||
| pixels.chunks_mut(width) |
Member
There was a problem hiding this comment.
I hope that we can soon drive this from the buffer instead, taking a stride into account.
README.md
Outdated
| for index in 0..(buffer.width().get() * buffer.height().get()) { | ||
| let y = index / buffer.width().get(); | ||
| let x = index % buffer.width().get(); | ||
| for (x, y, pixel) in buffer.iter_pixels() { |
Member
There was a problem hiding this comment.
Great way to get rid of the NonZero::get() API instead.
Member
Author
There was a problem hiding this comment.
It's not really gone though, people will wanna use it in larger examples, see e.g. examples/rectangle.rs (if you want to calculate distance a given pixel is from the right or bottom edge).
8afefda to
cc2d5e7
Compare
Buffer::pixel_rows and Buffer::iter_pixels iteratorsBuffer::pixel_rows and Buffer::pixels_iter iterators
cc2d5e7 to
540b3b8
Compare
Member
Author
|
I renamed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add:
pixel_rowsis a prerequisite for #291, as using a buffer with stride will be quite cumbersome without them. It currently chunks on the width, but should chunk on the stride once we add that.pixels_iteris more of a useful shorthand, it's not as critical, but still be nice to have.I decided to return opaque
impl Iterator<...>, we can add an explicitBufferRows<'_>iterator type later if desirable.