SPB Git

Repository Public License

LibraryFeatureTests.swift 7.8 KB 168 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Camera9// File             : Packages/ORIXCameraKit/Tests/ORIXImagingTests/LibraryFeatureTests.swift10// Description      : Engine pieces added for the library and editor: RAW11//                    settings inside the recipe, crop 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 Foundation93import Testing94@testable import ORIXImaging9596@Suite struct RecipeRAWTests {97    @Test func defaultRAWSettingsCountAsNoEdit() {98        var recipe = EditRecipe()99        recipe.raw = RAWSettings()100        #expect(recipe.raw == nil && recipe.isIdentity)101        var raw = RAWSettings(); raw.exposure = 0.5102        recipe.raw = raw103        #expect(!recipe.isIdentity && recipe.raw?.exposure == 0.5)104    }105106    @Test func rawSettingsRoundTripAndOldRecipesStillDecode() throws {107        var recipe = EditRecipe(); recipe.exposure = 0.2108        var raw = RAWSettings(); raw.temperature = 4800; raw.luminanceNoiseReduction = 0.4109        recipe.raw = raw110        let decoded = try JSONDecoder().decode(EditRecipe.self, from: JSONEncoder().encode(recipe))111        #expect(decoded == recipe)112        // A recipe saved before the field existed has no "raw" key.113        var object = try #require(try JSONSerialization.jsonObject(with: JSONEncoder().encode(recipe)) as? [String: Any])114        object["raw"] = nil115        let old = try JSONDecoder().decode(EditRecipe.self, from: JSONSerialization.data(withJSONObject: object))116        #expect(old.raw == nil && old.exposure == 0.2)117    }118119    @Test func presetsAndGeometryOnlyKeepWhatTheyShould() {120        var base = EditRecipe(); base.quarterTurns = 3; base.exposure = 1; base.lookID = ColorLUT.vivid.id121        var raw = RAWSettings(); raw.exposure = -1; base.raw = raw122        #expect(EditPreset.all[1].applied(to: base).raw == raw)123        let g = base.geometryOnly124        #expect(g.quarterTurns == 3 && g.exposure == 0 && g.lookID == nil && g.raw == nil)125    }126}127128@Suite struct CropMathTests {129    @Test func normalizationClampsAndFullFrameIsNil() {130        let r = CropMath.normalized(CGRect(x: -0.2, y: 0.9, width: 0.5, height: 0.5))131        #expect(r.minX == 0 && abs(r.maxY - 1) < 1e-9 && r.width == 0.5)132        #expect(CropMath.normalized(CGRect(x: 0.5, y: 0.5, width: 0.001, height: 0.001)).width == CropMath.minimumSide)133        #expect(CropMath.recipeValue(CropMath.unit) == nil)134        #expect(CropMath.recipeValue(CGRect(x: 0.1, y: 0, width: 0.8, height: 1)) != nil)135    }136137    @Test func aspectRectanglesHaveThePixelRatio() {138        for (ratio, aspect) in [(1.0, 1.5), (16.0 / 9, 0.75), (4.0 / 5, 1.5), (1.5, 1.5)] {139            let r = CropMath.rect(ratio: ratio, imageAspect: aspect)140            #expect(abs(Double(r.width / r.height) * aspect - ratio) < 1e-9)141            #expect(r.minX >= 0 && r.minY >= 0 && r.maxX <= 1 + 1e-9 && r.maxY <= 1 + 1e-9)142            #expect(abs(r.midX - 0.5) < 1e-9 && abs(r.midY - 0.5) < 1e-9)143        }144    }145146    @Test func dragsMoveOneCornerAndKeepTheOpposite() {147        let start = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)148        let dragged = CropMath.drag(start, handle: .topLeft, delta: CGSize(width: 0.1, height: 0.05))149        #expect(abs(dragged.minX - 0.3) < 1e-9 && abs(dragged.minY - 0.25) < 1e-9 && abs(dragged.maxX - 0.8) < 1e-9 && abs(dragged.maxY - 0.8) < 1e-9)150        let moved = CropMath.drag(start, handle: .move, delta: CGSize(width: 0.9, height: -0.9))151        #expect(abs(moved.maxX - 1) < 1e-9 && moved.minY == 0 && moved.size == start.size)152        let outside = CropMath.drag(start, handle: .bottomRight, delta: CGSize(width: 5, height: 5))153        #expect(abs(outside.maxX - 1) < 1e-9 && abs(outside.maxY - 1) < 1e-9)154    }155156    @Test func constrainedDragsKeepTheRatioInsideTheFrame() {157        let aspect = 1.5158        let start = CropMath.rect(ratio: 1, imageAspect: aspect)159        for handle in [CropMath.Handle.topLeft, .topRight, .bottomLeft, .bottomRight] {160            for delta in [CGSize(width: 0.1, height: 0.02), CGSize(width: -0.2, height: 0.3), CGSize(width: 2, height: 2), CGSize(width: -2, height: -2)] {161                let r = CropMath.drag(start, handle: handle, delta: delta, ratio: 1, imageAspect: aspect)162                #expect(abs(Double(r.width / r.height) * aspect - 1) < 1e-6)163                #expect(r.minX >= -1e-9 && r.minY >= -1e-9 && r.maxX <= 1 + 1e-9 && r.maxY <= 1 + 1e-9)164            }165        }166    }167}168
Files main
Menu

Theme