Skip to content

Commit

Permalink
fix image sequence requiring Sized
Browse files Browse the repository at this point in the history
  • Loading branch information
jay3332 committed Jun 9, 2024
1 parent 443fae9 commit c96408d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 53 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
13 changes: 5 additions & 8 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a, C: Default, P: Pixel> HasEncoderMetadata<C, P> for &'a Image<P> {
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
Expand All @@ -93,7 +93,7 @@ impl<'a, C: Default, P: Pixel> HasEncoderMetadata<C, P> for &'a Frame<P> {
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
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'a, C: Default, P: Pixel> HasEncoderMetadata<C, P> for &'a ImageSequence<P>

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)
})
}

Expand Down Expand Up @@ -591,10 +591,7 @@ pub trait FrameIterator<P: Pixel>: Iterator<Item = crate::Result<Frame<P>>> {
///
/// # Errors
/// * An error occured during decoding one of the frames.
fn into_sequence(self) -> crate::Result<ImageSequence<P>>
where
Self: Sized,
{
fn into_sequence(self: Box<Self>) -> crate::Result<ImageSequence<P>> {
let loop_count = self.loop_count();
let frames = self.collect::<crate::Result<Vec<_>>>()?;

Expand Down Expand Up @@ -622,7 +619,7 @@ impl<P: Pixel> FrameIterator<P> for SingleFrameIterator<P> {
LoopCount::Exactly(1)
}

fn into_sequence(mut self) -> crate::Result<ImageSequence<P>> {
fn into_sequence(mut self: Box<Self>) -> crate::Result<ImageSequence<P>> {
let image = self.0.take().unwrap();
let frame = Frame::from_image(image);

Expand Down
6 changes: 3 additions & 3 deletions src/encodings/gif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ impl<P: Pixel, W: Write> GifEncoder<P, W> {
(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?")
Expand All @@ -124,7 +124,7 @@ impl<P: Pixel, W: Write> GifEncoder<P, W> {
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())
Expand Down
20 changes: 10 additions & 10 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ impl<P: Pixel> Image<P> {
self
}

fn rotate_iterator(&self) -> impl Iterator<Item = P> + DoubleEndedIterator + '_ {
fn rotate_iterator(&self) -> impl DoubleEndedIterator<Item = P> + '_ {
(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])
Expand Down Expand Up @@ -1055,10 +1055,10 @@ impl<P: Pixel> Image<P> {

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;
Expand Down Expand Up @@ -1522,8 +1522,8 @@ impl Banded<(Band, Band, Band)> for Image<Rgb> {

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()
})
Expand All @@ -1540,9 +1540,9 @@ impl Banded<(Band, Band, Band, Band)> for Image<Rgba> {

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()
})
Expand Down
43 changes: 16 additions & 27 deletions src/resize.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -71,10 +70,10 @@ impl FilterType {
pub fn resize<P: Pixel>(
&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<P> {
let color_type = data[0].color_type();
let pixel_type = match P::BIT_DEPTH {
Expand All @@ -101,15 +100,14 @@ impl FilterType {

let buffer = data.iter().flat_map(P::as_bytes).collect::<Vec<_>>();
// 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()
Expand All @@ -119,16 +117,7 @@ impl FilterType {
}
}

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

fn resize_tiled<P: Pixel>(data: &[P], src_width: u32, dst_width: u32, dst_height: u32) -> Vec<P> {
let chunks = data.chunks_exact(src_width as _);

chunks
Expand All @@ -146,10 +135,10 @@ fn resize_tiled<P: Pixel>(
/// * Unsupported bit depth.
pub fn resize<P: Pixel>(
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<P> {
match filter {
Expand Down
2 changes: 1 addition & 1 deletion src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl<P: Pixel> ImageSequence<P> {
/// Returns a reference to the first frame in the image sequence, if any.
#[must_use]
pub fn first_frame(&self) -> Option<&Frame<P>> {
self.frames.get(0)
self.frames.first()
}

/// Returns a reference to the first frame in the image sequence. This does not check if there
Expand Down

0 comments on commit c96408d

Please sign in to comment.