Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
11 changes: 6 additions & 5 deletions src/codecs/avif/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ fn reshape_plane(source: &[u8], stride: usize, width: usize, height: usize) -> V
.chunks_exact_mut(width)
.zip(source.chunks_exact(stride))
{
for (dst, src) in shaped_row.iter_mut().zip(src_row.chunks_exact(2)) {
*dst = u16::from_ne_bytes([src[0], src[1]]);
for (dst, src) in shaped_row.iter_mut().zip(src_row.as_chunks::<2>().0) {
*dst = u16::from_ne_bytes(*src);
}
}
target_plane
Expand Down Expand Up @@ -500,7 +500,7 @@ impl<R: Read> ImageDecoder for AvifDecoder<R> {
buf.chunks_exact_mut(width as usize * 4),
plane.as_ref().chunks_exact(stride),
) {
for (rgba, a_src) in buf.chunks_exact_mut(4).zip(slice) {
for (rgba, a_src) in buf.as_chunks_mut::<4>().0.iter_mut().zip(slice) {
rgba[3] = *a_src;
}
}
Expand All @@ -514,7 +514,8 @@ impl<R: Read> ImageDecoder for AvifDecoder<R> {
// If buffer from Decoder is unaligned
let mut aligned_store = vec![0u16; buf.len() / 2];
self.process_16bit_picture(&mut aligned_store, yuv_range, matrix_strategy)?;
for (dst, src) in buf.chunks_exact_mut(2).zip(aligned_store.iter()) {
let buf_chunks = buf.as_chunks_mut::<2>().0.iter_mut();
for (dst, src) in buf_chunks.zip(aligned_store.iter()) {
let bytes = src.to_ne_bytes();
dst[0] = bytes[0];
dst[1] = bytes[1];
Expand Down Expand Up @@ -686,7 +687,7 @@ impl<R: Read> AvifDecoder<R> {
target.chunks_exact_mut(width as usize * 4),
a_plane_view.data.as_ref().chunks_exact(a_plane_view.stride),
) {
for (rgba, a_src) in buf.chunks_exact_mut(4).zip(slice) {
for (rgba, a_src) in buf.as_chunks_mut::<4>().0.iter_mut().zip(slice) {
rgba[3] = *a_src;
}
}
Expand Down
35 changes: 13 additions & 22 deletions src/codecs/avif/ycgco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ fn process_halved_chroma_row_cgco<

let bias_y = range.bias_y as i32;
let bias_uv = range.bias_uv as i32;
let y_iter = y_plane.chunks_exact(2);
let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
Copy link
Contributor

@awxkee awxkee Feb 12, 2026

Choose a reason for hiding this comment

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

Interesting fact: Rust with LLVM was always bad in autovectorizing YUV chroma subsampling.
Now, with as_chunks[_mut] it can at least autovectorize YUV encoding with chroma subsampling but not decoding.

let (y_iter, y_left) = y_plane.as_chunks::<2>();
let mut rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);

let scale_coef = ((max_value as f32 / range.range_y as f32) * (1 << PRECISION) as f32) as i32;

for (((y_src, &u_src), &v_src), rgb_dst) in y_iter.zip(u_plane).zip(v_plane).zip(rgb_chunks) {
for (((y_src, &u_src), &v_src), rgb_dst) in
y_iter.iter().zip(u_plane).zip(v_plane).zip(&mut rgb_chunks)
{
let y_value0: i32 = y_src[0].as_() - bias_y;
let cg_value: i32 = u_src.as_() - bias_uv;
let co_value: i32 = v_src.as_() - bias_uv;
Expand Down Expand Up @@ -154,11 +156,11 @@ fn process_halved_chroma_row_cgco<

// Process remainder if width is odd.
if image.width & 1 != 0 {
let y_left = y_plane.chunks_exact(2).remainder();
let rgb_chunks = rgba
.chunks_exact_mut(CHANNELS * 2)
let rgb_chunks = rgb_chunks
.into_remainder()
.chunks_exact_mut(CHANNELS);
.as_chunks_mut::<CHANNELS>()
.0
.iter_mut();
let u_iter = u_plane.iter().rev();
let v_iter = v_plane.iter().rev();

Expand All @@ -170,11 +172,7 @@ fn process_halved_chroma_row_cgco<
let co_value = v_src.as_() - bias_uv;

ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
rgb_dst.try_into().unwrap(),
y_value,
cg_value,
co_value,
scale_coef,
rgb_dst, y_value, cg_value, co_value, scale_coef,
);
}
}
Expand Down Expand Up @@ -249,7 +247,7 @@ where

// All branches on generic const will be optimized out.
for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();
match yuv_range {
YuvIntensityRange::Tv => {
let y_coef =
Expand All @@ -262,11 +260,7 @@ where
let co_value = v_src.as_() - bias_uv;

ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
rgb_dst.try_into().unwrap(),
y_value,
cg_value,
co_value,
y_coef,
rgb_dst, y_value, cg_value, co_value, y_coef,
);
}
}
Expand All @@ -279,10 +273,7 @@ where
let co_value = v_src.as_() - bias_uv;

ycgco_execute_full::<V, PRECISION, CHANNELS, BIT_DEPTH>(
rgb_dst.try_into().unwrap(),
y_value,
cg_value,
co_value,
rgb_dst, y_value, cg_value, co_value,
);
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/codecs/avif/yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ where

// All branches on generic const will be optimized out.
for (y_src, rgb) in y_iter.zip(rgb_iter) {
let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();

for (y_src, rgb_dst) in y_src.iter().zip(rgb_chunks) {
let r = *y_src;
Expand Down Expand Up @@ -403,7 +403,7 @@ where

// All branches on generic const will be optimized out.
for (y_src, rgb) in y_iter.zip(rgb_iter) {
let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();

for (y_src, rgb_dst) in y_src.iter().zip(rgb_chunks) {
let y_value = (y_src.as_() - bias_y) * y_coef;
Expand Down Expand Up @@ -567,9 +567,11 @@ fn process_halved_chroma_row_cbcr<

let bias_y = range.bias_y as i32;
let bias_uv = range.bias_uv as i32;
let y_iter = y_plane.chunks_exact(2);
let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
for (((y_src, &u_src), &v_src), rgb_dst) in y_iter.zip(u_plane).zip(v_plane).zip(rgb_chunks) {
let (y_iter, y_left) = y_plane.as_chunks::<2>();
let mut rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
for (((y_src, &u_src), &v_src), rgb_dst) in
y_iter.iter().zip(u_plane).zip(v_plane).zip(&mut rgb_chunks)
{
let y_value0: i32 = y_src[0].as_() - bias_y;
let cb_value: i32 = u_src.as_() - bias_uv;
let cr_value: i32 = v_src.as_() - bias_uv;
Expand Down Expand Up @@ -599,11 +601,11 @@ fn process_halved_chroma_row_cbcr<

// Process remainder if width is odd.
if image.width & 1 != 0 {
let y_left = y_plane.chunks_exact(2).remainder();
let rgb_chunks = rgba
.chunks_exact_mut(CHANNELS * 2)
let rgb_chunks = rgb_chunks
.into_remainder()
.chunks_exact_mut(CHANNELS);
.as_chunks_mut::<CHANNELS>()
.0
.iter_mut();
let u_iter = u_plane.iter().rev();
let v_iter = v_plane.iter().rev();

Expand All @@ -615,11 +617,7 @@ fn process_halved_chroma_row_cbcr<
let cr_value = v_src.as_() - bias_uv;

ycbcr_execute::<V, PRECISION, CHANNELS, BIT_DEPTH>(
rgb_dst.try_into().unwrap(),
y_value,
cb_value,
cr_value,
transform,
rgb_dst, y_value, cb_value, cr_value, transform,
);
}
}
Expand Down Expand Up @@ -1098,7 +1096,7 @@ where

// All branches on generic const will be optimized out.
for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();

for (((y_src, u_src), v_src), rgb_dst) in y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
{
Expand All @@ -1107,7 +1105,7 @@ where
let cr_value = v_src.as_() - bias_uv;

ycbcr_execute::<V, PRECISION, CHANNELS, BIT_DEPTH>(
rgb_dst.try_into().unwrap(),
rgb_dst,
y_value,
cb_value,
cr_value,
Expand Down Expand Up @@ -1240,7 +1238,7 @@ where
let y_bias = range.bias_y as i32;

for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();

for (((&y_src, &u_src), &v_src), rgb_dst) in
y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
Expand All @@ -1259,7 +1257,7 @@ where
}
YuvIntensityRange::Pc => {
for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();

for (((&y_src, &u_src), &v_src), rgb_dst) in
y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
Expand Down
19 changes: 10 additions & 9 deletions src/codecs/bmp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,10 @@ impl<R: BufRead + Seek> BmpDecoder<R> {

// Set alpha to opaque for all pixels if needed (only on first call)
if start_row == 0 && num_channels == 4 {
buf.chunks_exact_mut(4).for_each(|c| c[3] = ALPHA_OPAQUE);
buf.as_chunks_mut::<4>()
.0
.iter_mut()
.for_each(|c| c[3] = ALPHA_OPAQUE);
}

let reader = &mut self.reader;
Expand Down Expand Up @@ -1613,11 +1616,10 @@ impl<R: BufRead + Seek> BmpDecoder<R> {
start_row,
|row| {
reader.read_exact(&mut row_buffer)?;
for (row_data, pixel) in row_buffer
.chunks_exact(2)
.zip(row.chunks_exact_mut(num_channels))
let row_buffer_chunks = row_buffer.as_chunks::<2>().0.iter();
for (&row_data, pixel) in row_buffer_chunks.zip(row.chunks_exact_mut(num_channels))
{
let data = u32::from(u16::from_le_bytes(row_data.try_into().unwrap()));
let data = u32::from(u16::from_le_bytes(row_data));
pixel[0] = bitfields.r.read(data);
pixel[1] = bitfields.g.read(data);
pixel[2] = bitfields.b.read(data);
Expand Down Expand Up @@ -1664,11 +1666,10 @@ impl<R: BufRead + Seek> BmpDecoder<R> {
start_row,
|row| {
reader.read_exact(&mut row_buffer)?;
for (row_data, pixel) in row_buffer
.chunks_exact(4)
.zip(row.chunks_exact_mut(num_channels))
let row_buffer_chunks = row_buffer.as_chunks::<4>().0.iter();
for (&row_data, pixel) in row_buffer_chunks.zip(row.chunks_exact_mut(num_channels))
{
let data = u32::from_le_bytes(row_data.try_into().unwrap());
let data = u32::from_le_bytes(row_data);
pixel[0] = bitfields.r.read(data);
pixel[1] = bitfields.g.read(data);
pixel[2] = bitfields.b.read(data);
Expand Down
10 changes: 5 additions & 5 deletions src/codecs/farbfeld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<R: Read> Read for FarbfeldReader<R> {
bytes_written += 1;
self.current_offset += 1;
} else {
for channel_out in buf.chunks_exact_mut(2) {
for channel_out in buf.as_chunks_mut::<2>().0 {
consume_channel(&mut self.inner, channel_out)?;
bytes_written += 2;
self.current_offset += 2;
Expand Down Expand Up @@ -165,10 +165,10 @@ impl<R: Read + Seek> Seek for FarbfeldReader<R> {
}
}

fn consume_channel<R: Read>(from: &mut R, mut to: &mut [u8]) -> io::Result<()> {
fn consume_channel<R: Read>(from: &mut R, to: &mut [u8; 2]) -> io::Result<()> {
let mut ibuf = [0u8; 2];
from.read_exact(&mut ibuf)?;
to.write_all(&u16::from_be_bytes(ibuf).to_ne_bytes())?;
to.copy_from_slice(&u16::from_be_bytes(ibuf).to_ne_bytes());

Ok(())
}
Expand Down Expand Up @@ -249,9 +249,9 @@ impl<W: Write> FarbfeldEncoder<W> {
self.w.write_all(&width.to_be_bytes())?;
self.w.write_all(&height.to_be_bytes())?;

for channel in data.chunks_exact(2) {
for &channel in data.as_chunks::<2>().0 {
self.w
.write_all(&u16::from_ne_bytes(channel.try_into().unwrap()).to_be_bytes())?;
.write_all(&u16::from_ne_bytes(channel).to_be_bytes())?;
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/codecs/hdr/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ impl<R: Read> ImageDecoder for HdrDecoder<R> {
// read_scanline overwrites the entire buffer or returns an Err,
// so not resetting the buffer here is ok.
read_scanline(&mut self.r, &mut scanline[..])?;
for (dst, &pix) in chunk.chunks_exact_mut(PIXEL_SIZE).zip(scanline.iter()) {
let dst_chunks = chunk.as_chunks_mut::<PIXEL_SIZE>().0.iter_mut();
for (dst, &pix) in dst_chunks.zip(scanline.iter()) {
dst.copy_from_slice(bytemuck::cast_slice(&pix.to_hdr().0));
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/codecs/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,6 @@ impl<R: BufRead + Seek> ImageDecoder for PngDecoder<R> {
}

fn read_image(mut self, buf: &mut [u8]) -> ImageResult<()> {
use byteorder_lite::{BigEndian, ByteOrder, NativeEndian};

assert_eq!(u64::try_from(buf.len()), Ok(self.total_bytes()));
self.reader.next_frame(buf).map_err(ImageError::from_png)?;
// PNG images are big endian. For 16 bit per channel and larger types,
Expand All @@ -282,9 +280,8 @@ impl<R: BufRead + Seek> ImageDecoder for PngDecoder<R> {

match bpc {
1 => (), // No reodering necessary for u8
2 => buf.chunks_exact_mut(2).for_each(|c| {
let v = BigEndian::read_u16(c);
NativeEndian::write_u16(c, v);
2 => buf.as_chunks_mut::<2>().0.iter_mut().for_each(|c| {
*c = u16::from_be_bytes(*c).to_ne_bytes();
}),
_ => unreachable!(),
}
Expand Down Expand Up @@ -800,7 +797,7 @@ impl<W: Write> ImageEncoder for PngEncoder<W> {
let mut reordered;
let buf = if cfg!(target_endian = "little") {
reordered = vec_try_with_capacity(buf.len())?;
reordered.extend(buf.chunks_exact(2).flat_map(|le| [le[1], le[0]]));
reordered.extend(buf.as_chunks::<2>().0.iter().flat_map(|le| [le[1], le[0]]));
&reordered
} else {
buf
Expand Down
18 changes: 8 additions & 10 deletions src/codecs/pnm/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::error::{
use crate::io::ReadExt;
use crate::{utils, ImageDecoder, ImageFormat};

use byteorder_lite::{BigEndian, ByteOrder, NativeEndian};

/// All errors that can occur when attempting to parse a PNM
#[derive(Debug, Clone)]
enum DecoderError {
Expand Down Expand Up @@ -689,9 +687,9 @@ impl<R: Read> PnmDecoder<R> {
*v = (f32::from(*v) * factor).round() as u8;
}
} else if S::sample_size() == 2 {
for chunk in buf.chunks_exact_mut(2) {
let v = NativeEndian::read_u16(chunk);
NativeEndian::write_u16(chunk, (f32::from(v) * factor).round() as u16);
for chunk in buf.as_chunks_mut::<2>().0.iter_mut() {
let v = (f32::from(u16::from_ne_bytes(*chunk)) * factor).round() as u16;
chunk.copy_from_slice(&v.to_ne_bytes());
}
}
}
Expand Down Expand Up @@ -762,17 +760,17 @@ impl Sample for U16 {

fn from_bytes(bytes: &[u8], _row_size: usize, output_buf: &mut [u8]) -> ImageResult<()> {
output_buf.copy_from_slice(bytes);
for chunk in output_buf.chunks_exact_mut(2) {
let v = BigEndian::read_u16(chunk);
NativeEndian::write_u16(chunk, v);
for chunk in output_buf.as_chunks_mut::<2>().0.iter_mut() {
let v = u16::from_be_bytes(*chunk);
chunk.copy_from_slice(&v.to_ne_bytes());
}
Ok(())
}

fn from_ascii(reader: &mut dyn Read, output_buf: &mut [u8]) -> ImageResult<()> {
for chunk in output_buf.chunks_exact_mut(2) {
for chunk in output_buf.as_chunks_mut::<2>().0.iter_mut() {
let v = read_separated_ascii::<u16>(reader)?;
NativeEndian::write_u16(chunk, v);
chunk.copy_from_slice(&v.to_ne_bytes());
}
Ok(())
}
Expand Down
Loading
Loading