Android Renderscript地址错误

时间:2016-12-27 15:39:34

标签: android renderscript

我正在尝试使用android renderscript编写应用程序,未能写入函数的边缘检测部分。观察到如果设备android版本小于或等于Lolipop,它的工作完美,但在其他设备中,它大部分时间(从构建到构建不同,不依赖于我观察到的代码更改)给出SIGSEV 11或SIGBUS 7错误解决故障。

这是边缘检测rs实现:

BroadcastReceiver

或者这也不起作用:

uchar4 __attribute__((kernel)) Edge2( uchar in,uint32_t x, uint32_t y)   {
//sobel
uint32_t n = max(y - 1, (uint32_t)0);
uint32_t s = min(y + 1, (uint32_t)height);
uint32_t e = min(x + 1, (uint32_t)width);
uint32_t w = max(x - 1, (uint32_t)0);
const uchar *e11 = rsGetElementAt(gIn, w, n);
const uchar *e21 = rsGetElementAt(gIn, x, n);
const uchar *e31 = rsGetElementAt(gIn, e, n);

const uchar *e12 = rsGetElementAt(gIn, w, y);
const uchar *e22 = rsGetElementAt(gIn, x, y);
const uchar *e32 = rsGetElementAt(gIn, e, y);

const uchar *e13 = rsGetElementAt(gIn, w, s);
const uchar *e23 = rsGetElementAt(gIn, x, s);
const uchar *e33 = rsGetElementAt(gIn, e, s);

float r1 = ((*e12 - *e32)*3 + *e11 + *e13 - *e31 - *e33);
float r2 = ((*e21 - *e23)*3 + *e11 - *e13 + *e31 - *e33);
uchar res=(uchar)clamp(sqrt((r1*r1+r2*r2)), 0.0f, 255.0f);
return (uchar4){res, res, res,(uchar)255};
}

最后两行(return和uchar part)给出错误。顺便说一句,如果我“返回10”或类似的东西,它的工作原理。

这是java部分:

void Edge(const uchar *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y){

uint32_t n = max(y - 1, (uint32_t)0);
uint32_t s = min(y + 1, (uint32_t)height);
uint32_t e = min(x + 1, (uint32_t)width);
uint32_t w = max(x - 1, (uint32_t)0);
const uchar *e11 = rsGetElementAt(gIn, w, n);
const uchar *e21 = rsGetElementAt(gIn, x, n);
const uchar *e31 = rsGetElementAt(gIn, e, n);

const uchar *e12 = rsGetElementAt(gIn, w, y);
const uchar *e22 = rsGetElementAt(gIn, x, y);
const uchar *e32 = rsGetElementAt(gIn, e, y);

const uchar *e13 = rsGetElementAt(gIn, w, s);
const uchar *e23 = rsGetElementAt(gIn, x, s);
const uchar *e33 = rsGetElementAt(gIn, e, s);

// 1 0 -1
// 2 0 -2
// 1 0 -1
float r1 = ((*e12 - *e32)*3 + *e11 + *e13 - *e31 - *e33);
float r2 = ((*e21 - *e23)*3 + *e11 - *e13 + *e31 - *e33);
uchar res=(uchar)clamp(sqrt((r1*r1+r2*r2)), 0.0f, 255.0f);
*v_out = (uchar4){res, res, res,(uchar)255};
}

应用中有多个scriptC实例。这是一个问题还是可以? (完全相同的sobel边缘检测代码在其他rs文件中工作没有任何问题)

错误:

public Bitmap doFilter(byte[] data) {

    _inData.copy1DRangeFrom(0, _previewSamples, data);
    _inData_yuv.copyFrom(data);
    _filtersLib.forEach_Edge2(_inData, _outData);
    yuvToRgbIntrinsic.forEach(_outData_rgb);
    _filtersLib.forEach_combine(_outData, _outData);
    _outData.copyTo(_outBmp);
    return _outBmp;//Bitmap.createScaledBitmap(_outBmp, x, y, false);
}

设备:一般移动android一个(android N)

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

使用无符号数据类型传递x和y值,然后期望max(y - 1, (uint32_t)0);之类的内容正常工作?您正在进行左右边界的内存访问,因此会出错。