I want to read a raw file, which has 3 interleaving and has a size of about(3.5MB) large into a three dimensional array.. the code that I am using to read the file is:
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[i][j][k]=ifile.get();
}
}
}
The thing is this array is not what i expect it to be.. I need the 1278 to be the width of the image.. the 968 to be the height and 3 bytes are the RGB values.. how should i write the code to read from the file such that the array gets populated correctly.. Thanks.
First, image files are usually stored, in order of smallest jump to largest, color values, column, row order. You are not reading them in that order.
That is how the loop should be arranged, though you may want to rename your variables to keep things straight:
Secondly, the way you allocate your array is really broken and inefficient. Here is how it should work:
Using a class for
ColorValuekeeps you from having magic indexes everywhere in your code for the ‘r’, ‘g’, and ‘b’ components. And allocating the array in this way keeps all the memory used for the image contiguous and removes several levels of unnecessary indirection. These both have the property of making your program much more cache friendly.I also found a nice article that’s a really comprehensive treatment of multi-dimensional arrays in C++.