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/Editing/EditRecipe.swift10// Description : The non-destructive edit model: a recipe of parameters11// stored beside the untouched original, an undo/redo12// history, monotone tone curves, and the ORIX presets.13// Module : ORIXImaging14// Repository : https://git.spboucher.ai/orix-camera15// Project Website : https://www.orixlab.dev16// Created : 2026-11-2117// Last Modified : 2026-11-2118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : Proprietary, all rights reserved (see license file)84// License File : LICENSE85// Maintainer : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import CoreGraphics93import Foundation9495public struct EditRecipe: Codable, Sendable, Equatable {96 /// Exposure in stops (−3…3).97 public var exposure: Double = 098 /// −1…1.99 public var contrast: Double = 0100 public var highlights: Double = 0101 public var shadows: Double = 0102 /// −1 (cooler) … 1 (warmer).103 public var temperature: Double = 0104 /// −1 (green) … 1 (magenta).105 public var tint: Double = 0106 public var saturation: Double = 0107 public var vibrance: Double = 0108 /// 0…1.109 public var sharpness: Double = 0110 public var noiseReduction: Double = 0111 public var vignette: Double = 0112 public var monochrome = false113 /// Output levels of the tone curve at inputs 0, ¼, ½, ¾, 1 (encoded signal).114 public var curve: [Double] = EditRecipe.identityCurve115 public var lookID: String?116 public var lookIntensity: Double = 1117 /// Crop in the rotated, straightened image: normalized, origin top-left. Nil = full frame.118 public var crop: CGRect?119 /// Clockwise quarter turns.120 public var quarterTurns: Int = 0121 /// Degrees, −45…45.122 public var straighten: Double = 0123 public var flippedHorizontally = false124 /// RAW development settings (RAW items only). Nil = the default development.125 public var raw: RAWSettings? { didSet { if raw == RAWSettings() { raw = nil } } }126127 public static let identityCurve: [Double] = [0, 0.25, 0.5, 0.75, 1]128 public static let curveInputs: [Double] = [0, 0.25, 0.5, 0.75, 1]129130 public init() {}131132 public var isIdentity: Bool { self == EditRecipe() }133 /// The recipe with tone and colour reset: geometry (and RAW development) only. Used for before/after comparison.134 public var geometryOnly: EditRecipe {135 var out = EditRecipe()136 out.crop = crop; out.quarterTurns = quarterTurns; out.straighten = straighten; out.flippedHorizontally = flippedHorizontally137 return out138 }139140 public var hasGeometry: Bool { crop != nil || quarterTurns % 4 != 0 || straighten != 0 || flippedHorizontally }141142 /// Tone curve that combines the highlights/shadows sliders with the user curve.143 public var combinedCurve: [Double] {144 var points = curve.count == 5 ? curve : EditRecipe.identityCurve145 points[1] += shadows * 0.12146 points[3] += highlights * 0.12147 points[2] += (shadows + highlights) * 0.02148 return points.map { min(max($0, 0), 1) }149 }150 public var hasToneCurve: Bool { combinedCurve != EditRecipe.identityCurve }151}152153/// Monotone cubic interpolation (Fritsch–Carlson): a tone curve through the control points that never overshoots.154public enum ToneCurve {155 public static func evaluate(xs: [Double], ys: [Double], at x: Double) -> Double {156 let n = xs.count157 guard n == ys.count, n >= 2 else { return x }158 if x <= xs[0] { return ys[0] }159 if x >= xs[n - 1] { return ys[n - 1] }160 var delta = [Double](repeating: 0, count: n - 1)161 for i in 0..<(n - 1) { delta[i] = (ys[i + 1] - ys[i]) / (xs[i + 1] - xs[i]) }162 var m = [Double](repeating: 0, count: n)163 m[0] = delta[0]; m[n - 1] = delta[n - 2]164 for i in 1..<(n - 1) { m[i] = delta[i - 1] * delta[i] <= 0 ? 0 : (delta[i - 1] + delta[i]) / 2 }165 for i in 0..<(n - 1) {166 if delta[i] == 0 { m[i] = 0; m[i + 1] = 0; continue }167 let a = m[i] / delta[i], b = m[i + 1] / delta[i]168 let s = a * a + b * b169 if s > 9 { let t = 3 / s.squareRoot(); m[i] = t * a * delta[i]; m[i + 1] = t * b * delta[i] }170 }171 var i = 0172 while i < n - 2 && x > xs[i + 1] { i += 1 }173 let h = xs[i + 1] - xs[i], t = (x - xs[i]) / h174 let t2 = t * t, t3 = t2 * t175 return (2 * t3 - 3 * t2 + 1) * ys[i] + (t3 - 2 * t2 + t) * h * m[i] + (-2 * t3 + 3 * t2) * ys[i + 1] + (t3 - t2) * h * m[i + 1]176 }177178 /// `count` samples of the curve over 0…1.179 public static func table(ys: [Double], count: Int = 256) -> [Float] {180 (0..<count).map { Float(min(max(evaluate(xs: EditRecipe.curveInputs, ys: ys, at: Double($0) / Double(count - 1)), 0), 1)) }181 }182}183184/// Linear undo/redo history of recipes.185public struct EditHistory: Sendable, Equatable {186 public private(set) var states: [EditRecipe]187 public private(set) var index: Int188 public let limit: Int189190 public init(initial: EditRecipe = EditRecipe(), limit: Int = 100) {191 states = [initial]; index = 0; self.limit = limit192 }193194 public var current: EditRecipe { states[index] }195 public var canUndo: Bool { index > 0 }196 public var canRedo: Bool { index < states.count - 1 }197198 /// Records a new state (drops any redo branch). Identical consecutive states are not recorded.199 public mutating func commit(_ recipe: EditRecipe) {200 guard recipe != current else { return }201 states.removeSubrange((index + 1)...)202 states.append(recipe)203 if states.count > limit { states.removeFirst(states.count - limit) }204 index = states.count - 1205 }206207 @discardableResult public mutating func undo() -> EditRecipe { if canUndo { index -= 1 }; return current }208 @discardableResult public mutating func redo() -> EditRecipe { if canRedo { index += 1 }; return current }209}210211public struct EditPreset: Sendable, Identifiable, Equatable {212 public let id: String213 public let title: String214 public let recipe: EditRecipe215216 private static func make(_ id: String, _ title: String, _ configure: (inout EditRecipe) -> Void) -> EditPreset {217 var recipe = EditRecipe(); configure(&recipe)218 return EditPreset(id: id, title: title, recipe: recipe)219 }220221 /// Restrained presets: small tonal moves plus an ORIX look. Geometry is never part of a preset.222 public static let all: [EditPreset] = [223 make("orix.preset.original", "Original") { _ in },224 make("orix.preset.clarity", "Clarity") { $0.contrast = 0.12; $0.vibrance = 0.18; $0.sharpness = 0.25; $0.shadows = 0.15 },225 make("orix.preset.portrait", "Portrait") { $0.lookID = ColorLUT.amber.id; $0.lookIntensity = 0.6; $0.contrast = -0.05; $0.shadows = 0.2; $0.vignette = 0.2 },226 make("orix.preset.landscape", "Landscape") { $0.lookID = ColorLUT.vivid.id; $0.lookIntensity = 0.55; $0.highlights = -0.3; $0.shadows = 0.25; $0.sharpness = 0.3 },227 make("orix.preset.dusk", "Dusk") { $0.lookID = ColorLUT.tide.id; $0.lookIntensity = 0.8; $0.exposure = -0.15; $0.vignette = 0.3 },228 make("orix.preset.matte", "Matte") { $0.lookID = ColorLUT.matte.id; $0.lookIntensity = 0.9 },229 make("orix.preset.graphite", "Graphite") { $0.lookID = ColorLUT.graphite.id; $0.contrast = 0.1; $0.vignette = 0.15 },230 make("orix.preset.redfilter", "Red Filter") { $0.lookID = ColorLUT.redFilter.id; $0.contrast = 0.18 },231 ]232233 /// Applies the preset's tonal values while keeping the geometry of `base`.234 public func applied(to base: EditRecipe) -> EditRecipe {235 var out = recipe236 out.crop = base.crop; out.quarterTurns = base.quarterTurns; out.straighten = base.straighten; out.flippedHorizontally = base.flippedHorizontally237 out.raw = base.raw238 return out239 }240}241