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.

Answers


Okay, I figured out a (probably sub-optimal) solution. I decided to bridge to Objective-C (and used a slightly different function).

main.swift
import Foundation
func abs(x: matrix) -> matrix{
    let N = x.count
    var arg1 = NSArray(array: x)

    var yy = abs_objc(arg1, CInt(N))

    var y = zeros(N)
    for i in 0..N{
        y[i] = Double(yy[i])
    }
    return y
abs_objc.m
#import <Accelerate/Accelerate.h>
double* abs_objc(NSArray * x, int N){
    // converting input to double *
    double * xx = (double *)malloc(sizeof(double) * N);
    for (int i=0; i<[x count]; i++) {
        xx[i] = [[x objectAtIndex:i] doubleValue];
    }

    // init'ing output
    double * y = (double *)malloc(sizeof(double) * N);
    for (int i=0; i<N; i++){
        y[i] = 0;
    }

    vDSP_vabsD(xx,1,y,1,N);

    return y;
}
swix-Bridging-Header.h
#import <Foundation/Foundation.h>
double* abs_objc(NSArray * x, int N);

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))

}

I've been working with vDSP to just create a general matrix language.

I have found this way to be the easiest to get the correct pointer types.

Hopefully you find it useful (this might not be optimal either but a start).

import Accelerate
func myFunction(width:Float, height:Float) {

    var dstAsFloat = CFloat[](count: Int(width*height), repeatedValue: 0)
    var min:CFloat = 0.0
    var max:CFloat = 255.0

    func vclipPointerConversion(dstAsFloat:CMutablePointer<CFloat>) {
        vDSP_vclip(CConstPointer<CFloat>(nil,dstAsFloat.value), vDSP_Stride(1),
            &min, &max, dstAsFloat, vDSP_Stride(1), CUnsignedLong(width*height))
    }

    vclipPointerConversion(&dstAsFloat)

    //...
    // return whatever you wanted
}

Need Your Help

VS2015 Diagnostic with Code Fix - NuGet OR VSIX OR both?

c# .net roslyn visual-studio-2015

When using the .NET Compiler Platform SDK Templates (https://visualstudiogallery.msdn.microsoft.com/849f3ab1-05cf-4682-b4af-ef995e2aa1a5)

linq to sql where not in

c# linq-to-sql

I am trying find all the users that are not in a friends user friend list using linq to sql. Right now I am comparing friendslist to all the users in the system. Theres gotta be a better way of doing

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.