Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure empty huffman code optimization applies to color cache #14

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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
2 changes: 1 addition & 1 deletion test-data
30 changes: 18 additions & 12 deletions webpsan/src/parse/lossless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ impl EntropyCodedImage {
) -> Result<Self, Error> {
let color_cache = ColorCache::read(reader).while_parsing_type()?;
let codes = PrefixCodeGroup::read(reader, &color_cache).while_parsing_type()?;
let argb_readahead_bits = codes.argb_readahead_bits();
let readahead_bits = codes.readahead_bits();
let green_readahead_bits = codes.green_readahead_bits();
let arb_readahead_bits = codes.arb_readahead_bits();
let backref_readahead_bits = codes.backref_readahead_bits();
let readahead_bits = green_readahead_bits + arb_readahead_bits.max(backref_readahead_bits);

let len = width.saturating_mul(height);
let mut pixel_idx = 0;
Expand All @@ -314,7 +316,7 @@ impl EntropyCodedImage {
let color = Color::buf_read(reader, symbol as u8, &codes).while_parsing_type()?;
log::debug!("color: {color}");
fun(color)?;
if argb_readahead_bits == 0 {
if green_readahead_bits + arb_readahead_bits == 0 {
pixel_idx = len.get();
} else {
pixel_idx += 1;
Expand Down Expand Up @@ -344,7 +346,11 @@ impl EntropyCodedImage {
ParseError::InvalidInput,
ColorCacheIndexOutOfBounds(color_cache_index, color_cache.len()),
);
pixel_idx += 1;
if green_readahead_bits == 0 {
pixel_idx = len.get();
} else {
pixel_idx += 1;
}
}
}
}
Expand Down Expand Up @@ -600,16 +606,16 @@ impl PrefixCodeGroup {
Ok(T::new(tree))
}

pub fn argb_readahead_bits(&self) -> u32 {
self.alpha.tree.longest_code_len()
+ self.red.tree.longest_code_len()
+ self.green.tree.longest_code_len()
+ self.blue.tree.longest_code_len()
pub fn green_readahead_bits(&self) -> u32 {
self.green.tree.longest_code_len()
}

pub fn arb_readahead_bits(&self) -> u32 {
self.alpha.tree.longest_code_len() + self.red.tree.longest_code_len() + self.blue.tree.longest_code_len()
}

pub fn readahead_bits(&self) -> u32 {
self.argb_readahead_bits()
.max(self.green.tree.longest_code_len() + BackReference::readahead_bits(self))
pub fn backref_readahead_bits(&self) -> u32 {
self.green.tree.longest_code_len() + BackReference::readahead_bits(self)
}
}

Expand Down
Loading