pnm decoding and encoding
Diffstat (limited to 'src/ppm.rs')
| -rw-r--r-- | src/ppm.rs | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -39,7 +39,10 @@ pub mod plain { .split(u8::is_ascii_whitespace) .filter(|x| !x.is_empty() && x.len() <= 3) .filter(|x| x.iter().all(u8::is_ascii_digit)) - .map(|x| x.iter().fold(0, |acc, &x| acc * 10 + (x - b'0'))) + .flat_map(|x| { + x.iter() + .try_fold(0u8, |acc, &x| acc.checked_mul(10)?.checked_add(x - b'0')) + }) .map(|x| { if max == 255 { x @@ -53,7 +56,7 @@ pub mod plain { // SAFETY: iterator over `pixels` elements. unsafe { out.put(b) }; } - if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < (pixels * 3) as usize } { + if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < (pixels as usize * 3) } { return Err(Error::MissingData); } // SAFETY: checked that the pixels have been initialized. @@ -143,7 +146,7 @@ pub mod raw { // SAFETY: took `pixels` pixels. unsafe { out.put(b) }; } - if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < (pixels * 3) as usize } { + if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < (pixels as usize * 3) } { return Err(Error::MissingData); } // SAFETY: checked that the pixels have been initialized. |