3 = (t.s8 - c.x) * (t.s8 - c.x) + (t.s9 - c.y) * (t.s9 - c.y) +
(t.sa - c.z) * (t.sa - c.z);
if( norm2_1 <= isr2 )
{
s.x += t.s0; s.y += t.s1; s.z += t.s2;
sx += x; rowCount++;
}
if( norm2_2 <= isr2 )
{
s.x += t.s4; s.y += t.s5; s.z += t.s6;
sx += x+1; rowCount++;
}
if( norm2_3 <= isr2 )
{
s.x += t.s8; s.y += t.s9; s.z += t.sa;
sx += x+2; rowCount++;
}
}
if(rowCount == 0)
continue;
count += rowCount;
if(y == 0)
continue;
sy += y*rowCount;
}
if( count == 0 )
break;
int x1 = sx/count;
int y1 = sy/count;
s.x = s.x/count;
s.y = s.y/count;
s.z = s.z/count;
int4 tmp = s - convert_int4(c);
int norm2 = tmp.x * tmp.x + tmp.y *  tmp.y +
tmp.z * tmp.z;
bool stopFlag = (x1 == x0 && y1 == y0) || (abs(x1-x0) + abs(y1-y0) + norm2 <= eps);
x0 = x1;
y0 = y1;
c.x = s.x;
c.y = s.y;
c.z = s.z;
if( stopFlag )
break;
}
out[base] = c;
return (short2)((short)x0, (short)y0);
}
__kernel void meanshift_kernel(__global uchar4* out, int out_step,
__global uchar4* in, int in_step,
int dst_off, int src_off, int cols, int rows,
int sp, int sr, int maxIter, float eps)
{
int x0 = get_global_id(0);
int y0 = get_global_id(1);
if( x0 < cols && y0 < rows )
do_mean_shift(x0, y0, out, out_step, in, in_step, dst_off, src_off,
cols, rows, sp, sr, maxIter, eps);
}
__kernel void meanshiftproc_kernel( __global uchar4* in, __global uchar4* outr,
__global short2* outsp, int instep, int outrstep,
int outspstep, int in_off, int outr_off, int outsp_off,
int cols, int rows, int sp, int sr, int maxIter, float eps )
{
int x0 = get_global_id(0);
int y0 = get_global_id(1);
if( x0 < cols && y0 < rows )
{
outsp_off >>= 2;
outspstep >>= 2;
int basesp = outsp_off + y0 * outspstep + x0;
outsp[basesp] = do_mean_shift(x0, y0, outr, outrstep, in, instep, outr_off, in_off, cols, rows, sp, sr, maxIter, eps);
}
}
