Count the sensor's own numbers, so a cull can see headroom the render hides

FR-CULL-3's remaining two bullets. What existed was a *display* histogram
tagged FR-DSP-7: it binds AdjustPass's Rgba8Unorm output, recovers an 8-bit
code value, and counts clipping as `r == 255`. Its own documentation says a
clipped bin means "a highlight that is actually gone rather than one the
transform might still recover", which is the opposite of what a culling
decision needs. FR-CULL-3 asks for the histogram of the sensor data, on the
explicit grounds that a rendered image "systematically lies about what is
recoverable in the raw", and a readout that measures the render cannot answer
that however it is presented.

So this is a second instrument beside the first rather than a setting on it.
Both are true; they are true about different things; the panel offers both
behind a chip row and the words travel with the numbers, because a raw
saturation figure drawn under a heading saying Highlights would be mislabelled
exactly where the difference matters.

**What is reduced over, and what it cost to decide.** ARCH §5.5 specified the
pre-demosaic CFA samples. This reduces over the demosaiced scene-linear
texture instead, and §5.5 is amended to record the choice rather than let the
specification and the code disagree in silence. The texture is camera-native —
unbalanced, unmatrixed, uncurved — and normalised by the sensor's own black
and white levels, so 1.0 is saturation by construction and the distribution
below it is the headroom question with no calibration to carry. Retaining the
CFA samples would mean keeping the packed u32 buffer Demosaicer::run currently
drops: 48 MB at 24 MP, 120 MB at 60 MP, resident per open photograph whether
or not anyone looks at the histogram, on a platform §6.2 exists because memory
is scarce on.

Three things it therefore cannot say, written into the module docs and into
§5.5 rather than left to be discovered: it counts pixels not photosites, so a
saturated site drags its interpolated neighbours up and per-channel clipping
is smeared by about a demosaic kernel; it cannot see above white, because
demosaic.wgsl clamps each photosite at 1.0 for its own good reasons (a Canon
6D reads to 16383 against a declared 15070) so "at saturation" and "a stop
past it" share a bin; and it is measured after the CFA pattern is gone, so it
can name which colour clipped in the reconstructed image but not which
photosite went first.

The axis is stops below saturation, 16 bins per stop over 256 bins — the same
bin count the display reduction uses, so the fold into drawable columns is
shared and a divergence between the two plots would have to be deliberate. A
linear axis spends half its width on the top stop, which is why nobody has
ever drawn a useful linear raw histogram. The fourth series is the brightest
channel rather than luma: these values are unbalanced, so any weighted sum of
them is a number about nothing, and the brightest channel is the one that
saturates first and so the one the headroom question is actually about.

It is a property of the file and not of the render, which has two
consequences. It is computed once per photograph and cached — nothing
downstream of the demosaic can move a count in it — so a cull does not pay the
display histogram's per-frame cost three thousand times. And it describes the
whole frame rather than the visible region, deliberately opposite to
DevelopSession::histogram: a crop changes what is on screen and changes
nothing about what the sensor recorded.

Tags are on the reduction, the type, its constructor and the presentation
arithmetic, each of which has a test that fails if the behaviour goes. The
Slint panel and the push from lib.rs keep their reasoning as prose: nothing
asserts them, and a tag would claim coverage the assertions are not making.
This commit is contained in:
2026-08-29 23:36:21 +02:00
parent a2a0131693
commit 4b6c110816
10 changed files with 1793 additions and 47 deletions
+219 -1
View File
@@ -15,7 +15,7 @@ use std::sync::Arc;
use dr_decode::RawImage;
use dr_gpu::{
AdjustPass, DemosaicedImage, Demosaicer, FocusPeakPass, FocusPeaking, GpuContext, Histogram,
HistogramPass, MaskPass,
HistogramPass, MaskPass, RawHistogram, RawHistogramPass,
};
use dr_pipeline::mask::{MaskLayer, MaskSource};
@@ -724,6 +724,26 @@ pub struct DevelopSession {
/// photographer loses the histogram and keeps the photograph.
histogram: Option<HistogramPass>,
/// TRACES: FR-CULL-3
/// The raw-domain reduction, on the same terms as the display one above:
/// optional, because a session that cannot count the sensor data is still
/// a session that can develop it.
raw_histogram: Option<RawHistogramPass>,
/// The raw reading, once taken.
///
/// **Cached, where the display histogram is recomputed every settled
/// frame, and the difference is not an optimisation.** This measures the
/// demosaiced source, which nothing downstream of the demosaic can change:
/// no slider, no crop, no zoom, no output space moves a single count in
/// it. Recomputing it per frame would be a dispatch and a device sync
/// point spent to arrive back at the number already held — and on the
/// culling pass FR-CULL-3 is written for, that is a cost paid three
/// thousand times over.
///
/// `None` until first asked for, and it stays `None` on a file with no
/// sensor data behind it. The session is one photograph and the demosaiced
/// source is fixed for its life, so there is no invalidation to get wrong.
raw_counts: Option<RawHistogram>,
/// TRACES: FR-CULL-3
/// The focus-peaking overlay, on the same terms as the histogram above:
/// optional, because a device that cannot compile the pass is still a
/// device that can develop the photograph. What is lost is an instrument,
@@ -909,6 +929,10 @@ impl DevelopSession {
histogram: HistogramPass::new(ctx)
.inspect_err(|e| log::warn!("no histogram on this device: {e}"))
.ok(),
raw_histogram: RawHistogramPass::new(ctx)
.inspect_err(|e| log::warn!("no raw histogram on this device: {e}"))
.ok(),
raw_counts: None,
peak: FocusPeakPass::new(ctx)
.inspect_err(|e| log::warn!("no focus peaking on this device: {e}"))
.ok(),
@@ -2927,6 +2951,64 @@ impl DevelopSession {
.ok()
}
/// TRACES: FR-CULL-3
/// Whether there is sensor data behind this session at all.
///
/// False for the JPEG path, where [`DemosaicedImage::from_rgba8`] built
/// the source from an already-rendered image. There is no white level in
/// such a file and so no scale to measure headroom against: the honest
/// answer for one is that the raw instrument has nothing to say, which is
/// a different statement from a device that could not build the pass, and
/// the panel says the two differently.
pub fn has_sensor_data(&self) -> bool {
!self.demosaiced.is_non_linear()
}
/// TRACES: FR-CULL-3
/// Count the sensor data this photograph was demosaiced from.
///
/// **This is the other histogram, not a variant of the one above**, and
/// the two answer questions that a culling decision needs kept apart.
/// [`Self::histogram`] counts the frame on the canvas, after white
/// balance, the camera matrix, the base curve, the tone curve and the
/// output transform: a clipped bin there is a highlight that is gone as
/// the image currently stands. This counts the demosaiced scene-linear
/// texture, before any of that, on an axis of stops below sensor
/// saturation — so a clipped bin here is a highlight that is gone *in the
/// file*, and no edit will bring it back. FR-CULL-3 exists because the
/// tools that offer the second reading do not develop, and the ones that
/// develop offer only the first — "no shipping tool combines both".
///
/// **It describes the whole frame, not the visible region**, which is the
/// opposite of what [`Self::histogram`] does and deliberate. A crop and a
/// zoom change what is on screen; neither changes what the sensor
/// recorded, and the question this answers — how much latitude does this
/// exposure have — is asked of the capture rather than of the view.
///
/// Computed once and cached, for the reason `raw_counts` gives.
///
/// `None` where the file carries no sensor data, or where the device could
/// not build the reduction. The caller distinguishes those with
/// [`Self::has_sensor_data`].
pub fn raw_histogram(&mut self) -> Option<RawHistogram> {
if self.raw_counts.is_none() {
if !self.has_sensor_data() {
return None;
}
// Scoped so the shared borrow of the pass and of the source ends
// before the cache is written, rather than relying on the reader
// to see that the two field paths are disjoint.
let counted = {
let pass = self.raw_histogram.as_ref()?;
pass.compute(self.demosaiced.texture())
.inspect_err(|e| log::warn!("the raw histogram failed: {e}"))
.ok()
};
self.raw_counts = counted;
}
self.raw_counts.clone()
}
/// TRACES: FR-CULL-3
/// Whether this device could build the focus-peaking overlay.
///
@@ -5817,6 +5899,142 @@ mod tests {
assert_eq!(hist.clipped_shadows(), 32 * 64);
}
/// A flat Bayer frame whose every photosite normalises to `level`.
///
/// Black at zero and a power-of-two white level, so the normalisation is
/// exact and the value the raw histogram sees is the one this asked for
/// rather than one rounded by two divisions.
fn flat_raw(size: u32, level: f32) -> RawImage {
const WHITE: u16 = 16384;
let sample = (level * f32::from(WHITE)).round() as u16;
RawImage {
width: size,
height: size,
data: vec![sample; (size * size) as usize],
cfa_pattern: dr_decode::CfaPattern::Rggb,
black_level: [0; 4],
white_level: WHITE,
wb_coeffs: [1.0, 1.0, 1.0, 1.0],
color_matrix: None,
base_curve: dr_decode::BaseCurve::IDENTITY,
crop: dr_decode::CropRect {
x: 0,
y: 0,
width: size,
height: size,
},
}
}
/// TRACES: FR-CULL-3
#[test]
fn the_raw_histogram_describes_the_file_and_not_the_view() {
// **The property that makes it a second instrument rather than a
// second rendering of the first**, and the one every other test here
// would pass without. The display histogram beside it deliberately
// follows the edit and the visible region — that is what FR-DSP-7
// asks of it. This must do neither: cropping away half the photograph
// changes what is on the canvas and changes nothing about what the
// sensor recorded, and a culler asking how much latitude an exposure
// has is asking about the capture.
//
// Reading the adjusted output by mistake would pass a plausible-looking
// plot back — which is exactly why this asserts the *denominator* and
// the bin, not merely that something was counted.
let Ok(ctx) = pollster::block_on(dr_gpu::GpuContext::new_headless()) else {
log::warn!("no GPU adapter; skipping");
return;
};
// 0.234253 is the centre of the bin 33 sixteenths below saturation —
// mid-bin on purpose, so the assertion is about the reduction rather
// than about how this machine's `log2` rounds an exact tie.
let raw = flat_raw(16, 3838.0 / 16384.0);
let mut session =
DevelopSession::open(&ctx, &raw, dr_types::Orientation::NORMAL).expect("session");
let before = session.raw_histogram().expect("a raw session must count");
assert_eq!(before.pixels(), 16 * 16);
assert_eq!(
before.red()[dr_gpu::RAW_HISTOGRAM_BINS - 1 - 33],
16 * 16,
"a flat frame two stops down did not land in one bin"
);
assert_eq!(before.saturated(), 0, "nothing here is at the white level");
assert_eq!(before.at_black(), 0);
session.set_crop(CropRect {
x: 0.0,
y: 0.0,
width: 0.5,
height: 1.0,
});
session.render(16, 16).expect("render");
// The display histogram followed the crop, as it is supposed to.
// Asserted as an inequality rather than an exact figure: how a half
// crop of a 16px frame rounds to a viewport is `AdjustPass`'s
// business and has its own tests, and pinning it here would make this
// test fail for a reason it is not about.
let shown = session.histogram().expect("histogram");
assert!(
shown.pixels() < 16 * 16,
"the crop did not reach the display histogram, so this proves nothing"
);
// The raw one did not.
let after = session.raw_histogram().expect("raw histogram");
assert_eq!(
after, before,
"the raw reading followed the crop, so it is measuring the render"
);
}
/// TRACES: FR-CULL-3
#[test]
fn a_blown_frame_reads_as_clipped_in_the_raw_domain() {
// The other end, and the reason the requirement exists. Every
// photosite at the white level is a photograph with no highlight
// headroom left in the file — no edit recovers it — and the instrument
// has to say so in the same terms whatever the develop chain currently
// makes of it.
let Ok(ctx) = pollster::block_on(dr_gpu::GpuContext::new_headless()) else {
log::warn!("no GPU adapter; skipping");
return;
};
let raw = flat_raw(16, 1.0);
let mut session =
DevelopSession::open(&ctx, &raw, dr_types::Orientation::NORMAL).expect("session");
let hist = session.raw_histogram().expect("raw histogram");
assert_eq!(hist.pixels(), 16 * 16);
assert_eq!(hist.saturated(), 16 * 16);
assert_eq!(hist.red()[dr_gpu::RAW_HISTOGRAM_BINS - 1], 16 * 16);
}
/// TRACES: FR-CULL-3
#[test]
fn a_file_with_no_sensor_data_has_no_raw_reading_rather_than_a_wrong_one() {
// The JPEG path. Its source texture is gamma-encoded and carries no
// white level, so there is no scale to measure headroom against — and
// counting it anyway would produce a confident plot of a quantity that
// does not exist, which is the failure mode an instrument must not
// have. The panel says there is nothing to say.
let Ok(ctx) = pollster::block_on(dr_gpu::GpuContext::new_headless()) else {
log::warn!("no GPU adapter; skipping");
return;
};
let rgba = split_frame(16);
let mut session =
DevelopSession::open_rgb(&ctx, &rgba, 16, 16, dr_types::Orientation::NORMAL)
.expect("session");
assert!(!session.has_sensor_data());
assert!(session.raw_histogram().is_none());
}
#[test]
fn routing_indices_map_back_to_the_right_parameter() {
// A wrong index would silently move the wrong slider's value, which
+354 -8
View File
@@ -1,6 +1,17 @@
//! TRACES: FR-DSP-7
//! TRACES: FR-DSP-7 | FR-CULL-3
//! Turning bin counts into something a 280-pixel column can be read from.
//!
//! **Two instruments through one panel.** `dr_gpu` produces two reductions
//! that answer different questions — [`dr_gpu::Histogram`] counts the frame the
//! display is about to show (FR-DSP-7), [`dr_gpu::RawHistogram`] counts the
//! sensor data the file actually holds (FR-CULL-3) — and both are drawn by the
//! same plot, from the same [`HistogramView`], with a chip row to choose
//! between them. That is why the axis caption, the two clipping titles and the
//! empty-state line are all fields of the view rather than literals in Slint:
//! the words differ between the two readings as much as the numbers do, and a
//! panel that drew the raw counts under headings saying Shadows and Highlights
//! would be mislabelling them precisely where it matters.
//!
//! `dr_gpu` counts; this decides what the counting looks like. The split is
//! ARCH §4.3a's: how many columns a panel can show, which peak to scale
//! against and when a handful of specular pixels is worth an alarm are all
@@ -14,7 +25,7 @@
//! right shape look equally plausible. So they are all reachable without a GPU,
//! a window or a photograph.
use dr_gpu::{Histogram, HISTOGRAM_BINS};
use dr_gpu::{Histogram, RawHistogram, HISTOGRAM_BINS, RAW_HISTOGRAM_BINS};
use crate::HistogramView;
@@ -40,14 +51,48 @@ pub(crate) const COLUMNS: usize = 64;
/// stops being an artefact and starts being a decision.
const CLIP_VISIBLE: f32 = 0.001;
/// The whole panel's state, for one counted frame.
/// The words the display reading is drawn under.
///
/// Here rather than in the panel because the raw reading beside it uses
/// different ones for the same two counters, and a heading that stayed put
/// while the numbers under it changed meaning would be worse than no heading.
const DISPLAY_LOW: &str = "Shadows";
const DISPLAY_HIGH: &str = "Highlights";
/// What the plot's horizontal axis is, said once in the chip row's hint.
///
/// Short on purpose, and this is a layout constraint rather than a stylistic
/// one: the hint is an unwrapped `Text` in a `FieldRow`, so its natural width
/// is this panel's preferred width, and the develop column takes the largest
/// preferred width of any panel in it. A sentence here would hold the whole
/// sidebar open.
const DISPLAY_AXIS: &str = "output levels, 0 to 255";
/// What the plot says when nothing has been counted.
const NO_FRAME: &str = "no frame yet";
/// What a figure reads before there is a frame behind it.
const UNKNOWN: &str = "—";
/// The two reductions must agree on their bin count.
///
/// [`scaled`] folds either of them, and the fold is only free of a comb
/// because [`COLUMNS`] divides the bin count exactly. Asserted at compile time
/// rather than tested, so a change in `dr_gpu` stops the build here with this
/// sentence attached instead of drawing a plot with structure the photograph
/// does not have.
const _: () = assert!(RAW_HISTOGRAM_BINS == HISTOGRAM_BINS);
/// The same three, for the raw reading.
const RAW_LOW: &str = "Black";
const RAW_HIGH: &str = "Saturated";
const RAW_AXIS: &str = "stops below saturation";
/// The whole panel's state, for one counted display frame.
pub(crate) fn view(hist: &Histogram) -> HistogramView {
HistogramView {
// Zero pixels means nothing has been rendered yet, not an empty
// photograph — the panel draws its frame and no data rather than a flat
// line, which would be a claim about an image that does not exist.
available: hist.pixels() > 0,
luma: model(&scaled(hist.luma())),
overall: model(&scaled(hist.luma())),
red: model(&scaled(hist.red())),
green: model(&scaled(hist.green())),
blue: model(&scaled(hist.blue())),
@@ -55,14 +100,19 @@ pub(crate) fn view(hist: &Histogram) -> HistogramView {
shadows_clipped: fraction(hist.clipped_shadows(), hist.pixels()) >= CLIP_VISIBLE,
highlights_label: percentage(hist.clipped_highlights(), hist.pixels()).into(),
shadows_label: percentage(hist.clipped_shadows(), hist.pixels()).into(),
low_title: DISPLAY_LOW.into(),
high_title: DISPLAY_HIGH.into(),
hint: DISPLAY_AXIS.into(),
unavailable: NO_FRAME.into(),
}
}
/// An empty panel — no image open, or a frame that could not be counted.
/// An empty display panel — no image open, or a frame that could not be
/// counted.
pub(crate) fn empty() -> HistogramView {
HistogramView {
available: false,
luma: model(&[0.0; COLUMNS]),
overall: model(&[0.0; COLUMNS]),
red: model(&[0.0; COLUMNS]),
green: model(&[0.0; COLUMNS]),
blue: model(&[0.0; COLUMNS]),
@@ -72,11 +122,172 @@ pub(crate) fn empty() -> HistogramView {
// frame that does not exist is the one thing an instrument must not do.
highlights_label: UNKNOWN.into(),
shadows_label: UNKNOWN.into(),
low_title: DISPLAY_LOW.into(),
high_title: DISPLAY_HIGH.into(),
hint: DISPLAY_AXIS.into(),
unavailable: NO_FRAME.into(),
}
}
/// What a figure reads before there is a frame behind it.
const UNKNOWN: &str = "—";
/// The same panel, for one counted sensor frame.
///
/// Untagged, deliberately. Every judgement it makes — the fold, the scale, the
/// clipping threshold, the headroom figure, the words — belongs to a function
/// beside it that is tested without a device; this is the assembly, and a tag
/// here would claim coverage that the assertions are not making. See
/// `CONTRIBUTING.md` on closing a requirement with a test that would fail if
/// the behaviour were removed.
///
/// **Every number here is folded and scaled by the code the display reading
/// uses**, and that is deliberate rather than convenient: the two plots are
/// drawn in the same box, in the same column, one chip apart, and a difference
/// in how they are folded or what they are scaled against would read as a
/// difference in the photograph. What changes between them is the axis and the
/// words for it, and nothing else.
///
/// The shared fold is only sound because the two reductions have the same bin
/// count. That is a compile-time fact rather than a hope — `scaled` takes
/// `&[u32; HISTOGRAM_BINS]` and is handed `&[u32; RAW_HISTOGRAM_BINS]`, so if
/// `dr_gpu` ever changed one of them this file would stop compiling rather
/// than start drawing a comb.
pub(crate) fn raw_view(hist: &RawHistogram) -> HistogramView {
HistogramView {
available: hist.pixels() > 0,
// The brightest channel where the display reading draws luma — see
// `RawHistogram::brightest` for why a weighted sum of camera-native
// values would be a number about nothing.
overall: model(&scaled(hist.brightest())),
red: model(&scaled(hist.red())),
green: model(&scaled(hist.green())),
blue: model(&scaled(hist.blue())),
highlights_clipped: fraction(hist.saturated(), hist.pixels()) >= CLIP_VISIBLE,
shadows_clipped: fraction(hist.at_black(), hist.pixels()) >= CLIP_VISIBLE,
highlights_label: percentage(hist.saturated(), hist.pixels()).into(),
shadows_label: percentage(hist.at_black(), hist.pixels()).into(),
low_title: RAW_LOW.into(),
high_title: RAW_HIGH.into(),
// The headroom figure rather than the axis, when there is one. It is
// the answer FR-CULL-3 is written for — "the embedded JPEG's histogram
// misrepresents available highlight headroom" — and a photographer
// culling a folder wants the number, not a reminder of what the axis
// is.
hint: headroom_label(hist.brightest(), hist.pixels()).into(),
unavailable: NO_FRAME.into(),
}
}
/// Why there is no raw reading to draw.
///
/// Three genuinely different answers, and the panel must not flatten them into
/// one. "Nothing is open" is a state that passes; "this file has no sensor
/// data" is permanent for this photograph and says the instrument does not
/// apply; "this device could not build the reduction" says it applies and is
/// missing, which is the one worth reporting as a fault.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum RawAbsence {
/// No photograph open, or none rendered yet.
NoImage,
/// The file was never raw — a JPEG, or anything else already rendered.
NotRaw,
/// The device could not compile or run the reduction.
NoDevice,
}
impl RawAbsence {
/// What the empty plot says. Kept to a few words for the width reason
/// [`DISPLAY_AXIS`] gives.
fn note(self) -> &'static str {
match self {
RawAbsence::NoImage => NO_FRAME,
RawAbsence::NotRaw => "no sensor data",
RawAbsence::NoDevice => "no reduction here",
}
}
}
/// TRACES: FR-CULL-3
/// An empty raw panel, saying which of the three reasons applies.
pub(crate) fn raw_empty(why: RawAbsence) -> HistogramView {
HistogramView {
available: false,
overall: model(&[0.0; COLUMNS]),
red: model(&[0.0; COLUMNS]),
green: model(&[0.0; COLUMNS]),
blue: model(&[0.0; COLUMNS]),
highlights_clipped: false,
shadows_clipped: false,
highlights_label: UNKNOWN.into(),
shadows_label: UNKNOWN.into(),
low_title: RAW_LOW.into(),
high_title: RAW_HIGH.into(),
hint: RAW_AXIS.into(),
unavailable: why.note().into(),
}
}
/// TRACES: FR-CULL-3
/// How far the brightest real content sits below sensor saturation, in stops.
///
/// Takes the counts rather than the [`RawHistogram`] they came out of, on the
/// same principle as everything else in this file: a `RawHistogram` can only
/// be produced by a device, and every judgement here is one that shows up as a
/// wrong number rather than as a crash.
///
/// **The top [`CLIP_VISIBLE`] of the frame is skipped**, for the same reason
/// the clipping indicator has a threshold at all: almost every photograph has
/// a few pixels at an extreme — a specular glint on chrome, a hot pixel, the
/// sun itself — and a headroom figure that read 0.0 on all of them would be a
/// figure nobody looks at twice. What is wanted is the highlight a
/// photographer is protecting, not the brightest pixel in the file.
///
/// Measured on the brightest channel, because that is the one that saturates
/// first and so the one that decides whether a pixel survives.
///
/// `None` where nothing has been counted — distinct from zero, which is a
/// photograph with no headroom left at all.
pub(crate) fn headroom(brightest: &[u32; RAW_HISTOGRAM_BINS], pixels: u32) -> Option<f32> {
if pixels == 0 {
return None;
}
// `max(1)` so the same count that lights the clipping indicator also moves
// this figure: without it a frame sitting exactly on the threshold would
// read "clipped" beside "three stops of headroom", and a panel that
// contradicts itself in two adjacent readouts is one nobody trusts again.
// It also keeps a small frame — a proxy, a thumbnail — from ignoring
// everything, since a thousandth of it rounds to none.
let ignore = ((pixels as f32 * CLIP_VISIBLE) as u32).max(1);
let mut above = 0u32;
for (bin, count) in brightest.iter().enumerate().rev() {
above = above.saturating_add(*count);
if above >= ignore {
return Some(RawHistogram::stops(bin));
}
}
// Unreachable while `pixels` is non-zero — the counts sum to it, and
// `ignore` is a thousandth of it — but the floor of the axis is the honest
// answer to a frame with nothing in any bin, and a panic is not.
Some(RawHistogram::stops(0))
}
/// TRACES: FR-CULL-3
/// The headroom figure as the chip row's hint reads it.
pub(crate) fn headroom_label(brightest: &[u32; RAW_HISTOGRAM_BINS], pixels: u32) -> String {
match headroom(brightest, pixels) {
// Nothing counted: say what the axis is, since there is no figure to
// put on it.
None => RAW_AXIS.into(),
// The end of the scale is said in words, not as `0.0`. They are the
// same fact and not the same sentence: a measurement that came out
// small reads differently from one that ran out, and on a culling pass
// that is the difference between "tight" and "gone". Only the top bin
// produces it — the axis is divided finely enough that the next one
// down is already a tenth of a stop.
Some(stops) if stops <= 0.0 => "no headroom left".into(),
// Rounded to a tenth, which is finer than any decision made from it
// and coarse enough not to flicker between two frames of one scene.
Some(stops) => format!("{stops:.1} stops of headroom"),
}
}
fn model(heights: &[f32; COLUMNS]) -> slint::ModelRc<f32> {
slint::ModelRc::new(slint::VecModel::from(heights.to_vec()))
@@ -317,6 +528,141 @@ mod tests {
assert!(!blank.highlights_clipped && !blank.shadows_clipped);
}
/// A brightest-channel series with `count` pixels this many bins below
/// saturation, and nothing else.
fn brightest(entries: &[(usize, u32)]) -> [u32; RAW_HISTOGRAM_BINS] {
let mut out = [0u32; RAW_HISTOGRAM_BINS];
for (below, count) in entries {
out[RAW_HISTOGRAM_BINS - 1 - *below] = *count;
}
out
}
/// TRACES: FR-CULL-3
#[test]
fn headroom_is_the_distance_from_the_brightest_content_to_saturation() {
// The figure FR-CULL-3 is written for, in the units it is written in.
// A frame whose highest content sits two stops down has two stops of
// latitude, and one whose content reaches the white level has none —
// and the difference between those two answers is the whole of whether
// a photograph is worth keeping.
let per_stop = dr_gpu::RAW_HISTOGRAM_BINS_PER_STOP;
let two_down = headroom(&brightest(&[(2 * per_stop, 10_000)]), 10_000);
assert_eq!(two_down, Some(2.0));
let at_the_top = headroom(&brightest(&[(0, 10_000)]), 10_000);
assert_eq!(at_the_top, Some(0.0));
// Nothing counted is not the same statement as no headroom, and the
// panel must not turn the first into the second.
assert_eq!(headroom(&[0u32; RAW_HISTOGRAM_BINS], 0), None);
}
/// TRACES: FR-CULL-3
#[test]
fn headroom_ignores_a_handful_of_specular_pixels() {
// **The reason the figure is a percentile and not a maximum.** Almost
// every photograph has a few pixels at the ceiling — a glint on
// chrome, a hot pixel, the sun in the corner — and a headroom readout
// that reported 0.0 on all of them would be a readout nobody looks at
// twice. The threshold is the same one the clipping indicator uses,
// for the same reason.
let per_stop = dr_gpu::RAW_HISTOGRAM_BINS_PER_STOP;
let pixels = 1_000_000u32;
let threshold = (CLIP_VISIBLE * pixels as f32) as u32;
let glint = threshold - 1;
let with_glint = brightest(&[(0, glint), (3 * per_stop, pixels - glint)]);
assert_eq!(
headroom(&with_glint, pixels),
Some(3.0),
"a specular glint took three stops of headroom off the reading"
);
// At the threshold it is no longer a glint, and the figure has to
// follow the data — this is the same count that lights the clipping
// indicator, and the two readouts sit an inch apart.
let real = brightest(&[(0, threshold), (3 * per_stop, pixels - threshold)]);
assert_eq!(headroom(&real, pixels), Some(0.0));
}
/// TRACES: FR-CULL-3
#[test]
fn the_headroom_hint_says_none_rather_than_rounding_to_zero() {
// `0.0 stops of headroom` and `no headroom left` are the same fact and
// not the same sentence: the first reads as a measurement that came
// out small, the second as the end of the scale. On a culling pass
// this is the difference between "tight" and "gone".
let per_stop = dr_gpu::RAW_HISTOGRAM_BINS_PER_STOP;
assert_eq!(
headroom_label(&brightest(&[(2 * per_stop, 100)]), 100),
"2.0 stops of headroom"
);
assert_eq!(headroom_label(&brightest(&[(0, 100)]), 100), "no headroom left");
// One bin down is a sixteenth of a stop, which is a real if small
// amount of latitude and must not be rounded away into the sentence
// above it.
assert_eq!(
headroom_label(&brightest(&[(1, 100)]), 100),
"0.1 stops of headroom"
);
// And with nothing counted the hint falls back to naming the axis,
// rather than stating a figure about a photograph nobody has read.
assert_eq!(headroom_label(&[0u32; RAW_HISTOGRAM_BINS], 0), RAW_AXIS);
}
/// TRACES: FR-CULL-3
#[test]
fn an_absent_raw_reading_says_which_kind_of_absent_it_is() {
// Three different facts, and flattening them loses the one that
// matters. "No sensor data" is permanent for this photograph and means
// the instrument does not apply; "no reduction here" means it applies
// and this device could not build it, which is a fault worth
// reporting; "no frame yet" passes on its own. A single "unavailable"
// would have a photographer looking for a driver problem on a JPEG.
let notes: Vec<String> = [
RawAbsence::NoImage,
RawAbsence::NotRaw,
RawAbsence::NoDevice,
]
.iter()
.map(|why| raw_empty(*why).unavailable.to_string())
.collect();
for note in &notes {
assert!(!note.is_empty(), "an empty raw panel said nothing at all");
assert_eq!(
notes.iter().filter(|n| *n == note).count(),
1,
"two reasons read identically: {note}"
);
}
// And an empty panel never states a clipping figure — the failure the
// display panel's own test describes, in the instrument where it would
// be worse, because a raw clipping claim is a claim about the file.
let blank = raw_empty(RawAbsence::NotRaw);
assert!(!blank.available);
assert_eq!(blank.highlights_label, UNKNOWN);
assert_eq!(blank.shadows_label, UNKNOWN);
assert!(!blank.highlights_clipped && !blank.shadows_clipped);
}
/// TRACES: FR-CULL-3
#[test]
fn the_two_readings_are_labelled_apart() {
// The panel draws both in the same box, one chip apart. If they shared
// their headings, a photographer who had forgotten which was selected
// would read a raw saturation figure as a display clipping figure —
// and those disagree by exactly the amount FR-CULL-3 exists to expose.
let display = empty();
let raw = raw_empty(RawAbsence::NoImage);
assert_ne!(display.low_title, raw.low_title);
assert_ne!(display.high_title, raw.high_title);
assert_ne!(display.hint, raw.hint);
}
#[test]
fn the_indicator_ignores_a_handful_of_specular_pixels() {
// The threshold, from both sides. Below it the marker stays dark while
+38
View File
@@ -320,6 +320,11 @@ fn reset_view_state(window: &AppWindow) {
// beside the next one's filename is a confident, precise lie, and the gap
// before the new frame settles is exactly long enough to read it.
window.set_histogram(histogram::empty());
// And the raw reading with it. It belongs to one file more completely than
// the display histogram does — nothing about the edit can move it — which
// makes leaving it standing over the next photograph's filename worse
// rather than better: it would look exactly as current as it is wrong.
window.set_raw_histogram(histogram::raw_empty(histogram::RawAbsence::NoImage));
// TRACES: FR-CULL-3
// The marks go down with it, and for the same reason. What is *not* reset
// is whether peaking is switched on: that is a way of looking at a folder
@@ -1669,6 +1674,32 @@ pub fn run(paths: Vec<PathBuf>) -> Result<()> {
.as_ref()
.map_or_else(histogram::empty, histogram::view),
);
// **The raw reading, pushed on the same path but not
// recomputed on it.** `DevelopSession::raw_histogram`
// caches: the demosaiced source is fixed for the life
// of the session, so this costs one dispatch per
// photograph and a clone of four kilobytes per settled
// frame thereafter. It is pushed from here anyway
// rather than once at open, because this is the one
// path every photograph takes and a panel that had to
// be told separately would be empty on some route into
// develop mode.
//
// Three outcomes, said apart. A file that was never
// raw has no white level and so no scale to measure
// headroom against; a device that could not build the
// reduction has a fault worth reporting; and only the
// third is a reading.
let raw = if !s.has_sensor_data() {
histogram::raw_empty(histogram::RawAbsence::NotRaw)
} else {
s.raw_histogram().as_ref().map_or_else(
|| histogram::raw_empty(histogram::RawAbsence::NoDevice),
histogram::raw_view,
)
};
window.set_raw_histogram(raw);
}
// TRACES: FR-CULL-3 | NFR-P14
@@ -1705,6 +1736,13 @@ pub fn run(paths: Vec<PathBuf>) -> Result<()> {
// No frame, so nothing to describe. The stale plot would
// otherwise sit beside the error message looking current.
window.set_histogram(histogram::empty());
// **The raw reading is left standing, and that is not an
// oversight.** It describes the sensor data behind the
// session, which a failed *render* says nothing about: the
// demosaic succeeded or there would be no session at all.
// Emptying it here would take away the one instrument
// still telling the truth, at the moment the other one
// stopped.
// TRACES: FR-CULL-3
// And nothing to mark. Focus marks over the last frame
// that rendered, beside a message saying this one did not,
+11
View File
@@ -72,6 +72,16 @@ export component AppWindow inherits Window {
/// of a draft frame is a histogram of an image nobody is reading.
in property <HistogramView> histogram;
/// The same photograph counted in the raw domain — the demosaiced
/// scene-linear texture, before white balance, the camera matrix or any
/// curve, on an axis of stops below sensor saturation.
///
/// Beside the display histogram rather than instead of it: the two answer
/// different questions and the panel offers both. Unlike the one above it
/// this is a property of the *file* and not of the render, so it survives
/// a crop, a zoom and every slider — see `raw_histogram.rs`.
in property <HistogramView> raw-histogram;
/// TRACES: FR-CULL-3
/// Focus peaking: the marks, whether they describe *this* frame, and the
/// three things the photographer chose. All of them are Rust's, because
@@ -2251,6 +2261,7 @@ in property <bool> panel-visible: true;
// answer than an honest one that is not yet scoped.
HistogramPanel {
data: root.histogram;
raw-data: root.raw-histogram;
}
Rectangle {
+102 -18
View File
@@ -1,6 +1,22 @@
// TRACES: FR-DSP-7
// The live histogram, and what it says about clipping.
//
// **One plot, two readings, and a chip row to choose.** The panel draws either
// the frame the display is about to show (FR-DSP-7) or the sensor data the
// file holds (FR-CULL-3), from the same `HistogramView` and through the same
// traces. They are not two presentations of one measurement: the first is
// after white balance, the camera matrix and the whole tone chain, the second
// is before any of it on an axis of stops below sensor saturation, and
// FR-CULL-3 exists precisely because the first "systematically lies about what
// is recoverable in the raw". A culling decision needs the second; an export
// decision needs the first. Both are true.
//
// **So the words travel with the numbers.** The axis caption, the two clipping
// titles and the empty-state line are fields of the view rather than literals
// here, because they differ between the two readings — a raw saturation figure
// drawn under a heading saying Highlights would be mislabelled exactly where
// the difference matters.
//
// **Why this is hand-built when the rest of the column is generated.** The
// develop panel is built from operation capabilities, and a histogram is not an
// operation: it has no parameters, changes nothing about the photograph, and
@@ -17,6 +33,7 @@
import { Theme } from "theme.slint";
import { PanelHeading, Caption, Panel } from "widgets.slint";
import { Segmented } from "controls.slint";
// One counted frame, ready to draw.
//
@@ -29,7 +46,11 @@ export struct HistogramView {
// of one.
available: bool,
luma: [float],
// The aggregate trace, drawn filled behind the three channels: Rec.709
// luma for the display reading, the brightest channel for the raw one. In
// both cases the single curve exposure is read off — which is why it is
// named for its role in the plot rather than for either quantity.
overall: [float],
red: [float],
green: [float],
blue: [float],
@@ -43,6 +64,26 @@ export struct HistogramView {
// being unable to tell the two apart.
highlights-label: string,
shadows-label: string,
// What the two clipping readouts are called. Different words for the two
// readings — Shadows and Highlights describe a rendering, Black and
// Saturated describe a sensor — and they belong to the reading rather than
// to the panel for that reason.
low-title: string,
high-title: string,
// What the axis is, or what it currently says: the chip row's hint. For
// the raw reading this is the headroom figure, which is the answer
// FR-CULL-3 is written for.
//
// Kept short in Rust, and that is a layout constraint: `FieldRow` draws
// the hint as an unwrapped Text, so its natural width becomes this panel's
// preferred width and the develop column takes the largest of those.
hint: string,
// What the plot says when there is nothing to draw. Never "0%" and never
// an empty plot — both are claims about a photograph nobody has counted.
unavailable: string,
}
// One series, as a run of columns.
@@ -121,7 +162,26 @@ component ClipReadout inherits HorizontalLayout {
// TRACES: FR-DSP-7
export component HistogramPanel inherits Rectangle {
/// The frame the display is about to show (FR-DSP-7).
in property <HistogramView> data;
/// The sensor data the file holds (FR-CULL-3).
in property <HistogramView> raw-data;
/// Which of the two is on the plot. 0 is the display reading.
///
/// **Held here rather than pushed from Rust**, and for the reason
/// `chosen-peaking` gives about the focus overlay: this is a way of
/// *looking* rather than a property of a photograph. Someone culling three
/// thousand frames switches to the raw reading once, and a mode that reset
/// with each image would ask them to switch it three thousand times. Both
/// views are pushed on every settled frame, so the choice needs no
/// callback and no round trip — the raw one costs nothing to keep current,
/// because it is computed once per photograph and cached.
property <int> mode: 0;
/// The reading currently drawn. Everything below reads this and not the
/// two above it, so there is exactly one place the choice is made.
property <HistogramView> shown: root.mode == 1 ? root.raw-data : root.data;
/// TRACES: FR-UI-2
@@ -150,6 +210,21 @@ export component HistogramPanel inherits Rectangle {
PanelHeading { text: "HISTOGRAM"; }
// **Above the plot rather than below it**, because it says what the
// plot *is*: a photographer glancing at a shape has to know which of
// the two measurements they are looking at before they read it, not
// after. Two chips wide, which fits the narrowest column the
// application supports without the row setting the sidebar's width —
// see `ChipGrid`.
Segmented {
label: "Measured on";
hint: root.shown.hint;
options: ["Display", "Raw"];
selected: root.mode;
columns: 2;
picked(i) => { root.mode = i; }
}
Rectangle {
// Tall enough to read a shape off and no taller. The column is the
// photographer's instrument panel, and every pixel this takes is a
@@ -163,10 +238,17 @@ export component HistogramPanel inherits Rectangle {
// row of pixels inside the box rather than over its edge.
clip: true;
// Quarter gridlines, so a tone can be placed on the axis without
// Quarter gridlines, so a value can be placed on the axis without
// counting. Same construction and same weight as the tone curve's,
// because the two plots sit in one column and any difference
// between them would read as meaning something.
//
// They read as quarters of whichever axis is showing: 64 output
// levels for the display reading, four stops for the raw one. That
// is why the axis is named in the chip row's hint rather than
// labelled here — one set of gridlines cannot carry two scales,
// and drawing numbers against them would make one of the two
// readings wrong.
for i in [1, 2, 3]: Rectangle {
x: parent.width * i / 4;
width: 1px;
@@ -174,12 +256,14 @@ export component HistogramPanel inherits Rectangle {
opacity: 0.5;
}
// Luminance first, so it sits behind: it is the envelope the three
// channels decompose, and a filled area drawn over a line hides it.
// The aggregate first, so it sits behind: Rec.709 luma for the
// display reading, the brightest channel for the raw one, and in
// both cases the curve the three channels sit under. A filled area
// drawn over a line hides it.
Trace {
width: 100%;
height: 100%;
heights: root.data.luma;
heights: root.shown.overall;
ink: Theme.plot-luma;
filled: true;
}
@@ -190,21 +274,21 @@ export component HistogramPanel inherits Rectangle {
Trace {
width: 100%;
height: 100%;
heights: root.data.red;
heights: root.shown.red;
ink: Theme.plot-red;
shade: 0.85;
}
Trace {
width: 100%;
height: 100%;
heights: root.data.green;
heights: root.shown.green;
ink: Theme.plot-green;
shade: 0.85;
}
Trace {
width: 100%;
height: 100%;
heights: root.data.blue;
heights: root.shown.blue;
ink: Theme.plot-blue;
shade: 0.85;
}
@@ -220,24 +304,24 @@ export component HistogramPanel inherits Rectangle {
width: 2px;
height: 100%;
background: Theme.warn-ink;
visible: root.data.shadows-clipped;
visible: root.shown.shadows-clipped;
}
Rectangle {
x: parent.width - self.width;
width: 2px;
height: 100%;
background: Theme.warn-ink;
visible: root.data.highlights-clipped;
visible: root.shown.highlights-clipped;
}
// Nothing rendered yet. Said rather than shown as an empty plot,
// which would be a claim that the photograph has no tones in it.
Caption {
text: "no frame yet";
text: root.shown.unavailable;
width: 100%;
horizontal-alignment: center;
y: (parent.height - self.height) / 2;
visible: !root.data.available;
visible: !root.shown.available;
}
}
@@ -247,17 +331,17 @@ export component HistogramPanel inherits Rectangle {
// controls below it up and back down as each image opens.
HorizontalLayout {
ClipReadout {
title: "Shadows";
figure: root.data.shadows-label;
lit: root.data.shadows-clipped;
title: root.shown.low-title;
figure: root.shown.shadows-label;
lit: root.shown.shadows-clipped;
}
Rectangle { horizontal-stretch: 1; }
ClipReadout {
title: "Highlights";
figure: root.data.highlights-label;
lit: root.data.highlights-clipped;
title: root.shown.high-title;
figure: root.shown.highlights-label;
lit: root.shown.highlights-clipped;
}
}
}