RenderScript错误地操作内核的输出
我试图使用Android的RenderScript在图像后面渲染半透明的圆,但从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,如float),返回的颜色值(在我的应用程序的Java代码中)是正确的。
我做错了什么,或者这真的是在RenderScript实现中的某个地方?
注:我已经检查并在万普拉斯3T(安卓7.1.1),是Nexus 5(安卓7.1.2),Android的模拟器版本7.1.2和6.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);
你能分享你的java代码和你正在测试的设备吗? – sakridge
@sakridge谢谢你的回复!我已将用于测试的Java代码和设备添加到我的帖子中。 –
我不确定,但也许'fa.a == 0'比较结果为false,因为'fa.a'是浮动的。如果您更改为'in.a == 0',它是否会产生相同的输出? –