「这是我参加2022初次更文应战的第10天,活动概况查看:2022初次更文应战」

本事例的目的是了解如何用Metal完成图画阀值素描滤镜,用于图画阀值素描,形成有噪点的素描;


Demo

  • HarbethDemo地址
  • iDay每日分享文档地址

实操代码

// 用于图画阀值素描,形成有噪点的素描
let filter = C7ThresholdSketch.init(edgeStrength: 2.5, threshold: 0.25)
// 计划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: "C7ThresholdSketch"),参数因子[edgeStrength, threshold]

对外开放参数

  • threshold: 任何高于这个阈值的边际都是黑色的,低于白色的任何东西都是黑色的。
  • edgeStrength: 调整滤波器的动态规模。更高的值会导致更强的边际,但可以饱满强度的色彩空间。
public struct C7ThresholdSketch: C7FilterProtocol {
    public static let range: ParameterRange<Float, Self> = .init(min: 0.0, max: 1.0, value: 0.25)
    /// Any edge above this threshold will be black, and anything below white. Ranges from 0.0 to 1.0
    @ZeroOneRange public var threshold: Float = range.value
    /// Adjusts the dynamic range of the filter.
    /// Higher values lead to stronger edges, but can saturate the intensity colorspace.
    public var edgeStrength: Float = 1
    public var modifier: Modifier {
        return .compute(kernel: "C7ThresholdSketch")
    }
    public var factors: [Float] {
        return [edgeStrength, threshold]
    }
    public init(edgeStrength: Float = 1, threshold: Float = range.value) {
        self.edgeStrength = edgeStrength
        self.threshold = threshold
    }
}
  • 着色器

取出周边1像素对应点的归一化坐标,获取到这些点对应的赤色值;
水平方向取顶部和底部赤色值做个整合处理,竖直方向取左边和右边做个整合处理,别离得到(h, v);
length计算出规模值,step计算出阈值对应的色彩,最终得到黑色或白色像素色彩即可;

kernel void C7ThresholdSketch(texture2d<half, access::write> outputTexture [[texture(0)]],
                              texture2d<half, access::sample> inputTexture [[texture(1)]],
                              constant float *edgeStrength [[buffer(0)]],
                              constant float *threshold [[buffer(1)]],
                              uint2 grid [[thread_position_in_grid]]) {
    constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
    const float x = float(grid.x);
    const float y = float(grid.y);
    const float width = float(inputTexture.get_width());
    const float height = float(inputTexture.get_height());
    const float2 leftCoordinate = float2((x - 1) / width, y / height);
    const float2 rightCoordinate = float2((x + 1) / width, y / height);
    const float2 topCoordinate = float2(x / width, (y - 1) / height);
    const float2 bottomCoordinate = float2(x / width, (y + 1) / height);
    const float2 topLeftCoordinate = float2((x - 1) / width, (y - 1) / height);
    const float2 topRightCoordinate = float2((x + 1) / width, (y - 1) / height);
    const float2 bottomLeftCoordinate = float2((x - 1) / width, (y + 1) / height);
    const float2 bottomRightCoordinate = float2((x + 1) / width, (y + 1) / height);
    const half leftIntensity = inputTexture.sample(quadSampler, leftCoordinate).r;
    const half rightIntensity = inputTexture.sample(quadSampler, rightCoordinate).r;
    const half topIntensity = inputTexture.sample(quadSampler, topCoordinate).r;
    const half bottomIntensity = inputTexture.sample(quadSampler, bottomCoordinate).r;
    const half topLeftIntensity = inputTexture.sample(quadSampler, topLeftCoordinate).r;
    const half topRightIntensity = inputTexture.sample(quadSampler, topRightCoordinate).r;
    const half bottomLeftIntensity = inputTexture.sample(quadSampler, bottomLeftCoordinate).r;
    const half bottomRightIntensity = inputTexture.sample(quadSampler, bottomRightCoordinate).r;
    half h = -topLeftIntensity - 2.0h * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0h * bottomIntensity + bottomRightIntensity;
    h = max(0.0h, h);
    half v = -bottomLeftIntensity - 2.0h * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0h * rightIntensity + topRightIntensity;
    v = max(0.0h, v);
    half mag = length(half2(h, v)) * half(*edgeStrength);
    mag = 1.0h - step(half(*threshold), mag);
    const half4 outColor = half4(half3(mag), 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地址
  • 喜欢的老板们可以点个星,谢谢各位老板!!!

✌️.