1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use std::{fmt, fmt::Display, mem::transmute};
/// Filtering strategy for use in [`Options`][crate::Options]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub enum FilterStrategy {
/// Same filter for all rows
Basic(RowFilter),
/// Minimum sum of absolute differences
MinSum,
/// Shannon entropy
Entropy,
/// Count of distinct bigrams
Bigrams,
/// Shannon entropy of bigrams
BigEnt,
/// Deflate compression
Brute {
/// The number of lines to compress at once
num_lines: usize,
/// The compression level to use (1-12)
level: u8,
},
/// Predefined filter for each row
Predefined(Vec<RowFilter>),
}
impl FilterStrategy {
pub const NONE: Self = Self::Basic(RowFilter::None);
pub const SUB: Self = Self::Basic(RowFilter::Sub);
pub const UP: Self = Self::Basic(RowFilter::Up);
pub const AVERAGE: Self = Self::Basic(RowFilter::Average);
pub const PAETH: Self = Self::Basic(RowFilter::Paeth);
}
impl Display for FilterStrategy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Basic(filter) => filter.fmt(f),
Self::MinSum => "MinSum".fmt(f),
Self::Entropy => "Entropy".fmt(f),
Self::Bigrams => "Bigrams".fmt(f),
Self::BigEnt => "BigEnt".fmt(f),
Self::Brute { .. } => "Brute".fmt(f),
Self::Predefined(_) => "Predefined".fmt(f),
}
}
}
/// PNG delta filters
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum RowFilter {
None,
Sub,
Up,
Average,
Paeth,
}
impl TryFrom<u8> for RowFilter {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
if value > 4 {
return Err(());
}
unsafe { transmute(value as i8) }
}
}
impl Display for RowFilter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(
match self {
Self::None => "None",
Self::Sub => "Sub",
Self::Up => "Up",
Self::Average => "Average",
Self::Paeth => "Paeth",
},
f,
)
}
}
impl RowFilter {
pub(crate) const ALL: [Self; 5] = [Self::None, Self::Sub, Self::Up, Self::Average, Self::Paeth];
pub(crate) const SINGLE_LINE: [Self; 2] = [Self::None, Self::Sub];
pub(crate) fn filter_line(
self,
bpp: usize,
data: &mut [u8],
prev_line: &[u8],
buf: &mut Vec<u8>,
alpha_bytes: usize,
) {
assert!(data.len() >= bpp);
assert_eq!(data.len(), prev_line.len());
if alpha_bytes != 0 {
self.optimize_alpha(bpp, data, prev_line, bpp - alpha_bytes);
}
buf.clear();
buf.reserve(data.len() + 1);
buf.push(self as u8);
match self {
Self::None => {
buf.extend_from_slice(data);
}
Self::Sub => {
buf.extend_from_slice(&data[0..bpp]);
buf.extend(
data.iter()
.skip(bpp)
.zip(data.iter())
.map(|(cur, last)| cur.wrapping_sub(*last)),
);
}
Self::Up => {
buf.extend(
data.iter()
.zip(prev_line.iter())
.map(|(cur, last)| cur.wrapping_sub(*last)),
);
}
Self::Average => {
for (i, byte) in data.iter().enumerate() {
buf.push(byte.wrapping_sub(i.checked_sub(bpp).map_or_else(
|| prev_line[i] >> 1,
|x| ((u16::from(data[x]) + u16::from(prev_line[i])) >> 1) as u8,
)));
}
}
Self::Paeth => {
for (i, byte) in data.iter().enumerate() {
buf.push(byte.wrapping_sub(i.checked_sub(bpp).map_or_else(
|| prev_line[i],
|x| paeth_predictor(data[x], prev_line[i], prev_line[x]),
)));
}
}
}
}
// Optimize fully transparent pixels of a scanline such that they will be zeroed when filtered
fn optimize_alpha(self, bpp: usize, data: &mut [u8], prev_line: &[u8], color_bytes: usize) {
if self == Self::None {
// Assume transparent pixels already set to 0
return;
}
let mut pixels: Vec<_> = data.chunks_exact_mut(bpp).collect();
let prev_pixels: Vec<_> = prev_line.chunks_exact(bpp).collect();
for i in 0..pixels.len() {
if pixels[i].iter().skip(color_bytes).all(|b| *b == 0) {
// If the first pixel in the row is transparent, find the next non-transparent pixel and pretend
// it is the previous one. This can help improve effectiveness of the Sub and Paeth filters.
let prev = match i {
0 => pixels
.iter()
.position(|px| px.iter().skip(color_bytes).any(|b| *b != 0))
.unwrap_or(i),
_ => i - 1,
};
// These assertions help eliminate a few bounds checks in the slice accesses below
assert!(prev < pixels.len());
assert!(i < prev_pixels.len());
match self {
Self::None => unreachable!(),
Self::Sub => {
// The code below is roughly equivalent to pixels[i][0..color_bytes].copy_from_slice(&pixels[prev][0..color_bytes]),
// if such a thing was possible to do without violating Rust aliasing rules. See:
// https://users.rust-lang.org/t/problem-borrowing-two-elements-of-vec-mutably/21446/2
if prev < i {
let (pixels_head, pixels_tail) = pixels.split_at_mut(prev + 1);
pixels_tail[i - prev - 1][0..color_bytes]
.copy_from_slice(&pixels_head[prev][0..color_bytes]);
} else if prev > i {
let (pixels_head, pixels_tail) = pixels.split_at_mut(i + 1);
pixels_head[i][0..color_bytes]
.copy_from_slice(&pixels_tail[prev - i - 1][0..color_bytes]);
} else {
// If prev == i, we'd be copying the pixels onto themselves, which is useless
}
}
Self::Up => {
pixels[i][0..color_bytes].copy_from_slice(&prev_pixels[i][0..color_bytes]);
}
Self::Average => {
for j in 0..color_bytes {
pixels[i][j] = match i {
0 => prev_pixels[i][j] >> 1,
_ => {
((u16::from(pixels[i - 1][j]) + u16::from(prev_pixels[i][j]))
>> 1) as u8
}
};
}
}
Self::Paeth => {
for j in 0..color_bytes {
pixels[i][j] = match i {
0 => pixels[prev][j].min(prev_pixels[i][j]),
_ => paeth_predictor(
pixels[i - 1][j],
prev_pixels[i][j],
prev_pixels[i - 1][j],
),
};
}
}
}
}
}
}
pub(crate) fn unfilter_line(
self,
bpp: usize,
data: &[u8],
prev_line: &[u8],
buf: &mut Vec<u8>,
) {
buf.clear();
buf.reserve(data.len());
assert!(data.len() >= bpp);
assert_eq!(data.len(), prev_line.len());
match self {
Self::None => {
buf.extend_from_slice(data);
}
Self::Sub => {
for (i, &cur) in data.iter().enumerate() {
let prev_byte = i.checked_sub(bpp).and_then(|x| buf.get(x).copied());
buf.push(prev_byte.map_or(cur, |b| cur.wrapping_add(b)));
}
}
Self::Up => {
buf.extend(
data.iter()
.zip(prev_line)
.map(|(&cur, &last)| cur.wrapping_add(last)),
);
}
Self::Average => {
for (i, (&cur, &last)) in data.iter().zip(prev_line).enumerate() {
let prev_byte = i.checked_sub(bpp).and_then(|x| buf.get(x).copied());
buf.push(cur.wrapping_add(prev_byte.map_or_else(
|| last >> 1,
|b| ((u16::from(b) + u16::from(last)) >> 1) as u8,
)));
}
}
Self::Paeth => {
for (i, (&cur, &up)) in data.iter().zip(prev_line).enumerate() {
buf.push(
match i
.checked_sub(bpp)
.map(|x| (buf.get(x).copied(), prev_line.get(x).copied()))
{
Some((Some(left), Some(left_up))) => {
cur.wrapping_add(paeth_predictor(left, up, left_up))
}
_ => cur.wrapping_add(up),
},
);
}
}
}
}
}
fn paeth_predictor(a: u8, b: u8, c: u8) -> u8 {
let p = i32::from(a) + i32::from(b) - i32::from(c);
let pa = (p - i32::from(a)).abs();
let pb = (p - i32::from(b)).abs();
let pc = (p - i32::from(c)).abs();
if pa <= pb && pa <= pc {
a
} else if pb <= pc {
b
} else {
c
}
}