Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Camera9// File : App/Sources/Features/Editor/EditorSession.swift10// Description : One editing session: the live recipe, its undo/redo11// history and the geometry operations. Nothing here touches12// the original file; the result is a recipe saved beside it.13// Module : Features14// 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 Foundation94import Observation95import ORIXImaging9697nonisolated enum CropAspect: String, CaseIterable, Sendable, Identifiable {98 case free, original, square, r4x3, r3x2, r16x9, r5x499 var id: String { rawValue }100 var title: String {101 switch self {102 case .free: String(localized: "Free")103 case .original: String(localized: "Original")104 case .square: "1:1"105 case .r4x3: "4:3"106 case .r3x2: "3:2"107 case .r16x9: "16:9"108 case .r5x4: "5:4"109 }110 }111 /// Width / height for a landscape image; nil = unconstrained.112 func ratio(imageAspect: Double) -> Double? {113 let landscape: Double?114 switch self {115 case .free: landscape = nil116 case .original: return imageAspect117 case .square: landscape = 1118 case .r4x3: landscape = 4.0 / 3119 case .r3x2: landscape = 3.0 / 2120 case .r16x9: landscape = 16.0 / 9121 case .r5x4: landscape = 5.0 / 4122 }123 // On a portrait image the same preset means the tall version.124 return landscape.map { imageAspect < 1 ? 1 / $0 : $0 }125 }126}127128@Observable129final class EditorSession {130 /// The recipe the item had when the editor opened.131 let initial: EditRecipe132 /// The live recipe (changes continuously while a slider moves).133 var recipe: EditRecipe134 private(set) var history: EditHistory135 var cropAspect: CropAspect = .free136 /// Width / height of the unedited source, upright.137 var sourceAspect: Double = 1.5138139 init(recipe: EditRecipe?) {140 initial = recipe ?? EditRecipe()141 self.recipe = initial142 history = EditHistory(initial: initial)143 }144145 var canUndo: Bool { history.canUndo }146 var canRedo: Bool { history.canRedo }147 var hasChanges: Bool { recipe != initial }148 var isPristine: Bool { recipe.isIdentity }149150 /// Records the live recipe as an undo step (called when a gesture ends).151 func commit() { history.commit(recipe) }152153 func undo() { recipe = history.undo() }154 func redo() { recipe = history.redo() }155156 /// Back to the untouched original: every adjustment, the geometry and the RAW development.157 func reset() {158 recipe = EditRecipe()159 cropAspect = .free160 commit()161 }162163 func apply(_ preset: EditPreset) {164 recipe = preset.applied(to: recipe)165 commit()166 }167168 // MARK: Geometry169170 /// Aspect of the image the crop rectangle lives on (after quarter turns; straightening keeps the aspect).171 var imageAspect: Double {172 ((recipe.quarterTurns % 4) + 4) % 2 == 1 ? 1 / sourceAspect : sourceAspect173 }174175 var cropRect: CGRect { recipe.crop ?? CropMath.unit }176177 func setCrop(_ rect: CGRect) { recipe.crop = CropMath.recipeValue(rect) }178179 func setAspect(_ aspect: CropAspect) {180 cropAspect = aspect181 if let ratio = aspect.ratio(imageAspect: imageAspect) {182 recipe.crop = CropMath.recipeValue(CropMath.rect(ratio: ratio, imageAspect: imageAspect))183 }184 commit()185 }186187 /// Rotates 90° clockwise, carrying the crop rectangle with the picture.188 func rotateClockwise() {189 if let c = recipe.crop { recipe.crop = CGRect(x: 1 - c.maxY, y: c.minX, width: c.height, height: c.width) }190 recipe.quarterTurns = (recipe.quarterTurns + 1) % 4191 commit()192 }193194 /// Mirrors the displayed picture left to right, carrying the crop rectangle with it.195 func flipHorizontally() {196 if let c = recipe.crop { recipe.crop = CGRect(x: 1 - c.maxX, y: c.minY, width: c.width, height: c.height) }197 // The pipeline flips before it rotates: after an odd number of quarter turns a left-right mirror of the198 // displayed picture is a flip plus a half turn.199 if ((recipe.quarterTurns % 4) + 4) % 2 == 1 { recipe.quarterTurns = (recipe.quarterTurns + 2) % 4 }200 recipe.flippedHorizontally.toggle()201 commit()202 }203204 func resetGeometry() {205 recipe.crop = nil; recipe.quarterTurns = 0; recipe.straighten = 0; recipe.flippedHorizontally = false206 cropAspect = .free207 commit()208 }209210 // MARK: RAW211212 func updateRAW(_ change: (inout RAWSettings) -> Void) {213 var raw = recipe.raw ?? RAWSettings()214 change(&raw)215 recipe.raw = raw216 }217218 /// The recipe to render on the crop tab: everything but the crop, so the whole frame stays visible.219 var recipeForCropping: EditRecipe {220 var r = recipe221 r.crop = nil; r.vignette = 0222 return r223 }224}225