Skip to content

The Math

botrx555 edited this page Jul 14, 2026 · 3 revisions

The Math

The processing maths behind the refinements. Constants and formulas below are the tool's actual values.

Black level (-b)

A raw developer derives white balance from the black point, and the M8's green channel sits slightly above red and blue. Consequences:

  • too low a black level tints shadows green;
  • too high tints them magenta.

Bare -b writes the reference value 92; nudge per body or taste. With no -b, no BlackLevel tag is written and the developer assumes 0. -b writes a tag only - the raw pixel values are never edited (confirmed: the CFA is byte-identical at any -b value).

FNumber / aperture recovery (un-coded lens)

All estimators work in APEX units: Av = 2*log2(f) (aperture), Bv (brightness), Sv = log2(ISO/100) (speed) and Tv = log2(1/t) (time), with the written FNumber = 2^(Av/2) encoded as n/65536. This is why no logarithm appears in the FNumber write path in either tool - the only log is on the reverse MaxApertureValue path.

The M8 records no aperture for an un-coded lens. Three paths, in order of preference:

  1. Stored MeterOffset (--calibrate-fnumber). The aperture is recovered from the two light meters:

    Av_APEX  =  -( MeasuredLV - ExternalSensorBrightnessValue )  +  offset
    

    with the slope locked at -1, then snapped to the lens's half-stop grid from lensdb.ini. If an ImageCalib constant is also stored, a directional meter-vs-image cross-check runs automatically (when the two independent estimates diverge past ~2.5 stops the meter reading is treated as corrupt and the calibrated image estimate is used instead - this catches a glitched external-meter cell). Concrete case from the validation corpus: one frame's external-meter tag (0x0311) read -1.09 against neighbours around +2.4, corrupting the meter difference; every meter path then resolved f/3.4, and the cross-check correctly substituted the calibrated image estimate at f/11 against a true f/9.5. A black-frame guard falls back to the pure-image formula when the frame carries no image signal (a capped or blocked lens), so a long dark exposure is still read as correctly stopped down.

  2. Default estimate (no stored offset). A deterministic per-frame estimate anchored on ExternalSensorBrightnessValue.

  3. Alternative estimators. --mimic-fnumber reproduces the original tool's compressed-path estimate (base-ISO only); --legacy-fnumber uses the image-brightness constant only, ignoring the meters; --selfcal derives a batch offset from the camera's own ApproxF over a >=8-frame batch (off by default - batch-dependent).

-A/--aperture overrides all of the above with an exact value and yields a 100% byte-identical DNG.

Calibrating the offset

--calibrate-fnumber AP[,AP...] derives the per-(body, lens) meter offset from known-aperture frames: shoot 5-10 frames (auto exposure is fine) at the listed apertures in filename order, pass the apertures comma-separated and the lens code with -l. The tool stores MeterOffset.<code> under [serial] in sensdb.ini, plus a meter-free image constant ImageCalib = median(Av_known - (-Bv + Sv - Tv)) used for the cross-check. One calibration per (body, lens); afterwards every conversion recovers the true aperture with no -A.

Accuracy vs the original's estimate

The original tool synthesises FNumber from image brightness, which tracks the camera's own approximate estimate closely (r ~0.97) rather than true aperture.

Measured against dialled ground truth, on real-aperture frames, the two compare like this:

  • The original lands slightly more exact half-stop hits, but with larger mean and RMS error overall.
  • The deterministic default (ExtBV-anchored) trades a little exact-agreement for lower average error and, crucially, batch-independence: the same frame resolves to the same aperture regardless of what else is in the batch. The old self-calibration was batch-dependent; the default is not.
  • Pure image-brightness, the camera's own ApproxF, and --mimic-fnumber are all less accurate than either.

The tightest path, and the crucial refinement here, is a stored MeterOffset (--calibrate-fnumber): calibrated once per (body, lens), it recovers aperture to within a few hundredths of a stop and is scene-independent, with only rare one-click slips on boundary frames. A stored offset always wins the estimator ladder over every path below.

Validation corpus

A 71-frame corpus (dark frames, lens-cap frames, and a dialled-aperture sweep), apertures snapped to the half-stop grid:

corpus split original meter default directional cross-check
all 71 exact 17 59 60
dark 32 / cup 26 / sweep 13 0 / 12 / 5 31 / 17 / 11 32 / 17 / 11
39 real-aperture frames 17 exact, mean 0.57, RMS 0.92 stop 14 exact, mean 0.45, RMS 0.79, 37/39 within 1 stop best deterministic

The original lands slightly more exact half-stop hits but with the larger mean and RMS error; the ExtBV-anchored default trades a little exact-agreement for lower average error and, crucially, batch-independence. A four-way ensemble was tested and rejected: three of its four legs share the same brightness statistic, so averaging re-injects the scene-luminance lock the meter difference is meant to cancel.

The precision floor

The meter method recovers aperture from the difference of the two meters (through-lens minus external), in which scene luminance cancels - that is what makes it scene-independent. But the cancellation is only as good as the two cells are matched, and they are not, quite. The residual leans most on scenes least like the calibration light.

This is the uncalibrated default estimate (no stored MeterOffset). On it, most scene types track true aperture closely - the difference of the two meters cancels scene luminance well. The exception is dim interiors, which lean wide (the tool reads more open than shot): that scene is the dimmest and most spectrally unlike the reference, so the small residual mismatch between the two cells shows up most there. It is the inherent floor of aperture-from-meters, not a defect, and it only crosses a half-stop grid line because f/2.8 sits right at the rounding boundary.

A stored MeterOffset from --calibrate-fnumber removes this lean, so a calibrated workflow is tighter than the uncalibrated default.

Flat-field / sensor darkfield (-s, -sd)

Per image column, the level correction is:

LevelCorrection[c]  =  max( 0,  column_mean[c]  -  T )
T  =  0.8406 x mean(column_mean)

T is calibrated to the ISO-160 reference. -sd measures and stores these values (one per image column) in sensdb.ini; -s subtracts them. Stored Line columns are neighbour-interpolated rather than corrected by level.

-sd can average a whole folder of dark frames, auto-selecting the clean, fast, low-ISO ones for a less noisy darkfield; slow and high-ISO frames are skipped and logged.

ISO-gain scaling

The M8's real ISO steps (160 / 320 / 640 / 1250 / 2500) are successive doublings, so the analog gain relative to base ISO 160 is quantised to whole stops:

gain  =  2 ^ round( log2( ISO / 160 ) )   ->   1, 2, 4, 8, 16

When a base-ISO darkfield is applied to a higher-ISO frame, m8raw2dng2 scales both the LevelCorrection and (when -b is also given) the written BlackLevel by this gain (e.g. 92 -> 1472 at gain x16). Under plain -b (no -s) m8raw2dng2 leaves BlackLevel flat. The original writes no BlackLevel on a bare conversion, but under -b it writes base_black x gain (ISO-scaled, tag-only) - so the two tools agree under -s -b and diverge under plain -b at high ISO. The darkfield itself matches to within +/-1 DN at base ISO (see Fidelity).

Defect-column detection

detect_defect_columns flags columns deviating strongly from the local trend. With --auto-lines (under -sd) those columns are zeroed in LevelCorrection and recorded as Line entries; -s then repairs each by neighbour interpolation. The plain formula is applied to ~99.95% of columns, matching the reference tool. Defect repair uses a +2px RAW-to-DNG coordinate shift.

Bias subtraction (.BIA)

When a .BIA sidecar is present it is read as another raw frame and subtracted from the CFA, clamped to [0, white], after which BlackLevel is forced to 0. It corrects the per-exposure dark pedestal that appears on long exposures.

Clone this wiki locally