Not sure why you would want to copy all the data from within a CImg structure/class to another 2D array when you can access it exactly like that anyway. So, if you want the pixel at location [x,y], just use:
img(x,y)
Here is a complete program to dump an image - accessing individual pixels in the inner loop:
#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"
using namespace cimg_library;
using namespace std;
int main() {
CImg<unsigned char> img("test.pgm");
// Get width, height, number of channels
int w=img.width();
int h=img.height();
int c=img.spectrum();
cout << "Dimensions: " << w << "x" << h << " " << c << " channels" <<endl;
// Dump all pixels
for(int y=0;y<h;y++){
for(int x=0;x<w;x++){
cout << y << "," << x << " " << (int)img(x,y) << endl;
}
}
}
I used this test image - which is 5x3 PGM (Portable Grey Map) so you can easily see the pixel values but it is just the same with a PNG image:
P2
5 3
255
0 1 2 3 4
10 11 12 13 14
100 101 102 103 104
Here is the output, which you can see matches the image:
Dimensions: 5x3 1 channels
0,0 0
0,1 1
0,2 2
0,3 3
0,4 4
1,0 10
1,1 11
1,2 12
1,3 13
1,4 14
2,0 100
2,1 101
2,2 102
2,3 103
2,4 104