Skip to content

Commit

Permalink
Change pnm header to use Dims
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlstrom committed Dec 8, 2024
1 parent b33b58e commit 9d6eac5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
5 changes: 5 additions & 0 deletions core/src/util/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
62 changes: 25 additions & 37 deletions core/src/util/pnm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -183,7 +182,7 @@ pub fn read_pnm(src: impl IntoIterator<Item = u8>) -> Result<Buf2<Color3>> {
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<Color3> = match h.format {
BinaryPixmap => {
let mut col = [0u8; 3];
Expand Down Expand Up @@ -223,10 +222,10 @@ pub fn read_pnm(src: impl IntoIterator<Item = u8>) -> Result<Buf2<Color3>> {
_ => 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))
}
}

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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,
})
);
Expand All @@ -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,
})
);
Expand All @@ -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,
})
);
Expand All @@ -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,
})
);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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)]);
Expand All @@ -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)]);
Expand Down

0 comments on commit 9d6eac5

Please sign in to comment.