Files
swift-mirror/stdlib/public/Darwin/Accelerate/vImage_CGImageFormat.swift
simon gladman 5936a04510 [Accelerate] [vImage] Swift Overlays (#23592)
* 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`.
2019-04-25 08:40:45 -04:00

82 lines
3.0 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
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
//
// 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))
}
}