home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
-
- void add_ext (char *fname, char *ext, int force);
-
-
- int main (int argc, char *argv[])
- {
- char infile[64] = "", outfile[64] = "";
- char line[256];
- FILE *f, *g;
- int i, n;
- int convert_colours = 0, bad_colours = 0;
- float ax, ay, az, bx, by, bz, cx, cy, cz, red, green, blue;
-
- for (i = 1; i < argc; i++) {
- if (argv[i][0] != '-' && argv[i][0] != '/') {
- if (strlen(infile) == 0) {
- strcpy (infile, argv[i]);
- add_ext (infile, "txt", 0);
- }
- else if (strlen(outfile) == 0) {
- strcpy (outfile, argv[i]);
- add_ext (outfile, "raw", 0);
- }
- else {
- printf ("Too many file names specified\n");
- return 1;
- }
- }
- else {
- if (argv[i][1] == 'c' || argv[i][1] == 'C')
- convert_colours = 1;
- }
- }
-
- if (strlen (infile) == 0) {
- printf ("TXT2RAW Converter by Steve Anger\n");
- printf ("Converts TXT files created by the PV3D modeller to RAW\n");
- printf ("files readable by programs such as RAW2POV\n\n");
- printf ("Usage: txt2raw inputfile[.txt] [outputfile[.raw]] [options]\n");
- printf ("Options: -c Convert RGB colour information\n\n");
- return 1;
- }
-
- if (strlen (outfile) == 0) {
- strcpy (outfile, infile);
- add_ext (outfile, "raw", 1);
- }
-
- f = fopen (infile, "r");
- if (f == NULL) {
- printf ("Unable to open input file '%s'\n", infile);
- return 1;
- }
-
- g = fopen (outfile, "w");
- if (g == NULL) {
- printf ("Unable to open output file '%s'\n", outfile);
- return 1;
- }
-
- i = 0;
- while (!feof(f)) {
- if (++i % 10 == 0)
- printf ("%d\r", i);
-
- fgets (line, 256, f);
-
- n = sscanf (line, "%f %f %f %f %f %f %f %f %f %f %f %f", &ax, &ay,
- &az, &bx, &by, &bz, &cx, &cy, &cz, &red, &green, &blue);
-
- if (n != 9 && n != 12)
- continue;
-
- if (n == 12 && convert_colours) {
- fprintf (g, "%f %f %f %f %f %f %f %f %f %f %f %f\n", red, green,
- blue, ax, ay, az, bx, by, bz, cx, cy, cz);
-
- if (red < -0.01 || red > 1.01 || green < -0.01 || green > 1.01 ||
- blue < -0.01 || blue > 1.01)
- bad_colours = 1;
- }
- else {
- fprintf (g, "%f %f %f %f %f %f %f %f %f\n", ax, ay, az,
- bx, by, bz, cx, cy, cz);
- }
- }
-
- printf ("\n");
-
- fclose (f);
- fclose (g);
-
- if (bad_colours) {
- printf ("WARNING: Input file contains one or more illegal colour values.\n");
- printf ("Input file may not be in the correct format\n\n");
- }
- }
-
-
- void add_ext (char *fname, char *ext, int force)
- {
- int i;
-
- for (i = 0; i < strlen(fname); i++)
- if (fname[i] == '.') break;
-
- if (fname[i] == '\0' || force) {
- if (strlen(ext) > 0)
- fname[i++] = '.';
-
- strcpy (&fname[i], ext);
- }
- }
-