Skip to content

Commit

Permalink
Final cleanup: change all mention of 'pixels' to 'bits', bump version…
Browse files Browse the repository at this point in the history
… to 0.2.0
  • Loading branch information
pearfalse committed Jul 9, 2017
1 parent ef425ca commit b3607ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitterlemon"
version = "0.1.0"
version = "0.2.0"
authors = ["Simon Harris <simon@pearfalse.com>"]
description = "An RLE-based encoder and decoder for bit streams."
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where S : Iterator<Item=u8> {
let frame_size = byte_to_run_size(n);
let mode = n >= 0xc0;
self.state = if frame_size > 1 {
// don't bother moving to run state if only one pixel in this run
// don't bother moving to run state if only one bit in this run
// also, leaving this method in state Run(0, _) is a logic error
DecodeState::Run(frame_size - 1, mode)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ fn byte_to_frame_size(byte: u8) -> u8 {
enum DecodeState {
Pending, // should pull
Run(u8, bool), // run count, is_set
Frame(u8, u8, u8), // frame pixel count, stage contents, stage size
Frame(u8, u8, u8), // frame bit count, stage contents, stage size
Done, // iteration complete
}

Expand Down Expand Up @@ -285,7 +285,7 @@ mod test_decoder {

#[test]
fn error_on_frame_cutoff() {
let case = |bytes: &[u8], pixels_lost: u8, bytes_missing: u8| {
let case = |bytes: &[u8], bits_lost: u8, bytes_missing: u8| {
let mut iter = super::decode(bytes.iter().map(|&b| b));

let ok_count = (bytes.len() - 1) * 8;
Expand All @@ -303,7 +303,7 @@ mod test_decoder {
assert!(error.is_err());
match error.unwrap_err() {
super::Error::TruncatedInput(pl, bm) => {
assert_eq!(pl, pixels_lost);
assert_eq!(pl, bits_lost);
assert_eq!(bm, bytes_missing);
}
};
Expand Down
18 changes: 9 additions & 9 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fmt;
/// `source` can be any iterator that yields `bool` values.
pub fn encode<S>(source: S) -> Encoder<S>
where S : Iterator<Item=bool> {
let runs_only = RunIterator::from_pixels(source);
let runs_only = RunIterator::from_bits(source);
let with_frames = WithFrames::new(runs_only);
Encoder {
encoder_impl: with_frames,
Expand Down Expand Up @@ -140,7 +140,7 @@ const RLE_MAX_FRAME: u8 = 128;

impl<S: Iterator<Item=bool>> RunIterator<S> {

fn from_pixels(source: S) -> RunIterator<S> {
fn from_bits(source: S) -> RunIterator<S> {
RunIterator {
state: None,
source: source
Expand Down Expand Up @@ -240,7 +240,7 @@ mod test_run_builder {
let op = |input: &[u8], output: &[Run]| {

let bool_stream : Vec<bool> = input.iter().map(|&c| c == b'1').collect();
let collected : Vec<Run> = RunIterator::from_pixels(bool_stream.iter().map(|&b| b)).collect();
let collected : Vec<Run> = RunIterator::from_bits(bool_stream.iter().map(|&b| b)).collect();

assert_eq!(output, collected.as_slice());
};
Expand All @@ -267,7 +267,7 @@ mod test_run_builder {

#[test]
fn next_none_idempotence() {
let mut iter = RunIterator::from_pixels((&[] as &[bool]).iter().map(|&b| b));
let mut iter = RunIterator::from_bits((&[] as &[bool]).iter().map(|&b| b));
for i in 0..20 {
let next = iter.next();
assert_eq!(None, next, "pulling next() on run {} was {:?}, not None", i, next);
Expand All @@ -280,7 +280,7 @@ type RunHolding = arrayvec::ArrayVec<[Run; 128]>;

trait RunHoldingExtensions {
fn bytes_as_frame(&self) -> (u8, u8);
fn num_pixels(&self) -> u8;
fn num_bits(&self) -> u8;
fn unshift_bit(&mut self, ptr: &mut u8) -> u8;
}

Expand All @@ -297,9 +297,9 @@ impl RunHoldingExtensions for RunHolding {
(bytes, padding)
}

fn num_pixels(&self) -> u8 {
fn num_bits(&self) -> u8 {
let r : usize = self.iter().map(|r| r.size() as usize).sum();
debug_assert!(r <= RLE_MAX_FRAME as usize, "number of frame pixels too high at {:?}", r);
debug_assert!(r <= RLE_MAX_FRAME as usize, "number of frame bits too high at {:?}", r);
r as u8
}

Expand Down Expand Up @@ -394,7 +394,7 @@ impl<S: Iterator<Item=Run>> WithFrames<S> {
let run_size = self.next_run.unwrap().size();

// would the frame get too large?
let cur_frame_size = self.runs.num_pixels() as u16;
let cur_frame_size = self.runs.num_bits() as u16;
if cur_frame_size + (run_size as u16) > (RLE_MAX_FRAME as u16) { return false; }

// let's get some clear cases out of the way first
Expand Down Expand Up @@ -426,7 +426,7 @@ impl<S: Iterator<Item=Run>> WithFrames<S> {
// return header for new frame to output
// and prime next mode to be WithFramesMode::FlushingFrame
let frame_size = self.runs.len() as u8;
let header_byte = self.runs.num_pixels();
let header_byte = self.runs.num_bits();
(
Some(if header_byte == RLE_MAX_FRAME {0u8} else {header_byte}),
Some(WithFramesMode::FlushingFrame(0, frame_size))
Expand Down

0 comments on commit b3607ef

Please sign in to comment.