euv_core/media/gif/struct.rs
1use super::*;
2
3/// Decoding budgets and frame-delay compatibility policy for pure-Rust GIF87a/GIF89a playback.
4#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
5pub struct GifDecodeConfig {
6 /// Maximum allowed RGBA8 logical canvas size in bytes (`width * height * 4`).
7 #[get(type(copy))]
8 pub(crate) max_canvas_bytes: usize,
9 /// Maximum cumulative RGBA8 bytes retained in the bounded pre-decode frame cache.
10 #[get(type(copy))]
11 pub(crate) max_cache_bytes: usize,
12 /// Maximum number of composited frames retained in the bounded pre-decode cache.
13 #[get(type(copy))]
14 pub(crate) max_predecode_frames: usize,
15 /// Minimum non-zero frame delay clamp in milliseconds when `strict_delays == false`.
16 #[get(type(copy))]
17 pub(crate) min_delay_ms: u32,
18 /// Compatibility delay in milliseconds substituted when a frame specifies `0` or `10ms` and `strict_delays == false`.
19 #[get(type(copy))]
20 pub(crate) zero_delay_fallback_ms: u32,
21 /// When `true`, preserves exact raw centisecond delays (`delay_cs * 10`) without browser-compat substitution.
22 #[get(type(copy))]
23 pub(crate) strict_delays: bool,
24}
25
26/// Pixel-space dirty rectangle covering both previous-frame disposal changes and current sub-frame writes.
27#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
28pub struct GifDirtyRect {
29 /// Left pixel coordinate within the logical canvas.
30 #[get(type(copy))]
31 pub(crate) x: u32,
32 /// Top pixel coordinate within the logical canvas.
33 #[get(type(copy))]
34 pub(crate) y: u32,
35 /// Dirty region width in pixels.
36 #[get(type(copy))]
37 pub(crate) width: u32,
38 /// Dirty region height in pixels.
39 #[get(type(copy))]
40 pub(crate) height: u32,
41}
42
43/// Lightweight parsed metadata descriptor for a single compressed GIF frame inside a byte stream.
44#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
45pub struct GifFrameDescriptor {
46 /// Zero-based frame index in stream order.
47 #[get(type(copy))]
48 pub(crate) index: usize,
49 /// Horizontal sub-frame offset inside the logical screen.
50 #[get(type(copy))]
51 pub(crate) left: u16,
52 /// Vertical sub-frame offset inside the logical screen.
53 #[get(type(copy))]
54 pub(crate) top: u16,
55 /// Sub-frame width in pixels.
56 #[get(type(copy))]
57 pub(crate) width: u16,
58 /// Sub-frame height in pixels.
59 #[get(type(copy))]
60 pub(crate) height: u16,
61 /// Whether pixel rows are stored in 4-pass GIF interlaced order.
62 #[get(type(copy))]
63 pub(crate) interlaced: bool,
64 /// Graphic Control Extension disposal method applied after this frame displays.
65 #[get(type(copy))]
66 pub(crate) disposal: GifDisposalMethod,
67 /// Optional transparent palette index preserving underlying canvas pixels.
68 #[get(skip)]
69 pub(crate) transparent_index: Option<u8>,
70 /// Effective display duration in milliseconds after applying `GifDecodeConfig` delay policy.
71 #[get(type(copy))]
72 pub(crate) delay_ms: u32,
73 /// Presentation timestamp from timeline start in seconds.
74 #[get(type(copy))]
75 pub(crate) timestamp_ms: u32,
76 /// Optional `(start, end)` byte range of the frame's Local Color Table inside `GifDecoder::bytes`.
77 #[get(skip)]
78 pub(crate) lct_range: Option<(usize, usize)>,
79 /// Initial LZW minimum code size (`2..=8`).
80 #[get(type(copy))]
81 pub(crate) lzw_min_code_size: u8,
82 /// Byte offset where the first LZW data sub-block length byte begins inside `GifDecoder::bytes`.
83 #[get(type(copy))]
84 pub(crate) lzw_data_start: usize,
85 /// Exclusive byte offset immediately after the `0x00` sub-block terminator.
86 #[get(type(copy))]
87 pub(crate) lzw_data_end: usize,
88}
89
90/// Single fully composited GIF frame accompanied by its disposal-aware dirty rectangle.
91#[derive(Clone, Data, Debug, Default, PartialEq)]
92pub struct GifDecodedFrame {
93 /// Zero-based frame index.
94 #[get(type(copy))]
95 pub(crate) frame_index: usize,
96 /// Full logical-screen composited RGBA8 pixel buffer.
97 pub(crate) image: ImageBuffer,
98 /// Effective display duration in milliseconds.
99 #[get(type(copy))]
100 pub(crate) delay_ms: u32,
101 /// Presentation timestamp in seconds.
102 #[get(type(copy))]
103 pub(crate) timestamp_secs: f32,
104 /// Disposal-aware pixel dirty region covering prior disposal restoration plus current sub-frame pixels.
105 #[get(type(copy))]
106 pub(crate) dirty_rect: GifDirtyRect,
107 /// Disposal method declared for this frame.
108 #[get(type(copy))]
109 pub(crate) disposal: GifDisposalMethod,
110}
111
112/// Pure-Rust GIF87a/GIF89a container parser and indexed LZW frame decoder.
113#[derive(Clone, Data, Debug, Default)]
114pub struct GifDecoder {
115 /// Raw encoded GIF byte stream retained for on-demand bounded decoding.
116 pub(crate) bytes: Vec<u8>,
117 /// Detected GIF header signature (`"GIF87a"` or `"GIF89a"`).
118 pub(crate) version: String,
119 /// Logical screen width in pixels (exact source dimension, never downscaled).
120 #[get(type(copy))]
121 pub(crate) logical_width: u16,
122 /// Logical screen height in pixels (exact source dimension, never downscaled).
123 #[get(type(copy))]
124 pub(crate) logical_height: u16,
125 /// Declared global background color palette index.
126 #[get(type(copy))]
127 pub(crate) background_color_index: u8,
128 /// Parsed Global Color Table RGB triplets (`empty` if none declared).
129 pub(crate) global_color_table: Vec<[u8; 3]>,
130 /// Whether an explicit `NETSCAPE2.0` / `ANIMEXTS1.0` loop extension was parsed.
131 #[get(type(copy))]
132 pub(crate) has_netscape_loop: bool,
133 /// Parsed loop count (`0` = infinite looping, `N > 0` = finite repeat count).
134 #[get(type(copy))]
135 pub(crate) loop_count: u32,
136 /// Indexed frame descriptors parsed from the stream.
137 pub(crate) frames: Vec<GifFrameDescriptor>,
138 /// Active memory and frame-delay compatibility configuration.
139 #[get(type(copy))]
140 pub(crate) config: GifDecodeConfig,
141}
142
143/// Stateful streaming GIF player with reusable composite canvas, bounded pre-decode cache, disposal-aware dirty tracking, and off-screen pause/resume.
144#[derive(Clone, Data, Debug, Default)]
145pub struct GifStreamPlayer {
146 /// Underlying parsed GIF stream decoder.
147 pub(crate) decoder: GifDecoder,
148 /// Reusable full-canvas RGBA8 compositing surface (`logical_width * logical_height * 4`).
149 pub(crate) canvas: Vec<u8>,
150 /// Reusable canvas backup buffer populated only when a frame uses `RestoreToPrevious`.
151 #[get(skip)]
152 #[get_mut(skip)]
153 #[set(skip)]
154 pub(crate) restore_snapshot: Vec<u8>,
155 /// Reusable scratch buffer holding LZW-decoded palette indices for the active frame.
156 #[get(skip)]
157 #[get_mut(skip)]
158 #[set(skip)]
159 pub(crate) index_scratch: Vec<u8>,
160 /// Bounded cache of composited frames capped by `max_predecode_frames` and `max_cache_bytes`.
161 pub(crate) cache: Vec<GifDecodedFrame>,
162 /// Cumulative RGBA8 bytes currently stored in `cache`.
163 #[get(type(copy))]
164 pub(crate) cache_bytes: usize,
165 /// Index of the most recent frame composited directly onto `canvas`.
166 #[get(skip)]
167 pub(crate) last_composited_index: Option<usize>,
168 /// Disposal rectangle left behind by `last_composited_index` (applied when advancing to the next frame).
169 #[get(skip)]
170 pub(crate) pending_disposal_rect: Option<GifDirtyRect>,
171 /// Disposal method of `last_composited_index` pending before the next frame draws.
172 #[get(type(copy))]
173 pub(crate) pending_disposal: GifDisposalMethod,
174 /// Currently displayed frame index.
175 #[get(type(copy))]
176 pub(crate) current_frame: usize,
177 /// Elapsed milliseconds accumulated within `current_frame`.
178 #[get(type(copy))]
179 pub(crate) elapsed_ms: u32,
180 /// Number of full animation loops completed so far.
181 #[get(type(copy))]
182 pub(crate) completed_loops: u32,
183 /// Whether this player is currently visible on screen (`false` suspends decoding and timing).
184 #[get(type(copy))]
185 #[set(skip)]
186 pub(crate) visible: bool,
187 /// Policy controlling resume behavior when transitioning from off-screen back to visible.
188 #[get(type(copy))]
189 pub(crate) visibility_policy: GifVisibilityPolicy,
190 /// Active playback status.
191 #[get(type(copy))]
192 pub(crate) status: GifPlaybackStatus,
193 /// Disposal-aware dirty region reported for the active frame.
194 #[get(type(copy))]
195 pub(crate) last_dirty_rect: GifDirtyRect,
196 /// Number of frames served directly from the bounded pre-decode cache without re-running LZW.
197 #[get(type(copy))]
198 pub(crate) cache_hits: u64,
199 /// Number of frames decoded via LZW and composited onto the reusable canvas.
200 #[get(type(copy))]
201 pub(crate) decode_count: u64,
202}
203
204/// Frame specification consumed by `GifEncoder::encode` to emit valid spec-compliant GIF89a LZW byte streams.
205#[derive(Clone, Data, Debug, Default, PartialEq)]
206pub struct GifEncodeFrameInput {
207 /// Horizontal sub-frame offset inside the logical screen.
208 #[get(type(copy))]
209 pub(crate) left: u16,
210 /// Vertical sub-frame offset inside the logical screen.
211 #[get(type(copy))]
212 pub(crate) top: u16,
213 /// Sub-frame width in pixels.
214 #[get(type(copy))]
215 pub(crate) width: u16,
216 /// Sub-frame height in pixels.
217 #[get(type(copy))]
218 pub(crate) height: u16,
219 /// Emit rows in 4-pass GIF interlaced order.
220 #[get(type(copy))]
221 pub(crate) interlaced: bool,
222 /// Graphic Control Extension disposal method.
223 #[get(type(copy))]
224 pub(crate) disposal: GifDisposalMethod,
225 /// Optional transparent palette index.
226 #[get(skip)]
227 pub(crate) transparent_index: Option<u8>,
228 /// Delay in centiseconds (`1/100 s`).
229 #[get(type(copy))]
230 pub(crate) delay_cs: u16,
231 /// Optional frame-specific Local Color Table RGB triplets.
232 #[get(skip)]
233 pub(crate) local_palette: Option<Vec<[u8; 3]>>,
234 /// Row-major palette indices in visual (non-interlaced) top-to-bottom order of length `width * height`.
235 pub(crate) indices: Vec<u8>,
236}
237
238/// Stateless pure-Rust GIF89a LZW encoder producing spec-compliant animated GIF streams.
239#[derive(Clone, Copy, Debug, Default)]
240pub struct GifEncoder;