Metal Custom CIFilter不同的返回值 [英] Metal Custom CIFilter different return value

查看:108
本文介绍了Metal Custom CIFilter不同的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写CIFilter,但是结果像素的颜色与金属函数返回的值不同.

kernel.metal

#include <CoreImage/CoreImage.h>

extern "C" { namespace coreimage {

    float4 foo(sample_t rgb){

        return float4(0.3f, 0.5f, 0.7f, 1.0f);

    }
}

MetalFilter.swift

import CoreImage

class MetalFilter: CIFilter {

    private let kernel: CIColorKernel

    var inputImage: CIImage?

    override init() {
        let url = Bundle.main.url(forResource: "default", withExtension: "metallib")!
        let data = try! Data(contentsOf: url)
        kernel = try! CIColorKernel(functionName: "foo", fromMetalLibraryData: data)
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func outputImage() -> CIImage? {
        guard let inputImage = inputImage else {return nil}
        return kernel.apply(extent: inputImage.extent, arguments: [inputImage])
    }
}

当我获得outputImage时,我具有以下值:
R = 0.58431372549019611
G = 0.73725490196078436
B = 0.85490196078431369

这是金属函数返回值后的某种后处理(如pow(x,1/2.373)).

解决方案

Core Image在处理图像时执行两次颜色匹配: 从输入图像的颜色空间到CIContext工作颜色空间,以及在应用所有滤镜之后的最后渲染步骤中,从工作颜色空间到输出颜色空间.

这些颜色空间配置有默认值,根据我的经验,这些默认值取决于您所运行的设备(及其显示器).但是,您可以使用 kCIContextWorkingColorSpace 定义两个颜色空间,创建CIContext时, kCIContextOutputColorSpace 选项.

如果将两个值都设置为NSNull(),则Core Image将不执行任何颜色匹配,将所有颜色值都视为图像缓冲区中的值.但是,您的滤镜可能对输入样本的色彩空间有一些假设.因此,当您处理来自像摄像机这样的来源的输入时,请记住这一点,这些输入可能根据设备和照相机的配置而具有不同的色彩空间.

确保输入样本始终位于所需颜色空间中的另一种方法是设置创建CISampler作为自定义内核的输入时的kCISamplerColorSpace 选项.

I'm writing CIFilter, but result pixel colors are different than returned values from metal function.

kernel.metal

#include <CoreImage/CoreImage.h>

extern "C" { namespace coreimage {

    float4 foo(sample_t rgb){

        return float4(0.3f, 0.5f, 0.7f, 1.0f);

    }
}

MetalFilter.swift

import CoreImage

class MetalFilter: CIFilter {

    private let kernel: CIColorKernel

    var inputImage: CIImage?

    override init() {
        let url = Bundle.main.url(forResource: "default", withExtension: "metallib")!
        let data = try! Data(contentsOf: url)
        kernel = try! CIColorKernel(functionName: "foo", fromMetalLibraryData: data)
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func outputImage() -> CIImage? {
        guard let inputImage = inputImage else {return nil}
        return kernel.apply(extent: inputImage.extent, arguments: [inputImage])
    }
}

When I get outputImage I have these values:
R = 0.58431372549019611
G = 0.73725490196078436
B = 0.85490196078431369

It's some kind of post processing (like pow(x, 1/2.373) after metal function returns values.

解决方案

Core Image performs color matching two times when you process an image: From the color space if the input image to the working color space of the CIContext and, in the final rendering step after all filters were applied, from the working color space to the output color space of the context.

Those color spaces are configured with default values that, in my experience, depend on the device (and its display) you are running on. However, you can define both color spaces using the kCIContextWorkingColorSpace and kCIContextOutputColorSpace options when creating your CIContext.

If you set both values to NSNull(), Core Image won't perform any color matching, treating all color values as they are in the image buffers. However, your filter probably has some assumptions on the color space of the input samples. So keep that in mind when you are dealing with inputs from sources like the camera that might have different color spaces depending on the device and camera configuration.

Another way to ensure the input samples are always in the color space you need is to set the kCISamplerColorSpace option when creating a CISampler that serves as input to your custom kernel.

这篇关于Metal Custom CIFilter不同的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆