Sunday, August 4, 2013

Using CoreImage filters in Openframeworks

Macintosh OSX CoreImage has some nice filter implementations, so it can be attractive to use some of these within Openframeworks. The Gaussian Blur filter, for example, is flexible and fast. So the trick is to get CoreImage to work with an Openframeworks OpenGL textures.

I implemented the following solution in Openframeworks v0.7.4, working on Mac OS version 10.7.5, Xcode 4.6.3. It involves drawing the image to a framebuffer and then passing the texture ID for the framebuffer to CoreImage.

Note that the filename of "testApp.cpp" needs to be changed to "" and "main.cpp" to "". This changes these files to an Objective-C++ type, which can handle both C++ and Objective-C calls.

Note also that (apparently) the QuartzCore framework needs to be manually added to the Xcode project frameworks list.

N.B.: I have found that on occasional runs of this and similar code that occasionally (1 out of every 4-8 runs), the code generates a CoreImage: ROI is not tilable error, and the CI Filter doesn't run, even if the project is configured at a humble screen size like 800x600. I haven't been able to find a solution online, so if you know the solution, I'm all ears.

The "testApp.h" file:

#pragma once
//  *****************************************************************
//  Demonstration of using Max OSX Core Image filters in an
//  Open Frameworks 0.7.4 project
//  Bret Battey /
//  August 4, 2013
//  *****************************************************************

#include "ofMain.h"
#include "QuartzCore/QuartzCore.h"  // One way to get the CI classes

class testApp : public ofBaseApp{

  void setup();
  void update();
  void draw();

  void keyPressed  (int key);
  void keyReleased(int key);
  void mouseMoved(int x, int y );
  void mouseDragged(int x, int y, int button);
  void mousePressed(int x, int y, int button);
  void mouseReleased(int x, int y, int button);
  void windowResized(int w, int h);
  void dragEvent(ofDragInfo dragInfo);
  void gotMessage(ofMessage msg);

    // Basics
    int     outWidth, outHeight;
    ofFbo   sourceFbo;

    // Core Image
    CGLContextObj   CGLContext;
    NSOpenGLPixelFormatAttribute*   attr;
    NSOpenGLPixelFormat*    pf;
    CGColorSpaceRef genericRGB;
    CIContext*  glCIcontext;
    CIImage*    inputCIImage;
    CIFilter*   blurFilter;
    CIImage*    blurredCIImage;
    CGSize      texSize;
    GLint       tex;
    CGRect      outRect;
    CGRect      inRect;


The "" file, up through the draw() call:

//  *****************************************************************
//  Demonstration of using Max OSX Core Image filters in an
//  Open Frameworks 0.7.4 project
//  Note that this has to be a .mm file (Objective-C++), since it
//  combines Objective-C and C++. >>> ALSO main.cpp needs to be changed
//  to
//  On Xcode, you must manually add the QuartzCore framework to the
//  Project / Target / Summary list of frameworks for the build to
//  succeed.
//  Bret Battey /
//  August 4, 2013
//  *****************************************************************

#include "testApp.h"

void testApp::setup(){

    outWidth  = ofGetViewportWidth();
    outHeight = ofGetViewportHeight();

    // Setup a framebuffer for the drawing. Perhaps there is some way to do this
    // without a framebuffer, but this is the only way I could figure out how to
    // enable grabbing an OpenGL texture to pass to the CoreImage filter
    sourceFbo.allocate(outWidth, outHeight, GL_RGBA32F_ARB); //32-bit framebuffer for smoothness
    // The appended .autorelease methods should auto cleanup the memory at exit.
    // Use a generic RGB color space:
    genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    // Create the pixel format attributes... The Core Image Guide for processing images says:
    // "It’s important that the pixel format for the context includes the NSOpenGLPFANoRecovery constant as an
    // attribute. Otherwise Core Image may not be able to create another context that shares textures with this one."
    NSOpenGLPixelFormatAttribute attr[] = {
        NSOpenGLPFAColorSize, 32,
    // Setup the pixel format object:
    pf=[[NSOpenGLPixelFormat alloc] initWithAttributes:attr].autorelease;
    // Setup the core image context, tied to the OF Open GL context:
    glCIcontext = [CIContext contextWithCGLContext: CGLGetCurrentContext()
                                       pixelFormat: CGLPixelFormatObj(pf)
                                        colorSpace: genericRGB
                                           options: nil].autorelease;
    // Setup a Gaussian Blur filter:
    blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"].autorelease;
    // Supporting stuff
    texSize = CGSizeMake(outWidth, outHeight);
    inRect = CGRectMake(0,0,outWidth,outHeight);
    outRect = CGRectMake(0,0,outWidth,outHeight);


void testApp::update(){
    // For feedback fun, let's not clear the Fbo after the first frame
    if(ofGetFrameNum()==1) {
    // Draw circle
    ofSetColor(20, 130, 250);
    // Get the texture ID of the fbo:
    tex = sourceFbo.getTextureReference().texData.textureID; 
    // set the CI Image to link with the Fbo texture
    inputCIImage = [CIImage imageWithTexture:tex
    // Blur filter
    [blurFilter setValue:inputCIImage forKey:@"inputImage"];
    [blurFilter setValue:[NSNumber numberWithFloat: 8] forKey:@"inputRadius"];
    blurredCIImage = [blurFilter valueForKey:@"outputImage"];
    // Draw it
    [glCIcontext drawImage:blurredCIImage

void testApp::draw(){



Bézier-Spline Control Curves in Max/MSP/Jitter

I have made my Max/MSP 6 abstractions for generating Bézier-spline curves available on the software section of my web site. The package includes a Bernstein-polynomial abstraction, a 3rd-order Bézier-spline generator, and a wrapper that expresses a constrained Bézier-spline over time in a form excellent for creating control signal nuance or for reshaping linear inputs to have ease-in and ease-out characteristics.