From cb2050aea4e46c33dba479c4ac1d08af90d1428c Mon Sep 17 00:00:00 2001 From: Varphone Wong Date: Wed, 28 Feb 2024 09:58:03 +0800 Subject: [PATCH] `epaint`: Added `ColorImage::from_rgba_vec` constructor This is typically used when there's an existing image buffer and memory copying is to be avoided. --- 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 f653be2e7..6ae93f087 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -111,6 +111,16 @@ impl ColorImage { Self { size, pixels } } + /// Creates a [`ColorImage`] with an existing image buffer. + /// + /// This is typically used when you want to avoid unnecessary memory copying by reusing an existing image buffer. + /// + /// Panics if `size[0] * size[1] != pixels.len()`. + pub fn from_rgba_vec(size: [usize; 2], pixels: Vec) -> Self { + assert_eq!(size[0] * size[1], pixels.len()); + Self { size, pixels } + } + /// Create a [`ColorImage`] from flat opaque gray data. /// /// Panics if `size[0] * size[1] != gray.len()`.