「这是我参加2022初次更文应战的第11天,活动概况查看:2022初次更文应战」
本事例的目的是理解如何用Metal实现图画包装作用滤镜,用于图画处理颜色丢掉和模糊作用;
Demo
- HarbethDemo地址
- iDay每日共享文档地址
实操代码
// 颜色丢掉和模糊作用
let filter = C7ColorPacking.init(horizontalTexel: 2.5, verticalTexel: 5)
// 计划1:
let dest = BoxxIO.init(element: originImage, filter: filter)
ImageView.image = try? dest.output()
dest.filters.forEach {
NSLog("%@", "\($0.parameterDescription)")
}
// 计划2:
ImageView.image = try? originImage.make(filter: filter)
// 计划3:
ImageView.image = originImage ->> filter
实现原理
- 过滤器
这款滤镜选用并行计算编码器规划.compute(kernel: "C7ColorPacking")
,参数因子[horizontalTexel, verticalTexel]
对外开放参数
-
horizontalTexel
: 横向偏移,越大绿色概括虚影向右偏移越多; -
verticalTexel
: 纵向偏移,越大蓝色概括虚影向下偏移越多;
/// 颜色丢掉/模糊作用
public struct C7ColorPacking: C7FilterProtocol {
/// The larger the transverse offset, the more the green contour shadow offset to the right.
public var horizontalTexel: Float
/// The larger the vertical offset, the more the blue contour shadow offset downward.
public var verticalTexel: Float
public var modifier: Modifier {
return .compute(kernel: "C7ColorPacking")
}
public var factors: [Float] {
return [horizontalTexel, verticalTexel]
}
public init(horizontalTexel: Float = 0, verticalTexel: Float = 0) {
self.horizontalTexel = horizontalTexel
self.verticalTexel = verticalTexel
}
}
- 着色器
纹路坐标和偏移均归一化处理,然后获取到上下左右4个纹路偏移坐标,取出对应的纹路赤色值,最终得到像素值;
kernel void C7ColorPacking(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::sample> inputTexture [[texture(1)]],
device float *texelWidthPointer [[buffer(0)]],
device float *texelHeightPointer [[buffer(1)]],
uint2 grid [[thread_position_in_grid]]) {
constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
const float width = outputTexture.get_width();
const float height = outputTexture.get_height();
const float texelWidth = float(*texelWidthPointer / width);
const float texelHeight = float(*texelHeightPointer / height);
const float2 textureCoordinate = float2(float(grid.x) / width, float(grid.y) / height);
const float2 upperLeftTextureCoordinate = textureCoordinate + float2(-texelWidth, -texelHeight);
const float2 upperRightTextureCoordinate = textureCoordinate + float2(texelWidth, -texelHeight);
const float2 lowerLeftTextureCoordinate = textureCoordinate + float2(-texelWidth, texelHeight);
const float2 lowerRightTextureCoordinate = textureCoordinate + float2(texelWidth, texelHeight);
half upperLeftIntensity = inputTexture.sample(quadSampler, upperLeftTextureCoordinate).r;
half upperRightIntensity = inputTexture.sample(quadSampler, upperRightTextureCoordinate).r;
half lowerLeftIntensity = inputTexture.sample(quadSampler, lowerLeftTextureCoordinate).r;
half lowerRightIntensity = inputTexture.sample(quadSampler, lowerRightTextureCoordinate).r;
const half4 outColor = half4(upperLeftIntensity, upperRightIntensity, lowerLeftIntensity, lowerRightIntensity);
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地址
- 喜爱的老板们能够点个星,谢谢各位老板!!!
✌️.