capsule_core/lqip/sidecar.rs
1//! The bridge between an [`Lqip`] payload and the signed sidecar's `lqip` record.
2//!
3//! Gated on `native` for one reason only: [`crate::sidecar`] is itself `native`-gated. The
4//! *encoder* in the parent module is unconditional and is what `capsule-wasm` links, so the two
5//! surfaces never diverge — this file only assembles and reads the three fields the sidecar
6//! carries.
7//!
8//! `lqip` is a **signed** field: the sidecar signature covers it, so its encoding is
9//! signature-visible rather than a private detail. See [`LQIP_FORMAT_V1`] for why moving from
10//! ThumbHash to chromahash keeps version `1` and does not move `sidecar_schema`.
11
12use super::{LQIP_FORMAT_V1, Lqip, RgbaImage, render};
13use crate::sidecar::sidecar_v1::Lqip as SidecarLqip;
14
15impl Lqip {
16 /// Build the encrypted-sidecar record: the chromahash payload, the [`LQIP_FORMAT_V1`] tag,
17 /// and the `dominant_color` fill a reader paints when it cannot decode the payload.
18 ///
19 /// Infallible — `average_color` is a DC read that cannot fail, and the payload is this
20 /// type's invariant.
21 pub fn to_sidecar(&self) -> SidecarLqip {
22 SidecarLqip {
23 chromahash: self.as_bytes().to_vec(),
24 format_version: LQIP_FORMAT_V1,
25 dominant_color: self.dominant_color(),
26 }
27 }
28}
29
30/// Render a stored sidecar record to a displayable placeholder, band-limited to the box being
31/// painted.
32///
33/// Delegates to [`render`], so an unrecognized `format_version` or an undecodable payload
34/// yields the 1x1 solid `dominant_color` fill rather than a misrender.
35pub fn render_sidecar_lqip(lqip: &SidecarLqip, max_width: u32, max_height: u32) -> RgbaImage {
36 render(
37 lqip.format_version,
38 &lqip.chromahash,
39 lqip.dominant_color,
40 max_width,
41 max_height,
42 )
43}
44
45#[cfg(test)]
46mod tests;