SPB Git

Repository Public License

RAWDeveloper.swift 6.6 KB 150 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Camera9// File             : Packages/ORIXCameraKit/Sources/ORIXImaging/Editing/RAWDeveloper.swift10// Description      : RAW development with Core Image's CIRAWFilter (Bayer DNG11//                    and Apple ProRAW). The DNG on disk is never rewritten:12//                    development settings are parameters, the output is a new13//                    rendered image.14// Module           : ORIXImaging15// Repository       : https://git.spboucher.ai/orix-camera16// Project Website  : https://www.orixlab.dev17// Created          : 2026-11-2118// Last Modified    : 2026-11-2119//20// =============================================================================21// AUTHOR & PROFESSIONAL AFFILIATION22// =============================================================================23//24// Author           : Simon-Pierre Boucher25// Position         : Professor of Finance26// Institution      : Université du Québec en Outaouais (UQO)27// Department       : Département des sciences administratives28// Organization     : SP Boucher29//30// Website          : https://spboucher.ai31// Git              : https://git.spboucher.ai32//33// =============================================================================34// OFFICIAL CONTACT DIRECTORY35// =============================================================================36//37// General          : info@spboucher.ai38// Professional     : contact@spboucher.ai39// Direct           : simon@spboucher.ai40// Research         : research@spboucher.ai41// Development      : dev@spboucher.ai42// Support          : support@spboucher.ai43// Security/Admin   : admin@spboucher.ai44// Notifications    : notifications@spboucher.ai45//46// Preferred Contact: support@spboucher.ai47//48// =============================================================================49// COPYRIGHT & INTELLECTUAL PROPERTY50// =============================================================================51//52// Copyright (c) 202653// Simon-Pierre Boucher54//55// All rights reserved where applicable.56//57// This file is governed by the applicable project license,58// copyright notices, institutional agreements and relevant law.59//60// Unless expressly authorized by the applicable license or61// rights holder, copying, redistribution, sublicensing,62// publication and commercial exploitation are not permitted.63//64// Existing third-party rights and notices remain unaffected.65//66// =============================================================================67// CONFIDENTIALITY & SECURITY68// =============================================================================69//70// Classification   : Public71// Distribution     : Public repository; use is governed by the project license72// Security Contact : admin@spboucher.ai73//74// Access to this source does not independently grant permission75// to redistribute or disclose confidential materials.76//77// Do not embed credentials, private keys, access tokens,78// certificates or confidential personal information.79//80// =============================================================================81// LICENSE & MAINTENANCE82// =============================================================================83//84// License          : Proprietary, all rights reserved (see license file)85// License File     : LICENSE86// Maintainer       : Simon-Pierre Boucher87// Technical Contact: dev@spboucher.ai88//89// =============================================================================90// END OF SP BOUCHER SOURCE HEADER91// =============================================================================9293import CoreImage94import Foundation9596public struct RAWSettings: Codable, Sendable, Equatable {97    /// Stops added at the demosaic stage (before highlight clipping of the rendered image).98    public var exposure: Double = 099    /// Kelvin. Nil keeps the as-shot white balance.100    public var temperature: Double?101    public var tint: Double?102    /// 0…1.103    public var luminanceNoiseReduction: Double?104    public var colorNoiseReduction: Double?105    public var sharpness: Double?106    /// 0 = linear rendering, 1 = the default tone boost.107    public var boost: Double = 1108    public init() {}109}110111public struct RAWInfo: Sendable, Equatable {112    public var nativeWidth: Int113    public var nativeHeight: Int114    public var asShotTemperature: Double115    public var asShotTint: Double116    public var supportsLuminanceNoiseReduction: Bool117    public var supportsSharpness: Bool118}119120public enum RAWDeveloper {121    public enum RAWError: Error, Equatable { case unsupported }122123    public static func isRAW(url: URL) -> Bool {124        ["dng", "raw", "cr2", "cr3", "nef", "arw", "raf", "orf", "rw2"].contains(url.pathExtension.lowercased())125    }126127    public static func info(url: URL) -> RAWInfo? {128        guard let filter = CIRAWFilter(imageURL: url) else { return nil }129        return RAWInfo(nativeWidth: Int(filter.nativeSize.width), nativeHeight: Int(filter.nativeSize.height),130                       asShotTemperature: Double(filter.neutralTemperature), asShotTint: Double(filter.neutralTint),131                       supportsLuminanceNoiseReduction: filter.isLuminanceNoiseReductionSupported, supportsSharpness: filter.isSharpnessSupported)132    }133134    /// Develops the RAW file. `draft` trades quality for speed (previews).135    public static func develop(url: URL, settings: RAWSettings = RAWSettings(), draft: Bool = false, scale: Double = 1) throws -> CIImage {136        guard let filter = CIRAWFilter(imageURL: url) else { throw RAWError.unsupported }137        filter.isDraftModeEnabled = draft138        filter.scaleFactor = Float(min(max(scale, 0.05), 1))139        filter.exposure = Float(settings.exposure)140        filter.boostAmount = Float(min(max(settings.boost, 0), 1))141        if let t = settings.temperature { filter.neutralTemperature = Float(t) }142        if let t = settings.tint { filter.neutralTint = Float(t) }143        if let v = settings.luminanceNoiseReduction, filter.isLuminanceNoiseReductionSupported { filter.luminanceNoiseReductionAmount = Float(v) }144        if let v = settings.colorNoiseReduction, filter.isColorNoiseReductionSupported { filter.colorNoiseReductionAmount = Float(v) }145        if let v = settings.sharpness, filter.isSharpnessSupported { filter.sharpnessAmount = Float(v) }146        guard let image = filter.outputImage else { throw RAWError.unsupported }147        return image148    }149}150
Files main
Menu

Theme