Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
305 lines (290 loc) · 13.5 KB
/
Copy pathlib.rs
File metadata and controls
305 lines (290 loc) · 13.5 KB
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
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
//! GIF reading and writing. The block reader reads without decompressing pixels (GIF89a
//! specification: Logical Screen Descriptor, Global and Local Color Tables, Graphic Control
//! Extension, Application Extension, Image Descriptor).
//!
//! [`scan`] walks the file once and reports what an importer needs before it decodes anything:
//! the logical screen size, every frame's position, size, delay and transparent colour, whether
//! the file reached its trailer, and the loop count. That answers "is this an animation, how
//! long is it, how many frames" and "can a file this small hold a picture this large" from the
//! headers alone. [`palette`] gives the colour table of the first frame, for opening a GIF as an
//! indexed image.
//!
//! With feature `frames`, [`frames::read_frames`] decodes every frame through the `image` crate's
//! GIF decoder, after the checks [`scan`] makes possible, with bounded memory.
//! [`write::animation`] writes full-canvas RGBA8 animation frames with GIF palette quantization,
//! timing, loop and disposal metadata. Parallel producers can quantize frames with
//! [`write::prepare_frame`] and pass the compact results to
//! [`write::AnimationWriter::write_prepared_frame`].
//!
//! Input is treated as hostile: every read is bounds-checked, and the number of frames recorded
//! is capped by the caller.
#[cfg(feature = "frames")]
pub mod frames;
#[cfg(test)]
mod tests;
/// GIF image and animation encoding.
pub mod write;
/// One frame (image descriptor) of a GIF.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Frame {
/// File offset of the image descriptor (its `0x2C` separator).
pub offset: usize,
/// Position of the frame on the logical screen.
pub left: u16,
pub top: u16,
pub width: u16,
pub height: u16,
/// Delay before the next frame in hundredths of a second, as stored (0 when the frame has no
/// Graphic Control Extension). Players show very small delays longer; that is the caller's rule.
pub delay_cs: u16,
/// The transparent colour index, when the frame's Graphic Control Extension names one.
pub transparent: Option<u8>,
/// Disposal method (0–7) from the Graphic Control Extension.
pub disposal: u8,
/// Whether the frame has its own colour table.
pub local_table: bool,
}
/// What the blocks of a GIF say.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Info {
/// Logical screen size.
pub width: u16,
pub height: u16,
/// Whether the file has a Global Color Table.
pub global_table: bool,
/// The frames found, in file order, up to the caller's limit.
pub frames: Vec<Frame>,
/// Whether the walk reached the trailer. When it did not (the file is cut short or damaged, or
/// the frame limit was reached), more frames may exist than [`Info::frames`] holds.
pub complete: bool,
/// The NETSCAPE2.0 loop count: `Some(0)` loops forever, `None` when the file has none (play once).
pub loop_count: Option<u16>,
}
impl Info {
/// Whether the file is known to hold more than one frame, or `None` when the walk stopped
/// before the trailer after at most one frame, so frames past the break could not be counted.
pub fn has_more_frames(&self) -> Option<bool> {
if self.frames.len() > 1 { Some(true) } else { self.complete.then_some(false) }
}
/// The sum of the frame delays in hundredths of a second, as stored.
pub fn total_delay_cs(&self) -> u64 {
self.frames.iter().map(|f| u64::from(f.delay_cs)).sum()
}
}
/// Whether `bytes` starts with a GIF signature.
pub fn sniff(bytes: &[u8]) -> bool {
bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a")
}
/// Bytes of the colour table a packed-fields byte announces (0 without one).
fn table_len(packed: u8) -> usize {
if packed & 0x80 != 0 { 3usize << ((packed & 7) + 1) } else { 0 }
}
fn u16_at(b: &[u8], at: usize) -> Option<u16> {
Some(u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?))
}
/// Skips the data sub-blocks starting at `at` (each length-prefixed, ended by a zero length).
fn skip_sub_blocks(b: &[u8], mut at: usize) -> Option<usize> {
loop {
let n = usize::from(*b.get(at)?);
at = at.checked_add(1 + n)?;
if n == 0 {
return Some(at);
}
}
}
/// Walks the blocks of a GIF without decompressing any, recording at most `max_frames` frames.
/// `None` when `bytes` is not a GIF or ends inside its Logical Screen Descriptor. A file that
/// breaks off later gives what was read, with [`Info::complete`] false.
pub fn scan(bytes: &[u8], max_frames: usize) -> Option<Info> {
let b = bytes;
if !sniff(b) {
return None;
}
// Logical Screen Descriptor: width, height, packed fields, background, aspect.
let (width, height, packed) = (u16_at(b, 6)?, u16_at(b, 8)?, *b.get(10)?);
b.get(12)?;
let mut info = Info { width, height, global_table: packed & 0x80 != 0, frames: Vec::new(), complete: false, loop_count: None };
let mut at = 13 + table_len(packed);
// What the last Graphic Control Extension said, for the next image.
let (mut delay_cs, mut transparent, mut disposal) = (0u16, None, 0u8);
// Each step either finishes the walk or moves `at` forward, so this ends with the file.
let mut step = || -> Option<bool> {
match *b.get(at)? {
// Extension: label, then data sub-blocks up to a zero-length one.
0x21 => {
let label = *b.get(at + 1)?;
let body = at + 2;
// Graphic Control Extension: block size 4, packed (bit 0: transparent colour
// given; bits 2–4: disposal), delay (2 bytes), transparent colour index.
if label == 0xF9 && *b.get(body)? == 4 {
let flags = *b.get(body + 1)?;
delay_cs = u16_at(b, body + 2)?;
transparent = (flags & 0x01 != 0).then_some(*b.get(body + 4)?);
disposal = (flags >> 2) & 7;
}
// Application Extension NETSCAPE2.0 (or ANIMEXTS1.0): sub-block 3, 1, loop count.
if label == 0xFF
&& matches!(b.get(body..body + 12), Some([11, id @ ..]) if id == b"NETSCAPE2.0" || id == b"ANIMEXTS1.0")
&& b.get(body + 12..body + 14) == Some(&[3, 1])
{
info.loop_count = u16_at(b, body + 14);
}
at = skip_sub_blocks(b, body)?;
Some(true)
}
// Image Descriptor: separator, left, top, width, height, packed fields, then the
// Local Color Table if bit 7 is set, the LZW code size and the data sub-blocks.
0x2C => {
if info.frames.len() >= max_frames {
return None;
}
let packed = *b.get(at + 9)?;
info.frames.push(Frame {
offset: at,
left: u16_at(b, at + 1)?,
top: u16_at(b, at + 3)?,
width: u16_at(b, at + 5)?,
height: u16_at(b, at + 7)?,
delay_cs,
transparent,
disposal,
local_table: packed & 0x80 != 0,
});
(delay_cs, transparent, disposal) = (0, None, 0);
at = skip_sub_blocks(b, (at + 10).checked_add(table_len(packed))?.checked_add(1)?)?;
Some(true)
}
// Trailer.
0x3B => {
info.complete = true;
Some(false)
}
// Anything else: the blocks break off here.
_ => None,
}
};
while step() == Some(true) {}
Some(info)
}
/// The colours of the table the first frame uses (its Local Color Table when it has one, else
/// the Global one) and its transparent entry. `None` for anything that is not a well-formed GIF
/// up to its first image, or a first frame without a table. A transparent index past the table
/// is no transparent entry.
pub fn palette(bytes: &[u8]) -> Option<(Vec<[u8; 3]>, Option<u8>)> {
let info = scan(bytes, 1)?;
let first = info.frames.first()?;
let (at, packed) = if first.local_table { (first.offset + 10, *bytes.get(first.offset + 9)?) } else { (13, *bytes.get(10)?) };
let colors = bytes.get(at..at.checked_add(table_len(packed))?)?.as_chunks::<3>().0.to_vec();
if colors.is_empty() {
return None;
}
let transparent = first.transparent.filter(|i| usize::from(*i) < colors.len());
Some((colors, transparent))
}
/// Counts pixels emitted by the first GIF frame's LZW stream, up to its declared area.
/// Returns `None` when the first frame or its LZW minimum code size is unreadable. This reads
/// code lengths only; it does not allocate or materialize pixel values.
pub fn first_frame_decoded_pixels(bytes: &[u8]) -> Option<u64> {
let parsed = scan(bytes, 1)?;
let frame = parsed.frames.first()?;
let packed = *bytes.get(frame.offset.checked_add(9)?)?;
let table_bytes = table_len(packed);
let code_size_at = frame.offset.checked_add(10)?.checked_add(table_bytes)?;
let min_size = *bytes.get(code_size_at)?;
if !(1..=11).contains(&min_size) {
return None;
}
let clear = 1usize << min_size;
let end = clear + 1;
let max = u64::from(frame.width) * u64::from(frame.height);
let mut lengths = [1u16; 4096];
let (mut width, mut next, mut previous) = (u32::from(min_size) + 1, end + 1, None::<usize>);
let (mut bits, mut held, mut pixels) = (0u32, 0u32, 0u64);
let mut pos = code_size_at.checked_add(1)?;
while let Some(&length) = bytes.get(pos) {
let start = pos.checked_add(1)?;
pos = start.checked_add(usize::from(length))?;
if length == 0 {
break;
}
for &byte in bytes.get(start..pos.min(bytes.len()))? {
bits |= u32::from(byte) << held;
held += 8;
while held >= width {
let code = (bits & ((1 << width) - 1)) as usize;
bits >>= width;
held -= width;
if code == clear {
(width, next, previous) = (u32::from(min_size) + 1, end + 1, None);
continue;
}
let string_len = match previous {
_ if code == end => return Some(pixels),
None if code < clear => 1,
Some(_) if code < next => lengths.get(code).copied().unwrap_or(1),
Some(prev) if code == next => lengths.get(prev).copied().unwrap_or(1).saturating_add(1),
_ => return Some(pixels),
};
let defined_len = previous.map(|prev| lengths.get(prev).copied().unwrap_or(1).saturating_add(1));
if let Some(defined_len) = defined_len
&& let Some(slot) = lengths.get_mut(next)
{
*slot = defined_len;
next += 1;
if next == 1 << width && width < 12 {
width += 1;
}
}
previous = Some(code);
pixels = pixels.saturating_add(u64::from(string_len));
if pixels >= max {
return Some(max);
}
}
}
}
Some(pixels)
}
/// Counts compressed image-data bytes in the first frame's sub-blocks, excluding its LZW code-size
/// byte and all later blocks. A truncated final sub-block contributes only the bytes present.
pub fn first_frame_compressed_bytes(bytes: &[u8]) -> Option<usize> {
let parsed = scan(bytes, 1)?;
let frame = parsed.frames.first()?;
let packed = *bytes.get(frame.offset.checked_add(9)?)?;
let mut pos = frame.offset.checked_add(10)?.checked_add(table_len(packed))?.checked_add(1)?;
let mut total = 0usize;
loop {
let length = usize::from(*bytes.get(pos)?);
pos = pos.checked_add(1)?;
if length == 0 {
return Some(total);
}
let end = pos.checked_add(length)?;
total = total.checked_add(end.min(bytes.len()).saturating_sub(pos))?;
if end > bytes.len() {
return Some(total);
}
pos = end;
}
}
/// Whether a file of `file_len` bytes can plausibly hold `declared_bytes` of decoded picture: at
/// most `max_ratio` decoded bytes for each file byte, plus 1 MiB. A decoder that sets up the whole
/// logical screen before it reads a frame should be given only files that pass this.
pub fn plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> bool {
declared_bytes <= (file_len as u64).saturating_mul(max_ratio).saturating_add(1 << 20)
}
impl Info {
/// The decoded bytes the file must plausibly back at `bytes_per_pixel`: the first frame, and
/// whatever of the logical screen exceeds `unbacked_canvas` bytes (the screen around the first
/// frame needs no data, so that much of it is free). Use with [`plausible`].
pub fn bytes_to_back(&self, bytes_per_pixel: u64, unbacked_canvas: u64) -> u64 {
let screen = u64::from(self.width).saturating_mul(u64::from(self.height)).saturating_mul(bytes_per_pixel);
match self.frames.first() {
Some(f) => {
u64::from(f.width).saturating_mul(u64::from(f.height)).saturating_mul(bytes_per_pixel).max(screen.saturating_sub(unbacked_canvas))
}
None => screen,
}
}
}