Repository Public License
1// ORIX Convert — App/Sources/Features/QualityLab/QualityLabModel.swift2// Description: Quality Lab state: one source file, a destination and its options, the trial conversion (a real job3// on the studios' private queue, written to a temporary folder, re-run when options change), and the4// real data compared afterwards: decoded pictures, frames, waveforms, inspections and measurements.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import AppKit7import ConvertCore8import EngineImage9import EngineMedia10import Observation1112enum LabViewMode: String, CaseIterable, Identifiable {13 case sideBySide, split, original, result, difference14 var id: String { rawValue }15 var title: String {16 switch self {17 case .sideBySide: L("Side by side")18 case .split: L("Split")19 case .original: L("Original")20 case .result: L("Result")21 case .difference: L("Difference")22 }23 }24 var symbol: String {25 switch self {26 case .sideBySide: "rectangle.split.2x1"27 case .split: "square.split.2x1"28 case .original: "photo"29 case .result: "photo.fill"30 case .difference: "plusminus"31 }32 }33}3435/// What the comparison area can show for the current pair.36enum LabVisual: Equatable {37 case none38 case picture39 case waveform40 /// Both files exist but at least one side cannot be drawn; the text says which and why.41 case unavailable(String)42}4344struct LabPixel: Equatable {45 let x: Int, y: Int46 let original: PixelProbe.Sample?47 let result: PixelProbe.Sample?48}4950@Observable final class QualityLabModel {51 enum Phase: Equatable {52 case empty, analysing, ready, converting, done, failed(String)53 }5455 @ObservationIgnored unowned let app: AppModel56 let viewport = Viewport()5758 // Source59 private(set) var source: URL?60 private(set) var sourceFile: InspectedFile?61 private(set) var entries: [TargetEntry] = []62 private(set) var notice: String?6364 // Destination65 private(set) var choice: TargetChoice?66 private(set) var descriptors: [OptionDescriptor] = []67 private(set) var optionValues: [String: OptionValue] = [:]68 private(set) var warnings: [ConversionWarning] = []6970 // Trial71 private(set) var phase: Phase = .empty72 private(set) var progress: JobProgress?73 private(set) var job: ConversionJob?74 private(set) var outputs: [URL] = []75 private(set) var resultFile: InspectedFile?76 /// Options the visible result was converted with (the form may already hold newer ones).77 private(set) var resultOptions: [String: OptionValue] = [:]7879 // Comparison80 var viewMode: LabViewMode = .split { didSet { if viewMode == .difference { ensureDifference() } } }81 var differenceGain = 16 { didSet { if differenceGain != oldValue { differenceImage = nil; ensureDifference() } } }82 private(set) var visual: LabVisual = .none83 private(set) var leftPreview: CGImage?84 private(set) var rightPreview: CGImage?85 private(set) var leftDetail: CGImage?86 private(set) var rightDetail: CGImage?87 private(set) var differenceImage: CGImage?88 private(set) var isLoadingPictures = false89 private(set) var comparison: PixelDifference?90 /// Why no pixel metric was measured, when none was.91 private(set) var comparisonNote: String?92 private(set) var imageSize: CGSize = .zero93 private(set) var pixel: LabPixel?94 /// Page (PDF, multi-file results) and time (video) shown on both sides.95 var page = 0 { didSet { if page != oldValue { reloadPictures(debounce: false) } } }96 var time = 0.0 { didSet { if time != oldValue { reloadPictures(debounce: true) } } }97 private(set) var pageCount = 198 private(set) var duration: Double?99 private(set) var leftWave: AudioWaveform?100 private(set) var rightWave: AudioWaveform?101 let waveViewport = WaveViewport()102103 @ObservationIgnored private var leftSource: PictureSource?104 @ObservationIgnored private var rightSources: [PictureSource] = []105 @ObservationIgnored private var trialTask: Task<Void, Never>?106 @ObservationIgnored private var pictureTask: Task<Void, Never>?107 @ObservationIgnored private var loadTask: Task<Void, Never>?108 @ObservationIgnored private var runCounter = 0109 @ObservationIgnored private var presetCounter = 0110111 init(app: AppModel) { self.app = app }112113 var currentEntry: TargetEntry? { entries.first { $0.id == choice?.id } }114 var options: ConversionOptions { ConversionOptions(optionValues) }115 var isStale: Bool { phase == .done && resultOptions != optionValues }116 var sourceFormat: FormatID? { sourceFile?.detection.format }117 var bytesBefore: Int64? { sourceFile?.facts.size }118 var bytesAfter: Int64? {119 guard !outputs.isEmpty else { return nil }120 return outputs.reduce(Int64(0)) { $0 + (FileInspector.facts(of: $1).size ?? 0) }121 }122 /// Result size ÷ source size.123 var sizeRatio: Double? {124 guard let before = bytesBefore, let after = bytesAfter, before > 0 else { return nil }125 return Double(after) / Double(before)126 }127128 // MARK: Source129130 func choose() {131 let panel = NSOpenPanel()132 panel.canChooseFiles = true133 panel.canChooseDirectories = false134 panel.allowsMultipleSelection = false135 panel.prompt = L("Compare")136 panel.message = L("Choose a file to convert and compare")137 if panel.runModal() == .OK, let url = panel.url { load(url) }138 }139140 func load(_ url: URL, target: String? = nil) {141 loadTask?.cancel()142 loadTask = Task { await open(url, target: target) }143 }144145 func open(_ url: URL, target: String? = nil) async {146 cancelTrial()147 reset()148 source = url149 phase = .analysing150 let inspector = app.manager.inspector151 let file = await Task.detached(priority: .userInitiated) { await inspector.inspect(url) }.value152 guard !Task.isCancelled, source == url else { return }153 sourceFile = file154 guard let format = file.detection.format else {155 phase = .failed(L("The format of “%@” could not be recognised.", url.lastPathComponent))156 return157 }158 entries = TargetCatalog.entries(for: format, manager: app.manager)159 guard !entries.isEmpty else {160 phase = .failed(L("No conversion exists for %@ in this build.", format.displayName))161 return162 }163 leftSource = PictureSource.make(for: file)164 phase = .ready165 let wanted = target?.split(separator: "#", maxSplits: 1).map(String.init) ?? []166 if let first = wanted.first, select(destination: FormatID(first), operation: wanted.count > 1 ? wanted[1] : nil) { return }167 // Default: the first recommended plain conversion.168 let recommended = app.manager.resolver.capabilities(for: format).recommended.first { $0.operation == nil }169 if let recommended, select(destination: recommended.destination, operation: nil) { return }170 if let first = entries.first(where: { $0.operation == nil }) ?? entries.first { select(destination: first.destination, operation: first.operation) }171 }172173 func clear() {174 loadTask?.cancel()175 cancelTrial()176 reset()177 }178179 private func reset() {180 pictureTask?.cancel()181 source = nil; sourceFile = nil; entries = []; notice = nil182 choice = nil; descriptors = []; optionValues = [:]; warnings = []183 phase = .empty184 clearResult()185 leftSource = nil186 leftPreview = nil; leftDetail = nil; leftWave = nil187 viewport.fit()188 }189190 private func clearResult() {191 progress = nil; job = nil; outputs = []; resultFile = nil; resultOptions = [:]192 rightSources = []193 rightPreview = nil; rightDetail = nil; differenceImage = nil; rightWave = nil194 comparison = nil; comparisonNote = nil; pixel = nil195 visual = .none196 pageCount = 1; duration = nil197 }198199 // MARK: Destination and options200201 @discardableResult202 func select(destination: FormatID, operation: String?) -> Bool {203 guard let format = sourceFormat, let picked = TargetCatalog.choice(from: format, to: destination, operation: operation, manager: app.manager),204 !(picked.acceptsMultipleSources && operation != nil) else { return false }205 choice = picked206 descriptors = TargetCatalog.descriptors(for: picked, registry: app.registry)207 let valid = Set(descriptors.map(\.id))208 optionValues = optionValues.filter { valid.contains($0.key) }209 for (key, value) in app.launch.optionOverrides where valid.contains(key) && optionValues[key] == nil { optionValues[key] = value }210 scheduleTrial(debounce: false)211 return true212 }213214 func setOption(_ key: String, _ value: OptionValue?) {215 if let descriptor = descriptors.first(where: { $0.id == key }), descriptor.defaultValue == value { optionValues[key] = nil } else { optionValues[key] = value }216 scheduleTrial(debounce: true)217 }218219 func resetOptions() {220 optionValues = [:]221 scheduleTrial(debounce: false)222 }223224 // MARK: Trial conversion225226 func cancelTrial() {227 trialTask?.cancel()228 trialTask = nil229 if phase == .converting { phase = outputs.isEmpty ? .ready : .done }230 progress = nil231 }232233 func rerun() { scheduleTrial(debounce: false) }234235 /// Starts (or restarts) the trial. A run made stale by a newer change is cancelled.236 private func scheduleTrial(debounce: Bool) {237 trialTask?.cancel()238 guard source != nil, choice != nil else { return }239 phase = .converting240 progress = nil241 trialTask = Task { [weak self] in242 if debounce { try? await Task.sleep(for: .milliseconds(450)) }243 if Task.isCancelled { return }244 await self?.runTrial()245 }246 }247248 /// Runs the trial now and waits for the comparison data (used by the self-test).249 func runTrialNow() async {250 trialTask?.cancel()251 let task = Task { await runTrial() }252 trialTask = task253 await task.value254 await pictureTask?.value255 }256257 private func runTrial() async {258 guard let source, let file = sourceFile, let format = sourceFormat, let choice else { return }259 let usedOptions = optionValues260 let options = ConversionOptions(usedOptions)261 phase = .converting262 // What the engine says before converting, for this file and these options.263 if let item = await app.manager.smartDrop.analyze([source]).items.first,264 let preflight = try? app.manager.smartDrop.preflight(item, to: choice.destination, operation: choice.operation, options: options) {265 warnings = preflight.warnings266 }267 if Task.isCancelled { return }268 runCounter += 1269 let run = runCounter270 let directory = app.studio.scratchDirectory("quality-lab").appendingPathComponent("run-\(run)", isDirectory: true)271 let request = ConversionJobRequest(sources: [source], sourceFormat: format, destinationFormat: choice.destination, operation: choice.operation, options: options,272 destination: OutputDestination(directory: directory, nameTemplate: "{name}", conflictPolicy: .rename), priority: .high)273 let finished = await app.studio.run(request) { [weak self] progress in274 if self?.runCounter == run { self?.progress = progress }275 }276 guard !Task.isCancelled, run == runCounter else {277 try? FileManager.default.removeItem(at: directory)278 return279 }280 progress = nil281 guard let finished, finished.state == .finished else {282 job = finished283 phase = .failed(finished?.error.map { LE($0) } ?? L("The trial conversion did not finish."))284 return285 }286 // The previous result is replaced only now, so the comparison stays on screen while a new run converts.287 // A result can be a folder (one image per page): its files are what gets compared and measured.288 let written = finished.outputs.filter { $0.outcome != .skipped }.map(\.url).flatMap(Self.filesInside)289 .sorted { $0.path.localizedStandardCompare($1.path) == .orderedAscending }290 let inspector = app.manager.inspector291 let inspectedResult: InspectedFile? = await { guard let first = written.first else { return nil }; return await inspector.inspect(first) }()292 var pictures: [PictureSource] = []293 for url in written.prefix(200) {294 let inspected = url == written.first ? inspectedResult : await inspector.inspect(url, deep: false)295 if let inspected, let picture = PictureSource.make(for: inspected) { pictures.append(picture) } else { pictures = []; break }296 }297 guard !Task.isCancelled, run == runCounter else { return }298 clearResult()299 job = finished300 outputs = written301 resultFile = inspectedResult302 resultOptions = usedOptions303 rightSources = pictures304 phase = .done305 removeOldRuns(keeping: directory)306 decideVisual(source: file)307 }308309 private nonisolated static func filesInside(_ url: URL) -> [URL] {310 var isDirectory: ObjCBool = false311 guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory), isDirectory.boolValue else { return [url] }312 var found: [URL] = []313 if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles]) {314 for case let file as URL in enumerator where (try? file.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true {315 found.append(file)316 if found.count >= 2000 { break }317 }318 }319 return found320 }321322 private func removeOldRuns(keeping: URL) {323 let root = keeping.deletingLastPathComponent()324 for name in (try? FileManager.default.contentsOfDirectory(atPath: root.path)) ?? [] where name != keeping.lastPathComponent {325 try? FileManager.default.removeItem(at: root.appendingPathComponent(name))326 }327 }328329 // MARK: Comparison data330331 private func decideVisual(source file: InspectedFile) {332 guard let result = resultFile else { visual = .unavailable(L("The conversion produced no file to compare.")); return }333 let resultCategory = result.detection.format?.category334 let sourceHasAudio = file.detection.format?.category == .audio || file.inspection.audioCodec != nil335 if let leftSource, !rightSources.isEmpty {336 let pages = max(leftSource.pageCount, rightSources.count > 1 ? rightSources.count : (rightSources.first?.pageCount ?? 1))337 pageCount = max(1, pages)338 duration = [leftSource.duration, rightSources.first?.duration].compactMap { $0 }.min()339 if page >= pageCount { page = 0 }340 if let duration, time > duration || time == 0 { time = min(duration * 0.35, max(0, duration - 0.05)) }341 visual = .picture342 reloadPictures(debounce: false)343 } else if resultCategory == .audio, sourceHasAudio {344 visual = .waveform345 loadWaveforms()346 } else {347 let side = leftSource == nil ? (file.detection.format?.displayName ?? "?") : (result.detection.format?.displayName ?? "?")348 visual = .unavailable(L("A visual comparison is not available for this pair: %@ cannot be drawn here. The measured facts are listed on the right.", side))349 }350 }351352 private func rightSource(for page: Int) -> PictureSource? {353 guard let first = rightSources.first else { return nil }354 return rightSources.count > 1 ? rightSources[min(page, rightSources.count - 1)] : first355 }356357 private func reloadPictures(debounce: Bool) {358 guard visual == .picture, let leftSource, let right = rightSource(for: page) else { return }359 pictureTask?.cancel()360 isLoadingPictures = true361 let page = page, time = time362 let rightPage = rightSources.count > 1 ? 0 : page363 let gain = differenceGain, wantsDifference = viewMode == .difference364 let leftWidth = sourceFile?.inspection.pixelWidth, leftHeight = sourceFile?.inspection.pixelHeight365 pictureTask = Task { [weak self] in366 if debounce { try? await Task.sleep(for: .milliseconds(140)) }367 if Task.isCancelled { return }368 async let a = Self.render(leftSource, limit: PictureSource.previewLimit, page: page, time: time)369 async let b = Self.render(right, limit: PictureSource.previewLimit, page: rightPage, time: time)370 let (left, rightImage) = await (a, b)371 guard !Task.isCancelled, let self else { return }372 guard let left = left.image?.image, let rightImage = rightImage.image?.image else {373 self.isLoadingPictures = false374 self.leftPreview = nil; self.rightPreview = nil375 self.visual = .unavailable(L("A visual comparison is not available for this pair: %@", LE(left.error ?? rightImage.error ?? "")))376 return377 }378 self.leftPreview = left379 self.rightPreview = rightImage380 self.leftDetail = nil; self.rightDetail = nil; self.differenceImage = nil381 // The canvas works in the pixels of the original when they are known (images, video), otherwise in the382 // pixels of the rendered page.383 if case .image = leftSource, let w = leftWidth, let h = leftHeight, w > 0, h > 0 {384 let upright = (CGFloat(left.width) / CGFloat(left.height) > 1) == (CGFloat(w) / CGFloat(h) > 1)385 self.imageSize = upright ? CGSize(width: w, height: h) : CGSize(width: h, height: w)386 } else {387 self.imageSize = CGSize(width: left.width, height: left.height)388 }389 self.isLoadingPictures = false390 let measured = await Task.detached(priority: .userInitiated) { () -> (PixelDifference?, String?, SendableImage?) in391 var difference: PixelDifference?392 var note: String?393 if left.width == rightImage.width, left.height == rightImage.height {394 do { difference = try ImageComparison.compare(left, rightImage) } catch { note = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription }395 } else {396 note = "sizes"397 }398 let image = wantsDifference ? PixelMath.difference(left, rightImage, gain: gain).map(SendableImage.init) : nil399 return (difference, note, image)400 }.value401 guard !Task.isCancelled else { return }402 self.comparison = measured.0403 self.comparisonNote = measured.1 == "sizes"404 ? L("Not measured: the two pictures do not have the same pixel dimensions (%@ and %@).", "\(left.width) × \(left.height)", "\(rightImage.width) × \(rightImage.height)")405 : measured.1.map { LE($0) }406 if let image = measured.2?.image { self.differenceImage = image }407 // Larger bitmaps for zooming, only when the files really hold more pixels than the previews.408 if case .image = leftSource, Int(max(self.imageSize.width, self.imageSize.height)) > PictureSource.previewLimit {409 async let la = Self.render(leftSource, limit: PictureSource.detailLimit, page: page, time: time)410 async let lb = Self.render(right, limit: PictureSource.detailLimit, page: rightPage, time: time)411 let (detailA, detailB) = await (la, lb)412 guard !Task.isCancelled else { return }413 self.leftDetail = detailA.image?.image414 self.rightDetail = detailB.image?.image415 }416 }417 }418419 private nonisolated static func render(_ source: PictureSource, limit: Int, page: Int, time: Double) async -> (image: SendableImage?, error: String?) {420 do { return (SendableImage(try await source.render(limit: limit, page: page, time: time)), nil) } catch {421 return (nil, (error as? LocalizedError)?.errorDescription ?? error.localizedDescription)422 }423 }424425 private func ensureDifference() {426 guard differenceImage == nil, let left = leftPreview, let right = rightPreview else { return }427 let gain = differenceGain428 Task { [weak self] in429 let image = await Task.detached(priority: .userInitiated) { PixelMath.difference(left, right, gain: gain).map(SendableImage.init) }.value430 guard let self, self.leftPreview === left, self.differenceGain == gain else { return }431 self.differenceImage = image?.image432 }433 }434435 private func loadWaveforms() {436 guard let source, let output = outputs.first else { return }437 pictureTask?.cancel()438 isLoadingPictures = true439 let needsLeft = leftWave == nil440 pictureTask = Task { [weak self] in441 async let a: AudioWaveform? = needsLeft ? (try? await MediaQualityLab.waveform(of: source, buckets: 4000)) : nil442 async let b = try? await MediaQualityLab.waveform(of: output, buckets: 4000)443 let (left, right) = await (a, b)444 guard !Task.isCancelled, let self else { return }445 if needsLeft { self.leftWave = left }446 self.rightWave = right447 self.isLoadingPictures = false448 if self.leftWave == nil || right == nil {449 self.visual = .unavailable(L("A visual comparison is not available for this pair: the audio of one of the two files could not be decoded."))450 }451 }452 }453454 /// Reads the pixel under the pointer in both pictures, from the largest bitmaps loaded.455 func hover(_ point: CGPoint?) {456 guard let point, visual == .picture, let left = leftDetail ?? leftPreview, let right = rightDetail ?? rightPreview else {457 if pixel != nil { pixel = nil }458 return459 }460 let next = LabPixel(x: Int(point.x), y: Int(point.y), original: PixelProbe.sample(left, at: point, originalSize: imageSize),461 result: PixelProbe.sample(right, at: point, originalSize: imageSize))462 if next != pixel { pixel = next }463 }464465 /// The bitmaps the inspector reads are reduced copies when the file holds more pixels than this.466 var inspectorIsReduced: Bool {467 guard let left = leftDetail ?? leftPreview else { return false }468 return CGFloat(left.width) < imageSize.width * 0.999469 }470471 // MARK: Actions472473 /// Sends the file with the chosen destination and options to the Convert workspace.474 func useSettings() {475 guard let source, let choice else { return }476 let values = optionValues477 Task {478 app.section = .convert479 app.convertPane = .prepare480 await app.workspace.ingest([source])481 guard let group = app.workspace.groups.first(where: { $0.files.contains { $0.url.standardizedFileURL == source.standardizedFileURL } }) else { return }482 app.workspace.selectedGroupID = group.id483 guard app.workspace.select(destination: choice.destination, operation: choice.operation, in: group) else { return }484 let valid = Set(group.descriptors.map(\.id))485 group.optionValues = values.filter { valid.contains($0.key) }486 app.workspace.schedulePreflight(group)487 }488 }489490 func defaultPresetName() -> String {491 guard let format = sourceFormat, let choice else { return "" }492 return "\(format.shortLabel) → \(choice.title)"493 }494495 /// Saves the destination and options as a preset (secrets are never stored).496 func savePreset(named name: String) async {497 guard let format = sourceFormat, let choice else { return }498 let preset = ConversionPreset(name: name, source: .format(format), destinationFormat: choice.destination, operation: choice.operation, options: options.redactingSecrets())499 do {500 _ = try await app.presets.add(preset)501 notice = L("Preset “%@” saved.", name)502 } catch { notice = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription }503 await app.reloadPresets()504 }505506 func dismissNotice() { notice = nil }507}508