POV-Ray : Newsgroups : povray.pov4.discussion.general : Request for *.df4 format (ASCII text based) : Re: Request for *.df4 format (ASCII text based) Server Time
18 May 2024 15:53:51 EDT (-0400)
  Re: Request for *.df4 format (ASCII text based)  
From: Warp
Date: 30 Dec 2010 03:22:04
Message: <4d1c412b@news.povray.org>
Woody <nomail@nomail> wrote:
> What do I need to do to modify the program code shown below, so that the first
> line of the data file, instead of taking 3 parameters (Size of each dimension),
> takes 4 arguments (Size of each of the three dimensins + bit resolution type).
> Where the bit resolution type is 8, 16 or 32bit.

  It requires quite some modifications because writing the larger integrals
is slightly more complicated, but this should do it:

//---------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdio>
#include <cassert>

template<typename Int_t>
void writeIntValue(Int_t value, std::ostream& os)
{
    for(unsigned byteInd = sizeof(Int_t); byteInd-- > 0;)
        os << (unsigned char)((value >> (byteInd * 8)) & 0xFF);
}

template<typename Int_t>
void createDF3(std::istream& is, std::ostream& os,
               size_t xSize, size_t ySize, size_t zSize)
{
    std::vector<Int_t> data(xSize*ySize*zSize, 0);

    while(true)
    {
        size_t x, y, z, value;
        is >> x >> y >> z >> value;
        if(!is) break;
        const size_t ind = x + y*xSize + z*xSize*ySize;
        if(ind < data.size())
            data[ind] = Int_t(value);
    }

    for(size_t i = 0; i < data.size(); ++i)
        writeIntValue(data[i], os);
}

int main(int argc, char* argv[])
{
    // Limit the size of the df3 dimensions (safeguard against invalid input).
    // Maximum df3 size will be SIZE_LIMIT*SIZE_LIMIT*SIZE_LIMIT.
    const size_t SIZE_LIMIT = 256;

    typedef unsigned char Int8_t;
    typedef unsigned short Int16_t;
    typedef unsigned int Int32_t;
    assert(sizeof(Int16_t) == 2);
    assert(sizeof(Int32_t) == 4);

    if(argc < 2)
    {
        std::cout << "Usage: " << argv[0] << " <files>\n";
        return 0;
    }

    for(int i = 1; i < argc; ++i) // for each input file
    {
        std::ifstream is(argv[i]);
        if(!is.good())
        {
            std::cerr << "Couldn't open ";
            std::perror(argv[i]);
            continue;
        }

        size_t xSize, ySize, zSize, bits;
        is >> xSize >> ySize >> zSize >> bits;
        if(xSize > SIZE_LIMIT || ySize > SIZE_LIMIT || zSize > SIZE_LIMIT ||
           (bits != 8 && bits != 16 && bits != 32))
        {
            std::cout << "Skipping " << argv[i] << " (invalid input)\n";
            continue;
        }

        std::string filename = argv[i];
        size_t ind = filename.find_last_of('.');
        if(ind != filename.npos) filename.resize(ind);
        filename += ".df3";
        std::ofstream os(filename.c_str(), std::ios::binary);

        char header[6];
        header[0] = xSize/256; header[1] = xSize%256;
        header[2] = ySize/256; header[3] = ySize%256;
        header[4] = zSize/256; header[5] = zSize%256;
        os.write(header, 6);

        switch(bits)
        {
          case 8: createDF3<Int8_t>(is, os, xSize, ySize, zSize); break;
          case 16: createDF3<Int16_t>(is, os, xSize, ySize, zSize); break;
          case 32: createDF3<Int32_t>(is, os, xSize, ySize, zSize); break;
        }

        std::cout << argv[i] << " -> " << filename << std::endl;
    }
}
//---------------------------------------------------------------------

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.