From c96408d2ff5bcc4c2523352377afebe9cec69280 Mon Sep 17 00:00:00 2001 From: jay3332 <40323796+jay3332@users.noreply.github.com> Date: Sat, 8 Jun 2024 21:11:58 -0400 Subject: [PATCH] fix image sequence requiring ``Sized`` --- Cargo.toml | 8 ++++---- src/encode.rs | 13 +++++-------- src/encodings/gif.rs | 6 +++--- src/image.rs | 20 ++++++++++---------- src/resize.rs | 43 ++++++++++++++++--------------------------- src/sequence.rs | 2 +- 6 files changed, 39 insertions(+), 53 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 200b097e2..606641867 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,15 +13,15 @@ categories = ["encoding", "graphics", "multimedia", "visualization"] [dependencies] num-traits = "0.2" -fast_image_resize = { version = "^2.2", optional = true } +fast_image_resize = { version = "^4.0", optional = true } png = { version = "^0.17", optional = true } jpeg-decoder = { version = "^0.3", optional = true } -jpeg-encoder = { version = "^0.5", features = ["simd"], optional = true } -gif = { version = "^0.12", optional = true } +jpeg-encoder = { version = "^0.6", features = ["simd"], optional = true } +gif = { version = "^0.13", optional = true } libwebp-sys2 = { version = "^0.1", features = ["1_2", "mux", "demux"], optional = true } fontdue = { version = "^0.7", optional = true } color_quant = { version = "^1.1", optional = true } -colorgrad = { version = "^0.6", optional = true, default_features = false } +colorgrad = { version = "^0.6", optional = true, default-features = false } [features] default = ["resize", "text", "quantize", "gradient"] diff --git a/src/encode.rs b/src/encode.rs index e996d816d..171187716 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -72,7 +72,7 @@ impl<'a, C: Default, P: Pixel> HasEncoderMetadata for &'a Image

{ None } fn color_type(&self) -> ColorType { - self.data.get(0).map_or(P::COLOR_TYPE, P::color_type) + self.data.first().map_or(P::COLOR_TYPE, P::color_type) } fn bit_depth(&self) -> u8 { P::BIT_DEPTH @@ -93,7 +93,7 @@ impl<'a, C: Default, P: Pixel> HasEncoderMetadata for &'a Frame

{ Some((1, LoopCount::Infinite)) } fn color_type(&self) -> ColorType { - self.data.get(0).map_or(P::COLOR_TYPE, P::color_type) + self.data.first().map_or(P::COLOR_TYPE, P::color_type) } fn bit_depth(&self) -> u8 { P::BIT_DEPTH @@ -126,7 +126,7 @@ impl<'a, C: Default, P: Pixel> HasEncoderMetadata for &'a ImageSequence

fn color_type(&self) -> ColorType { self.first_frame().map_or(P::COLOR_TYPE, |image| { - image.data.get(0).map_or(P::COLOR_TYPE, P::color_type) + image.data.first().map_or(P::COLOR_TYPE, P::color_type) }) } @@ -591,10 +591,7 @@ pub trait FrameIterator: Iterator>> { /// /// # Errors /// * An error occured during decoding one of the frames. - fn into_sequence(self) -> crate::Result> - where - Self: Sized, - { + fn into_sequence(self: Box) -> crate::Result> { let loop_count = self.loop_count(); let frames = self.collect::>>()?; @@ -622,7 +619,7 @@ impl FrameIterator

for SingleFrameIterator

{ LoopCount::Exactly(1) } - fn into_sequence(mut self) -> crate::Result> { + fn into_sequence(mut self: Box) -> crate::Result> { let image = self.0.take().unwrap(); let frame = Frame::from_image(image); diff --git a/src/encodings/gif.rs b/src/encodings/gif.rs index 36a9a9aa9..d3f75de5a 100644 --- a/src/encodings/gif.rs +++ b/src/encodings/gif.rs @@ -99,11 +99,11 @@ impl GifEncoder { (ColorType::Rgb, 8) => rgb!(data!()), (ColorType::Rgba, 8) => rgba!(data!()), (ColorType::L, 1 | 8) => rgb!(data!(crate::Rgb)), - (ColorType::LA, 1 | 8) => rgba!(data!(crate::Rgba)), + (ColorType::LA, 1 | 8) => rgba!(data!(Rgba)), (ColorType::PaletteRgb, 8) => gif::Frame::from_palette_pixels( image.width() as u16, image.height() as u16, - &data!(crate::Rgb), + data!(crate::Rgb), image .palette() .expect("paletted image without palette?") @@ -124,7 +124,7 @@ impl GifEncoder { gif::Frame::from_palette_pixels( image.width() as u16, image.height() as u16, - &data!(crate::Rgba), + data!(Rgba), pixels .iter() .flat_map(|p| p.as_rgb().as_bytes()) diff --git a/src/image.rs b/src/image.rs index 6617a4347..a51bc618a 100644 --- a/src/image.rs +++ b/src/image.rs @@ -955,7 +955,7 @@ impl Image

{ self } - fn rotate_iterator(&self) -> impl Iterator + DoubleEndedIterator + '_ { + fn rotate_iterator(&self) -> impl DoubleEndedIterator + '_ { (0..self.width() as usize).flat_map(move |i| { (0..self.height() as usize) .map(move |j| self.data[j * self.width() as usize + i]) @@ -1055,10 +1055,10 @@ impl Image

{ self.data = crate::resize::resize( &self.data, - self.width, - self.height, - width, - height, + self.width.get(), + self.height.get(), + width.get(), + height.get(), algorithm, ); self.width = width; @@ -1522,8 +1522,8 @@ impl Banded<(Band, Band, Band)> for Image { r.map_data(|data| { data.into_iter() - .zip(g.data.into_iter()) - .zip(b.data.into_iter()) + .zip(g.data) + .zip(b.data) .map(|((L(r), L(g)), L(b))| Rgb::new(r, g, b)) .collect() }) @@ -1540,9 +1540,9 @@ impl Banded<(Band, Band, Band, Band)> for Image { r.map_data(|data| { data.into_iter() - .zip(g.data.into_iter()) - .zip(b.data.into_iter()) - .zip(a.data.into_iter()) + .zip(g.data) + .zip(b.data) + .zip(a.data) .map(|(((L(r), L(g)), L(b)), L(a))| Rgba::new(r, g, b, a)) .collect() }) diff --git a/src/resize.rs b/src/resize.rs index 84de1f7f6..1876d0217 100644 --- a/src/resize.rs +++ b/src/resize.rs @@ -1,12 +1,11 @@ //! An interfacing layer between `fast_image_resize` and this crate. use crate::{encodings::ColorType, Pixel}; - use fast_image_resize::{ - FilterType as ResizeFilterType, Image as ResizeImage, PixelType as ResizePixelType, ResizeAlg, + images::{Image as ImageOut, ImageRef}, + FilterType as ResizeFilterType, PixelType as ResizePixelType, ResizeAlg, ResizeOptions, Resizer, }; -use std::num::NonZeroU32; /// A filtering algorithm that is used to resize an image. #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -71,10 +70,10 @@ impl FilterType { pub fn resize( &self, data: &[P], - src_width: NonZeroU32, - src_height: NonZeroU32, - dst_width: NonZeroU32, - dst_height: NonZeroU32, + src_width: u32, + src_height: u32, + dst_width: u32, + dst_height: u32, ) -> Vec

{ let color_type = data[0].color_type(); let pixel_type = match P::BIT_DEPTH { @@ -101,15 +100,14 @@ impl FilterType { let buffer = data.iter().flat_map(P::as_bytes).collect::>(); // We are able to unwrap here since we validated the buffer throughout the creation of the image. - let image = ResizeImage::from_vec_u8(src_width, src_height, buffer, pixel_type).unwrap(); - let view = image.view(); + let src = ImageRef::new(src_width, src_height, &buffer, pixel_type).unwrap(); + let mut dest = ImageOut::new(dst_width, dst_height, pixel_type); - let mut dest = ResizeImage::new(dst_width, dst_height, pixel_type); - let mut dst_view = dest.view_mut(); + let mut resizer = Resizer::new(); + let options = ResizeOptions::new().resize_alg(ResizeAlg::from(*self)); - let mut resizer = Resizer::new(ResizeAlg::from(*self)); // The pixel type is the same, we can unwrap here - resizer.resize(&view, &mut dst_view).unwrap(); + resizer.resize(&src, &mut dest, Some(&options)).unwrap(); let bpp = color_type.channels() * ((P::BIT_DEPTH as usize + 7) >> 3); dest.into_vec() @@ -119,16 +117,7 @@ impl FilterType { } } -fn resize_tiled( - data: &[P], - src_width: NonZeroU32, - dst_width: NonZeroU32, - dst_height: NonZeroU32, -) -> Vec

{ - let src_width = src_width.get(); - let dst_width = dst_width.get(); - let dst_height = dst_height.get(); - +fn resize_tiled(data: &[P], src_width: u32, dst_width: u32, dst_height: u32) -> Vec

{ let chunks = data.chunks_exact(src_width as _); chunks @@ -146,10 +135,10 @@ fn resize_tiled( /// * Unsupported bit depth. pub fn resize( data: &[P], - src_width: NonZeroU32, - src_height: NonZeroU32, - dst_width: NonZeroU32, - dst_height: NonZeroU32, + src_width: u32, + src_height: u32, + dst_width: u32, + dst_height: u32, filter: FilterType, ) -> Vec

{ match filter { diff --git a/src/sequence.rs b/src/sequence.rs index 1774e071f..51c8a5996 100644 --- a/src/sequence.rs +++ b/src/sequence.rs @@ -520,7 +520,7 @@ impl ImageSequence

{ /// Returns a reference to the first frame in the image sequence, if any. #[must_use] pub fn first_frame(&self) -> Option<&Frame

> { - self.frames.get(0) + self.frames.first() } /// Returns a reference to the first frame in the image sequence. This does not check if there