复用 LUT 浏览器位图并消除点击闪黑

这个提交包含在:
mac 于 2026-11-08 21:50:39 +08:00 提交
1 父节点 a0225404a2
当前提交 e0bd8342c0
9 个文件已更改
+150 -83

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

+99 -74
查看文件
@@ -88,6 +88,7 @@ static NSRect DYBrowserTileImageRect(NSRect bounds,BOOL list) {
@interface DYBrowserTile : NSButton
@property(retain) NSImage *thumbnail;
@property NSUInteger previewGeneration;
@property(copy) NSString *displayName;
@property(copy) NSString *failure;
@property(copy) NSDictionary *palette;
@@ -158,6 +159,7 @@ static NSRect DYBrowserTileImageRect(NSRect bounds,BOOL list) {
- (BOOL)isFlipped{return YES;}
- (void)dealloc{[_original release];[_styled release];[super dealloc];}
- (void)setOriginal:(NSImage *)original styled:(NSImage *)styled{
if(_original==original&&_styled==styled)return;
if(_original!=original){[_original release];_original=[original retain];}
if(_styled!=styled){[_styled release];_styled=[styled retain];}
[self setNeedsDisplay:YES];
@@ -210,7 +212,8 @@ static NSRect DYBrowserTileImageRect(NSRect bounds,BOOL list) {
@interface DYLUTBrowser () <NSWindowDelegate,NSPopoverDelegate,NSSearchFieldDelegate,NSSplitViewDelegate> {
DYLUTLibrary *_library;
NSDictionary *_previewFrames;
NSDictionary *_previewFrames,*_comparisonOriginal;
NSNumber *_selectionFrameTime;
NSString *_selectedPath,*_folder;
NSDictionary *_palette;
DYLUTSelectionHandler _selection;
@@ -232,12 +235,12 @@ static NSRect DYBrowserTileImageRect(NSRect bounds,BOOL list) {
NSMutableSet *_queuedPaths;
NSCache *_thumbnailCache;
dispatch_queue_t _previewQueue,_compareQueue;
DYBrowserWork *_work,*_compareWork;
BOOL _closed,_sizing,_visible,_remoteMode,_importing,_layingOut,_detached,_embedded,_compactRecent;
DYBrowserWork *_work;
BOOL _closed,_visible,_remoteMode,_importing,_layingOut,_detached,_embedded,_compactRecent;
NSArray *_recentSnapshot;
NSProgress *_libraryTask,*_selectionTask;
NSTimer *_progressTimer;
NSUInteger _libraryGeneration,_selectionGeneration;
NSUInteger _libraryGeneration,_selectionGeneration,_previewGeneration;
NSInteger _columns;
CGFloat _tileWidth,_tileHeight,_sidebarWidth;
}
@@ -261,7 +264,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
NSString *savedFolder=[[NSUserDefaults standardUserDefaults]stringForKey:DYBrowserPreference(DYBrowserFolderPreference,library)]?:[[NSUserDefaults standardUserDefaults]stringForKey:DYBrowserFolderPreference];
if(!library.recentPaths.count&&path.length)[library recordUsedPath:path];
if(!savedFolder)savedFolder=library.recentPaths.count?DYBrowserRecentFolder:@"*";
_library=[library retain];_previewFrames=[frames copy];_recentSnapshot=[library.recentPaths copy];_selectedPath=[path copy];_folder=[(savedFolder?:@"*") copy];_selection=[selection copy];
_library=[library retain];_previewFrames=[frames copy];_previewGeneration=1;_recentSnapshot=[library.recentPaths copy];_selectedPath=[path copy];_folder=[(savedFolder?:@"*") copy];_selection=[selection copy];
_folderButtons=[NSMutableArray new];_tiles=[NSMutableDictionary new];_queuedPaths=[NSMutableSet new];_thumbnailCache=[NSCache new];_thumbnailCache.countLimit=600;_thumbnailCache.totalCostLimit=64*1024*1024;_previewQueue=dispatch_queue_create("com.dayu.color.lut-thumbnails",DISPATCH_QUEUE_SERIAL);dispatch_set_target_queue(_previewQueue,dispatch_get_global_queue(QOS_CLASS_UTILITY,0));
panel.title=@"风格 LUT";panel.contentMinSize=self.class.minimumContentSize;panel.contentMaxSize=self.class.maximumContentSize;panel.releasedWhenClosed=NO;panel.hidesOnDeactivate=NO;panel.delegate=self;
_root=[[DYBrowserCanvas alloc]initWithFrame:NSMakeRect(0,0,760,560)];_root.accessibilityLabel=@"LUT 缩略图浏览库";panel.contentView=_root;
@@ -280,7 +283,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
NSUserDefaults *preferences=NSUserDefaults.standardUserDefaults;
_hd=[[NSButton checkboxWithTitle:@"高清预览" target:self action:@selector(qualityChanged:)]retain];
_hd.font=[NSFont systemFontOfSize:11];_hd.state=[preferences boolForKey:DYBrowserPreference(DYBrowserHDPreference,library)]?NSControlStateValueOn:NSControlStateValueOff;
_hd.toolTip=@"从源帧取样并以双倍像素生成缩略图";
_hd.toolTip=@"缩略图与划像共用高清帧预览";
_comparePanel=[[DYBrowserCanvas alloc]initWithFrame:NSZeroRect];_comparePanel.fillKey=@"card";
_compareTitle=[BrowserLabel(@"原图与 LUT 对比",13)retain];_compareTitle.font=[NSFont systemFontOfSize:13 weight:NSFontWeightSemibold];
_compareStatus=[BrowserLabel(@"等待播放头画面",11)retain];
@@ -324,8 +327,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
[_libraryTask cancel];[_libraryTask release];[_selectionTask cancel];[_selectionTask release];[_progressTimer invalidate];[_progressTimer release];
_work.cancelled=YES;_work.owner=nil;[_work release];self.window.delegate=nil;
_popover.delegate=nil;[_popover close];[_popover release];[_root removeFromSuperview];
_compareWork.cancelled=YES;_compareWork.owner=nil;[_compareWork release];
[_library release];[_previewFrames release];[_selectedPath release];[_folder release];[_palette release];[_selection release];[_closeAction release];
[_library release];[_previewFrames release];[_comparisonOriginal release];[_selectionFrameTime release];[_selectedPath release];[_folder release];[_palette release];[_selection release];[_closeAction release];
[_root release];[_folderDocument release];[_gridDocument release];[_folderScroll release];[_gridScroll release];
[_titleLabel release];[_countLabel release];[_statusLabel release];[_sizeLabel release];[_emptyLabel release];[_search release];
[_importFiles release];[_importFolder release];[_reveal release];[_refresh release];[_none release];[_done release];[_sizeSlider release];[_layoutMenu release];_splitView.delegate=nil;[_splitView release];[_presentationWindow release];
@@ -453,7 +455,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
// The API owner's completion may release this controller synchronously.
[self retain];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
_closed=YES;_visible=NO;_work.cancelled=YES;_work.owner=nil;_compareWork.cancelled=YES;_compareWork.owner=nil;[_libraryTask cancel];[_selectionTask cancel];[_progressTimer invalidate];[_progressTimer release];_progressTimer=nil;
_closed=YES;_visible=NO;_work.cancelled=YES;_work.owner=nil;[_libraryTask cancel];[_selectionTask cancel];[_progressTimer invalidate];[_progressTimer release];_progressTimer=nil;
_popover.delegate=nil;if(_popover.shown)[_popover close];
if(_remoteMode&&_root.superview)for(NSNotificationName name in @[NSViewFrameDidChangeNotification,NSViewBoundsDidChangeNotification])[[NSNotificationCenter defaultCenter]removeObserver:self name:name object:_root.superview];
[_root removeFromSuperview];[_queuedPaths removeAllObjects];[_thumbnailCache removeAllObjects];
@@ -546,7 +548,8 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
}
for(NSString *path in _tiles.allKeys)if(![wanted containsObject:path]){[_tiles[path]removeFromSuperview];[_tiles removeObjectForKey:path];changed=YES;}
if(changed||!_work)[self resetThumbnailWork];
if(!_sizing)[self generateVisibleThumbnails];
[self refreshComparison];
[self generateVisibleThumbnails];
}
- (void)updatePalette:(NSDictionary *)palette{
@@ -564,47 +567,97 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
for(DYBrowserTile *tile in _tiles.allValues){tile.palette=_palette;[tile setNeedsDisplay:YES];}
[_root setNeedsDisplay:YES];[_folderDocument setNeedsDisplay:YES];[_gridDocument setNeedsDisplay:YES];
}
- (NSString *)previewQualityForFrames:(NSDictionary *)frames {
BOOL high=!_compactRecent&&_hd.state==NSControlStateValueOn;
return frames[@"high"]&&(high||!frames[@"standard"])?@"high":@"standard";
}
- (NSSize)previewSize {
NSValue *size=_previewFrames[@"standardSize"]?:_previewFrames[@"highSize"];
NSString *quality=[self previewQualityForFrames:_previewFrames];
NSValue *size=_previewFrames[[quality stringByAppendingString:@"Size"]];
return size?size.sizeValue:NSMakeSize(320,180);
}
- (void)updatePreview:(NSDictionary *)frames{
if(_closed||_previewFrames==frames||[_previewFrames isEqual:frames])return;
[_previewFrames release];_previewFrames=[frames copy];
- (void)invalidatePreviewImages {
++_previewGeneration;
[_thumbnailCache removeAllObjects];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshThumbnailSize) object:nil];
[self refreshThumbnailSize];
[self refreshComparison];
[_comparisonOriginal release];_comparisonOriginal=nil;
[self resetThumbnailWork];
}
- (void)updatePreview:(NSDictionary *)frames{
if(_closed||_previewFrames==frames||(!frames[@"standard"]&&!frames[@"high"]))return;
NSString *currentQuality=[self previewQualityForFrames:_previewFrames],*nextQuality=[self previewQualityForFrames:frames];
NSValue *currentSize=_previewFrames[[currentQuality stringByAppendingString:@"Size"]];
NSValue *nextSize=frames[[nextQuality stringByAppendingString:@"Size"]];
BOOL sameSize=[currentSize isEqual:nextSize];
// A host render after our LUT write can already contain that LUT. Keep
// the browser's pre-click reference until the playhead moves.
if(sameSize&&[_selectionFrameTime isEqual:frames[@"time"]])return;
[_selectionFrameTime release];_selectionFrameTime=nil;
NSData *current=_previewFrames[currentQuality],*next=frames[nextQuality];
BOOL unchanged=sameSize&&[current isEqualToData:next];
NSDictionary *snapshot=[frames copy];
[_previewFrames release];_previewFrames=snapshot;
if(unchanged)return;
[self invalidatePreviewImages];
[self layoutBrowser];
}
- (void)qualityChanged:(id)sender{
[NSUserDefaults.standardUserDefaults setBool:_hd.state==NSControlStateValueOn forKey:DYBrowserPreference(DYBrowserHDPreference,_library)];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshThumbnailSize) object:nil];
[self refreshThumbnailSize];
[self invalidatePreviewImages];
[self layoutBrowser];
}
- (void)comparePositionChanged:(id)sender{_compareView.position=_compareSlider.doubleValue;}
- (void)refreshComparison{
if(_closed||_compactRecent||!self.visible)return;
_compareWork.cancelled=YES;_compareWork.owner=nil;[_compareWork release];_compareWork=nil;
NSData *frame=_previewFrames[@"high"]?:_previewFrames[@"standard"];
if(!frame){[_compareView setOriginal:nil styled:nil];_compareSlider.enabled=NO;_compareStatus.stringValue=@"等待播放头画面";return;}
NSSize size=[(_previewFrames[@"highSize"]?:_previewFrames[@"standardSize"]) sizeValue];
NSString *path=_selectedPath;
NSString *name=path.lastPathComponent.stringByDeletingPathExtension;
_compareWork=[DYBrowserWork new];_compareWork.owner=self;DYBrowserWork *work=_compareWork;
dispatch_async(_compareQueue,^{@autoreleasepool{
- (NSDictionary *)previewResultForPath:(NSString *)path{
DYBrowserTile *tile=_tiles[path];
if(tile.previewGeneration==_previewGeneration){
if(tile.thumbnail)return @{@"image":tile.thumbnail};
if(tile.failure.length)return @{@"error":tile.failure};
}
return [_thumbnailCache objectForKey:path];
}
- (void)displayPreviewResult:(NSDictionary *)result inTile:(DYBrowserTile *)tile{
tile.previewGeneration=_previewGeneration;
tile.thumbnail=result[@"image"];tile.failure=result[@"error"];
tile.enabled=tile.thumbnail!=nil;
tile.toolTip=tile.failure?:tile.identifier;
[tile setNeedsDisplay:YES];
}
- (void)requestPreviewForPath:(NSString *)path comparison:(BOOL)comparison{
if(!_work||[_queuedPaths containsObject:path])return;
[_queuedPaths addObject:path];
NSData *frame=_previewFrames[[self previewQualityForFrames:_previewFrames]];
NSSize size=[self previewSize];
DYBrowserWork *work=_work;
dispatch_async(comparison?_compareQueue:_previewQueue,^{@autoreleasepool{
if(work.cancelled)return;
NSImage *original=DYLUTThumbnail(nil,frame,size.width,size.height,size,nil);
if(work.cancelled)return;
NSImage *styled=path.length?DYLUTThumbnail(path,frame,size.width,size.height,size,nil):nil;
NSError *error=nil;
NSImage *image=DYLUTThumbnail(path,frame,size.width,size.height,size,&error);
NSDictionary *result=image?@{@"image":image}:@{@"error":error.localizedDescription?:@"无法预览此 LUT"};
dispatch_async(dispatch_get_main_queue(),^{
DYLUTBrowser *owner=work.owner;if(work.cancelled||!owner)return;
owner->_compareSlider.enabled=styled!=nil&&original!=nil;
[owner->_compareView setOriginal:original styled:styled];
owner->_compareStatus.stringValue=path.length?(styled?name:@"无法预览当前 LUT"):@"选择一个 LUT 查看对比";
owner->_compareStatus.toolTip=owner->_compareStatus.stringValue;
[owner->_queuedPaths removeObject:path];
if(path.length){
[owner->_thumbnailCache setObject:result forKey:path cost:image?(NSUInteger)(size.width*size.height)*4:128];
DYBrowserTile *tile=owner->_tiles[path];
if(tile)[owner displayPreviewResult:result inTile:tile];
}else owner->_comparisonOriginal=[result retain];
if(!path.length||[path isEqualToString:owner->_selectedPath])[owner refreshComparison];
});
}});
}
- (void)refreshComparison{
if(_closed||_compactRecent||!self.visible||!_previewFrames.count)return;
NSString *path=_selectedPath;
NSDictionary *original=_comparisonOriginal,*styled=path.length?[self previewResultForPath:path]:nil;
if(!original)[self requestPreviewForPath:@"" comparison:YES];
if(path.length&&!styled)[self requestPreviewForPath:path comparison:YES];
if(!original||(path.length&&!styled))return;
NSImage *source=original[@"image"],*look=styled[@"image"];
_compareSlider.enabled=source!=nil&&look!=nil;
[_compareView setOriginal:source styled:look];
_compareStatus.stringValue=path.length?(look?path.lastPathComponent.stringByDeletingPathExtension:@"无法预览当前 LUT"):@"选择一个 LUT 查看对比";
_compareStatus.toolTip=_compareStatus.stringValue;
}
- (void)setStatus:(NSString *)status{_statusLabel.stringValue=status?:@"";_statusLabel.toolTip=status;}
- (void)rebuildFolders{
NSMutableArray *paths=[NSMutableArray arrayWithObjects:DYBrowserRecentFolder,@"*",nil];NSMutableSet *seen=[NSMutableSet setWithArray:paths];
@@ -641,7 +694,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
}
- (void)reloadLibrary:(id)sender{
if(_libraryTask){[_libraryTask cancel];[self setStatus:@"正在取消…"];return;}
[_thumbnailCache removeAllObjects];NSUInteger generation=++_libraryGeneration;
[self invalidatePreviewImages];NSUInteger generation=++_libraryGeneration;
NSProgress *task=[_library reloadAsync:^(NSError *error){
if(_closed||generation!=_libraryGeneration)return;[self endLibraryTask];
[self rebuildFolders];[self rebuildGrid];[self refreshComparison];[self setStatus:error?error.localizedDescription:@"单击缩略图应用 LUT"];
@@ -657,48 +710,18 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
- (void)controlTextDidChange:(NSNotification *)notification{if(notification.object==_search)[self rebuildGrid];}
- (void)sizeChanged:(id)sender{
[[NSUserDefaults standardUserDefaults]setDouble:_sizeSlider.doubleValue forKey:DYBrowserPreference(DYBrowserSizePreference,_library)];
_sizing=YES;[self layoutBrowser];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshThumbnailSize) object:nil];
[self performSelector:@selector(refreshThumbnailSize) withObject:nil afterDelay:.22];
}
- (void)refreshThumbnailSize{
if(_closed)return;_sizing=NO;
_work.cancelled=YES;_work.owner=nil;[_work release];_work=[DYBrowserWork new];_work.owner=self;[_queuedPaths removeAllObjects];
for(DYBrowserTile *tile in _tiles.allValues){tile.thumbnail=nil;tile.failure=nil;tile.enabled=YES;[tile setNeedsDisplay:YES];}
[self layoutBrowser];
}
- (void)gridScrolled:(NSNotification *)notification{[self updateVisibleTiles];}
- (void)generateVisibleThumbnails{
if(_closed||!_work)return;
NSRect visible=NSInsetRect(_gridScroll.contentView.bounds,-30,-180);
BOOL hd=!_compactRecent&&_hd.state==NSControlStateValueOn;
BOOL useHigh=_previewFrames[@"high"]&&(hd||!_previewFrames[@"standard"]);
NSData *frame=_previewFrames[useHigh?@"high":@"standard"];
NSSize source=useHigh?[_previewFrames[@"highSize"] sizeValue]:[self previewSize];
NSSize box=DYBrowserTileImageRect(NSMakeRect(0,0,_tileWidth,_tileHeight),_layoutMenu.indexOfSelectedItem==1).size;
NSSize target=DYBrowserFitSize([self previewSize],box);
NSUInteger width=MAX(1,llround(target.width*(hd?2:1))),height=MAX(1,llround(target.height*(hd?2:1)));
for(NSString *path in _tiles.allKeys){
DYBrowserTile *tile=_tiles[path];
if(!NSIntersectsRect(tile.frame,visible)){if(tile.thumbnail){tile.thumbnail=nil;tile.enabled=NO;}continue;}
if(tile.thumbnail||tile.failure.length)continue;
NSString *cacheKey=[NSString stringWithFormat:@"%d|%lux%lu|%@",hd,(unsigned long)width,(unsigned long)height,path];
NSDictionary *cached=[_thumbnailCache objectForKey:cacheKey];
if(cached){tile.thumbnail=cached[@"image"];tile.failure=cached[@"error"];tile.enabled=tile.thumbnail!=nil;if(tile.failure.length)tile.toolTip=tile.failure;[tile setNeedsDisplay:YES];continue;}
if([_queuedPaths containsObject:cacheKey])continue;[_queuedPaths addObject:cacheKey];
DYBrowserWork *work=_work;
dispatch_async(_previewQueue,^{@autoreleasepool{
if(work.cancelled)return;
NSError *error=nil;NSImage *image=DYLUTThumbnail(path,frame,source.width,source.height,NSMakeSize(width,height),&error);NSString *failure=error.localizedDescription;
dispatch_async(dispatch_get_main_queue(),^{
DYLUTBrowser *owner=work.owner;if(work.cancelled||!owner)return;
[owner->_queuedPaths removeObject:cacheKey];
NSDictionary *result=image?@{@"image":image}:@{@"error":failure?:@"无法预览此 LUT"};
[owner->_thumbnailCache setObject:result forKey:cacheKey cost:image?width*height*4:128];
DYBrowserTile *current=owner->_tiles[path];if(!current||!NSIntersectsRect(current.frame,NSInsetRect(owner->_gridScroll.contentView.bounds,-30,-180)))return;
current.thumbnail=image;current.failure=failure;current.enabled=image!=nil;if(failure.length)current.toolTip=failure;[current setNeedsDisplay:YES];
});
}});
if(!NSIntersectsRect(tile.frame,visible)||tile.previewGeneration==_previewGeneration)continue;
NSDictionary *cached=[self previewResultForPath:path];
if(cached)[self displayPreviewResult:cached inTile:tile];
else [self requestPreviewForPath:path comparison:NO];
}
}
- (void)rebuildGrid{
@@ -724,7 +747,8 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
void (^applied)(NSError *)=^(NSError *error){
if(_closed||generation!=_selectionGeneration)return;[_selectionTask release];_selectionTask=nil;
if(error){[self setStatus:error.localizedDescription];return;}
[_selectedPath release];_selectedPath=[path copy];[_library recordUsedPath:path];[self refreshComparison];
[_selectedPath release];_selectedPath=[path copy];[_library recordUsedPath:path];
[_selectionFrameTime release];_selectionFrameTime=[_previewFrames[@"time"] retain];[self refreshComparison];
for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=[tile.identifier isEqualToString:path];[tile setNeedsDisplay:YES];}
[self setStatus:[@"已选择 " stringByAppendingString:name]];if(closeAfter)[self close];
};
@@ -736,7 +760,8 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
void (^cleared)(NSError *)=^(NSError *error){
if(_closed||generation!=_selectionGeneration)return;[_selectionTask release];_selectionTask=nil;
if(error){[self setStatus:error.localizedDescription];return;}
[_selectedPath release];_selectedPath=nil;[self refreshComparison];
[_selectedPath release];_selectedPath=nil;
[_selectionFrameTime release];_selectionFrameTime=[_previewFrames[@"time"] retain];[self refreshComparison];
for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=NO;[tile setNeedsDisplay:YES];}
[self setStatus:@"已移除 LUT"];
};
@@ -749,7 +774,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
NSProgress *task=[_library importURLsAsync:urls completion:^(NSArray *imported,NSError *error){
if(_closed||generation!=_libraryGeneration)return;[self endLibraryTask];
if(!imported){[self showImportError:error];return;}
[_thumbnailCache removeAllObjects];[self rebuildFolders];[self rebuildGrid];[self setStatus:[NSString stringWithFormat:@"已导入 %lu 个 LUT",(unsigned long)imported.count]];
[self invalidatePreviewImages];[self rebuildFolders];[self rebuildGrid];[self setStatus:[NSString stringWithFormat:@"已导入 %lu 个 LUT",(unsigned long)imported.count]];
}];[self beginLibraryTask:task];
}
- (void)importPanelForFolders:(BOOL)folders{