pnm decoding and encoding
smol opt
bendn 2024-03-15
parent 9498a75 · commit 612c597
-rw-r--r--src/pam.rs12
-rw-r--r--src/pgm.rs7
-rw-r--r--src/ppm.rs7
3 files changed, 10 insertions, 16 deletions
diff --git a/src/pam.rs b/src/pam.rs
index d8c1314..726562c 100644
--- a/src/pam.rs
+++ b/src/pam.rs
@@ -141,15 +141,15 @@ unsafe fn encode_into<const N: usize>(
o.put(*b"TUPLTYPE ");
o.put(*tupltype);
o.put(*b"\nENDHDR\n");
- for &x in buf {
- if &tupltype[..] == b"BLACKANDWHITE" {
+ if tupltype[..] == *b"BLACKANDWHITE" {
+ for &x in buf {
o.push(x ^ 1)
- } else {
- o.push(x)
}
+ o.sub_ptr(out)
+ } else {
+ o.copy_from(buf.as_ptr(), buf.len());
+ o.sub_ptr(out) + buf.len()
}
-
- o.sub_ptr(out)
}
#[derive(Copy, Clone, Debug)]
diff --git a/src/pgm.rs b/src/pgm.rs
index 0b844d8..f2dcb73 100644
--- a/src/pgm.rs
+++ b/src/pgm.rs
@@ -131,11 +131,8 @@ pub mod raw {
o.push(b' ');
encodeu32(x.height(), &mut o);
o.put(*b" 255\n");
- for &x in *x.buffer() {
- o.push(x)
- }
-
- o.sub_ptr(out)
+ o.copy_from(x.buffer().as_ptr(), x.len());
+ o.sub_ptr(out) + x.len()
}
#[doc = include_str!("decode_body_into.md")]
diff --git a/src/ppm.rs b/src/ppm.rs
index f57a3d9..3b10c30 100644
--- a/src/ppm.rs
+++ b/src/ppm.rs
@@ -131,11 +131,8 @@ pub mod raw {
o.push(b' ');
encodeu32(x.height(), &mut o);
o.put(*b" 255\n");
- for &x in *x.buffer() {
- o.push(x)
- }
-
- o.sub_ptr(out)
+ o.copy_from(x.buffer().as_ptr(), x.len());
+ o.sub_ptr(out) + x.len()
}
#[doc = include_str!("decode_body_into.md")]