home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / etc / hf / a2hf.c next >
Text File  |  1992-05-04  |  1KB  |  66 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  * simple ascii to rayshade height field converter.
  5.  * reads standard in, writes standard out.
  6.  * each number must be on a line by itself.
  7.  *
  8.  * Rusty Wright
  9.  * rusty@garnet.berkeley.edu
  10.  */
  11.  
  12. #ifdef __WATCOMC__
  13. #include <fcntl.h>
  14. #endif
  15.  
  16. main(int argc,char **argv) {
  17.     char    buf[BUFSIZ];
  18.     int    length, line;
  19.     float    height;
  20.  
  21.  
  22.     if (argc!=1) {
  23.         printf("Convert ascii file to .hf file for RayShade.\n\n");
  24.         printf("usage:  a2hf < infile > outfile\n\n");
  25.         exit(1);
  26.         }
  27.  
  28. #ifdef __WATCOMC__
  29.         setmode(stdin->_handle,O_BINARY);
  30.         setmode(stdout->_handle,O_BINARY);
  31. #endif
  32.  
  33.     if (fgets(buf, sizeof(buf), stdin) == NULL) {
  34.         fprintf(stderr, "premature eof on stdin\n");
  35.         exit(1);
  36.     }
  37.  
  38.     line = 1;
  39.  
  40.     if (sscanf(buf, "%d", &length) != 1) {
  41.         fprintf(stderr, "format error line %d\n", line);
  42.         exit(1);
  43.     }
  44.  
  45.     if (fwrite((char *) &length, sizeof(length), 1, stdout) != 1) {
  46.         perror("write");
  47.         exit(1);
  48.     }
  49.  
  50.     while (fgets(buf, sizeof(buf), stdin) != NULL) {
  51.         line++;
  52.  
  53.         if (sscanf(buf, "%g", &height) != 1) {
  54.             fprintf(stderr, "format error line %d\n", line);
  55.             exit(1);
  56.         }
  57.  
  58.         if (fwrite((char *) &height, sizeof(height), 1, stdout) != 1) {
  59.             perror("write");
  60.             exit(1);
  61.         }
  62.     }
  63.  
  64.     exit(0);
  65. }
  66.