Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix avif odd width/height handling #2130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/codecs/avif/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R> {
let pixel_format = match self.picture.pixel_layout() {
PixelLayout::I400 => todo!(),
PixelLayout::I420 => dcp::PixelFormat::I420,
PixelLayout::I422 => dcp::PixelFormat::I422,
PixelLayout::I422 => todo!(),
PixelLayout::I444 => dcp::PixelFormat::I444,
};
let src_format = dcp::ImageFormat {
Expand All @@ -127,29 +127,52 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R> {
num_planes: 1,
};
let (width, height) = self.dimensions();

// Round up to the nearest multiple of 2
let (rounded_width, rounded_height) =
(((width + 2 - 1) / 2) * 2, ((height + 2 - 1) / 2) * 2);

let planes = &[
self.picture.plane(PlanarImageComponent::Y),
self.picture.plane(PlanarImageComponent::U),
self.picture.plane(PlanarImageComponent::V),
];
let src_buffers = planes.iter().map(AsRef::as_ref).collect::<Vec<_>>();
let mut src_buffers = planes.iter().map(|p| p.to_vec()).collect::<Vec<_>>();
let strides = &[
self.picture.stride(PlanarImageComponent::Y) as usize,
self.picture.stride(PlanarImageComponent::U) as usize,
self.picture.stride(PlanarImageComponent::V) as usize,
];
let dst_buffers = &mut [&mut buf[..]];

if rounded_height != height {
let len = src_buffers[0].len();
src_buffers[0].resize(len + strides[0], 0u8);
}

let mut tmp_buf = vec![0u8; rounded_width as usize * rounded_height as usize * 4];
let mut tmp_buf_wrapper = [tmp_buf.as_mut_slice()];

let src_buffers = src_buffers.iter().map(AsRef::as_ref).collect::<Vec<_>>();

dcp::convert_image(
width,
height,
rounded_width,
rounded_height,
&src_format,
Some(strides),
&src_buffers,
&dst_format,
None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worth seeing whether a non-None value here would enable support for odd sized images

dst_buffers,
&mut tmp_buf_wrapper,
)
.map_err(error_map)?;

for l in 0..height as usize {
let src = l * rounded_width as usize * 4
..l * rounded_width as usize * 4 + width as usize * 4;
let dst = l * width as usize * 4..l * width as usize * 4 + width as usize * 4;

buf[dst].clone_from_slice(&tmp_buf[src]);
}
} else {
let plane = self.picture.plane(PlanarImageComponent::Y);
buf.copy_from_slice(plane.as_ref());
Expand Down
Loading