{
 "id": "film-emulation",
 "title": "Film emulation (spectral stock simulation)",
 "aliases": [],
 "domain": "post",
 "subdomain": "film look",
 "stage": "post",
 "realtime": "offline",
 "platforms": [
  "python",
  "cpu"
 ],
 "year_introduced": 2025,
 "inputs": [
  "hdr-image",
  "ldr-image"
 ],
 "outputs": [
  "ldr-image"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Simulates analog colour film end to end: exposure of a spectral stock model, dye density, optional enlarger and print stage, producing the cross-channel colour behaviour of a real emulsion rather than a LUT.",
 "how_it_works": "Instead of a colour matrix, film emulation models the physical chain. Scene RGB is expanded to a spectrum, which exposes three light-sensitive layers with their own spectral sensitivities. Each layer's exposure goes through a characteristic density curve, and the resulting dye densities modulate transmitted light with spectral absorption overlap between dyes, which is what gives film its characteristic colour crosstalk. A print or scan stage then converts densities to display colour. Grain, halation and other effects can be layered on. spektrafilm implements this in Python from manufacturer spectral datasheet data; full-resolution export is slow, so treat it as an offline stills tool.",
 "when_to_use": "Choose it when you want the colour behaviour and highlights of a specific stock, beyond what a 3D LUT captures, for stills or pre-rendered frames.\n\nPrefer `lut-color-grading` for real-time. Add `film-grain-synthesis` for texture.",
 "cost": {
  "class": "offline",
  "notes": "Python per-image processing; not a real-time pipeline."
 },
 "failure_modes": [
  "Depends on the quality of the measured stock data",
  "Needs scene-referred linear input; display-referred sources give odd results",
  "Slow for video",
  "Results vary between releases of the tool while it is young"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:andreavolpato/spektrafilm",
   "type": "code",
   "title": "andreavolpato/spektrafilm",
   "description": "Spectral simulation of analog photography processes",
   "authors": [
    "andreavolpato"
   ],
   "year": 2025,
   "venue": "GitHub",
   "url": "https://github.com/andreavolpato/spektrafilm",
   "ids": {
    "repo": "andreavolpato/spektrafilm"
   },
   "license": "GPL-3.0",
   "extra": {
    "stars": 2478,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-07-14T12:41:20Z",
    "language": "Python",
    "homepage": null,
    "license_name": "GNU General Public License v3.0"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/andreavolpato/spektrafilm",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/andreavolpato/spektrafilm",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/andreavolpato/spektrafilm",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/andreavolpato/spektrafilm",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/andreavolpato/spektrafilm",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/andreavolpato/spektrafilm",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "andreavolpato2025andreavolpato"
  }
 ],
 "presets": [
  {
   "id": "film-emulation-live",
   "title": "Left: raw digital sRGB clip. Right: spectral film emulsion with H&D curve, red halation and grain",
   "runtime": "glsl-fragment",
   "params": {
    "EXPOSURE": 1.15,
    "HALATION": 0.68,
    "GRAIN": 0.32,
    "CONTRAST": 1.25
   },
   "basis": "original",
   "license_note": "MIT — written for this index",
   "code": "const float EXPOSURE = 1.15; // P\nconst float HALATION = 0.68; // P\nconst float GRAIN = 0.32; // P\nconst float CONTRAST = 1.25; // P\n\nfloat hash(vec2 p) {\n  vec3 q = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));\n  q += dot(q, q.yzx + 33.33);\n  return fract((q.x + q.y) * q.z);\n}\n\n// Scene-referred HDR dusk streetscape (bright tungsten lamps, cyan neon, wet street reflection)\nvec3 hdrScene(vec2 uv) {\n  float pulse = 0.88 + 0.12 * sin(iTime * 1.4);\n  // Twilight gradient sky\n  vec3 col = mix(vec3(0.28, 0.14, 0.24), vec3(0.03, 0.07, 0.22), clamp((uv.y + 0.12) * 1.6, 0.0, 1.0));\n\n  // Architectural skyline silhouettes\n  float bld1 = step(abs(uv.x + 0.34), 0.15) * step(uv.y, 0.26) * step(-0.12, uv.y);\n  float bld2 = step(abs(uv.x - 0.30), 0.18) * step(uv.y, 0.34) * step(-0.12, uv.y);\n  col = mix(col, vec3(0.025, 0.03, 0.05), max(bld1, bld2));\n\n  // Glowing HDR cyan neon bar on right building\n  vec2 nBar = abs(uv - vec2(0.24, 0.16)) - vec2(0.095, 0.014);\n  float dNeon = length(max(nBar, 0.0));\n  col += vec3(0.18, 2.2, 3.6) * exp(-dNeon * 38.0) * pulse;\n  col += vec3(0.55, 4.5, 6.2) * smoothstep(0.012, 0.002, dNeon);\n\n  // Overexposed tungsten streetlamps (prime halation triggers)\n  vec2 l1 = vec2(-0.18, 0.18 + 0.02 * sin(iTime * 0.7));\n  vec2 l2 = vec2( 0.08, 0.05);\n  vec2 l3 = vec2(-0.36, 0.08);\n  float d1 = length(uv - l1), d2 = length(uv - l2), d3 = length(uv - l3);\n  col += vec3(7.5, 4.8, 2.1) * smoothstep(0.042, 0.016, d1) + vec3(2.8, 1.5, 0.55) * exp(-d1 * 18.0);\n  col += vec3(6.8, 4.2, 1.9) * smoothstep(0.028, 0.010, d2) + vec3(2.2, 1.1, 0.42) * exp(-d2 * 24.0);\n  col += vec3(5.2, 3.1, 1.4) * smoothstep(0.022, 0.008, d3) + vec3(1.6, 0.85, 0.32) * exp(-d3 * 28.0);\n\n  // Wet asphalt road below y = -0.12 with stretched vertical specular streaks\n  if (uv.y < -0.12) {\n    float dist = -0.12 - uv.y;\n    vec2 rUv = vec2(uv.x + 0.006 * sin(uv.y * 95.0 + iTime * 2.0), -0.12 + dist * 0.92);\n    float streak1 = exp(-abs(rUv.x - l1.x) * 14.0) * exp(-dist * 2.2);\n    float streak2 = exp(-abs(rUv.x - l2.x) * 18.0) * exp(-dist * 2.8);\n    float streakN = exp(-abs(rUv.x - 0.24) * 9.5) * exp(-dist * 2.4);\n    col = vec3(0.018, 0.022, 0.032);\n    col += vec3(3.2, 1.85, 0.75) * streak1 * 0.85;\n    col += vec3(2.6, 1.45, 0.60) * streak2 * 0.72;\n    col += vec3(0.22, 1.85, 2.95) * streakN * 0.65 * pulse;\n  }\n  return col * max(EXPOSURE, 0.05);\n}\n\n// Emulsion Hurter-Driffield S-curve + cross-channel dye-coupler print stage\nvec3 filmEmulsion(vec3 lin, vec2 uv, vec2 fragCoord) {\n  // 1. Red-orange anti-halation backing scatter from nearby overexposed HDR highlights\n  vec3 hal = vec3(0.0);\n  float halStr = max(HALATION, 0.0);\n  for (int i = 0; i < 12; i++) {\n    float a = float(i) * 0.523599;\n    vec2 dir = vec2(cos(a), sin(a));\n    vec3 s1 = hdrScene(uv + dir * 0.024);\n    vec3 s2 = hdrScene(uv + dir * 0.052);\n    hal += max(s1 - 1.15, 0.0) * 0.65 + max(s2 - 1.35, 0.0) * 0.35;\n  }\n  hal /= 12.0;\n  lin += vec3(hal.r * 0.82 + hal.g * 0.28, hal.r * 0.18 + hal.g * 0.08, hal.b * 0.02) * halStr;\n\n  // 2. Spectral-inspired cross-channel sensitivity matrix (warm tungsten skin/gold, cyan shadow tint)\n  mat3 sens = mat3(\n    0.92, 0.09, 0.02,\n    0.04, 0.90, 0.06,\n    0.01, 0.08, 0.88\n  );\n  vec3 expRGB = max(sens * lin, vec3(1e-5));\n\n  // 3. Per-layer H&D density curve (toe + gamma + shoulder rolloff)\n  vec3 logE = log2(expRGB) * max(CONTRAST, 0.25);\n  vec3 density = 1.0 / (1.0 + exp(-(logE + vec3(0.42, 0.35, 0.22))));\n\n  // 4. Print-stock dye crosstalk (subtle cool-teal shadows, creamy warm highlights)\n  vec3 printCol = vec3(\n    pow(density.r, 0.94) * 1.04,\n    pow(density.g, 1.00) * 1.00,\n    pow(density.b, 1.06) * 0.96\n  );\n  printCol += vec3(0.012, 0.024, 0.032) * (1.0 - printCol);\n\n  // 5. Luminance-dependent silver-halide grain\n  float g = (hash(floor(fragCoord * 0.85) + floor(iTime * 24.0) * 17.3) - 0.5) * 2.0;\n  float lum = dot(printCol, vec3(0.2126, 0.7152, 0.0722));\n  float grainEnv = smoothstep(0.0, 0.35, lum) * smoothstep(1.0, 0.45, lum);\n  printCol += g * max(GRAIN, 0.0) * 0.14 * (0.35 + 0.65 * grainEnv);\n  return clamp(printCol, 0.0, 1.0);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float splitX = 0.0;\n  if (iMouse.z > 0.5 || length(iMouse.xy) > 1.0) {\n    splitX = (iMouse.x - 0.5 * iResolution.xy.x) / iResolution.y;\n  }\n\n  vec3 hdr = hdrScene(uv);\n  vec3 col;\n  if (uv.x < splitX) {\n    // Left: raw digital hard-clipped sRGB\n    col = pow(clamp(hdr, 0.0, 1.0), vec3(1.0 / 2.2));\n  } else {\n    // Right: spectral photochemical film stock emulation\n    col = filmEmulsion(hdr, uv, fragCoord);\n  }\n  if (abs(uv.x - splitX) < 0.0025) col = vec3(1.0, 0.88, 0.62);\n  fragColor = vec4(col, 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "film-emulation",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:15:34.232Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.3723,
      "std": 0.2679,
      "unique_colors": 1142
     },
     "image": "/specimens/film-emulation-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "spectral-upsampling-sigmoid",
  "lut-color-grading",
  "film-grain-synthesis"
 ],
 "extends": [
  "lut-color-grading",
  "spectral-upsampling-sigmoid"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "film-grain-synthesis",
   "why": "Grain layers on top of the emulated density image."
  },
  {
   "id": "spectral-upsampling-sigmoid",
   "why": "Provides spectra from RGB input."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/post/film-emulation.json"
 },
 "solves": [
  "lut grading doesn't capture real film colour behaviour",
  "want the look of a specific analog stock",
  "highlights should respond like real emulsion dyes",
  "digital render looks too clinical compared to film"
 ],
 "authority": "andreavolpato, 2025",
 "url": "https://texelatlas.vercel.app/t/film-emulation/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 34.4,
  "tier": "heavy",
  "p50_ms": 1.1316,
  "p95_ms": 1.8699,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "film-emulation-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 31.85,
   "tier": "heavy",
   "p50_ms": 0.5064,
   "p95_ms": 0.522,
   "speedup_x": 2.23,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 24.32,
   "tier": "heavy",
   "p50_ms": 0.7003,
   "p95_ms": 0.7036,
   "speedup_x": 1.62,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}