<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/vImage/PixelBuffer/init(data:width:height:byteCountPerRow:pixelFormat:)-27czc",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate6vImageO11PixelBufferVA2A06StaticC6FormatRzrlE4data5width6height15byteCountPerRow05pixelF0AEy_xGSv_S3iSgxmtcfc"
  },
  "title" : "init(data:width:height:byteCountPerRow:pixelFormat:)"
}
-->

# init(data:width:height:byteCountPerRow:pixelFormat:)

Calculates the correct bytes per row and returns a new buffer that references existing data.

```
init(data: UnsafeMutableRawPointer, width: Int, height: Int, byteCountPerRow: Int? = nil, pixelFormat: Format.Type = Format.self)
```

## Parameters

`data`

A pointer to the top-left pixel of the buffer.

`width`

The width, in pixels, of the pixel buffer.

`height`

The height, in pixels, of the pixel buffer.

`byteCountPerRow`

The number of bytes in a pixel row.

`pixelFormat`

The pixel format of the initialized buffer.

## Discussion

Pass `nil` to `byteCountPerRow` to have the function calculate the bytes per row as `width * MemoryLayout<T.ComponentType>.stride * T.channelCount`.

For example, the following code creates a 32-bit planar pixel buffer:

```swift
 let data = UnsafeMutableBufferPointer<Float>.allocate(capacity: 10)
 _ = data.initialize(from: [0, 1, 2, 3, 4,
                            5, 6, 7, 8, 9] as [Float])
 defer {
     data.deallocate()
 }

 let src = vImage.PixelBuffer(data: data.baseAddress!,
                              width: 5,
                              height: 2,
                              byteCountPerRow: nil,
                              pixelFormat: vImage.PlanarF.self)

 // Prints "[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]".
 print(src.array)
```

> Important:
> The pixel buffer is valid only for the lifetime of `data`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)