Skip to content

Commit d10f520

Browse files
committed
refactoring
1 parent fd682b1 commit d10f520

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

Sources/swiftui-loop-videoplayer/fn/fn+.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,24 @@ internal func handleVideoComposition(request: AVAsynchronousCIImageFilteringRequ
110110
// Finish the composition request by outputting the final image
111111
request.finish(with: currentImage, context: nil)
112112
}
113+
114+
/// Combines an array of CIFilters with additional brightness and contrast adjustments.
115+
///
116+
/// This function appends brightness and contrast adjustments as CIFilters to the existing array of filters.
117+
///
118+
/// - Parameters:
119+
/// - filters: An array of CIFilter objects to which the brightness and contrast filters will be added.
120+
/// - brightness: A Float value representing the brightness adjustment to apply.
121+
/// - contrast: A Float value representing the contrast adjustment to apply.
122+
///
123+
/// - Returns: An array of CIFilter objects, including the original filters and the added brightness and contrast adjustments.
124+
internal func combineFilters(_ filters: [CIFilter],_ brightness: Float,_ contrast: Float) -> [CIFilter] {
125+
var allFilters = filters
126+
if let filter = CIFilter(name: "CIColorControls", parameters: [kCIInputBrightnessKey: brightness]) {
127+
allFilters.append(filter)
128+
}
129+
if let filter = CIFilter(name: "CIColorControls", parameters: [kCIInputContrastKey: contrast]) {
130+
allFilters.append(filter)
131+
}
132+
return allFilters
133+
}

Sources/swiftui-loop-videoplayer/protocol/view/AbstractPlayer.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -259,28 +259,14 @@ extension AbstractPlayer{
259259
filters.append(value)
260260
}
261261

262-
/// Combines all currently applied filters with brightness and contrast adjustments.
263-
/// Brightness and contrast are applied as additional filters on top of the existing filters in the stack.
264-
/// - Returns: An array of CIFilter objects that include the existing filters and the brightness/contrast adjustments.
265-
private var combineFilters: [CIFilter] {
266-
var allFilters = filters
267-
if let filter = CIFilter(name: "CIColorControls", parameters: [kCIInputBrightnessKey: brightness]) {
268-
allFilters.append(filter)
269-
}
270-
if let filter = CIFilter(name: "CIColorControls", parameters: [kCIInputContrastKey: contrast]) {
271-
allFilters.append(filter)
272-
}
273-
return allFilters
274-
}
275-
276262
/// Applies the current set of filters to the video using an AVVideoComposition.
277263
/// This method combines the existing filters and brightness/contrast adjustments, creates a new video composition,
278264
/// and assigns it to the current AVPlayerItem. The video is paused during this process to ensure smooth application.
279265
/// This method is not supported on Vision OS.
280266
func applyVideoComposition() {
281267
guard let player = player, let currentItem = player.currentItem else { return }
282268

283-
let allFilters = combineFilters
269+
let allFilters = combineFilters(filters, brightness, contrast)
284270
#if !os(visionOS)
285271
// Might be heavy operation need to explore
286272
let videoComposition = AVVideoComposition(asset: currentItem.asset, applyingCIFiltersWithHandler: { request in
@@ -305,7 +291,14 @@ extension AbstractPlayer{
305291
#endif
306292
}
307293

308-
/// Removes all filters from the video playback.
294+
/// Removes all applied CIFilters from the video playback.
295+
///
296+
/// This function clears the array of filters and optionally re-applies the video composition
297+
/// to ensure the changes take effect immediately.
298+
///
299+
/// - Parameters:
300+
/// - apply: A Boolean value indicating whether to immediately apply the video composition after removing the filters.
301+
/// Defaults to `true`.
309302
func removeAllFilters(apply : Bool = true) {
310303

311304
guard !filters.isEmpty else { return }

0 commit comments

Comments
 (0)