Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/lzss_huff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub struct Options {
/// backfill symbol for LZSS dictionary
pub precursor: u8,
/// return error if file is larger
pub max_file_size: u64
pub max_file_size: u64,
/// updates tree when the root frequency comes to this value.
pub max_freq: usize,
}

pub const STD_OPTIONS: Options = Options {
Expand All @@ -47,7 +49,8 @@ pub const STD_OPTIONS: Options = Options {
threshold: 2,
lookahead: 60,
precursor: b' ',
max_file_size: u32::MAX as u64/4
max_file_size: u32::MAX as u64/4,
max_freq: 0x8000,
};

/// Structure to perform the LZSS stage of compression.
Expand Down Expand Up @@ -243,7 +246,7 @@ where R: Read + Seek, W: Write + Seek {
// init
let mut bytes = reader.bytes();
let mut lzss = LZSS::create(opt.clone());
let mut huff = AdaptiveHuffmanCoder::create(256 + opt.lookahead - opt.threshold);
let mut huff = AdaptiveHuffmanCoder::create(opt.max_freq, 256 + opt.lookahead - opt.threshold);
// setup dictionary
let start_pos = opt.window_size - opt.lookahead;
for i in 0..start_pos {
Expand Down Expand Up @@ -339,7 +342,7 @@ where R: Read + Seek, W: Write + Seek {
false => u32::MAX
};
// init
let mut huff = AdaptiveHuffmanDecoder::create(256 + opt.lookahead - opt.threshold);
let mut huff = AdaptiveHuffmanDecoder::create(opt.max_freq, 256 + opt.lookahead - opt.threshold);
let mut lzss= LZSS::create(opt.clone());
let start_pos = opt.window_size - opt.lookahead;
for i in 0..start_pos {
Expand Down
3 changes: 2 additions & 1 deletion src/td0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub const TD_V2_OPTIONS: lzss_huff::Options = lzss_huff::Options {
threshold: 2,
lookahead: 60,
precursor: b' ',
max_file_size: 3_000_000
max_file_size: 3_000_000,
max_freq: 0x8000,
};

/// Convert a TD0 image from advanced compression to normal.
Expand Down
12 changes: 6 additions & 6 deletions src/tools/adaptive_huff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ const D_CODE: [u8;256] = [
];

impl AdaptiveHuffmanTree {
pub fn create(num_symbols: usize) -> Self {
pub fn create(max_freq: usize, num_symbols: usize) -> Self {
let mut ans = Self {
max_freq: 0x8000,
max_freq,
num_symb: num_symbols,
node_count: 2*num_symbols - 1,
root: 2*num_symbols - 2,
Expand Down Expand Up @@ -292,9 +292,9 @@ impl AdaptiveHuffmanTree {
}

impl AdaptiveHuffmanCoder {
pub fn create(num_symbols: usize) -> Self {
pub fn create(max_freq: usize, num_symbols: usize) -> Self {
Self {
tree: AdaptiveHuffmanTree::create(num_symbols),
tree: AdaptiveHuffmanTree::create(max_freq, num_symbols),
bits: BitVec::new(),
ptr: 0
}
Expand Down Expand Up @@ -356,9 +356,9 @@ impl AdaptiveHuffmanCoder {
}

impl AdaptiveHuffmanDecoder {
pub fn create(num_symbols: usize) -> Self {
pub fn create(max_freq: usize, num_symbols: usize) -> Self {
Self {
tree: AdaptiveHuffmanTree::create(num_symbols),
tree: AdaptiveHuffmanTree::create(max_freq, num_symbols),
bits: BitVec::new(),
ptr: 0
}
Expand Down