Last active
October 21, 2021 18:10
-
-
Save chockenberry/30608eae03f63750363fc1470991f166 to your computer and use it in GitHub Desktop.
vImage_Buffer Unsafe Mutable Pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let srcRowBytes = workingBuffer.buffer.rowBytes | |
let srcData: UnsafeMutablePointer<Pixel_8> = workingBuffer.buffer.data!.assumingMemoryBound(to: Pixel_8.self) | |
let srcIndex = y * srcRowBytes + x * workingBuffer.bytesPerPixel | |
var srcPointer = srcData[srcIndex] | |
withUnsafeMutablePointer(to: &srcPointer) { srcPointer in | |
// srcPointer is (UnsafeMutablePointer<Pixel_8>) | |
var srcBuffer = vImage_Buffer(data: srcPointer, height: vImagePixelCount(h), width: vImagePixelCount(w), rowBytes: srcRowBytes) | |
... | |
var error = kvImageNoError | |
// EXC_BAD_ACCESS because srcPointer is not srcData + srcIndex | |
error = vImageConvert_AnyToAny(converter, &srcBuffer, &dstBuffer, nil, vImage_Flags(kvImagePrintDiagnosticsToConsole)) | |
} |
For anyone who stumbles across this gist with search, the fix is to create the vImage_Buffer like this:
var srcBuffer = vImage_Buffer(data: srcData.advanced(by: srcIndex), height: vImagePixelCount(h), width: vImagePixelCount(w), rowBytes: srcRowBytes)
For more help, check out these links:
Apple documentation: https://developer.apple.com/documentation/accelerate/applying_vimage_operations_to_regions_of_interest
Twitter thread: https://twitter.com/chockenberry/status/1450885687369101313
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Previously, this code was:
But there's an inout warning about the &srcData temporary pointer: