Skip to content

Commit

Permalink
Simplification of BlockContext (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Melirius committed May 25, 2024
1 parent c47c987 commit 5cd11e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
8 changes: 2 additions & 6 deletions src/structs/block_based_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,13 @@ impl BlockBasedImage {
);
}

// blocks above the first line are never dereferenced
pub fn off_y(&self, y: i32) -> BlockContext {
return BlockContext::new(
self.block_width * y,
if y != 0 {
self.block_width * (y - 1)
} else {
-1
},
self.block_width * (y - 1),
if (y & 1) != 0 { self.block_width } else { 0 },
if (y & 1) != 0 { 0 } else { self.block_width },
self,
);
}

Expand Down
29 changes: 5 additions & 24 deletions src/structs/block_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use super::neighbor_summary::{NeighborSummary, NEIGHBOR_DATA_EMPTY};
use super::probability_tables::ProbabilityTables;

pub struct BlockContext {
block_width: i32,

cur_block_index: i32,
above_block_index: i32,

Expand All @@ -32,41 +30,24 @@ impl BlockContext {
self.cur_block_index
}

pub fn next(&mut self, has_more: bool) -> i32 {
// as each new line BlockContext is set by `off_y`, no edge cases with dereferencing
// out of bounds indices is possilbe, therefore no special treatment is needed
pub fn next(&mut self) -> i32 {
self.cur_block_index += 1;

let retval = self.cur_block_index;

if retval < self.block_width {
self.above_block_index = self.cur_block_index + self.block_width;
} else {
self.above_block_index = self.cur_block_index - self.block_width;
}

self.above_block_index += 1;
self.cur_num_non_zeros_index += 1;
self.above_num_non_zero_index += 1;

if !has_more {
let cur_row_first = self.cur_num_non_zeros_index < self.above_num_non_zero_index;
if cur_row_first {
self.above_num_non_zero_index -= self.block_width * 2;
} else {
self.cur_num_non_zeros_index -= self.block_width * 2;
}
}

return retval;
self.cur_block_index
}

pub fn new(
cur_block_index: i32,
above_block_index: i32,
cur_num_non_zeros_index: i32,
above_num_non_zero_index: i32,
image_data: &BlockBasedImage,
) -> Self {
return BlockContext {
block_width: image_data.get_block_width(),
cur_block_index,
above_block_index,
cur_num_non_zeros_index,
Expand Down
6 changes: 3 additions & 3 deletions src/structs/lepton_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn decode_row<R: Read>(
features,
)
.context(here!())?;
let offset = block_context.next(true);
let offset = block_context.next();

if offset >= component_size_in_blocks {
return Ok(()); // no sure if this is an error
Expand Down Expand Up @@ -237,7 +237,7 @@ fn decode_row<R: Read>(
.context(here!())?;
}

let offset = block_context.next(true);
let offset = block_context.next();

if offset >= component_size_in_blocks {
return Ok(()); // no sure if this is an error
Expand Down Expand Up @@ -271,7 +271,7 @@ fn decode_row<R: Read>(
.context(here!())?;
}

block_context.next(false);
block_context.next();
}
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/structs/lepton_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn process_row<W: Write>(
features,
)
.context(here!())?;
let offset = state.next(true);
let offset = state.next();

if offset >= component_size_in_block {
return Ok(());
Expand Down Expand Up @@ -238,7 +238,7 @@ fn process_row<W: Write>(
.context(here!())?;
}

let offset = state.next(true);
let offset = state.next();

if offset >= component_size_in_block {
return Ok(());
Expand Down Expand Up @@ -272,7 +272,7 @@ fn process_row<W: Write>(
.context(here!())?;
}

state.next(false);
state.next();
}
Ok(())
}
Expand Down

0 comments on commit 5cd11e4

Please sign in to comment.