From 76d24ff2267cf8c9653a89a8fe7285e632c902ea Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 2 Feb 2025 22:38:31 +1300 Subject: [PATCH] Use chunks_exact --- src/filters.rs | 4 ++-- src/headers.rs | 2 +- src/reduction/alpha.rs | 6 +++--- src/reduction/bit_depth.rs | 6 +++--- src/reduction/color.rs | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index e1074ed5..d0b03b1d 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -126,8 +126,8 @@ impl RowFilter { return; } - let mut pixels: Vec<_> = data.chunks_mut(bpp).collect(); - let prev_pixels: Vec<_> = prev_line.chunks(bpp).collect(); + let mut pixels: Vec<_> = data.chunks_exact_mut(bpp).collect(); + let prev_pixels: Vec<_> = prev_line.chunks_exact(bpp).collect(); for i in 0..pixels.len() { if pixels[i].iter().skip(color_bytes).all(|b| *b == 0) { // If the first pixel in the row is transparent, find the next non-transparent pixel and pretend diff --git a/src/headers.rs b/src/headers.rs index 1c3860a4..cf447ed0 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -225,7 +225,7 @@ fn palette_to_rgba( ) -> Result, PngError> { let palette_data = palette_data.ok_or_else(|| PngError::new("no palette in indexed image"))?; let mut palette: Vec<_> = palette_data - .chunks(3) + .chunks_exact(3) .map(|color| RGBA8::new(color[0], color[1], color[2], 255)) .collect(); diff --git a/src/reduction/alpha.rs b/src/reduction/alpha.rs index 9e17be9e..04527cfd 100644 --- a/src/reduction/alpha.rs +++ b/src/reduction/alpha.rs @@ -17,7 +17,7 @@ pub fn cleaned_alpha_channel(png: &PngImage) -> Option { let colored_bytes = bpp - byte_depth; let mut reduced = Vec::with_capacity(png.data.len()); - for pixel in png.data.chunks(bpp) { + for pixel in png.data.chunks_exact(bpp) { if pixel.iter().skip(colored_bytes).all(|b| *b == 0) { reduced.resize(reduced.len() + bpp, 0); } else { @@ -46,7 +46,7 @@ pub fn reduced_alpha_channel(png: &PngImage, optimize_alpha: bool) -> Option Option { raw_data.resize(raw_data.len() + colored_bytes, trns); diff --git a/src/reduction/bit_depth.rs b/src/reduction/bit_depth.rs index ee8b6314..caa83980 100644 --- a/src/reduction/bit_depth.rs +++ b/src/reduction/bit_depth.rs @@ -16,13 +16,13 @@ pub fn reduced_bit_depth_16_to_8(png: &PngImage, force_scale: bool) -> Option Option { // Reduce from 16 to 8 bits per channel per pixel by scaling when necessary let data = png .data - .chunks(2) + .chunks_exact(2) .map(|pair| { if pair[0] == pair[1] { return pair[0]; diff --git a/src/reduction/color.rs b/src/reduction/color.rs index fe11d048..ddf40234 100644 --- a/src/reduction/color.rs +++ b/src/reduction/color.rs @@ -106,7 +106,7 @@ pub fn reduced_rgb_to_grayscale(png: &PngImage) -> Option { let byte_depth = png.bytes_per_channel(); let bpp = png.channels_per_pixel() * byte_depth; let last_color = 2 * byte_depth; - for pixel in png.data.chunks(bpp) { + for pixel in png.data.chunks_exact(bpp) { if byte_depth == 1 { if pixel[0] != pixel[1] || pixel[1] != pixel[2] { return None;