new features
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import AppKit
|
||||
import CoreImage
|
||||
import Metal
|
||||
import ImageIO
|
||||
import ImagePlayground
|
||||
import SwiftUI
|
||||
@@ -10,6 +12,33 @@ struct StudioImagePreview: Sendable {
|
||||
let height: Int
|
||||
let png: Data
|
||||
|
||||
private static let renderer: CIContext = {
|
||||
if let device = MTLCreateSystemDefaultDevice() { return CIContext(mtlDevice: device) }
|
||||
return CIContext(options: [.useSoftwareRenderer: true])
|
||||
}()
|
||||
|
||||
func adjusted(exposure: Double) throws -> Self {
|
||||
guard exposure.isFinite, let input = CIImage(data: png) else { throw StudioError("Could not read the preview.") }
|
||||
let output = input.applyingFilter("CIExposureAdjust", parameters: [kCIInputEVKey: min(3, max(-3, exposure))])
|
||||
guard let image = Self.renderer.createCGImage(output, from: input.extent) else {
|
||||
throw StudioError("Could not render the adjusted preview.")
|
||||
}
|
||||
let encoded = NSMutableData()
|
||||
guard let destination = CGImageDestinationCreateWithData(encoded, UTType.png.identifier as CFString, 1, nil) else {
|
||||
throw StudioError("Could not create the PNG preview.")
|
||||
}
|
||||
CGImageDestinationAddImage(destination, image, nil)
|
||||
guard CGImageDestinationFinalize(destination) else { throw StudioError("Could not encode the PNG preview.") }
|
||||
return Self(name: name, width: width, height: height, png: encoded as Data)
|
||||
}
|
||||
|
||||
private init(name: String, width: Int, height: Int, png: Data) {
|
||||
self.name = name
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.png = png
|
||||
}
|
||||
|
||||
init(data: Data, name: String, maxDimension: Int = 2048) throws {
|
||||
guard let source = CGImageSourceCreateWithData(data as CFData, nil),
|
||||
let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any],
|
||||
@@ -92,6 +121,9 @@ struct CreativeWorkspace: View {
|
||||
private struct ImageWorkspace: View {
|
||||
@State private var prompt = ""
|
||||
@State private var preview: StudioImagePreview?
|
||||
@State private var original: StudioImagePreview?
|
||||
@State private var exposureStep = 0
|
||||
@State private var adjustment: Task<Void, Never>?
|
||||
@State private var importing = false
|
||||
@State private var status = ""
|
||||
|
||||
@@ -111,6 +143,9 @@ private struct ImageWorkspace: View {
|
||||
}
|
||||
Divider()
|
||||
Button("Open Image…", systemImage: "folder") { importing = true }
|
||||
Stepper("Exposure \(Double(exposureStep) / 2, specifier: "%+.1f") EV", value: $exposureStep, in: -6...6)
|
||||
.disabled(original == nil)
|
||||
.onChange(of: exposureStep) { applyExposure() }
|
||||
Button("Export Preview PNG…", systemImage: "square.and.arrow.up", action: export)
|
||||
.disabled(preview == nil)
|
||||
if !status.isEmpty {
|
||||
@@ -156,12 +191,32 @@ private struct ImageWorkspace: View {
|
||||
let access = url.startAccessingSecurityScopedResource()
|
||||
defer { if access { url.stopAccessingSecurityScopedResource() } }
|
||||
do {
|
||||
preview = try StudioImagePreview(data: Data(contentsOf: url, options: .mappedIfSafe),
|
||||
let image = try StudioImagePreview(data: Data(contentsOf: url, options: .mappedIfSafe),
|
||||
name: url.deletingPathExtension().lastPathComponent)
|
||||
adjustment?.cancel()
|
||||
exposureStep = 0
|
||||
original = image
|
||||
preview = image
|
||||
status = ""
|
||||
} catch { status = error.localizedDescription }
|
||||
}
|
||||
|
||||
private func applyExposure() {
|
||||
adjustment?.cancel()
|
||||
guard let original else { return }
|
||||
let exposure = Double(exposureStep) / 2
|
||||
if exposure == 0 { preview = original; return }
|
||||
adjustment = Task {
|
||||
do {
|
||||
let image = try await Task.detached(priority: .userInitiated) { try original.adjusted(exposure: exposure) }.value
|
||||
guard !Task.isCancelled else { return }
|
||||
preview = image
|
||||
} catch {
|
||||
if !Task.isCancelled { status = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func export() {
|
||||
guard let preview else { return }
|
||||
let panel = NSSavePanel()
|
||||
|
||||
Reference in New Issue
Block a user