6

我正在尝试使用 Android 的 RenderScript 在图像后面渲染一个半透明的圆圈,但是当从 RenderScript 内核返回一个值时,事情变得非常错误。

这是我的内核:

#pragma version(1)
#pragma rs java_package_name(be.abyx.aurora)
// We don't need very high precision floating points
#pragma rs_fp_relaxed

// Center position of the circle
int centerX = 0;
int centerY = 0;

// Radius of the circle
int radius = 0;

// Destination colour of the background can be set here.
float destinationR;
float destinationG;
float destinationB;
float destinationA;

static int square(int input) {
    return input * input;
}

uchar4 RS_KERNEL circleRender(uchar4 in, uint32_t x, uint32_t y) {
    //Convert input uchar4 to float4
    float4 f4 = rsUnpackColor8888(in);

    // Check if the current coordinates fall inside the circle
    if (square(x - centerX) + square(y - centerY) < square(radius)) {
        // Check if current position is transparent, we then need to add the background!)
        if (f4.a == 0) {
            uchar4 temp = rsPackColorTo8888(0.686f, 0.686f, 0.686f, 0.561f);
            return temp;
        }
    }

    return rsPackColorTo8888(f4);
}

现在,该rsPackColorTo8888()函数采用 4 个浮点数,其值介于 0.0 和 1.0 之间。然后通过计算每个浮点值的 255 次来找到生成的 ARGB 颜色。所以给定的浮点数对应于颜色 R = 0.686 * 255 = 175, G = 0.686 * 255 = 175, B = 0.686 * 255 = 175 和 A = 0.561 * 255 = 143。

rsPackColorTo8888()函数本身可以正常工作,但是当uchar4从内核返回找到的值时,会发生一些非常奇怪的事情。R、G 和 B 值分别更改为 Red * Alpha = 56、Green * Alpha = 56 和 Blue * Alpha = 56,其中 Alpha 为 0.561。这意味着 R、G 和 B 的任何值都不能大于 A = 0.561 * 255。

手动设置输出,而不是使用rsPackColorTo8888()产生完全相同的行为。我的意思是下面的代码产生完全相同的结果,这反过来证明这rsPackColorTo8888()不是问题:

if (square(x - centerX) + square(y - centerY) < square(radius)) {
    // Check if current position is transparent, we then need to add the background!)
    if (f4.a == 0) {
        uchar4 temp;
        temp[0] = 175;
        temp[1] = 175;
        temp[2] = 175;
        temp[3] = 143;
        return temp;
    }
}

这是调用脚本的 Java 代码:

@Override
public Bitmap renderParallel(Bitmap input, int backgroundColour, int padding) {
    ResizeUtility resizeUtility = new ResizeUtility();

    // We want to end up with a square Bitmap with some padding applied to it, so we use the
    // the length of the largest dimension (width or height) as the width of our square.
    int dimension = resizeUtility.getLargestDimension(input.getWidth(), input.getHeight()) + 2 * padding;

    Bitmap output = resizeUtility.createSquareBitmapWithPadding(input, padding);
    output.setHasAlpha(true);

    RenderScript rs = RenderScript.create(this.context);

    Allocation inputAlloc = Allocation.createFromBitmap(rs, output);
    Type t = inputAlloc.getType();

    Allocation outputAlloc = Allocation.createTyped(rs, t);

    ScriptC_circle_render circleRenderer = new ScriptC_circle_render(rs);

    circleRenderer.set_centerX(dimension / 2);
    circleRenderer.set_centerY(dimension / 2);
    circleRenderer.set_radius(dimension / 2);
    circleRenderer.set_destinationA(((float) Color.alpha(backgroundColour)) / 255.0f);
    circleRenderer.set_destinationR(((float) Color.red(backgroundColour)) / 255.0f);
    circleRenderer.set_destinationG(((float) Color.green(backgroundColour)) / 255.0f);
    circleRenderer.set_destinationB(((float) Color.blue(backgroundColour)) / 255.0f);

    circleRenderer.forEach_circleRender(inputAlloc, outputAlloc);
    outputAlloc.copyTo(output);

    inputAlloc.destroy();
    outputAlloc.destroy();
    circleRenderer.destroy();
    rs.destroy();

    return output;
}

当 alpha 设置为 255(或 1.0 作为浮点数)时,返回的颜色值(在我的应用程序的 Java 代码中)是正确的。

我做错了什么,还是这真的是 RenderScript 实现中的某个错误?

注意:我已经在 Oneplus 3T (Android 7.1.1)、Nexus 5 (Android 7.1.2)、Android 模拟器版本 7.1.2 和 6.0 上检查并验证了此行为

4

1 回答 1

0

而不是传递具有类型的值:

uchar4 temp = rsPackColorTo8888(0.686f, 0.686f, 0.686f, 0.561f);

尝试创建一个 float4 并通过它。

float4 newFloat4 = { 0.686, 0.686, 0.686, 0.561 };
uchar4 temp = rsPackColorTo8888(newFloat4);
于 2017-09-09T08:07:59.367 回答