Reputation: 23
I'm trying to implement a median filter in RenderScript. But the work of the code is not stable. Reading elements not from the current line rsGetElementAt_uchar4 (inputAlloc, x , y + a ) causes errors. What is the problem ? Is there an example of such a filter using RenderScript?
#pragma version(1)
#pragma rs java_package_name(a.myapplication)
#pragma rs_fp_relaxed
rs_allocation inputAlloc;
int bWidht, bHeight;
static uchar4 arrpix[9];
static uchar4 buff;
uchar4 __attribute__((kernel)) median(uchar4 in, uint32_t x, uint32_t y)
{
uchar4 arrpix[9];
uchar4 buff;
if((x<bWidht) && (y<bHeight)){
arrpix[0] = rsGetElementAt_uchar4(inputAlloc, x -1 , y - 1);
arrpix[1] = rsGetElementAt_uchar4(inputAlloc, x , y - 1);
arrpix[2] = rsGetElementAt_uchar4(inputAlloc, x +1 , y - 1);
arrpix[3] = rsGetElementAt_uchar4(inputAlloc, x -1 , y );
arrpix[4] = in;
arrpix[5] = rsGetElementAt_uchar4(inputAlloc, x +1 , y );
arrpix[6] = rsGetElementAt_uchar4(inputAlloc, x -1 , y + 1);
arrpix[7] = rsGetElementAt_uchar4(inputAlloc, x , y + 1);
arrpix[8] = rsGetElementAt_uchar4(inputAlloc, x +1 , y + 1);
for(int i=0; i<4; i++)
for(int i=0; i<=8; i++){
if(arrpix[i].r>arrpix[i+1].r){
buff.r = arrpix[i].r; arrpix[i].r = arrpix[i+1].r;
arrpix[i+1].r = buff.r;}
if(arrpix[i].g>arrpix[i+1].g){
buff.g = arrpix[i].g; arrpix[i].g = arrpix[i+1].g;
arrpix[i+1].g = buff.g;}
if(arrpix[i].b>arrpix[i+1].b){
buff.b = arrpix[i].b; arrpix[i].b = arrpix[i+1].b;
arrpix[i+1].b = buff.b;}
}
}
return arrpix[4];
}
Upvotes: 0
Views: 243
Reputation: 578
You need to check x>0 and y>0 because 0-1 =-1
Bottom loops don't look completely correct either. Can you fix the spacing and did you mean to use i in both loops?
Upvotes: 1