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.

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review you PR, but my time is limited!
-->

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
wangxiaochuTHU 2024-02-20 22:29:23 +08:00 committed by GitHub
parent 4fc0c49a6b
commit ca8eeb8621
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 0 deletions

View File

@ -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>) -> 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] {