/
EditingCrop.swift
370 lines (295 loc) · 10 KB
/
EditingCrop.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// Copyright (c) 2018 Muukii <muukii.app@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
import Vision
import SwiftUI
/// A representation of cropping extent in Image.
public struct EditingCrop: Equatable {
public enum Rotation: Equatable, CaseIterable {
/// 0 degree - default
case angle_0
/// 90 degree
case angle_90
/// 180 degree
case angle_180
/// 270 degree
case angle_270
public var angle: AdjustmentAngle {
switch self {
case .angle_0:
return .degrees(0)
case .angle_90:
return .degrees(-90)
case .angle_180:
return .degrees(-180)
case .angle_270:
return .degrees(-270)
}
}
public var transform: CGAffineTransform {
.init(rotationAngle: angle.radians)
}
public func next() -> Self {
switch self {
case .angle_0: return .angle_90
case .angle_90: return .angle_180
case .angle_180: return .angle_270
case .angle_270: return .angle_0
}
}
}
public typealias AdjustmentAngle = SwiftUI.Angle
/// The dimensions in pixel for the image.
/// Applied image-orientation.
public var imageSize: CGSize
/// The rectangle that specifies the extent of the cropping.
public private(set) var cropExtent: CGRect
/// The angle that specifies rotation for the image.
public var rotation: Rotation = .angle_0
/// An angle to rotate in addition to the specified rotation.
public var adjustmentAngle: AdjustmentAngle = .zero
public var aggregatedRotation: AdjustmentAngle {
rotation.angle + adjustmentAngle
}
public private(set) var scaleToRestore: CGFloat
public init(from ciImage: CIImage) {
self.init(
imageSize: .init(image: ciImage),
cropRect: .init(origin: .zero, size: ciImage.extent.size)
)
}
public init(imageSize: CGSize) {
self.init(
imageSize: imageSize,
cropRect: .init(origin: .zero, size: imageSize),
rotation: .angle_0
)
}
public init(
imageSize: CGSize,
cropRect: CGRect,
rotation: Rotation = .angle_0,
scaleToRestore: CGFloat = 1
) {
self.imageSize = imageSize
self.cropExtent = Self.fittingRect(rect: cropRect, in: imageSize, respectingAspectRatio: nil)
self.rotation = rotation
self.scaleToRestore = scaleToRestore
}
public func makeInitial() -> Self {
.init(
imageSize: imageSize,
cropRect: .init(origin: .zero, size: imageSize),
scaleToRestore: scaleToRestore
)
}
public func scaledWithPixelPerfect(maxPixelSize: CGFloat) -> Self {
let scaledImageSize = imageSize.scaled(maxPixelSize: maxPixelSize)
let scale = scaledImageSize.width / imageSize.width
var new = scaled(scale)
new.imageSize = scaledImageSize
new.scaleToRestore = imageSize.width / scaledImageSize.width
return new
}
private consuming func scaled(_ scale: CGFloat) -> Self {
var modified = self
var cropExtent = modified.cropExtent
var imageSize = modified.imageSize
cropExtent.origin.x *= scale
cropExtent.origin.y *= scale
cropExtent.size.width *= scale
cropExtent.size.height *= scale
imageSize.width *= scale
imageSize.height *= scale
modified.cropExtent = Self.fittingRect(
rect: cropExtent,
in: imageSize,
respectingAspectRatio: nil
)
modified.imageSize = imageSize
return modified
}
/**
Set new aspect ratio with updating cropping extent.
Currently, the cropping extent changes to maximum size in the size of image.
- TODO: Resizing cropping extent with keeping area by new aspect ratio.
*/
public mutating func updateCropExtent(toFitAspectRatio newAspectRatio: PixelAspectRatio) {
let maxSize = newAspectRatio.sizeThatFits(in: imageSize)
let proposed = CGRect(
origin: .init(
x: (imageSize.width - maxSize.width) / 2,
y: (imageSize.height - maxSize.height) / 2
),
size: maxSize
)
self.cropExtent = Self.fittingRect(
rect: proposed,
in: imageSize,
respectingAspectRatio: newAspectRatio
)
}
/**
(Won't do mutating, If current aspect ratio is the same with specified aspect ratio.)
Set new aspect ratio with updating cropping extent.
Currently, the cropping extent changes to maximum size in the size of image.
*/
public mutating func updateCropExtentIfNeeded(toFitAspectRatio newAspectRatio: PixelAspectRatio) {
// FIXME: it won't perform correctly. won't match values as using floating point value
/*
Depends on the size of image, it can not always express the exact value of given aspect-ratio.
- image-size: (7864.0, 5248.0)
- aspect ratio: 1.0:1.2 (0.8333333333)
- calculated fitting image size with ratio: (1745.0, 0.0, 4373.0, 5247.0)
- (4373.0 : 5247.0) -> 0.8334286259
*/
guard PixelAspectRatio(cropExtent.size) != newAspectRatio else {
return
}
updateCropExtent(toFitAspectRatio: newAspectRatio)
}
/**
Updates the crop extent to fit bounding box that comes from Vision.framework.
*/
public mutating func updateCropExtent(
toFitBoundingBox boundingBox: CGRect,
respectingApectRatio: PixelAspectRatio?
) {
var proposed = cropExtent
let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -proposed.height)
let scale = CGAffineTransform.identity.scaledBy(x: proposed.width, y: proposed.height)
proposed =
boundingBox
.applying(scale)
.applying(transform)
self.cropExtent = Self.fittingRect(
rect: proposed,
in: imageSize,
respectingAspectRatio: respectingApectRatio
)
}
/// Updates cropExtent with new specified rect and normalizing value using aspectRatio(optional).
/// cropExtent would be rounded in order to drop floating point value for fitting pixel.
/// With specifing `respectingAspectRatio`, it fixes cropExtent's size.
///
/// - Parameters:
/// - cropExtent:
/// - respectingAspectRatio:
public mutating func updateCropExtent(
_ cropExtent: CGRect,
respectingAspectRatio: PixelAspectRatio?
) {
// self.cropExtent = Self.fittingRect(
// rect: cropExtent,
// in: imageSize,
// respectingAspectRatio: respectingAspectRatio
// )
self.cropExtent = cropExtent
}
private static func fittingRect(
rect: CGRect,
in imageSize: CGSize,
respectingAspectRatio: PixelAspectRatio?
) -> CGRect {
var fixed = rect
func clamp<T: Comparable>(value: T, lower: T, upper: T) -> T {
return min(max(value, lower), upper)
}
/*
Cuts the area off that out of maximum bounds
image-size
┌────────────┐
│ │
│ │ crop extent
│ ┌───────┼───┐
│ │xxxxxxx│ │
│ │xxxxxxx│ │
│ │xxxxxxx│ │
│ │xxxxxxx│ │
└────┼───────┘ │
│ │
└───────────┘
*/
fixed = CGRect(origin: .zero, size: imageSize).intersection(fixed)
respectAspectRatio: do {
/*
Fits the fixed rect to aspect ratio if present.
*/
if let aspectRatio = respectingAspectRatio {
/*
Find maximum bounds to create a new rect inside.
*/
let maxSizeFromPoint = CGSize(
width: imageSize.width - fixed.minX,
height: imageSize.height - fixed.minY
)
let maxRect = CGRect(
origin: fixed.origin,
size: .init(
width: clamp(value: fixed.width, lower: 0, upper: maxSizeFromPoint.width),
height: clamp(value: fixed.height, lower: 0, upper: maxSizeFromPoint.height)
)
)
let newRect = aspectRatio.rectThatFits(in: maxRect)
fixed = newRect
}
}
validation: do {
assert(fixed.maxX <= imageSize.width)
assert(fixed.maxY <= imageSize.height)
assert(fixed.origin.x >= 0)
assert(fixed.origin.y >= 0)
assert(fixed.width <= imageSize.width)
assert(fixed.height <= imageSize.height)
}
#if DEBUG
EngineLog.debug(
"""
[Normalizing CropExtent]
Output: \(fixed)
- resultAspectRatio: \(PixelAspectRatio(fixed.size)._minimized().localizedText)
- source: \(rect)
- imageSize: \(imageSize)
- respectingApectRatio: \(respectingAspectRatio.map { "\($0.width):\($0.height)" } ?? "null")
"""
)
#endif
return fixed
}
/*
@objc
public func debugQuickLookObject() -> AnyObject? {
let path = UIBezierPath(rect: CGRect(origin: .zero, size: imageSize))
return path
}
*/
}
extension CIImage {
func cropped(to _cropRect: EditingCrop) -> CIImage {
let targetImage = self
var cropRect = _cropRect.cropExtent
cropRect.origin.y = targetImage.extent.height - cropRect.minY - cropRect.height
let croppedImage =
targetImage
.cropped(to: cropRect)
return croppedImage
}
}