-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
stephentyrone
merged 22 commits into
swiftlang:master
from
FlexMonkey:accelerate-vImage
Apr 25, 2019
Merged
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 b8cfea4
Add new methods for generating CV -> CG and CG -> CV converters.
FlexMonkey 5001900
Merge branch 'master' into accelerate-vImage
FlexMonkey 37e9a0a
update comments: `height` and `width` to `size`.
FlexMonkey 6a94692
`vImage_CGImageFormat.componentCount` should be `Int`.
FlexMonkey e9b67b5
`vImage_CGImageFormat.componentCount` should be `Int`
FlexMonkey 40488b3
Move `self.init()` to after the size check for kvImageNoAllocate init.
FlexMonkey 8389a98
Buffer initializers to width and height rather than size.
FlexMonkey f9e9434
change vImage_CGImageFormat lightweight initializer to accept Int for…
FlexMonkey 992285e
Flesh out docs for vImage_Buffer.size
FlexMonkey 09c454a
Remove faux initializer in favor of new static function: `preferredAl…
FlexMonkey 3ef13f1
Merge branch 'master' into accelerate-vImage
bf16bbe
Change functions to use proper error handling rather than inout error…
3d9baaf
Removed `flags` from basic init.
ecd3676
Tests to check error throwing for buffer copy.
8175931
remove unnecessary import, add missing docs.
04c4ffd
Add comments to error enums.
4f6f636
Merge branch 'master' into accelerate-vImage
397ba35
Merge branch 'master' into accelerate-vImage
FlexMonkey 69d2c5b
Fix bug creating string from format code.
FlexMonkey cec0293
Make `vImageCVImageFormat.formatCode` a `UInt32`.
21c0bf0
Remove equivalence operator from `CGImageFormat`.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
FlexMonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if error != kvImageNoError { | ||
throw vImage.Error(vImageError: error) | ||
} | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
stdlib/public/Darwin/Accelerate/vImage_CGImageFormat.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
FlexMonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// 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)) | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.