Skip to content
Open
9 changes: 8 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ default-boxed = "0.2"
wide = "0.7"
log = "0.4"
git-version = "0.3"
deranged = "0.5.2"

[target.'cfg(target_os = "windows")'.dependencies]
cpu-time = "1.0"
Expand Down
37 changes: 27 additions & 10 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* This software incorporates material from third parties. See NOTICE.txt for details.
*--------------------------------------------------------------------------------------------*/

use deranged::RangedU8;

use crate::jpeg::jpeg_code;

#[derive(PartialEq, Debug)]
Expand All @@ -22,35 +24,50 @@ pub enum JpegType {

pub const COLOR_CHANNEL_NUM_BLOCK_TYPES: usize = 3;

pub const RASTER_TO_ZIGZAG: [u8; 64] = [
/// Convert a slice of u8 into an array of ConstRangedX. Useful for const initialization, eg
/// const CONTARRAY : [ConstRangedX<1,10>;5] = ConstRangedX::<1,10>::into_array([1,2,3,4,5]);
/// will panic if any value is out of range
const fn into_array<const N: usize, const MIN: u8, const MAX: u8>(
a: [u8; N],
) -> [RangedU8<MIN, MAX>; N] {
let mut r = [RangedU8::<MIN, MAX>::MIN; N];
let mut i = 0;
while i < N {
r[i] = RangedU8::<MIN, MAX>::new(a[i]).unwrap();
i += 1;
}
r
}

pub const RASTER_TO_ZIGZAG: [RangedU8<0, 63>; 64] = into_array([
0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9, 11,
18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34,
37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63,
];
]);

// pub const ZIGZAG_TO_RASTER: [u8; 64] = [
// 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20,
// 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59,
// 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
// ];

pub const ZIGZAG_TO_TRANSPOSED: [u8; 64] = [
pub const ZIGZAG_TO_TRANSPOSED: [RangedU8<0, 63>; 64] = into_array([
0, 8, 1, 2, 9, 16, 24, 17, 10, 3, 4, 11, 18, 25, 32, 40, 33, 26, 19, 12, 5, 6, 13, 20, 27, 34,
41, 48, 56, 49, 42, 35, 28, 21, 14, 7, 15, 22, 29, 36, 43, 50, 57, 58, 51, 44, 37, 30, 23, 31,
38, 45, 52, 59, 60, 53, 46, 39, 47, 54, 61, 62, 55, 63,
];
]);

// pub const UNZIGZAG_49: [u8; 49] = [
// 9, 10, 17, 25, 18, 11, 12, 19, 26, 33, 41, 34, 27, 20, 13, 14, 21, 28, 35, 42, 49, 57, 50, 43,
// 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62,
// 63,
// ];

pub const UNZIGZAG_49_TR: [u8; 49] = [
pub const UNZIGZAG_49_TR: [RangedU8<9, 63>; 49] = into_array([
9, 17, 10, 11, 18, 25, 33, 26, 19, 12, 13, 20, 27, 34, 41, 49, 42, 35, 28, 21, 14, 15, 22, 29,
36, 43, 50, 57, 58, 51, 44, 37, 30, 23, 31, 38, 45, 52, 59, 60, 53, 46, 39, 47, 54, 61, 62, 55,
63,
];
]);

// precalculated int base values for 8x8 IDCT scaled by 8192
// DC coef is zeroed intentionally
Expand All @@ -64,15 +81,15 @@ pub const FREQ_MAX: [u16; 14] = [
];

// used to get prediction branches basing on nonzero-number predictor `num_non_zeros_context`
pub const NON_ZERO_TO_BIN: [u8; 26] = [
pub const NON_ZERO_TO_BIN: [RangedU8<0, 8>; 26] = into_array([
0, 1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
];
]);

// used to get prediction branches basing on current `num_non_zeros_left_7x7`, 0th element is not used
pub const NON_ZERO_TO_BIN_7X7: [u8; 50] = [
pub const NON_ZERO_TO_BIN_7X7: [RangedU8<0, 8>; 50] = into_array([
0, 0, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
];
]);

pub const RESIDUAL_NOISE_FLOOR: usize = 7;

Expand Down
8 changes: 4 additions & 4 deletions lib/src/jpeg/block_based_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl AlignedBlock {
}

#[inline(always)]
pub fn get_count_of_non_zeros_7x7(&self) -> u8 {
pub fn get_count_of_non_zeros_7x7(&self) -> usize {
/// counts a row of non-zero values in the 7x7 block
#[inline(always)]
fn count_non_zeros_7x7_row(v: i16x8) -> i16x8 {
Expand All @@ -320,7 +320,7 @@ impl AlignedBlock {
sum += count_non_zeros_7x7_row(self.as_i16x8(i));
}

return sum.reduce_add() as u8;
return sum.reduce_add() as usize;
}

#[inline(always)]
Expand All @@ -335,12 +335,12 @@ impl AlignedBlock {

#[inline(always)]
pub fn set_transposed_from_zigzag(&mut self, index: usize, v: i16) {
self.raw_data[usize::from(ZIGZAG_TO_TRANSPOSED[index])] = v;
self.raw_data[usize::from(ZIGZAG_TO_TRANSPOSED[index].get())] = v;
}

#[inline(always)]
pub fn get_transposed_from_zigzag(&self, index: usize) -> i16 {
return self.raw_data[usize::from(ZIGZAG_TO_TRANSPOSED[index])];
return self.raw_data[usize::from(ZIGZAG_TO_TRANSPOSED[index].get())];
}

#[inline(always)]
Expand Down
Loading