|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +// |
| 15 | +// vImage_Buffer |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 20 | +extension vImage_Buffer { |
| 21 | + |
| 22 | + /// The size of the vImage buffer. |
| 23 | + /// |
| 24 | + /// The `CGSize` is rounded down to the nearest representable `CGFloat` that |
| 25 | + /// is less than or equal to the actual size of the image. In practice the |
| 26 | + /// conversion will always be exact, except for really big images. In that |
| 27 | + /// case, some part of the bottom or right edge might be truncated. |
| 28 | + public var size: CGSize { |
| 29 | + var mutableSelf = self |
| 30 | + return vImageBuffer_GetSize(&mutableSelf) |
| 31 | + } |
| 32 | + |
| 33 | + //===----------------------------------------------------------------------===// |
| 34 | + |
| 35 | + /// Returns the preferred alignment and row bytes for a specified buffer |
| 36 | + /// size and bits-per-pixel. |
| 37 | + /// |
| 38 | + /// - Parameter width: The width of the buffer. |
| 39 | + /// - Parameter height: The height of the buffer. |
| 40 | + /// - Parameter bitsPerPixel: The number of bits in a pixel of image data. |
| 41 | + /// |
| 42 | + /// - Returns: The preferred alignment and row bytes. |
| 43 | + public static func preferredAlignmentAndRowBytes(width: Int, |
| 44 | + height: Int, |
| 45 | + bitsPerPixel: UInt32) throws -> (alignment: Int, rowBytes: Int) { |
| 46 | + |
| 47 | + if width < 0 || height < 0 { |
| 48 | + throw vImage.Error.invalidParameter |
| 49 | + } |
| 50 | + |
| 51 | + var buffer = vImage_Buffer() |
| 52 | + |
| 53 | + let error = vImageBuffer_Init(&buffer, |
| 54 | + vImagePixelCount(height), |
| 55 | + vImagePixelCount(width), |
| 56 | + bitsPerPixel, |
| 57 | + vImage_Flags(kvImageNoAllocate)) |
| 58 | + |
| 59 | + if error < kvImageNoError { |
| 60 | + throw vImage.Error(vImageError: error) |
| 61 | + } else { |
| 62 | + return(alignment: error, |
| 63 | + rowBytes: buffer.rowBytes) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + //===----------------------------------------------------------------------===// |
| 68 | + // |
| 69 | + // Initializers. |
| 70 | + // |
| 71 | + //===----------------------------------------------------------------------===// |
| 72 | + |
| 73 | + /// Initializes a vImage buffer of a specified size. |
| 74 | + /// |
| 75 | + /// - Parameter width: The width of the buffer. |
| 76 | + /// - Parameter height: The height of the buffer. |
| 77 | + /// - Parameter bitsPerPixel: The number of bits in a pixel of image data. |
| 78 | + /// |
| 79 | + /// - Returns: An initialized vImage buffer. |
| 80 | + public init(width: Int, |
| 81 | + height: Int, |
| 82 | + bitsPerPixel: UInt32) throws { |
| 83 | + |
| 84 | + if width < 0 || height < 0 { |
| 85 | + throw vImage.Error.invalidParameter |
| 86 | + } |
| 87 | + |
| 88 | + self.init() |
| 89 | + |
| 90 | + let error = vImageBuffer_Init(&self, |
| 91 | + vImagePixelCount(height), |
| 92 | + vImagePixelCount(width), |
| 93 | + bitsPerPixel, |
| 94 | + vImage_Flags(kvImageNoFlags)) |
| 95 | + |
| 96 | + if error < kvImageNoError { |
| 97 | + throw vImage.Error(vImageError: error) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public func free() { |
| 102 | + Darwin.free(data) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// MARK: Core Graphics Support |
| 107 | + |
| 108 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 109 | +extension vImage_Buffer { |
| 110 | + |
| 111 | + /// Initialize a vImage buffer with the contents of a Core Graphics image. |
| 112 | + /// |
| 113 | + /// - Parameter cgImage: A `CGImage` instance to be used as the source. |
| 114 | + /// - Parameter options: The options to use when performing this operation. |
| 115 | + /// |
| 116 | + /// - Returns: An initialized vImage buffer. |
| 117 | + /// |
| 118 | + /// This function will instantiate and initialize a vImage buffer from a `CGImage` using a `CGImageFormat` based on the provided image's properties. |
| 119 | + public init(cgImage: CGImage, |
| 120 | + flags options: vImage.Options = .noFlags) throws { |
| 121 | + |
| 122 | + self.init() |
| 123 | + |
| 124 | + guard var format = vImage_CGImageFormat(cgImage: cgImage) else { |
| 125 | + throw vImage.Error.invalidImageFormat |
| 126 | + } |
| 127 | + |
| 128 | + let error = vImageBuffer_InitWithCGImage(&self, |
| 129 | + &format, |
| 130 | + nil, |
| 131 | + cgImage, |
| 132 | + options.flags) |
| 133 | + |
| 134 | + if error != kvImageNoError { |
| 135 | + throw vImage.Error(vImageError: error) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + //===----------------------------------------------------------------------===// |
| 140 | + |
| 141 | + /// Initialize a vImage buffer with the contents of a Core Graphics image, |
| 142 | + /// using a supplied format. |
| 143 | + /// |
| 144 | + /// - Parameter cgImage: A `CGImage` instance to be used as the source. |
| 145 | + /// - Parameter format: A `vImage_CGImageFormat` that describes the source image/ |
| 146 | + /// - Parameter options: The options to use when performing this operation. |
| 147 | + /// |
| 148 | + /// - Returns: An initialized vImage buffer. |
| 149 | + /// |
| 150 | + /// This function will instantiate and initialize a vImage buffer from a `CGImage` using a provided `CGImageFormat`. |
| 151 | + public init(cgImage: CGImage, |
| 152 | + format: vImage_CGImageFormat, |
| 153 | + flags options: vImage.Options = .noFlags) throws { |
| 154 | + |
| 155 | + self.init() |
| 156 | + |
| 157 | + var format = format |
| 158 | + let error = vImageBuffer_InitWithCGImage(&self, |
| 159 | + &format, |
| 160 | + nil, |
| 161 | + cgImage, |
| 162 | + options.flags) |
| 163 | + |
| 164 | + if error != kvImageNoError { |
| 165 | + throw vImage.Error(vImageError: error) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + //===----------------------------------------------------------------------===// |
| 170 | + |
| 171 | + /// Creates a `CGImage` instance from a vImage buffer |
| 172 | + /// |
| 173 | + /// - Parameter format: The image format of this vImage buffer. |
| 174 | + /// - Parameter options: The options to use when performing this operation. |
| 175 | + /// |
| 176 | + /// - Returns: A Core Graphics image containing a representation of the vImage buffer. |
| 177 | + public func createCGImage(format: vImage_CGImageFormat, |
| 178 | + flags options: vImage.Options = .noFlags) throws -> CGImage { |
| 179 | + var format = format |
| 180 | + var error = kvImageNoError |
| 181 | + |
| 182 | + var cgImage: CGImage? |
| 183 | + |
| 184 | + withUnsafePointer(to: self) { |
| 185 | + cgImage = vImageCreateCGImageFromBuffer( |
| 186 | + $0, |
| 187 | + &format, |
| 188 | + nil, |
| 189 | + nil, |
| 190 | + options.flags, |
| 191 | + &error).takeRetainedValue() |
| 192 | + } |
| 193 | + |
| 194 | + if error != kvImageNoError { |
| 195 | + throw vImage.Error(vImageError: error) |
| 196 | + } else if cgImage == nil { |
| 197 | + throw vImage.Error.internalError |
| 198 | + } |
| 199 | + |
| 200 | + return cgImage! |
| 201 | + } |
| 202 | + |
| 203 | + //===----------------------------------------------------------------------===// |
| 204 | + |
| 205 | + /// Copies this buffer to `destinationBuffer`. |
| 206 | + /// |
| 207 | + /// - Parameter destinationBuffer: The destination vImage buffer. |
| 208 | + /// - Parameter options: The options to use when performing this operation. |
| 209 | + public func copy(destinationBuffer: inout vImage_Buffer, |
| 210 | + flags options: vImage.Options = .noFlags) throws { |
| 211 | + |
| 212 | + if Int(width) == 0 { |
| 213 | + throw vImage.Error(vImageError: kvImageInvalidParameter) |
| 214 | + } |
| 215 | + |
| 216 | + var error = kvImageNoError |
| 217 | + |
| 218 | + _ = withUnsafePointer(to: self) { |
| 219 | + error = vImageCopyBuffer($0, |
| 220 | + &destinationBuffer, |
| 221 | + rowBytes / Int(width), |
| 222 | + options.flags) |
| 223 | + } |
| 224 | + |
| 225 | + if error != kvImageNoError { |
| 226 | + throw vImage.Error(vImageError: error) |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | +} |
0 commit comments