From 72e2488d4f3acf9891771de8e3492d4d705a0e04 Mon Sep 17 00:00:00 2001 From: wangxiaochuTHU <86735730+wangxiaochuTHU@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:41:17 +0800 Subject: [PATCH 1/4] Add a method to create a [`ColorImage`] 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. --- 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 0a40fe4a0c3..d5f5bfcae11 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 + 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] { From 8830efcaabf7557de4b177dd4e0c50c734f67009 Mon Sep 17 00:00:00 2001 From: wangxiaochuTHU <86735730+wangxiaochuTHU@users.noreply.github.com> Date: Sat, 11 Nov 2023 20:22:26 +0800 Subject: [PATCH 2/4] Update crates/epaint/src/image.rs Co-authored-by: Emil Ernerfeldt --- crates/epaint/src/image.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/epaint/src/image.rs b/crates/epaint/src/image.rs index d5f5bfcae11..e32f64d5899 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -124,9 +124,9 @@ impl ColorImage { /// 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 + ExactSizeIterator) -> Self { - assert_eq!(size[0] * size[1], gray_iter.len()); - let pixels = gray_iter.map(|p| Color32::from_gray(*p)).collect(); + pub fn from_gray_iter(size: [usize; 2], gray_iter: impl Iterator) -> Self { + let pixels = gray_iter.map(Color32::from_gray).collect(); + assert_eq!(size[0] * size[1], pixels.len()); Self { size, pixels } } From e49c7977216359be32f0fa8605151ff4853a3064 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 16 Nov 2023 11:29:48 +0100 Subject: [PATCH 3/4] fix compliation --- crates/epaint/src/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/epaint/src/image.rs b/crates/epaint/src/image.rs index e32f64d5899..54eb3dc6bd0 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -125,7 +125,7 @@ impl ColorImage { /// /// Panics if `size[0] * size[1] != gray_iter.len()`. pub fn from_gray_iter(size: [usize; 2], gray_iter: impl Iterator) -> Self { - let pixels = gray_iter.map(Color32::from_gray).collect(); + let pixels: Vec<_> = gray_iter.map(Color32::from_gray).collect(); assert_eq!(size[0] * size[1], pixels.len()); Self { size, pixels } } From d1a062994eb27e4519ee5b02d8d906090f7ef785 Mon Sep 17 00:00:00 2001 From: wangxiaochuTHU Date: Mon, 19 Feb 2024 12:31:37 +0800 Subject: [PATCH 4/4] Fix `fmt` --- crates/epaint/src/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/epaint/src/image.rs b/crates/epaint/src/image.rs index 54eb3dc6bd0..a31999edff9 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -122,7 +122,7 @@ impl ColorImage { /// 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();