From 5e44c13b481d8bdcadde977f04ed3cd078975359 Mon Sep 17 00:00:00 2001 From: Paul Hazen Date: Wed, 19 Oct 2022 06:01:53 -0700 Subject: [PATCH] Allows creating a `ColorImage` struct without an alpha channel (#2167) * Added functionality to image.rs that allows for creating an image from rgb instead of just rgba * remove "unmultiplied" * remove "unmultiplied" * rgba -> rgb Co-authored-by: Mingun Co-authored-by: Emil Ernerfeldt Co-authored-by: Mingun --- crates/epaint/src/image.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/epaint/src/image.rs b/crates/epaint/src/image.rs index 2283411c..c7a05c55 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -101,6 +101,21 @@ impl ColorImage { Self { size, pixels } } + /// Create a [`ColorImage`] from flat RGB data. + /// + /// This is what you want to use after having loaded an image file (and if + /// you are ignoring the alpha channel - considering it to always be 0xff) + /// + /// Panics if `size[0] * size[1] * 3 != rgb.len()`. + pub fn from_rgb(size: [usize; 2], rgb: &[u8]) -> Self { + assert_eq!(size[0] * size[1] * 3, rgb.len()); + let pixels = rgb + .chunks_exact(3) + .map(|p| Color32::from_rgb(p[0], p[1], p[2])) + .collect(); + Self { size, pixels } + } + /// An example color image, useful for tests. pub fn example() -> Self { let width = 128;