diff --git a/Cargo.toml b/Cargo.toml index 6df3939..10a6baa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,7 @@ [package] -name = "tiff" +name = "ndbioimage-tiff" version = "0.11.3" edition = "2021" -resolver = "2" # note: when changed, also update test runner in `.github/workflows/rust.yml` rust-version = "1.88.0" @@ -16,6 +15,9 @@ categories = ["multimedia::images", "multimedia::encoding"] exclude = ["tests/images/*", "tests/fuzz_images/*", "tests/**/*.tif"] +[lib] +name = "tiff" + [dependencies] half = { version = "2.4.1" } quick-error = "2.0.1" diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index f2bd19d..32fddc6 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -599,8 +599,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(2) - .map(|ch| Short(u16::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<2>() + .0 + .iter() + .map(|&ch| Short(u16::from_ne_bytes(ch))), ) }) } @@ -609,8 +611,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(2) - .map(|ch| SignedShort(i16::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<2>() + .0 + .iter() + .map(|&ch| SignedShort(i16::from_ne_bytes(ch))), ) }) } @@ -619,8 +623,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(4) - .map(|ch| Unsigned(u32::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<4>() + .0 + .iter() + .map(|&ch| Unsigned(u32::from_ne_bytes(ch))), ) }) } @@ -629,8 +635,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(4) - .map(|ch| Signed(i32::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<4>() + .0 + .iter() + .map(|&ch| Signed(i32::from_ne_bytes(ch))), ) }) } @@ -639,8 +647,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(4) - .map(|ch| Float(f32::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<4>() + .0 + .iter() + .map(|&ch| Float(f32::from_ne_bytes(ch))), ) }) } @@ -649,15 +659,17 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(8) - .map(|ch| Double(f64::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<8>() + .0 + .iter() + .map(|&ch| Double(f64::from_ne_bytes(ch))), ) }) } Type::RATIONAL => { v = Self::vec_with_capacity(self.count, self.type_, limits)?; self.decode_values(self.count, self.type_, reader, |bytes| { - v.extend(bytes.chunks_exact(8).map(|ch| { + v.extend(bytes.as_chunks::<8>().0.iter().map(|ch| { Rational( u32::from_ne_bytes(ch[..4].try_into().unwrap()), u32::from_ne_bytes(ch[4..].try_into().unwrap()), @@ -668,7 +680,7 @@ impl Entry { Type::SRATIONAL => { v = Self::vec_with_capacity(self.count, self.type_, limits)?; self.decode_values(self.count, self.type_, reader, |bytes| { - v.extend(bytes.chunks_exact(8).map(|ch| { + v.extend(bytes.as_chunks::<8>().0.iter().map(|ch| { SRational( i32::from_ne_bytes(ch[..4].try_into().unwrap()), i32::from_ne_bytes(ch[4..].try_into().unwrap()), @@ -681,8 +693,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(8) - .map(|ch| UnsignedBig(u64::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<8>() + .0 + .iter() + .map(|&ch| UnsignedBig(u64::from_ne_bytes(ch))), ) }) } @@ -691,8 +705,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(8) - .map(|ch| SignedBig(i64::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<8>() + .0 + .iter() + .map(|&ch| SignedBig(i64::from_ne_bytes(ch))), ) }) } @@ -701,8 +717,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(4) - .map(|ch| Ifd(u32::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<4>() + .0 + .iter() + .map(|&ch| Ifd(u32::from_ne_bytes(ch))), ) }) } @@ -711,8 +729,10 @@ impl Entry { self.decode_values(self.count, self.type_, reader, |bytes| { v.extend( bytes - .chunks_exact(8) - .map(|ch| IfdBig(u64::from_ne_bytes(ch.try_into().unwrap()))), + .as_chunks::<8>() + .0 + .iter() + .map(|&ch| IfdBig(u64::from_ne_bytes(ch))), ) }) } diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index ab567d7..d1c7c9b 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -294,8 +294,8 @@ fn one_byte_predict(buf: &mut [u8]) { acc.copy_from_slice(&buf[..N]); // Process N bytes per iteration. Each channel's prefix sum is independent. - let mut chunks = buf[N..].chunks_exact_mut(N); - for chunk in &mut chunks { + let chunks = buf[N..].as_chunks_mut::(); + for chunk in &mut chunks.0.iter_mut() { for c in 0..N { acc[c] = chunk[c].wrapping_add(acc[c]); chunk[c] = acc[c]; @@ -303,7 +303,7 @@ fn one_byte_predict(buf: &mut [u8]) { } // Handle trailing bytes (when buf.len() - N is not a multiple of N). - for (c, byte) in chunks.into_remainder().iter_mut().enumerate() { + for (c, byte) in chunks.1.iter_mut().enumerate() { *byte = byte.wrapping_add(acc[c]); } } diff --git a/src/tags.rs b/src/tags.rs index c0eff16..09d49bc 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -617,26 +617,22 @@ impl ByteOrder { return; } - // FIXME: at MSRV 1.89 or higher use `slice::as_chunks_mut`. match cls { EndianBytes::One => { // No change needed } EndianBytes::Two => { - for chunk in buffer.chunks_exact_mut(2) { - let chunk: &mut [u8; 2] = chunk.try_into().unwrap(); + for chunk in buffer.as_chunks_mut::<2>().0 { *chunk = u16::from_be_bytes(*chunk).to_le_bytes(); } } EndianBytes::Four => { - for chunk in buffer.chunks_exact_mut(4) { - let chunk: &mut [u8; 4] = chunk.try_into().unwrap(); + for chunk in buffer.as_chunks_mut::<4>().0 { *chunk = u32::from_be_bytes(*chunk).to_le_bytes(); } } EndianBytes::Eight => { - for chunk in buffer.chunks_exact_mut(8) { - let chunk: &mut [u8; 8] = chunk.try_into().unwrap(); + for chunk in buffer.as_chunks_mut::<8>().0 { *chunk = u64::from_be_bytes(*chunk).to_le_bytes(); } } diff --git a/tests/decode_images.rs b/tests/decode_images.rs index 5c210ee..d7db448 100644 --- a/tests/decode_images.rs +++ b/tests/decode_images.rs @@ -898,22 +898,22 @@ fn test_image_bytes( } fn byte_order_u16(bytes: &mut [u8]) { - for chunk in bytes.chunks_exact_mut(2) { - let n = u16::from_ne_bytes(chunk.try_into().unwrap()); + for chunk in bytes.as_chunks_mut::<2>().0 { + let n = u16::from_ne_bytes(*chunk); chunk.copy_from_slice(&n.to_le_bytes()); } } fn byte_order_u32(bytes: &mut [u8]) { - for chunk in bytes.chunks_exact_mut(4) { - let n = u32::from_ne_bytes(chunk.try_into().unwrap()); + for chunk in bytes.as_chunks_mut::<4>().0 { + let n = u32::from_ne_bytes(*chunk); chunk.copy_from_slice(&n.to_le_bytes()); } } fn byte_order_u64(bytes: &mut [u8]) { - for chunk in bytes.chunks_exact_mut(8) { - let n = u64::from_ne_bytes(chunk.try_into().unwrap()); + for chunk in bytes.as_chunks_mut::<8>().0 { + let n = u64::from_ne_bytes(*chunk); chunk.copy_from_slice(&n.to_le_bytes()); } }