Photivra
Photivra is an open TypeScript engine for the science and mathematics of photography and digital imaging.
Pronunciation (IPA): /foʊˈtɪvɹə/
Photivra gives you a reusable, tested API for:
- camera geometry and optics;
- focus and depth of field;
- exposure;
- subject and camera motion;
- sensor sampling;
- diffraction;
- cropping and framing;
- stabilization approximations;
- signal and noise.
Every calculation uses explicit units, documented assumptions, validation and provenance (a record of how the result was produced). Approximations are clearly labelled. Results are reproducible, and their limits are written down.
The source is licensed under Apache-2.0.
Related foundations
- Digital optics foundations document the standalone stray-light irradiance, composed digital geometry and generic lens-correction APIs. They keep the optical/sensor capture model in charge, and make sampling and noise costs explicit. Production composition is separate.
- The standalone SimulatedCapture contract stores format-neutral linear floating-point master data plus public metadata.
- The standalone SDR rendering foundation keeps three steps apart: rendering after capture, output encoding, and adaptation to an external display. (SDR = standard dynamic range.) The processed output contract connects capture, WB and correction, with explicit input domains and bounded support.
- The standalone Print planner works out printer-file geometry from native pixels only. It uses the physical image size, an independent viewing distance and explicit sampling conventions. It does not assess captured detail or print quality.
Quick start
Install the published ESM package below. The 2.29.0 release record and changelog describe the current release:
npm install @photivra/engine@2.29.0
Simulate a photograph of a test chart with a generic camera, then read the developed sRGB pixels:
import { simulateCapture } from "@photivra/engine";
const capture = await simulateCapture({
camera: "professional",
lens: { fNumber: 8 },
exposure: { mode: "manual", iso: 100, shutterSeconds: 1 / 125 },
scene: { preset: "slanted-edge" },
output: { width: 16, height: 16 }
});
console.log(capture.image.samples.length, capture.exposure.settings);
console.log(capture.assumptions.map((a) => `${a.id}: ${a.explanation}`));
simulateCapture() runs the full physical pipeline through the engine's existing contracts: scene radiance, lens, spectral sensor, noise, RAW readout, RAW development and optional JPEG. It lists every default and modelling choice it made in assumptions.
The Quick Start covers the inputs, the chart presets (slanted edge, Siemens star, step wedge, grey ramp, checkerboard, point grid, caller patches and, from 2.14, Photivra's own color patches), illuminants, white balance and limits.
The individual calculations are still available as small functions. For example, the horizontal field of view for a 36 mm sensor dimension and a 50 mm focal length:
import { calculateFieldOfView } from "@photivra/engine";
const horizontal = calculateFieldOfView({
focalLengthMm: 50,
sensorDimensionMm: 36
});
console.log(horizontal.value.degrees);
console.log(horizontal.provenance);
Start with the Developer Guide. Then use the Usage Guide and the API reference, which is generated from the package exports.
Repository main may describe newer, unreleased work. For the stable contract, use the docs bundled in the published package, or the exact v2.29.0 tag.
Documentation
Getting help
Use GitHub Issues for reproducible bugs, documentation problems and feature discussions. For security vulnerabilities, follow SECURITY.md.
What is implemented
The root package exports models in four areas. Each model is either deterministic or explicitly labelled as an approximation.
A few abbreviations used below: FOV (field of view), PSF (point-spread function: how a lens spreads one point of light), MTF (modulation transfer function), CA (chromatic aberration), AA (anti-aliasing filter), CFA (colour filter array, such as a Bayer mosaic), QE / EQE (quantum efficiency / external quantum efficiency), A/W (amperes per watt, a detector's responsivity), ADC (analog-to-digital converter), AE / AF / WB (auto-exposure / autofocus / white balance).
Optics and geometry
- Generic focus control: MF, single AF and continuous AF. Explicit acquisition, loss and lock state; a renderer-neutral target distance along the lens axis; strict rules for losing and reacquiring a target; and a separate gate for focus-priority versus release-priority.
- Explicit finite and infinity focus-plane state, with no non-finite or made-up focus distances.
- Field of view for centered and asymmetric rectilinear framing, with optional focus-aware thin-lens projection.
- Physical versus diagonal-based 35 mm-equivalent focal length.
- Focus-breathing approximation of projection and FOV, using a scale the caller declares.
- Generic invertible radial distortion mapping, including batch inverse sampling of many points.
- Generic lateral CA field mapping per RGB channel.
- Generic illumination-vignetting approximation in linear light.
- Front-of-lens filter transmission for generic passive filters: neutral (linear), neutral optical density, and wavelength-resolved. No polarization claims.
- Scene radiance to sensor irradiance: an optical bridge from the light leaving the scene to the light arriving at the sensor. It combines explicit pupil acceptance, lens transmission or a working-T-stop approximation, optional front-filter transmission, and exactly one field-throughput term. It stops before the sensor's own optical stack.
- Gaussian thin-lens image distance and magnification.
- Geometric depth of field and defocus-circle diameter.
- First-zero Airy diameter for an ideal circular aperture.
- Real-lens sampled PSF and complex-pupil framework:
- full 2D directional kernels;
- bounded interpolation over field, focus, aperture, wavelength and defocus;
- explicit field-curvature and longitudinal-focus offsets;
- PSF shape normalized to unit energy, with pupil throughput kept separate;
- MTF-only data fails closed: it lacks phase and spatial information, so it returns blockers instead of a guessed PSF.
- Separated PSF/pupil contribution foundation.
- Ideal regular-polygon aperture geometry and sunstar direction symmetry.
Exposure and motion