-
Notifications
You must be signed in to change notification settings - Fork 132
/
gifsicle.rs
391 lines (364 loc) · 13.2 KB
/
gifsicle.rs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
pub struct GiflossyImage<'data> {
img: &'data [u8],
width: u16,
height: u16,
interlace: bool,
transparent: Option<u8>,
pal: Option<&'data [RGB8]>,
}
use rgb::RGB8;
use crate::Error;
pub type LzwCode = u16;
#[derive(Clone, Copy)]
pub struct GiflossyWriter {
pub loss: u32,
}
struct CodeTable {
pub nodes: Vec<Node>,
pub links_used: usize,
pub clear_code: LzwCode,
}
struct Node {
pub code: LzwCode,
pub suffix: u8,
pub children: NodeChild,
}
enum NodeChild {
/// This emulates old quirks to keep bit-exactly same output
ThisUsedToBeALinkedList(Vec<Node>),
Table(Box<[Option<Node>]>),
}
type RgbDiff = rgb::RGB<i16>;
#[inline]
fn color_diff(a: RGB8, b: RGB8, a_transparent: bool, b_transparent: bool, dither: RgbDiff) -> u32 {
if a_transparent != b_transparent {
return (1 << 25) as u32;
}
if a_transparent {
return 0;
}
let dith =
((i32::from(a.r) - i32::from(b.r) + i32::from(dither.r)) * (i32::from(a.r) - i32::from(b.r) + i32::from(dither.r))
+ (i32::from(a.g) - i32::from(b.g) + i32::from(dither.g)) * (i32::from(a.g) - i32::from(b.g) + i32::from(dither.g))
+ (i32::from(a.b) - i32::from(b.b) + i32::from(dither.b)) * (i32::from(a.b) - i32::from(b.b) + i32::from(dither.b))) as u32;
let undith =
((i32::from(a.r) - i32::from(b.r) + i32::from(dither.r) / 2) * (i32::from(a.r) - i32::from(b.r) + i32::from(dither.r) / 2)
+ (i32::from(a.g) - i32::from(b.g) + i32::from(dither.g) / 2) * (i32::from(a.g) - i32::from(b.g) + i32::from(dither.g) / 2)
+ (i32::from(a.b) - i32::from(b.b) + i32::from(dither.b) / 2) * (i32::from(a.b) - i32::from(b.b) + i32::from(dither.b) / 2)) as u32;
if dith < undith {
dith
} else {
undith
}
}
#[inline]
fn diffused_difference(
a: RGB8,
b: RGB8,
a_transparent: bool,
b_transparent: bool,
dither: RgbDiff,
) -> RgbDiff {
if a_transparent || b_transparent {
RgbDiff { r: 0, g: 0, b: 0 }
} else {
RgbDiff {
r: (i32::from(a.r) - i32::from(b.r) + i32::from(dither.r) * 3 / 4) as i16,
g: (i32::from(a.g) - i32::from(b.g) + i32::from(dither.g) * 3 / 4) as i16,
b: (i32::from(a.b) - i32::from(b.b) + i32::from(dither.b) * 3 / 4) as i16,
}
}
}
impl CodeTable {
#[inline]
fn define(&mut self, work_node: &mut Node, suffix: u8, next_code: LzwCode) {
let next_node = Node {
code: next_code,
suffix,
children: NodeChild::ThisUsedToBeALinkedList(Vec::new()),
};
match &mut work_node.children {
NodeChild::ThisUsedToBeALinkedList(list) if list.len() < 4 || self.links_used + self.clear_code as usize > 0x1000 => {
if list.is_empty() {
list.reserve_exact(4);
}
list.push(next_node);
},
NodeChild::Table(table) => {
table[suffix as usize] = Some(next_node);
},
NodeChild::ThisUsedToBeALinkedList(list) => {
self.links_used += self.clear_code as usize;
let mut table = Vec::with_capacity(self.clear_code as usize);
debug_assert_eq!(table.capacity(), self.clear_code as usize);
table.resize_with(self.clear_code as usize, || None);
let idx = next_node.suffix as usize;
table[idx] = Some(next_node);
for node in list.drain(..) {
let idx = node.suffix as usize;
table[idx] = Some(node);
}
work_node.children = NodeChild::Table(table.into_boxed_slice());
},
};
}
#[cold]
fn reset(&mut self) {
self.links_used = 0;
self.nodes.clear();
self.nodes.extend((0..usize::from(self.clear_code)).map(|i| Node {
code: i as u16,
suffix: i as u8,
children: NodeChild::ThisUsedToBeALinkedList(Vec::new()),
}));
}
}
struct Lookup<'a> {
pub pal: &'a [RGB8],
pub image: &'a GiflossyImage<'a>,
pub max_diff: u32,
pub best_node: *mut Node,
pub best_pos: usize,
pub best_total_diff: u64,
}
impl<'a> Lookup<'a> {
pub fn lossy_node(&mut self, pos: usize, node: &mut Node, total_diff: u64, dither: RgbDiff) {
let Some(px) = self.image.px_at_pos(pos) else {
return;
};
match &mut node.children {
NodeChild::ThisUsedToBeALinkedList(table) => {
table.iter_mut().rev().for_each(|node| {
self.try_node(
pos,
node,
px,
node.suffix,
dither,
total_diff,
);
});
},
NodeChild::Table(table) => {
table.iter_mut().for_each(|node| {
if let Some(node) = node.as_mut() {
self.try_node(
pos,
node,
px,
node.suffix,
dither,
total_diff,
);
}
});
},
}
}
#[inline]
fn try_node(
&mut self,
pos: usize,
node: &mut Node,
px: u8,
next_px: u8,
dither: RgbDiff,
total_diff: u64,
) {
let diff = if px == next_px {
0
} else {
color_diff(
self.pal[px as usize],
self.pal[next_px as usize],
Some(px) == self.image.transparent,
Some(next_px) == self.image.transparent,
dither,
)
};
if diff <= self.max_diff {
let new_dither = diffused_difference(
self.pal[px as usize],
self.pal[next_px as usize],
Some(px) == self.image.transparent,
Some(next_px) == self.image.transparent,
dither,
);
let new_pos = pos + 1;
let new_diff = total_diff + u64::from(diff);
if new_pos > self.best_pos || new_pos == self.best_pos && new_diff < self.best_total_diff {
self.best_node = node;
self.best_pos = new_pos;
self.best_total_diff = new_diff;
}
self.lossy_node(
new_pos,
node,
new_diff,
new_dither,
);
}
}
}
const RUN_EWMA_SHIFT: usize = 4;
const RUN_EWMA_SCALE: usize = 19;
const RUN_INV_THRESH: usize = (1 << RUN_EWMA_SCALE) / 3000;
impl GiflossyWriter {
pub fn write(&mut self, image: &GiflossyImage, global_pal: Option<&[RGB8]>) -> Result<Vec<u8>, Error> {
let mut buf = Vec::new();
buf.try_reserve((image.height as usize * image.width as usize / 4).next_power_of_two())?;
let mut run = 0;
let mut run_ewma = 0;
let mut next_code = 0;
let pal = image.pal.or(global_pal).unwrap();
let min_code_size = (pal.len() as u32).max(3).next_power_of_two().trailing_zeros() as u8;
buf.push(min_code_size);
let mut bufpos_bits = 8;
let mut code_table = CodeTable {
clear_code: 1 << u16::from(min_code_size),
links_used: 0,
nodes: Vec::new(),
};
code_table.reset();
let mut cur_code_bits = min_code_size + 1;
let mut output_code = code_table.clear_code as LzwCode;
let mut clear_bufpos_bits = bufpos_bits;
let mut pos = 0;
let mut clear_pos = pos;
loop {
let endpos_bits = bufpos_bits + (cur_code_bits as usize);
loop {
if bufpos_bits & 7 != 0 {
buf[bufpos_bits / 8] |= (output_code << (bufpos_bits & 7)) as u8;
} else {
buf.push((output_code >> (bufpos_bits + (cur_code_bits as usize) - endpos_bits)) as u8);
}
bufpos_bits = bufpos_bits + 8 - (bufpos_bits & 7);
if bufpos_bits >= endpos_bits {
break;
}
}
bufpos_bits = endpos_bits;
if output_code == code_table.clear_code {
cur_code_bits = min_code_size + 1;
next_code = (code_table.clear_code + 2) as LzwCode;
run_ewma = 1 << RUN_EWMA_SCALE;
code_table.reset();
clear_bufpos_bits = 0;
clear_pos = clear_bufpos_bits;
} else {
if output_code == (code_table.clear_code + 1) {
break;
}
if next_code > (1 << cur_code_bits) && cur_code_bits < 12 {
cur_code_bits += 1;
}
run = (((run as u32) << RUN_EWMA_SCALE) + (1 << (RUN_EWMA_SHIFT - 1) as u32)) as usize;
if run < run_ewma {
run_ewma = run_ewma - ((run_ewma - run) >> RUN_EWMA_SHIFT);
} else {
run_ewma = run_ewma + ((run - run_ewma) >> RUN_EWMA_SHIFT);
}
}
if let Some(px) = image.px_at_pos(pos) {
let mut l = Lookup {
pal,
image,
max_diff: self.loss,
best_node: &mut code_table.nodes[px as usize],
best_pos: pos + 1,
best_total_diff: 0,
};
l.lossy_node(pos + 1, &mut code_table.nodes[px as usize], 0, RgbDiff { r: 0, g: 0, b: 0 }, );
let selected_node = unsafe { &mut * l.best_node };
run = l.best_pos - pos;
pos = l.best_pos;
output_code = selected_node.code;
if let Some(px) = image.px_at_pos(pos) {
if next_code < 0x1000 {
code_table.define(selected_node, px, next_code);
next_code += 1;
} else {
next_code = 0x1001;
}
if next_code >= 0x0FFF {
let pixels_left = image.img.len() - pos - 1;
let do_clear = pixels_left != 0
&& (run_ewma
< (36 << RUN_EWMA_SCALE) / (min_code_size as usize)
|| pixels_left > (0x7FFF_FFFF * 2 + 1) / RUN_INV_THRESH
|| run_ewma < pixels_left * RUN_INV_THRESH);
if (do_clear || run < 7) && clear_pos == 0 {
clear_pos = pos - run;
clear_bufpos_bits = bufpos_bits;
} else if !do_clear && run > 50 {
clear_bufpos_bits = 8; // buf contains min code
clear_pos = 0;
}
if do_clear {
output_code = code_table.clear_code;
pos = clear_pos;
bufpos_bits = clear_bufpos_bits;
buf.truncate((bufpos_bits + 7) / 8);
if buf.len() > bufpos_bits / 8 {
buf[bufpos_bits / 8] &= (1 << (bufpos_bits & 7)) - 1;
}
continue;
}
}
run = (((run as u32) << RUN_EWMA_SCALE) + (1 << (RUN_EWMA_SHIFT - 1) as u32)) as usize;
if run < run_ewma {
run_ewma = run_ewma - ((run_ewma - run) >> RUN_EWMA_SHIFT);
} else {
run_ewma = run_ewma + ((run - run_ewma) >> RUN_EWMA_SHIFT);
}
}
} else {
run = 0;
output_code = code_table.clear_code + 1;
};
}
Ok(buf)
}
}
impl<'a> GiflossyImage<'a> {
#[must_use]
pub fn new(
img: &'a [u8],
width: u16,
height: u16,
transparent: Option<u8>,
pal: Option<&'a [RGB8]>,
) -> Self {
assert_eq!(img.len(), width as usize * height as usize);
GiflossyImage {
img,
width,
height,
interlace: false,
transparent,
pal,
}
}
#[inline]
fn px_at_pos(&self, pos: usize) -> Option<u8> {
if !self.interlace {
self.img.get(pos).copied()
} else {
let y = pos / self.width as usize;
let x = pos - (y * self.width as usize);
self.img.get(self.width as usize * interlaced_line(y, self.height as usize) + x).copied()
}
}
}
fn interlaced_line(line: usize, height: usize) -> usize {
if line > height / 2 {
line * 2 - (height | 1)
} else if line > height / 4 {
return line * (4) - (height & !(1) | 2);
} else if line > height / 8 {
return line * (8) - (height & !(3) | 4);
} else {
return line * 8;
}
}