Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Applying a Filter to Video

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:

  1. 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;
    }
  2. 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.

  3. Set the filter input parameters, except for the input image.

  4. 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];
            }
    }
  5. In your dealloc method, make sure you release the filter.

The following sample applications apply Core Image filters to video:

You can download these and other sample applications from the ADC Reference Library (Sample Code > Graphics & Imaging > Quartz).



< Previous PageNext Page > Hide TOC


Last updated: 2008-06-09




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice