Skip to main content

ToneMapper

Trait ToneMapper 

Source
pub trait ToneMapper:
    Send
    + Sync
    + UnwindSafe
    + RefUnwindSafe
    + Debug {
    // Required methods
    fn requirements(&self) -> Requirements;
    fn prepare(
        &self,
        ctx: &ToneMapContext,
        pulse: Option<&dyn Pulse>,
    ) -> Result<PreparedMapper, At<ConvertError>>;
    fn name(&self) -> &'static str;

    // Provided methods
    fn working_primaries_for(&self, _target: Primaries) -> Primaries { ... }
    fn baked_peaks(&self) -> Option<(f32, f32)> { ... }
}
Expand description

Pluggable HDR→SDR tone mapper for RowPlan and ImagePlan.

Implementations declare their analysis needs via requirements, then receive the gathered ToneMapContext in prepare and produce a PreparedMapper — typically a StripMapper with image-specific coefficients precomputed.

The trait is object-safe: ConvertPolicy stores Arc<dyn ToneMapper> internally, so the dispatch table never has to know the concrete mapper type.

§Invariants

  • Pure and deterministic. Given &self and a ToneMapContext, every method must be a pure function of its inputs. Interior mutability that perturbs output is forbidden.
  • Send + Sync + UnwindSafe + RefUnwindSafe + Debug. The supertrait bounds are required so plans (which are Clone + Debug) keep their auto-traits across the Arc<dyn ToneMapper> field stored on ConvertStep::ToneMap. Mappers are pure functions of their inputs and pose no panic-safety hazard; concrete implementations virtually always satisfy this without thought (any #[derive(Debug)] struct of plain numeric / Arc fields does).
  • Stable name. The string is consumed by diagnostics, trace recorders, and estimate / oracle cache keys. Treat it as part of the implementation’s public contract: once shipped, do not rename across releases.

§Working space

The strip handed to StripMapper::map_strip is linear-light RGB in the primaries declared by working_primaries_for (BT.2020 by default — the canonical HDR working space). Mappers that operate in a different gamut (a few Filmic variants prefer DCI-P3) override working_primaries_for; the pipeline inserts an extra gamut matrix on each side of the strip to honor that.

Required Methods§

Source

fn requirements(&self) -> Requirements

What pipeline-gathered context does this mapper need before it can produce a PreparedMapper?

Examples:

  • A pointwise curve with peaks pre-baked at construction returns Requirements::NONE — no analysis needed.
  • A pointwise curve that wants the pipeline to manage peaks returns Requirements::SOURCE_PEAK_ONLY.
  • A local operator (Mantiuk, Fattal, bilateral) returns Requirements { full_image: true, ..Requirements::NONE } — currently rejected by the builder (reserved for a future release; see PreparedMapper::Streaming).
Source

fn prepare( &self, ctx: &ToneMapContext, pulse: Option<&dyn Pulse>, ) -> Result<PreparedMapper, At<ConvertError>>

Build a per-image strip executor from the gathered context.

Called once by the pipeline after the analysis pass. Implementations should precompute curve coefficients / LUTs / adaptation fields here, then return a PreparedMapper that the pipeline applies strip-by-strip during row dispatch.

ctx carries every field the mapper declared in requirements, plus the always-populated source_peak_nits / target_peak_nits / working_primaries.

pulse carries optional cooperative cancellation and progress reporting from how_far::Pulse. Callers pass None when cancellation and progress reporting are not needed, or Some(pulse) (e.g. Some(&StopOnly::borrowed(stop)) for cancellation alone). Implementations doing non-trivial preparation work (such as building 3D LUTs or running a local spatial pass) should call pulse.check() before and during that work and map StopReason into ConvertError::Cancelled. Per how-far’s contract, implementations borrow pulse and never finish it.

Source

fn name(&self) -> &'static str

Short kebab-case identifier for diagnostics, trace logs, and estimate / oracle cache keys.

Examples: "bt2446a", "filmic-spline", "aces-rrt", "itu2408", "adaptive". Must be stable across releases of the implementing crate — treat changing it as a breaking change.

Provided Methods§

Source

fn working_primaries_for(&self, _target: Primaries) -> Primaries

The working color primaries this mapper expects on its input strip, given a target primaries hint.

Default: Primaries::Bt2020 — the canonical HDR working space.

Source

fn baked_peaks(&self) -> Option<(f32, f32)>

Pre-baked (source_peak_nits, target_peak_nits) when this mapper ignores pipeline-supplied peaks (e.g., Bt2446A::with_peaks(Nits::new_positive_or_panic(4000.0), Nits::new_positive_or_panic(100.0))).

When Some((src_peak, dst_peak)), the pipeline normalizes input/output strips to those exact peaks and skips redundant peak scanning.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§