-
Notifications
You must be signed in to change notification settings - Fork 57
/
ImageCoreGraphics.swift
201 lines (160 loc) · 9.43 KB
/
ImageCoreGraphics.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#if os(iOS) || os(OSX) || os(watchOS) || os(tvOS)
import CoreGraphics
import Foundation
extension Image where Pixel == RGBA { // Initializers
public init(cgImage: CGImage) {
let width = cgImage.width
let height = cgImage.height
self.init(width: width, height: height, setUp: { context in
let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height))
context.draw(cgImage, in: rect)
})
}
fileprivate init(width: Int, height: Int, setUp: (CGContext) -> ()) {
let safeWidth = Swift.max(width, 0)
let safeHeight = Swift.max(height, 0)
let count = safeWidth * safeHeight
let defaultPixel = RGBA.transparent
var pixels = [RGBA](repeating: defaultPixel, count: count)
let context = CGContext(data: &pixels, width: safeWidth, height: safeHeight, bitsPerComponent: 8, bytesPerRow: safeWidth * 4, space: Image.colorSpace, bitmapInfo: Image.bitmapInfo.rawValue)!
context.clear(CGRect(x: 0.0, y: 0.0, width: CGFloat(safeWidth), height: CGFloat(safeHeight)))
setUp(context)
for i in 0..<count {
let pixel = pixels[i]
if pixel.alpha == 0 {
pixels[i] = defaultPixel
} else {
pixels[i] = RGBA(red: UInt8(255 * Int(pixel.red) / Int(pixel.alpha)), green: UInt8(255 * Int(pixel.green) / Int(pixel.alpha)), blue: UInt8(255 * Int(pixel.blue) / Int(pixel.alpha)), alpha: pixel.alpha)
}
}
let ps: [Pixel] = [Pixel](UnsafeBufferPointer<Pixel>(start: UnsafeMutablePointer<RGBA>(mutating: pixels)!.withMemoryRebound(to: Pixel.self, capacity: pixels.count) { $0 }, count: pixels.count))
self.init(width: safeWidth, height: safeHeight, pixels: ps)
}
}
extension Image where Pixel == UInt8 { // Initializers
public init(cgImage: CGImage) {
let width = cgImage.width
let height = cgImage.height
self.init(width: width, height: height, setUp: { context in
let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height))
context.draw(cgImage, in: rect)
})
}
fileprivate init(width: Int, height: Int, setUp: (CGContext) -> ()) {
let safeWidth = Swift.max(width, 0)
let safeHeight = Swift.max(height, 0)
let count = safeWidth * safeHeight
var pixels = [UInt8](repeating: 0, count: count)
let context = CGContext(data: &pixels, width: safeWidth, height: safeHeight, bitsPerComponent: 8, bytesPerRow: safeWidth, space: Image.colorSpace, bitmapInfo: Image.bitmapInfo.rawValue)!
context.clear(CGRect(x: 0.0, y: 0.0, width: CGFloat(safeWidth), height: CGFloat(safeHeight)))
setUp(context)
let ps: [Pixel] = [Pixel](UnsafeBufferPointer<Pixel>(start: UnsafeMutablePointer<UInt8>(mutating: pixels)!.withMemoryRebound(to: Pixel.self, capacity: pixels.count) { $0 }, count: pixels.count))
self.init(width: safeWidth, height: safeHeight, pixels: ps)
}
}
extension Image where Pixel == Float { // Initializers
public init(cgImage: CGImage) {
let width = cgImage.width
let height = cgImage.height
self.init(width: width, height: height, setUp: { context in
let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height))
context.draw(cgImage, in: rect)
})
}
fileprivate init(width: Int, height: Int, setUp: (CGContext) -> ()) {
let safeWidth = Swift.max(width, 0)
let safeHeight = Swift.max(height, 0)
let count = safeWidth * safeHeight
var pixels = [UInt8](repeating: 0, count: count)
let context = CGContext(data: &pixels, width: safeWidth, height: safeHeight, bitsPerComponent: 8, bytesPerRow: safeWidth, space: Image.colorSpace, bitmapInfo: Image.bitmapInfo.rawValue)!
context.clear(CGRect(x: 0.0, y: 0.0, width: CGFloat(safeWidth), height: CGFloat(safeHeight)))
setUp(context)
let ps: [Pixel] = [Pixel](UnsafeBufferPointer<Pixel>(start: UnsafeMutablePointer<Float>(mutating: pixels.map { Float($0) / 255.0 })!.withMemoryRebound(to: Pixel.self, capacity: pixels.count) { $0 }, count: pixels.count))
self.init(width: safeWidth, height: safeHeight, pixels: ps)
}
}
extension Image where Pixel == RGBA { // Conversion
public var cgImage: CGImage {
let length = count * 4
var data = Data(capacity: length)
data.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Void in
var pointer = bytes
for pixel in pixels {
let alphaInt = Int(pixel.alpha)
pointer.pointee = UInt8(pixel.redInt * alphaInt / 255)
pointer += 1
pointer.pointee = UInt8(pixel.greenInt * alphaInt / 255)
pointer += 1
pointer.pointee = UInt8(pixel.blueInt * alphaInt / 255)
pointer += 1
pointer.pointee = pixel.alpha
pointer += 1
}
}
let provider: CGDataProvider = CGDataProvider(data: data as CFData)!
return CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: width * 4, space: Image.colorSpace, bitmapInfo: Image.bitmapInfo, provider: provider, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent)!
}
fileprivate static var colorSpace: CGColorSpace {
return CGColorSpaceCreateDeviceRGB()
}
fileprivate static var bitmapInfo: CGBitmapInfo {
return CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue)
}
}
extension Image where Pixel == UInt8 { // Conversion
public var cgImage: CGImage {
let provider: CGDataProvider = CGDataProvider(data: Data(bytes: UnsafeMutableRawPointer(mutating: pixels), count: count) as CFData)!
return CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 8, bytesPerRow: width, space: Image.colorSpace, bitmapInfo: Image.bitmapInfo, provider: provider, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent)!
}
fileprivate static var colorSpace: CGColorSpace {
return CGColorSpaceCreateDeviceGray()
}
fileprivate static var bitmapInfo: CGBitmapInfo {
return CGBitmapInfo()
}
}
extension Image where Pixel == Float { // Conversion
public var cgImage: CGImage {
return (map { UInt8(Swift.min(Swift.max($0, 0.0), 1.0) * 255.0) }).cgImage
}
fileprivate static var colorSpace: CGColorSpace {
return CGColorSpaceCreateDeviceGray()
}
fileprivate static var bitmapInfo: CGBitmapInfo {
return CGBitmapInfo()
}
}
extension Image where Pixel == RGBA { // Resizing
public func resize(width: Int, height: Int) -> Image<Pixel> {
return resize(width: width, height: height, interpolationQuality: CGInterpolationQuality.default)
}
public func resize(width: Int, height: Int, interpolationQuality: CGInterpolationQuality) -> Image<Pixel> {
return Image(width: width, height: height) { context in
context.interpolationQuality = interpolationQuality
context.draw(self.cgImage, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height)))
}
}
}
extension Image where Pixel == UInt8 { // Resizing
public func resize(width: Int, height: Int) -> Image<Pixel> {
return resize(width: width, height: height, interpolationQuality: CGInterpolationQuality.default)
}
public func resize(width: Int, height: Int, interpolationQuality: CGInterpolationQuality) -> Image<Pixel> {
return Image(width: width, height: height) { context in
context.interpolationQuality = interpolationQuality
context.draw(self.cgImage, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height)))
}
}
}
extension Image where Pixel == Float { // Resizing
public func resize(width: Int, height: Int) -> Image<Pixel> {
return resize(width: width, height: height, interpolationQuality: CGInterpolationQuality.default)
}
public func resize(width: Int, height: Int, interpolationQuality: CGInterpolationQuality) -> Image<Pixel> {
return Image(width: width, height: height) { context in
context.interpolationQuality = interpolationQuality
context.draw(self.cgImage, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height)))
}
}
}
#endif