1use crate::util::*;
2use crate::{ImageResult, ImageSize};
34use std::io::{self, BufRead, Seek, SeekFrom};
56pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
7 reader.seek(SeekFrom::Start(2))?;
89// We try to loop until we find a line that does not start with a comment
10 // or is empty. After that, we should expect width and height back to back
11 // separated by an arbitrary amount of whitespace.
12loop {
13// Lines can be arbitrarily long, but 1k is a good enough cap I think.
14 // Anything higher and I blame whoever made the file.
15let line = read_until_whitespace(reader, 1024)?;
16let trimmed_line = line.trim();
1718// If it's a comment, skip until newline
19if trimmed_line.starts_with('#') {
20 read_until_capped(reader, b'\n', 1024)?;
21continue
22}
2324// If it's just empty skip
25if trimmed_line.is_empty() {
26continue;
27 }
2829// The first thing we read that isn't empty or a comment should be the width
30let raw_width = line;
3132// Read in the next non-whitespace section as the height
33let line = read_until_whitespace(reader, 1024)?;
34let raw_height = line.trim();
3536// Try to parse the width and height
37let width_parsed = raw_width.parse::<usize>().ok();
38let height_parsed = raw_height.parse::<usize>().ok();
3940// If successful return it
41if let (Some(width), Some(height)) = (width_parsed, height_parsed) {
42return Ok(ImageSize { width, height });
43 }
4445// If no successful then assume that it cannot be read
46 // If this happens we need to gather test files for those cases
47break;
48 }
4950Err(io::Error::new(io::ErrorKind::InvalidData, "PNM dimensions not found").into())
51}
5253pub fn matches(header: &[u8]) -> bool {
54if header[0] != b'P' {
55return false;
56 }
5758// We only support P1 to P6. Currently ignoring P7, PF, PFM
59if header[1] < b'1' && header[1] > b'6' {
60return false;
61 }
6263true
64}