Nested iteration over all pixels of ofxCvGrayscaleImage

Hi list, i have this noobie question:
I need to iterate over all the pixels of a ofxCvGrayscaleImage in order to get its color value.
I need to be able to iterate over all the pixels using nested iteration .

The problem is that most of the examples of iteration over pixels that ive found uses “getPixels” that store pixels on a one dimensional array that makes possible to iterate over all the pixels without nested iteration.

like this:

unsigned char * pix = grayImage.getPixels(); 
    int numPixels = grayImage.getWidth() * grayImage.getHeight();
        for(int i = 0; i < numPixels; i++) {
        pix[i];
} 

But how can i load into a 2d dimensional array?

I need to achieve something like this:

for(int y = 0; y < h; y += 1) {
     for(int x = 0; x < w; x += 1) {
     pix[x][y]; }}

there’s a simple operation you can do to know the index from x and y: pix[y*width+x]

or you can increment one more variable in the inner loop and use that as the index.

int i=0;
for(int y = 0; y < h; y += 1) {
     for(int x = 0; x < w; x += 1,i++) {
         pix[i];
     }
}