Skip to content

Commit a563d78

Browse files
committed
Update conformity the filter protocol
1 parent 57f39af commit a563d78

26 files changed

Lines changed: 264 additions & 303 deletions

Harbeth.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'Harbeth'
11-
s.version = '1.1.3'
11+
s.version = '1.1.4'
1212
s.summary = 'About image and video add filter for metal.'
1313

1414
# This description is used to generate tags and improve search results.

Harbeth.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
87671B832A8E01B800C0A5F0 /* C7CircleBlur.metal in Sources */ = {isa = PBXBuildFile; fileRef = 87671B822A8E01B800C0A5F0 /* C7CircleBlur.metal */; };
252252
8775D56D2B034C0700F6089F /* Locked.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8775D56C2B034C0700F6089F /* Locked.swift */; };
253253
8777826E2A8A0AEB00FE17CC /* CGRect+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8777826D2A8A0AEB00FE17CC /* CGRect+Ext.swift */; };
254-
877BBBC72B1F06730009C335 /* FilterableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 877BBBC62B1F06730009C335 /* FilterableView.swift */; };
254+
877BBBC72B1F06730009C335 /* HarbethView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 877BBBC62B1F06730009C335 /* HarbethView.swift */; };
255255
877BBBC92B1F10730009C335 /* Published_Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 877BBBC82B1F10730009C335 /* Published_Image.swift */; };
256256
877C24A22B2FE5E600DA6440 /* C7View+iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 877C24A12B2FE5E600DA6440 /* C7View+iOS.swift */; };
257257
87901EEF2A7210B5008FCB47 /* Image+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87901EEE2A7210B5008FCB47 /* Image+Ext.swift */; };
@@ -535,7 +535,7 @@
535535
87671B822A8E01B800C0A5F0 /* C7CircleBlur.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = C7CircleBlur.metal; sourceTree = "<group>"; };
536536
8775D56C2B034C0700F6089F /* Locked.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Locked.swift; sourceTree = "<group>"; };
537537
8777826D2A8A0AEB00FE17CC /* CGRect+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGRect+Ext.swift"; sourceTree = "<group>"; };
538-
877BBBC62B1F06730009C335 /* FilterableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterableView.swift; sourceTree = "<group>"; };
538+
877BBBC62B1F06730009C335 /* HarbethView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarbethView.swift; sourceTree = "<group>"; };
539539
877BBBC82B1F10730009C335 /* Published_Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Published_Image.swift; sourceTree = "<group>"; };
540540
877C24A12B2FE5E600DA6440 /* C7View+iOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "C7View+iOS.swift"; sourceTree = "<group>"; };
541541
87901EEE2A7210B5008FCB47 /* Image+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+Ext.swift"; sourceTree = "<group>"; };
@@ -1033,7 +1033,7 @@
10331033
children = (
10341034
874035812A987F33001D0A98 /* AsyncDest.swift */,
10351035
872BBA992B1983AE00493F25 /* Color+Ext.swift */,
1036-
877BBBC62B1F06730009C335 /* FilterableView.swift */,
1036+
877BBBC62B1F06730009C335 /* HarbethView.swift */,
10371037
87901EEE2A7210B5008FCB47 /* Image+Ext.swift */,
10381038
877BBBC82B1F10730009C335 /* Published_Image.swift */,
10391039
);
@@ -1203,7 +1203,7 @@
12031203
872661082994D82200E612C7 /* C7SplitScreen.swift in Sources */,
12041204
872660D02994D82200E612C7 /* Point2D.swift in Sources */,
12051205
877C24A22B2FE5E600DA6440 /* C7View+iOS.swift in Sources */,
1206-
877BBBC72B1F06730009C335 /* FilterableView.swift in Sources */,
1206+
877BBBC72B1F06730009C335 /* HarbethView.swift in Sources */,
12071207
872661332994D82200E612C7 /* C7DepthLuminance.swift in Sources */,
12081208
872661362994D82200E612C7 /* C7LookupSplit.metal in Sources */,
12091209
8752597E2A739CFD009085EE /* C7Pow.metal in Sources */,

Sources/Basic/Core/Compute.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,24 @@ extension C7FilterProtocol {
8484
computeEncoder.setComputePipelineState(pipelineState)
8585

8686
let destTexture = textures[0]
87-
for (i, texture) in textures.enumerated() {
88-
computeEncoder.setTexture(texture, index: i)
87+
for (index, texture) in textures.enumerated() {
88+
computeEncoder.setTexture(texture, index: index)
8989
}
9090

9191
let size = MemoryLayout<Float>.size
92-
let count = self.factors.count
93-
for i in 0..<count {
92+
for i in 0..<self.factors.count {
9493
var factor = self.factors[i]
9594
computeEncoder.setBytes(&factor, length: size, index: i)
9695
}
97-
98-
if let filter = self as? ComputeProtocol {
99-
/// 配置特殊参数非`Float`类型,例如4x4矩阵
100-
filter.setupSpecialFactors(for: computeEncoder, index: count - 1)
101-
}
96+
/// 配置特殊参数非`Float`类型,例如4x4矩阵
97+
self.setupSpecialFactors(for: computeEncoder, index: self.factors.count - 1)
10298

10399
// Too large some Gpus are not supported. Too small gpus have low efficiency
104100
// 2D texture, depth set to 1
105101
let threadgroupSize = MTLSize(width: 16, height: 16, depth: 1)
106102
// -1 pixel to solve the problem that the edges of images are not drawn.
107103
// Minimum 1 pixel, solve the problem of zero without drawing.
108-
let width = max(Int((destTexture.width + threadgroupSize.width - 1) / threadgroupSize.width), 1)
104+
let width = max(Int((destTexture.width + threadgroupSize.width - 1) / threadgroupSize.width), 1)
109105
let height = max(Int((destTexture.height + threadgroupSize.height - 1) / threadgroupSize.height), 1)
110106
//let threadGroups = MTLSizeMake(width, height, destTexture.arrayLength)
111107
let threadgroupCount = MTLSize(width: width, height: height, depth: 1)

Sources/Basic/Core/Filtering.swift

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ public protocol C7FilterProtocol: Mirrorable {
4747

4848
/// The resize of the output texture.
4949
func resize(input size: C7Size) -> C7Size
50+
51+
/// Special type of parameter factor, such as 4x4 matrix
52+
/// It is recommended to pass the parameters directly. Don't use this function if you have to.
53+
///
54+
/// - Parameters:
55+
/// - encoder: encoder, can be parallel computation encoder, can also be render 3D encoder
56+
/// - index: Current parameter factor, after use please directly add, Please refer to the `C7ColorMatrix4x4`
57+
func setupSpecialFactors(for encoder: MTLCommandEncoder, index: Int)
58+
59+
/// If you need to replace the subsequent input source texture, return to a new texture with copied to dest.
60+
/// - Parameters:
61+
/// - buffer: A valid MTLCommandBuffer to receive the encoded filter.
62+
/// - texture: Original input texture.
63+
/// - texture2: The final output texture, This parameter is mainly provided for copied new textures to use.
64+
/// - Returns: A new texture with copied to dest.
65+
func combinationBegin(for buffer: MTLCommandBuffer, source texture: MTLTexture, dest texture2: MTLTexture) throws -> MTLTexture
66+
67+
/// Combination output metal texture, support `compute`, `render` and `mps` type.
68+
/// - Parameters:
69+
/// - buffer: A valid MTLCommandBuffer to receive the encoded filter.
70+
/// - texture: The output metal texture of the first filter.
71+
/// - texture2: Original input texture.
72+
/// - Returns: Metal texture after combined filter treatment.
73+
func combinationAfter(for buffer: MTLCommandBuffer, input texture: MTLTexture, source texture2: MTLTexture) throws -> MTLTexture
5074
}
5175

5276
extension C7FilterProtocol {
@@ -56,7 +80,19 @@ extension C7FilterProtocol {
5680
public var otherInputTextures: C7InputTextures { [] }
5781
/// The resize of the output texture.
5882
public func resize(input size: C7Size) -> C7Size { size }
59-
83+
/// Special type of parameter factor, such as 4x4 matrix.
84+
public func setupSpecialFactors(for encoder: MTLCommandEncoder, index: Int) { }
85+
/// If you need to replace the subsequent input source texture, return to a new texture with copied to dest.
86+
public func combinationBegin(for buffer: MTLCommandBuffer, source texture: MTLTexture, dest texture2: MTLTexture) throws -> MTLTexture {
87+
return texture
88+
}
89+
/// Combination output metal texture, support `compute`, `render` and `mps` type.
90+
public func combinationAfter(for buffer: MTLCommandBuffer, input texture: MTLTexture, source texture2: MTLTexture) throws -> MTLTexture {
91+
return texture
92+
}
93+
}
94+
95+
extension C7FilterProtocol {
6096
/// Add the filter into the output texture with compute, render and mps filter.
6197
/// - Parameters:
6298
/// - texture: Input metal texture.
@@ -83,17 +119,6 @@ extension C7FilterProtocol {
83119
}
84120
}
85121

86-
// MARK: - compute filter protocol
87-
public protocol ComputeProtocol: C7FilterProtocol {
88-
/// Special type of parameter factor, such as 4x4 matrix
89-
/// It is recommended to pass the parameters directly. Don't use this function if you have to.
90-
///
91-
/// - Parameters:
92-
/// - encoder: encoder, can be parallel computation encoder, can also be render 3D encoder
93-
/// - index: Current parameter factor, after use please directly add, Please refer to the `C7ColorMatrix4x4`
94-
func setupSpecialFactors(for encoder: MTLCommandEncoder, index: Int)
95-
}
96-
97122
// MARK: - coreimage filter protocol
98123
public protocol CoreImageProtocol: C7FilterProtocol {
99124
/// Compatible with CoreImage.
@@ -127,33 +152,3 @@ public protocol MPSKernelProtocol: C7FilterProtocol {
127152
/// - Returns: Return output metal texture.
128153
func encode(commandBuffer: MTLCommandBuffer, textures: [MTLTexture]) throws -> MTLTexture
129154
}
130-
131-
// MARK: - combination filter protocol
132-
public protocol CombinationProtocol: C7FilterProtocol {
133-
134-
/// If you need to replace the subsequent input source texture, return to a new texture with copied to dest.
135-
/// - Parameters:
136-
/// - buffer: A valid MTLCommandBuffer to receive the encoded filter.
137-
/// - texture: Original input texture.
138-
/// - texture2: The final output texture, This parameter is mainly provided for copied new textures to use.
139-
/// - Returns: A new texture with copied to dest.
140-
func combinationBegin(for buffer: MTLCommandBuffer, source texture: MTLTexture, dest texture2: MTLTexture) throws -> MTLTexture
141-
142-
/// Combination output metal texture, support `compute`, `render` and `mps` type.
143-
/// - Parameters:
144-
/// - buffer: A valid MTLCommandBuffer to receive the encoded filter.
145-
/// - texture: The output metal texture of the first filter.
146-
/// - texture2: Original input texture.
147-
/// - Returns: Metal texture after combined filter treatment.
148-
func combinationAfter(for buffer: MTLCommandBuffer, input texture: MTLTexture, source texture2: MTLTexture) throws -> MTLTexture
149-
}
150-
151-
extension CombinationProtocol {
152-
public func combinationBegin(for buffer: MTLCommandBuffer, source texture: MTLTexture, dest texture2: MTLTexture) throws -> MTLTexture {
153-
return texture
154-
}
155-
156-
public func combinationAfter(for buffer: MTLCommandBuffer, input texture: MTLTexture, source texture2: MTLTexture) throws -> MTLTexture {
157-
return texture
158-
}
159-
}

Sources/Basic/Extensions/C7Image+iOS.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ extension HarbethWrapper where Base: C7Image {
3030
base.withRenderingMode(.alwaysOriginal)
3131
}
3232

33+
// 解决前面有绘制过Bitmap《UIGraphicsGetCurrentContext》,导致失效问题
34+
public func copy(compressionQuality: CGFloat) -> C7Image? {
35+
if let data = base.jpegData(compressionQuality: compressionQuality) {
36+
return C7Image.init(data: data)
37+
}
38+
return nil
39+
}
40+
3341
/// 白色背景透明化,色值在[222...255]之间均可祛除
3442
/// The white background is transparent, and the color value can be removed between [222...255].
3543
public func imageByMakingWhiteBackgroundTransparent() -> C7Image? {
@@ -50,18 +58,13 @@ extension HarbethWrapper where Base: C7Image {
5058
/// - compressionQuality: Compression ratio.
5159
/// - Returns: Remove the picture of the background.
5260
public func transparentColor(colorMasking: [CGFloat], compressionQuality: CGFloat = 1.0) -> C7Image? {
53-
// 解决前面有绘制过Bitmap《UIGraphicsGetCurrentContext》,导致失效问题
54-
guard let data = base.jpegData(compressionQuality: compressionQuality),
55-
let image = C7Image(data: data) else {
61+
UIGraphicsBeginImageContext(base.size)
62+
guard let maskedImageRef = base.cgImage?.copy(maskingColorComponents: colorMasking) else {
5663
return nil
5764
}
58-
UIGraphicsBeginImageContext(image.size)
59-
guard let maskedImageRef = image.cgImage?.copy(maskingColorComponents: colorMasking) else {
60-
return nil
61-
}
62-
let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
65+
let rect = CGRect(x: 0, y: 0, width: base.size.width, height: base.size.height)
6366
let context = UIGraphicsGetCurrentContext()
64-
context?.translateBy(x: 0.0, y: image.size.height)
67+
context?.translateBy(x: 0.0, y: base.size.height)
6568
context?.scaleBy(x: 1.0, y: -1.0)
6669
context?.draw(maskedImageRef, in: rect)
6770
let result = UIGraphicsGetImageFromCurrentImageContext()

Sources/Basic/Extensions/CIImage+Ext.swift

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,21 @@ extension HarbethWrapper where Base: CIImage {
2929
.transformed(by: CGAffineTransform(translationX: 0, y: base.extent.height))
3030
}
3131

32-
/// Write CIImage to metal texture synchronously.
32+
/// Write CIImage to metal texture, and then wait for final submission.
3333
/// Render bounds of CIImage to a Metal texture, optionally specifying what command buffer to use.
3434
/// - Parameters:
3535
/// - texture: Output the texture and write CIImage to the metal texture.
3636
/// - commandBuffer: A valid MTLCommandBuffer to receive the encoded filter.
37-
public func renderCIImageToTexture(_ texture: MTLTexture, commandBuffer: MTLCommandBuffer? = nil) throws {
38-
guard let commandBuffer = commandBuffer ?? Device.commandQueue().makeCommandBuffer() else {
39-
throw HarbethError.commandBuffer
40-
}
41-
let colorSpace = Device.colorSpace()
42-
let ctx = Device.context(colorSpace: colorSpace)
43-
let fixedImage = base.c7.fixHorizontalFlip()
37+
public func renderCIImageToTexture(_ texture: MTLTexture, commandBuffer: MTLCommandBuffer) throws {
38+
let ctx = Device.context(colorSpace: Device.colorSpace())
39+
let img = fixHorizontalFlip()
4440
if #available(iOS 11.0, macOS 10.13, *) {
4541
let renderDestination = CIRenderDestination(mtlTexture: texture, commandBuffer: commandBuffer)
46-
//try ctx.prepareRender(fixedImage, from: fixedImage.extent, to: renderDestination, at: .zero)
47-
_ = try ctx.startTask(toRender: fixedImage, to: renderDestination)
42+
//try ctx.prepareRender(img, from: img.extent, to: renderDestination, at: .zero)
43+
_ = try ctx.startTask(toRender: img, to: renderDestination)
4844
} else {
49-
ctx.render(fixedImage, to: texture, commandBuffer: commandBuffer, bounds: fixedImage.extent, colorSpace: colorSpace)
50-
}
51-
commandBuffer.commitAndWaitUntilCompleted()
52-
}
53-
54-
/// Asynchronous write CIImage to metal texture.
55-
/// Render `bounds` of `image` to a Metal texture, optionally specifying what command buffer to use.
56-
/// - Parameters:
57-
/// - texture: Texture type must be MTLTexture2D.
58-
/// - commandBuffer: A valid MTLCommandBuffer to receive the encoded filter.
59-
public func asyncRenderCIImageToTexture(_ texture: MTLTexture,
60-
commandBuffer: MTLCommandBuffer? = nil,
61-
complete: @escaping (Result<MTLTexture, HarbethError>) -> Void) {
62-
guard let buffer = commandBuffer ?? Device.commandQueue().makeCommandBuffer() else {
63-
complete(.failure(HarbethError.commandBuffer))
64-
return
45+
ctx.render(img, to: texture, commandBuffer: commandBuffer, bounds: img.extent, colorSpace: Device.colorSpace())
6546
}
66-
let colorSpace = Device.colorSpace()
67-
let ctx = Device.context(colorSpace: colorSpace)
68-
let fixedImage = fixHorizontalFlip()
69-
ctx.render(fixedImage, to: texture, commandBuffer: buffer, bounds: fixedImage.extent, colorSpace: colorSpace)
70-
buffer.asyncCommit(texture: texture, complete: complete)
7147
}
7248

7349
public func removingExtentOffset() -> CIImage {

Sources/Basic/Extensions/MTLTexture+Ext.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ public struct MTLTextureCompatible_ {
8080
CIImage.init(mtlTexture: target)
8181
}
8282

83+
public func toCIImage(mirrored: Bool) throws -> CIImage {
84+
guard let ciImage = toCIImage() else {
85+
throw HarbethError.texture2CIImage
86+
}
87+
if mirrored, #available(iOS 11.0, macOS 10.13, *) {
88+
// When the CIImage is created, it is mirrored and flipped upside down.
89+
// But upon inspecting the texture, it still renders the CIImage as expected.
90+
// Nevertheless, we can fix this by simply transforming the CIImage with the downMirrored orientation.
91+
return ciImage.oriented(.downMirrored)
92+
}
93+
return ciImage
94+
}
95+
8396
/// Create a CGImage with the data and information we provided.
8497
/// Each pixel contains of 4 UInt8s or 32 bits, each byte is representing one channel.
8598
/// The layout of the pixels is described with bitmap info.

0 commit comments

Comments
 (0)