I have a data file that stored like this :
6 byte (3*16-bits) header.
– byte 0+1 (16-bits): x dimension
– byte 2+3 (16-bits): y dimension
– byte 4+5 (16-bits): z dimension
then: 3D volume data in x,y,z order. 16 bits (2 bytes) per voxel, only 12 bits of these 16 are used.
I want to load the data and store it in an array using C++
so how can I load 2 Bytes from a .data file the algorithm that I want to implement is
load(String filename)
{
File fp = openfile(filename);
Byte2 sizeX, sizeY, sizeZ;
sizeX = readfile(sizeof(Byte2),fp);
sizeY = readfile(sizeof(Byte2),fp);
sizeZ = readfile(sizeof(Byte2),fp);
UnsignedShort data[sizeX*sizeY*sizeZ];
for(Integer z=0; z < sizeZ; z++) {
for(Integer y=0; y < sizeY; y++) {
for(Integer x=0; x < sizeX; x++) {
data[x+y*sizeX+z*sizeX*sizeY] = readfile(sizeof(Byte2),fp);
}
}
}
}
I would recommend using an
ifstreamobject to read the data file. When you open a file in C++, you can mention that you want to read the data in binary instead of text. There is no direct equivalent of Java’sBinaryReaderin C++.More details on
ifstreamcan be found in this C++ reference page.