Lichtkasten v0.1: Culling, IPTC (PM-kompatibel), Vorlagen, FTP/Immich, Lightroom-Bridge
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
37 files changed
+4431
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import AppKit
|
||||
import ImageIO
|
||||
import LichtkastenCore
|
||||
|
||||
/// Fast thumbnails/previews via ImageIO. RAWs use their embedded JPEG preview first (like PM),
|
||||
/// and fall back to Apple's RAW engine only if the embedded preview is too small.
|
||||
final class ImageLoader: @unchecked Sendable {
|
||||
static let shared = ImageLoader()
|
||||
|
||||
private let thumbs = NSCache<NSString, NSImage>()
|
||||
private let previews = NSCache<NSString, NSImage>()
|
||||
private let queue = OperationQueue()
|
||||
|
||||
init() {
|
||||
thumbs.countLimit = 4000
|
||||
previews.countLimit = 12
|
||||
queue.maxConcurrentOperationCount = 6
|
||||
queue.qualityOfService = .userInitiated
|
||||
}
|
||||
|
||||
func cachedThumbnail(_ url: URL) -> NSImage? { thumbs.object(forKey: url.path as NSString) }
|
||||
func cachedPreview(_ url: URL) -> NSImage? { previews.object(forKey: url.path as NSString) }
|
||||
|
||||
func thumbnail(for url: URL, maxPixel: Int = 480) async -> NSImage? {
|
||||
if let c = cachedThumbnail(url) { return c }
|
||||
return await withCheckedContinuation { cont in
|
||||
queue.addOperation {
|
||||
let img = Self.makeImage(url, maxPixel: maxPixel, minAcceptable: 240)
|
||||
if let img { self.thumbs.setObject(img, forKey: url.path as NSString) }
|
||||
cont.resume(returning: img)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func preview(for url: URL, maxPixel: Int = 3200) async -> NSImage? {
|
||||
if let c = cachedPreview(url) { return c }
|
||||
return await withCheckedContinuation { cont in
|
||||
queue.addOperation {
|
||||
let img = Self.makeImage(url, maxPixel: maxPixel, minAcceptable: 1400)
|
||||
if let img { self.previews.setObject(img, forKey: url.path as NSString) }
|
||||
cont.resume(returning: img)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func prefetchPreviews(_ urls: [URL]) {
|
||||
for u in urls where cachedPreview(u) == nil {
|
||||
Task { _ = await preview(for: u) }
|
||||
}
|
||||
}
|
||||
|
||||
static func makeImage(_ url: URL, maxPixel: Int, minAcceptable: Int) -> NSImage? {
|
||||
guard let src = CGImageSourceCreateWithURL(url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary) else { return nil }
|
||||
var opts: [CFString: Any] = [
|
||||
kCGImageSourceCreateThumbnailFromImageIfAbsent: true,
|
||||
kCGImageSourceCreateThumbnailWithTransform: true,
|
||||
kCGImageSourceThumbnailMaxPixelSize: maxPixel,
|
||||
kCGImageSourceShouldCacheImmediately: true,
|
||||
]
|
||||
var cg = CGImageSourceCreateThumbnailAtIndex(src, 0, opts as CFDictionary)
|
||||
if let c = cg, max(c.width, c.height) < minAcceptable {
|
||||
opts[kCGImageSourceCreateThumbnailFromImageAlways] = true
|
||||
if let better = CGImageSourceCreateThumbnailAtIndex(src, 0, opts as CFDictionary) { cg = better }
|
||||
}
|
||||
guard let cg else { return nil }
|
||||
return NSImage(cgImage: cg, size: NSSize(width: cg.width, height: cg.height))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user