2D image... index of pixel

I am trying to get [i, j] index of image pixel.

I Initialize dimensions this way

dim3 Db = dim3( 16, 16, 1 ); // block dimensions are fixed to be 256 threads

dim3 Dg = dim3( (width+Db.x-1)/Db.x, (height+Db.y-1)/Db.y, 1 );

Run<<<Dg, Db>>>()

Than in kernel i calculate [i, j]

int i = blockIdx.x * blockDim.x + threadIdx.x;

int j = blockIdx.y * blockDim.y + threadIdx.y;

printf("[%d, %d], ", i, j);

But if I print those values, I never got numbers like [0, 0], [0,1], [0,2] etc… but always high numbers [350, 352] etc

Hovewer, if i access image pixels

unsigned char * pixel = (unsigned char*)(surface + y*pitch) + 4*x;

I got correct result…

How can I got “real” image indexes, because I need them in algorithm?

Your dimension calculation seems like it’ll be generating more threads than you have pixels - wouldn’t something more like

dim3 Db = dim3( 16, 16, 1 );

dim3 Dg;

Dg.x = (width % Dg.x != 0) ? (width / Dg.x + 1) : (width / Dg.x);

Dg.y = (height % Dg.y != 0) ? (height / Dg.y + 1) : (height / Dg.y);

be what you’re looking for?