Search code examples
iosswiftaccelerate-framework

Casting parameters to make Swift compile with vDSP API


I am running into some issues trying to use the Accelerate framework with vDSP API from Swift. Obviously I am doing something wrong although the compiler gives me all sorts of warnings

var srcAsFloat:CConstPointer<CFloat> = CFloat[](count: Int(width*height), repeatedValue: 0)
var dstAsFloat = CFloat[](count: Int(width*height), repeatedValue: 0)

if shouldClip {
    var min:CFloat = 0.0
    var max:CFloat = 255.0
    var l:vDSP_Stride = Int(width*height)
    vDSP_vclip(CConstPointer<CFloat>(&dstAsFloat), vDSP_Stride(1), CConstPointer<CFloat>(&min), CConstPointer<CFloat>(&max), CMutablePointer<CFloat>(&dstAsFloat), vDSP_Stride(1), l)
}

The error:

error: could not find an overload for 'init' that accepts the supplied arguments 
    vDSP_vclip(CConstPointer<CFloat>(&dstAsFloat), 
    vDSP_Stride(1), 
    CConstPointer<CFloat>(&min), 
    CConstPointer<CFloat>(&max), 
    CMutablePointer<CFloat>(&dstAsFloat), 
    vDSP_Stride(1), 
    l) – 

I've tried to cast the heck out of it but so far, no luck.


Solution

  • I got it to work with a few tweaks. Notice the first param doesn't have an ampersand (CConstPointer). However the second one does (I use the same pointer for src & dst). I also replaced the casting (necessary) of values. You need to use CFloats for ceiling values. Here is the code:

    let width: UInt = CGBitmapContextGetWidth(context)
    let height: UInt = CGBitmapContextGetHeight(context)
    
    var dstAsFloat = CFloat[](count: Int(width*height), repeatedValue: 0)
    
    if shouldClip {
        var min:CFloat = 0.0
        var max:CFloat = 255.0
        vDSP_vclip(dstAsFloat, CLong(1), &min, &max, &dstAsFloat, CLong(1), UInt(width*height))
    
    }