Add canonical mod with reference i16-diff encode

This commit is contained in:
Ivan Smirnov 2021-11-29 23:24:47 +00:00
parent b290afbc02
commit 439a285920
4 changed files with 27 additions and 15 deletions

View file

@ -19,10 +19,6 @@ exclude = [
[dev-dependencies] [dev-dependencies]
png = "^0.17.2" png = "^0.17.2"
[features]
reference-encoder = []
default = []
[target.'cfg(bench)'.dev-dependencies] [target.'cfg(bench)'.dev-dependencies]
# to activate, pass RUSTFLAGS="--cfg bench" until cargo does this automatically # to activate, pass RUSTFLAGS="--cfg bench" until cargo does this automatically
criterion = "^0.3.5" criterion = "^0.3.5"

10
src/canonical.rs Normal file
View file

@ -0,0 +1,10 @@
use crate::colorspace::ColorSpace;
use crate::encode::qoi_encode_to_vec_impl;
use crate::error::Result;
pub fn qoi_encode_to_vec(
data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
colorspace: impl Into<ColorSpace>,
) -> Result<Vec<u8>> {
qoi_encode_to_vec_impl::<true>(data.as_ref(), width, height, channels, colorspace.into())
}

View file

@ -46,7 +46,7 @@ impl WriteBuf {
} }
#[inline] #[inline]
fn encode_diff_reference<const N: usize>( fn encode_diff_canonical<const N: usize>(
px: Pixel<N>, px_prev: Pixel<N>, buf: &mut WriteBuf, px: Pixel<N>, px_prev: Pixel<N>, buf: &mut WriteBuf,
) -> Option<(bool, bool, bool, bool)> { ) -> Option<(bool, bool, bool, bool)> {
let vr = (px.r() as i16) - (px_prev.r() as i16); let vr = (px.r() as i16) - (px_prev.r() as i16);
@ -122,7 +122,7 @@ fn encode_diff_wrapping<const N: usize>(
} }
} }
pub(crate) fn qoi_encode_impl<const N: usize>( pub(crate) fn qoi_encode_impl<const N: usize, const CANONICAL: bool>(
data: &[u8], width: u32, height: u32, colorspace: ColorSpace, data: &[u8], width: u32, height: u32, colorspace: ColorSpace,
) -> Result<Vec<u8>> ) -> Result<Vec<u8>>
where where
@ -190,8 +190,8 @@ where
} else { } else {
*index_px = px; *index_px = px;
let nonzero = if cfg!(feature = "reference-encoder") { let nonzero = if CANONICAL {
encode_diff_reference::<N>(px, px_prev, &mut buf) encode_diff_canonical::<N>(px, px_prev, &mut buf)
} else { } else {
encode_diff_wrapping::<N>(px, px_prev, &mut buf) encode_diff_wrapping::<N>(px, px_prev, &mut buf)
}; };
@ -227,15 +227,19 @@ where
Ok(bytes) Ok(bytes)
} }
pub(crate) fn qoi_encode_to_vec_impl<const CANONICAL: bool>(
data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace,
) -> Result<Vec<u8>> {
match channels {
3 => qoi_encode_impl::<3, CANONICAL>(data, width, height, colorspace),
4 => qoi_encode_impl::<4, CANONICAL>(data, width, height, colorspace),
_ => Err(Error::InvalidChannels { channels }),
}
}
pub fn qoi_encode_to_vec( pub fn qoi_encode_to_vec(
data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8, data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
colorspace: impl Into<ColorSpace>, colorspace: impl Into<ColorSpace>,
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
let data = data.as_ref(); qoi_encode_to_vec_impl::<false>(data.as_ref(), width, height, channels, colorspace.into())
let colorspace = colorspace.into();
match channels {
3 => qoi_encode_impl::<3>(data, width, height, colorspace.into()),
4 => qoi_encode_impl::<4>(data, width, height, colorspace.into()),
_ => Err(Error::InvalidChannels { channels }),
}
} }

View file

@ -6,6 +6,8 @@ mod error;
mod header; mod header;
mod pixel; mod pixel;
pub mod canonical;
pub use crate::colorspace::ColorSpace; pub use crate::colorspace::ColorSpace;
pub use crate::decode::qoi_decode_to_vec; pub use crate::decode::qoi_decode_to_vec;
pub use crate::encode::qoi_encode_to_vec; pub use crate::encode::qoi_encode_to_vec;