As long as you always access your data through the three-dimensional array it does not matter how the structure of the array is laid down in memory. In your example you define the first index (i) as the column index, the (j) index as the row index and the (k) index as the pixel component index.
I suspect your image file has the data organized in the standard way, which is an array of rows, where each row is an array of pixels, where each pixel is an array of color components. If that is the case, then you can modify your code to work with that structure:
ifile.open(argv[2], ios::in|ios::binary); for(int i=0; i<1278; i++){ for(int j=0; j<968; j++){ for(int k=0; k<3; k++){ Imagedata1[j][i][k]=ifile.get(); } } }
Note that I just swapped the i and j indexes in the body of the innermost for loop, the rest of the code is identical to yours.
If you also need to access the image data as a planar buffer, then you should reorganize your arrays so that the data in memory mimics the layout from the file. For this you want the first dimension to be the rows, then columns, then color components. That would end up like this:
unsigned char Imagedata1[968][1278][3]; ifile.open(argv[2], ios::in|ios::binary); for(int i=0; i<968; i++){ for(int j=0; j<1278; j++){ for(int k=0; k<3; k++){ Imagedata1[i][j][k]=ifile.get(); } } }
Update: after a quick discussion in the comments, it seems you do want to read this in a more efficient way. If you want to load this image on the stack (probably not a good idea) you would declare your image buffer as:
unsigned char Imagedata1[968][1278][3];
I would instead put it on the heap, like this:
unsigned char* Imagedata1 = new unsigned char[968*1278*3];
Then you can read the data from the file with:
if (fread(Imagedata1, 1278*3, 968, fd) != 968) { // handle error here }
Note that the numbers that you put as count and size in the fread call are not that important, as long as those two numbers multiplied are equal to width*height*components.
If you then need to access this image as a 3-dimensional array, I recommend that you create an auxiliary function, something like this:
unsigned char* getPixel(int col, int row) { return Imagedata1 + row * 1278 * 3 + col * 3; }
The return value of this function can be used as a one dimensional array of three elements, the red, green and blue of the pixel.
As a final suggestion, I recommend that you wrap your image in a class, and add member variables that define the width and height (and maybe the number of color planes as well). You do not want to hardcode the 1278s and the 968s all over the place in your code!
I hope this helps.
Imagedata1? Does the file consist of RGB triples in ascii? If not, how are they encoded?