Skip to content

Commit

Permalink
Alpha heuristic improvements (#144)
Browse files Browse the repository at this point in the history
* Compress alpha with strategy sensitive to repetitions

* Update reductions flag when alpha changed

* Avoid needlessly cloning out-of-date idat_data and temp raw_data
  • Loading branch information
kornelski authored and shssoichiro committed Nov 14, 2018
1 parent 96eda85 commit 4765eaa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,9 @@ fn perform_reductions(png: &mut PngData, opts: &Options, deadline: &Deadline) ->
return reduction_occurred;
}

png.try_alpha_reduction(&opts.alphas);
if png.try_alpha_reduction(&opts.alphas) {
reduction_occurred = true;
}

reduction_occurred
}
Expand Down
73 changes: 37 additions & 36 deletions src/png/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use std::iter::Iterator;
use std::path::Path;

const STD_COMPRESSION: u8 = 8;
const STD_STRATEGY: u8 = 2; // Huffman only
/// Must use normal compression, as faster ones (Huffman/RLE-only) are not representative
const STD_STRATEGY: u8 = 0;
const STD_WINDOW: u8 = 15;
const STD_FILTERS: [u8; 2] = [0, 5];

Expand Down Expand Up @@ -586,7 +587,7 @@ impl PngData {
changed
}

pub fn try_alpha_reduction(&mut self, alphas: &HashSet<AlphaOptim>) {
pub fn try_alpha_reduction(&mut self, alphas: &HashSet<AlphaOptim>) -> bool {
assert!(!alphas.is_empty());
let alphas = alphas.iter().collect::<Vec<_>>();
let best_size = AtomicMin::new(None);
Expand All @@ -596,8 +597,10 @@ impl PngData {
let alphas_iter = alphas.iter();
let best = alphas_iter
.filter_map(|&alpha| {
let mut image = self.clone();
image.reduce_alpha_channel(*alpha);
let image = match self.reduced_alpha_channel(*alpha) {
Some(image) => image,
None => return None,
};
#[cfg(feature = "parallel")]
let filters_iter = STD_FILTERS.par_iter().with_max_len(1);
#[cfg(not(feature = "parallel"))]
Expand All @@ -622,49 +625,47 @@ impl PngData {

if let Some(best) = best {
self.raw_data = best.1.raw_data;
return true;
}
false
}

pub fn reduce_alpha_channel(&mut self, optim: AlphaOptim) -> bool {
/// It doesn't recompress `idat_data`, so this field is out of date
/// after the reduction.
pub fn reduced_alpha_channel(&self, optim: AlphaOptim) -> Option<Self> {
let (bpc, bpp) = match self.ihdr_data.color_type {
ColorType::RGBA | ColorType::GrayscaleAlpha => {
let cpp = self.channels_per_pixel();
let bpc = self.ihdr_data.bit_depth.as_u8() / 8;
(bpc as usize, (bpc * cpp) as usize)
}
_ => {
return false;
return None;
}
};

match optim {
AlphaOptim::NoOp => {
return false;
}
AlphaOptim::Black => {
self.raw_data = self.reduce_alpha_to_black(bpc, bpp);
}
AlphaOptim::White => {
self.raw_data = self.reduce_alpha_to_white(bpc, bpp);
}
AlphaOptim::Up => {
self.raw_data = self.reduce_alpha_to_up(bpc, bpp);
}
AlphaOptim::Down => {
self.raw_data = self.reduce_alpha_to_down(bpc, bpp);
}
AlphaOptim::Left => {
self.raw_data = self.reduce_alpha_to_left(bpc, bpp);
}
AlphaOptim::Right => {
self.raw_data = self.reduce_alpha_to_right(bpc, bpp);
}
}
let raw_data = match optim {
AlphaOptim::NoOp => return None,
AlphaOptim::Black => self.reduced_alpha_to_black(bpc, bpp),
AlphaOptim::White => self.reduced_alpha_to_white(bpc, bpp),
AlphaOptim::Up => self.reduced_alpha_to_up(bpc, bpp),
AlphaOptim::Down => self.reduced_alpha_to_down(bpc, bpp),
AlphaOptim::Left => self.reduced_alpha_to_left(bpc, bpp),
AlphaOptim::Right => self.reduced_alpha_to_right(bpc, bpp),
};

true
Some(Self {
raw_data,
idat_data: vec![],
ihdr_data: self.ihdr_data,
palette: self.palette.clone(),
transparency_pixel: self.transparency_pixel.clone(),
transparency_palette: self.transparency_palette.clone(),
aux_headers: self.aux_headers.clone(),
})
}

fn reduce_alpha_to_black(&self, bpc: usize, bpp: usize) -> Vec<u8> {
fn reduced_alpha_to_black(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut reduced = Vec::with_capacity(self.raw_data.len());
for line in self.scan_lines() {
reduced.push(line.filter);
Expand All @@ -681,7 +682,7 @@ impl PngData {
reduced
}

fn reduce_alpha_to_white(&self, bpc: usize, bpp: usize) -> Vec<u8> {
fn reduced_alpha_to_white(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut reduced = Vec::with_capacity(self.raw_data.len());
for line in self.scan_lines() {
reduced.push(line.filter);
Expand All @@ -701,7 +702,7 @@ impl PngData {
reduced
}

fn reduce_alpha_to_up(&self, bpc: usize, bpp: usize) -> Vec<u8> {
fn reduced_alpha_to_up(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut lines = Vec::new();
let mut scan_lines = self.scan_lines().collect::<Vec<ScanLine>>();
scan_lines.reverse();
Expand Down Expand Up @@ -729,7 +730,7 @@ impl PngData {
flatten(lines.into_iter().rev()).collect()
}

fn reduce_alpha_to_down(&self, bpc: usize, bpp: usize) -> Vec<u8> {
fn reduced_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::new();
for line in self.scan_lines() {
Expand All @@ -752,7 +753,7 @@ impl PngData {
reduced
}

fn reduce_alpha_to_left(&self, bpc: usize, bpp: usize) -> Vec<u8> {
fn reduced_alpha_to_left(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut reduced = Vec::with_capacity(self.raw_data.len());
for line in self.scan_lines() {
let mut line_bytes = Vec::with_capacity(line.data.len());
Expand All @@ -774,7 +775,7 @@ impl PngData {
reduced
}

fn reduce_alpha_to_right(&self, bpc: usize, bpp: usize) -> Vec<u8> {
fn reduced_alpha_to_right(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut reduced = Vec::with_capacity(self.raw_data.len());
for line in self.scan_lines() {
reduced.push(line.filter);
Expand Down

0 comments on commit 4765eaa

Please sign in to comment.