SPB Git

Repository Public License

TestSupport.swift 8.9 KB 202 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/TestSupport.swift10// Description      : Shared fixtures for the imaging tests: deterministic11//                    buffers, noise, temporary directories.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 CoreVideo93import Foundation94@testable import ORIXImaging9596struct SeededRandom {97    var state: UInt6498    init(_ seed: UInt64) { state = seed }99    mutating func next() -> Double {100        state = state &* 6364136223846793005 &+ 1442695040888963407101        return Double((state >> 33) & 0xFFFFFF) / Double(0xFFFFFF)102    }103    /// Approximately normal (sum of uniforms), mean 0, standard deviation 1.104    mutating func gaussian() -> Double {105        var s = 0.0106        for _ in 0..<12 { s += next() }107        return s - 6108    }109}110111enum Fixture {112    static func solid(_ r: UInt8, _ g: UInt8, _ b: UInt8, width: Int = 64, height: Int = 48) -> CVPixelBuffer {113        let buffer = PixelBuffers.makeBGRA(width: width, height: height)!114        PixelBuffers.fill(buffer) { _, _ in (r, g, b) }115        return buffer116    }117118    /// Horizontal grey ramp 0…255.119    static func ramp(width: Int = 256, height: Int = 32) -> CVPixelBuffer {120        let buffer = PixelBuffers.makeBGRA(width: width, height: height)!121        PixelBuffers.fill(buffer) { x, _ in let v = UInt8(x * 255 / (width - 1)); return (v, v, v) }122        return buffer123    }124125    /// Checkerboard of `cell` pixels: maximal fine detail.126    static func checker(width: Int = 128, height: Int = 96, cell: Int = 4, low: UInt8 = 30, high: UInt8 = 220) -> CVPixelBuffer {127        let buffer = PixelBuffers.makeBGRA(width: width, height: height)!128        PixelBuffers.fill(buffer) { x, y in let v = ((x / cell) + (y / cell)) % 2 == 0 ? low : high; return (v, v, v) }129        return buffer130    }131132    /// A smooth, textured colour image (no repetition, so registration has a unique answer).133    static func textured(width: Int = 256, height: Int = 192, shiftX: Int = 0, shiftY: Int = 0) -> CVPixelBuffer {134        let buffer = PixelBuffers.makeBGRA(width: width, height: height)!135        PixelBuffers.fill(buffer) { px, py in136            let x = Double(px - shiftX), y = Double(py - shiftY)137            let a = sin(x * 0.071 + y * 0.013) + sin(x * 0.023 - y * 0.091) + sin((x * x + y * y).squareRoot() * 0.05)138            let b = sin(x * 0.19 + 1.3) * sin(y * 0.17 + 0.4)139            let v = 128 + a * 30 + b * 25140            func c(_ d: Double) -> UInt8 { UInt8(min(max(d, 0), 255)) }141            return (c(v + 20 * sin(x * 0.03)), c(v), c(v - 20 * sin(y * 0.04)))142        }143        return buffer144    }145146    /// Aperiodic value noise (seeded lattice, bilinear): registration has a single correct answer.147    static func natural(width: Int = 320, height: Int = 240, shiftX: Int = 0, shiftY: Int = 0) -> CVPixelBuffer {148        func lattice(_ ix: Int, _ iy: Int, _ octave: Int) -> Double {149            var h = UInt64(bitPattern: Int64(ix &* 73856093 ^ iy &* 19349663 ^ octave &* 83492791))150            h = (h ^ (h >> 33)) &* 0xff51afd7ed558ccd; h = (h ^ (h >> 33)) &* 0xc4ceb9fe1a85ec53; h ^= h >> 33151            return Double(h & 0xFFFF) / 65535152        }153        func noise(_ x: Double, _ y: Double, _ cell: Double, _ octave: Int) -> Double {154            let fx = x / cell, fy = y / cell155            let ix = Int(floor(fx)), iy = Int(floor(fy)), tx = fx - floor(fx), ty = fy - floor(fy)156            let a = lattice(ix, iy, octave) * (1 - tx) + lattice(ix + 1, iy, octave) * tx157            let b = lattice(ix, iy + 1, octave) * (1 - tx) + lattice(ix + 1, iy + 1, octave) * tx158            return a * (1 - ty) + b * ty159        }160        let buffer = PixelBuffers.makeBGRA(width: width, height: height)!161        PixelBuffers.fill(buffer) { px, py in162            let x = Double(px - shiftX) + 1000, y = Double(py - shiftY) + 1000163            let v = noise(x, y, 37, 0) * 0.5 + noise(x, y, 13, 1) * 0.3 + noise(x, y, 5, 2) * 0.2164            let c = UInt8(min(max(v * 255, 0), 255))165            return (c, c, c)166        }167        return buffer168    }169170    static func noisy(_ source: CVPixelBuffer, sigma: Double, seed: UInt64) -> CVPixelBuffer {171        let w = CVPixelBufferGetWidth(source), h = CVPixelBufferGetHeight(source)172        let out = PixelBuffers.copy(source)!173        var random = SeededRandom(seed)174        PixelBuffers.withLocked(out, readOnly: false) { base, rowBytes, _, _ in175            for y in 0..<h {176                let row = base.advanced(by: y * rowBytes).assumingMemoryBound(to: UInt8.self)177                for x in 0..<(w * 4) where x % 4 != 3 {178                    row[x] = UInt8(min(max(Double(row[x]) + random.gaussian() * sigma, 0), 255))179                }180            }181        }182        return out183    }184185    static func tempDirectory() -> URL {186        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-tests-" + UUID().uuidString, isDirectory: true)187        try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)188        return url189    }190191    static func image(_ buffer: CVPixelBuffer) -> CGImage { PixelBuffers.cgImage(from: buffer)! }192193    /// Mean of each channel of a CGImage (0…255).194    static func means(_ image: CGImage) -> (r: Double, g: Double, b: Double) {195        let bytes = FloatImage.rgba8(image)!196        var r = 0.0, g = 0.0, b = 0.0197        let n = image.width * image.height198        for i in 0..<n { r += Double(bytes[i * 4]); g += Double(bytes[i * 4 + 1]); b += Double(bytes[i * 4 + 2]) }199        return (r / Double(n), g / Double(n), b / Double(n))200    }201}202
Files main
Menu

Theme