Skip to content

Commit 986d5ca

Browse files
authored
Merge pull request #24409 from FlexMonkey/accelerate-vImage-arrayFromBuffer
2 parents ed32972 + b93479c commit 986d5ca

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

test/stdlib/Accelerate_vImage.swift

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
6464
destination: &destinationCGBuffer)
6565

6666
let destinationPixels: [UInt8] = arrayFromBuffer(buffer: destinationCGBuffer,
67+
channelCount: 4,
6768
count: pixels.count)
6869

6970
expectEqual(destinationPixels, pixels)
@@ -187,8 +188,10 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
187188
// Compare results
188189

189190
let destinationPixels: [UInt8] = arrayFromBuffer(buffer: destinationBuffer,
191+
channelCount: 4,
190192
count: pixels.count)
191193
let legacyDestinationPixels: [UInt8] = arrayFromBuffer(buffer: legacyDestinationBuffer,
194+
channelCount: 4,
192195
count: pixels.count)
193196

194197
expectTrue(legacyDestinationPixels.elementsEqual(destinationPixels))
@@ -300,8 +303,10 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
300303
pixelSize: 4)
301304

302305
let sourcePixels: [UInt8] = arrayFromBuffer(buffer: source,
306+
channelCount: 4,
303307
count: pixels.count)
304308
let destinationPixels: [UInt8] = arrayFromBuffer(buffer: destination,
309+
channelCount: 4,
305310
count: pixels.count)
306311

307312
expectTrue(sourcePixels.elementsEqual(destinationPixels))
@@ -325,6 +330,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
325330
format: format)
326331

327332
let bufferPixels: [UInt8] = arrayFromBuffer(buffer: buffer,
333+
channelCount: 4,
328334
count: pixels.count)
329335

330336
expectTrue(bufferPixels.elementsEqual(pixels))
@@ -386,9 +392,11 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
386392
}()
387393

388394
let bufferPixels: [UInt8] = arrayFromBuffer(buffer: buffer,
395+
channelCount: 4,
389396
count: pixels.count)
390397

391398
let legacyBufferPixels: [UInt8] = arrayFromBuffer(buffer: legacyBuffer,
399+
channelCount: 4,
392400
count: pixels.count)
393401

394402
expectTrue(bufferPixels.elementsEqual(legacyBufferPixels))
@@ -550,7 +558,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
550558
expectTrue(vImageCGImageFormat_IsEqual(&format, &legacyFormat))
551559
expectTrue(format.componentCount == 4)
552560
}
553-
561+
554562
//===----------------------------------------------------------------------===//
555563
//
556564
// MARK: Helper Functions
@@ -569,10 +577,33 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
569577
return pixelBuffer
570578
}
571579

572-
func arrayFromBuffer<T>(buffer: vImage_Buffer, count: Int) -> Array<T> {
573-
let ptr = buffer.data.bindMemory(to: T.self, capacity: count)
574-
let buf = UnsafeBufferPointer(start: ptr, count: count)
575-
return Array(buf)
580+
func arrayFromBuffer<T>(buffer: vImage_Buffer,
581+
channelCount: Int,
582+
count: Int) -> Array<T> {
583+
584+
if (buffer.rowBytes == Int(buffer.width) * MemoryLayout<T>.stride * channelCount) {
585+
let ptr = buffer.data.bindMemory(to: T.self,
586+
capacity: count)
587+
588+
let buf = UnsafeBufferPointer(start: ptr, count: count)
589+
return Array(buf)
590+
} else {
591+
var returnArray = [T]()
592+
593+
let perRowCount = Int(buffer.width) * MemoryLayout<T>.stride * channelCount
594+
var ptr = buffer.data.bindMemory(to: T.self,
595+
capacity: perRowCount)
596+
597+
for _ in 0 ..< buffer.height {
598+
let buf = UnsafeBufferPointer(start: ptr, count: perRowCount)
599+
600+
returnArray.append(contentsOf: Array(buf))
601+
602+
ptr = ptr.advanced(by: buffer.rowBytes)
603+
}
604+
605+
return returnArray
606+
}
576607
}
577608

578609
func imageToPixels(image: CGImage) -> [UInt8] {

0 commit comments

Comments
 (0)