From 439a28592048070ef8e78df3bafae3e67eca93dd Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 29 Nov 2021 23:24:47 +0000 Subject: [PATCH] Add `canonical` mod with reference i16-diff encode --- Cargo.toml | 4 ---- src/canonical.rs | 10 ++++++++++ src/encode.rs | 26 +++++++++++++++----------- src/lib.rs | 2 ++ 4 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 src/canonical.rs diff --git a/Cargo.toml b/Cargo.toml index a2c1aaa..e27c5f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,10 +19,6 @@ exclude = [ [dev-dependencies] png = "^0.17.2" -[features] -reference-encoder = [] -default = [] - [target.'cfg(bench)'.dev-dependencies] # to activate, pass RUSTFLAGS="--cfg bench" until cargo does this automatically criterion = "^0.3.5" diff --git a/src/canonical.rs b/src/canonical.rs new file mode 100644 index 0000000..191b9c3 --- /dev/null +++ b/src/canonical.rs @@ -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, +) -> Result> { + qoi_encode_to_vec_impl::(data.as_ref(), width, height, channels, colorspace.into()) +} diff --git a/src/encode.rs b/src/encode.rs index 5e8487c..08fea97 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -46,7 +46,7 @@ impl WriteBuf { } #[inline] -fn encode_diff_reference( +fn encode_diff_canonical( px: Pixel, px_prev: Pixel, buf: &mut WriteBuf, ) -> Option<(bool, bool, bool, bool)> { let vr = (px.r() as i16) - (px_prev.r() as i16); @@ -122,7 +122,7 @@ fn encode_diff_wrapping( } } -pub(crate) fn qoi_encode_impl( +pub(crate) fn qoi_encode_impl( data: &[u8], width: u32, height: u32, colorspace: ColorSpace, ) -> Result> where @@ -190,8 +190,8 @@ where } else { *index_px = px; - let nonzero = if cfg!(feature = "reference-encoder") { - encode_diff_reference::(px, px_prev, &mut buf) + let nonzero = if CANONICAL { + encode_diff_canonical::(px, px_prev, &mut buf) } else { encode_diff_wrapping::(px, px_prev, &mut buf) }; @@ -227,15 +227,19 @@ where Ok(bytes) } +pub(crate) fn qoi_encode_to_vec_impl( + data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace, +) -> Result> { + 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( data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8, colorspace: impl Into, ) -> Result> { - let data = data.as_ref(); - 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 }), - } + qoi_encode_to_vec_impl::(data.as_ref(), width, height, channels, colorspace.into()) } diff --git a/src/lib.rs b/src/lib.rs index c43d49f..14d4dc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,8 @@ mod error; mod header; mod pixel; +pub mod canonical; + pub use crate::colorspace::ColorSpace; pub use crate::decode::qoi_decode_to_vec; pub use crate::encode::qoi_encode_to_vec;