Skip to content

Commit

Permalink
Support adding custom filters to EditingStack.filters (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
shima11 authored May 30, 2024
1 parent 1a73d1f commit 354f6f3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 44 deletions.
6 changes: 6 additions & 0 deletions Dev/Sources/SwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct ContentView: View {
}
}

NavigationLink("Custom Filter") {
DemoFilterView(editingStack: {
horizontalStack
})
}

Section("Restoration Horizontal") {
Button("Crop") {
fullScreenView = .init {
Expand Down
71 changes: 71 additions & 0 deletions Dev/Sources/SwiftUIDemo/DemoFilterView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

import BrightroomEngine
import BrightroomUI
import SwiftUI
import SwiftUISupport
import UIKit

struct DemoFilterView: View {

struct InvertFilter: Filtering {
func apply(to image: CIImage, sourceImage: CIImage) -> CIImage {
image
.applyingFilter("CIColorInvert")
}
}

let invertFilter: InvertFilter = .init()

@StateObject var editingStack: EditingStack
@State var toggle: Bool = false

init(
editingStack: @escaping () -> EditingStack
) {
self._editingStack = .init(wrappedValue: editingStack())
}

var body: some View {
VStack {

ViewHost(instantiated: ImagePreviewView(editingStack: editingStack))

Toggle("Invert", isOn: $toggle)
.onChange(of: toggle) { newValue in
editingStack.set(filters: {
if newValue {
$0.additionalFilters = [invertFilter.asAny()]
} else {
$0.additionalFilters = []
}
})
}
.padding()
}
.onAppear {
editingStack.start()
}
}

}

#Preview("local") {
DemoFilterView(
editingStack: { Mocks.makeEditingStack(image: Mocks.imageHorizontal()) }
)
}

#Preview("remote") {
DemoFilterView(
editingStack: {
EditingStack(
imageProvider: .init(
editableRemoteURL: URL(
string:
"https://images.unsplash.com/photo-1604456930969-37f67bcd6e1e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1"
)!
)
)
}
)
}
74 changes: 32 additions & 42 deletions Sources/BrightroomEngine/Core/EditingStack.Edit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,9 @@ extension EditingStack {
// TODO: Remove Rect from DrawnPath
public var blurredMaskPaths: [DrawnPath] = []
}

//
// public struct Light {
//
// }
//
// public struct Color {
//
// }
//
// public struct Effects {
//
// }
//
// public struct Detail {
//
// }


public struct Filters: Equatable {

public var preset: FilterPreset?

public var brightness: FilterBrightness?
Expand All @@ -82,31 +66,37 @@ extension EditingStack {

public var vignette: FilterVignette?
public var fade: FilterFade?


public var additionalFilters: [AnyFilter] = []

func makeFilters() -> [AnyFilter] {
return ([

/**
Must be first filter since color-cube does not support wide range color.
*/
preset?.asAny(),

// Before
exposure?.asAny(),
brightness?.asAny(),
temperature?.asAny(),
highlights?.asAny(),
shadows?.asAny(),
saturation?.asAny(),
contrast?.asAny(),

// After
sharpen?.asAny(),
unsharpMask?.asAny(),
gaussianBlur?.asAny(),
fade?.asAny(),
vignette?.asAny(),
] as [AnyFilter?])
return (
([

/**
Must be first filter since color-cube does not support wide range color.
*/
preset?.asAny(),

// Before
exposure?.asAny(),
brightness?.asAny(),
temperature?.asAny(),
highlights?.asAny(),
shadows?.asAny(),
saturation?.asAny(),
contrast?.asAny(),

// After
sharpen?.asAny(),
unsharpMask?.asAny(),
gaussianBlur?.asAny(),
fade?.asAny(),
vignette?.asAny(),

] as [AnyFilter?])
+ additionalFilters
)
.compactMap { $0 }
}

Expand Down
1 change: 0 additions & 1 deletion Sources/BrightroomEngine/Core/Filtering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ enum RadiusCalculator {
}
}


public protocol Filtering: Hashable {

func apply(to image: CIImage, sourceImage: CIImage) -> CIImage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ open class ClassicImageEditContrastControl : ClassicImageEditContrastControlBase

open override func didReceiveCurrentEdit(state: Changes<ClassicImageEditViewModel.State>) {

state.ifChanged(\.editingState.loadedState?.currentEdit.filters.contrast) { value in
state.ifChanged(\.editingState.loadedState?.currentEdit.filters.contrast).do { value in
slider.set(value: value?.value ?? 0, in: FilterContrast.range)
}

Expand Down

0 comments on commit 354f6f3

Please sign in to comment.