POV-Ray : Newsgroups : povray.pov4.discussion.general : Request for *.df4 format (ASCII text based) : Re: Request for *.df4 format (ASCII text based) Server Time
5 May 2024 09:38:03 EDT (-0400)
  Re: Request for *.df4 format (ASCII text based)  
From: Warp
Date: 16 Feb 2008 06:10:31
Message: <47b6c4a6@news.povray.org>
Woody <nomail@nomail> wrote:
> If you every have a few moments do you think you could modify the source code
> you posted at http://news.povray.org/4799ff49%40news.povray.org, so that it can
> take one or more *.txt files specified in the command line (instead of the
> testdata.txt) and output them into df3 format with the same name (except for
> the txt extension).

  You mean with the same input file format?
  How about this:


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdio>

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;

    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;
        is >> xSize >> ySize >> zSize;
        if(xSize > SIZE_LIMIT || ySize > SIZE_LIMIT || zSize > SIZE_LIMIT)
        {
            std::cout << "Skipping " << argv[i] << " (invalid input)\n";
            continue;
        }

        std::vector<char> data(xSize*ySize*zSize + 6, 0);

        // Setup df3 header:
        data[0] = xSize/256; data[1] = xSize%256;
        data[2] = ySize/256; data[3] = ySize%256;
        data[4] = zSize/256; data[5] = zSize%256;

        // Read input data and create df3 data:
        while(true)
        {
            size_t x, y, z, value;
            is >> x >> y >> z >> value;
            if(!is.good()) break;
            const size_t ind = x + y*xSize + z*xSize*ySize + 6;
            if(ind < data.size())
                data[ind] = char(value);
        }

        // Write df3 file:
        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);
        os.write(&data[0], data.size());
        std::cout << argv[i] << " -> " << filename << std::endl;
    }
}


Post a reply to this message

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