euv-core 0.48.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Decoding budgets and frame-delay compatibility policy for pure-Rust GIF87a/GIF89a playback.
#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
pub struct GifDecodeConfig {
    /// Maximum allowed RGBA8 logical canvas size in bytes (`width * height * 4`).
    #[get(type(copy))]
    pub(crate) max_canvas_bytes: usize,
    /// Maximum cumulative RGBA8 bytes retained in the bounded pre-decode frame cache.
    #[get(type(copy))]
    pub(crate) max_cache_bytes: usize,
    /// Maximum number of composited frames retained in the bounded pre-decode cache.
    #[get(type(copy))]
    pub(crate) max_predecode_frames: usize,
    /// Minimum non-zero frame delay clamp in milliseconds when `strict_delays == false`.
    #[get(type(copy))]
    pub(crate) min_delay_ms: u32,
    /// Compatibility delay in milliseconds substituted when a frame specifies `0` or `10ms` and `strict_delays == false`.
    #[get(type(copy))]
    pub(crate) zero_delay_fallback_ms: u32,
    /// When `true`, preserves exact raw centisecond delays (`delay_cs * 10`) without browser-compat substitution.
    #[get(type(copy))]
    pub(crate) strict_delays: bool,
}

/// Pixel-space dirty rectangle covering both previous-frame disposal changes and current sub-frame writes.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
pub struct GifDirtyRect {
    /// Left pixel coordinate within the logical canvas.
    #[get(type(copy))]
    pub(crate) x: u32,
    /// Top pixel coordinate within the logical canvas.
    #[get(type(copy))]
    pub(crate) y: u32,
    /// Dirty region width in pixels.
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Dirty region height in pixels.
    #[get(type(copy))]
    pub(crate) height: u32,
}

/// Lightweight parsed metadata descriptor for a single compressed GIF frame inside a byte stream.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
pub struct GifFrameDescriptor {
    /// Zero-based frame index in stream order.
    #[get(type(copy))]
    pub(crate) index: usize,
    /// Horizontal sub-frame offset inside the logical screen.
    #[get(type(copy))]
    pub(crate) left: u16,
    /// Vertical sub-frame offset inside the logical screen.
    #[get(type(copy))]
    pub(crate) top: u16,
    /// Sub-frame width in pixels.
    #[get(type(copy))]
    pub(crate) width: u16,
    /// Sub-frame height in pixels.
    #[get(type(copy))]
    pub(crate) height: u16,
    /// Whether pixel rows are stored in 4-pass GIF interlaced order.
    #[get(type(copy))]
    pub(crate) interlaced: bool,
    /// Graphic Control Extension disposal method applied after this frame displays.
    #[get(type(copy))]
    pub(crate) disposal: GifDisposalMethod,
    /// Optional transparent palette index preserving underlying canvas pixels.
    #[get(skip)]
    pub(crate) transparent_index: Option<u8>,
    /// Effective display duration in milliseconds after applying `GifDecodeConfig` delay policy.
    #[get(type(copy))]
    pub(crate) delay_ms: u32,
    /// Presentation timestamp from timeline start in seconds.
    #[get(type(copy))]
    pub(crate) timestamp_ms: u32,
    /// Optional `(start, end)` byte range of the frame's Local Color Table inside `GifDecoder::bytes`.
    #[get(skip)]
    pub(crate) lct_range: Option<(usize, usize)>,
    /// Initial LZW minimum code size (`2..=8`).
    #[get(type(copy))]
    pub(crate) lzw_min_code_size: u8,
    /// Byte offset where the first LZW data sub-block length byte begins inside `GifDecoder::bytes`.
    #[get(type(copy))]
    pub(crate) lzw_data_start: usize,
    /// Exclusive byte offset immediately after the `0x00` sub-block terminator.
    #[get(type(copy))]
    pub(crate) lzw_data_end: usize,
}

/// Single fully composited GIF frame accompanied by its disposal-aware dirty rectangle.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct GifDecodedFrame {
    /// Zero-based frame index.
    #[get(type(copy))]
    pub(crate) frame_index: usize,
    /// Full logical-screen composited RGBA8 pixel buffer.
    pub(crate) image: ImageBuffer,
    /// Effective display duration in milliseconds.
    #[get(type(copy))]
    pub(crate) delay_ms: u32,
    /// Presentation timestamp in seconds.
    #[get(type(copy))]
    pub(crate) timestamp_secs: f32,
    /// Disposal-aware pixel dirty region covering prior disposal restoration plus current sub-frame pixels.
    #[get(type(copy))]
    pub(crate) dirty_rect: GifDirtyRect,
    /// Disposal method declared for this frame.
    #[get(type(copy))]
    pub(crate) disposal: GifDisposalMethod,
}

/// Pure-Rust GIF87a/GIF89a container parser and indexed LZW frame decoder.
#[derive(Clone, Data, Debug, Default)]
pub struct GifDecoder {
    /// Raw encoded GIF byte stream shared via `Arc<[u8]>` for O(1) zero-copy cloning.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) bytes: Arc<[u8]>,
    /// Detected GIF header signature (`"GIF87a"` or `"GIF89a"`).
    pub(crate) version: String,
    /// Logical screen width in pixels (exact source dimension, never downscaled).
    #[get(type(copy))]
    pub(crate) logical_width: u16,
    /// Logical screen height in pixels (exact source dimension, never downscaled).
    #[get(type(copy))]
    pub(crate) logical_height: u16,
    /// Declared global background color palette index.
    #[get(type(copy))]
    pub(crate) background_color_index: u8,
    /// Parsed Global Color Table RGB triplets (`empty` if none declared).
    pub(crate) global_color_table: Vec<[u8; 3]>,
    /// Whether an explicit `NETSCAPE2.0` / `ANIMEXTS1.0` loop extension was parsed.
    #[get(type(copy))]
    pub(crate) has_netscape_loop: bool,
    /// Parsed loop count (`0` = infinite looping, `N > 0` = finite repeat count).
    #[get(type(copy))]
    pub(crate) loop_count: u32,
    /// Indexed frame descriptors parsed from the stream.
    pub(crate) frames: Vec<GifFrameDescriptor>,
    /// Active memory and frame-delay compatibility configuration.
    #[get(type(copy))]
    pub(crate) config: GifDecodeConfig,
}

/// Stateful streaming GIF player with reusable composite canvas, bounded pre-decode cache, disposal-aware dirty tracking, and off-screen pause/resume.
#[derive(Clone, Data, Debug, Default)]
pub struct GifStreamPlayer {
    /// Underlying parsed GIF stream decoder.
    pub(crate) decoder: GifDecoder,
    /// Reusable full-canvas RGBA8 compositing surface (`logical_width * logical_height * 4`).
    pub(crate) canvas: Vec<u8>,
    /// Reusable canvas backup buffer populated only when a frame uses `RestoreToPrevious`.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) restore_snapshot: Vec<u8>,
    /// Reusable scratch buffer holding LZW-decoded palette indices for the active frame.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) index_scratch: Vec<u8>,
    /// Bounded LRU cache of composited frames capped by `max_predecode_frames` and `max_cache_bytes`.
    pub(crate) cache: VecDeque<GifDecodedFrame>,
    /// Cumulative RGBA8 bytes currently stored in `cache`.
    #[get(type(copy))]
    pub(crate) cache_bytes: usize,
    /// Index of the most recent frame composited directly onto `canvas`.
    #[get(skip)]
    pub(crate) last_composited_index: Option<usize>,
    /// Disposal rectangle left behind by `last_composited_index` (applied when advancing to the next frame).
    #[get(skip)]
    pub(crate) pending_disposal_rect: Option<GifDirtyRect>,
    /// Disposal method of `last_composited_index` pending before the next frame draws.
    #[get(type(copy))]
    pub(crate) pending_disposal: GifDisposalMethod,
    /// Currently displayed frame index.
    #[get(type(copy))]
    pub(crate) current_frame: usize,
    /// Elapsed milliseconds accumulated within `current_frame`.
    #[get(type(copy))]
    pub(crate) elapsed_ms: u32,
    /// Number of full animation loops completed so far.
    #[get(type(copy))]
    pub(crate) completed_loops: u32,
    /// Whether this player is currently visible on screen (`false` suspends decoding and timing).
    #[get(type(copy))]
    #[set(skip)]
    pub(crate) visible: bool,
    /// Policy controlling resume behavior when transitioning from off-screen back to visible.
    #[get(type(copy))]
    pub(crate) visibility_policy: GifVisibilityPolicy,
    /// Active playback status.
    #[get(type(copy))]
    pub(crate) status: GifPlaybackStatus,
    /// Disposal-aware dirty region reported for the active frame.
    #[get(type(copy))]
    pub(crate) last_dirty_rect: GifDirtyRect,
    /// Number of frames served directly from the bounded pre-decode cache without re-running LZW.
    #[get(type(copy))]
    pub(crate) cache_hits: u64,
    /// Number of frames decoded via LZW and composited onto the reusable canvas.
    #[get(type(copy))]
    pub(crate) decode_count: u64,
}

/// Frame specification consumed by `GifEncoder::encode` to emit valid spec-compliant GIF89a LZW byte streams.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct GifEncodeFrameInput {
    /// Horizontal sub-frame offset inside the logical screen.
    #[get(type(copy))]
    pub(crate) left: u16,
    /// Vertical sub-frame offset inside the logical screen.
    #[get(type(copy))]
    pub(crate) top: u16,
    /// Sub-frame width in pixels.
    #[get(type(copy))]
    pub(crate) width: u16,
    /// Sub-frame height in pixels.
    #[get(type(copy))]
    pub(crate) height: u16,
    /// Emit rows in 4-pass GIF interlaced order.
    #[get(type(copy))]
    pub(crate) interlaced: bool,
    /// Graphic Control Extension disposal method.
    #[get(type(copy))]
    pub(crate) disposal: GifDisposalMethod,
    /// Optional transparent palette index.
    #[get(skip)]
    pub(crate) transparent_index: Option<u8>,
    /// Delay in centiseconds (`1/100 s`).
    #[get(type(copy))]
    pub(crate) delay_cs: u16,
    /// Optional frame-specific Local Color Table RGB triplets.
    #[get(skip)]
    pub(crate) local_palette: Option<Vec<[u8; 3]>>,
    /// Row-major palette indices in visual (non-interlaced) top-to-bottom order of length `width * height`.
    pub(crate) indices: Vec<u8>,
}

/// Reusable flat LZW dictionary table and packed bitstream scratch shared across frames in `GifEncoder::encode`.
#[derive(Clone, Data, Debug)]
pub(crate) struct GifLzwEncodeScratch {
    /// Fixed reusable open-addressed flat table backing the 4096-entry GIF LZW dictionary (`gen << 32 | code << 20 | key`).
    pub(crate) table: Vec<u64>,
    /// Active table generation counter incremented in O(1) on every LZW clear code.
    #[get(type(copy))]
    pub(crate) generation: u32,
    /// Reusable packed LZW bitstream buffer cleared between frames without reallocating.
    pub(crate) packed_bytes: Vec<u8>,
}

/// Stateless pure-Rust GIF89a LZW encoder producing spec-compliant animated GIF streams.
#[derive(Clone, Copy, Debug, Default)]
pub struct GifEncoder;