1

我正在尝试将 Stucky 抖动(误差扩散)实现为 CIKernel,但我有点迷茫。我找不到调试过滤器的方法(我是 CIKernel 的新手)。到目前为止,这是我提出的,但内核无法编译,另外,我想知道如何将传播的错误存储到目标像素。欢迎帮助。是否有工具/方法来跟踪或调试代码?

   float mDiffCoef = [
        0,    0     0,    8/42, 4/42,
        2/42, 4/42, 8/42, 4/42, 2/42,
        1/42, 2/42, 4/42, 2/42, 1/42
    ];

    kernel vec4 dither(__sample image) {

        vec2 coord = samplerCoord(image);
        vec4 pixel = sample(image, coord);

        // decompress pixel into rgb
        float red   = pixel.r;

        float color = 0;
        if (red >= 127) color = 255;

        float err = red - color;

        // Spread the error according to the matrix
        for (int x = -2; x <= 2; x++) {
            for (int y = 0; y <= 3; y++) {
                vec2 workingSpaceCoordinate = destCoord() + vec2(x,y);
                vec2 imageSpaceCoordinate = samplerTransform(image, workingSpaceCoordinate);
                vec3 color = sample(image, imageSpaceCoordinate).rgb;
                color += err * mDiffCoef[(2+x)+5*y];
            }
        }

        return vec4(color, 1.0);
    }

[更新代码以删除语法错误]:

kernel vec4 dither(__sample image) {

    float mDiffCoef[3 * 5];
    int i = 0;
    mDiffCoef[i++] = 0.;
    mDiffCoef[i++] = 0.;
    mDiffCoef[i++] = 0.;
    mDiffCoef[i++] = 8./42.;
    mDiffCoef[i++] = 4./42.;
    mDiffCoef[i++] = 2./42.;
    mDiffCoef[i++] = 4./42.;
    mDiffCoef[i++] = 8./42.;
    mDiffCoef[i++] = 4./42.;
    mDiffCoef[i++] = 2./42.;
    mDiffCoef[i++] = 1./42.;
    mDiffCoef[i++] = 2./42.;
    mDiffCoef[i++] = 4./42.;
    mDiffCoef[i++] = 2./42.;
    mDiffCoef[i++] = 1./42.;

    vec2 coord = samplerCoord(image);
    vec4 pixel = sample(image, coord);

    // decompress pixel into rgb
    float red   = pixel.r;

    float c = 0.;
    if (red >= 127.) c = 255.;

    float err = red - c;
    float color = 0.;

    // Spread the error according to the matrix
    for (int x = -2; x <= 2; x++) {
        for (int y = 0; y < 3; y++) {
            vec2 workingSpaceCoordinate = destCoord() + vec2(x,y);
            vec2 imageSpaceCoordinate = samplerTransform(image, workingSpaceCoordinate);

            float c = sample(image, imageSpaceCoordinate).r; //gb;
            color = c + err * mDiffCoef[(2+x)+5*y];
        }
    }


    color = clamp(color, 0., 255.);
    return vec4(255., color, color, 1.);

我的主要问题是如何通过从误差扩散中累积部分值来编写输出像素缓冲区?

4

0 回答 0