From 54dcc516279d6d57b0bb41b52bd661ee641e07c7 Mon Sep 17 00:00:00 2001 From: Oliver Maskery Date: Fri, 30 May 2025 16:24:55 +0100 Subject: [PATCH] Expose max_freq option --- src/lzss_huff.rs | 11 +++++++---- src/td0.rs | 3 ++- src/tools/adaptive_huff.rs | 12 ++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/lzss_huff.rs b/src/lzss_huff.rs index 27a8bc7..eed5085 100644 --- a/src/lzss_huff.rs +++ b/src/lzss_huff.rs @@ -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 { @@ -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. @@ -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 { @@ -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 { diff --git a/src/td0.rs b/src/td0.rs index 2a3c41e..e2b8103 100644 --- a/src/td0.rs +++ b/src/td0.rs @@ -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. diff --git a/src/tools/adaptive_huff.rs b/src/tools/adaptive_huff.rs index 2c73207..1c39ca2 100644 --- a/src/tools/adaptive_huff.rs +++ b/src/tools/adaptive_huff.rs @@ -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, @@ -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 } @@ -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 }