@interface DYTrackingPreviewImage : NSView - (void)showFrame:(NSDictionary *)frame; @end @implementation DYTrackingPreviewImage { NSDictionary *_frame; NSImage *_image; } - (instancetype)initWithFrame:(NSRect)frame { if((self=[super initWithFrame:frame]))self.clipsToBounds=YES; return self; } - (BOOL)isFlipped {return YES;} - (BOOL)isAccessibilityElement {return YES;} - (NSString *)accessibilityRole {return NSAccessibilityImageRole;} - (void)dealloc {[_frame release];[_image release];[super dealloc];} - (void)showFrame:(NSDictionary *)frame { [_frame release];_frame=[frame copy];[_image release];_image=nil; NSData *pixels=frame[@"rgba"]; NSUInteger w=[frame[@"width"] unsignedIntegerValue],h=[frame[@"height"] unsignedIntegerValue]; if(w&&h&&pixels.length==w*h*4){ CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)pixels); CGColorSpaceRef space=CGColorSpaceCreateWithName(kCGColorSpaceSRGB); CGImageRef cg=CGImageCreate(w,h,8,32,w*4,space,kCGImageAlphaNoneSkipLast,provider,nil,true,kCGRenderingIntentDefault); if(cg){_image=[[NSImage alloc]initWithCGImage:cg size:NSMakeSize(w,h)];CGImageRelease(cg);} CGColorSpaceRelease(space);CGDataProviderRelease(provider); } self.accessibilityLabel=_image?[NSString stringWithFormat:@"同步预览 · 源帧 %.3f 秒 · %@",[frame[@"time"] doubleValue], frame[@"sourceShape"]?@"取样与覆盖区域":@"蒙版轮廓"]:@"等待源帧"; [self setNeedsDisplay:YES]; } - (void)drawShape:(NSData *)shape nodes:(NSData *)nodes inRect:(NSRect)rect source:(BOOL)source { if(shape.length!=sizeof(DYMaskParameters))return; DYMaskParameters p;[shape getBytes:&p length:sizeof(p)]; double aspect=rect.size.width/rect.size.height,c=cos(p.angle),s=sin(p.angle); auto point=[&](double x,double y){ double px=x*p.width*aspect,py=y*p.height; return NSMakePoint(rect.origin.x+(p.centerX+(px*c-py*s)/aspect)*rect.size.width, rect.origin.y+(p.centerY+px*s+py*c)*rect.size.height); }; NSBezierPath *path=[NSBezierPath bezierPath]; auto add=[&](NSPoint position,int i){if(i)[path lineToPoint:position];else [path moveToPoint:position];}; if(p.kind==DYMaskBezier){ auto contour=dyFlattenMask((const DYBezierNode *)nodes.bytes,nodes.length/sizeof(DYBezierNode)); for(int i=0;i