Code cleanup

This commit is contained in:
Josh Holmer 2017-07-26 07:57:13 -04:00
parent f7dd2f0323
commit ec65bf7676
2 changed files with 139 additions and 128 deletions

View file

@ -258,9 +258,7 @@ fn reductions_palette_full_reduction(b: &mut Bencher) {
#[bench] #[bench]
fn reductions_alpha_black(b: &mut Bencher) { fn reductions_alpha_black(b: &mut Bencher) {
let input = test::black_box(PathBuf::from( let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha_black.png"));
"tests/files/rgba_8_reduce_alpha_black.png",
));
let png = png::PngData::new(&input, false).unwrap(); let png = png::PngData::new(&input, false).unwrap();
b.iter(|| { b.iter(|| {
@ -271,9 +269,7 @@ fn reductions_alpha_black(b: &mut Bencher) {
#[bench] #[bench]
fn reductions_alpha_white(b: &mut Bencher) { fn reductions_alpha_white(b: &mut Bencher) {
let input = test::black_box(PathBuf::from( let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha_white.png"));
"tests/files/rgba_8_reduce_alpha_white.png",
));
let png = png::PngData::new(&input, false).unwrap(); let png = png::PngData::new(&input, false).unwrap();
b.iter(|| { b.iter(|| {
@ -284,9 +280,7 @@ fn reductions_alpha_white(b: &mut Bencher) {
#[bench] #[bench]
fn reductions_alpha_left(b: &mut Bencher) { fn reductions_alpha_left(b: &mut Bencher) {
let input = test::black_box(PathBuf::from( let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha_left.png"));
"tests/files/rgba_8_reduce_alpha_left.png",
));
let png = png::PngData::new(&input, false).unwrap(); let png = png::PngData::new(&input, false).unwrap();
b.iter(|| { b.iter(|| {
@ -297,9 +291,7 @@ fn reductions_alpha_left(b: &mut Bencher) {
#[bench] #[bench]
fn reductions_alpha_right(b: &mut Bencher) { fn reductions_alpha_right(b: &mut Bencher) {
let input = test::black_box(PathBuf::from( let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha_right.png"));
"tests/files/rgba_8_reduce_alpha_right.png",
));
let png = png::PngData::new(&input, false).unwrap(); let png = png::PngData::new(&input, false).unwrap();
b.iter(|| { b.iter(|| {
@ -310,9 +302,7 @@ fn reductions_alpha_right(b: &mut Bencher) {
#[bench] #[bench]
fn reductions_alpha_up(b: &mut Bencher) { fn reductions_alpha_up(b: &mut Bencher) {
let input = test::black_box(PathBuf::from( let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha_up.png"));
"tests/files/rgba_8_reduce_alpha_up.png",
));
let png = png::PngData::new(&input, false).unwrap(); let png = png::PngData::new(&input, false).unwrap();
b.iter(|| { b.iter(|| {
@ -323,9 +313,7 @@ fn reductions_alpha_up(b: &mut Bencher) {
#[bench] #[bench]
fn reductions_alpha_down(b: &mut Bencher) { fn reductions_alpha_down(b: &mut Bencher) {
let input = test::black_box(PathBuf::from( let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha_down.png"));
"tests/files/rgba_8_reduce_alpha_down.png",
));
let png = png::PngData::new(&input, false).unwrap(); let png = png::PngData::new(&input, false).unwrap();
b.iter(|| { b.iter(|| {

View file

@ -774,31 +774,46 @@ impl PngData {
pub fn reduce_alpha_channel(&mut self, optim: AlphaOptim) -> bool { pub fn reduce_alpha_channel(&mut self, optim: AlphaOptim) -> bool {
let (bpc, bpp) = match self.ihdr_data.color_type { let (bpc, bpp) = match self.ihdr_data.color_type {
ColorType::RGBA => { ColorType::RGBA |
match self.ihdr_data.bit_depth {
BitDepth::Sixteen => (2, 8),
BitDepth::Eight => (1, 4),
_ => unreachable!(),
}
}
ColorType::GrayscaleAlpha => { ColorType::GrayscaleAlpha => {
match self.ihdr_data.bit_depth { let cpp = self.channels_per_pixel();
BitDepth::Sixteen => (2, 4), let bpc = self.ihdr_data.bit_depth.as_u8() / 8;
BitDepth::Eight => (1, 2), (bpc as usize, (bpc * cpp) as usize)
_ => unreachable!(),
}
} }
_ => { _ => {
return false; return false;
} }
}; };
let mut reduced = Vec::with_capacity(self.raw_data.len());
match optim { match optim {
AlphaOptim::NoOp => { AlphaOptim::NoOp => {
return false; return false;
} }
AlphaOptim::Black => { 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);
}
}
true
}
fn reduce_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() { for line in self.scan_lines() {
reduced.push(line.filter); reduced.push(line.filter);
for pixel in line.data.chunks(bpp) { for pixel in line.data.chunks(bpp) {
@ -811,8 +826,11 @@ impl PngData {
} }
} }
} }
reduced
} }
AlphaOptim::White => {
fn reduce_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() { for line in self.scan_lines() {
reduced.push(line.filter); reduced.push(line.filter);
for pixel in line.data.chunks(bpp) { for pixel in line.data.chunks(bpp) {
@ -828,8 +846,10 @@ impl PngData {
} }
} }
} }
reduced
} }
AlphaOptim::Up => {
fn reduce_alpha_to_up(&self, bpc: usize, bpp: usize) -> Vec<u8> {
let mut lines = Vec::new(); let mut lines = Vec::new();
let scan_lines = self.scan_lines().collect::<Vec<ScanLine>>(); let scan_lines = self.scan_lines().collect::<Vec<ScanLine>>();
let mut last_line = vec![0; scan_lines[0].data.len()]; let mut last_line = vec![0; scan_lines[0].data.len()];
@ -850,9 +870,11 @@ impl PngData {
lines.push(current_line.clone()); lines.push(current_line.clone());
current_line.clear(); current_line.clear();
} }
reduced.extend(lines.into_iter().rev().flatten()); lines.into_iter().rev().flatten().collect()
} }
AlphaOptim::Down => {
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![0; self.scan_lines().next().unwrap().data.len()];
for line in self.scan_lines() { for line in self.scan_lines() {
reduced.push(line.filter); reduced.push(line.filter);
@ -868,8 +890,11 @@ impl PngData {
} }
last_line = reduced.clone(); last_line = reduced.clone();
} }
reduced
} }
AlphaOptim::Left => {
fn reduce_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() { for line in self.scan_lines() {
let mut line_bytes = Vec::with_capacity(line.data.len()); let mut line_bytes = Vec::with_capacity(line.data.len());
let mut last_pixel = vec![0; bpp]; let mut last_pixel = vec![0; bpp];
@ -887,8 +912,11 @@ impl PngData {
reduced.push(line.filter); reduced.push(line.filter);
reduced.extend(line_bytes.chunks(bpp).rev().flatten()); reduced.extend(line_bytes.chunks(bpp).rev().flatten());
} }
reduced
} }
AlphaOptim::Right => {
fn reduce_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() { for line in self.scan_lines() {
reduced.push(line.filter); reduced.push(line.filter);
let mut last_pixel = vec![0; bpp]; let mut last_pixel = vec![0; bpp];
@ -904,11 +932,7 @@ impl PngData {
last_pixel = pixel.to_owned(); last_pixel = pixel.to_owned();
} }
} }
} reduced
}
self.raw_data = reduced;
true
} }
/// Convert the image to the specified interlacing type /// Convert the image to the specified interlacing type
@ -932,7 +956,6 @@ impl PngData {
} }
} }
#[inline]
fn write_png_block(key: &[u8], header: &[u8], output: &mut Vec<u8>) { fn write_png_block(key: &[u8], header: &[u8], output: &mut Vec<u8>) {
let mut header_data = Vec::with_capacity(header.len() + 4); let mut header_data = Vec::with_capacity(header.len() + 4);
header_data.extend_from_slice(key); header_data.extend_from_slice(key);