home *** CD-ROM | disk | FTP | other *** search
/ Chestnut's Multimedia Mania / MM_MANIA.ISO / graphics / povsrc20 / povray.c < prev    next >
C/C++ Source or Header  |  1993-10-03  |  33KB  |  1,054 lines

  1. /****************************************************************************
  2. *                povray.c
  3. *
  4. *  This module contains the entry routine for the raytracer and the code to
  5. *  parse the parameters on the command line.
  6. *
  7. *  from Persistence of Vision Raytracer
  8. *  Copyright 1993 Persistence of Vision Team
  9. *---------------------------------------------------------------------------
  10. *  NOTICE: This source code file is provided so that users may experiment
  11. *  with enhancements to POV-Ray and to port the software to platforms other 
  12. *  than those supported by the POV-Ray Team.  There are strict rules under
  13. *  which you are permitted to use this file.  The rules are in the file
  14. *  named POVLEGAL.DOC which should be distributed with this file. If 
  15. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  16. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  17. *  Forum.  The latest version of POV-Ray may be found there as well.
  18. *
  19. * This program is based on the popular DKB raytracer version 2.12.
  20. * DKBTrace was originally written by David K. Buck.
  21. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  22. *
  23. *****************************************************************************/
  24.  
  25. #include <ctype.h>
  26. #include <time.h>          /* BP */
  27. #include "frame.h"        /* common to ALL modules in this program */
  28. #include "povproto.h"
  29.  
  30. #define MAX_FILE_NAMES 1
  31. unsigned int Options;
  32. unsigned long Quality_Flags;
  33. int Quality,Use_Slabs;
  34. int Case_Sensitive_Flag = CASE_SENSITIVE_DEFAULT;
  35.  
  36. extern FRAME Frame;
  37. OBJECT *Root_Object;
  38.  
  39. char Input_File_Name[FILE_NAME_LENGTH], Output_File_Name[FILE_NAME_LENGTH], Stat_File_Name[FILE_NAME_LENGTH];
  40.  
  41. #define MAX_LIBRARIES 10
  42. char *Library_Paths[MAX_LIBRARIES];
  43. int Library_Path_Index;
  44. int Max_Symbols = 1000;
  45.  
  46. FILE_HANDLE *Output_File_Handle;
  47. int File_Buffer_Size;
  48. static int Number_Of_Files;
  49. static int inflag, outflag; 
  50. DBL VTemp;
  51. DBL Antialias_Threshold;
  52. int First_Line, Last_Line;
  53. int First_Column, Last_Column;
  54. DBL First_Column_Temp, Last_Column_Temp;
  55. DBL First_Line_Temp, Last_Line_Temp;
  56. int Display_Started = FALSE;
  57. int Shadow_Test_Flag = FALSE;
  58. DBL Clock_Value = 0.0;
  59. DBL Language_Version = 2.0;
  60. long Bounds_Threshold = 25;
  61. long AntialiasDepth = 3;
  62. DBL JitterScale = 1.0;
  63.  
  64. /* Quality constants */
  65.   long Quality_Values[10]=
  66.     {
  67.     QUALITY_0, QUALITY_1, QUALITY_2, QUALITY_3, QUALITY_4,
  68.     QUALITY_5, QUALITY_6, QUALITY_7, QUALITY_8, QUALITY_9
  69.     };
  70. /* Stats kept by the ray tracer: */
  71. long Number_Of_Pixels, Number_Of_Rays, Number_Of_Pixels_Supersampled;
  72. long Ray_Sphere_Tests, Ray_Sphere_Tests_Succeeded;
  73. long Ray_Box_Tests, Ray_Box_Tests_Succeeded;    
  74. long Ray_Blob_Tests, Ray_Blob_Tests_Succeeded;  
  75. long Ray_Cone_Tests, Ray_Cone_Tests_Succeeded;
  76. long Ray_Disc_Tests, Ray_Disc_Tests_Succeeded;
  77. long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  78. long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  79. long Ray_Triangle_Tests, Ray_Triangle_Tests_Succeeded;
  80. long Ray_Quadric_Tests, Ray_Quadric_Tests_Succeeded;
  81. long Ray_Poly_Tests, Ray_Poly_Tests_Succeeded;
  82. long Ray_Bicubic_Tests, Ray_Bicubic_Tests_Succeeded;
  83. long Ray_Ht_Field_Tests, Ray_Ht_Field_Tests_Succeeded;
  84. long Ray_Ht_Field_Box_Tests, Ray_HField_Box_Tests_Succeeded;
  85. long Bounding_Region_Tests, Bounding_Region_Tests_Succeeded;
  86. long Clipping_Region_Tests, Clipping_Region_Tests_Succeeded;
  87. long Calls_To_Noise, Calls_To_DNoise;
  88. /* SJA */
  89. long Shadow_Ray_Tests, Shadow_Rays_Succeeded, Shadow_Cache_Hits;
  90. /* SJA */
  91. long Reflected_Rays_Traced, Refracted_Rays_Traced;
  92. long Transmitted_Rays_Traced;
  93. long Istack_overflows;
  94. int Number_of_istacks;
  95. int Max_Intersections;
  96. ISTACK *free_istack;
  97.  
  98. DBL    tused;             /* Trace timer variables. - BP */ 
  99. time_t tstart, tstop;
  100.  
  101. char DisplayFormat, OutputFormat, VerboseFormat, PaletteOption, Color_Bits;
  102.  
  103. struct Constant_Struct Constants[MAX_CONSTANTS];
  104.  
  105. #ifdef NOCMDLINE    /* a main() by any other name... */
  106. #ifdef ALTMAIN
  107. void alt_main()
  108. #else
  109.   void main()
  110. #endif
  111. #else
  112. #ifdef ALTMAIN
  113.   void alt_main(argc, argv)
  114. #else
  115.   void main(argc, argv)
  116. #endif
  117.     int argc;
  118. char **argv;
  119. #endif            /* ...would be a lot less hassle!! :-) AAC */
  120.   {
  121.   register int i;
  122.   FILE *stat_file;
  123.  
  124.   STARTUP_POVRAY
  125.  
  126.   PRINT_CREDITS
  127.  
  128.   PRINT_OTHER_CREDITS
  129.  
  130.   /* Parse the command line parameters */
  131. #ifndef NOCMDLINE
  132.   if (argc == 1)
  133.     usage();
  134. #endif
  135.  
  136.   init_vars();
  137.  
  138.   Output_File_Name[0]='\0';
  139.  
  140.   Library_Paths[0] = NULL;
  141.   Library_Path_Index = 0;
  142.  
  143.   /* Read the default parameters from povray.def */
  144.   get_defaults();
  145.  
  146. #ifndef NOCMDLINE
  147.   for (i = 1 ; i < argc ; i++ )
  148.     if ((*argv[i] == '+') || (*argv[i] == '-'))
  149.       parse_option(argv[i]);
  150.     else
  151.       parse_file_name(argv[i]);
  152.  
  153. #endif
  154.  
  155.   if (Last_Line == -1)
  156.     Last_Line = Frame.Screen_Height;
  157.  
  158.   if (Last_Column == -1)
  159.     Last_Column = Frame.Screen_Width-1;
  160.  
  161.   if (Options & DISKWRITE) 
  162.     {
  163.     switch (OutputFormat) 
  164.     {
  165.     case '\0':
  166.     case 'd':
  167.     case 'D':
  168.       if ((Output_File_Handle = Get_Dump_File_Handle()) == NULL) 
  169.         {
  170.         close_all();
  171.         exit(1);
  172.         }
  173.       break;
  174.       /*
  175.          case 'i':
  176.          case 'I':
  177.                    if ((Output_File_Handle = Get_Iff_File_Handle()) == NULL) {
  178.                       close_all();
  179.                       exit(1);
  180.                       }
  181.                    break;
  182.  
  183. */
  184.     case 'r':
  185.     case 'R':
  186.       if ((Output_File_Handle = Get_Raw_File_Handle()) == NULL) 
  187.         {
  188.         close_all();
  189.         exit(1);
  190.         }
  191.       break;
  192.  
  193.     case 't':
  194.     case 'T':
  195.       if ((Output_File_Handle = Get_Targa_File_Handle()) == NULL) 
  196.         {
  197.         close_all();
  198.         exit(1);
  199.         }
  200.       break;
  201.  
  202.     default:
  203.       fprintf (stderr, "Unrecognized output file format %c\n", OutputFormat);
  204.       close_all(); 
  205.       exit(1);
  206.     }
  207.     if (Output_File_Name[0] == '\0')
  208.       strcpy (Output_File_Name, Default_File_Name (Output_File_Handle));
  209.     }
  210.  
  211.   Print_Options();
  212.  
  213.   Initialize_Tokenizer(Input_File_Name);
  214.   fprintf (stderr,"Parsing...");
  215.   if (Options & VERBOSE_FILE)
  216.     {
  217.     stat_file = fopen(Stat_File_Name,"w+t");
  218.     fprintf (stat_file, "Parsing...\n");
  219.     fclose(stat_file);
  220.     }
  221.  
  222.   Parse ();
  223.   Terminate_Tokenizer();
  224.  
  225.   if (Use_Slabs) 
  226.     {
  227.     fprintf (stderr, "Preprocessing...\n");  /* Added */
  228.     BuildBoundingSlabs(&Root_Object);        /* Added */
  229.     }
  230.  
  231.   if (Options & DISPLAY)
  232.     {
  233.     printf ("Displaying...\n");
  234.     display_init(Frame.Screen_Width, Frame.Screen_Height);
  235.     Display_Started = TRUE;
  236.     }
  237.  
  238.   /* Get things ready for ray tracing */
  239.   if (Options & DISKWRITE)
  240.     if (Options & CONTINUE_TRACE) 
  241.     {
  242.     if (Open_File (Output_File_Handle, Output_File_Name,
  243.       &Frame.Screen_Width, &Frame.Screen_Height, File_Buffer_Size,
  244.       READ_MODE) != 1) 
  245.       {
  246.       fprintf (stderr, "Error opening continue trace output file\n");
  247.       fprintf (stderr, "Opening new output file %s.\n",Output_File_Name);
  248.       Options &= ~CONTINUE_TRACE; /* Turn off continue trace */
  249.  
  250.       if (Open_File (Output_File_Handle, Output_File_Name,
  251.         &Frame.Screen_Width, &Frame.Screen_Height, File_Buffer_Size,
  252.         WRITE_MODE) != 1) 
  253.         {
  254.         fprintf (stderr, "Error opening output file\n");
  255.         close_all();
  256.         exit(1);
  257.         }
  258.       }
  259.  
  260.     Initialize_Renderer();
  261.     if (Options & CONTINUE_TRACE) 
  262.       Read_Rendered_Part();
  263.     }
  264.     else 
  265.     {
  266.     if (Open_File (Output_File_Handle, Output_File_Name,
  267.       &Frame.Screen_Width, &Frame.Screen_Height, File_Buffer_Size,
  268.       WRITE_MODE) != 1) 
  269.       {
  270.       fprintf (stderr, "Error opening output file\n");
  271.       close_all();
  272.       exit(1);
  273.       }
  274.  
  275.     Initialize_Renderer();
  276.     }
  277.   else
  278.     Initialize_Renderer();
  279.  
  280.   Initialize_Noise();
  281.  
  282.   START_TIME  /* Store start time for trace. Timer macro in CONFIG.H */
  283.  
  284.  
  285.   /* Ok, go for it - trace the picture */
  286.   if ((Options & VERBOSE) && (VerboseFormat !='1'))
  287.     printf ("Rendering...\n");
  288.   else if ((Options & VERBOSE) && (VerboseFormat=='1'))
  289.     fprintf (stderr,"POV-Ray rendering %s to %s :\n",Input_File_Name,Output_File_Name);
  290.   if (Options & VERBOSE_FILE)
  291.     {
  292.     stat_file = fopen(Stat_File_Name,"w+t");
  293.     fprintf (stat_file,"Parsed ok. Now rendering %s to %s :\n",Input_File_Name,Output_File_Name);
  294.     fclose(stat_file);
  295.     }
  296.   CONFIG_MATH               /* Macro for setting up any special FP options */
  297.   Start_Tracing ();
  298.  
  299.   if (Options & VERBOSE && VerboseFormat=='1')
  300.     fprintf (stderr,"\n");
  301.  
  302.   /* Record the time so well spent... */
  303.   STOP_TIME                  /* Get trace done time. */
  304.   tused = TIME_ELAPSED       /* Calc. elapsed time. Define TIME_ELAPSED as */
  305.   /* 0 in your specific CONFIG.H if unsupported */
  306.  
  307.   /* Clean up and leave */
  308.   display_finished();
  309.  
  310.   close_all ();
  311.  
  312.   PRINT_STATS
  313.  
  314.   if (Options & VERBOSE_FILE)
  315.     {
  316.     stat_file = fopen(Stat_File_Name,"a+t");
  317.     fprintf (stat_file,"Done Tracing\n");
  318.     fclose(stat_file);
  319.     }
  320.  
  321.   FINISH_POVRAY
  322.   }
  323.  
  324. /* Print out usage error message */
  325.  
  326. void usage ()
  327.   {
  328.   fprintf (stdout,"\nUsage: POVRAY  [+/-] Option1 [+/-] Option2 ...");
  329.   fprintf (stdout,"\n  Example: +L\\povray\\include +Iscene.pov +Oscene.tga +W320 +H200");
  330.   fprintf (stdout,"\n  Example: +Iscene.pov +Oscene.tga +W160 +H200 +V -D +X ");
  331.   fprintf (stdout,"\n");
  332.   fprintf (stdout,"\n[ Paused for keypress... ]\n");
  333.   WAIT_FOR_KEYPRESS;
  334.   fprintf (stdout,"\n\n\n\n\n Options:");
  335.   fprintf (stdout,"\n    Dxy = display in format x, using palette option y");
  336.   fprintf (stdout,"\n    V   = verbose messages on");
  337.   fprintf (stdout,"\n    P  = pause before exit");
  338.   fprintf (stdout,"\n    X  = enable early exit by key hit");
  339.   fprintf (stdout,"\n    Fx = write output file in format x");
  340.   fprintf (stdout,"\n         FT - Uncompressed Targa-24  |  FD - DKB/QRT Dump  | FR - RGB Raw Files");
  341.   fprintf (stdout,"\n    C  = continue aborted trace");
  342.   fprintf (stdout,"\n    Qx = image quality 0=rough, 9=full");
  343.   fprintf (stdout,"\n    A0.x = perform antialiasing");
  344.   fprintf (stdout,"\n    Bxxx = Use xxx KB for output file buffer");
  345.   fprintf (stdout,"\n    Jx.x = set aa-jitter amount");
  346.   fprintf (stdout,"\n    Kx.x = set frame clocK to x.x");
  347.   fprintf (stdout,"\n    MBxxx = use slabs if more than xxx objects");
  348.   fprintf (stdout,"\n    MSxxx = set max symbol table size to xxx");
  349.   fprintf (stdout,"\n    MVx.x = set compability to version x.x");
  350.   fprintf (stdout,"\n    Rn   = set aa-depth (use n X n rays/pixel)");
  351.   fprintf (stdout,"\n    SRxx = start at row xxx         |  SR0.xx start row at x%% of screen");
  352.   fprintf (stdout,"\n    ERxx = end   at row xxx         |  ER0.xx end   row at x%% of screen");
  353.   fprintf (stdout,"\n    SCxx = start at col xxx         |  SC0.xx start col at x%% of screen");
  354.   fprintf (stdout,"\n    ECxx = end   at col xxx         |  EC0.xx end   col at x%% of screen");
  355.   fprintf (stdout,"\n    I<filename> = input file name   |  O<filename> = output file name");
  356.   fprintf (stdout,"\n    L<pathname> = library path prefix");
  357.   fprintf (stdout,"\n");
  358.   exit(1);
  359.   }
  360. void init_vars()
  361.   {
  362.   Output_File_Handle = NULL;
  363.   File_Buffer_Size = 0;
  364.   Options = JITTER;
  365.   Quality_Flags = QUALITY_9;
  366.   Quality = 9;
  367.   Use_Slabs=TRUE;
  368.   Number_Of_Files = 0;
  369.   Frame.Screen_Height = 100;
  370.   Frame.Screen_Width = 100;
  371.   First_Line = 0;
  372.   Last_Line = -1;
  373.   First_Column = 0;
  374.   Last_Column = -1;
  375.  
  376.   First_Line_Temp = (DBL) First_Line;
  377.   Last_Line_Temp = (DBL) Last_Line;
  378.   First_Column_Temp = (DBL) First_Column ;
  379.   Last_Column_Temp = (DBL) Last_Column;
  380.  
  381.   Color_Bits = 8;
  382.  
  383.   Number_Of_Pixels = 0L;
  384.   Number_Of_Rays = 0L;
  385.   Number_Of_Pixels_Supersampled = 0L;
  386.   Ray_Ht_Field_Tests = 0L;
  387.   Ray_Ht_Field_Tests_Succeeded = 0L;
  388.   Ray_Ht_Field_Box_Tests = 0L;
  389.   Ray_HField_Box_Tests_Succeeded = 0L;
  390.   Ray_Bicubic_Tests = 0L;
  391.   Ray_Bicubic_Tests_Succeeded = 0L;
  392.   Ray_Blob_Tests = 0L;
  393.   Ray_Blob_Tests_Succeeded = 0L;
  394.   Ray_Box_Tests = 0L;
  395.   Ray_Box_Tests_Succeeded = 0L;
  396.   Ray_Disc_Tests = 0L;
  397.   Ray_Disc_Tests_Succeeded = 0L;
  398.   Ray_Cone_Tests = 0L;
  399.   Ray_Cone_Tests_Succeeded = 0L;
  400.   Ray_Sphere_Tests = 0L;
  401.   Ray_Sphere_Tests_Succeeded = 0L;
  402.   Ray_Plane_Tests = 0L;
  403.   Ray_Plane_Tests_Succeeded = 0L;
  404.   Ray_Triangle_Tests = 0L;
  405.   Ray_Triangle_Tests_Succeeded = 0L;
  406.   Ray_Quadric_Tests = 0L;
  407.   Ray_Quadric_Tests_Succeeded = 0L;
  408.   Ray_Poly_Tests = 0L;
  409.   Ray_Poly_Tests_Succeeded = 0L;
  410.   Bounding_Region_Tests = 0L;
  411.   Bounding_Region_Tests_Succeeded = 0L;
  412.   Clipping_Region_Tests = 0L;
  413.   Clipping_Region_Tests_Succeeded = 0L;
  414.   Calls_To_Noise = 0L;
  415.   Calls_To_DNoise = 0L;
  416.   Shadow_Ray_Tests = 0L;
  417.   /* SJA */
  418.   Shadow_Cache_Hits = 0L;
  419.   /* SJA */
  420.   Shadow_Rays_Succeeded = 0L;
  421.   Reflected_Rays_Traced = 0L;
  422.   Refracted_Rays_Traced = 0L;
  423.   Transmitted_Rays_Traced = 0L;
  424.   Istack_overflows = 0L;
  425.   Number_of_istacks = 0;
  426.   free_istack = NULL;
  427.   Max_Intersections = 64; /*128*/
  428.   Antialias_Threshold = 0.3;
  429.   strcpy (Input_File_Name, "object.pov");
  430.   return;
  431.   }
  432.  
  433. /* Close all the stuff that has been opened. */
  434. void close_all ()
  435.   {
  436.   if ((Options & DISPLAY) && Display_Started)
  437.     display_close();
  438.  
  439.   if (Output_File_Handle)
  440.     Close_File (Output_File_Handle);
  441.   }
  442.  
  443. /* Read the default parameters from povray.def */
  444. void get_defaults()
  445.   {
  446.   FILE *defaults_file;
  447.   char Option_String[256], *Option_String_Ptr;
  448.   /* READ_ENV_VAR_? should be defined in config.h */
  449.   /* Only one READ_ENV_VAR_? should ever be defined. */
  450.   /* This allows some machines to read environment variable before */
  451.   /* reading povray.def and others to do it after depending on the */
  452.   /* operating system. IBM-PC is before. Default is after if not */
  453.   /* defined in config.h. CDW 2/92 */
  454.   /* Set Diskwrite as default */
  455.   Options |= DISKWRITE;
  456.   OutputFormat = DEFAULT_OUTPUT_FORMAT;
  457.  
  458.   READ_ENV_VAR_BEFORE
  459.   if ((defaults_file = Locate_File("povray.def", "r")) != NULL) 
  460.     {
  461.     while (fgets(Option_String, 256, defaults_file) != NULL)
  462.       read_options(Option_String);
  463.     fclose (defaults_file);
  464.     }
  465.   READ_ENV_VAR_AFTER
  466.   }
  467.  
  468. void read_options (Option_Line)
  469. char *Option_Line;
  470.   {
  471.   register int c, String_Index, Option_Started;
  472.   short Option_Line_Index = 0;
  473.   char Option_String[80];
  474.  
  475.   String_Index = 0;
  476.   Option_Started = FALSE;
  477.   while ((c = Option_Line[Option_Line_Index++]) != '\0')
  478.     {
  479.     if (Option_Started)
  480.       if (isspace(c))
  481.         {
  482.         Option_String[String_Index] = '\0';
  483.         parse_option (Option_String);
  484.         Option_Started = FALSE;
  485.         String_Index = 0;
  486.         }
  487.       else
  488.         Option_String[String_Index++] = (char) c;
  489.  
  490.     else /* Option_Started */
  491.       if ((c == (int) '-') || (c == (int) '+'))
  492.         {
  493.         String_Index = 0;
  494.         Option_String[String_Index++] = (char) c;
  495.         Option_Started = TRUE;
  496.         }
  497.       else
  498.         if (!isspace(c))
  499.           {
  500.           fprintf (stderr, 
  501.             "\nCommand line or .DEF error. Bad character (%c), val: %d.\n", 
  502.             (char) c, c);
  503.           exit (1);
  504.           }
  505.     }
  506.  
  507.   if (Option_Started)
  508.     {
  509.     Option_String[String_Index] = '\0';
  510.     parse_option (Option_String);
  511.     }
  512.   }
  513.  
  514. /* parse the command line parameters */
  515. void parse_option (Option_String)
  516. char *Option_String;
  517.   {
  518.   register int Add_Option;
  519.   unsigned int Option_Number;
  520.   long bounds_thresh;
  521.   DBL threshold;
  522.  
  523.   inflag = outflag = FALSE;   /* if these flags aren't immediately used, reset them on next -/+ option! */
  524.   if (*(Option_String++) == '-')
  525.     Add_Option = FALSE;
  526.   else
  527.     Add_Option = TRUE;
  528.  
  529.   Option_Number = 0;
  530.  
  531.   switch (*Option_String)
  532.   {
  533.   case 'B':
  534.   case 'b':  
  535.     sscanf (&Option_String[1], "%d", &File_Buffer_Size);
  536.     File_Buffer_Size *= 1024;
  537.     if (File_Buffer_Size < BUFSIZ)   /* system default MIN */
  538.       File_Buffer_Size = BUFSIZ;
  539.     if (File_Buffer_Size > MAX_BUFSIZE)    /* unsigned short MAX */
  540.       File_Buffer_Size = MAX_BUFSIZE;
  541.     break;
  542.  
  543.   case 'C':
  544.   case 'c':  
  545.     Option_Number = CONTINUE_TRACE;
  546.     break;
  547.  
  548.   case 'D':
  549.   case 'd':  
  550.     Option_Number = DISPLAY;
  551.     DisplayFormat = '0';
  552.     PaletteOption = '3';
  553.     if (Option_String[1] != '\0')
  554.       DisplayFormat = (char)toupper(Option_String[1]);
  555.  
  556.     if (Option_String[1] != '\0' && Option_String[2] != '\0')
  557.       PaletteOption = (char)toupper(Option_String[2]);
  558.     break;
  559.  
  560.   case '@':  
  561.     Option_Number = VERBOSE_FILE;
  562.     if(Option_String[1] == '\0')
  563.       strcpy(Stat_File_Name, "POVSTAT.OUT");
  564.     else
  565.       strncpy (Stat_File_Name, &Option_String[1], FILE_NAME_LENGTH);
  566.     break;
  567.  
  568.   case 'V':
  569.   case 'v':  
  570.     Option_Number = VERBOSE;
  571.     VerboseFormat = (char)toupper(Option_String[1]);
  572.     if (VerboseFormat == '\0')
  573.       VerboseFormat = '1';
  574.     break;
  575.  
  576.   case 'W':
  577.   case 'w':  
  578.     sscanf (&Option_String[1], "%d", &Frame.Screen_Width);
  579.     break;
  580.  
  581.   case 'H':
  582.   case 'h':  
  583.     sscanf (&Option_String[1], "%d", &Frame.Screen_Height);
  584.     break;
  585.  
  586.   case 'F':
  587.   case 'f':  
  588.     Option_Number = DISKWRITE;
  589.     if (isupper(Option_String[1]))
  590.       OutputFormat = (char)tolower(Option_String[1]);
  591.     else
  592.       OutputFormat = Option_String[1];
  593.  
  594.     /* Default the output format to the default in the config file */
  595.     if (OutputFormat == '\0')
  596.       OutputFormat = DEFAULT_OUTPUT_FORMAT;
  597.     break;
  598.  
  599.   case 'P':
  600.   case 'p':  
  601.     Option_Number = PROMPTEXIT;
  602.     break;
  603.  
  604.   case 'I':
  605.   case 'i':  
  606.     if (Option_String[1] == '\0')
  607.       inflag = TRUE;
  608.     else 
  609.       strncpy (Input_File_Name, &Option_String[1], FILE_NAME_LENGTH);
  610.     break;
  611.  
  612.   case 'O':
  613.   case 'o':  
  614.     if (Option_String[1] == '\0')
  615.       outflag = TRUE;
  616.     else
  617.       strncpy (Output_File_Name, &Option_String[1], FILE_NAME_LENGTH);
  618.     break;
  619.  
  620.   case 'A':
  621.   case 'a':  
  622.     Option_Number = ANTIALIAS;
  623.     if (sscanf (&Option_String[1], DBL_FORMAT_STRING, &threshold) != EOF)
  624.       Antialias_Threshold = threshold;
  625.     break;
  626.  
  627.   case 'J':
  628.   case 'j':
  629.     Option_Number = JITTER;
  630.     if (sscanf (&Option_String[1], DBL_FORMAT_STRING, &threshold) != EOF)
  631.        JitterScale = threshold;
  632.     if (JitterScale<=0.0)
  633.        Add_Option = FALSE;
  634.     break; 
  635.  
  636.   case 'R':
  637.   case 'r':  
  638.     sscanf (&Option_String[1], "%ld", &AntialiasDepth);
  639.     if (AntialiasDepth < 1)
  640.       AntialiasDepth = 1;
  641.     if (AntialiasDepth > 9)
  642.       AntialiasDepth = 9;
  643.     break;
  644.  
  645.   case 'X':
  646.   case 'x':  
  647.     Option_Number = EXITENABLE;
  648.     break;
  649.  
  650.   case 'L':
  651.   case 'l':  
  652.     if (Library_Path_Index >= MAX_LIBRARIES) 
  653.       {
  654.       fprintf (stderr, "Too many library directories specified\n");
  655.       exit(1);
  656.       }
  657.     Library_Paths [Library_Path_Index] = malloc (strlen(Option_String));
  658.     if (Library_Paths [Library_Path_Index] == NULL) 
  659.       {
  660.       fprintf (stderr, "Out of memory. Cannot allocate memory for library pathname\n");
  661.       exit(1);
  662.       }
  663.     strcpy (Library_Paths [Library_Path_Index], &Option_String[1]);
  664.     Library_Path_Index++;
  665.     break;
  666.  
  667.   case 'T':
  668.   case 't':  
  669.     switch (toupper(Option_String[1]))
  670.     {
  671.     case 'Y':
  672.       Case_Sensitive_Flag = 0;
  673.       break;
  674.     case 'N':
  675.       Case_Sensitive_Flag = 1;
  676.       break;
  677.     case 'O':
  678.       Case_Sensitive_Flag = 2;
  679.       break;
  680.     default:
  681.       Case_Sensitive_Flag = 2;
  682.       break;
  683.     }  
  684.     break;
  685.  
  686.   case 'S':
  687.   case 's':  
  688.     switch (Option_String[1])
  689.     {
  690.     case 'c': 
  691.     case 'C':
  692.       sscanf (&Option_String[2], DBL_FORMAT_STRING, &First_Column_Temp);
  693.       break;
  694.  
  695.     case 'r': 
  696.     case 'R':
  697.       sscanf (&Option_String[2], DBL_FORMAT_STRING, &First_Line_Temp);
  698.       break;
  699.  
  700.     default:
  701.       sscanf (&Option_String[1], DBL_FORMAT_STRING, &First_Line_Temp);
  702.       break;
  703.     }
  704.  
  705.     if(First_Column_Temp > 0.0 && First_Column_Temp < 1.0)
  706.       First_Column = (int) (Frame.Screen_Width * First_Column_Temp);
  707.     else
  708.       First_Column = (int) First_Column_Temp;
  709.  
  710.     if(First_Line_Temp > 0.0 && First_Line_Temp < 1.0)
  711.       First_Line = (int) (Frame.Screen_Height * First_Line_Temp);
  712.     else
  713.       First_Line = (int) First_Line_Temp;
  714.  
  715.     if (First_Column < 0)
  716.       First_Column = 0;
  717.  
  718.     if (First_Line < 0)
  719.       First_Line = 0;
  720.  
  721.     break;
  722.  
  723.   case 'E':
  724.   case 'e': 
  725.     switch (Option_String[1])
  726.     {
  727.     case 'c': 
  728.     case 'C':
  729.       sscanf (&Option_String[2], DBL_FORMAT_STRING, &Last_Column_Temp);
  730.       break;
  731.  
  732.     case 'r': 
  733.     case 'R':
  734.       sscanf (&Option_String[2], DBL_FORMAT_STRING, &Last_Line_Temp);
  735.       break;
  736.  
  737.     default:
  738.       sscanf (&Option_String[1], DBL_FORMAT_STRING, &Last_Line_Temp);
  739.       break;
  740.     }
  741.  
  742.     if(Last_Column_Temp > 0.0 && Last_Column_Temp < 1.0)
  743.       Last_Column = (int) (Frame.Screen_Width * Last_Column_Temp);
  744.     else
  745.       Last_Column = (int) Last_Column_Temp;
  746.  
  747.     if(Last_Line_Temp > 0.0 && Last_Line_Temp < 1.0)
  748.       Last_Line = (int) (Frame.Screen_Height * Last_Line_Temp);
  749.     else
  750.       Last_Line = (int) Last_Line_Temp;
  751.  
  752.     if (Last_Column < 0 || Last_Column >= Frame.Screen_Width)
  753.       Last_Column = Frame.Screen_Width - 1;
  754.  
  755.     if (Last_Line > Frame.Screen_Height)
  756.       Last_Line = Frame.Screen_Height;
  757.  
  758.     break;
  759.  
  760.   case 'M': /* Switch used so other max values can be inserted easily */
  761.   case 'm':  
  762.     switch (Option_String[1])
  763.     {
  764.     case 's': /* Max Symbols */
  765.     case 'S':
  766.       sscanf (&Option_String[2], "%d", &Max_Symbols);
  767.       break;
  768.     case 'v': /* Max Version */
  769.     case 'V':
  770.       sscanf (&Option_String[2], DBL_FORMAT_STRING, &Language_Version);
  771.       break;
  772.     case 'b': /* Min Bounded */
  773.     case 'B':
  774.       if (sscanf (&Option_String[2], "%d", &bounds_thresh) != EOF)
  775.         Bounds_Threshold=bounds_thresh;
  776.       Use_Slabs = Add_Option;
  777.       break;
  778.     default:
  779.       break;
  780.     }
  781.     break;
  782.  
  783.   case 'Q':
  784.   case 'q':
  785.     sscanf (&Option_String[1], "%d", &Quality);
  786.     if ((Quality < 0) || (Quality > 9))
  787.       Error("Illegal +Q switch setting");
  788.     Quality_Flags = Quality_Values[Quality];
  789.     break;
  790.  
  791.     /* Turn on debugging print statements. */
  792.   case 'Z':
  793.   case 'z':  
  794.     Option_Number = DEBUGGING;
  795.     break;
  796.  
  797.     /* +Y switch to remain undocumented.  Add to +Q later */
  798.   case 'Y':
  799.   case 'y':  
  800.     Use_Slabs = Add_Option;
  801.     break;
  802.  
  803.   case 'K':
  804.   case 'k': 
  805.     sscanf (&Option_String[1], DBL_FORMAT_STRING, &Clock_Value);
  806.     break;
  807.  
  808.   default:   
  809.     fprintf (stderr, "\nInvalid option: %s\n\n", --Option_String);
  810.   }
  811.  
  812.   if (Option_Number != 0)
  813.     if (Add_Option)
  814.       Options |= Option_Number;
  815.     else Options &= ~Option_Number;
  816.   }
  817.  
  818.   void Print_Options()
  819.     {
  820.     int i;
  821.  
  822.     fprintf (stdout,"\nPOV-Ray Options in effect: ");
  823.  
  824.     if (Options & CONTINUE_TRACE)
  825.       fprintf (stdout,"+c ");
  826.  
  827.     if (Options & DISPLAY)
  828.       fprintf (stdout,"+d%c%c ", DisplayFormat, PaletteOption);
  829.  
  830.     if (Options & VERBOSE)
  831.       fprintf (stdout,"+v%c ", VerboseFormat);
  832.  
  833.     if (Options & VERBOSE_FILE)
  834.       fprintf (stdout,"+@%s ", Stat_File_Name);
  835.  
  836.     if (Options & DISKWRITE)
  837.       fprintf (stdout,"+f%c ", OutputFormat);
  838.  
  839.     if (Options & PROMPTEXIT)
  840.       fprintf (stdout,"+p ");
  841.  
  842.     if (Options & EXITENABLE)
  843.       fprintf (stdout,"+x ");
  844.  
  845.     if (Use_Slabs)
  846.       fprintf (stdout,"+mb%d ", Bounds_Threshold);
  847.     else
  848.       fprintf (stdout,"-mb ");
  849.  
  850.     if (Options & DEBUGGING)
  851.       fprintf (stdout,"+z ");
  852.  
  853.     if (File_Buffer_Size != 0)
  854.       fprintf (stdout,"-b%d ", File_Buffer_Size/1024);
  855.  
  856.     if (Options & ANTIALIAS)
  857.       {
  858.       fprintf (stdout,"+a%.3f ", Antialias_Threshold);
  859.       if (Options & JITTER)
  860.         fprintf (stdout,"+j%.3f ", JitterScale);
  861.       fprintf (stdout,"+r%ld ",AntialiasDepth);
  862.       }
  863.  
  864.     /* quality flags rewrite in progress by CEY */
  865.  
  866.     fprintf (stdout,"-q%d -w%d -h%d -s%d -e%d\n",Quality,
  867.       Frame.Screen_Width, Frame.Screen_Height, First_Line, Last_Line);
  868.  
  869.     fprintf (stdout, "-k%.3f -mv%.1f -i%s ", Clock_Value, Language_Version, 
  870.       Input_File_Name);
  871.  
  872.     if (Options & DISKWRITE)
  873.       fprintf (stdout,"-o%s ", Output_File_Name);
  874.  
  875.     for (i = 0 ; i < Library_Path_Index ; i++)
  876.       fprintf (stdout,"-l%s ", Library_Paths[i]);
  877.  
  878.     fprintf (stdout,"\n");
  879.     }
  880.  
  881. void parse_file_name (File_Name)
  882. char *File_Name;
  883.   {
  884.   FILE *defaults_file;
  885.   char Option_String[256];
  886.  
  887.   if (inflag)   /* file names may now be separated by spaces from cmdline option */
  888.     {
  889.     strncpy (Input_File_Name, File_Name, FILE_NAME_LENGTH);
  890.     inflag = FALSE;
  891.     return;
  892.     }
  893.  
  894.   if (outflag)  /* file names may now be separated by spaces from cmdline option */
  895.     {
  896.     strncpy (Output_File_Name, File_Name, FILE_NAME_LENGTH);
  897.     outflag = FALSE;
  898.     return;
  899.     }
  900.  
  901.  
  902.   if (++Number_Of_Files > MAX_FILE_NAMES)
  903.     {
  904.     fprintf (stderr, "\nOnly %d option file names are allowed in a command line.", 
  905.       MAX_FILE_NAMES);
  906.     exit(1);
  907.     }
  908.  
  909.   if ((defaults_file = Locate_File(File_Name, "r")) != NULL) 
  910.     {
  911.     while (fgets(Option_String, 256, defaults_file) != NULL)
  912.       read_options(Option_String);
  913.     fclose (defaults_file);
  914.     }
  915.   else
  916.     printf("\nError opening option file %s.",File_Name);  
  917.   }
  918.  
  919. void print_stats()
  920.   {
  921.   long hours,min;
  922.   DBL sec;
  923.   FILE *stat_out;
  924.   long Pixels_In_Image;
  925.  
  926.   if (Options & VERBOSE_FILE)
  927.     stat_out = fopen(Stat_File_Name,"w+t");
  928.   else
  929.     stat_out = stdout;  
  930.  
  931.   Pixels_In_Image = (long)Frame.Screen_Width * (long)Frame.Screen_Height;
  932.  
  933.  
  934.   fprintf (stat_out,"\n%s statistics\n",Input_File_Name);
  935.   if(Pixels_In_Image > Number_Of_Pixels)
  936.     fprintf (stat_out,"  Partial Image Rendered");
  937.  
  938.   fprintf (stat_out,"--------------------------------------\n");
  939.   fprintf (stat_out,"Resolution %d x %d\n",Frame.Screen_Width, Frame.Screen_Height);
  940.   fprintf (stat_out,"# Rays:  %10ld    # Pixels:  %10ld  # Pixels supersampled: %10ld\n",
  941.     Number_Of_Rays, Number_Of_Pixels, Number_Of_Pixels_Supersampled);
  942.  
  943.   fprintf (stat_out,"  Ray->Shape Intersection Tests:\n");
  944.   fprintf (stat_out,"   Type             Tests    Succeeded   Percentage\n");
  945.   fprintf (stat_out,"  -----------------------------------------------------------\n");
  946.   if(Ray_Sphere_Tests)
  947.     fprintf (stat_out,"  Sphere       %10ld  %10ld  %10.2f\n", Ray_Sphere_Tests, Ray_Sphere_Tests_Succeeded, ( ((DBL)Ray_Sphere_Tests_Succeeded/(DBL)Ray_Sphere_Tests) *100.0 ) );
  948.   if(Ray_Plane_Tests)
  949.     fprintf (stat_out,"  Plane        %10ld  %10ld  %10.2f\n", Ray_Plane_Tests, Ray_Plane_Tests_Succeeded, ( ((DBL)Ray_Plane_Tests_Succeeded/(DBL)Ray_Plane_Tests) *100.0 ));
  950.   if(Ray_Triangle_Tests)
  951.     fprintf (stat_out,"  Triangle     %10ld  %10ld  %10.2f\n", Ray_Triangle_Tests, Ray_Triangle_Tests_Succeeded, ( ((DBL)Ray_Triangle_Tests_Succeeded/(DBL)Ray_Triangle_Tests) *100.0 ));
  952.   if(Ray_Quadric_Tests)
  953.     fprintf (stat_out,"  Quadric      %10ld  %10ld  %10.2f\n", Ray_Quadric_Tests, Ray_Quadric_Tests_Succeeded, ( ((DBL)Ray_Quadric_Tests_Succeeded/(DBL)Ray_Quadric_Tests) *100.0 ));
  954.   if(Ray_Blob_Tests)
  955.     fprintf (stat_out,"  Blob         %10ld  %10ld  %10.2f\n", Ray_Blob_Tests, Ray_Blob_Tests_Succeeded, ( ((DBL)Ray_Blob_Tests_Succeeded/(DBL)Ray_Blob_Tests) *100.0 ));
  956.   if(Ray_Box_Tests)
  957.     fprintf (stat_out,"  Box          %10ld  %10ld  %10.2f\n", Ray_Box_Tests, Ray_Box_Tests_Succeeded, ( ((DBL)Ray_Box_Tests_Succeeded/(DBL)Ray_Box_Tests) *100.0 ));
  958.   if(Ray_Cone_Tests)
  959.     fprintf (stat_out,"  Cone         %10ld  %10ld  %10.2f\n", Ray_Cone_Tests, Ray_Cone_Tests_Succeeded, ( ((DBL)Ray_Cone_Tests_Succeeded/(DBL)Ray_Cone_Tests) *100.0 ) );
  960.   if(Ray_Disc_Tests)
  961.     fprintf (stat_out,"  Disc         %10ld  %10ld  %10.2f\n", Ray_Disc_Tests, Ray_Disc_Tests_Succeeded, ( ((DBL)Ray_Disc_Tests_Succeeded/(DBL)Ray_Disc_Tests) *100.0 ) );
  962.   if(Ray_Poly_Tests)
  963.     fprintf (stat_out,"  Quartic\\Poly %10ld  %10ld  %10.2f\n", Ray_Poly_Tests, Ray_Poly_Tests_Succeeded, ( ((DBL)Ray_Poly_Tests_Succeeded/(DBL)Ray_Poly_Tests) *100.0 ));
  964.   if(Ray_Bicubic_Tests)
  965.     fprintf (stat_out,"  Bezier Patch %10ld  %10ld  %10.2f\n", Ray_Bicubic_Tests, Ray_Bicubic_Tests_Succeeded, ( ((DBL)Ray_Bicubic_Tests_Succeeded/(DBL)Ray_Bicubic_Tests) *100.0 ));
  966.   if(Ray_Ht_Field_Tests)
  967.     fprintf (stat_out,"  Height Fld   %10ld  %10ld  %10.2f\n", Ray_Ht_Field_Tests, Ray_Ht_Field_Tests_Succeeded, ( ((DBL)Ray_Ht_Field_Tests_Succeeded/(DBL)Ray_Ht_Field_Tests) *100.0 ));
  968.   if(Ray_Ht_Field_Box_Tests)
  969.     fprintf (stat_out,"  Hght Fld Box %10ld  %10ld  %10.2f\n", Ray_Ht_Field_Box_Tests, Ray_HField_Box_Tests_Succeeded, ( ((DBL)Ray_HField_Box_Tests_Succeeded/(DBL)Ray_Ht_Field_Box_Tests) *100.0 ));
  970.   if(Bounding_Region_Tests)
  971.     fprintf (stat_out,"  Bounds       %10ld  %10ld  %10.2f\n", Bounding_Region_Tests, Bounding_Region_Tests_Succeeded, ( ((DBL)Bounding_Region_Tests_Succeeded/(DBL)Bounding_Region_Tests) *100.0 ));
  972.   if(Clipping_Region_Tests)
  973.     fprintf (stat_out,"  Clips        %10ld  %10ld  %10.2f\n", Clipping_Region_Tests, Clipping_Region_Tests_Succeeded, ( ((DBL)Clipping_Region_Tests_Succeeded/(DBL)Clipping_Region_Tests) *100.0 ));
  974.  
  975.   if(Calls_To_Noise) 
  976.  
  977.     fprintf (stat_out,"  Calls to Noise:   %10ld\n", Calls_To_Noise);
  978.   if(Calls_To_DNoise)
  979.     fprintf (stat_out,"  Calls to DNoise:  %10ld\n", Calls_To_DNoise);
  980.   if(Shadow_Ray_Tests)
  981.     fprintf (stat_out,"  Shadow Ray Tests: %10ld     Blocking Objects Found:  %10ld\n",
  982.       Shadow_Ray_Tests, Shadow_Rays_Succeeded);
  983.   if(Reflected_Rays_Traced)
  984.     fprintf (stat_out,"  Reflected Rays:   %10ld\n", Reflected_Rays_Traced);
  985.   if(Refracted_Rays_Traced)
  986.     fprintf (stat_out,"  Refracted Rays:   %10ld\n", Refracted_Rays_Traced);
  987.   if(Transmitted_Rays_Traced)
  988.     fprintf (stat_out,"  Transmitted Rays: %10ld\n", Transmitted_Rays_Traced);
  989.   if(Istack_overflows)
  990.     fprintf (stat_out,"  I-Stack overflows:%10ld\n", Istack_overflows);
  991.  
  992.   if(tused==0) 
  993.     {
  994.     STOP_TIME                  /* Get trace done time. */
  995.     tused = TIME_ELAPSED       /* Calc. elapsed time. Define TIME_ELAPSED as */
  996.     /* 0 in your specific CONFIG.H if unsupported */
  997.     }
  998.   if (tused != 0)
  999.     {
  1000.     /* Convert seconds to hours, min & sec. CdW */
  1001.     hours = (long) tused/3600;
  1002.     min = (long) (tused - hours*3600)/60;
  1003.     sec = tused - (DBL) (hours*3600 + min*60);
  1004.     fprintf (stat_out,"  Time For Trace:   %2ld hours %2ld minutes %4.2f seconds\n", hours,min,sec); 
  1005.     }
  1006.   if (Options & VERBOSE_FILE)
  1007.     fclose(stat_out);
  1008.  
  1009.   }
  1010.  
  1011. /* Find a file in the search path. */
  1012.  
  1013. FILE *Locate_File (filename, mode)
  1014. char *filename, *mode;
  1015.   {
  1016.   FILE *f;
  1017.   int i;
  1018.   char pathname[FILE_NAME_LENGTH];
  1019.  
  1020.   /* Check the current directory first. */
  1021.   if ((f = fopen (filename, mode)) != NULL)
  1022.     return (f);
  1023.  
  1024.   for (i = 0 ; i < Library_Path_Index ; i++) 
  1025.     {
  1026.     strcpy (pathname, Library_Paths[i]);
  1027.     if (FILENAME_SEPARATOR != NULL)
  1028.       strcat (pathname, FILENAME_SEPARATOR);
  1029.     strcat (pathname, filename);
  1030.     if ((f = fopen (pathname, mode)) != NULL)
  1031.       return (f);
  1032.     }
  1033.  
  1034.   return (NULL);
  1035.   }
  1036. void print_credits()
  1037.   {
  1038.   fprintf (stderr,"\n");
  1039.   fprintf (stderr,"  Persistence of Vision Raytracer Ver %s%s\n",POV_RAY_VERSION,COMPILER_VER);
  1040.   fprintf (stderr,"    %s\n",DISTRIBUTION_MESSAGE_1);
  1041.   fprintf (stderr,"     %s\n",DISTRIBUTION_MESSAGE_2);
  1042.   fprintf (stderr,"     %s\n",DISTRIBUTION_MESSAGE_3);
  1043.   fprintf (stderr,"  Copyright 1993 POV-Team\n");
  1044.   fprintf (stderr,"  ----------------------------------------------------------------------\n");
  1045.   fprintf (stderr,"  POV-Ray is based on DKBTrace 2.12 by David K. Buck & Aaron A. Collins.\n");
  1046.   fprintf (stderr,"    Contributing Authors: (Alphabetically)\n");
  1047.   fprintf (stderr,"       Steve Anger        Steve A. Bennett   David K. Buck\n");
  1048.   fprintf (stderr,"       Aaron A. Collins   Alexander Enzmann  Dan Farmer\n");
  1049.   fprintf (stderr,"       Douglas Muir       Bill Pulver        Robert Skinner\n");
  1050.   fprintf (stderr,"       Scott Taylor       Drew Wells         Chris Young\n");
  1051.   fprintf (stderr,"    Other contributors listed in the documentation.\n");
  1052.   fprintf (stderr,"  ----------------------------------------------------------------------\n");
  1053.   }
  1054.