I'm using an external library written in C that applies filters to an image. It receives the original image pixels as an array of float values and writes the new image float values on another array. One specific filter creates a mask to be used by a sharpen filter, and I don't know why but it only works with smaller images, while bigger images (a million pixels more or less) cause the application to crash, giving an EXC_BAD_ACCESS error right after executing the wrapper that calls the external lib function. Is there anything wrong with my code, which creates the parameters that will be passed to the external lib, or is the problem likely in the external library?
func allocateMaskArgs() { //method to allocate mask parameters in memory, to be used by sharpen filter
let size = originalImageMatrix.params[0] * originalImageMatrix.params[1] //height of the image multiplied by width
if maskBuffer != nil {
self.maskBuffer.deallocate()
}
maskBuffer = UnsafeMutablePointer<UnsafeMutablePointer<Float>?>.allocate(capacity: 2)
let constantPointer: UnsafeMutablePointer<Float>?
constantPointer = UnsafeMutablePointer<Float>.allocate(capacity: 1)
constantPointer!.advanced(by: 0).pointee = 4.0 //this is the intensity value of the mask, it should always be 4
maskBuffer.advanced(by: 0).pointee = constantPointer
let maskArrayPointer: UnsafeMutablePointer<Float>? //this is where the mask created by createMask() should be stored by the external lib function
maskArrayPointer = UnsafeMutablePointer<Float>.allocate(capacity: size)
maskBuffer.advanced(by: 1).pointee = maskArrayPointer
}
func createMask() { //creates sharpen mask and stores in maskBuffer
var input_params: [Int] = [self.originalImageMatrix.params[0], self.originalImageMatrix.params[1]]
var output_params: [Int] = [self.newImageMatrix.params[0], self.newImageMatrix.params[1]]
self.imagingAPI.applyFilters(self.originalImageMatrix.v!, input_params: &input_params, output_image: self.newImageMatrix.v!, output_params: &output_params, filter_id: 11, args: self.maskBuffer)
}
The external library function is accessed through this wrapper function:
- (void) applyFilters: (float *) input_image input_params: (long *) input_params output_image : (float *) output_image output_params : (long *) output_params filter_id : (int) filter_id args : (float**) args;