diff --git a/src/lib.rs b/src/lib.rs index 6a9d43f6..f4b49097 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 } diff --git a/src/png/mod.rs b/src/png/mod.rs index 56c682e1..9dbf8b4d 100644 --- a/src/png/mod.rs +++ b/src/png/mod.rs @@ -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]; @@ -586,7 +587,7 @@ impl PngData { changed } - pub fn try_alpha_reduction(&mut self, alphas: &HashSet) { + pub fn try_alpha_reduction(&mut self, alphas: &HashSet) -> bool { assert!(!alphas.is_empty()); let alphas = alphas.iter().collect::>(); let best_size = AtomicMin::new(None); @@ -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"))] @@ -622,10 +625,14 @@ 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 { let (bpc, bpp) = match self.ihdr_data.color_type { ColorType::RGBA | ColorType::GrayscaleAlpha => { let cpp = self.channels_per_pixel(); @@ -633,38 +640,32 @@ impl PngData { (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 { + fn reduced_alpha_to_black(&self, bpc: usize, bpp: usize) -> Vec { let mut reduced = Vec::with_capacity(self.raw_data.len()); for line in self.scan_lines() { reduced.push(line.filter); @@ -681,7 +682,7 @@ impl PngData { reduced } - fn reduce_alpha_to_white(&self, bpc: usize, bpp: usize) -> Vec { + fn reduced_alpha_to_white(&self, bpc: usize, bpp: usize) -> Vec { let mut reduced = Vec::with_capacity(self.raw_data.len()); for line in self.scan_lines() { reduced.push(line.filter); @@ -701,7 +702,7 @@ impl PngData { reduced } - fn reduce_alpha_to_up(&self, bpc: usize, bpp: usize) -> Vec { + fn reduced_alpha_to_up(&self, bpc: usize, bpp: usize) -> Vec { let mut lines = Vec::new(); let mut scan_lines = self.scan_lines().collect::>(); scan_lines.reverse(); @@ -729,7 +730,7 @@ impl PngData { flatten(lines.into_iter().rev()).collect() } - fn reduce_alpha_to_down(&self, bpc: usize, bpp: usize) -> Vec { + fn reduced_alpha_to_down(&self, bpc: usize, bpp: usize) -> Vec { let mut reduced = Vec::with_capacity(self.raw_data.len()); let mut last_line = Vec::new(); for line in self.scan_lines() { @@ -752,7 +753,7 @@ impl PngData { reduced } - fn reduce_alpha_to_left(&self, bpc: usize, bpp: usize) -> Vec { + fn reduced_alpha_to_left(&self, bpc: usize, bpp: usize) -> Vec { 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()); @@ -774,7 +775,7 @@ impl PngData { reduced } - fn reduce_alpha_to_right(&self, bpc: usize, bpp: usize) -> Vec { + fn reduced_alpha_to_right(&self, bpc: usize, bpp: usize) -> Vec { let mut reduced = Vec::with_capacity(self.raw_data.len()); for line in self.scan_lines() { reduced.push(line.filter);