mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
* Accelerate vImage Swift Overlays A suite of functions, enumerations, and option sets to make working with vImage in Swift simpler. * vImage_Buffer - new initializers to instantiate and initialize buffers with a single function call. * vImage_Buffer - new functions to copy buffers and create CGImage instances from contents. * vImage_CGImageFormat - new initializers. * vImage_CGImageFormat - new equivalence operator. * vImageConverter - methods to wrap free functions. * vImageConverter - new make and convert functions. * vImageCVImageFormat - new make functions. * vImageCVImageFormat - methods to wrap free functions. * vImage_Error - errorDescription function. * vImage flags as an option set. * Add new methods for generating CV -> CG and CG -> CV converters. * update comments: `height` and `width` to `size`. * `vImage_CGImageFormat.componentCount` should be `Int`. * `vImage_CGImageFormat.componentCount` should be `Int` * Move `self.init()` to after the size check for kvImageNoAllocate init. * Buffer initializers to width and height rather than size. * change vImage_CGImageFormat lightweight initializer to accept Int for `bitsPerComponent` and `bitsPerPixel`. * Flesh out docs for vImage_Buffer.size * Remove faux initializer in favor of new static function: `preferredAlignmentAndRowBytes`. * Change functions to use proper error handling rather than inout error codes. * Removed `flags` from basic init. The only real flag to pass here is print diagnostics to console, and I now throw proper errors. * Tests to check error throwing for buffer copy. * remove unnecessary import, add missing docs. * Add comments to error enums. * Fix bug creating string from format code. * Make `vImageCVImageFormat.formatCode` a `UInt32`. * Remove equivalence operator from `CGImageFormat`.
241 lines
10 KiB
Swift
241 lines
10 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// vImageConverter
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
|
|
extension vImageConverter {
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Returns an array of vImage source buffer types specifying the order of planes.
|
|
///
|
|
/// - Parameter colorSpace: The color space of the source format.
|
|
public func sourceBuffers(colorSpace: CGColorSpace) -> [vImage.BufferType?] {
|
|
let sourceBuffers = vImageConverter_GetSourceBufferOrder(self)
|
|
let codes = Array(UnsafeBufferPointer(start: sourceBuffers,
|
|
count: sourceBufferCount))
|
|
|
|
return codes.map {
|
|
vImage.BufferType(bufferTypeCode: Int($0),
|
|
model: colorSpace.model)
|
|
}
|
|
}
|
|
|
|
/// Returns an array of vImage source buffer types specifying the order of planes.
|
|
///
|
|
/// - Parameter colorSpace: The color space of the destination format.
|
|
public func destinationBuffers(colorSpace: CGColorSpace) -> [vImage.BufferType?] {
|
|
let destinationBuffers = vImageConverter_GetDestinationBufferOrder(self)
|
|
let codes = Array(UnsafeBufferPointer(start: destinationBuffers,
|
|
count: destinationBufferCount))
|
|
|
|
return codes.map {
|
|
vImage.BufferType(bufferTypeCode: Int($0),
|
|
model: colorSpace.model)
|
|
}
|
|
}
|
|
|
|
/// The number of source buffers consumed by the converter.
|
|
public var sourceBufferCount: Int {
|
|
return Int(vImageConverter_GetNumberOfSourceBuffers(self))
|
|
}
|
|
|
|
/// The number of destination buffers written to by the converter.
|
|
public var destinationBufferCount: Int {
|
|
return Int(vImageConverter_GetNumberOfDestinationBuffers(self))
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Determines whether a converter is capable of operating in place.
|
|
///
|
|
/// - Parameter source: The source buffer.
|
|
/// - Parameter destination: The destination buffer.
|
|
/// - Parameter options: The options to use when performing this operation.
|
|
///
|
|
/// - Returns: `true` if the conversion must operate out of place or `false`
|
|
/// if the operation will work in place.
|
|
public func mustOperateOutOfPlace(source: vImage_Buffer,
|
|
destination: vImage_Buffer,
|
|
flags options: vImage.Options = .noFlags) throws -> Bool {
|
|
var error = kvImageNoError
|
|
|
|
withUnsafePointer(to: source) { src in
|
|
withUnsafePointer(to: destination) { dest in
|
|
error = vImageConverter_MustOperateOutOfPlace(self,
|
|
src,
|
|
dest,
|
|
vImage_Flags(options.rawValue))
|
|
}
|
|
}
|
|
|
|
switch error {
|
|
case kvImageOutOfPlaceOperationRequired:
|
|
return true
|
|
case kvImageNoError:
|
|
return false
|
|
default:
|
|
throw vImage.Error(vImageError: error)
|
|
}
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// MARK: CG -> CG
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Creates a vImage converter to convert from one vImage Core Graphics image format to another.
|
|
///
|
|
/// - Parameter sourceFormat: A `vImage_CGImageFormat` structure describing the image format of the source image
|
|
/// - Parameter destinationFormat: A `vImage_CGImageFormat` structure describing the image format of the destination image
|
|
/// - Parameter options: The options to use when performing this operation.
|
|
///
|
|
/// - Returns: a vImage converter to convert from one vImage Core Graphics image format to another.
|
|
public static func make(sourceFormat: vImage_CGImageFormat,
|
|
destinationFormat: vImage_CGImageFormat,
|
|
flags options: vImage.Options = .noFlags) throws -> vImageConverter {
|
|
|
|
var error = kvImageNoError
|
|
var unmanagedConverter: Unmanaged<vImageConverter>?
|
|
|
|
withUnsafePointer(to: destinationFormat) { dest in
|
|
withUnsafePointer(to: sourceFormat) { src in
|
|
unmanagedConverter = vImageConverter_CreateWithCGImageFormat(
|
|
src,
|
|
dest,
|
|
nil,
|
|
vImage_Flags(options.rawValue),
|
|
&error)
|
|
}
|
|
}
|
|
|
|
if error != kvImageNoError {
|
|
throw vImage.Error(vImageError: error)
|
|
} else if unmanagedConverter == nil {
|
|
throw vImage.Error.internalError
|
|
}
|
|
|
|
return unmanagedConverter!.takeRetainedValue()
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// MARK: CG -> CV
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Creates a vImage converter that converts a Core Graphics-formatted image to a Core Video-formatted image.
|
|
///
|
|
/// - Parameter sourceFormat: A `vImage_CGImageFormat` structure describing the image format of the source image
|
|
/// - Parameter destinationFormat: A `vImageCVImageFormat` structure describing the image format of the destination image
|
|
/// - Parameter options: The options to use when performing this operation.
|
|
///
|
|
/// - Returns: a vImage converter to convert a Core Graphics-formatted image to a Core Video-formatted image.
|
|
public static func make(sourceFormat: vImage_CGImageFormat,
|
|
destinationFormat: vImageCVImageFormat,
|
|
flags options: vImage.Options = .noFlags) throws -> vImageConverter {
|
|
|
|
var error = kvImageNoError
|
|
var unmanagedConverter: Unmanaged<vImageConverter>?
|
|
|
|
withUnsafePointer(to: sourceFormat) { src in
|
|
unmanagedConverter = vImageConverter_CreateForCGToCVImageFormat(
|
|
src,
|
|
destinationFormat,
|
|
nil,
|
|
vImage_Flags(options.rawValue),
|
|
&error)
|
|
}
|
|
|
|
if error != kvImageNoError {
|
|
throw vImage.Error(vImageError: error)
|
|
} else if unmanagedConverter == nil {
|
|
throw vImage.Error.internalError
|
|
}
|
|
|
|
return unmanagedConverter!.takeRetainedValue()
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// MARK: CV -> CG
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Creates a vImage converter that converts a Core Video-formatted image to a Core Graphics-formatted image.
|
|
///
|
|
/// - Parameter sourceFormat: A `vImageCVImageFormat` structure describing the image format of the source image
|
|
/// - Parameter destinationFormat: A `vImage_CGImageFormat` structure describing the image format of the destination image
|
|
/// - Parameter options: The options to use when performing this operation.
|
|
///
|
|
/// - Returns: a vImage converter to convert a Core Video-formatted image to a Core Graphics-formatted image.
|
|
public static func make(sourceFormat: vImageCVImageFormat,
|
|
destinationFormat: vImage_CGImageFormat,
|
|
flags options: vImage.Options = .noFlags) throws -> vImageConverter {
|
|
|
|
var error = kvImageInternalError
|
|
var unmanagedConverter: Unmanaged<vImageConverter>?
|
|
|
|
withUnsafePointer(to: destinationFormat) { dest in
|
|
unmanagedConverter = vImageConverter_CreateForCVToCGImageFormat(
|
|
sourceFormat,
|
|
dest,
|
|
nil,
|
|
vImage_Flags(options.rawValue),
|
|
&error)
|
|
}
|
|
|
|
if error != kvImageNoError {
|
|
throw vImage.Error(vImageError: error)
|
|
} else if unmanagedConverter == nil {
|
|
throw vImage.Error.internalError
|
|
}
|
|
|
|
return unmanagedConverter!.takeRetainedValue()
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Converts the pixels in a vImage buffer to another format.
|
|
///
|
|
/// - Parameter source: The source buffer.
|
|
/// - Parameter destination: The destination buffer.
|
|
/// - Parameter options: The options to use when performing this operation.
|
|
///
|
|
/// - Returns: `kvImageNoError`; otherwise, one of the error codes described in _Data Types and Constants.
|
|
public func convert(source: vImage_Buffer,
|
|
destination: inout vImage_Buffer,
|
|
flags options: vImage.Options = .noFlags) throws {
|
|
|
|
var error = kvImageNoError
|
|
|
|
_ = withUnsafePointer(to: source) { src in
|
|
error = vImageConvert_AnyToAny(self,
|
|
src,
|
|
&destination,
|
|
nil,
|
|
vImage_Flags(options.rawValue))
|
|
}
|
|
|
|
if error != kvImageNoError {
|
|
throw vImage.Error(vImageError: error)
|
|
}
|
|
}
|
|
}
|
|
|