fluxframe-cli 0.13.0

fluxframe CLI binary: list, check, run, benchmark.
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Idle-mode placeholder frame source.
//!
//! When the supervisor is in `Idle` it stops pulling real frames from
//! the input pipeline and instead pushes a cheap pre-rendered
//! placeholder to the output sink at `max(idle.fps,
//! idle.min_visibility_fps)`. The converted bytes are cached as
//! `Arc<[u8]>` so the per-tick cost collapses to one `Arc` clone plus
//! an enqueue into the writer slot.
//!
//! Two concrete kinds are supported in Stage 15:
//!
//! * [`ColorPlaceholder`] — solid RGB fill at the configured
//!   `placeholder_rgb`. Always available.
//! * [`ImagePlaceholder`] — static image loaded from a file path
//!   on disk. Decoded via the `image` crate (PNG, JPEG) at build
//!   time, then resize-stretched into the supervisor's output
//!   dimensions.
//!
//! Both impls cache an RGB byte buffer matching the supervisor's
//! output `(width, height)`. On top of that, each placeholder memoizes
//! the *converted* output bytes per `(width, height, format)` as an
//! `Arc<[u8]>` and hands out `FrameBuffer::Shared` clones, so a
//! steady-state idle push at the raised `min_visibility_fps` cadence
//! collapses to an `Arc` clone plus an enqueue — no per-tick
//! RGB→target conversion or large allocation.
//!
//! Format coverage matches the small set that shows up as
//! `input.format` (the appsrc format) in practice: RGB, RGBA, BGR,
//! GRAY8, YUY2. NV12 is intentionally not supported — planar
//! two-plane formats need a `Stride::Planar` `VideoFrame`, which the
//! placeholder cache would have to construct differently. NV12 is
//! never the appsrc format in current configs (it is only a sink
//! format, where videoconvert handles it). If a future config needs
//! it, extend [`Placeholder::render`] then.

use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};

use fluxframe_core::frame::{FrameBuffer, FrameMeta, PixelFormat, VideoFrame};
use fluxframe_core::{FluxError, IdleConfig, IdlePlaceholderKind};
#[cfg(feature = "image-fill")]
use fluxframe_effects::processing::image_loader::{ImageLoadError, decode_rgb_bounded};

// ---------------------------------------------------------------------------
// BT.601 colour-space constants (Q8 fixed point).
//
// Source: ITU-R BT.601-7 §2.5.1 + JFIF/JPEG full-range Y'CbCr matrix.
// All coefficients scaled by 256 (Q8), so `(x * K + ROUND) >> 8`
// computes round-to-nearest division by 256.
// ---------------------------------------------------------------------------

/// Luma Kr = 0.299 scaled by 256.
const BT601_KR_Q8: i32 = 77;
/// Luma Kg = 0.587 scaled by 256.
const BT601_KG_Q8: i32 = 150;
/// Luma Kb = 0.114 scaled by 256.
const BT601_KB_Q8: i32 = 29;
/// Cb coefficient for R (-0.169 × 256 ≈ -43).
const BT601_CB_R_Q8: i32 = -43;
/// Cb coefficient for G (-0.331 × 256 ≈ -85).
const BT601_CB_G_Q8: i32 = -85;
/// Cb coefficient for B (0.500 × 256 = 128).
const BT601_CB_B_Q8: i32 = 128;
/// Cr coefficient for R (0.500 × 256 = 128).
const BT601_CR_R_Q8: i32 = 128;
/// Cr coefficient for G (-0.419 × 256 ≈ -107).
const BT601_CR_G_Q8: i32 = -107;
/// Cr coefficient for B (-0.081 × 256 ≈ -21).
const BT601_CR_B_Q8: i32 = -21;
/// Round-to-nearest bias for the Q8 reduction: `(x + 128) >> 8`.
const BT601_ROUND_Q8: i32 = 128;
/// Chroma 0-centre offset: signed `(-128..=127)` → unsigned
/// `(0..=255)`.
const BT601_CHROMA_BIAS: i32 = 128;

// ---------------------------------------------------------------------------
// Trait + builder
// ---------------------------------------------------------------------------

/// One static placeholder frame produced on demand. Impls are
/// `Send + Sync` so the supervisor can share the trait object
/// behind an `Arc` across the worker thread and any future
/// reload-coordinator thread without locking.
pub(crate) trait Placeholder: Send + Sync {
    /// Produce a [`VideoFrame`] in `format` at the dimensions the
    /// placeholder was built for. The first call for a given
    /// `(width, height, format)` runs the format conversion from the
    /// cached RGB buffer and memoizes the result as
    /// `FrameBuffer::Shared(Arc<[u8]>)`; subsequent calls return a
    /// zero-copy `Arc` clone.
    ///
    /// # Single-instance contract
    ///
    /// One [`Placeholder`] instance is built per `(width, height)`
    /// pair. Calling [`render`] with different dimensions returns
    /// [`FluxError::Config`]: the supervisor must rebuild the
    /// placeholder via [`build`] when output dimensions change
    /// (e.g. on `reload`).
    ///
    /// # Errors
    ///
    /// Returns [`FluxError::Config`] when the placeholder cannot
    /// satisfy the request: unsupported target format, dimension
    /// mismatch against the built cache, or zero-sized output.
    ///
    /// [`render`]: Self::render
    fn render(&self, width: u32, height: u32, format: PixelFormat)
    -> Result<VideoFrame, FluxError>;
}

/// Build the placeholder described by `cfg`, sized for the
/// supervisor's output `(width, height)`. For
/// [`IdlePlaceholderKind::Image`] this is the eager decode + resize
/// — failure here surfaces a real [`FluxError::Config`] with the
/// full source chain (`io::Error` from `ENOENT`, decoder error from
/// a corrupt PNG, …), rather than the generic "failed to load"
/// surfaced by a deferred `OnceLock` path on first render.
///
/// # Errors
///
/// Returns [`FluxError::Config`] when:
/// * `placeholder = "image"` is set without a `placeholder_path`
///   (validation also catches this; defensive against bypass);
/// * the image file cannot be opened, parsed, or decoded;
/// * `width` or `height` is zero.
pub(crate) fn build(
    cfg: &IdleConfig,
    width: u32,
    height: u32,
) -> Result<Box<dyn Placeholder>, FluxError> {
    if width == 0 || height == 0 {
        return Err(FluxError::Config {
            reason: format!("placeholder asked for zero-sized frame {width}x{height}"),
            hint: None,
        });
    }
    match cfg.placeholder {
        IdlePlaceholderKind::Color => Ok(Box::new(ColorPlaceholder::new(
            cfg.placeholder_rgb,
            width,
            height,
        ))),
        IdlePlaceholderKind::Image => {
            // The image branch requires the `image-fill` feature to
            // pull in the PNG/JPEG decoder. Slim builds
            // (`--no-default-features`) report a clean config error
            // pointing the operator at the build flag rather than
            // silently swapping in the colour placeholder.
            #[cfg(feature = "image-fill")]
            {
                let path = cfg
                    .placeholder_path
                    .as_ref()
                    .ok_or_else(|| FluxError::Config {
                        reason: "idle.placeholder = \"image\" requires idle.placeholder_path"
                            .into(),
                        hint: Some("set idle.placeholder_path to a PNG/JPEG file".into()),
                    })?;
                ImagePlaceholder::load(path.as_path(), width, height)
                    .map(|p| Box::new(p) as Box<dyn Placeholder>)
            }
            #[cfg(not(feature = "image-fill"))]
            {
                Err(FluxError::Config {
                    reason: "image placeholders require the `image-fill` cargo feature".into(),
                    hint: Some(
                        "rebuild with --features image-fill or switch idle.placeholder to \"color\""
                            .into(),
                    ),
                })
            }
        }
        // `IdlePlaceholderKind` is `#[non_exhaustive]`; this arm
        // future-proofs the builder against new variants added in
        // later stages. Surface the missing handler loudly instead
        // of silently defaulting to colour fill.
        _ => Err(FluxError::Config {
            reason: format!("unsupported idle placeholder kind {:?}", cfg.placeholder),
            hint: Some("update fluxframe-cli to handle the new IdlePlaceholderKind variant".into()),
        }),
    }
}

// ---------------------------------------------------------------------------
// ColorPlaceholder — solid RGB fill, cached once.
// ---------------------------------------------------------------------------

/// Solid-fill placeholder. The cached RGB buffer is built once at
/// construction; converted output bytes are memoized per format.
pub(crate) struct ColorPlaceholder {
    width: u32,
    height: u32,
    rgb: Vec<u8>,
    cache: ConvertedCache,
}

impl ColorPlaceholder {
    fn new(rgb: [u8; 3], width: u32, height: u32) -> Self {
        // Callers (via `build`) validated non-zero dimensions, so the
        // `(width * height * 3)` allocation is well-defined: u32 fits
        // in usize on every target FluxFrame builds on, and the
        // resulting size is bounded by the upstream config-level cap
        // on output dims.
        let pixels = (width as usize) * (height as usize);
        let mut buf = vec![0u8; pixels * 3];
        for chunk in buf.chunks_exact_mut(3) {
            chunk.copy_from_slice(&rgb);
        }
        Self {
            width,
            height,
            rgb: buf,
            cache: ConvertedCache::default(),
        }
    }
}

impl Placeholder for ColorPlaceholder {
    fn render(
        &self,
        width: u32,
        height: u32,
        format: PixelFormat,
    ) -> Result<VideoFrame, FluxError> {
        ensure_dims_match(self.width, self.height, width, height)?;
        let bytes = self
            .cache
            .get_or_convert(&self.rgb, width, height, format)?;
        wrap_into_frame(FrameBuffer::Shared(bytes), width, height, format)
    }
}

// ---------------------------------------------------------------------------
// ImagePlaceholder — eager decode + resize, cached RGB once.
// ---------------------------------------------------------------------------

/// Static-image placeholder. The image file is decoded and resized
/// to `(width, height)` at construction time via
/// [`decode_rgb_bounded`]; subsequent `render` calls do format
/// conversion only.
///
/// Gated behind the `image-fill` cargo feature so slim builds
/// (`--no-default-features`) do not link the PNG/JPEG decoder. The
/// `build()` constructor surfaces a clean config error pointing the
/// operator at the feature flag when the slim binary is asked for an
/// image placeholder.
#[cfg(feature = "image-fill")]
pub(crate) struct ImagePlaceholder {
    width: u32,
    height: u32,
    rgb: Vec<u8>,
    cache: ConvertedCache,
}

#[cfg(feature = "image-fill")]
impl ImagePlaceholder {
    fn load(path: &std::path::Path, width: u32, height: u32) -> Result<Self, FluxError> {
        let decoded = decode_rgb_bounded(path).map_err(|e: ImageLoadError| FluxError::Config {
            reason: e.to_string(),
            hint: Some("ensure the path exists and contains a valid PNG/JPEG".into()),
        })?;
        // `decode_rgb_bounded`'s contract guarantees
        // `data.len() == width * height * 3` and non-zero dims, so
        // `RgbImage::from_raw` cannot return `None`. The `expect`
        // catches a future regression in the decoder contract — not
        // user input.
        let src = image::RgbImage::from_raw(decoded.width, decoded.height, decoded.data).expect(
            "decode_rgb_bounded guarantees data.len() == width * height * 3 and non-zero dims",
        );
        let resized =
            image::imageops::resize(&src, width, height, image::imageops::FilterType::Triangle);
        Ok(Self {
            width,
            height,
            rgb: resized.into_raw(),
            cache: ConvertedCache::default(),
        })
    }
}

#[cfg(feature = "image-fill")]
impl Placeholder for ImagePlaceholder {
    fn render(
        &self,
        width: u32,
        height: u32,
        format: PixelFormat,
    ) -> Result<VideoFrame, FluxError> {
        ensure_dims_match(self.width, self.height, width, height)?;
        let bytes = self
            .cache
            .get_or_convert(&self.rgb, width, height, format)?;
        wrap_into_frame(FrameBuffer::Shared(bytes), width, height, format)
    }
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// Per-placeholder memo of converted output bytes keyed by
/// `(width, height, format)`. The placeholder image is static, so the
/// first `render` for a given key runs the RGB→target conversion and
/// every subsequent call returns a cheap `Arc` clone. This keeps the
/// raised idle cadence (`min_visibility_fps`, ~10 Hz) from churning a
/// fresh allocation + conversion on every tick.
///
/// `render` takes `&self`, so the cache uses interior mutability via a
/// `Mutex`. In practice only the worker thread renders, but the
/// `Placeholder` trait is `Send + Sync` and the `Mutex` keeps that
/// contract honest at negligible cost (uncontended lock per tick).
/// Memo keyed by the converted output's `(width, height, format)`.
type ConvertedMap = HashMap<(u32, u32, PixelFormat), Arc<[u8]>>;

#[derive(Default)]
struct ConvertedCache {
    map: Mutex<ConvertedMap>,
}

impl ConvertedCache {
    /// Return the converted bytes for `(width, height, format)`, running
    /// the conversion once and memoizing the result. Errors (unsupported
    /// format, odd-width YUY2) are deliberately NOT cached, so they keep
    /// surfacing on every call exactly as the un-cached path did.
    fn get_or_convert(
        &self,
        rgb: &[u8],
        width: u32,
        height: u32,
        format: PixelFormat,
    ) -> Result<Arc<[u8]>, FluxError> {
        let key = (width, height, format);
        // Recover from a poisoned lock: a prior panic while holding the
        // guard does not corrupt the memo (entries are immutable once
        // inserted), so the cached bytes stay valid.
        let mut map = self.map.lock().unwrap_or_else(PoisonError::into_inner);
        if let Some(bytes) = map.get(&key) {
            return Ok(Arc::clone(bytes));
        }
        let bytes: Arc<[u8]> = Arc::from(convert_rgb_to(rgb, width, height, format)?);
        map.insert(key, Arc::clone(&bytes));
        Ok(bytes)
    }
}

fn ensure_dims_match(
    cached_w: u32,
    cached_h: u32,
    asked_w: u32,
    asked_h: u32,
) -> Result<(), FluxError> {
    if cached_w == asked_w && cached_h == asked_h {
        Ok(())
    } else {
        Err(FluxError::Config {
            reason: format!(
                "placeholder built for {cached_w}x{cached_h}, asked for {asked_w}x{asked_h}"
            ),
            hint: Some("supervisor must rebuild the placeholder when output dims change".into()),
        })
    }
}

/// Wrap a packed buffer in a [`VideoFrame`] with a synthetic
/// [`FrameMeta`]. The supervisor's writer thread overwrites
/// timestamps anyway, so meta values are nominal. Takes the buffer as
/// a [`FrameBuffer`] so the caller can pass a zero-copy
/// [`FrameBuffer::Shared`] clone from the converted-bytes cache.
fn wrap_into_frame(
    buffer: FrameBuffer,
    width: u32,
    height: u32,
    format: PixelFormat,
) -> Result<VideoFrame, FluxError> {
    VideoFrame::new_packed(buffer, width, height, format, FrameMeta::default()).ok_or_else(|| {
        FluxError::Config {
            reason: format!(
                "placeholder frame construction failed for {format:?} {width}x{height}"
            ),
            hint: None,
        }
    })
}

/// Convert a packed RGB byte buffer to one of the supported target
/// formats. The supervisor's appsrc only emits a handful of formats
/// in practice; anything else is rejected loudly rather than
/// silently mangled.
fn convert_rgb_to(
    rgb: &[u8],
    width: u32,
    height: u32,
    target: PixelFormat,
) -> Result<Vec<u8>, FluxError> {
    let pixels = (width as usize) * (height as usize);
    debug_assert_eq!(rgb.len(), pixels * 3, "RGB source has wrong length");
    match target {
        PixelFormat::Rgb => Ok(rgb.to_vec()),
        PixelFormat::Bgr => Ok(rgb_to_bgr(rgb, pixels)),
        PixelFormat::Rgba => Ok(rgb_to_rgba(rgb, pixels)),
        PixelFormat::Gray8 => Ok(rgb_to_gray8(rgb, pixels)),
        PixelFormat::Yuy2 => rgb_to_yuy2(rgb, width, height),
        PixelFormat::Nv12 => Err(FluxError::Config {
            reason: "idle placeholder does not support NV12 appsrc format".into(),
            hint: Some(
                "set input.format to RGB / RGBA / BGR / YUY2 / GRAY8 — \
                 videoconvert handles the NV12 sink leg automatically"
                    .into(),
            ),
        }),
        // `PixelFormat` is `#[non_exhaustive]`. New variants surface
        // as a config error rather than silently producing a
        // mis-coloured frame.
        _ => Err(FluxError::Config {
            reason: format!("idle placeholder does not support pixel format {target:?}"),
            hint: Some("add a converter or change input.format".into()),
        }),
    }
}

fn rgb_to_bgr(rgb: &[u8], pixels: usize) -> Vec<u8> {
    let mut out = vec![0u8; pixels * 3];
    for (src, dst) in rgb.chunks_exact(3).zip(out.chunks_exact_mut(3)) {
        dst[0] = src[2];
        dst[1] = src[1];
        dst[2] = src[0];
    }
    out
}

fn rgb_to_rgba(rgb: &[u8], pixels: usize) -> Vec<u8> {
    let mut out = vec![0u8; pixels * 4];
    for (src, dst) in rgb.chunks_exact(3).zip(out.chunks_exact_mut(4)) {
        dst[0] = src[0];
        dst[1] = src[1];
        dst[2] = src[2];
        dst[3] = 0xFF;
    }
    out
}

fn rgb_to_gray8(rgb: &[u8], pixels: usize) -> Vec<u8> {
    let mut out = vec![0u8; pixels];
    for (src, dst) in rgb.chunks_exact(3).zip(out.iter_mut()) {
        let r = i32::from(src[0]);
        let g = i32::from(src[1]);
        let b = i32::from(src[2]);
        let y = (BT601_KR_Q8 * r + BT601_KG_Q8 * g + BT601_KB_Q8 * b + BT601_ROUND_Q8) >> 8;
        *dst = clamp_u8(y);
    }
    out
}

/// RGB → YUYV / YUY2 packed conversion using BT.601 with full-range
/// JPEG coefficients (see ITU-R BT.601-7 §2.5.1 + JFIF Y'CbCr
/// matrix). Two RGB pixels collapse to one 4-byte YUY2 pair:
/// `[Y0 U Y1 V]`. Odd widths are rejected because YUY2 is
/// fundamentally pair-packed.
fn rgb_to_yuy2(rgb: &[u8], width: u32, height: u32) -> Result<Vec<u8>, FluxError> {
    if width % 2 != 0 {
        return Err(FluxError::Config {
            reason: format!("YUY2 placeholder requires even width, got {width}"),
            hint: None,
        });
    }
    let pixels = (width as usize) * (height as usize);
    let mut out = vec![0u8; pixels * 2];
    let row_rgb = (width as usize) * 3;
    let row_yuy2 = (width as usize) * 2;
    // Iterate row-by-row so we can pair-step through the RGB source
    // (6 bytes = 2 pixels) and the YUY2 destination (4 bytes = 1
    // packed word) with the `chunks_exact` idiom used by the other
    // converters in this module. Width-evenness checked above
    // guarantees `chunks_exact` consumes every byte without a
    // remainder.
    for (rgb_row, yuy2_row) in rgb
        .chunks_exact(row_rgb)
        .zip(out.chunks_exact_mut(row_yuy2))
    {
        for (rgb_pair, yuy2_word) in rgb_row.chunks_exact(6).zip(yuy2_row.chunks_exact_mut(4)) {
            let (y0, u0, v0) = rgb_to_ycbcr(rgb_pair[0], rgb_pair[1], rgb_pair[2]);
            let (y1, u1, v1) = rgb_to_ycbcr(rgb_pair[3], rgb_pair[4], rgb_pair[5]);
            // Sub-sample chroma to one pair per two luma. The +1
            // rounding bias keeps the integer division unbiased.
            let u = ((u32::from(u0) + u32::from(u1) + 1) >> 1) as u8;
            let v = ((u32::from(v0) + u32::from(v1) + 1) >> 1) as u8;
            yuy2_word[0] = y0;
            yuy2_word[1] = u;
            yuy2_word[2] = y1;
            yuy2_word[3] = v;
        }
    }
    Ok(out)
}

/// BT.601 JPEG-range RGB → YCbCr, Q8 fixed point. Result components
/// are clamped to `0..=255` via [`clamp_u8`]; the un-clamped
/// intermediate stays inside `i32` so the BT.601 chroma offsets
/// (signed, `-128..=127`) round-trip without overflow.
fn rgb_to_ycbcr(r: u8, g: u8, b: u8) -> (u8, u8, u8) {
    let r = i32::from(r);
    let g = i32::from(g);
    let b = i32::from(b);
    let y = (BT601_KR_Q8 * r + BT601_KG_Q8 * g + BT601_KB_Q8 * b + BT601_ROUND_Q8) >> 8;
    let cb = ((BT601_CB_R_Q8 * r + BT601_CB_G_Q8 * g + BT601_CB_B_Q8 * b + BT601_ROUND_Q8) >> 8)
        + BT601_CHROMA_BIAS;
    let cr = ((BT601_CR_R_Q8 * r + BT601_CR_G_Q8 * g + BT601_CR_B_Q8 * b + BT601_ROUND_Q8) >> 8)
        + BT601_CHROMA_BIAS;
    (clamp_u8(y), clamp_u8(cb), clamp_u8(cr))
}

#[inline]
fn clamp_u8(v: i32) -> u8 {
    // `as u8` only well-defined after the clamp; the BT.601 math
    // above can in principle round past 255 by one — clamp keeps
    // the helper total without relying on hand-proofs.
    v.clamp(0, 255) as u8
}

#[cfg(test)]
mod tests {
    use super::*;

    fn color_cfg(rgb: [u8; 3]) -> IdleConfig {
        IdleConfig {
            enabled: true,
            placeholder_rgb: rgb,
            ..IdleConfig::default()
        }
    }

    #[test]
    fn color_renders_rgb() {
        let p = build(&color_cfg([10, 20, 30]), 4, 2).expect("build");
        let frame = p.render(4, 2, PixelFormat::Rgb).expect("render rgb");
        assert_eq!(frame.width, 4);
        assert_eq!(frame.height, 2);
        assert_eq!(frame.format, PixelFormat::Rgb);
        let data = frame.data.as_slice();
        assert_eq!(data.len(), 4 * 2 * 3);
        for chunk in data.chunks_exact(3) {
            assert_eq!(chunk, [10, 20, 30]);
        }
    }

    #[test]
    fn color_converts_to_bgr() {
        let p = build(&color_cfg([10, 20, 30]), 2, 1).expect("build");
        let frame = p.render(2, 1, PixelFormat::Bgr).expect("render bgr");
        let data = frame.data.as_slice();
        for chunk in data.chunks_exact(3) {
            assert_eq!(chunk, [30, 20, 10]);
        }
    }

    #[test]
    fn color_converts_to_rgba_with_full_alpha() {
        let p = build(&color_cfg([10, 20, 30]), 2, 1).expect("build");
        let frame = p.render(2, 1, PixelFormat::Rgba).expect("render rgba");
        let data = frame.data.as_slice();
        for chunk in data.chunks_exact(4) {
            assert_eq!(chunk, [10, 20, 30, 0xFF]);
        }
    }

    #[test]
    fn color_converts_to_yuy2() {
        let p = build(&color_cfg([255, 0, 0]), 2, 1).expect("build");
        let frame = p.render(2, 1, PixelFormat::Yuy2).expect("render yuy2");
        let data = frame.data.as_slice();
        assert_eq!(data.len(), 4);
        assert_eq!(data[0], data[2], "Y plane is uniform for a solid fill");
    }

    #[test]
    fn color_rejects_yuy2_with_odd_width() {
        let p = build(&color_cfg([255, 0, 0]), 3, 1).expect("build");
        match p.render(3, 1, PixelFormat::Yuy2) {
            Ok(_) => panic!("odd width must be rejected"),
            Err(e) => assert!(format!("{e}").contains("even width"), "{e}"),
        }
    }

    #[test]
    fn color_converts_to_gray8() {
        let p = build(&color_cfg([255, 255, 255]), 2, 2).expect("build");
        let frame = p.render(2, 2, PixelFormat::Gray8).expect("render gray");
        let data = frame.data.as_slice();
        assert_eq!(data.len(), 4);
        for v in data {
            assert!(*v > 250, "luma {v} should be near 255");
        }
    }

    #[test]
    fn color_rejects_nv12() {
        let p = build(&color_cfg([10, 20, 30]), 4, 2).expect("build");
        match p.render(4, 2, PixelFormat::Nv12) {
            Ok(_) => panic!("nv12 should be rejected"),
            Err(e) => assert!(format!("{e}").contains("NV12"), "{e}"),
        }
    }

    #[test]
    fn build_rejects_zero_dimensions() {
        match build(&color_cfg([0, 0, 0]), 0, 10) {
            Ok(_) => panic!("zero width must be rejected"),
            Err(e) => assert!(format!("{e}").contains("zero-sized")),
        }
    }

    #[test]
    fn build_color_placeholder_from_config() {
        let p = build(&color_cfg([1, 2, 3]), 2, 1).expect("build color");
        let frame = p.render(2, 1, PixelFormat::Rgb).expect("render");
        assert_eq!(frame.data.as_slice(), [1, 2, 3, 1, 2, 3]);
    }

    #[cfg(feature = "image-fill")]
    #[test]
    fn build_image_placeholder_requires_path() {
        let cfg = IdleConfig {
            enabled: true,
            placeholder: IdlePlaceholderKind::Image,
            placeholder_path: None,
            ..IdleConfig::default()
        };
        // `Box<dyn Placeholder>` does not implement `Debug`, so we
        // match on the Result instead of using `expect_err`.
        match build(&cfg, 4, 2) {
            Ok(_) => panic!("image without path must fail"),
            Err(e) => assert!(format!("{e}").contains("placeholder_path")),
        }
    }

    #[cfg(not(feature = "image-fill"))]
    #[test]
    fn build_image_placeholder_rejects_without_image_fill_feature() {
        // Slim build: the image branch is configured out. The error
        // must point the operator at the `image-fill` feature flag
        // rather than silently falling back to the colour placeholder.
        let cfg = IdleConfig {
            enabled: true,
            placeholder: IdlePlaceholderKind::Image,
            placeholder_path: Some(std::path::PathBuf::from("/tmp/bg.png")),
            ..IdleConfig::default()
        };
        match build(&cfg, 4, 2) {
            Ok(_) => panic!("image kind must fail without image-fill feature"),
            Err(e) => {
                let msg = format!("{e}");
                assert!(
                    msg.contains("image-fill"),
                    "error must mention the feature flag, got: {msg}"
                );
            }
        }
    }

    #[test]
    fn repeated_render_is_byte_identical_via_cache() {
        // The converted-bytes cache must be transparent: a second
        // render of the same (w, h, format) returns the same bytes as
        // the first. Also exercises two distinct formats sharing one
        // placeholder so the per-key memo is keyed correctly.
        let p = build(&color_cfg([10, 20, 30]), 4, 2).expect("build");
        let first = p.render(4, 2, PixelFormat::Rgb).expect("render 1");
        let second = p.render(4, 2, PixelFormat::Rgb).expect("render 2");
        assert_eq!(first.data.as_slice(), second.data.as_slice());

        let bgr = p.render(4, 2, PixelFormat::Bgr).expect("render bgr");
        assert_eq!(bgr.format, PixelFormat::Bgr);
        for chunk in bgr.data.as_slice().chunks_exact(3) {
            assert_eq!(chunk, [30, 20, 10]);
        }
    }

    #[test]
    fn render_rejects_dim_mismatch() {
        let p = build(&color_cfg([1, 2, 3]), 2, 1).expect("build");
        match p.render(4, 1, PixelFormat::Rgb) {
            Ok(_) => panic!("dim mismatch must be rejected"),
            Err(e) => {
                let msg = format!("{e}");
                // `FluxError::Config` renders only `reason`; the
                // "rebuild" hint is on the separate `hint` field.
                assert!(
                    msg.contains("built for 2x1") && msg.contains("asked for 4x1"),
                    "expected dim-mismatch reason, got: {msg}"
                );
            }
        }
    }
}