Skip to content

Commit 4dbc4ca

Browse files
committed
Add Buffer::pixels
To make it explicit when you're accessing the buffer data as pixels.
1 parent 8ea991e commit 4dbc4ca

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- Added `Buffer::pixels()` for accessing the buffer's pixel data.
34
- Added `Buffer::pixel_rows()` for iterating over rows of the buffer data.
45
- Added `Buffer::pixels_iter()` for iterating over each pixel with its associated `x`/`y` coordinate.
56
- **Breaking:** Removed generic type parameters `D` and `W` from `Buffer<'_>` struct.

src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,24 @@ impl Buffer<'_> {
272272
}
273273
}
274274

275-
/// Pixel helper accessors.
275+
/// Helper accessors for viewing the buffer's data as RGBA pixel data.
276276
impl Buffer<'_> {
277+
/// Access the buffer's pixels.
278+
///
279+
/// The size of the returned slice is `buffer.width() * buffer.height()`.
280+
///
281+
/// # Examples
282+
///
283+
/// Clear the buffer with red.
284+
///
285+
/// ```no_run
286+
/// # let buffer: softbuffer::Buffer<'_> = unimplemented!();
287+
/// buffer.pixels().fill(0x00ff0000);
288+
/// ```
289+
pub fn pixels(&mut self) -> &mut [u32] {
290+
self.buffer_impl.pixels_mut()
291+
}
292+
277293
/// Iterate over each row of pixels.
278294
///
279295
/// Each slice returned from the iterator has a length of `buffer.width()`.
@@ -327,7 +343,7 @@ impl Buffer<'_> {
327343
&mut self,
328344
) -> impl DoubleEndedIterator<Item = &mut [u32]> + ExactSizeIterator {
329345
let width = self.width().get() as usize;
330-
let pixels = self.buffer_impl.pixels_mut();
346+
let pixels = self.pixels();
331347
assert_eq!(pixels.len() % width, 0, "buffer must be multiple of width");
332348
// NOTE: This won't panic because `width` is `NonZeroU32`
333349
pixels.chunks_mut(width)

0 commit comments

Comments
 (0)