Skip to content

Instantly share code, notes, and snippets.

@oluseyi oluseyi/RLCompositor.m
Last active Oct 6, 2019

Embed
What would you like to do?
Excerpt from circa-2015 implementation of early rendering compositor (no layers)
// Objective-C snippet circa 2015
CMTime frameTime = CMTimeMake(timescale / self.frameRate, timescale);
NSUInteger frameStride = imageFilePaths.count;
NSUInteger maxIndex = frameStride - 1;
NSUInteger loopStride = frameStride * ((self.mirrorSequence) ? 2 : 1);
NSMutableArray *pixelBuffers = [NSMutableArray arrayWithCapacity:imageFilePaths.count];
NSUInteger frameCount = loopStride * self.loopCount;
for (int i = 0; i < frameStride; ++i) {
NSString *imageFileName = imageFilePaths[i];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:
[rootDirectory stringByAppendingPathComponent:imageFileName]];
size_t bitsPerComponent = 8;
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGBitmapInfo bitmapInfo = (CGBitmapInfo)kCGImageAlphaNoneSkipFirst;
CVPixelBufferRef pixelBuffer = nil;
CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault,
adaptor.pixelBufferPool,
&pixelBuffer);
assert(pixelBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
void *pixelData = CVPixelBufferGetBaseAddress(pixelBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
CGContextRef cgContext = CGBitmapContextCreate(pixelData,
self.frameWidth,
self.frameHeight,
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
assert(cgContext);
NSGraphicsContext *gfxContext =
[NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:gfxContext];
[image drawInRect:NSMakeRect(0, 0, self.frameWidth, self.frameHeight)
fromRect:NSZeroRect
operation:NSCompositeCopy
fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
[pixelBuffers addObject:(__bridge id _Nonnull)(pixelBuffer)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.