Skip to content

Commit

Permalink
Improve testability and add tests to the way that blocks of coefficie…
Browse files Browse the repository at this point in the history
…nts are encoded (#69)
  • Loading branch information
mcroomp authored May 6, 2024
1 parent c2aaf2c commit ebf1837
Show file tree
Hide file tree
Showing 12 changed files with 825 additions and 261 deletions.
11 changes: 9 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ thread-priority = "1.0.0"
[dev-dependencies]
rstest = "0.19"
rand = "0.8"
siphasher = "1"

[[bin]]
name = "lepton_jpeg_util"
Expand Down
69 changes: 31 additions & 38 deletions src/structs/block_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*--------------------------------------------------------------------------------------------*/

use super::block_based_image::{AlignedBlock, BlockBasedImage, EMPTY_BLOCK};
use super::neighbor_summary::NeighborSummary;
use super::neighbor_summary::{NeighborSummary, NEIGHBOR_DATA_EMPTY};
use super::probability_tables::ProbabilityTables;

pub struct BlockContext {
Expand All @@ -17,6 +17,13 @@ pub struct BlockContext {
cur_num_non_zeros_index: i32,
above_num_non_zero_index: i32,
}
pub struct NeighborData<'a> {
pub above: &'a AlignedBlock,
pub left: &'a AlignedBlock,
pub above_left: &'a AlignedBlock,
pub neighbor_context_above: &'a NeighborSummary,
pub neighbor_context_left: &'a NeighborSummary,
}

impl BlockContext {
// for debugging
Expand Down Expand Up @@ -72,60 +79,46 @@ impl BlockContext {
return retval;
}

pub fn get_neighbors<'a, const ALL_PRESENT: bool>(
pub fn get_neighbor_data<'a, const ALL_PRESENT: bool>(
&self,
image_data: &'a BlockBasedImage,
neighbor_summary: &'a [NeighborSummary],
pt: &ProbabilityTables,
) -> (&'a AlignedBlock, &'a AlignedBlock, &'a AlignedBlock) {
(
if ALL_PRESENT {
) -> NeighborData<'a> {
NeighborData::<'a> {
above_left: if ALL_PRESENT {
image_data.get_block(self.above_block_index - 1)
} else {
&EMPTY_BLOCK
},
if ALL_PRESENT || pt.is_above_present() {
above: if ALL_PRESENT || pt.is_above_present() {
image_data.get_block(self.above_block_index)
} else {
&EMPTY_BLOCK
},
if ALL_PRESENT || pt.is_left_present() {
left: if ALL_PRESENT || pt.is_left_present() {
image_data.get_block(self.cur_block_index - 1)
} else {
&EMPTY_BLOCK
},
)
}

pub fn non_zeros_here(&self, num_non_zeros: &[NeighborSummary]) -> u8 {
return num_non_zeros[self.cur_num_non_zeros_index as usize].get_num_non_zeros();
}

pub fn get_non_zeros_above(&self, num_non_zeros: &[NeighborSummary]) -> u8 {
return num_non_zeros[self.above_num_non_zero_index as usize].get_num_non_zeros();
}

pub fn get_non_zeros_left(&self, num_non_zeros: &[NeighborSummary]) -> u8 {
return num_non_zeros[(self.cur_num_non_zeros_index - 1) as usize].get_num_non_zeros();
neighbor_context_above: if ALL_PRESENT || pt.is_above_present() {
&neighbor_summary[self.above_num_non_zero_index as usize]
} else {
&NEIGHBOR_DATA_EMPTY
},
neighbor_context_left: if ALL_PRESENT || pt.is_left_present() {
&neighbor_summary[(self.cur_num_non_zeros_index - 1) as usize]
} else {
&NEIGHBOR_DATA_EMPTY
},
}
}

pub fn neighbor_context_here<'a>(
pub fn set_neighbor_summary_here(
&mut self,
num_non_zeros: &'a mut [NeighborSummary],
) -> &'a mut NeighborSummary {
return &mut num_non_zeros[self.cur_num_non_zeros_index as usize];
}

pub fn neighbor_context_above<'a>(
&self,
num_non_zeros: &'a [NeighborSummary],
) -> &'a NeighborSummary {
return &num_non_zeros[self.above_num_non_zero_index as usize];
}

pub fn neighbor_context_left<'a>(
&self,
num_non_zeros: &'a [NeighborSummary],
) -> &'a NeighborSummary {
return &num_non_zeros[(self.cur_num_non_zeros_index - 1) as usize];
neighbor_summary_cache: &mut [NeighborSummary],
neighbor_summary: NeighborSummary,
) {
neighbor_summary_cache[self.cur_num_non_zeros_index as usize] = neighbor_summary;
}
}
12 changes: 12 additions & 0 deletions src/structs/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ impl Branch {
Branch { counts: 0x0101 }
}

/// used for testing to set counts to a specific value
#[cfg(test)]
pub fn set_count(&mut self, count: u16) {
self.counts = count;
}

/// used for testing to set counts to a specific value
#[cfg(test)]
pub fn get_count(&self) -> u16 {
self.counts
}

/// used for debugging to keep the state for hashing
#[allow(dead_code)]
pub fn get_u64(&self) -> u64 {
Expand Down
Loading

0 comments on commit ebf1837

Please sign in to comment.