「这是我参加2022首次更文挑战的第13天,活动概况检查:2022首次更文挑战」
本事例的目的是了解如何用Metal完成连环画滤镜和油画滤镜,将图画处理成连环画和油画作用;
Demo
- HarbethDemo地址
- iDay每日共享文档地址
实操代码
// 连环画作用
let filter = C7ComicStrip.init()
// 计划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
连环画完成原理
- 过滤器
这款滤镜选用并行计算编码器规划.compute(kernel: "C7ComicStrip")
/// 连环画滤镜
public struct C7ComicStrip: C7FilterProtocol {
public var modifier: Modifier {
return .compute(kernel: "C7ComicStrip")
}
public init() { }
}
- 着色器
获取到红色值abs(g - b + g + r) * r
绝对值,绿色abs(b - g + b + r) * r
,蓝色abs(b - g + b + r) * g
,最终获取到像素色彩;
kernel void C7ComicStrip(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half r = inColor.r;
const half g = inColor.g;
const half b = inColor.b;
const half R = half(abs(g - b + g + r) * r);
const half G = half(abs(b - g + b + r) * r);
const half B = half(abs(b - g + b + r) * g);
const half4 outColor = half4(R, G, B, inColor.a);
outputTexture.write(outColor, grid);
}
油画完成原理
- 过滤器
这款滤镜选用并行计算编码器规划.compute(kernel: "C7OilPainting")
,参数因子[radius, Float(pixel)]
对外开放参数
-
radius
: 含糊半径; -
pixel
: 像素颗粒度;
/// 油画滤镜
public struct C7OilPainting: C7FilterProtocol {
public var radius: Float = 3.0
public var pixel: Int = 1
public var modifier: Modifier {
return .compute(kernel: "C7OilPainting")
}
public var factors: [Float] {
return [radius, Float(pixel)]
}
public init(radius: Float = 3.0, pixel: Int = 1) {
self.radius = radius
self.pixel = pixel
}
}
- 着色器
kernel void C7OilPainting(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::sample> inputTexture [[texture(1)]],
constant float *radius [[buffer(0)]],
constant float *pixel [[buffer(1)]],
uint2 grid [[thread_position_in_grid]]) {
constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
const float2 size = float2(*pixel) / float2(outputTexture.get_width(), outputTexture.get_height());
const float2 textureCoordinate = float2(grid) / float2(outputTexture.get_width(), outputTexture.get_height());
const float r = float(*radius);
const float n = float((r + 1.0) * (r + 1.0));
float3 m0 = float3(0.0);
float3 m1 = float3(0.0);
float3 s0 = float3(0.0);
float3 s1 = float3(0.0);
float3 color = float3(0.0);
for (float j = -r; j <= 0.0; ++j) {
for (float k = -r; k <= 0.0; ++k) {
color = float3(inputTexture.sample(quadSampler, textureCoordinate + float2(k,j) * size).rgb);
m0 += color;
s0 += color * color;
}
}
for (float j = -r; j <= 0.0; ++j) {
for (float k = 0.0; k <= r; ++k) {
color = float3(inputTexture.sample(quadSampler, textureCoordinate + float2(k,j) * size).rgb);
m1 += color;
s1 += color * color;
}
}
half4 outColor = half4(0.0h);
float min_sigma2 = 100.0;
m0 /= n;
s0 = abs(s0 / n - m0 * m0);
float sigma2 = s0.r + s0.g + s0.b;
if (sigma2 < min_sigma2) {
min_sigma2 = sigma2;
outColor = half4(half3(m0), 1.0h);
}
m1 /= n;
s1 = abs(s1 / n - m1 * m1);
sigma2 = s1.r + s1.g + s1.b;
if (sigma2 < min_sigma2) {
min_sigma2 = sigma2;
outColor = half4(half3(m1), 1.0h);
}
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地址
- 喜欢的老板们能够点个星,谢谢各位老板!!!
✌️.