@@ -64,6 +64,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
64
64
destination: & destinationCGBuffer)
65
65
66
66
let destinationPixels : [ UInt8 ] = arrayFromBuffer ( buffer: destinationCGBuffer,
67
+ channelCount: 4 ,
67
68
count: pixels. count)
68
69
69
70
expectEqual ( destinationPixels, pixels)
@@ -187,8 +188,10 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
187
188
// Compare results
188
189
189
190
let destinationPixels : [ UInt8 ] = arrayFromBuffer ( buffer: destinationBuffer,
191
+ channelCount: 4 ,
190
192
count: pixels. count)
191
193
let legacyDestinationPixels : [ UInt8 ] = arrayFromBuffer ( buffer: legacyDestinationBuffer,
194
+ channelCount: 4 ,
192
195
count: pixels. count)
193
196
194
197
expectTrue ( legacyDestinationPixels. elementsEqual ( destinationPixels) )
@@ -300,8 +303,10 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
300
303
pixelSize: 4 )
301
304
302
305
let sourcePixels : [ UInt8 ] = arrayFromBuffer ( buffer: source,
306
+ channelCount: 4 ,
303
307
count: pixels. count)
304
308
let destinationPixels : [ UInt8 ] = arrayFromBuffer ( buffer: destination,
309
+ channelCount: 4 ,
305
310
count: pixels. count)
306
311
307
312
expectTrue ( sourcePixels. elementsEqual ( destinationPixels) )
@@ -325,6 +330,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
325
330
format: format)
326
331
327
332
let bufferPixels : [ UInt8 ] = arrayFromBuffer ( buffer: buffer,
333
+ channelCount: 4 ,
328
334
count: pixels. count)
329
335
330
336
expectTrue ( bufferPixels. elementsEqual ( pixels) )
@@ -386,9 +392,11 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
386
392
} ( )
387
393
388
394
let bufferPixels : [ UInt8 ] = arrayFromBuffer ( buffer: buffer,
395
+ channelCount: 4 ,
389
396
count: pixels. count)
390
397
391
398
let legacyBufferPixels : [ UInt8 ] = arrayFromBuffer ( buffer: legacyBuffer,
399
+ channelCount: 4 ,
392
400
count: pixels. count)
393
401
394
402
expectTrue ( bufferPixels. elementsEqual ( legacyBufferPixels) )
@@ -550,7 +558,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
550
558
expectTrue ( vImageCGImageFormat_IsEqual ( & format, & legacyFormat) )
551
559
expectTrue ( format. componentCount == 4 )
552
560
}
553
-
561
+
554
562
//===----------------------------------------------------------------------===//
555
563
//
556
564
// MARK: Helper Functions
@@ -569,10 +577,33 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
569
577
return pixelBuffer
570
578
}
571
579
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
+ }
576
607
}
577
608
578
609
func imageToPixels( image: CGImage ) -> [ UInt8 ] {
0 commit comments