diff options
author | 2024-05-01 06:57:38 +0200 | |
---|---|---|
committer | 2024-05-01 06:57:38 +0200 | |
commit | a11784f9edfd5668fa998b2a7d2a50971a4cdac5 (patch) | |
tree | 0ec25d8bcbf4bff4bf7e9ee9c9e462ef2f47af3a /graphics/src | |
parent | 89892f1760d4ec67f458d67ae722c3f06d524a1b (diff) | |
parent | 01b014c19fa2a3c200fb2077e31822f525f729cf (diff) | |
download | iced-a11784f9edfd5668fa998b2a7d2a50971a4cdac5.tar.gz iced-a11784f9edfd5668fa998b2a7d2a50971a4cdac5.tar.bz2 iced-a11784f9edfd5668fa998b2a7d2a50971a4cdac5.zip |
Merge pull request #2356 from Bajix/feature/bytes
Utilize bytes::Bytes for images
Diffstat (limited to 'graphics/src')
-rw-r--r-- | graphics/src/image.rs | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/graphics/src/image.rs b/graphics/src/image.rs index c6135e9e..04c45057 100644 --- a/graphics/src/image.rs +++ b/graphics/src/image.rs @@ -50,7 +50,8 @@ impl Image { /// [`Handle`]: image::Handle pub fn load( handle: &image::Handle, -) -> ::image::ImageResult<::image::DynamicImage> { +) -> ::image::ImageResult<::image::ImageBuffer<::image::Rgba<u8>, image::Bytes>> +{ use bitflags::bitflags; bitflags! { @@ -100,8 +101,8 @@ pub fn load( } } - match handle.data() { - image::Data::Path(path) => { + let (width, height, pixels) = match handle { + image::Handle::Path(_, path) => { let image = ::image::open(path)?; let operation = std::fs::File::open(path) @@ -110,33 +111,44 @@ pub fn load( .and_then(|mut reader| Operation::from_exif(&mut reader).ok()) .unwrap_or_else(Operation::empty); - Ok(operation.perform(image)) + let rgba = operation.perform(image).into_rgba8(); + + ( + rgba.width(), + rgba.height(), + image::Bytes::from(rgba.into_raw()), + ) } - image::Data::Bytes(bytes) => { + image::Handle::Bytes(_, bytes) => { let image = ::image::load_from_memory(bytes)?; let operation = Operation::from_exif(&mut std::io::Cursor::new(bytes)) .ok() .unwrap_or_else(Operation::empty); - Ok(operation.perform(image)) + let rgba = operation.perform(image).into_rgba8(); + + ( + rgba.width(), + rgba.height(), + image::Bytes::from(rgba.into_raw()), + ) } - image::Data::Rgba { + image::Handle::Rgba { width, height, pixels, - } => { - if let Some(image) = - ::image::ImageBuffer::from_vec(*width, *height, pixels.to_vec()) - { - Ok(::image::DynamicImage::ImageRgba8(image)) - } else { - Err(::image::error::ImageError::Limits( - ::image::error::LimitError::from_kind( - ::image::error::LimitErrorKind::DimensionError, - ), - )) - } - } + .. + } => (*width, *height, pixels.clone()), + }; + + if let Some(image) = ::image::ImageBuffer::from_raw(width, height, pixels) { + Ok(image) + } else { + Err(::image::error::ImageError::Limits( + ::image::error::LimitError::from_kind( + ::image::error::LimitErrorKind::DimensionError, + ), + )) } } |