文件
dayu-color/大鱼调色/Plugin/DYScopeSample.mm
T

58 行
2.4 KiB
Plaintext

#import "DYScopeSample.h"
#import "DYHistogram.h"
#import "../../ColorManagement/Sources/DYColorProfile.h"
#include <mutex>
@implementation DYScopeSample {
NSData *_canvasHistogram,*_spiderScope;
std::shared_ptr<const DYScopeReference> _reference;
std::mutex _referenceMutex;
}
- (NSData *)canvasHistogram{
@synchronized(self){
if(!_canvasHistogram){
CFStringRef name=_space?CGColorSpaceGetName(_space):nullptr;
bool linear709=name&&(CFEqual(name,kCGColorSpaceExtendedLinearSRGB)||CFEqual(name,kCGColorSpaceLinearSRGB));
_canvasHistogram=[DYHistogramFromRGBA(_pixels,true,linear709) retain];
}
return [[_canvasHistogram retain]autorelease];
}
}
- (std::shared_ptr<const DYScopeReference>)displayReferenceWithProfile:(DYColorProfile *)profile {
std::lock_guard<std::mutex> lock(_referenceMutex);
if(_reference)return _reference;
const auto *raw=(const dyscope::RGBA *)_pixels.bytes;
auto reference=std::make_shared<DYScopeReference>();
reference->pixels.resize(_pixels.length/sizeof(*raw));
for(size_t i=0;i<reference->pixels.size();i++){
const auto &p=raw[i];
if(!std::isfinite(p.r)||!std::isfinite(p.g)||!std::isfinite(p.b)||!std::isfinite(p.a)){
reference->invalid++;
continue;
}
if(p.a<=1e-6f)continue;
dyscope::RGBA straight={p.r/p.a,p.g/p.a,p.b/p.a,p.a};
if(dyscope::visible(straight))reference->pixels[i]=straight;
else reference->invalid++;
}
if(profile){
auto result=[profile convertPixels:reference->pixels alpha:dycm::Alpha::Straight
budget:32ull*1024*1024 cancelled:nullptr error:nil];
if(result){reference->pixels=std::move(*result);reference->converted=true;}
}
_reference=reference;
return _reference;
}
- (NSData *)spiderScopeWithProfile:(DYColorProfile *)profile {
@synchronized(self){if(_spiderScope)return [[_spiderScope retain]autorelease];}
auto reference=[self displayReferenceWithProfile:profile];
auto raster=dyscope::spiderVectorImage(reference->pixels);
NSData *data=[NSData dataWithBytes:raster.data() length:raster.size()];
@synchronized(self){
if(!_spiderScope)_spiderScope=[data retain];
return [[_spiderScope retain]autorelease];
}
}
- (void)setSpace:(CGColorSpaceRef)space{if(_space==space)return;if(_space)CGColorSpaceRelease(_space);_space=space?CGColorSpaceRetain(space):nil;}
- (void)dealloc{[_pixels release];[_metadata release];[_canvasHistogram release];[_spiderScope release];if(_space)CGColorSpaceRelease(_space);[super dealloc];}
@end