Metal每日分享,颜色转换滤镜效果

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

本案例的目的是理解如何用Metal实现像素颜色转换滤镜,通过对像素颜色的不同读取方式获取到相应像素颜色,灰度图移除场景中除了黑白灰以外所有的颜色,让整个图像灰度化;

效果图

WX20221121-165812.png

实操代码

// 转成灰度图滤镜
let filter = C7ColorConvert(with: .gray)

// 方案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: "C7ColorConvert")

/// 颜色通道`RGB`位置转换
public struct C7ColorConvert: C7FilterProtocol {
    
    public enum ColorType: String, CaseIterable {
        case invert = "C7ColorInvert"
        case gray = "C7Color2Gray"
        case bgra = "C7Color2BGRA"
        case brga = "C7Color2BRGA"
        case gbra = "C7Color2GBRA"
        case grba = "C7Color2GRBA"
        case rbga = "C7Color2RBGA"
    }
    
    private let type: ColorType
    
    public var modifier: Modifier {
        return .compute(kernel: type.rawValue)
    }
    
    public init(with type: ColorType) {
        self.type = type
    }
}

取出像素rgb值,然后根据对应像素颜色;灰度图则是取所有的颜色分量,将它们加权或平均;

// 颜色反转,1 - rgb
kernel void C7ColorInvert(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 half4 outColor(1.0h - inColor.rgb, inColor.a);
    
    outputTexture.write(outColor, grid);
}

// 转灰度图
kernel void C7Color2Gray(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 half3 kRec709Luma = half3(0.2126, 0.7152, 0.0722);
    const half gray = dot(inColor.rgb, kRec709Luma);
    const half4 outColor = half4(half3(gray), 1.0h);
    
    outputTexture.write(outColor, grid);
}

kernel void C7Color2BGRA(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 half4 outColor(inColor.bgr, inColor.a);
    
    outputTexture.write(outColor, grid);
}

kernel void C7Color2BRGA(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 half4 outColor(inColor.brg, inColor.a);
    
    outputTexture.write(outColor, grid);
}

kernel void C7Color2GBRA(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 half4 outColor(inColor.gbr, inColor.a);
    
    outputTexture.write(outColor, grid);
}

kernel void C7Color2GRBA(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 half4 outColor(inColor.grb, inColor.a);
    
    outputTexture.write(outColor, grid);
}

kernel void C7Color2RBGA(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 half4 outColor(inColor.rbg, inColor.a);
    
    outputTexture.write(outColor, grid);
}

多滤镜联动

<p align="left">
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ae83280ff32340a889d7d4a61d0af8f6~tplv-k3u1fbpfcp-zoom-in-crop-mark:1304:0:0:0.awebp" width="250" hspace="1px">
</p>

/// 1.转换成BGRA
let filter1 = C7ColorConvert(with: C7ColorConvert.ColorType.bgra)

/// 2.调整颗粒度
var filter2 = C7Granularity()
filter2.grain = 0.8

/// 3.调整白平衡
var filter3 = C7WhiteBalance()
filter3.temperature = 5555

/// 4.调整高光阴影
var filter4 = C7HighlightShadow()
filter4.shadows = 0.4
filter4.highlights = 0.5

/// 5.组合操作,获取结果
filterImageView.image = originImage ->> filter1 ->> filter2 ->> filter3 ->> filter4

<p align="left">
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6f454038a958434da8bc26fc3aa1486a~tplv-k3u1fbpfcp-zoom-in-crop-mark:1304:0:0:0.awebp" width="250" hspace="1px">
</p>

/// 1.转换成RBGA
let filter1 = C7ColorConvert(with: C7ColorConvert.ColorType.rbga)

/// 2.调整颗粒度
var filter2 = C7Granularity()
filter2.grain = 0.8

/// 3.配置灵魂效果
var filter3 = C7SoulOut()
filter3.soul = 0.7

/// 4.组合操作
let group: [C7FilterProtocol] = [filter1, filter2, filter3]

/// 5.获取结果
filterImageView.image = try? originImage.makeGroup(filters: group)

Harbeth功能清单

最后

✌️.

上一篇下一篇

猜你喜欢

热点阅读