Core Image and Core Video can work together to achieve a variety of effects. For example, you can use a color correction filter on a video shot under water to correct for the fact that water absorbs red light faster than green and blue light. There are many more ways you can use these technologies together.
Follow these steps to apply a Core Image filter to a video displayed using Core Video:
When you subclass NSView to create a view for the video, declare a CIFilter object in the interface, similar to what’s shown in this code:
@interface MyVideoView : NSView |
{ |
NSRecursiveLock *lock; |
QTMovie *qtMovie; |
QTVisualContextRef qtVisualContext; |
CVDisplayLinkRef displayLink; |
CVImageBufferRef currentFrame; |
CIFilter *effectFilter; |
id delegate; |
} |
When you initialize the view with a frame, you create a CIFilter object for the filter and set the default values using code similar to the following:
effectFilter = [[CIFilter filterWithName:@"CILineScreen"] retain]; |
[effectFilter setDefaults]; |
This example uses the Core Image filter CILineScreen, but you’d use whatever is appropriate for your application.
Set the filter input parameters, except for the input image.
Each time you render a frame, you need to set the input image and draw the output image. Your renderCurrentFrame routine would look similar to the following. Note that this example, to avoid interpolation, uses integer coordinates when it draws the output.
- (void)renderCurrentFrame |
{ |
NSRect frame = [self frame]; |
if(currentFrame) |
{ |
CGRect imageRect; |
CIImage *inputImage, *outputImage; |
inputImage = [CIImage imageWithCVImageBuffer:currentFrame]; |
imageRect = [inputImage extent]; |
[effectFilter setValue:inputImage forKey:@"inputImage"]; |
[[[NSGraphicsContext currentContext] CIContext] |
drawImage:[effectFilter valueForKey:@"outputImage"] |
atPoint:CGPointMake( |
(int)((frame.size.width - imageRect.size.width) * 0.5), |
(int)((frame.size.height - imageRect.size.height) * 0.5)) |
fromRect:imageRect]; |
} |
} |
In your dealloc method, make sure you release the filter.
The following sample applications apply Core Image filters to video:
CIVideoDemoGL demonstrates using Core Image with QuickTime through Core Video.
QTCoreImage101 is a Cocoa application that demonstrates how to render a QuickTime Movie using Core Image filters, Core Video, and Visual Contexts.
You can download these and other sample applications from the ADC Reference Library (Sample Code > Graphics & Imaging > Quartz).
Last updated: 2008-06-09