Skip to content

Instantly share code, notes, and snippets.

@oluseyi
Last active October 6, 2019 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oluseyi/ee31d6667353b75794d44b686d789d0e to your computer and use it in GitHub Desktop.
Save oluseyi/ee31d6667353b75794d44b686d789d0e to your computer and use it in GitHub Desktop.
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