Metal每日分享,调节亮度滤镜效果

2022-11-24  本文已影响0人  弹吉他的少年

本案例的目的是理解如何用Metal实现调节亮度滤镜,简单讲就是通过对像素rgb+亮度值来改变图片亮度;


效果图

WX20221121-143816.png

Demo

实操代码

// 调节亮度滤镜
let filter = C7Brightness.init(brightness: 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: "C7Brightness"),参数因子[brightness]

对外开放参数

public struct C7Brightness: C7FilterProtocol {
    
    /// The adjusted brightness, from -1.0 to 1.0, with a default of 0.0 being the original picture.
    public static let range: ParameterRange<Float, Self> = .init(min: -1.0, max: 1.0, value: 0.0)
    
    public var brightness: Float = range.value
    
    public var modifier: Modifier {
        return .compute(kernel: "C7Brightness")
    }
    
    public var factors: [Float] {
        return [brightness]
    }
    
    public init(brightness: Float = range.value) {
        self.brightness = brightness
    }
}

取像素点rgb+亮度值,得到最终像素颜色;

kernel void C7Brightness(texture2d<half, access::write> outputTexture [[texture(0)]],
                         texture2d<half, access::read> inputTexture [[texture(1)]],
                         device float *brightness [[buffer(0)]],
                         uint2 grid [[thread_position_in_grid]]) {
    const half4 inColor = inputTexture.read(grid);
    
    const half4 outColor = half4(inColor.rgb + half3(*brightness), inColor.a);
    
    outputTexture.write(outColor, grid);
}

Harbeth功能清单

最后

✌️.

上一篇下一篇

猜你喜欢

热点阅读