Search code examples
iosswiftaccelerate

Fatal error when using withMemoryRebound in iOS/Swift


I have the following code to create a table for sampling an image in iOS using the Swift accelerate functions

When I rebound the memory to UInt16 which the table creation expects from Int which is the original type I get a fatal error.

var arr = Array<Float>(repeating: 0, count: 163840)

arr.withUnsafeBufferPointer{
    arr_pointer in do {
         arr_pointer.withMemoryRebound(to: UInt16.self){ // This causes a FATAL ERROR
             arr_r_pointer in do {
                 let table = vImageMultidimensionalTable_Create( arr_r_pointer.baseAddress!,
                            3, 3, dims_r_pointer.baseAddress!, kvImageMDTableHint_Float, 
                            vImage_Flags(kvImageNoFlags), nil )                          
                 vImageMultiDimensionalInterpolatedLookupTable_PlanarF( &srcBuffer,
                                       &destBuffer,nil,table!,
                                       kvImageFullInterpolation,
                                      vImage_Flags(kvImageNoFlags))
             }
        }
    }
}

Could anyone point out my mistake here?


Solution

  • Your original array arr is an array of Floats

    var arr = Array<Float>(repeating: 0, count: 163840)
    

    but you're trying to bind the pointer to a UInt16

    arr_pointer.withMemoryRebound(to: UInt16.self)