Skip to content

Commit

Permalink
Remove num_traits dependency (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia authored Dec 26, 2023
1 parent bb34e55 commit 088ab4f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 29 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ publish = false

[dependencies]
byteorder = "1.4.3"
num-traits = "0.2.16"
thiserror = "1.0.47"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/loop_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[inline]
fn c(val: i32) -> i32 {
num_traits::clamp(val, -128, 127)
val.max(-128).min(127)
}

//unsigned to signed
Expand Down
23 changes: 11 additions & 12 deletions src/lossless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
//! [Lossless spec](https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification)
//!
use std::{
convert::TryInto,
io::Read,
ops::{AddAssign, Shl},
};
use std::{convert::TryInto, io::Read, mem};

use crate::decoder::DecodingError;

Expand Down Expand Up @@ -644,18 +640,18 @@ impl BitReader {
self.buf = buf;
}

pub(crate) fn read_bits<T>(&mut self, num: u8) -> Result<T, DecodingError>
where
T: num_traits::Unsigned + Shl<u8, Output = T> + AddAssign<T> + From<bool>,
{
let mut value: T = T::zero();
pub(crate) fn read_bits<T: TryFrom<u16>>(&mut self, num: u8) -> Result<T, DecodingError> {
debug_assert!(num as usize <= 8 * mem::size_of::<T>());
debug_assert!(num <= 16);

let mut value = 0;

for i in 0..num {
if self.buf.len() <= self.index {
return Err(DecodingError::BitStreamError);
}
let bit_true = self.buf[self.index] & (1 << self.bit_count) != 0;
value += T::from(bit_true) << i;
value += u16::from(bit_true) << i;
self.bit_count = if self.bit_count == 7 {
self.index += 1;
0
Expand All @@ -664,7 +660,10 @@ impl BitReader {
};
}

Ok(value)
match value.try_into() {
Ok(value) => Ok(value),
Err(_) => unreachable!("Value too large to fit in type"),
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/vp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//!
use byteorder::{LittleEndian, ReadBytesExt};
use num_traits::clamp;
use std::cmp;
use std::convert::TryInto;
use std::default::Default;
Expand Down Expand Up @@ -1147,11 +1146,11 @@ impl<R: Read> Vp8Decoder<R> {

fn read_quantization_indices(&mut self) {
fn dc_quant(index: i32) -> i16 {
DC_QUANT[clamp(index, 0, 127) as usize]
DC_QUANT[index.max(0).min(127) as usize]
}

fn ac_quant(index: i32) -> i16 {
AC_QUANT[clamp(index, 0, 127) as usize]
AC_QUANT[index.max(0).min(127) as usize]
}

let yac_abs = self.b.read_literal(7);
Expand Down Expand Up @@ -2064,13 +2063,13 @@ impl<R: Read> Vp8Decoder<R> {
}
}

filter_level = clamp(filter_level, 0, 63);
filter_level = filter_level.max(0).min(63);

if macroblock.luma_mode == LumaMode::B {
filter_level += self.mode_delta[0];
}

let filter_level = clamp(filter_level, 0, 63) as u8;
let filter_level = filter_level.max(0).min(63) as u8;

//interior limit
let mut interior_limit = filter_level;
Expand Down

0 comments on commit 088ab4f

Please sign in to comment.