Unit tests for huffman::build_tables
.
#44
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@fintelia, can you PTAL?
I lack a good understanding of how the
fdeflate
crate works, so I thought that I can improve this by trying to add some unit tests. I would need your help with this, because there are a few things that I don't quite understand (some of which manifest in test failures in the new tests added by this PR - i.e. this PR is not ready to be merged as-is and so I marked it as a draft PR).Edit on 2024-12-27 @ 16:43PST - I've actually figured out why single-vs-double entries were getting created. So let me hide the no-longer-relevant question below.
1 stale snippet below
Unexpected single (not double) literal entry
Unexpectedly to me, the code/tables doesn't decode/map the input below as two "B" symbols (instead mapping it somewhat inefficiently to just a single "B" symbol).
QUESTION: Is this also unexpected to you? If this is expected, then can you please help me understand why?
Edit on 2024-12-27 @ 16:22PST - I've actually figured out my last 2 questions on my own - let me hide them in the collapsible section below. Sorry for the noise...
2 more (stale) questions
Format of
SECONDARY_TABLE_ENTRY
https://github.com/image-rs/fdeflate/blob/07d3e744b7a9f65ea822a33c8eec6958694ea4c4/src/tables.rs#L90C44-L97C33 says in a comment that long (> 12 bits) codewords will have the following primary table entry:
I note that the least significant byte above is filled with zeros.
But... it seems that in practice some entries do not follow the format above:
This is probably related to how the
secondary_table_index
is computed here:fdeflate/src/decompress.rs
Lines 539 to 540 in 07d3e74
I think I understand that
litlen_entry >> 16
is the base index (i.e. the0000xxxx_xxxxxxxx
from the doc comment).bits >> 12
discards bits that were covered by the primary table (let's call thisshifted_bits
)But I don't understand why there is
shifted_bits & (litlen_entry 0xff)
. I am guessing that the format really is something like:(not sure what to call this).
Does that make sense? If so, then I can tweak the comment in this PR.
advance_input_bits
greater than 15This is probably a P3 (I feel that I haven't done enough homework to ask for help here).
This is a bit of a heads-up that I have a a WIP branch (currently with a single commit) to add support for different table sizes via
const
generics (i.e.pub type Decompressor = GenericDecompressor<2048, 512>
). I hope this will enable further experimentation with improving performance ofpng
for small images.At any rate, this WIP branch is what motivated addition of
huffman.rs
unit tests (i.e. this PR). The WIP code passes some tests, but the new fuzzer found an input where a literal entry (in a 512-bits primary litlen table) says to advance 60+ input bits - the unexpected entry is created here and I feel that I need to build a better understanding of howbuild_tables
works to figure out what goes wrong. I hope that I can do this by working on the unit tests :-).