Fix issues in transparency palette reduction and release version 0.14.3

This commit is contained in:
Josh Holmer 2017-02-25 23:33:55 -05:00
parent 44818a854b
commit 87810829ce
6 changed files with 36 additions and 12 deletions

View file

@ -1,3 +1,6 @@
**Version 0.14.3**
- Fix multiple bugs when reducing transparency palettes
**Version 0.14.2** **Version 0.14.2**
- Fix a bug when reducing palette in images with bit depth less than 8 - Fix a bug when reducing palette in images with bit depth less than 8
- Fix a bug when reducing palette in images with transparency - Fix a bug when reducing palette in images with transparency

2
Cargo.lock generated
View file

@ -1,6 +1,6 @@
[root] [root]
name = "oxipng" name = "oxipng"
version = "0.14.2" version = "0.14.3"
dependencies = [ dependencies = [
"bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -8,7 +8,7 @@ homepage = "https://github.com/shssoichiro/oxipng"
license = "MIT" license = "MIT"
name = "oxipng" name = "oxipng"
repository = "https://github.com/shssoichiro/oxipng" repository = "https://github.com/shssoichiro/oxipng"
version = "0.14.2" version = "0.14.3"
[lib] [lib]
name = "oxipng" name = "oxipng"

View file

@ -471,7 +471,7 @@ impl PngData {
.clone() .clone()
.unwrap() .unwrap()
.chunks(3) .chunks(3)
.zip(trns.iter()) .zip(trns.iter().chain([255].iter().cycle()))
.flat_map(|(pixel, trns)| { .flat_map(|(pixel, trns)| {
let mut pixel = pixel.to_owned(); let mut pixel = pixel.to_owned();
pixel.push(*trns); pixel.push(*trns);
@ -574,7 +574,6 @@ impl PngData {
index_map: &mut HashMap<u8, u8>, index_map: &mut HashMap<u8, u8>,
indexed_palette: &mut Vec<&[u8]>) { indexed_palette: &mut Vec<&[u8]>) {
let mut new_data = Vec::with_capacity(self.raw_data.len()); let mut new_data = Vec::with_capacity(self.raw_data.len());
let mut alpha_palette = self.aux_headers.get("tRNS").cloned();
let original_len = indexed_palette.len(); let original_len = indexed_palette.len();
for idx in indices.iter().sorted_by(|a, b| b.cmp(a)) { for idx in indices.iter().sorted_by(|a, b| b.cmp(a)) {
for i in (*idx as usize + 1)..original_len { for i in (*idx as usize + 1)..original_len {
@ -584,14 +583,15 @@ impl PngData {
} }
} }
indexed_palette.remove(*idx as usize); indexed_palette.remove(*idx as usize);
if let Some(ref mut alpha) = alpha_palette { if let Some(ref mut alpha) = self.transparency_palette {
alpha.remove(*idx as usize); if (*idx as usize) < alpha.len() {
alpha.remove(*idx as usize);
}
} }
} }
if alpha_palette.is_some() { if let Some(ref mut alpha) = self.transparency_palette {
let alpha_header = self.aux_headers.get_mut("tRNS"); while let Some(255) = alpha.last().cloned() {
if let Some(alpha_hdr) = alpha_header { alpha.pop();
*alpha_hdr = alpha_palette.unwrap();
} }
} }
// Reassign data bytes to new indices // Reassign data bytes to new indices
@ -615,7 +615,7 @@ impl PngData {
new_byte |= if let Some(new_idx) = index_map.get(&upper) { new_byte |= if let Some(new_idx) = index_map.get(&upper) {
*new_idx << 4 *new_idx << 4
} else { } else {
upper << 4 upper
}; };
new_byte |= if let Some(new_idx) = index_map.get(&lower) { new_byte |= if let Some(new_idx) = index_map.get(&lower) {
*new_idx *new_idx
@ -660,7 +660,13 @@ impl PngData {
} }
index_map.clear(); index_map.clear();
self.raw_data = new_data; self.raw_data = new_data;
let new_palette = indexed_palette.iter().cloned().flatten().cloned().collect::<Vec<u8>>(); let new_palette = indexed_palette.iter()
.cloned()
.flatten()
.enumerate()
.filter(|&(i, _)| !(self.transparency_palette.is_some() && i % 4 == 3))
.map(|(_, x)| *x)
.collect::<Vec<u8>>();
self.palette = Some(new_palette); self.palette = Some(new_palette);
} }

BIN
tests/files/issue-58.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 585 B

View file

@ -222,3 +222,18 @@ fn issue_56() {
ColorType::Indexed, ColorType::Indexed,
BitDepth::Four); BitDepth::Four);
} }
#[test]
fn issue_58() {
let input = PathBuf::from("tests/files/issue-58.png");
let opts = get_opts(&input);
let output = opts.out_file.clone();
test_it_converts(&input,
&output,
&opts,
ColorType::Indexed,
BitDepth::Four,
ColorType::Indexed,
BitDepth::Four);
}