Skip to content

Commit

Permalink
Rename HarbethIO
Browse files Browse the repository at this point in the history
  • Loading branch information
yangKJ committed Apr 18, 2024
1 parent 3171df0 commit 0fa511e
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Demo/Harbeth-SwiftUI-Demo/Resources/Res.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Res {
public static func rgUVB1Gradient(_ size: CGSize = .onePixel) throws -> MTLTexture {
let texture = try TextureLoader.emptyTexture(at: size)
let filter = C7ColorGradient(with: .rgUVB1)
var dest = BoxxIO(element: texture, filter: filter)
var dest = HarbethIO(element: texture, filter: filter)
//dest.createDestTexture = false
return try dest.output()
}
Expand Down
2 changes: 1 addition & 1 deletion Demo/Harbeth-SwiftUI-Demo/Views/BlendView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct BlendView: View {
let overTexture = try! Res.rgUVB1Gradient(CGSize(width: 420, height: 270))
var filter = C7Blend(with: blendMode, blendTexture: overTexture)
filter.intensity = intensity
let dest = BoxxIO(element: inputImage, filter: filter)
let dest = HarbethIO(element: inputImage, filter: filter)
return try dest.output()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Demo/Harbeth-SwiftUI-Demo/Views/CubeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct CubeView: View {
func setupImage() {
let inputImage = R.image("IMG_0020")!
let filter = CIColorCube(cubeName: "vista200 v1")
let dest = BoxxIO(element: inputImage, filter: filter)
let dest = HarbethIO(element: inputImage, filter: filter)
dest.transmitOutput { img in
DispatchQueue.main.async {
self.outImage = img
Expand Down
4 changes: 2 additions & 2 deletions Demo/Harbeth-iOS-Demo/Collector/C7Collector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension C7Collector {
}
delegate?.captureOutput?(self, pixelBuffer: pixelBuffer)
let texture = pixelBuffer.c7.toMTLTexture(textureCache: textureCache)
let dest = BoxxIO(element: texture, filters: filters)
let dest = HarbethIO(element: texture, filters: filters)
guard let texture = try? dest.output() else {
return nil
}
Expand All @@ -83,7 +83,7 @@ extension C7Collector {
guard let texture = pixelBuffer.c7.toMTLTexture(textureCache: textureCache) else {
return
}
var dest = BoxxIO(element: texture, filters: filters)
var dest = HarbethIO(element: texture, filters: filters)
dest.transmitOutputRealTimeCommit = true
dest.transmitOutput(success: { [weak self] desTexture in
guard let `self` = self else {
Expand Down
2 changes: 1 addition & 1 deletion Demo/Harbeth-iOS-Demo/HomeViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extension ViewControllerType {
return nil
}
let filter = C7SolidColor.init(color: color)
let dest = BoxxIO(element: texture, filter: filter)
let dest = HarbethIO(element: texture, filter: filter)
return try? dest.output()
}
}
2 changes: 1 addition & 1 deletion Demo/Harbeth-iOS-Demo/Modules/ImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension ImageViewController {
guard let filter = filter else {
return
}
let dest = BoxxIO(element: originImage, filter: filter)
let dest = HarbethIO(element: originImage, filter: filter)
dest.transmitOutput { [weak self] image in
DispatchQueue.main.async {
self?.renderView.image = image
Expand Down
2 changes: 1 addition & 1 deletion Demo/Harbeth-iOS-Demo/Modules/UnitTestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ extension UnitTestViewController {
func unitTest() {
let filter = C7Storyboard(ranks: 2)

//let dest = BoxxIO.init(element: originImage, filters: [filter])
//let dest = HarbethIO.init(element: originImage, filters: [filter])
//renderView.image = try? dest.output()
renderView.filters = [filter]
}
Expand Down
2 changes: 1 addition & 1 deletion Demo/Harbeth-macOS-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class ViewController: NSViewController {
return
}
NSLog("%@", "\(type(of: filter)) --- \(filter.parameterDescription)")
let dest = BoxxIO.init(element: originImage, filter: filter)
let dest = HarbethIO.init(element: originImage, filter: filter)
//ImageView.image = try? dest.output()
dest.transmitOutput(success: { [weak self] img in
DispatchQueue.main.async {
Expand Down
2 changes: 1 addition & 1 deletion Harbeth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Harbeth'
s.version = '1.1.8'
s.version = '1.1.9'
s.summary = 'About image and video add filter for metal.'

# This description is used to generate tags and improve search results.
Expand Down
8 changes: 4 additions & 4 deletions Harbeth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
872660E02994D82200E612C7 /* Matrix4x4.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87265FE42994D82100E612C7 /* Matrix4x4.swift */; };
872660EF2994D82200E612C7 /* Destype.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87265FF52994D82100E612C7 /* Destype.swift */; };
872660F02994D82200E612C7 /* Outputable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87265FF62994D82100E612C7 /* Outputable.swift */; };
872660F22994D82200E612C7 /* BoxxIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87265FF82994D82100E612C7 /* BoxxIO.swift */; };
872660F22994D82200E612C7 /* HarbethIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87265FF82994D82100E612C7 /* HarbethIO.swift */; };
872660F62994D82200E612C7 /* C7Rotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87265FFF2994D82100E612C7 /* C7Rotate.swift */; };
872660F72994D82200E612C7 /* C7Resize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872660002994D82100E612C7 /* C7Resize.swift */; };
872660F82994D82200E612C7 /* C7Crop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872660012994D82100E612C7 /* C7Crop.swift */; };
Expand Down Expand Up @@ -327,7 +327,7 @@
87265FE42994D82100E612C7 /* Matrix4x4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix4x4.swift; sourceTree = "<group>"; };
87265FF52994D82100E612C7 /* Destype.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Destype.swift; sourceTree = "<group>"; };
87265FF62994D82100E612C7 /* Outputable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Outputable.swift; sourceTree = "<group>"; };
87265FF82994D82100E612C7 /* BoxxIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BoxxIO.swift; sourceTree = "<group>"; };
87265FF82994D82100E612C7 /* HarbethIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HarbethIO.swift; sourceTree = "<group>"; };
87265FFF2994D82100E612C7 /* C7Rotate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C7Rotate.swift; sourceTree = "<group>"; };
872660002994D82100E612C7 /* C7Resize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C7Resize.swift; sourceTree = "<group>"; };
872660012994D82100E612C7 /* C7Crop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C7Crop.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -797,8 +797,8 @@
87265FF42994D82100E612C7 /* Outputs */ = {
isa = PBXGroup;
children = (
87265FF82994D82100E612C7 /* BoxxIO.swift */,
87265FF52994D82100E612C7 /* Destype.swift */,
87265FF82994D82100E612C7 /* HarbethIO.swift */,
87A249E82BAA75B900AF7D1A /* HugeImage.swift */,
87265FC62994D82100E612C7 /* Operators.swift */,
87265FF62994D82100E612C7 /* Outputable.swift */,
Expand Down Expand Up @@ -1363,7 +1363,7 @@
87901EF12A726588008FCB47 /* C7ColorGradient.swift in Sources */,
8726617C2994D82200E612C7 /* C7BlendColorBurn.metal in Sources */,
8726611A2994D82200E612C7 /* C7Sketch.metal in Sources */,
872660F22994D82200E612C7 /* BoxxIO.swift in Sources */,
872660F22994D82200E612C7 /* HarbethIO.swift in Sources */,
872660C12994D82200E612C7 /* Shared.swift in Sources */,
872661762994D82200E612C7 /* C7BlendDissolve.metal in Sources */,
8726616A2994D82200E612C7 /* C7HighlightShadowTint.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ let filter3 = C7SoulOut(soul: 0.7)
let filters = [filter1, filter2, filter3]
// Use:
let dest = BoxxIO.init(element: originImage, filters: filters)
let dest = HarbethIO.init(element: originImage, filters: filters)
// Synchronize do something..
ImageView.image = try? dest.output()
Expand All @@ -77,7 +77,7 @@ ImageView.image = originImage ->> filter1 ->> filter2 ->> filter3
This performance is the best. 🚗🚗

```
let dest = BoxxIO.init(element: ``Source``, filter: ``filter``)
let dest = HarbethIO.init(element: ``Source``, filter: ``filter``)
dest.transmitOutput(success: { [weak self] image in
// do something..
Expand Down Expand Up @@ -119,7 +119,7 @@ extension CameraViewController: C7CollectorImageDelegate {

- 📺 Local video or Network video are simply apply with filters.
- 🙄 For details, See [PlayerViewController](https://github.com/yangKJ/Harbeth/blob/master/Demo/Harbeth-iOS-Demo/Modules/PlayerViewController.swift).
- You can also extend this by using [BoxxIO](https://github.com/yangKJ/Harbeth/blob/master/Sources/Basic/Outputs/BoxxIO.swift) to filter the collected `CVPixelBuffer`.
- You can also extend this by using [HarbethIO](https://github.com/yangKJ/Harbeth/blob/master/Sources/Basic/Outputs/HarbethIO.swift) to filter the collected `CVPixelBuffer`.

```
lazy var video: C7CollectorVideo = {
Expand All @@ -143,7 +143,7 @@ extension PlayerViewController: C7CollectorImageDelegate {
```

### SwiftUI Support
- For the direct use [HarbethView](https://github.com/yangKJ/Harbeth/blob/master/Sources/SwiftUI/FilterableView.swift), it is just a simple implementation.
- For the direct use [HarbethView](https://github.com/yangKJ/Harbeth/blob/master/Sources/SwiftUI/HarbethView.swift), it is just a simple implementation.
- The SwiftUI API is still in-progress and may not be production ready. We're looking for help! 🤲

```
Expand Down
6 changes: 3 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let filters = [filter, filter2, filter3]
ImageView.image = try? originImage.makeGroup(filters: filters)
// 也可数据源模式使用
let dest = BoxxIO.init(element: originImage, filters: filters)
let dest = HarbethIO.init(element: originImage, filters: filters)
// 同步处理
ImageView.image = try? dest.output()
Expand Down Expand Up @@ -90,7 +90,7 @@ extension CameraViewController: C7CollectorImageDelegate {

- 本地视频 or 网络视频简单注入滤镜
- 🙄 详细请参考[PlayerViewController](https://github.com/yangKJ/Harbeth/blob/master/MetalDemo/Modules/PlayerViewController.swift)
- 您也可以自己去扩展,使用`BoxxIO`对采集的`CVPixelBuffer`进行滤镜注入处理。
- 您也可以自己去扩展,使用`HarbethIO`对采集的`CVPixelBuffer`进行滤镜注入处理。

```
lazy var video: C7CollectorVideo = {
Expand Down Expand Up @@ -118,7 +118,7 @@ extension PlayerViewController: C7CollectorImageDelegate {
```

### SwiftUI Support
- 直接使用即可 [HarbethView](https://github.com/yangKJ/Harbeth/blob/master/Sources/SwiftUI/FilterableView.swift)
- 直接使用即可 [HarbethView](https://github.com/yangKJ/Harbeth/blob/master/Sources/SwiftUI/HarbethView.swift)
- 这个API可能也暂时不够稳定,暂时先这样吧!
- 当然你也可以来完善它,感谢! 🤲

Expand Down
2 changes: 1 addition & 1 deletion Sources/Basic/Extensions/C7Color+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ extension HarbethWrapper where Base: C7Color {
#if HARBETH_COMPUTE_LIBRARY_IN_BUNDLE
let texture = try? TextureLoader.emptyTexture(at: size)
let filter = C7SolidColor(color: base)
let dest = BoxxIO(element: texture, filter: filter)
let dest = HarbethIO(element: texture, filter: filter)
let image = (try? dest.output())?.c7.toImage()
return image
#else
Expand Down
20 changes: 16 additions & 4 deletions Sources/Basic/Outputs/Destype.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public protocol Destype {

init(element: Element, filters: [C7FilterProtocol])

func filtered() -> Element

/// Add filters to sources synchronously.
/// - Returns: Added filter source.
func output() throws -> Element
Expand All @@ -34,10 +36,20 @@ public protocol Destype {
}

extension Destype {
/// Asynchronous quickly add filters to sources.
/// - Parameters:
/// - success: Successful callback of adding filters to the sources asynchronously.
/// - failed: An error occurred during the conversion process, the error is `HarbethError`.

public init(element: Element, filter: C7FilterProtocol) {
self.init(element: element, filters: [filter])
}

public func filtered() -> Element {
do {
let dest = HarbethIO.init(element: element, filters: filters)
return try dest.output()
} catch {
return element
}
}

public func transmitOutput(success: @escaping (Element) -> Void, failed: ((HarbethError) -> Void)? = nil) {
transmitOutput { res in
switch res {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// BoxxIO.swift
// HarbethIO.swift
// Harbeth
//
// Created by Condy on 2022/10/22.
Expand All @@ -11,21 +11,24 @@ import CoreImage
import CoreMedia
import CoreVideo

@available(*, deprecated, message: "Typo. Use `HarbethIO` instead", renamed: "HarbethIO")
public typealias BoxxIO<Dest> = HarbethIO<Dest>

/// Quickly add filters to sources.
/// Support use `UIImage/NSImage, CGImage, CIImage, MTLTexture, CMSampleBuffer, CVPixelBuffer/CVImageBuffer`
///
/// For example:
///
/// let filter = C7Storyboard(ranks: 2)
/// let dest = BoxxIO.init(element: originImage, filter: filter)
/// let dest = HarbethIO.init(element: originImage, filter: filter)
/// ImageView.image = try? dest.output()
///
/// // Asynchronous add filters to sources.
/// dest.transmitOutput(success: { [weak self] image in
/// // do somthing..
/// })
///
@frozen public struct BoxxIO<Dest> : Destype {
@frozen public struct HarbethIO<Dest> : Destype {
public typealias Element = Dest
public let element: Dest
public let filters: [C7FilterProtocol]
Expand Down Expand Up @@ -58,10 +61,6 @@ import CoreVideo
/// Considering the maximization of performance, we will deal with it separately.
private var hasCoreImage: Bool

public init(element: Dest, filter: C7FilterProtocol) {
self.init(element: element, filters: [filter])
}

public init(element: Dest, filters: [C7FilterProtocol]) {
self.element = element
self.filters = filters
Expand Down Expand Up @@ -125,16 +124,17 @@ import CoreVideo
complete(.success(texture))
return
}
var result: MTLTexture = texture
var iterator = self.filters.makeIterator()
var commandBuffer: MTLCommandBuffer?
if self.hasCoreImage == false {
do {
commandBuffer = try makeCommandBuffer()
} catch {
complete(.failure(HarbethError.toHarbethError(error)))
return
}
}
var result: MTLTexture = texture
var iterator = self.filters.makeIterator()
// 递归处理
func recursion(filter: C7FilterProtocol?, sourceTexture: MTLTexture) {
guard let filter = filter else {
Expand All @@ -155,12 +155,12 @@ import CoreVideo
}
}, buffer: commandBuffer)
}
recursion(filter: iterator.next(), sourceTexture: texture)
recursion(filter: iterator.next(), sourceTexture: result)
}
}

// MARK: - filtering methods
extension BoxxIO {
extension HarbethIO {

private func filtering(pixelBuffer: CVPixelBuffer) throws -> CVPixelBuffer {
let inTexture = try TextureLoader.init(with: pixelBuffer).texture
Expand Down Expand Up @@ -219,7 +219,7 @@ extension BoxxIO {
}

// MARK: - asynchronous filtering methods
extension BoxxIO {
extension HarbethIO {

private func filtering(pixelBuffer: CVPixelBuffer, complete: @escaping (Result<CVPixelBuffer, HarbethError>) -> Void) {
do {
Expand Down Expand Up @@ -319,7 +319,7 @@ extension BoxxIO {
}

// MARK: - private methods
extension BoxxIO {
extension HarbethIO {

private func createDestTexture(with sourceTexture: MTLTexture, filter: C7FilterProtocol) throws -> MTLTexture {
if self.createDestTexture == false {
Expand Down Expand Up @@ -371,10 +371,11 @@ extension BoxxIO {
default:
return texture
}
let outputTexture = try filter.combinationAfter(for: commandBuffer, input: destTexture, source: texture)
if hasCoreImage {
commandBuffer.commitAndWaitUntilCompleted()
}
return try filter.combinationAfter(for: commandBuffer, input: destTexture, source: texture)
return outputTexture
}

/// Whether to synchronously wait for the execution of the Metal command buffer to complete.
Expand Down

0 comments on commit 0fa511e

Please sign in to comment.