Skip to content

Commit a55641a

Browse files
committed
[vImage] Don't Infer Pixel Size in Buffer Copy
A buffer's `rowBytes` doesn't have a direct relationship with its `height`, so `rowBytes / Int(width)` isn't a good way to calculate pixel size. To resolve this, I've added `pixelSize` as a parameter to `vImage_Buffer.copy`.
1 parent c138770 commit a55641a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

stdlib/public/Darwin/Accelerate/vImage_Buffer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ extension vImage_Buffer {
205205
/// Copies this buffer to `destinationBuffer`.
206206
///
207207
/// - Parameter destinationBuffer: The destination vImage buffer.
208+
/// - Parameter pixelSize: The number of bytes for one pixel.
208209
/// - Parameter options: The options to use when performing this operation.
209210
public func copy(destinationBuffer: inout vImage_Buffer,
211+
pixelSize: Int,
210212
flags options: vImage.Options = .noFlags) throws {
211213

212214
if Int(width) == 0 {
@@ -218,7 +220,7 @@ extension vImage_Buffer {
218220
_ = withUnsafePointer(to: self) {
219221
error = vImageCopyBuffer($0,
220222
&destinationBuffer,
221-
rowBytes / Int(width),
223+
pixelSize,
222224
options.flags)
223225
}
224226

test/stdlib/Accelerate_vImage.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,17 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
269269
var destination = try! vImage_Buffer(width: 20, height: 20, bitsPerPixel: 32)
270270

271271
expectCrashLater()
272-
try! source.copy(destinationBuffer: &destination)
272+
try! source.copy(destinationBuffer: &destination,
273+
pixelSize: 4)
273274
}
274275

275276
Accelerate_vImageTests.test("vImage/CopyBadHeight") {
276277
var source = try! vImage_Buffer(width: 100, height: 10, bitsPerPixel: 32)
277278
var destination = try! vImage_Buffer(width: 20, height: 20, bitsPerPixel: 32)
278279

279280
expectCrashLater()
280-
try! source.copy(destinationBuffer: &destination)
281+
try! source.copy(destinationBuffer: &destination,
282+
pixelSize: 4)
281283
}
282284

283285
Accelerate_vImageTests.test("vImage/Copy") {
@@ -294,7 +296,8 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
294296
var destination = try! vImage_Buffer(width: widthi, height: heighti,
295297
bitsPerPixel: 32)
296298

297-
try! source.copy(destinationBuffer: &destination)
299+
try! source.copy(destinationBuffer: &destination,
300+
pixelSize: 4)
298301

299302
let sourcePixels: [UInt8] = arrayFromBuffer(buffer: source,
300303
count: pixels.count)
@@ -306,7 +309,7 @@ if #available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) {
306309
source.free()
307310
destination.free()
308311
}
309-
312+
310313
Accelerate_vImageTests.test("vImage/InitializeWithFormat") {
311314
let pixels: [UInt8] = (0 ..< width * height * 4).map { _ in
312315
return UInt8.random(in: 0 ..< 255)

0 commit comments

Comments
 (0)