From ca8eeb8621592b8a74cc60f6890b0274165bb535 Mon Sep 17 00:00:00 2001 From: wangxiaochuTHU <86735730+wangxiaochuTHU@users.noreply.github.com> Date: Tue, 20 Feb 2024 22:29:23 +0800 Subject: [PATCH] Add `ColorImage::from_gray_iter` (#3536) 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. --------- Co-authored-by: Emil Ernerfeldt --- crates/epaint/src/image.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/epaint/src/image.rs b/crates/epaint/src/image.rs index fa3f1e5e0c0..454969d75cb 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -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) -> Self { + let pixels: Vec<_> = gray_iter.map(Color32::from_gray).collect(); + assert_eq!(size[0] * size[1], pixels.len()); + Self { size, pixels } + } + /// A view of the underlying data as `&[u8]` #[cfg(feature = "bytemuck")] pub fn as_raw(&self) -> &[u8] {