.df3 Format

df3 File

Volume data format that Pov-Ray can read is 3 dimensional arrayed integer data[z][y][x]. Integer must be unsigned byte, unsigned short, or unsigned int.
The order of loop is z-y-x.

At the head of file, a header must be exist that shows the size of array. It mush be short int (2 bytes) numbers for x, y, and z (total 6 bytes).
Important thing is that df3 format is high order byte first format. Thus, byte order must be swapped in most PC environment.
As for byte order, you can get informations in more detail by googling with keywords like big-endian and little-endian.

Which type of integer is used, unsigned byte, unsigned short, or unsigned int, is automatically evaluated by array size and file size.
Directly after the header, body of data follows. In the case of unsigned byte, it is 1 byte data and there is no difference between big-endian and little-endian. Thus, a routine for writing data is rather simple.
In Oosawa, unsigned byte, and unsigned short can be read. However, the data is stored as 1 byte data inside the application. Thus, unsigned short data is converted to 1 byte ( 256 degree data).

Following is the sample of data writing in df3 format in language c++. Assuming that variables such as FILE *pFile are predefined.

unsigned char ucvalue[32][32][32]; //main data

// Writing Header
short size3[3] = {32, 32, 32};
ChangeEndian(&size3[0], sizeof(short)); //Swap byte order
ChangeEndian(&size3[1], sizeof(short));
ChangeEndian(&size3[2], sizeof(short));
fwrite(size3, sizeof(short), 3, pFile);

// Writing Body
for (int z = 0; z < 32; z++) 
    for (int y = 0; y < 32; y++) 
        for (int x = 0; x < 32; x++) 
            fwrite(&ucvalue[z][y][x], sizeof(char), 1, pFile);


Actually speaking, data can be wrote by fwrite at once, but to show its concept, it was writen 1 byte by 1 byte in loops.

Following is a sample of functino that sawp byte order.

// Definition@(pointer position, byte length)
void ChangeEndian(void *pHead, unsigned int length);

// Function 
void ChangeEndian(void* pHead, unsigned int length){
    unsigned char *pBuf = new unsigned char [length]; // temporary data buffer

    // copy data to buffer
    for (unsigned int i = 0; i < length; i++)
        pBuf[i] = *((unsigned char*)pHead + i);

    // copy data from buffer backwardly
    for (unsigned int i = 0; i < length; i++)
        *((unsigned char*)pHead + i) = pBuf[length - 1 - i];

    delete pBuf; // release buffer
}
inserted by FC2 system