android - Renderscript 的行为不一致

标签 android c renderscript android-renderscript

我编写了以下渲染脚本:

ushort* curve_hth;
ushort* curve_hts;
ushort* curve_htv;
ushort* curve_sth;
ushort* curve_sts;
ushort* curve_stv;
ushort* curve_vth;
ushort* curve_vts;
ushort* curve_vtv;

uchar4 RS_KERNEL transform(uchar4 in, uint32_t x, uint32_t y)
{
    if(!isSelected(x, y)) return in;

    ushort3 in_rgb = { (ushort) in.r, (ushort) in.g, (ushort) in.b };
    ushort3 in_hsv = rgb_to_hsv(in_rgb);
    ushort in_h = in_hsv.r;
    ushort in_s = in_hsv.g;
    ushort in_v = in_hsv.b;
    ushort out_h, out_s, out_v;
    rsDebug("out1", out_s);
    if(curve_hth != NULL) out_h += curve_hth[in_h]; else out_h += in_h;
    rsDebug("out2", out_s);
    if(curve_hts != NULL) out_s += curve_hts[in_h];
    rsDebug("out3", out_s);
    if(curve_htv != NULL) out_v += curve_htv[in_h];
    rsDebug("out4", out_s);
    if(curve_sth != NULL) out_h += curve_sth[in_s];
    rsDebug("out5", out_s);
    if(curve_sts != NULL) out_s += curve_sts[in_s]; else out_s += in_s;
    rsDebug("out6", out_s);
    if(curve_stv != NULL) out_v += curve_stv[in_s];
    rsDebug("out7", out_s);
    if(curve_vth != NULL) out_h += curve_vth[in_v];
    rsDebug("out8", out_s);
    if(curve_vts != NULL) out_s += curve_vts[in_v];
    rsDebug("out9", out_s);
    if(curve_vtv != NULL) out_v += curve_vtv[in_v]; else out_v += in_v;
    rsDebug("out10", out_s);

    out_h = min((float) out_h, (float) 360);
    out_s = min((float) out_s, (float) 100);
    out_v = min((float) out_v, (float) 100);

    ushort3 out_hsv = { out_h, out_s, out_v };
    ushort3 out_rgb = hsv_to_rgb(out_hsv);
    uchar4 out = { out_rgb.r, out_rgb.g, out_rgb.b, in.a };
    return out;
}

当我运行该脚本时,我进入 logcat:

D/RenderScript: out1 0  0x0
D/RenderScript: out2 0  0x0
D/RenderScript: out3 255  0xff
D/RenderScript: out4 255  0xff
D/RenderScript: out5 255  0xff
D/RenderScript: out6 255  0xff
D/RenderScript: out7 255  0xff
D/RenderScript: out8 255  0xff
D/RenderScript: out9 255  0xff
D/RenderScript: out10 255  0xff

这让我很惊讶,因为所有 curve_?t? 指针都应该为 null,我不会在 Java 代码中绑定(bind)它们。很奇怪,所以我修改了脚本的调试部分,得到了:

if(curve_hth != NULL) out_h += curve_hth[in_h]; else out_h += in_h;
rsDebug("out1", out_s);
rsDebug("out2", curve_hts == NULL);
if(curve_hts != NULL)
{
    out_s += curve_hts[in_h];
    rsDebug("out3", 1);
}
rsDebug("out4", out_s);
if(curve_htv != NULL) out_v += curve_htv[in_h];
if(curve_sth != NULL) out_h += curve_sth[in_s];
if(curve_sts != NULL) out_s += curve_sts[in_s]; else out_s += in_s;
if(curve_stv != NULL) out_v += curve_stv[in_s];
if(curve_vth != NULL) out_h += curve_vth[in_v];
if(curve_vts != NULL) out_s += curve_vts[in_v];
if(curve_vtv != NULL) out_v += curve_vtv[in_v]; else out_v += in_v;
rsDebug("out5", out_s);

logcat 中的结果是:

D/RenderScript: out1 0  0x0
D/RenderScript: out2 1  0x1
D/RenderScript: out4 25004  0x61ac
D/RenderScript: out5 25004  0x61ac

out2 为 1,因此 curve_hts 为 NULL。没有out3日志,if指令没有被调用,那么out_s如何改变它的值呢?

最佳答案

局部变量 out_h、out_s、out_v 未初始化,因此行为未定义。

ushort out_h, out_s, out_v;

如果你将它们初始化为0,我认为问题应该消失。

关于android - Renderscript 的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42393824/

相关文章:

java - Android 从数组中随机动态改变背景

java - 如何以编程方式在顶部的垂直线性布局中设置一个 View ,在底部设置另一个 View ?

c - 简单链表代码出错

C:将结构体指针类型的值保存到结构体中

Android rsSetElementAt 4.2

android - 实时图像处理Android camera2 api

android - Proguard 混淆和渲染脚本

android - 新的 Android Gradle 构建系统构建配置包名称与 Provider Authority 冲突

javascript - 护照 Facebook 手机

c - pic Controller 中如何将字符串转换为int