From 9d6eac59eda6163b051e4a4f8d4178b0bdf9530b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Dahlstr=C3=B6m?= Date: Sat, 7 Dec 2024 20:04:40 +0200 Subject: [PATCH] Change pnm header to use Dims --- core/src/util/buf.rs | 5 ++++ core/src/util/pnm.rs | 62 ++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/core/src/util/buf.rs b/core/src/util/buf.rs index 0b20018c..47eb07d3 100644 --- a/core/src/util/buf.rs +++ b/core/src/util/buf.rs @@ -378,6 +378,11 @@ pub mod inner { pub fn height(&self) -> u32 { self.dims.1 } + /// Returns the width and height of `self`. + #[inline] + pub fn dims(&self) -> Dims { + self.dims + } /// Returns the stride of `self`. #[inline] pub fn stride(&self) -> u32 { diff --git a/core/src/util/pnm.rs b/core/src/util/pnm.rs index cab4efa2..a130b6b2 100644 --- a/core/src/util/pnm.rs +++ b/core/src/util/pnm.rs @@ -27,17 +27,17 @@ use Error::*; use Format::*; use crate::math::color::{rgb, Color3}; -use crate::util::buf::Buf2; #[cfg(feature = "std")] -use crate::util::buf::AsSlice2; +use super::buf::AsSlice2; + +use super::{buf::Buf2, Dims}; /// The header of a PNM image #[derive(Copy, Clone, Debug, Eq, PartialEq)] struct Header { format: Format, - width: u32, - height: u32, + dims: Dims, #[allow(unused)] // TODO Currently not used max: u16, @@ -140,24 +140,23 @@ impl Header { it.next().ok_or(UnexpectedEnd)?, ]; let format = magic.try_into()?; - let width: u32 = parse_num(&mut it)?; - let height: u32 = parse_num(&mut it)?; + let dims = (parse_num(&mut it)?, parse_num(&mut it)?); let max: u16 = match &format { TextBitmap | BinaryBitmap => 1, _ => parse_num(&mut it)?, }; - Ok(Self { format, width, height, max }) + Ok(Self { format, dims, max }) } /// Writes `self` to `dest` as a valid PNM header, /// including a trailing newline. #[cfg(feature = "std")] fn write(&self, mut dest: impl Write) -> io::Result<()> { - let Self { format, width, height, max } = *self; + let Self { format, dims: (w, h), max } = *self; let max: &dyn Display = match format { TextBitmap | BinaryBitmap => &"", _ => &max, }; - writeln!(dest, "{} {} {} {}", format, width, height, max) + writeln!(dest, "{} {} {} {}", format, w, h, max) } } @@ -183,7 +182,7 @@ pub fn read_pnm(src: impl IntoIterator) -> Result> { let mut it = src.into_iter(); let h = Header::parse(&mut it)?; - let count = h.width * h.height; + let count = h.dims.0 * h.dims.1; let data: Vec = match h.format { BinaryPixmap => { let mut col = [0u8; 3]; @@ -223,10 +222,10 @@ pub fn read_pnm(src: impl IntoIterator) -> Result> { _ => unimplemented!(), }; - if data.len() < (h.width * h.height) as usize { + if data.len() < count as usize { Err(UnexpectedEnd) } else { - Ok(Buf2::new_from((h.width, h.height), data)) + Ok(Buf2::new_from(h.dims, data)) } } @@ -259,9 +258,8 @@ pub fn write_ppm( ) -> io::Result<()> { let slice = data.as_slice2(); Header { - format: Format::BinaryPixmap, - width: slice.width(), - height: slice.height(), + format: BinaryPixmap, + dims: slice.dims(), max: 255, } .write(&mut out)?; @@ -336,8 +334,7 @@ mod tests { Header::parse(*b"P6 123\t \n\r321 255 "), Ok(Header { format: BinaryPixmap, - width: 123, - height: 321, + dims: (123, 321), max: 255, }) ); @@ -349,8 +346,7 @@ mod tests { Header::parse(*b"P6 # foo 42\n 123\n#bar\n#baz\n321 255 "), Ok(Header { format: BinaryPixmap, - width: 123, - height: 321, + dims: (123, 321), max: 255, }) ); @@ -362,8 +358,7 @@ mod tests { Header::parse(*b"P4 123 456 "), Ok(Header { format: BinaryBitmap, - width: 123, - height: 456, + dims: (123, 456), max: 1, }) ); @@ -375,8 +370,7 @@ mod tests { Header::parse(*b"P5 123 456 64 "), Ok(Header { format: BinaryGraymap, - width: 123, - height: 456, + dims: (123, 456), max: 64, }) ); @@ -412,9 +406,8 @@ mod tests { fn write_header_p1() { let mut out = Vec::new(); let hdr = Header { - format: Format::TextBitmap, - width: 16, - height: 32, + format: TextBitmap, + dims: (16, 32), max: 1, }; hdr.write(&mut out).unwrap(); @@ -426,9 +419,8 @@ mod tests { fn write_header_p6() { let mut out = Vec::new(); let hdr = Header { - format: Format::BinaryPixmap, - width: 64, - height: 16, + format: BinaryPixmap, + dims: (64, 16), max: 4, }; hdr.write(&mut out).unwrap(); @@ -441,8 +433,7 @@ mod tests { let buf = read_pnm(data).unwrap(); - assert_eq!(buf.width(), 2); - assert_eq!(buf.height(), 2); + assert_eq!(buf.dims(), (2, 2)); assert_eq!(buf[[0, 0]], rgb(0, 0, 0)); assert_eq!(buf[[1, 0]], rgb(123, 0, 42)); @@ -455,8 +446,7 @@ mod tests { // 0x69 == 0b0110_1001 let buf = read_pnm(*b"P4 4 2\n\x69").unwrap(); - assert_eq!(buf.width(), 4); - assert_eq!(buf.height(), 2); + assert_eq!(buf.dims(), (4, 2)); let b = rgb(0u8, 0, 0); let w = rgb(0xFFu8, 0xFF, 0xFF); @@ -469,8 +459,7 @@ mod tests { fn read_pnm_p5() { let buf = read_pnm(*b"P5 2 2 255\n\x01\x23\x45\x67").unwrap(); - assert_eq!(buf.width(), 2); - assert_eq!(buf.height(), 2); + assert_eq!(buf.dims(), (2, 2)); assert_eq!(buf[0usize], [rgb(0x01, 0x01, 0x01), rgb(0x23, 0x23, 0x23)]); assert_eq!(buf[1usize], [rgb(0x45, 0x45, 0x45), rgb(0x67, 0x67, 0x67)]); @@ -487,8 +476,7 @@ mod tests { ) .unwrap(); - assert_eq!(buf.width(), 2); - assert_eq!(buf.height(), 2); + assert_eq!(buf.dims(), (2, 2)); assert_eq!(buf[0usize], [rgb(0x01, 0x12, 0x23), rgb(0x34, 0x45, 0x56)]); assert_eq!(buf[1usize], [rgb(0x67, 0x78, 0x89), rgb(0x9A, 0xAB, 0xBC)]);