Skip to main content

capsule_core/media/
mod.rs

1//! Still-image decode, orientation, metadata normalisation and derivative generation — the
2//! Capsule-side owner of the media pipeline.
3//!
4//! SSoT: [Thumbnails and Previews](https://docs/design/thumbnails/).
5//!
6//! # Boundaries
7//!
8//! Rawshift owns codecs; this module owns everything Capsule decides. Concretely,
9//! [`rawshift-image`] performs format sniffing, pixel decode, and the byte encode, while this
10//! module owns:
11//!
12//! - **the closed format sets** — [`StillFormat`](crate::media::StillFormat) (what Capsule
13//!   models as a still) and [`DerivativeFormat`](crate::derivative_format::DerivativeFormat) (what a signed
14//!   `DerivativeManifest.format` may say);
15//! - **the pixel budget and the panic guard**, because a third-party pre-1.0 decoder is fed
16//!   untrusted bytes on the import path;
17//! - **tier sizing and the downscale**, because `rawshift-image` has no resize and because a
18//!   derivative's bytes are signed, so the resample must be deterministic;
19//! - **the metadata strip**, because the crate's own default embeds EXIF (GPS included) into
20//!   every encode.
21//!
22//! Every path above is reached through the re-exports below, and the doc links name them by
23//! their full `crate::media::…` path deliberately. A bare ``[`StillFormat`]`` here does **not**
24//! resolve under the `doc-check-rust` gate (`cargo doc --no-deps`, `-D warnings`) even though
25//! the type is re-exported a few lines down — while it *does* resolve when the same command is
26//! given `--document-private-items`, which is why the failure only appeared in CI. Rather than
27//! guess at which of rustdoc's resolution rules produces that asymmetry, these links use the
28//! path that resolves under both.
29//!
30//! LQIP is *not* here: it lives in the unconditional [`crate::lqip`] module so the import
31//! pipeline, the app FFI and `capsule-wasm` share one implementation. This
32//! module produces the pixels [`crate::lqip::Lqip::encode`] consumes.
33//!
34//! # What this build can and cannot do
35//!
36//! Every gap is a typed
37//! [`UnsupportedFormat`](crate::media::MediaError::UnsupportedFormat) or a recorded per-format
38//! deferral — never a silent absence, and never a panic.
39//!
40//! **Decode** covers JPEG, PNG, JXL, TIFF and GIF, the ARW and DNG RAW families (issue #589,
41//! through `rawshift-image`'s RAW pipeline rather than its standard-image one; see
42//! `media::decode`), plus AVIF under the opt-in `media-avif-decode` feature, which the CLI
43//! enables (issue #588). AVIF goes through gamut's container parser and decodes its AV1
44//! codestream only where a hardware decoder is present (VAAPI on Linux today); elsewhere an
45//! AVIF defers like a missing codec until upstream gains a software AV1 fallback
46//! (visualcommons/rawshift#78). An ARW or DNG variant upstream has no decoder for — Sony's
47//! lossy compressed ARW — defers the same way. **Encode** writes the AVIF variant through
48//! gamut's pure-Rust AV1 encoder in every build. The opt-in `media-linked-codecs` feature, which
49//! the CLI enables and the mobile FFI does not, adds the **lossy** JXL master (libjxl) and the
50//! preview tier; without it only the thumbnail is encoded, as AVIF alone, and the JXL master is
51//! a recorded deferral.
52//!
53//! **Embedded previews** ([`extract_embedded_preview`](crate::media::extract_embedded_preview))
54//! are read from every RAW family the build decodes through `RawFile::thumbnail`, under the same
55//! IFD walk and pixel budget, and never developed, uploaded or signed. They are a stand-in on
56//! the render ladder until an encoded tier exists, not a derivative.
57//!
58//! HEIC, WebP and the other five RAW families (CR2, CR3, CRW, NEF, RAF) — and AVIF without
59//! `media-avif-decode` — sniff correctly and refuse to decode. HEIC is refused by decision: libheif and libde265 are LGPL-3.0 and excluded
60//! by policy (`rawshift-image`'s gamut HEIC decoder needs a hardware HEVC decoder besides, and
61//! is not enabled). **WebP is not enabled**: `rawshift-image`'s WebP is now pure-Rust gamut and
62//! compiles everywhere, but enabling it is its own change.
63//!
64//! [`rawshift-image`]: https://docs.rs/rawshift-image
65
66mod decode;
67mod derivative;
68mod detect;
69mod error;
70mod resize;
71mod tiff_bounds;
72
73// `native`-gated because `lifecycle` is its only caller and `lifecycle` is `native`-gated: a
74// `--features media` build without `native` (which the aarch64 cross-check uses, to isolate the
75// codecs from SQLite's C build) would otherwise carry an unused re-export.
76#[cfg(feature = "native")]
77pub use self::decode::guarded;
78pub use self::decode::{
79    DecodedImage, Decoder, EmbeddedPreview, MediaMetadata, RawshiftDecoder,
80    decode_embedded_preview, decode_guarded, extract_embedded_preview,
81};
82pub use self::derivative::{
83    DerivativeContext, DerivativeSealer, DerivativeTier, GeneratedDerivative, SealedDerivative,
84    StillDerivatives, generate_still_derivatives,
85};
86pub use self::detect::{MAX_DECODE_PIXELS, SUPPORTED_STILL_FORMATS, StillFormat};
87pub use self::error::{FormatOp, MediaError};
88pub use self::resize::{capped_dimensions, downscale_rgba8};
89// `DerivativeFormat` and `verify_still_format` are deliberately *not* re-exported here: they are
90// owned by the unconditional `crate::derivative_format`, because the crates that receive a
91// manifest build without this feature, and that module is their one public path.
92
93#[cfg(test)]
94mod tests;