#![allow(incomplete_features)]
#![feature(ptr_sub_ptr, let_chains, iter_array_chunks)]
#![warn(
clippy::missing_const_for_fn,
clippy::suboptimal_flops,
clippy::dbg_macro,
clippy::use_self
)]
use fimg::{uninit, DynImage, Image};
pub mod decode;
pub(crate) mod encode;
pub mod pam;
pub mod pbm;
pub mod pgm;
pub mod ppm;
pub use pam::PAM;
pub fn decode(x: &impl AsRef<[u8]>) -> decode::Result<DynImage<Vec<u8>>> {
let mut x = x.as_ref();
let magic = decode::magic(&mut x).ok_or(decode::Error::MissingMagic)?;
match magic {
pbm::raw::MAGIC => {
let header = decode::decode_header(&mut x, pbm::raw::MAGIC)?;
Ok(DynImage::Y(pbm::raw::decode_body_into_u8(
x,
uninit::Image::new(header.width, header.height),
)?))
}
pbm::plain::MAGIC => {
let header = decode::decode_header(&mut x, pbm::plain::MAGIC)?;
Ok(DynImage::Y(pbm::plain::decode_body_into_u8(
x,
uninit::Image::new(header.width, header.height),
)?))
}
pgm::raw::MAGIC => Ok(DynImage::Y(pgm::raw::decode_wo_magic(x)?)),
pgm::plain::MAGIC => Ok(DynImage::Y(pgm::plain::decode_wo_magic(x)?)),
ppm::raw::MAGIC => Ok(DynImage::Rgb(ppm::raw::decode_wo_magic(x)?)),
ppm::plain::MAGIC => Ok(DynImage::Rgb(ppm::plain::decode_wo_magic(x)?)),
pam::MAGIC => pam::decode_wo_magic(x),
_ => Err(decode::Error::BadMagic(magic)),
}
}
pub fn encode(x: impl Encode) -> Vec<u8> {
x.encode()
}
pub fn encode_plain(x: impl Encode) -> String {
x.encode_plain()
}
#[doc(hidden)]
pub trait Encode {
fn encode(self) -> Vec<u8>;
fn encode_plain(self) -> String;
}
macro_rules! x {
($mod:ident) => {
impl<T: AsRef<[u8]>> Encode for Image<T, { $mod::CHANNELS }> {
fn encode(self) -> Vec<u8> {
$mod::raw::encode(self)
}
fn encode_plain(self) -> String {
$mod::plain::encode(self)
}
}
};
(t $mod:ident, $n:literal) => {
impl<T: AsRef<[u8]>> Encode for Image<T, $n> {
fn encode(self) -> Vec<u8> {
$mod::raw::encode(<Image<Box<[u8]>, { $mod::CHANNELS }>>::from(self.as_ref()))
}
fn encode_plain(self) -> String {
$mod::plain::encode(<Image<Box<[u8]>, { $mod::CHANNELS }>>::from(self.as_ref()))
}
}
};
}
x![pgm];
x![t pgm, 2];
x![ppm];
x![t ppm, 4];
macro_rules! e {
($dyn:expr, |$image: pat_param| $do:expr) => {
match $dyn {
DynImage::Y($image) => $do,
DynImage::Ya($image) => $do,
DynImage::Rgb($image) => $do,
DynImage::Rgba($image) => $do,
}
};
}
use e;
impl<T: AsRef<[u8]>> Encode for DynImage<T> {
fn encode(self) -> Vec<u8> {
e!(self, |x| encode(x))
}
fn encode_plain(self) -> String {
e!(self, |x| encode_plain(x))
}
}