Skip to content

[Accelerate] [vImage] Swift Overlays #23592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
72950c3
Accelerate vImage Swift Overlays
FlexMonkey Mar 26, 2019
b8cfea4
Add new methods for generating CV -> CG and CG -> CV converters.
FlexMonkey Mar 27, 2019
5001900
Merge branch 'master' into accelerate-vImage
FlexMonkey Mar 27, 2019
37e9a0a
update comments: `height` and `width` to `size`.
FlexMonkey Mar 27, 2019
6a94692
`vImage_CGImageFormat.componentCount` should be `Int`.
FlexMonkey Apr 3, 2019
e9b67b5
`vImage_CGImageFormat.componentCount` should be `Int`
FlexMonkey Apr 3, 2019
40488b3
Move `self.init()` to after the size check for kvImageNoAllocate init.
FlexMonkey Apr 3, 2019
8389a98
Buffer initializers to width and height rather than size.
FlexMonkey Apr 3, 2019
f9e9434
change vImage_CGImageFormat lightweight initializer to accept Int for…
FlexMonkey Apr 3, 2019
992285e
Flesh out docs for vImage_Buffer.size
FlexMonkey Apr 3, 2019
09c454a
Remove faux initializer in favor of new static function: `preferredAl…
FlexMonkey Apr 3, 2019
3ef13f1
Merge branch 'master' into accelerate-vImage
Apr 4, 2019
bf16bbe
Change functions to use proper error handling rather than inout error…
Apr 4, 2019
3d9baaf
Removed `flags` from basic init.
Apr 4, 2019
ecd3676
Tests to check error throwing for buffer copy.
Apr 4, 2019
8175931
remove unnecessary import, add missing docs.
Apr 4, 2019
04c4ffd
Add comments to error enums.
Apr 4, 2019
4f6f636
Merge branch 'master' into accelerate-vImage
Apr 23, 2019
397ba35
Merge branch 'master' into accelerate-vImage
FlexMonkey Apr 23, 2019
69d2c5b
Fix bug creating string from format code.
FlexMonkey Apr 23, 2019
cec0293
Make `vImageCVImageFormat.formatCode` a `UInt32`.
Apr 24, 2019
21c0bf0
Remove equivalence operator from `CGImageFormat`.
Apr 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/public/Darwin/Accelerate/Accelerate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
//
//===----------------------------------------------------------------------===//

/// An enum that acts as a namespace for Swift overlays to vImage option sets and enums..
public enum vImage {}

/// An enum that acts as a namespace for Swift overlays to vDSP based functions.
public enum vDSP {}

Expand Down
6 changes: 6 additions & 0 deletions stdlib/public/Darwin/Accelerate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ include("../../../../cmake/modules/StandaloneOverlay.cmake")

add_swift_target_library(swiftAccelerate ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
Accelerate.swift
vImage_Error.swift
vImage_Options.swift
vImage_Buffer.swift
vImage_CVImageFormat.swift
vImage_CGImageFormat.swift
vImage_Converter.swift
AccelerateBuffer.swift
Quadrature.swift
vDSP_Arithmetic.swift
Expand Down
230 changes: 230 additions & 0 deletions stdlib/public/Darwin/Accelerate/vImage_Buffer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
//
// vImage_Buffer
//
//===----------------------------------------------------------------------===//

@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
extension vImage_Buffer {

/// The size of the vImage buffer.
///
/// The `CGSize` is rounded down to the nearest representable `CGFloat` that
/// is less than or equal to the actual size of the image. In practice the
/// conversion will always be exact, except for really big images. In that
/// case, some part of the bottom or right edge might be truncated.
public var size: CGSize {
var mutableSelf = self
return vImageBuffer_GetSize(&mutableSelf)
}

//===----------------------------------------------------------------------===//

/// Returns the preferred alignment and row bytes for a specified buffer
/// size and bits-per-pixel.
///
/// - Parameter width: The width of the buffer.
/// - Parameter height: The height of the buffer.
/// - Parameter bitsPerPixel: The number of bits in a pixel of image data.
///
/// - Returns: The preferred alignment and row bytes.
public static func preferredAlignmentAndRowBytes(width: Int,
height: Int,
bitsPerPixel: UInt32) throws -> (alignment: Int, rowBytes: Int) {

if width < 0 || height < 0 {
throw vImage.Error.invalidParameter
}

var buffer = vImage_Buffer()

let error = vImageBuffer_Init(&buffer,
vImagePixelCount(height),
vImagePixelCount(width),
bitsPerPixel,
vImage_Flags(kvImageNoAllocate))

if error < kvImageNoError {
throw vImage.Error(vImageError: error)
} else {
return(alignment: error,
rowBytes: buffer.rowBytes)
}
}

//===----------------------------------------------------------------------===//
//
// Initializers.
//
//===----------------------------------------------------------------------===//

/// Initializes a vImage buffer of a specified size.
///
/// - Parameter width: The width of the buffer.
/// - Parameter height: The height of the buffer.
/// - Parameter bitsPerPixel: The number of bits in a pixel of image data.
///
/// - Returns: An initialized vImage buffer.
public init(width: Int,
height: Int,
bitsPerPixel: UInt32) throws {

if width < 0 || height < 0 {
throw vImage.Error.invalidParameter
}

self.init()

let error = vImageBuffer_Init(&self,
vImagePixelCount(height),
vImagePixelCount(width),
bitsPerPixel,
vImage_Flags(kvImageNoFlags))

if error < kvImageNoError {
throw vImage.Error(vImageError: error)
}
}

public func free() {
Darwin.free(data)
}
}

// MARK: Core Graphics Support

@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
extension vImage_Buffer {

/// Initialize a vImage buffer with the contents of a Core Graphics image.
///
/// - Parameter cgImage: A `CGImage` instance to be used as the source.
/// - Parameter options: The options to use when performing this operation.
///
/// - Returns: An initialized vImage buffer.
///
/// This function will instantiate and initialize a vImage buffer from a `CGImage` using a `CGImageFormat` based on the provided image's properties.
public init(cgImage: CGImage,
flags options: vImage.Options = .noFlags) throws {

self.init()

guard var format = vImage_CGImageFormat(cgImage: cgImage) else {
throw vImage.Error.invalidImageFormat
}

let error = vImageBuffer_InitWithCGImage(&self,
&format,
nil,
cgImage,
options.flags)

if error != kvImageNoError {
throw vImage.Error(vImageError: error)
}
}

//===----------------------------------------------------------------------===//

/// Initialize a vImage buffer with the contents of a Core Graphics image,
/// using a supplied format.
///
/// - Parameter cgImage: A `CGImage` instance to be used as the source.
/// - Parameter format: A `vImage_CGImageFormat` that describes the source image/
/// - Parameter options: The options to use when performing this operation.
///
/// - Returns: An initialized vImage buffer.
///
/// This function will instantiate and initialize a vImage buffer from a `CGImage` using a provided `CGImageFormat`.
public init(cgImage: CGImage,
format: vImage_CGImageFormat,
flags options: vImage.Options = .noFlags) throws {

self.init()

var format = format
let error = vImageBuffer_InitWithCGImage(&self,
&format,
nil,
cgImage,
options.flags)

if error != kvImageNoError {
throw vImage.Error(vImageError: error)
}
}

//===----------------------------------------------------------------------===//

/// Creates a `CGImage` instance from a vImage buffer
///
/// - Parameter format: The image format of this vImage buffer.
/// - Parameter options: The options to use when performing this operation.
///
/// - Returns: A Core Graphics image containing a representation of the vImage buffer.
public func createCGImage(format: vImage_CGImageFormat,
flags options: vImage.Options = .noFlags) throws -> CGImage {
var format = format
var error = kvImageNoError

var cgImage: CGImage?

withUnsafePointer(to: self) {
cgImage = vImageCreateCGImageFromBuffer(
$0,
&format,
nil,
nil,
options.flags,
&error).takeRetainedValue()
}

if error != kvImageNoError {
throw vImage.Error(vImageError: error)
} else if cgImage == nil {
throw vImage.Error.internalError
}

return cgImage!
}

//===----------------------------------------------------------------------===//

/// Copies this buffer to `destinationBuffer`.
///
/// - Parameter destinationBuffer: The destination vImage buffer.
/// - Parameter options: The options to use when performing this operation.
public func copy(destinationBuffer: inout vImage_Buffer,
flags options: vImage.Options = .noFlags) throws {

if Int(width) == 0 {
throw vImage.Error(vImageError: kvImageInvalidParameter)
}

var error = kvImageNoError

_ = withUnsafePointer(to: self) {
error = vImageCopyBuffer($0,
&destinationBuffer,
rowBytes / Int(width),
options.flags)
}

if error != kvImageNoError {
throw vImage.Error(vImageError: error)
}
}

}
81 changes: 81 additions & 0 deletions stdlib/public/Darwin/Accelerate/vImage_CGImageFormat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
//
// vImage_CGImageFormat
//
//===----------------------------------------------------------------------===//

@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
extension vImage_CGImageFormat {

/// Initializes an image format from a Core Graphics image.
///
/// - Parameter cgImage: The image from which to derive the image format.
///
/// - Returns: An initialized `vImage_CGImageFormat`.
public init?(cgImage: CGImage) {
guard
let colorSpace = cgImage.colorSpace else {
return nil
}

self = vImage_CGImageFormat(
bitsPerComponent: UInt32(cgImage.bitsPerComponent),
bitsPerPixel: UInt32(cgImage.bitsPerPixel),
colorSpace: Unmanaged.passRetained(colorSpace),
bitmapInfo: cgImage.bitmapInfo,
version: 0,
decode: nil,
renderingIntent: cgImage.renderingIntent)
}

/// Initializes an image format.
///
/// - Parameter bitsPerComponent: The number of bits needed to represent one
/// channel of data in one pixel.
/// - Parameter bitsPerPixel: The number of bits needed to represent one pixel.
/// - Parameter colorSpace: The color space for the format.
/// - Parameter bitmapInfo: The component information describing the color channels.
/// - Parameter renderingIntent: A rendering intent constant that specifies how
/// Core Graphics should handle colors that are not located within the gamut of the
/// destination color space of a graphics context.
///
/// - Returns: An initialized `vImage_CGImageFormat`.
public init?(bitsPerComponent: Int,
bitsPerPixel: Int,
colorSpace: CGColorSpace,
bitmapInfo: CGBitmapInfo,
renderingIntent: CGColorRenderingIntent = .defaultIntent) {

if bitsPerComponent < 1 || bitsPerPixel < 0 {
return nil
}

self = vImage_CGImageFormat(
bitsPerComponent: UInt32(bitsPerComponent),
bitsPerPixel: UInt32(bitsPerPixel),
colorSpace: Unmanaged.passRetained(colorSpace),
bitmapInfo: bitmapInfo,
version: 0,
decode: nil,
renderingIntent: renderingIntent)
}

/// The number of color channels.
public var componentCount: Int {
var mutableSelf = self
return Int(vImageCGImageFormat_GetComponentCount(&mutableSelf))
}
}

Loading