pnm decoding and encoding
bendn 4 months ago
parent d7d614b · commit 673d91e
-rw-r--r--Cargo.toml4
-rw-r--r--src/lib.rs6
-rw-r--r--src/pam.rs4
-rw-r--r--src/pbm.rs12
-rw-r--r--src/pgm.rs8
-rw-r--r--src/ppm.rs12
6 files changed, 25 insertions, 21 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1a7a943..fa002be 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "pnm"
-version = "0.1.0"
-edition = "2021"
+version = "0.1.1"
+edition = "2024"
description = "portable anymap format encoding and decoding"
authors = ["bend-n <[email protected]>"]
license = "MIT"
diff --git a/src/lib.rs b/src/lib.rs
index aa83c8e..55b6b3c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,8 +19,8 @@
//!
//! assert_eq!(pnm::encode(out), data);
//! ```
-#![allow(incomplete_features)]
-#![feature(ptr_sub_ptr, let_chains, iter_array_chunks)]
+#![allow(incomplete_features, unsafe_op_in_unsafe_fn)]
+#![feature(iter_array_chunks)]
#![warn(
clippy::missing_const_for_fn,
clippy::suboptimal_flops,
@@ -28,7 +28,7 @@
clippy::use_self
)]
-use fimg::{uninit, DynImage, Image};
+use fimg::{DynImage, Image, uninit};
pub mod decode;
pub(crate) mod encode;
pub mod pam;
diff --git a/src/pam.rs b/src/pam.rs
index 49225cc..37bfea6 100644
--- a/src/pam.rs
+++ b/src/pam.rs
@@ -147,10 +147,10 @@ unsafe fn encode_into<const N: usize>(
for &x in buf {
o.push(x ^ 1)
}
- o.sub_ptr(out)
+ o.offset_from_unsigned(out)
} else {
o.copy_from(buf.as_ptr(), buf.len());
- o.sub_ptr(out) + buf.len()
+ o.offset_from_unsigned(out) + buf.len()
}
}
diff --git a/src/pbm.rs b/src/pbm.rs
index 1461344..065b359 100644
--- a/src/pbm.rs
+++ b/src/pbm.rs
@@ -47,7 +47,7 @@ pub mod plain {
// SAFETY: iterator over `pixels` elements.
unsafe { out.push(b == b'1') };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < pixels as usize } {
+ if unsafe { out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < pixels as usize } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
@@ -69,7 +69,7 @@ pub mod plain {
// SAFETY: iterator over `pixels` elements.
unsafe { out.push((b == b'0') as u8 * 0xff) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < pixels as usize } {
+ if unsafe { out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < pixels as usize } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
@@ -94,7 +94,7 @@ pub mod plain {
// cosmetic
o.push(b'\n');
}
- o.sub_ptr(out)
+ o.offset_from_unsigned(out)
}
#[doc = include_str!("est.md")]
@@ -162,7 +162,7 @@ pub mod raw {
})
.for_each(|x| o.push(x));
- o.sub_ptr(out)
+ o.offset_from_unsigned(out)
}
#[doc = include_str!("decode_body_into.md")]
@@ -185,7 +185,7 @@ pub mod raw {
// SAFETY: took `width` * `height` pixels.
unsafe { out.push(x) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < pixels as usize } {
+ if unsafe { out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < pixels as usize } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
@@ -216,7 +216,7 @@ pub mod raw {
// SAFETY: took `height` * `width` pixels.
unsafe { out.push(x) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < pixels as usize } {
+ if unsafe { out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < pixels as usize } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
diff --git a/src/pgm.rs b/src/pgm.rs
index f1f2cb8..a523287 100644
--- a/src/pgm.rs
+++ b/src/pgm.rs
@@ -56,7 +56,7 @@ pub mod plain {
// SAFETY: iterator over `pixels` elements.
unsafe { out.push(b) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < pixels as usize } {
+ if unsafe { out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < pixels as usize } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
@@ -79,7 +79,7 @@ pub mod plain {
// cosmetic
o.push(b'\n');
}
- o.sub_ptr(out)
+ o.offset_from_unsigned(out)
}
#[doc = include_str!("est.md")]
@@ -135,7 +135,7 @@ pub mod raw {
encodeu32(x.height(), &mut o);
o.put(*b" 255\n");
o.copy_from(x.buffer().as_ptr(), x.len());
- o.sub_ptr(out) + x.len()
+ o.offset_from_unsigned(out) + x.len()
}
#[doc = include_str!("decode_body_into.md")]
@@ -146,7 +146,7 @@ pub mod raw {
// SAFETY: took `pixels` pixels.
unsafe { out.push(b) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < pixels as usize } {
+ if unsafe { out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < pixels as usize } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
diff --git a/src/ppm.rs b/src/ppm.rs
index 950e4d0..349a424 100644
--- a/src/ppm.rs
+++ b/src/ppm.rs
@@ -56,7 +56,9 @@ pub mod plain {
// SAFETY: iterator over `pixels` elements.
unsafe { out.put(b) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < (pixels as usize * 3) } {
+ if unsafe {
+ out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < (pixels as usize * 3)
+ } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.
@@ -79,7 +81,7 @@ pub mod plain {
// cosmetic
o.push(b'\n');
}
- o.sub_ptr(out)
+ o.offset_from_unsigned(out)
}
#[doc = include_str!("est.md")]
@@ -135,7 +137,7 @@ pub mod raw {
encodeu32(x.height(), &mut o);
o.put(*b" 255\n");
o.copy_from(x.buffer().as_ptr(), x.len());
- o.sub_ptr(out) + x.len()
+ o.offset_from_unsigned(out) + x.len()
}
#[doc = include_str!("decode_body_into.md")]
@@ -146,7 +148,9 @@ pub mod raw {
// SAFETY: took `pixels` pixels.
unsafe { out.put(b) };
}
- if unsafe { out.sub_ptr(into.buf().as_mut_ptr().cast()) < (pixels as usize * 3) } {
+ if unsafe {
+ out.offset_from_unsigned(into.buf().as_mut_ptr().cast()) < (pixels as usize * 3)
+ } {
return Err(Error::MissingData);
}
// SAFETY: checked that the pixels have been initialized.