Fix filtering, passes some more tests
This commit is contained in:
parent
675a28ee32
commit
30f03678d2
2 changed files with 37 additions and 25 deletions
|
|
@ -162,7 +162,8 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
||||||
if (best.is_some() && ok_result.4.len() < best.clone().unwrap().4.len()) ||
|
if (best.is_some() && ok_result.4.len() < best.clone().unwrap().4.len()) ||
|
||||||
(best.is_none() &&
|
(best.is_none() &&
|
||||||
(ok_result.4.len() < png.idat_data.len() ||
|
(ok_result.4.len() < png.idat_data.len() ||
|
||||||
opts.interlace != Some(png.ihdr_data.interlaced) ||
|
(opts.interlace.is_some() &&
|
||||||
|
opts.interlace != Some(png.ihdr_data.interlaced)) ||
|
||||||
opts.force)) {
|
opts.force)) {
|
||||||
best = Some(ok_result);
|
best = Some(ok_result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
59
src/png.rs
59
src/png.rs
|
|
@ -356,15 +356,16 @@ impl PngData {
|
||||||
unfiltered.push(0);
|
unfiltered.push(0);
|
||||||
match line.filter {
|
match line.filter {
|
||||||
0 => {
|
0 => {
|
||||||
let mut data = line.data.clone();
|
unfiltered.extend_from_slice(&line.data);
|
||||||
last_line = data.clone();
|
|
||||||
unfiltered.append(&mut data);
|
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
let mut data = Vec::with_capacity(line.data.len());
|
let mut data = Vec::with_capacity(line.data.len());
|
||||||
for (i, byte) in line.data.iter().enumerate() {
|
for (i, byte) in line.data.iter().enumerate() {
|
||||||
match i.checked_sub(bpp as usize) {
|
match i.checked_sub(bpp as usize) {
|
||||||
Some(x) => data.push(byte.wrapping_add(line.data[x])),
|
Some(x) => {
|
||||||
|
let b = data[x];
|
||||||
|
data.push(byte.wrapping_add(b))
|
||||||
|
}
|
||||||
None => data.push(*byte),
|
None => data.push(*byte),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -387,17 +388,23 @@ impl PngData {
|
||||||
let mut data = Vec::with_capacity(line.data.len());
|
let mut data = Vec::with_capacity(line.data.len());
|
||||||
for (i, byte) in line.data.iter().enumerate() {
|
for (i, byte) in line.data.iter().enumerate() {
|
||||||
if !last_line.is_empty() {
|
if !last_line.is_empty() {
|
||||||
data.push(match i.checked_sub(bpp as usize) {
|
match i.checked_sub(bpp as usize) {
|
||||||
Some(x) => byte.wrapping_add(
|
Some(x) => {
|
||||||
((line.data[x] as u16 + last_line[i] as u16) >> 1) as u8
|
let b = data[x];
|
||||||
),
|
data.push(byte.wrapping_add(
|
||||||
None => byte.wrapping_add(last_line[i] >> 1),
|
((b as u16 + last_line[i] as u16) >> 1) as u8
|
||||||
});
|
))
|
||||||
|
}
|
||||||
|
None => data.push(byte.wrapping_add(last_line[i] >> 1)),
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
data.push(match i.checked_sub(bpp as usize) {
|
match i.checked_sub(bpp as usize) {
|
||||||
Some(x) => byte.wrapping_add(line.data[x] >> 1),
|
Some(x) => {
|
||||||
None => *byte,
|
let b = data[x];
|
||||||
});
|
data.push(byte.wrapping_add(b >> 1))
|
||||||
|
}
|
||||||
|
None => data.push(*byte),
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
last_line = data.clone();
|
last_line = data.clone();
|
||||||
|
|
@ -407,19 +414,23 @@ impl PngData {
|
||||||
let mut data = Vec::with_capacity(line.data.len());
|
let mut data = Vec::with_capacity(line.data.len());
|
||||||
for (i, byte) in line.data.iter().enumerate() {
|
for (i, byte) in line.data.iter().enumerate() {
|
||||||
if !last_line.is_empty() {
|
if !last_line.is_empty() {
|
||||||
data.push(match i.checked_sub(bpp as usize) {
|
match i.checked_sub(bpp as usize) {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
byte.wrapping_add(paeth_predictor(line.data[x],
|
let b = data[x];
|
||||||
last_line[i],
|
data.push(byte.wrapping_add(paeth_predictor(b,
|
||||||
last_line[x]))
|
last_line[i],
|
||||||
|
last_line[x])))
|
||||||
}
|
}
|
||||||
None => byte.wrapping_add(last_line[i]),
|
None => data.push(byte.wrapping_add(last_line[i])),
|
||||||
});
|
};
|
||||||
} else {
|
} else {
|
||||||
data.push(match i.checked_sub(bpp as usize) {
|
match i.checked_sub(bpp as usize) {
|
||||||
Some(x) => byte.wrapping_add(line.data[x]),
|
Some(x) => {
|
||||||
None => *byte,
|
let b = data[x];
|
||||||
});
|
data.push(byte.wrapping_add(b))
|
||||||
|
}
|
||||||
|
None => data.push(*byte),
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
last_line = data.clone();
|
last_line = data.clone();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue