pnm decoding and encoding
Diffstat (limited to 'src/pam.rs')
-rw-r--r--src/pam.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/pam.rs b/src/pam.rs
index f55b4b8..d8c1314 100644
--- a/src/pam.rs
+++ b/src/pam.rs
@@ -10,8 +10,20 @@ use fimg::{DynImage, Image};
pub const MAGIC: u8 = 7;
-/// Encode an <code>[Image]<[u8], N></code> to [PAM](https://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format) Raw (binary) Image.
-/// And decode.
+/// Encode this <code>[Image]<[u8], N></code> to a [PAM](https://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format) Raw (binary) Image.
+///
+/// ```
+/// # use pnm::pam;
+/// # use fimg::Image;
+/// let out = pam::encode(
+/// Image::<_, 1>::build(20, 15).buf(&include_bytes!("../tdata/fimg-gray.imgbuf")[..])
+/// );
+/// ```
+pub fn encode(x: impl PAM) -> Vec<u8> {
+ x.encode()
+}
+
+#[doc(hidden)]
pub trait PAM {
/// Encode this image to pam.
fn encode(self) -> Vec<u8>;
@@ -100,6 +112,16 @@ impl<T: AsRef<[u8]>> PAM for Image<T, 4> {
}
}
+impl<T: AsRef<[u8]>> PAM for DynImage<T> {
+ fn encode(self) -> Vec<u8> {
+ super::e!(self, |x| encode(x))
+ }
+
+ unsafe fn encode_into(x: Self, out: *mut u8) -> usize {
+ super::e!(x, |x| PAM::encode_into(x, out))
+ }
+}
+
#[inline]
unsafe fn encode_into<const N: usize>(
(buf, (w, h)): (&[u8], (u32, u32)),