pnm decoding and encoding
Diffstat (limited to 'src/ppm.rs')
-rw-r--r--src/ppm.rs9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/ppm.rs b/src/ppm.rs
index 3b10c30..950e4d0 100644
--- a/src/ppm.rs
+++ b/src/ppm.rs
@@ -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.