Skip to content

Commit

Permalink
Add a method to create a [ColorImage]
Browse files Browse the repository at this point in the history
Add an alternative method for creating a [`ColorImage`] that accepts `Iterator` as the argument. It can be useful when `&[u8]` is not available but the iterator is.
  • Loading branch information
wangxiaochuTHU authored Nov 8, 2023
1 parent f218825 commit 72e2488
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/epaint/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ impl ColorImage {
Self { size, pixels }
}

/// Alternative method to `from_gray`.
/// Create a [`ColorImage`] from iterator over flat opaque gray data.
///
/// Panics if `size[0] * size[1] != gray_iter.len()`.
pub fn from_gray_iter<'_>(size: [usize; 2], gray_iter: impl Iterator<Item = &'_ u8> + ExactSizeIterator) -> Self {
assert_eq!(size[0] * size[1], gray_iter.len());
let pixels = gray_iter.map(|p| Color32::from_gray(*p)).collect();
Self { size, pixels }
}

/// A view of the underlying data as `&[u8]`
#[cfg(feature = "bytemuck")]
pub fn as_raw(&self) -> &[u8] {
Expand Down

0 comments on commit 72e2488

Please sign in to comment.