Skip to content

Conversation

@madsmtm
Copy link
Member

@madsmtm madsmtm commented Jan 19, 2026

Add:

impl Buffer<'_> {
    pub fn pixel_rows(&mut self) -> impl Iterator<Item = &mut [u32]> + DoubleEndedIterator + ExactSizeIterator;
    pub fn pixels_iter(&mut self) -> impl Iterator<Item = (u32, u32, &mut u32)> + DoubleEndedIterator;
}

pixel_rows is 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_iter is 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 explicit BufferRows<'_> iterator type later if desirable.

@madsmtm madsmtm added the enhancement New feature or request label 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that we can soon drive this from the buffer instead, taking a stride into account.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it ;)

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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great way to get rid of the NonZero::get() API instead.

Copy link
Member Author

@madsmtm madsmtm Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@madsmtm madsmtm force-pushed the madsmtm/buffer-helpers branch 2 times, most recently from 8afefda to cc2d5e7 Compare January 19, 2026 14:44
@madsmtm madsmtm changed the title Add Buffer::pixel_rows and Buffer::iter_pixels iterators Add Buffer::pixel_rows and Buffer::pixels_iter iterators Jan 19, 2026
@madsmtm madsmtm force-pushed the madsmtm/buffer-helpers branch from cc2d5e7 to 540b3b8 Compare January 19, 2026 14:47
@madsmtm
Copy link
Member Author

madsmtm commented Jan 19, 2026

I renamed iter_pixels -> pixels_iter, I felt that was more in line with how I've seen iterator methods named elsewhere (e.g. par_iter).

@madsmtm madsmtm merged commit 8ea991e into master Jan 19, 2026
40 checks passed
@madsmtm madsmtm deleted the madsmtm/buffer-helpers branch January 19, 2026 16:28
@madsmtm madsmtm added this to the Softbuffer v0.5 milestone Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

3 participants