「这是我参与2022首次更文应战的第21天,活动详情查看:2022首次更文应战」
本事例的目的是理解如何用Metal完成图画单色作用滤镜,将图画转换为单色版别,依据每个像素的亮度进行上色;
Demo
- HarbethDemo地址
- iDay每日分享文档地址
实操代码
// 去雾作用滤镜
let filter = C7Monochrome.init(intensity: 0.83, color: .blue)
// 计划1:
ImageView.image = try? BoxxIO(element: originImage, filters: [filter, filter2, filter3]).output()
// 计划2:
ImageView.image = originImage.filtering(filter, filter2, filter3)
// 计划3:
ImageView.image = originImage ->> filter ->> filter2 ->> filter3
作用对比图
- 不同参数下作用
intensity: 0.25, color: .blue | intensity: 0.5, color: .blue | intensity: 0.83, color: .blue |
---|---|---|
intensity: 0.5, color: .yellow | intensity: 0.5, color: .red | intensity: 0.5, color: .green |
---|---|---|
完成原理
- 过滤器
这款滤镜采用并行计算编码器规划.compute(kernel: "C7Monochrome")
,参数因子[intensity] + RGBAColor(color: color).toRGB()
;
对外开放参数
-
intensity
: 特定色彩取代正常图画色彩的程度,从0.0到1.0,默认值为0.0; -
color
: 保存配色计划;
/// 将图画转换为单色版别,依据每个像素的亮度进行上色
public struct C7Monochrome: C7FilterProtocol {
public static let range: ParameterRange<Float, Self> = .init(min: 0.0, max: 1.0, value: 0.0)
/// The degree to which the specific color replaces the normal image color, from 0.0 to 1.0, with 0.0 as the default.
@ZeroOneRange public var intensity: Float = range.value
/// Keep the color scheme
public var color: C7Color = .zero
public var modifier: Modifier {
return .compute(kernel: "C7Monochrome")
}
public var factors: [Float] {
return [intensity] + RGBAColor(color: color).toRGB()
}
public init(intensity: Float = range.value, color: C7Color = .zero) {
self.intensity = intensity
self.color = color
}
}
- 上色器
获取像素亮度值dot(inColor.rgb, luminanceWeighting)
,然后混合强度intensity
原始像素和单色值三者获取新的像素色彩rgb;
kernel void C7Monochrome(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
constant float *intensity [[buffer(0)]],
constant float *colorR [[buffer(1)]],
constant float *colorG [[buffer(2)]],
constant float *colorB [[buffer(3)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half3 luminanceWeighting = half3(0.2125, 0.7154, 0.0721);
const half luminance = dot(inColor.rgb, luminanceWeighting);
const half4 desat = half4(half3(luminance), 1.0h);
const half r = desat.r < 0.5 ? (2.0 * desat.r * half(*colorR)) : (1.0 - 2.0 * (1.0 - desat.r) * (1.0 - half(*colorR)));
const half g = desat.g < 0.5 ? (2.0 * desat.g * half(*colorG)) : (1.0 - 2.0 * (1.0 - desat.g) * (1.0 - half(*colorG)));
const half b = desat.b < 0.5 ? (2.0 * desat.b * half(*colorB)) : (1.0 - 2.0 * (1.0 - desat.b) * (1.0 - half(*colorB)));
const half4 outColor = half4(mix(inColor.rgb, half3(r, g, b), half(*intensity)), inColor.a);
outputTexture.write(outColor, grid);
}
Harbeth功能清单
- 支撑ios体系和macOS体系
- 支撑运算符函数式操作
- 支撑多种形式数据源 UIImage, CIImage, CGImage, CMSampleBuffer, CVPixelBuffer.
- 支撑快速规划滤镜
- 支撑兼并多种滤镜作用
- 支撑输出源的快速扩展
- 支撑相机采集特效
- 支撑视频增加滤镜特效
- 支撑矩阵卷积
- 支撑使用体系 MetalPerformanceShaders.
- 支撑兼容 CoreImage.
- 滤镜部分大致分为以下几个模块:
- Blend:图画交融技能
- Blur:含糊作用
- Pixel:图画的基本像素色彩处理
- Effect:作用处理
- Lookup:查找表过滤器
- Matrix: 矩阵卷积滤波器
- Shape:图画形状巨细相关
- Visual: 视觉动态特效
- MPS: 体系 MetalPerformanceShaders.
最后
- 渐渐再补充其他相关滤镜,喜爱就给我点个星吧。
-
滤镜Demo地址,目前包含
100+
种滤镜,同时也支撑CoreImage混合使用。 - 再附上一个开发加速库KJCategoriesDemo地址
- 再附上一个网络根底库RxNetworksDemo地址
- 喜爱的老板们能够点个星,谢谢各位老板!!!
✌️.