From 328cfac40d9ce7400a00170ea4ef9b4fd4a0d8e8 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 3 Jan 2022 21:40:24 +0300 Subject: [PATCH] Big batch of renames: strip qoi prefix everywhere --- Cargo.toml | 2 +- {qoi-bench => bench}/Cargo.toml | 1 + {qoi-bench => bench}/build.rs | 0 {qoi-bench => bench}/src/main.rs | 10 ++-- {qoi-bench => bench}/src/qoi.c | 0 fuzz/Cargo.toml | 8 ++-- .../fuzz_targets/{qoi_decode.rs => decode.rs} | 12 +++-- .../fuzz_targets/{qoi_encode.rs => encode.rs} | 6 ++- src/decode.rs | 48 +++++++++---------- src/encode.rs | 34 ++++++------- src/header.rs | 6 +-- src/lib.rs | 10 ++-- tests/test_ref.rs | 8 ++-- 13 files changed, 73 insertions(+), 72 deletions(-) rename {qoi-bench => bench}/Cargo.toml (95%) rename {qoi-bench => bench}/build.rs (100%) rename {qoi-bench => bench}/src/main.rs (97%) rename {qoi-bench => bench}/src/qoi.c (100%) rename fuzz/fuzz_targets/{qoi_decode.rs => decode.rs} (67%) rename fuzz/fuzz_targets/{qoi_encode.rs => encode.rs} (70%) diff --git a/Cargo.toml b/Cargo.toml index 2500e2c..eac840f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ reference = [] # follows reference encoder implementation precisely, but may be bytemuck = "1.7" [workspace] -members = ["qoi-bench"] +members = ["bench"] [dev-dependencies] anyhow = "1.0" diff --git a/qoi-bench/Cargo.toml b/bench/Cargo.toml similarity index 95% rename from qoi-bench/Cargo.toml rename to bench/Cargo.toml index 71640e5..add470f 100644 --- a/qoi-bench/Cargo.toml +++ b/bench/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2018" license = "MIT/Apache-2.0" authors = ["Ivan Smirnov "] +publish = false [dependencies] # internal diff --git a/qoi-bench/build.rs b/bench/build.rs similarity index 100% rename from qoi-bench/build.rs rename to bench/build.rs diff --git a/qoi-bench/src/main.rs b/bench/src/main.rs similarity index 97% rename from qoi-bench/src/main.rs rename to bench/src/main.rs index 9c0c60c..e6d7e32 100644 --- a/qoi-bench/src/main.rs +++ b/bench/src/main.rs @@ -123,11 +123,11 @@ impl Codec for CodecQoiFast { } fn encode(img: &Image) -> Result> { - Ok(qoi_fast::qoi_encode_to_vec(&img.data, img.width, img.height)?) + Ok(qoi_fast::encode_to_vec(&img.data, img.width, img.height)?) } fn decode(data: &[u8], _img: &Image) -> Result> { - Ok(qoi_fast::qoi_decode_to_vec(data)?.1) + Ok(qoi_fast::decode_to_vec(data)?.1) } } @@ -165,8 +165,7 @@ impl Codec for CodecQoiC { fn encode(img: &Image) -> Result> { unsafe { let (ptr, len) = Self::qoi_encode(img)?; - let mut vec = Vec::with_capacity(len); - vec.set_len(len); + let mut vec = vec![0; len]; ptr::copy_nonoverlapping(ptr, vec.as_mut_ptr(), len); libc::free(ptr as _); Ok(vec) @@ -185,8 +184,7 @@ impl Codec for CodecQoiC { unsafe { let (ptr, desc) = Self::qoi_decode(data, img)?; let len = desc.width as usize * desc.height as usize * desc.channels as usize; - let mut vec = Vec::with_capacity(len); - vec.set_len(len); + let mut vec = vec![0; len]; ptr::copy_nonoverlapping(ptr, vec.as_mut_ptr(), len); libc::free(ptr as _); Ok(vec) diff --git a/qoi-bench/src/qoi.c b/bench/src/qoi.c similarity index 100% rename from qoi-bench/src/qoi.c rename to bench/src/qoi.c diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 8d5d839..f9c04dc 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -19,13 +19,13 @@ libfuzzer-sys = "0.4" members = ["."] [[bin]] -name = "qoi_encode" -path = "fuzz_targets/qoi_encode.rs" +name = "encode" +path = "fuzz_targets/encode.rs" test = false doc = false [[bin]] -name = "qoi_decode" -path = "fuzz_targets/qoi_decode.rs" +name = "decode" +path = "fuzz_targets/decode.rs" test = false doc = false diff --git a/fuzz/fuzz_targets/qoi_decode.rs b/fuzz/fuzz_targets/decode.rs similarity index 67% rename from fuzz/fuzz_targets/qoi_decode.rs rename to fuzz/fuzz_targets/decode.rs index f14517b..9ecb174 100644 --- a/fuzz/fuzz_targets/qoi_decode.rs +++ b/fuzz/fuzz_targets/decode.rs @@ -1,6 +1,8 @@ #![no_main] use libfuzzer_sys::fuzz_target; +use qoi_fast::{decode_header, decode_to_vec, Channels, ColorSpace, Header}; + fuzz_target!(|input: (u16, u16, bool, &[u8])| { let (w, h, is_4, data) = input; let (w, h) = (1 + w % 260, 1 + h % 260); @@ -25,15 +27,15 @@ fuzz_target!(|input: (u16, u16, bool, &[u8])| { vec.extend(&*data); vec.extend(&[0, 0, 0, 0, 0, 0, 0, 1]); - let header_expected = qoi_fast::Header { + let header_expected = Header { width: w as u32, height: h as u32, - channels: qoi_fast::Channels::try_from(channels).unwrap(), - colorspace: qoi_fast::ColorSpace::try_from(0).unwrap(), + channels: Channels::try_from(channels).unwrap(), + colorspace: ColorSpace::try_from(0).unwrap(), }; - assert_eq!(qoi_fast::qoi_decode_header(&vec).unwrap(), header_expected); + assert_eq!(decode_header(&vec).unwrap(), header_expected); - if let Ok((header, out)) = qoi_fast::qoi_decode_to_vec(&vec) { + if let Ok((header, out)) = decode_to_vec(&vec) { assert_eq!(header, header_expected); assert_eq!(out.len(), header.n_bytes()); } diff --git a/fuzz/fuzz_targets/qoi_encode.rs b/fuzz/fuzz_targets/encode.rs similarity index 70% rename from fuzz/fuzz_targets/qoi_encode.rs rename to fuzz/fuzz_targets/encode.rs index 0945661..33f2315 100644 --- a/fuzz/fuzz_targets/qoi_encode.rs +++ b/fuzz/fuzz_targets/encode.rs @@ -1,6 +1,8 @@ #![no_main] use libfuzzer_sys::fuzz_target; +use qoi_fast::{encode_size_limit, encode_to_vec}; + fuzz_target!(|input: (bool, u8, &[u8])| { let (is_4, w_frac, data) = input; let channels = if is_4 { 4 } else { 3 }; @@ -13,10 +15,10 @@ fuzz_target!(|input: (bool, u8, &[u8])| { let h = n_pixels / w; (w, h) }; - let out = qoi_fast::qoi_encode_to_vec(&data[..(w * h * channels as usize)], w as u32, h as u32); + let out = encode_to_vec(&data[..(w * h * channels as usize)], w as u32, h as u32); if w * h != 0 { let out = out.unwrap(); - assert!(out.len() <= qoi_fast::encoded_size_limit(w as u32, h as u32, channels)); + assert!(out.len() <= encode_size_limit(w as u32, h as u32, channels)); } else { assert!(out.is_err()); } diff --git a/src/decode.rs b/src/decode.rs index 862e068..f0721bf 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -22,9 +22,7 @@ const QOI_OP_DIFF_END: u8 = QOI_OP_DIFF | 0x3f; const QOI_OP_LUMA_END: u8 = QOI_OP_LUMA | 0x3f; #[inline] -fn qoi_decode_impl_slice( - data: &[u8], out: &mut [u8], -) -> Result +fn decode_impl_slice(data: &[u8], out: &mut [u8]) -> Result where Pixel: SupportedChannels, [u8; N]: Pod, @@ -92,14 +90,14 @@ where } #[inline] -fn qoi_decode_impl_slice_all( +fn decode_impl_slice_all( data: &[u8], out: &mut [u8], channels: u8, src_channels: u8, ) -> Result { match (channels, src_channels) { - (3, 3) => qoi_decode_impl_slice::<3, false>(data, out), - (3, 4) => qoi_decode_impl_slice::<3, true>(data, out), - (4, 3) => qoi_decode_impl_slice::<4, false>(data, out), - (4, 4) => qoi_decode_impl_slice::<4, true>(data, out), + (3, 3) => decode_impl_slice::<3, false>(data, out), + (3, 4) => decode_impl_slice::<3, true>(data, out), + (4, 3) => decode_impl_slice::<4, false>(data, out), + (4, 4) => decode_impl_slice::<4, true>(data, out), _ => { cold(); Err(Error::InvalidChannels { channels }) @@ -108,33 +106,33 @@ fn qoi_decode_impl_slice_all( } #[inline] -pub fn qoi_decode_to_buf(buf: impl AsMut<[u8]>, data: impl AsRef<[u8]>) -> Result
{ - let mut decoder = QoiDecoder::new(&data)?; +pub fn decode_to_buf(buf: impl AsMut<[u8]>, data: impl AsRef<[u8]>) -> Result
{ + let mut decoder = Decoder::new(&data)?; decoder.decode_to_buf(buf)?; Ok(*decoder.header()) } #[cfg(any(feature = "std", feature = "alloc"))] #[inline] -pub fn qoi_decode_to_vec(data: impl AsRef<[u8]>) -> Result<(Header, Vec)> { - let mut decoder = QoiDecoder::new(&data)?; +pub fn decode_to_vec(data: impl AsRef<[u8]>) -> Result<(Header, Vec)> { + let mut decoder = Decoder::new(&data)?; let out = decoder.decode_to_vec()?; Ok((*decoder.header(), out)) } #[inline] -pub fn qoi_decode_header(data: impl AsRef<[u8]>) -> Result
{ +pub fn decode_header(data: impl AsRef<[u8]>) -> Result
{ Header::decode(data) } #[derive(Clone)] -pub struct QoiDecoder<'a> { +pub struct Decoder<'a> { data: &'a [u8], header: Header, channels: Channels, } -impl<'a> QoiDecoder<'a> { +impl<'a> Decoder<'a> { #[inline] pub fn new(data: &'a (impl AsRef<[u8]> + ?Sized)) -> Result { let data = data.as_ref(); @@ -171,7 +169,7 @@ impl<'a> QoiDecoder<'a> { if unlikely(buf.len() < size) { return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size }); } - let n_read = qoi_decode_impl_slice_all( + let n_read = decode_impl_slice_all( self.data, buf, self.channels.as_u8(), @@ -191,7 +189,7 @@ impl<'a> QoiDecoder<'a> { #[cfg(any(feature = "std"))] #[inline] -fn qoi_decode_impl_stream( +fn decode_impl_stream( data: &mut R, out: &mut [u8], ) -> Result<()> where @@ -261,14 +259,14 @@ where #[cfg(feature = "std")] #[inline] -fn qoi_decode_impl_stream_all( +fn decode_impl_stream_all( data: &mut R, out: &mut [u8], channels: u8, src_channels: u8, ) -> Result<()> { match (channels, src_channels) { - (3, 3) => qoi_decode_impl_stream::<_, 3, false>(data, out), - (3, 4) => qoi_decode_impl_stream::<_, 3, true>(data, out), - (4, 3) => qoi_decode_impl_stream::<_, 4, false>(data, out), - (4, 4) => qoi_decode_impl_stream::<_, 4, true>(data, out), + (3, 3) => decode_impl_stream::<_, 3, false>(data, out), + (3, 4) => decode_impl_stream::<_, 3, true>(data, out), + (4, 3) => decode_impl_stream::<_, 4, false>(data, out), + (4, 4) => decode_impl_stream::<_, 4, true>(data, out), _ => { cold(); Err(Error::InvalidChannels { channels }) @@ -277,14 +275,14 @@ fn qoi_decode_impl_stream_all( } #[cfg(feature = "std")] -pub struct QoiStreamDecoder { +pub struct StreamDecoder { reader: R, header: Header, channels: Channels, } #[cfg(feature = "std")] -impl QoiStreamDecoder { +impl StreamDecoder { #[inline] pub fn new(mut reader: R) -> Result { let mut b = [0; QOI_HEADER_SIZE]; @@ -325,7 +323,7 @@ impl QoiStreamDecoder { if unlikely(buf.len() < size) { return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size }); } - qoi_decode_impl_stream_all( + decode_impl_stream_all( &mut self.reader, buf, self.channels.as_u8(), diff --git a/src/encode.rs b/src/encode.rs index 4fbac4b..33cf9bb 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -14,7 +14,7 @@ use crate::utils::GenericWriter; use crate::utils::{unlikely, BytesMut, Writer}; #[allow(clippy::cast_possible_truncation, unused_assignments)] -fn qoi_encode_impl(mut buf: W, data: &[u8]) -> Result +fn encode_impl(mut buf: W, data: &[u8]) -> Result where Pixel: SupportedChannels, { @@ -70,15 +70,15 @@ where } #[inline] -fn qoi_encode_impl_all(out: W, data: &[u8], channels: Channels) -> Result { +fn encode_impl_all(out: W, data: &[u8], channels: Channels) -> Result { match channels { - Channels::Rgb => qoi_encode_impl::<_, 3>(out, data), - Channels::Rgba => qoi_encode_impl::<_, 4>(out, data), + Channels::Rgb => encode_impl::<_, 3>(out, data), + Channels::Rgba => encode_impl::<_, 4>(out, data), } } #[inline] -pub fn encoded_size_limit(width: u32, height: u32, channels: impl Into) -> usize { +pub fn encode_size_limit(width: u32, height: u32, channels: impl Into) -> usize { let (width, height) = (width as usize, height as usize); let n_pixels = width.saturating_mul(height); QOI_HEADER_SIZE @@ -88,24 +88,24 @@ pub fn encoded_size_limit(width: u32, height: u32, channels: impl Into) -> u } #[inline] -pub fn qoi_encode_to_buf( +pub fn encode_to_buf( buf: impl AsMut<[u8]>, data: impl AsRef<[u8]>, width: u32, height: u32, ) -> Result { - QoiEncoder::new(&data, width, height)?.encode_to_buf(buf) + Encoder::new(&data, width, height)?.encode_to_buf(buf) } #[cfg(any(feature = "alloc", feature = "std"))] #[inline] -pub fn qoi_encode_to_vec(data: impl AsRef<[u8]>, width: u32, height: u32) -> Result> { - QoiEncoder::new(&data, width, height)?.encode_to_vec() +pub fn encode_to_vec(data: impl AsRef<[u8]>, width: u32, height: u32) -> Result> { + Encoder::new(&data, width, height)?.encode_to_vec() } -pub struct QoiEncoder<'a> { +pub struct Encoder<'a> { data: &'a [u8], header: Header, } -impl<'a> QoiEncoder<'a> { +impl<'a> Encoder<'a> { #[inline] #[allow(clippy::cast_possible_truncation)] pub fn new(data: &'a (impl AsRef<[u8]> + ?Sized), width: u32, height: u32) -> Result { @@ -138,27 +138,27 @@ impl<'a> QoiEncoder<'a> { } #[inline] - pub fn encoded_size_limit(&self) -> usize { - self.header.encoded_size_limit() + pub fn encode_size_limit(&self) -> usize { + self.header.encode_size_limit() } #[inline] pub fn encode_to_buf(&self, mut buf: impl AsMut<[u8]>) -> Result { let buf = buf.as_mut(); - let size_required = self.encoded_size_limit(); + let size_required = self.encode_size_limit(); if unlikely(buf.len() < size_required) { return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size_required }); } let (head, tail) = buf.split_at_mut(QOI_HEADER_SIZE); // can't panic head.copy_from_slice(&self.header.encode()); - let n_written = qoi_encode_impl_all(BytesMut::new(tail), self.data, self.header.channels)?; + let n_written = encode_impl_all(BytesMut::new(tail), self.data, self.header.channels)?; Ok(QOI_HEADER_SIZE + n_written) } #[cfg(any(feature = "alloc", feature = "std"))] #[inline] pub fn encode_to_vec(&self) -> Result> { - let mut out = vec![0_u8; self.encoded_size_limit()]; + let mut out = vec![0_u8; self.encode_size_limit()]; let size = self.encode_to_buf(&mut out)?; out.truncate(size); Ok(out) @@ -169,7 +169,7 @@ impl<'a> QoiEncoder<'a> { pub fn encode_to_stream(&self, writer: &mut W) -> Result { writer.write_all(&self.header.encode())?; let n_written = - qoi_encode_impl_all(GenericWriter::new(writer), self.data, self.header.channels)?; + encode_impl_all(GenericWriter::new(writer), self.data, self.header.channels)?; Ok(n_written + QOI_HEADER_SIZE) } } diff --git a/src/header.rs b/src/header.rs index 2a6539d..fa9f4f3 100644 --- a/src/header.rs +++ b/src/header.rs @@ -3,7 +3,7 @@ use core::convert::TryInto; use bytemuck::cast_slice; use crate::consts::{QOI_HEADER_SIZE, QOI_MAGIC, QOI_PIXELS_MAX}; -use crate::encoded_size_limit; +use crate::encode_size_limit; use crate::error::{Error, Result}; use crate::types::{Channels, ColorSpace}; use crate::utils::unlikely; @@ -112,7 +112,7 @@ impl Header { /// /// This comes useful when pre-allocating a buffer to encode the image into. #[inline] - pub fn encoded_size_limit(&self) -> usize { - encoded_size_limit(self.width, self.height, self.channels) + pub fn encode_size_limit(&self) -> usize { + encode_size_limit(self.width, self.height, self.channels) } } diff --git a/src/lib.rs b/src/lib.rs index f60e515..2bb3e17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,14 +26,14 @@ mod utils; pub mod consts; #[cfg(any(feature = "alloc", feature = "std"))] -pub use crate::decode::qoi_decode_to_vec; +pub use crate::decode::decode_to_vec; #[cfg(feature = "std")] -pub use crate::decode::QoiStreamDecoder; -pub use crate::decode::{qoi_decode_header, qoi_decode_to_buf, QoiDecoder}; +pub use crate::decode::StreamDecoder; +pub use crate::decode::{decode_header, decode_to_buf, Decoder}; #[cfg(any(feature = "alloc", feature = "std"))] -pub use crate::encode::qoi_encode_to_vec; -pub use crate::encode::{encoded_size_limit, qoi_encode_to_buf, QoiEncoder}; +pub use crate::encode::encode_to_vec; +pub use crate::encode::{encode_size_limit, encode_to_buf, Encoder}; pub use crate::error::{Error, Result}; pub use crate::header::Header; pub use crate::types::{Channels, ColorSpace}; diff --git a/tests/test_ref.rs b/tests/test_ref.rs index ee54e34..29edbb6 100644 --- a/tests/test_ref.rs +++ b/tests/test_ref.rs @@ -5,7 +5,7 @@ use anyhow::{bail, Result}; use cfg_if::cfg_if; use walkdir::{DirEntry, WalkDir}; -use qoi_fast::{qoi_decode_to_vec, qoi_encode_to_vec}; +use qoi_fast::{decode_to_vec, encode_to_vec}; fn find_qoi_png_pairs(root: impl AsRef) -> Vec<(PathBuf, PathBuf)> { let root = root.as_ref(); @@ -96,7 +96,7 @@ fn test_reference_images() -> Result<()> { let png_name = png_path.file_name().unwrap_or_default().to_string_lossy(); let img = Image::from_png(png_path)?; println!("{} {} {} {}", png_name, img.width, img.height, img.channels); - let encoded = qoi_encode_to_vec(&img.data, img.width, img.height)?; + let encoded = encode_to_vec(&img.data, img.width, img.height)?; let expected = fs::read(qoi_path)?; assert_eq!(encoded.len(), expected.len()); // this should match regardless cfg_if! { @@ -104,8 +104,8 @@ fn test_reference_images() -> Result<()> { compare_slices(&png_name, "encoding", &encoded, &expected)?; } } - let (_header1, decoded1) = qoi_decode_to_vec(&encoded)?; - let (_header2, decoded2) = qoi_decode_to_vec(&expected)?; + let (_header1, decoded1) = decode_to_vec(&encoded)?; + let (_header2, decoded2) = decode_to_vec(&expected)?; compare_slices(&png_name, "decoding [1]", &decoded1, &img.data)?; compare_slices(&png_name, "decoding [2]", &decoded2, &img.data)?; }