feat: load LUT libraries and selections in background with cancellation and visible-only thumbnails

这个提交包含在:
mac 于 2026-11-01 11:08:10 +08:00 提交
1 父节点 57830ca744
当前提交 87baa785fd
9 个文件已更改
+710 -100

没有与搜索条件相匹配的文件

+108 -41
查看文件
@@ -1,5 +1,9 @@
#import "DYLUTLibrary.h"
#include <algorithm>
#include <charconv>
#include <string_view>
#include <string>
#include <xlocale.h>
#include <cmath>
#include <cstring>
#include <limits>
@@ -58,7 +62,12 @@ static void DYLUTSample(const std::vector<float>& table,NSUInteger n,const float
}
}
static BOOL DYLUTParse(NSString *path,std::vector<float>& data,NSUInteger& size,NSError **error) {
static BOOL DYLUTCancelled(NSProgress *progress,NSError **error) {
if(!progress.cancelled)return NO;
if(error)*error=[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:@{NSLocalizedDescriptionKey:@"已取消 LUT 加载"}];
return YES;
}
static BOOL DYLUTParse(NSString *path,std::vector<float>& data,NSUInteger& size,NSProgress *progress,NSError **error) {
data.clear();size=0;
NSDictionary *attributes=[[NSFileManager defaultManager]attributesOfItemAtPath:path error:error];
if(!attributes)return NO;
@@ -76,28 +85,37 @@ static BOOL DYLUTParse(NSString *path,std::vector<float>& data,NSUInteger& size,
for(float v:data)if(!std::isfinite(v))return DYLUTFail(error,@"输入 LUT 含无效数字");
size=n;return YES;
}
NSString *text=[[[NSString alloc]initWithData:bytes encoding:NSUTF8StringEncoding]autorelease];
if(!text)return DYLUTFail(error,@"LUT 必须是 UTF-8 文本");
float domainMin[3]={0,0,0},domainMax[3]={1,1,1};
BOOL foundSize=NO;
for(NSString *line in [text componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) {
NSString *clean=[[[line componentsSeparatedByString:@"#"]firstObject]stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
if(!clean.length)continue;
const char *c=clean.UTF8String;int n=0;float r,g,b;
if(sscanf(c,"LUT_3D_SIZE %d",&n)==1) {
if(foundSize||n<2||n>128)return DYLUTFail(error,@"LUT 尺寸应为 2–128,且只能声明一次");
// Validate encoding once, then parse byte spans without allocating an
// NSString, NSArray and temporary strings for every numeric row.
NSString *text=[[NSString alloc]initWithData:bytes encoding:NSUTF8StringEncoding];
if(!text)return DYLUTFail(error,@"LUT 必须是 UTF-8 文本");[text release];
std::string storage((const char *)bytes.bytes,bytes.length);
static locale_t numericLocale;static dispatch_once_t localeOnce;dispatch_once(&localeOnce,^{numericLocale=newlocale(LC_NUMERIC_MASK,"C",NULL);});
float domainMin[3]={0,0,0},domainMax[3]={1,1,1};BOOL foundSize=NO;
std::string_view remaining(storage);NSUInteger rows=0;
if(remaining.size()>=3&&remaining.substr(0,3)=="\xef\xbb\xbf")remaining.remove_prefix(3);
auto trim=[](std::string_view v){while(!v.empty()&&(v.front()==' '||v.front()=='\t'||v.front()=='\r'))v.remove_prefix(1);return v;};
auto number=[&](std::string_view& v,float& value){v=trim(v);if(v.empty())return false;char *end=nullptr;value=strtof_l(v.data(),&end,numericLocale);if(end==v.data()||end>v.data()+v.size())return false;v.remove_prefix(end-v.data());return true;};
while(!remaining.empty()) {
if((rows++&1023)==0&&DYLUTCancelled(progress,error))return NO;
size_t end=remaining.find_first_of("\r\n");std::string_view line=remaining.substr(0,end);
if(end==std::string_view::npos)remaining={};else remaining.remove_prefix(end+1);
line=line.substr(0,line.find('#'));line=trim(line);if(line.empty())continue;
float r=0,g=0,b=0;
auto starts=[&](std::string_view prefix){return line.substr(0,prefix.size())==prefix;};
if(starts("LUT_3D_SIZE")) {
auto v=trim(line.substr(11));int n=0;auto result=std::from_chars(v.data(),v.data()+v.size(),n);
if(result.ec!=std::errc{}||foundSize||n<2||n>128)return DYLUTFail(error,@"LUT 尺寸应为 2–128,且只能声明一次");
size=n;foundSize=YES;data.reserve(size*size*size*4);
} else if(sscanf(c,"DOMAIN_MIN %f %f %f",&r,&g,&b)==3) {
domainMin[0]=r;domainMin[1]=g;domainMin[2]=b;
} else if(sscanf(c,"DOMAIN_MAX %f %f %f",&r,&g,&b)==3) {
domainMax[0]=r;domainMax[1]=g;domainMax[2]=b;
} else if(sscanf(c,"%f %f %f",&r,&g,&b)==3) {
if(!std::isfinite(r)||!std::isfinite(g)||!std::isfinite(b))return DYLUTFail(error,@"LUT 含无效数字");
data.insert(data.end(),{r,g,b,1});
if(data.size()>128*128*128*4)return DYLUTFail(error,@"LUT 数据过多");
} else if([clean hasPrefix:@"LUT_1D_SIZE"])return DYLUTFail(error,@"请选择三维 .cube LUT");
else if(![clean hasPrefix:@"TITLE"]&&![clean hasPrefix:@"LUT_3D_INPUT_RANGE"])
return DYLUTFail(error,[NSString stringWithFormat:@"无法解析 LUT 行:%@",[clean substringToIndex:MIN(clean.length,(NSUInteger)80)]]);
} else if(starts("DOMAIN_MIN")||starts("DOMAIN_MAX")) {
auto v=line.substr(10);if(!number(v,r)||!number(v,g)||!number(v,b))return DYLUTFail(error,@"LUT DOMAIN 范围无效");
float *domain=starts("DOMAIN_MIN")?domainMin:domainMax;domain[0]=r;domain[1]=g;domain[2]=b;
} else if(starts("TITLE")||starts("LUT_3D_INPUT_RANGE"))continue;
else if(starts("LUT_1D_SIZE"))return DYLUTFail(error,@"请选择三维 .cube LUT");
else {
auto v=line;if(!number(v,r)||!number(v,g)||!number(v,b)||!std::isfinite(r)||!std::isfinite(g)||!std::isfinite(b))return DYLUTFail(error,@"LUT 含无效数据行");
data.insert(data.end(),{r,g,b,1});if(data.size()>128*128*128*4)return DYLUTFail(error,@"LUT 数据过多");
}
}
if(size<2||data.size()!=size*size*size*4)return DYLUTFail(error,@"LUT 数据数量与尺寸不符");
bool resample=false;
@@ -108,6 +126,7 @@ static BOOL DYLUTParse(NSString *path,std::vector<float>& data,NSUInteger& size,
if(resample) {
std::vector<float> original=data;
for(NSUInteger z=0;z<size;z++)for(NSUInteger y=0;y<size;y++)for(NSUInteger x=0;x<size;x++) {
if(x==0&&DYLUTCancelled(progress,error))return NO;
float rgb[3]={(float(x)/(size-1)-domainMin[0])/(domainMax[0]-domainMin[0]),
(float(y)/(size-1)-domainMin[1])/(domainMax[1]-domainMin[1]),
(float(z)/(size-1)-domainMin[2])/(domainMax[2]-domainMin[2])};
@@ -118,7 +137,7 @@ static BOOL DYLUTParse(NSString *path,std::vector<float>& data,NSUInteger& size,
return YES;
}
BOOL DYLUTReadCube(NSString *path,std::vector<float>& data,NSUInteger& size,NSError **error) {
static BOOL DYLUTReadCancellable(NSString *path,std::vector<float>& data,NSUInteger& size,NSProgress *progress,NSError **error) {
data.clear();size=0;
if(!path.length)return DYLUTFail(error,@"未选择 LUT 文件");
NSDictionary *a=[[NSFileManager defaultManager]attributesOfItemAtPath:path error:error];
@@ -127,12 +146,26 @@ BOOL DYLUTReadCube(NSString *path,std::vector<float>& data,NSUInteger& size,NSEr
DYLUTTable *cached=[DYLUTTableCache()objectForKey:key];
if(cached){data=cached->values;size=cached->dimension;return YES;}
std::vector<float> parsed;NSUInteger n=0;
if(!DYLUTParse(path,parsed,n,error))return NO;
if(DYLUTCancelled(progress,error)||!DYLUTParse(path,parsed,n,progress,error))return NO;
DYLUTTable *table=[[[DYLUTTable alloc]init]autorelease];table->values=std::move(parsed);table->dimension=n;
[DYLUTTableCache()setObject:table forKey:key cost:table->values.size()*sizeof(float)];
data=table->values;size=n;return YES;
}
BOOL DYLUTReadCube(NSString *path,std::vector<float>& data,NSUInteger& size,NSError **error) {
return DYLUTReadCancellable(path,data,size,nil,error);
}
NSProgress *DYLUTPrepareAsync(NSString *path,void (^completion)(NSError *)) {
static dispatch_queue_t queue;static dispatch_once_t once;
dispatch_once(&once,^{queue=dispatch_queue_create("com.dayu.color.lut-selection",DISPATCH_QUEUE_SERIAL);dispatch_set_target_queue(queue,dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0));});
NSProgress *progress=[NSProgress discreteProgressWithTotalUnitCount:1];progress.localizedDescription=@"正在载入 LUT…";
dispatch_async(queue,^{@autoreleasepool{
NSError *error=nil;std::vector<float> values;NSUInteger n=0;
DYLUTReadCancellable(path,values,n,progress,&error);DYLUTCancelled(progress,&error);progress.completedUnitCount=1;
dispatch_async(dispatch_get_main_queue(),^{if(completion)completion(error);});
}});return progress;
}
NSData *DYLUTTransformRGBA(NSData *source,NSUInteger width,NSUInteger height,NSString *path,NSError **error) {
if(!width||!height||width>8192||height>8192||width*height>8192*8192||source.length!=width*height*16) {
DYLUTFail(error,@"缩略图源必须是有效的 float RGBA 图像");return nil;
@@ -238,6 +271,8 @@ static BOOL DYLUTEnsureDirectory(NSURL *directory,NSURL *root,NSError **error) {
NSURL *_rootURL;
NSArray *_entries,*_folders;
NSMutableSet *_favoritePaths;
NSObject *_fileLock;
dispatch_queue_t _fileQueue;
}
@end
@implementation DYLUTLibrary
@@ -246,25 +281,30 @@ static BOOL DYLUTEnsureDirectory(NSURL *directory,NSURL *root,NSError **error) {
- (instancetype)initWithRootURL:(NSURL *)rootURL {
if(!rootURL.isFileURL||!rootURL.path.length){[self release];return nil;}
if((self=[super init])) {
_fileLock=[NSObject new];_fileQueue=dispatch_queue_create("com.dayu.color.lut-files",DISPATCH_QUEUE_SERIAL);dispatch_set_target_queue(_fileQueue,dispatch_get_global_queue(QOS_CLASS_UTILITY,0));
_rootURL=[DYLUTCanonicalURL(rootURL)copy];_entries=[@[]copy];_folders=[@[@""]copy];_favoritePaths=[NSMutableSet new];
NSDictionary *saved=[NSDictionary dictionaryWithContentsOfURL:[_rootURL URLByAppendingPathComponent:@".favorites.plist"]];
NSArray *relativePaths=[saved[@"relativePaths"]isKindOfClass:NSArray.class]?saved[@"relativePaths"]:@[];
for(NSString *path in relativePaths)if([path isKindOfClass:NSString.class]&&![path isAbsolutePath]&&![path.pathComponents containsObject:@".."]) [_favoritePaths addObject:[_rootURL.path stringByAppendingPathComponent:path]];
}return self;
}
- (void)dealloc {[_rootURL release];[_entries release];[_folders release];[_favoritePaths release];[super dealloc];}
- (void)dealloc {[_rootURL release];[_entries release];[_folders release];[_favoritePaths release];[_fileLock release];dispatch_release(_fileQueue);[super dealloc];}
- (NSURL *)rootURL {return [[_rootURL copy]autorelease];}
- (NSArray *)entries {@synchronized(self){return [[_entries copy]autorelease];}}
- (NSArray *)folders {@synchronized(self){return [[_folders copy]autorelease];}}
- (NSSet *)favoritePaths {@synchronized(self){return [[_favoritePaths copy]autorelease];}}
- (BOOL)reload:(NSError **)error {@synchronized(self){
- (BOOL)reload:(NSError **)error {return [self reloadProgress:nil error:error];}
- (BOOL)reloadProgress:(NSProgress *)progress error:(NSError **)error {@synchronized(_fileLock){
BOOL isDirectory=NO;NSFileManager *fm=NSFileManager.defaultManager;
if(![fm fileExistsAtPath:_rootURL.path isDirectory:&isDirectory]){[_entries release];_entries=[@[]copy];[_folders release];_folders=[@[@""]copy];return YES;}
if(DYLUTCancelled(progress,error))return NO;
if(![fm fileExistsAtPath:_rootURL.path isDirectory:&isDirectory]){@synchronized(self){[_entries release];_entries=[@[]copy];[_folders release];_folders=[@[@""]copy];}return YES;}
if(!isDirectory||!DYLUTOrdinaryURL(_rootURL,NULL,error))return DYLUTFail(error,@"LUT 库目录无效");
NSMutableArray *entries=[NSMutableArray array];NSMutableSet *folders=[NSMutableSet setWithObject:@""];
__block NSError *scanError=nil;
NSDirectoryEnumerator *enumerator=[fm enumeratorAtURL:_rootURL includingPropertiesForKeys:@[NSURLIsDirectoryKey,NSURLIsRegularFileKey,NSURLIsSymbolicLinkKey] options:NSDirectoryEnumerationSkipsHiddenFiles|NSDirectoryEnumerationSkipsPackageDescendants errorHandler:^BOOL(NSURL *url,NSError *e){(void)url;scanError=e;return NO;}];
for(NSURL *url in enumerator) {
if(DYLUTCancelled(progress,error))return NO;
progress.completedUnitCount++;
NSNumber *link=nil;[url getResourceValue:&link forKey:NSURLIsSymbolicLinkKey error:NULL];
if(link.boolValue){[enumerator skipDescendants];continue;}
BOOL directory=NO;if(!DYLUTOrdinaryURL(url,&directory,NULL))continue;
@@ -275,12 +315,15 @@ static BOOL DYLUTEnsureDirectory(NSURL *directory,NSURL *root,NSError **error) {
}
if(scanError){if(error)*error=scanError;return NO;}
[entries sortUsingComparator:^NSComparisonResult(NSDictionary *a,NSDictionary *b){return [a[@"relativePath"]localizedStandardCompare:b[@"relativePath"]];}];
[_entries release];_entries=[entries copy];[_folders release];_folders=[[folders.allObjects sortedArrayUsingSelector:@selector(localizedStandardCompare:)]copy];return YES;
if(DYLUTCancelled(progress,error))return NO;
@synchronized(self){[_entries release];_entries=[entries copy];[_folders release];_folders=[[folders.allObjects sortedArrayUsingSelector:@selector(localizedStandardCompare:)]copy];}return YES;
}}
- (NSArray *)importURLs:(NSArray<NSURL *> *)urls error:(NSError **)error {@synchronized(self){
- (NSArray *)importURLs:(NSArray<NSURL *> *)urls error:(NSError **)error {return [self importURLs:urls progress:nil error:error];}
- (NSArray *)importURLs:(NSArray<NSURL *> *)urls progress:(NSProgress *)progress error:(NSError **)error {@synchronized(_fileLock){
NSFileManager *fm=NSFileManager.defaultManager;NSMutableArray *plans=[NSMutableArray array];
NSString *root=_rootURL.path;
for(NSURL *sourceURL in urls) {
if(DYLUTCancelled(progress,error))return nil;
if(!sourceURL.isFileURL){DYLUTFail(error,@"只能导入本机 LUT 文件");return nil;}
NSURL *source=[sourceURL URLByStandardizingPath];BOOL directory=NO;
if(!DYLUTOrdinaryURL(source,&directory,error))return nil;
@@ -295,6 +338,7 @@ static BOOL DYLUTEnsureDirectory(NSURL *directory,NSURL *root,NSError **error) {
__block NSError *scanError=nil;
NSDirectoryEnumerator *enumerator=[fm enumeratorAtURL:source includingPropertiesForKeys:@[NSURLIsSymbolicLinkKey,NSURLIsDirectoryKey,NSURLIsRegularFileKey] options:NSDirectoryEnumerationSkipsHiddenFiles|NSDirectoryEnumerationSkipsPackageDescendants errorHandler:^BOOL(NSURL *url,NSError *e){(void)url;scanError=e;return NO;}];
for(NSURL *file in enumerator) {
if(DYLUTCancelled(progress,error))return nil;
NSNumber *link=nil;[file getResourceValue:&link forKey:NSURLIsSymbolicLinkKey error:NULL];
if(link.boolValue){[enumerator skipDescendants];continue;}
BOOL subdirectory=NO;if(!DYLUTOrdinaryURL(file,&subdirectory,NULL)||subdirectory||![file.pathExtension.lowercaseString isEqual:@"cube"])continue;
@@ -308,23 +352,46 @@ static BOOL DYLUTEnsureDirectory(NSURL *directory,NSURL *root,NSError **error) {
}
if(!plans.count){DYLUTFail(error,@"没有找到可导入的 .cube 文件");return nil;}
// Validate every source before writing the library, preserving existing files.
for(NSDictionary *plan in plans){std::vector<float> values;NSUInteger n=0;if(!DYLUTReadCube([plan[@"source"]path],values,n,error))return nil;}
progress.totalUnitCount=plans.count*2;progress.completedUnitCount=0;
for(NSDictionary *plan in plans){
NSError *itemError=nil;BOOL valid=NO;
@autoreleasepool{
progress.localizedDescription=[NSString stringWithFormat:@"正在校验 %lld/%lu",progress.completedUnitCount+1,(unsigned long)plans.count];progress.localizedAdditionalDescription=[plan[@"source"]lastPathComponent];
std::vector<float> values;NSUInteger n=0;valid=!DYLUTCancelled(progress,&itemError)&&DYLUTReadCancellable([plan[@"source"]path],values,n,progress,&itemError);[itemError retain];
}
if(!valid){if(error)*error=[itemError autorelease];else[itemError release];return nil;}[itemError release];progress.completedUnitCount++;
}
if(!DYLUTEnsureDirectory(_rootURL,_rootURL,error))return nil;
NSMutableArray *created=[NSMutableArray array],*imported=[NSMutableArray array];BOOL success=YES;
for(NSDictionary *plan in plans) {
NSURL *destination=[_rootURL URLByAppendingPathComponent:plan[@"relative"]];
if(!DYLUTEnsureDirectory(destination.URLByDeletingLastPathComponent,_rootURL,error)){success=NO;break;}
NSString *stem=destination.path.stringByDeletingPathExtension,*extension=destination.pathExtension;
NSUInteger suffix=1;
while([fm fileExistsAtPath:destination.path])destination=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@ %lu.%@",stem,(unsigned long)++suffix,extension]];
if(![fm copyItemAtURL:plan[@"source"] toURL:destination error:error]){success=NO;break;}
[created addObject:destination];
std::vector<float> values;NSUInteger n=0;if(!DYLUTReadCube(destination.path,values,n,error)){success=NO;break;}
[imported addObject:DYLUTEntry(destination,_rootURL)];
for(NSDictionary *plan in plans){
NSError *itemError=nil;BOOL copied=NO;
@autoreleasepool{
copied=[&]()->BOOL{
if(DYLUTCancelled(progress,&itemError))return NO;
progress.localizedDescription=[NSString stringWithFormat:@"正在导入 %lu/%lu",(unsigned long)imported.count+1,(unsigned long)plans.count];progress.localizedAdditionalDescription=[plan[@"source"]lastPathComponent];
NSURL *destination=[_rootURL URLByAppendingPathComponent:plan[@"relative"]];
if(!DYLUTEnsureDirectory(destination.URLByDeletingLastPathComponent,_rootURL,&itemError))return NO;
NSString *stem=destination.path.stringByDeletingPathExtension,*extension=destination.pathExtension;NSUInteger suffix=1;
while([fm fileExistsAtPath:destination.path])destination=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@ %lu.%@",stem,(unsigned long)++suffix,extension]];
if(![fm copyItemAtURL:plan[@"source"] toURL:destination error:&itemError])return NO;[created addObject:destination];
std::vector<float> values;NSUInteger n=0;if(!DYLUTReadCancellable(destination.path,values,n,progress,&itemError))return NO;
[imported addObject:DYLUTEntry(destination,_rootURL)];return YES;
}();[itemError retain];
}
if(!copied){if(error)*error=[itemError autorelease];else[itemError release];success=NO;break;}[itemError release];progress.completedUnitCount++;
}
if(success)success=!DYLUTCancelled(progress,error)&&[self reload:error];
if(!success){for(NSURL *url in created)[fm removeItemAtURL:url error:NULL];return nil;}
if(![self reload:error])return nil;return imported;
return imported;
}}
- (NSProgress *)reloadAsync:(void (^)(NSError *))completion {
NSProgress *progress=[NSProgress discreteProgressWithTotalUnitCount:-1];progress.localizedDescription=@"正在扫描 LUT 文件夹…";
dispatch_async(_fileQueue,^{@autoreleasepool{NSError *error=nil;[self reloadProgress:progress error:&error];progress.totalUnitCount=MAX(1,progress.completedUnitCount);progress.completedUnitCount=progress.totalUnitCount;dispatch_async(dispatch_get_main_queue(),^{if(completion)completion(error);});}});return progress;
}
- (NSProgress *)importURLsAsync:(NSArray<NSURL *> *)urls completion:(void (^)(NSArray<NSDictionary *> *,NSError *))completion {
NSProgress *progress=[NSProgress discreteProgressWithTotalUnitCount:-1];progress.localizedDescription=@"正在查找 LUT 文件…";NSArray *sources=[[urls copy]autorelease];
dispatch_async(_fileQueue,^{@autoreleasepool{NSError *error=nil;NSArray *imported=[self importURLs:sources progress:progress error:&error];dispatch_async(dispatch_get_main_queue(),^{if(completion)completion(imported,error);});}});return progress;
}
- (BOOL)isFavoritePath:(NSString *)path {@synchronized(self){return [_favoritePaths containsObject:path];}}
- (BOOL)setFavorite:(BOOL)favorite forPath:(NSString *)path error:(NSError **)error {@synchronized(self){
NSString *prefix=[_rootURL.path stringByAppendingString:@"/"];