Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Camera9// File : Packages/ORIXCameraKit/Tests/ORIXImagingTests/EditingTests.swift10// Description : Edit recipe, tone curves, history and the Core Image11// pipeline (tone and geometry).12// Module : ORIXImagingTests13// Repository : https://git.spboucher.ai/orix-camera14// Project Website : https://www.orixlab.dev15// Created : 2026-11-2116// Last Modified : 2026-11-2117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : Proprietary, all rights reserved (see license file)83// License File : LICENSE84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import CoreGraphics92import CoreImage93import Foundation94import Testing95@testable import ORIXImaging9697@Suite struct EditPipelineTests {98 let source = CIImage(cgImage: Fixture.image(Fixture.textured(width: 240, height: 160)))99100 func render(_ configure: (inout EditRecipe) -> Void) throws -> CGImage {101 var recipe = EditRecipe(); configure(&recipe)102 return try #require(EditPipeline.render(recipe: recipe, source: source))103 }104105 @Test func identityRecipeChangesNothing() throws {106 let out = try render { _ in }107 let a = Fixture.means(out), b = Fixture.means(Fixture.image(Fixture.textured(width: 240, height: 160)))108 #expect(out.width == 240 && out.height == 160)109 #expect(abs(a.r - b.r) < 0.6 && abs(a.g - b.g) < 0.6 && abs(a.b - b.b) < 0.6)110 #expect(EditRecipe().isIdentity)111 }112113 @Test func exposureBrightensByOneStop() throws {114 let base = Fixture.means(try render { _ in }), up = Fixture.means(try render { $0.exposure = 1 }), down = Fixture.means(try render { $0.exposure = -1 })115 #expect(up.g > base.g + 20 && down.g < base.g - 20)116 // One stop doubles linear light: check on a mid-grey patch through the sRGB curve (0.5 encoded = 0.214 linear → 0.428 linear = 0.686 encoded).117 let grey = CIImage(color: CIColor(red: 0.5, green: 0.5, blue: 0.5)).cropped(to: CGRect(x: 0, y: 0, width: 8, height: 8))118 var recipe = EditRecipe(); recipe.exposure = 1119 let out = Fixture.means(try #require(EditPipeline.render(recipe: recipe, source: grey)))120 #expect(abs(out.g - 0.686 * 255) < 3)121 }122123 @Test func temperatureAndTintMoveColourTheRightWay() throws {124 let base = Fixture.means(try render { _ in })125 let warm = Fixture.means(try render { $0.temperature = 0.8 }), cool = Fixture.means(try render { $0.temperature = -0.8 })126 #expect(warm.r - warm.b > base.r - base.b + 8)127 #expect(cool.r - cool.b < base.r - base.b - 8)128 let magenta = Fixture.means(try render { $0.tint = 0.8 })129 #expect(magenta.g - (magenta.r + magenta.b) / 2 < base.g - (base.r + base.b) / 2 - 3)130 }131132 @Test func saturationContrastAndMonochrome() throws {133 func spread(_ image: CGImage) -> Double {134 let bytes = FloatImage.rgba8(image)!135 var total = 0.0136 for i in 0..<(image.width * image.height) {137 let r = Double(bytes[i * 4]), g = Double(bytes[i * 4 + 1]), b = Double(bytes[i * 4 + 2])138 total += max(r, g, b) - min(r, g, b)139 }140 return total / Double(image.width * image.height)141 }142 let base = spread(try render { _ in })143 #expect(spread(try render { $0.saturation = 0.6 }) > base * 1.3)144 #expect(spread(try render { $0.saturation = -1 }) < 1.5)145 #expect(spread(try render { $0.monochrome = true }) < 1.5)146 #expect(spread(try render { $0.lookID = ColorLUT.graphite.id }) < 1.5)147 func deviation(_ image: CGImage) -> Double {148 let bytes = FloatImage.rgba8(image)!149 let n = image.width * image.height150 let mean = (0..<n).reduce(0.0) { $0 + Double(bytes[$1 * 4 + 1]) } / Double(n)151 return ((0..<n).reduce(0.0) { $0 + pow(Double(bytes[$1 * 4 + 1]) - mean, 2) } / Double(n)).squareRoot()152 }153 #expect(deviation(try render { $0.contrast = 0.8 }) > deviation(try render { _ in }) * 1.15)154 #expect(deviation(try render { $0.contrast = -0.8 }) < deviation(try render { _ in }) * 0.85)155 }156157 @Test func toneCurveLiftsShadowsWithoutMovingTheEndpoints() throws {158 let ramp = CIImage(cgImage: Fixture.image(Fixture.ramp(width: 256, height: 8)))159 var recipe = EditRecipe(); recipe.shadows = 1160 let out = FloatImage.rgba8(try #require(EditPipeline.render(recipe: recipe, source: ramp)))!161 let plain = FloatImage.rgba8(try #require(EditPipeline.render(recipe: EditRecipe(), source: ramp)))!162 func value(_ bytes: [UInt8], _ x: Int) -> Int { Int(bytes[(4 * 256 + x) * 4 + 1]) }163 #expect(value(out, 64) > value(plain, 64) + 15)164 #expect(abs(value(out, 0) - value(plain, 0)) <= 2 && abs(value(out, 255) - value(plain, 255)) <= 2)165 // Monotonic: no tone reversal.166 for x in 1..<256 { #expect(value(out, x) >= value(out, x - 1) - 1) }167 }168169 @Test func sharpenAndNoiseReductionChangeDetailInOppositeDirections() throws {170 let noisy = CIImage(cgImage: Fixture.image(Fixture.noisy(Fixture.textured(width: 240, height: 160), sigma: 8, seed: 3)))171 func detail(_ image: CGImage) -> Double {172 let buffer = PixelBuffers.make(from: image)!173 return FocusAnalyzer.analyze(LumaPlane(pixelBuffer: buffer)!).sharpness174 }175 var recipe = EditRecipe()176 let base = detail(try #require(EditPipeline.render(recipe: recipe, source: noisy)))177 recipe.sharpness = 1178 #expect(detail(try #require(EditPipeline.render(recipe: recipe, source: noisy))) > base * 1.2)179 recipe = EditRecipe(); recipe.noiseReduction = 1180 #expect(detail(try #require(EditPipeline.render(recipe: recipe, source: noisy))) < base * 0.8)181 }182183 @Test func vignetteDarkensCornersNotTheCentre() throws {184 let white = CIImage(color: CIColor(red: 0.7, green: 0.7, blue: 0.7)).cropped(to: CGRect(x: 0, y: 0, width: 120, height: 80))185 var recipe = EditRecipe(); recipe.vignette = 1186 let bytes = FloatImage.rgba8(try #require(EditPipeline.render(recipe: recipe, source: white)))!187 let corner = Int(bytes[(2 * 120 + 2) * 4]), centre = Int(bytes[(40 * 120 + 60) * 4])188 #expect(centre - corner > 25)189 }190191 @Test func geometryProducesTheExpectedSizes() throws {192 #expect(try render { $0.quarterTurns = 1 }.width == 160)193 #expect(try render { $0.quarterTurns = 1 }.height == 240)194 #expect(try render { $0.quarterTurns = 2 }.width == 240)195 let cropped = try render { $0.crop = CGRect(x: 0.25, y: 0.5, width: 0.5, height: 0.5) }196 #expect(cropped.width == 120 && cropped.height == 80)197 let straight = try render { $0.straighten = 10 }198 let s = EditPipeline.inscribedScale(width: 240, height: 160, degrees: 10)199 #expect(abs(Double(straight.width) - 240 * s) <= 2 && abs(Double(straight.height) - 160 * s) <= 2)200 #expect(abs(Double(straight.width) / Double(straight.height) - 1.5) < 0.03)201 }202203 @Test func rotationAndFlipMovePixelsCorrectly() throws {204 // Red top-left quadrant marker on black.205 let buffer = PixelBuffers.makeBGRA(width: 40, height: 20)!206 PixelBuffers.fill(buffer) { x, y in (x < 20 && y < 10) ? (255, 0, 0) : (0, 0, 0) }207 let marker = CIImage(cgImage: Fixture.image(buffer))208 func red(_ image: CGImage, _ x: Int, _ y: Int) -> Bool { FloatImage.rgba8(image)![(y * image.width + x) * 4] > 200 }209 var recipe = EditRecipe(); recipe.quarterTurns = 1210 let turned = try #require(EditPipeline.render(recipe: recipe, source: marker))211 #expect(red(turned, 15, 5) && !red(turned, 5, 5) && !red(turned, 15, 30)) // top-left → top-right212 recipe = EditRecipe(); recipe.flippedHorizontally = true213 let flipped = try #require(EditPipeline.render(recipe: recipe, source: marker))214 #expect(red(flipped, 30, 5) && !red(flipped, 5, 5))215 recipe = EditRecipe(); recipe.crop = CGRect(x: 0, y: 0, width: 0.5, height: 0.5)216 let crop = try #require(EditPipeline.render(recipe: recipe, source: marker))217 #expect(crop.width == 20 && crop.height == 10 && red(crop, 10, 5))218 }219220 @Test func straightenLeavesNoEmptyCorners() throws {221 let white = CIImage(color: CIColor(red: 1, green: 1, blue: 1)).cropped(to: CGRect(x: 0, y: 0, width: 300, height: 200))222 for degrees in [-30.0, -7, 3, 18, 45] {223 var recipe = EditRecipe(); recipe.straighten = degrees224 let image = try #require(EditPipeline.render(recipe: recipe, source: white))225 let bytes = FloatImage.rgba8(image)!226 for (x, y) in [(1, 1), (image.width - 2, 1), (1, image.height - 2), (image.width - 2, image.height - 2)] {227 #expect(bytes[(y * image.width + x) * 4] > 245, "corner (\(x), \(y)) at \(degrees)°")228 }229 }230 }231232 @Test func previewRenderIsDownsizedAndDeepRenderIs16Bit() throws {233 var recipe = EditRecipe(); recipe.exposure = 0.3234 let preview = try #require(EditPipeline.render(recipe: recipe, source: source, maxDimension: 60))235 #expect(max(preview.width, preview.height) == 60)236 let deep = try #require(EditPipeline.render(recipe: recipe, source: source, deep: true))237 #expect(deep.bitsPerComponent == 16)238 }239}240241@Suite struct EditModelTests {242 @Test func recipeRoundTripsThroughJSON() throws {243 var recipe = EditRecipe()244 recipe.exposure = 0.7; recipe.crop = CGRect(x: 0.1, y: 0.2, width: 0.5, height: 0.6); recipe.lookID = ColorLUT.tide.id; recipe.curve[2] = 0.6245 let decoded = try JSONDecoder().decode(EditRecipe.self, from: JSONEncoder().encode(recipe))246 #expect(decoded == recipe && !decoded.isIdentity && decoded.hasGeometry)247 }248249 @Test func historyUndoRedo() {250 var history = EditHistory()251 var a = EditRecipe(); a.exposure = 1252 var b = a; b.contrast = 0.5253 history.commit(a); history.commit(a); history.commit(b)254 #expect(history.states.count == 3 && history.canUndo && !history.canRedo)255 #expect(history.undo() == a)256 #expect(history.undo() == EditRecipe())257 #expect(!history.canUndo && history.redo() == a)258 var c = EditRecipe(); c.vignette = 0.3259 history.commit(c) // drops the redo branch (b)260 #expect(!history.canRedo && history.states.count == 3 && history.current == c)261 }262263 @Test func historyIsBounded() {264 var history = EditHistory(limit: 10)265 for i in 1...50 { var r = EditRecipe(); r.exposure = Double(i) / 100; history.commit(r) }266 #expect(history.states.count == 10 && history.current.exposure == 0.5)267 }268269 @Test func toneCurveInterpolatesMonotonically() {270 let ys = [0.0, 0.4, 0.5, 0.55, 1.0]271 #expect(abs(ToneCurve.evaluate(xs: EditRecipe.curveInputs, ys: ys, at: 0.25) - 0.4) < 1e-9)272 let table = ToneCurve.table(ys: ys)273 #expect(table.first == 0 && table.last == 1)274 for i in 1..<table.count { #expect(table[i] >= table[i - 1]) }275 let identity = ToneCurve.table(ys: EditRecipe.identityCurve)276 for (i, v) in identity.enumerated() { #expect(abs(v - Float(i) / 255) < 1e-5) }277 }278279 @Test func presetsKeepGeometryAndReferenceRealLooks() {280 var base = EditRecipe(); base.quarterTurns = 1; base.crop = CGRect(x: 0, y: 0, width: 0.5, height: 0.5)281 for preset in EditPreset.all {282 let applied = preset.applied(to: base)283 #expect(applied.quarterTurns == 1 && applied.crop == base.crop)284 if let id = preset.recipe.lookID { #expect(ColorLUT.builtIn(id: id) != nil) }285 }286 #expect(Set(EditPreset.all.map(\.id)).count == EditPreset.all.count)287 }288289 @Test func rawDeveloperRejectsNonRAWFiles() throws {290 let directory = Fixture.tempDirectory()291 var options = ImageExportOptions(); options.format = .jpeg292 let jpeg = try ImageExporter.export(Fixture.image(Fixture.textured(width: 64, height: 48)), options: options, to: directory)293 #expect(!RAWDeveloper.isRAW(url: jpeg))294 #expect(RAWDeveloper.isRAW(url: URL(fileURLWithPath: "/tmp/IMG_0001.DNG")))295 #expect(throws: (any Error).self) { try RAWDeveloper.develop(url: directory.appendingPathComponent("missing.dng")) }296 }297}298