Skip to content

Commit

Permalink
Fix more issues with alpha optim on interlaced images
Browse files Browse the repository at this point in the history
Closes #133
  • Loading branch information
shssoichiro committed Aug 12, 2018
1 parent 4ea310f commit 71d0f6f
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Version 2.1.1
- More fixes for alpha optimization on interlaced images ([#133](https://github.com/shssoichiro/oxipng/issues/133))

### Version 2.1.0
- [SEMVER_MINOR] Bump minimum Rust version to 1.27.0
- [SEMVER_MINOR] Reenable faster Cloudflare zlib compression on platforms that support it
Expand Down
10 changes: 8 additions & 2 deletions src/png/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,12 @@ impl PngData {
let mut lines = Vec::new();
let mut scan_lines = self.scan_lines().collect::<Vec<ScanLine>>();
scan_lines.reverse();
let mut last_line = vec![0; scan_lines[0].data.len()];
let mut last_line = Vec::new();
let mut current_line = Vec::with_capacity(last_line.len());
for line in scan_lines {
if line.data.len() != last_line.len() {
last_line = vec![0; line.data.len()];
}
current_line.push(line.filter);
for (pixel, last_pixel) in line.data.chunks(bpp).zip(last_line.chunks(bpp)) {
if pixel.iter().skip(bpp - bpc).fold(0, |sum, i| sum | i) == 0 {
Expand All @@ -735,8 +738,11 @@ impl PngData {

fn reduce_alpha_to_down(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut reduced = Vec::with_capacity(self.raw_data.len());
let mut last_line = vec![0; self.scan_lines().next().unwrap().data.len()];
let mut last_line = Vec::new();
for line in self.scan_lines() {
if line.data.len() != last_line.len() {
last_line = vec![0; line.data.len()];
}
reduced.push(line.filter);
for (pixel, last_pixel) in line.data.chunks(bpp).zip(last_line.chunks(bpp)) {
if pixel.iter().skip(bpp - bpc).fold(0, |sum, i| sum | i) == 0 {
Expand Down
Binary file added tests/files/issue-133.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
218 changes: 216 additions & 2 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,114 @@ fn issue_92_filter_5() {
}

#[test]
fn issue_113() {
fn issue_113_white() {
let input = "tests/files/issue-113.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1);
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Black);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-white-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::GrayscaleAlpha,
BitDepth::Eight,
);
}

#[test]
fn issue_113_black() {
let input = "tests/files/issue-113.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1);
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Black);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-black-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::GrayscaleAlpha,
BitDepth::Eight,
);
}

#[test]
fn issue_113_right() {
let input = "tests/files/issue-113.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1);
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Right);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-right-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::GrayscaleAlpha,
BitDepth::Eight,
);
}

#[test]
fn issue_113_left() {
let input = "tests/files/issue-113.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1);
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Left);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-left-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::GrayscaleAlpha,
BitDepth::Eight,
);
}

#[test]
fn issue_113_up() {
let input = "tests/files/issue-113.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1);
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Up);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-f5-out.png").to_owned(),
Path::new(input).with_extension("-up-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::GrayscaleAlpha,
BitDepth::Eight,
);
}

#[test]
fn issue_113_down() {
let input = "tests/files/issue-113.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.interlace = Some(1);
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Down);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-down-out.png").to_owned(),
));
test_it_converts(
input,
Expand All @@ -309,3 +409,117 @@ fn issue_113() {
BitDepth::Eight,
);
}

#[test]
fn issue_133_black() {
let input = "tests/files/issue-133.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Black);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-black-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::RGBA,
BitDepth::Eight,
);
}

#[test]
fn issue_133_white() {
let input = "tests/files/issue-133.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::White);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-white-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::RGBA,
BitDepth::Eight,
);
}

#[test]
fn issue_133_up() {
let input = "tests/files/issue-133.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Up);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-up-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::RGBA,
BitDepth::Eight,
);
}

#[test]
fn issue_133_down() {
let input = "tests/files/issue-133.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Down);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-down-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::RGBA,
BitDepth::Eight,
);
}

#[test]
fn issue_133_right() {
let input = "tests/files/issue-133.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Right);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-right-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::RGBA,
BitDepth::Eight,
);
}

#[test]
fn issue_133_left() {
let input = "tests/files/issue-133.png";
let (_, mut opts) = get_opts(Path::new(input));
opts.alphas = HashSet::new();
opts.alphas.insert(AlphaOptim::Left);
let output = OutFile::Path(Some(
Path::new(input).with_extension("-left-out.png").to_owned(),
));
test_it_converts(
input,
Some((output, opts)),
ColorType::RGBA,
BitDepth::Eight,
ColorType::RGBA,
BitDepth::Eight,
);
}

0 comments on commit 71d0f6f

Please sign in to comment.