Skip to main content

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 shared via `Arc<[u8]>` for O(1) zero-copy cloning.
116    #[get(skip)]
117    #[get_mut(skip)]
118    #[set(skip)]
119    pub(crate) bytes: Arc<[u8]>,
120    /// Detected GIF header signature (`"GIF87a"` or `"GIF89a"`).
121    pub(crate) version: String,
122    /// Logical screen width in pixels (exact source dimension, never downscaled).
123    #[get(type(copy))]
124    pub(crate) logical_width: u16,
125    /// Logical screen height in pixels (exact source dimension, never downscaled).
126    #[get(type(copy))]
127    pub(crate) logical_height: u16,
128    /// Declared global background color palette index.
129    #[get(type(copy))]
130    pub(crate) background_color_index: u8,
131    /// Parsed Global Color Table RGB triplets (`empty` if none declared).
132    pub(crate) global_color_table: Vec<[u8; 3]>,
133    /// Whether an explicit `NETSCAPE2.0` / `ANIMEXTS1.0` loop extension was parsed.
134    #[get(type(copy))]
135    pub(crate) has_netscape_loop: bool,
136    /// Parsed loop count (`0` = infinite looping, `N > 0` = finite repeat count).
137    #[get(type(copy))]
138    pub(crate) loop_count: u32,
139    /// Indexed frame descriptors parsed from the stream.
140    pub(crate) frames: Vec<GifFrameDescriptor>,
141    /// Active memory and frame-delay compatibility configuration.
142    #[get(type(copy))]
143    pub(crate) config: GifDecodeConfig,
144}
145
146/// Stateful streaming GIF player with reusable composite canvas, bounded pre-decode cache, disposal-aware dirty tracking, and off-screen pause/resume.
147#[derive(Clone, Data, Debug, Default)]
148pub struct GifStreamPlayer {
149    /// Underlying parsed GIF stream decoder.
150    pub(crate) decoder: GifDecoder,
151    /// Reusable full-canvas RGBA8 compositing surface (`logical_width * logical_height * 4`).
152    pub(crate) canvas: Vec<u8>,
153    /// Reusable canvas backup buffer populated only when a frame uses `RestoreToPrevious`.
154    #[get(skip)]
155    #[get_mut(skip)]
156    #[set(skip)]
157    pub(crate) restore_snapshot: Vec<u8>,
158    /// Reusable scratch buffer holding LZW-decoded palette indices for the active frame.
159    #[get(skip)]
160    #[get_mut(skip)]
161    #[set(skip)]
162    pub(crate) index_scratch: Vec<u8>,
163    /// Bounded LRU cache of composited frames capped by `max_predecode_frames` and `max_cache_bytes`.
164    pub(crate) cache: VecDeque<GifDecodedFrame>,
165    /// Cumulative RGBA8 bytes currently stored in `cache`.
166    #[get(type(copy))]
167    pub(crate) cache_bytes: usize,
168    /// Index of the most recent frame composited directly onto `canvas`.
169    #[get(skip)]
170    pub(crate) last_composited_index: Option<usize>,
171    /// Disposal rectangle left behind by `last_composited_index` (applied when advancing to the next frame).
172    #[get(skip)]
173    pub(crate) pending_disposal_rect: Option<GifDirtyRect>,
174    /// Disposal method of `last_composited_index` pending before the next frame draws.
175    #[get(type(copy))]
176    pub(crate) pending_disposal: GifDisposalMethod,
177    /// Currently displayed frame index.
178    #[get(type(copy))]
179    pub(crate) current_frame: usize,
180    /// Elapsed milliseconds accumulated within `current_frame`.
181    #[get(type(copy))]
182    pub(crate) elapsed_ms: u32,
183    /// Number of full animation loops completed so far.
184    #[get(type(copy))]
185    pub(crate) completed_loops: u32,
186    /// Whether this player is currently visible on screen (`false` suspends decoding and timing).
187    #[get(type(copy))]
188    #[set(skip)]
189    pub(crate) visible: bool,
190    /// Policy controlling resume behavior when transitioning from off-screen back to visible.
191    #[get(type(copy))]
192    pub(crate) visibility_policy: GifVisibilityPolicy,
193    /// Active playback status.
194    #[get(type(copy))]
195    pub(crate) status: GifPlaybackStatus,
196    /// Disposal-aware dirty region reported for the active frame.
197    #[get(type(copy))]
198    pub(crate) last_dirty_rect: GifDirtyRect,
199    /// Number of frames served directly from the bounded pre-decode cache without re-running LZW.
200    #[get(type(copy))]
201    pub(crate) cache_hits: u64,
202    /// Number of frames decoded via LZW and composited onto the reusable canvas.
203    #[get(type(copy))]
204    pub(crate) decode_count: u64,
205}
206
207/// Frame specification consumed by `GifEncoder::encode` to emit valid spec-compliant GIF89a LZW byte streams.
208#[derive(Clone, Data, Debug, Default, PartialEq)]
209pub struct GifEncodeFrameInput {
210    /// Horizontal sub-frame offset inside the logical screen.
211    #[get(type(copy))]
212    pub(crate) left: u16,
213    /// Vertical sub-frame offset inside the logical screen.
214    #[get(type(copy))]
215    pub(crate) top: u16,
216    /// Sub-frame width in pixels.
217    #[get(type(copy))]
218    pub(crate) width: u16,
219    /// Sub-frame height in pixels.
220    #[get(type(copy))]
221    pub(crate) height: u16,
222    /// Emit rows in 4-pass GIF interlaced order.
223    #[get(type(copy))]
224    pub(crate) interlaced: bool,
225    /// Graphic Control Extension disposal method.
226    #[get(type(copy))]
227    pub(crate) disposal: GifDisposalMethod,
228    /// Optional transparent palette index.
229    #[get(skip)]
230    pub(crate) transparent_index: Option<u8>,
231    /// Delay in centiseconds (`1/100 s`).
232    #[get(type(copy))]
233    pub(crate) delay_cs: u16,
234    /// Optional frame-specific Local Color Table RGB triplets.
235    #[get(skip)]
236    pub(crate) local_palette: Option<Vec<[u8; 3]>>,
237    /// Row-major palette indices in visual (non-interlaced) top-to-bottom order of length `width * height`.
238    pub(crate) indices: Vec<u8>,
239}
240
241/// Reusable flat LZW dictionary table and packed bitstream scratch shared across frames in `GifEncoder::encode`.
242#[derive(Clone, Data, Debug)]
243pub(crate) struct GifLzwEncodeScratch {
244    /// Fixed reusable open-addressed flat table backing the 4096-entry GIF LZW dictionary (`gen << 32 | code << 20 | key`).
245    pub(crate) table: Vec<u64>,
246    /// Active table generation counter incremented in O(1) on every LZW clear code.
247    #[get(type(copy))]
248    pub(crate) generation: u32,
249    /// Reusable packed LZW bitstream buffer cleared between frames without reallocating.
250    pub(crate) packed_bytes: Vec<u8>,
251}
252
253/// Stateless pure-Rust GIF89a LZW encoder producing spec-compliant animated GIF streams.
254#[derive(Clone, Copy, Debug, Default)]
255pub struct GifEncoder;