Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Camera9// File : Packages/ORIXCameraKit/Sources/ORIXImaging/Support/SyntheticScene.swift10// Description : A procedural test scene (sky with a clipped sun, dark11// ridge, colour patches, grey ramp, Siemens star, moving12// lights). It feeds the pipeline where no camera exists:13// unit tests and the iOS Simulator. It is a test double,14// never a substitute for a camera in a release build.15// Module : ORIXImaging16// Repository : https://git.spboucher.ai/orix-camera17// Project Website : https://www.orixlab.dev18// Created : 2026-11-2119// Last Modified : 2026-11-2120//21// =============================================================================22// AUTHOR & PROFESSIONAL AFFILIATION23// =============================================================================24//25// Author : Simon-Pierre Boucher26// Position : Professor of Finance27// Institution : Université du Québec en Outaouais (UQO)28// Department : Département des sciences administratives29// Organization : SP Boucher30//31// Website : https://spboucher.ai32// Git : https://git.spboucher.ai33//34// =============================================================================35// OFFICIAL CONTACT DIRECTORY36// =============================================================================37//38// General : info@spboucher.ai39// Professional : contact@spboucher.ai40// Direct : simon@spboucher.ai41// Research : research@spboucher.ai42// Development : dev@spboucher.ai43// Support : support@spboucher.ai44// Security/Admin : admin@spboucher.ai45// Notifications : notifications@spboucher.ai46//47// Preferred Contact: support@spboucher.ai48//49// =============================================================================50// COPYRIGHT & INTELLECTUAL PROPERTY51// =============================================================================52//53// Copyright (c) 202654// Simon-Pierre Boucher55//56// All rights reserved where applicable.57//58// This file is governed by the applicable project license,59// copyright notices, institutional agreements and relevant law.60//61// Unless expressly authorized by the applicable license or62// rights holder, copying, redistribution, sublicensing,63// publication and commercial exploitation are not permitted.64//65// Existing third-party rights and notices remain unaffected.66//67// =============================================================================68// CONFIDENTIALITY & SECURITY69// =============================================================================70//71// Classification : Public72// Distribution : Public repository; use is governed by the project license73// Security Contact : admin@spboucher.ai74//75// Access to this source does not independently grant permission76// to redistribute or disclose confidential materials.77//78// Do not embed credentials, private keys, access tokens,79// certificates or confidential personal information.80//81// =============================================================================82// LICENSE & MAINTENANCE83// =============================================================================84//85// License : Proprietary, all rights reserved (see license file)86// License File : LICENSE87// Maintainer : Simon-Pierre Boucher88// Technical Contact: dev@spboucher.ai89//90// =============================================================================91// END OF SP BOUCHER SOURCE HEADER92// =============================================================================9394import Accelerate95import CoreGraphics96import CoreVideo97import Foundation9899public final class SyntheticScene: @unchecked Sendable {100 public struct Controls: Sendable, Equatable {101 /// Linear-ish gain on the encoded signal (1 = nominal exposure).102 public var exposureGain: Double = 1103 public var redGain: Double = 1104 public var greenGain: Double = 1105 public var blueGain: Double = 1106 /// 0 = sharp, 1 = strongly defocused.107 public var defocus: Double = 0108 /// Digital zoom (centre crop).109 public var zoom: Double = 1110 public init() {}111 }112113 public let width: Int114 public let height: Int115 private let background: CGImage116 private let lock = NSLock()117118 public init?(width: Int = 1280, height: Int = 720) {119 self.width = width; self.height = height120 guard let image = SyntheticScene.drawBackground(width: width, height: height) else { return nil }121 background = image122 }123124 private static func context(width: Int, height: Int, data: UnsafeMutableRawPointer? = nil, rowBytes: Int = 0) -> CGContext? {125 let info = CGImageAlphaInfo.noneSkipFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue126 return CGContext(data: data, width: width, height: height, bitsPerComponent: 8, bytesPerRow: data == nil ? 0 : rowBytes,127 space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: info)128 }129130 private static func rgb(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1) -> CGColor {131 CGColor(srgbRed: r, green: g, blue: b, alpha: a)132 }133134 /// Static part of the scene. Coordinates: CoreGraphics (origin bottom-left).135 private static func drawBackground(width: Int, height: Int) -> CGImage? {136 guard let g = context(width: width, height: height) else { return nil }137 let w = CGFloat(width), h = CGFloat(height)138 let space = CGColorSpace(name: CGColorSpace.sRGB)!139 // Sky: dusk gradient.140 let sky = CGGradient(colorsSpace: space, colors: [rgb(0.04, 0.05, 0.16), rgb(0.22, 0.16, 0.42), rgb(0.86, 0.45, 0.33), rgb(0.99, 0.80, 0.52)] as CFArray,141 locations: [0, 0.35, 0.75, 1])!142 g.drawLinearGradient(sky, start: CGPoint(x: 0, y: h), end: CGPoint(x: 0, y: h * 0.36), options: [.drawsAfterEndLocation])143 // Sun: a clipped core with a glow (drives zebras and the clipping warning).144 let sun = CGPoint(x: w * 0.70, y: h * 0.50)145 let glow = CGGradient(colorsSpace: space, colors: [rgb(1, 1, 1), rgb(1, 1, 1), rgb(1, 0.92, 0.7, 0.55), rgb(1, 0.8, 0.5, 0)] as CFArray, locations: [0, 0.22, 0.45, 1])!146 g.drawRadialGradient(glow, startCenter: sun, startRadius: 0, endCenter: sun, endRadius: min(w, h) * 0.30, options: [])147 // Far ridge and near ridge: deep shadows with a little texture.148 func ridge(base: CGFloat, amplitude: CGFloat, seed: CGFloat, color: CGColor) {149 g.beginPath()150 g.move(to: CGPoint(x: 0, y: 0))151 var x: CGFloat = 0152 while x <= w {153 let t = x / w154 let y = base + amplitude * (sin(t * 6.3 + seed) * 0.5 + sin(t * 17.1 + seed * 2.1) * 0.22 + sin(t * 41 + seed * 3.3) * 0.08)155 g.addLine(to: CGPoint(x: x, y: h * y))156 x += 4157 }158 g.addLine(to: CGPoint(x: w, y: 0))159 g.closePath()160 g.setFillColor(color)161 g.fillPath()162 }163 ridge(base: 0.40, amplitude: 0.10, seed: 0.7, color: rgb(0.16, 0.12, 0.26))164 ridge(base: 0.27, amplitude: 0.07, seed: 2.9, color: rgb(0.035, 0.035, 0.07))165 // Foreground slab that holds the charts.166 g.setFillColor(rgb(0.012, 0.012, 0.02))167 g.fill(CGRect(x: 0, y: 0, width: w, height: h * 0.20))168 // Colour patches (a small checker: primaries, secondaries, skin-like, neutrals).169 let patches: [(CGFloat, CGFloat, CGFloat)] = [170 (0.45, 0.32, 0.26), (0.76, 0.58, 0.50), (0.37, 0.48, 0.61), (0.35, 0.42, 0.26), (0.51, 0.50, 0.69), (0.40, 0.74, 0.67),171 (0.84, 0.48, 0.17), (0.28, 0.36, 0.65), (0.76, 0.33, 0.38), (0.36, 0.24, 0.42), (0.62, 0.74, 0.25), (0.88, 0.64, 0.18),172 (0.75, 0.10, 0.12), (0.10, 0.55, 0.20), (0.10, 0.18, 0.70), (0.93, 0.93, 0.93), (0.47, 0.47, 0.47), (0.12, 0.12, 0.12),173 ]174 let chart = CGRect(x: w * 0.045, y: h * 0.045, width: w * 0.30, height: h * 0.20)175 g.setFillColor(rgb(0.02, 0.02, 0.02)); g.fill(chart.insetBy(dx: -6, dy: -6))176 let cols = 6, rows = 3177 let cw = chart.width / CGFloat(cols), ch = chart.height / CGFloat(rows)178 for (i, p) in patches.enumerated() {179 let rect = CGRect(x: chart.minX + CGFloat(i % cols) * cw, y: chart.maxY - CGFloat(i / cols + 1) * ch, width: cw, height: ch).insetBy(dx: 3, dy: 3)180 g.setFillColor(rgb(p.0, p.1, p.2)); g.fill(rect)181 }182 // Grey ramp: 0 → 100 % (histogram and zebra reference).183 let ramp = CGRect(x: w * 0.38, y: h * 0.045, width: w * 0.32, height: h * 0.06)184 let grey = CGGradient(colorsSpace: space, colors: [rgb(0, 0, 0), rgb(1, 1, 1)] as CFArray, locations: [0, 1])!185 g.saveGState(); g.clip(to: ramp)186 g.drawLinearGradient(grey, start: CGPoint(x: ramp.minX, y: 0), end: CGPoint(x: ramp.maxX, y: 0), options: [])187 g.restoreGState()188 // Step wedge: eleven patches, 0 … 100 % in 10 % steps.189 let wedge = CGRect(x: w * 0.38, y: h * 0.125, width: w * 0.32, height: h * 0.06)190 for i in 0...10 {191 let v = CGFloat(i) / 10192 g.setFillColor(rgb(v, v, v))193 g.fill(CGRect(x: wedge.minX + CGFloat(i) * wedge.width / 11, y: wedge.minY, width: wedge.width / 11 + 0.5, height: wedge.height))194 }195 // Siemens star: fine radial detail (focus peaking reference).196 let radius = min(w * 0.12, h * 0.12), star = CGPoint(x: w * 0.86, y: h * 0.045 + radius)197 g.setFillColor(rgb(0.92, 0.92, 0.92)); g.fillEllipse(in: CGRect(x: star.x - radius, y: star.y - radius, width: radius * 2, height: radius * 2))198 g.setFillColor(rgb(0.04, 0.04, 0.04))199 let spokes = 36200 for i in 0..<spokes where i % 2 == 0 {201 let a0 = CGFloat(i) / CGFloat(spokes) * 2 * .pi, a1 = CGFloat(i + 1) / CGFloat(spokes) * 2 * .pi202 g.beginPath(); g.move(to: star)203 g.addArc(center: star, radius: radius, startAngle: a0, endAngle: a1, clockwise: false)204 g.closePath(); g.fillPath()205 }206 // Stars in the sky: point detail.207 var seed: UInt64 = 0x0D1CE5EED208 func random() -> CGFloat {209 seed = seed &* 6364136223846793005 &+ 1442695040888963407210 return CGFloat((seed >> 33) & 0xFFFF) / 65535211 }212 for _ in 0..<70 {213 let x = random() * w, y = h * (0.62 + random() * 0.38), r = 0.6 + random() * 1.4, a = 0.35 + random() * 0.6214 g.setFillColor(rgb(1, 1, 1, a)); g.fillEllipse(in: CGRect(x: x, y: y, width: r, height: r))215 }216 return g.makeImage()217 }218219 /// Renders the scene at `time` (seconds) into a new BGRA buffer.220 public func render(time: Double, controls: Controls = Controls()) -> CVPixelBuffer? {221 guard let buffer = PixelBuffers.makeBGRA(width: width, height: height) else { return nil }222 lock.lock(); defer { lock.unlock() }223 let w = CGFloat(width), h = CGFloat(height)224 PixelBuffers.withLocked(buffer, readOnly: false) { base, rowBytes, _, _ in225 guard let g = SyntheticScene.context(width: width, height: height, data: base, rowBytes: rowBytes) else { return }226 if controls.zoom > 1.001 {227 let z = CGFloat(controls.zoom)228 g.translateBy(x: w / 2, y: h / 2); g.scaleBy(x: z, y: z); g.translateBy(x: -w / 2, y: -h / 2)229 }230 g.draw(background, in: CGRect(x: 0, y: 0, width: w, height: h))231 // Moving lights: two "vehicles" crossing the valley and one orbiting lantern (light-trail subjects).232 let t = CGFloat(time)233 func light(_ p: CGPoint, _ color: CGColor, _ r: CGFloat) {234 g.setFillColor(color.copy(alpha: 0.25) ?? color); g.fillEllipse(in: CGRect(x: p.x - r * 2.2, y: p.y - r * 2.2, width: r * 4.4, height: r * 4.4))235 g.setFillColor(color); g.fillEllipse(in: CGRect(x: p.x - r, y: p.y - r, width: r * 2, height: r * 2))236 }237 let r = max(2.5, min(w, h) * 0.006)238 let x1 = (t * 0.13).truncatingRemainder(dividingBy: 1.2) - 0.1239 light(CGPoint(x: w * x1, y: h * (0.245 + 0.02 * sin(x1 * 9))), SyntheticScene.rgb(1, 0.95, 0.85), r)240 let x2 = 1.1 - (t * 0.09 + 0.4).truncatingRemainder(dividingBy: 1.2)241 light(CGPoint(x: w * x2, y: h * (0.225 + 0.015 * sin(x2 * 7 + 1))), SyntheticScene.rgb(1, 0.15, 0.1), r)242 let angle = t * 1.7243 light(CGPoint(x: w * 0.25 + cos(angle) * min(w, h) * 0.11, y: h * 0.62 + sin(angle) * min(w, h) * 0.11), SyntheticScene.rgb(0.35, 0.9, 1), r * 0.9)244245 var image = vImage_Buffer(data: base, height: vImagePixelCount(height), width: vImagePixelCount(width), rowBytes: rowBytes)246 if controls.defocus > 0.01 {247 var kernel = UInt32(controls.defocus * Double(height) * 0.035) | 1248 kernel = max(3, kernel)249 if let scratch = malloc(rowBytes * height) {250 var temp = vImage_Buffer(data: scratch, height: vImagePixelCount(height), width: vImagePixelCount(width), rowBytes: rowBytes)251 vImageTentConvolve_ARGB8888(&image, &temp, nil, 0, 0, kernel, kernel, nil, vImage_Flags(kvImageEdgeExtend))252 memcpy(base, scratch, rowBytes * height)253 free(scratch)254 }255 }256 let gains = [controls.blueGain * controls.exposureGain, controls.greenGain * controls.exposureGain, controls.redGain * controls.exposureGain]257 if gains.contains(where: { abs($0 - 1) > 0.002 }) {258 // Memory order B, G, R, A. Divisor 256; results saturate, which is what an over-exposed sensor does.259 let q = gains.map { Int16(min(max($0, 0), 60) * 256) }260 let matrix: [Int16] = [q[0], 0, 0, 0, 0, q[1], 0, 0, 0, 0, q[2], 0, 0, 0, 0, 256]261 vImageMatrixMultiply_ARGB8888(&image, &image, matrix, 256, nil, nil, vImage_Flags(kvImageNoFlags))262 }263 }264 return buffer265 }266}267