Skip to content

Commit

Permalink
Added Surface.Image.from(data:)
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed May 3, 2019
1 parent 111e0e0 commit b5f867a
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions Sources/Cairo/SurfaceImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,24 @@ public extension Surface {
try super.init(internalPointer)
}

/// Creates an image surface for the provided pixel data, copying the buffer.
public convenience init(data: Data, format: ImageFormat, width: Int, height: Int, stride: Int) throws {

var data = data

let pointer = data.withUnsafeMutableBytes { $0.baseAddress!.assumingMemoryBound(to: UInt8.self) }

try self.init(mutableBytes: pointer,
format: format,
width: width,
height: height,
stride: stride)
/// Creates an image surface for the provided pixel data.
static func from <Result> (data: inout Data,
format: ImageFormat,
width: Int,
height: Int,
stride: Int,
body: (Surface.Image) throws -> Result) throws -> Result {

return try data.withUnsafeMutableBytes {
let surface = try Surface.Image(
mutableBytes: $0.baseAddress!.assumingMemoryBound(to: UInt8.self),
format: format,
width: width,
height: height,
stride: stride
)
return try body(surface)
}
}

/// For internal use with extensions (e.g. `init(png:)`)
Expand All @@ -66,12 +72,7 @@ public extension Surface {
// MARK: - Class Methods

public override class func isCompatible(with surfaceType: SurfaceType) -> Bool {

switch surfaceType {

case .image: return true
default: return false
}
return surfaceType == .image
}

// MARK: - Accessors
Expand Down

0 comments on commit b5f867a

Please sign in to comment.