Played again with the puzzle and got better result. MHA file
Having referenced images is possible to do it clean (dimensions,origin, orientation, spacing, etc.)
#include <fstream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <iostream>
int main(int, char**)
{
char * data1 = new char[(284*320*192)/8];
// input is raw binary data from 0x7fe0, 0x0010
std::ifstream file("binary.bin", std::ios::in | std::ios::binary);
file.read(data1, (284*320*192)/8);
file.close();
unsigned char * data2 = new unsigned char[284*320*192];
size_t count_data2 = 0;
for (size_t x = 0; x < (284*320*192)/8; x++)
{
const unsigned char c = data1[x];
data2[count_data2 + 0] = (c & 0x01) ? 255 : 0;
data2[count_data2 + 1] = (c & 0x02) ? 255 : 0;
data2[count_data2 + 2] = (c & 0x04) ? 255 : 0;
data2[count_data2 + 3] = (c & 0x08) ? 255 : 0;
data2[count_data2 + 4] = (c & 0x10) ? 255 : 0;
data2[count_data2 + 5] = (c & 0x20) ? 255 : 0;
data2[count_data2 + 6] = (c & 0x40) ? 255 : 0;
data2[count_data2 + 7] = (c & 0x80) ? 255 : 0;
count_data2 += 8;
}
std::cout << "count_data2=" << count_data2 << std::endl;
std::ofstream out_file("out_data.raw", std::ios::out | std::ios::binary);
#if 0
out_file.write((char*)data2, 284*320*192);
#else
const size_t out_x = 288;
const size_t out_y = 320;
char * data3 = new char[out_x*out_y*192];
std::memset(data3, 0, out_x*out_y*192);
//std::memcpy(&data3[(out_x*out_y*192) - (284*320*192)], data2, 284*320*192);
std::memcpy(&data3[0], data2, 284*320*192);
out_file.write((char*)data3, out_x*out_y*192);
#endif
out_file.close();
delete [] data2;
delete [] data3;
return 0;
}
Edit:
Here is that raw binary data (input for code above) and MetaIO header (for mhd/raw):
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 1 0 0 0 1 0 0 0 1
Offset = 0 0 0
CenterOfRotation = 0 0 0
AnatomicalOrientation = RAI
ElementSpacing = 1 1 1
DimSize = 288 320 192
ElementType = MET_UCHAR
ElementDataFile = out_data.raw
