Back to index

radiance  4R0+20100331
lookamb.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: lookamb.c,v 2.10 2004/03/30 16:13:01 schorsch Exp $";
00003 #endif
00004 /*
00005  *  lookamb.c - program to examine ambient components.
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 #include  "platform.h"
00011 #include  "ray.h"
00012 #include  "ambient.h"
00013 #include  "resolu.h"
00014 
00015 
00016 int  dataonly = 0;
00017 int  header = 1;
00018 int  reverse = 0;
00019 
00020 AMBVAL  av;
00021 
00022 
00023 int
00024 main(         /* load ambient values from a file */
00025        int  argc,
00026        char  *argv[]
00027 )
00028 {
00029        FILE  *fp;
00030        int  i;
00031 
00032        for (i = 1; i < argc; i++)
00033               if (argv[i][0] == '-')
00034                      switch (argv[i][1]) {
00035                      case 'd':
00036                             dataonly = 1;
00037                             break;
00038                      case 'r':
00039                             reverse = 1;
00040                             break;
00041                      case 'h':
00042                             header = 0;
00043                             break;
00044                      default:
00045                             fprintf(stderr, "%s: unknown option '%s'\n",
00046                                           argv[0], argv[i]);
00047                             return(1);
00048                      }
00049               else
00050                      break;
00051 
00052        if (i >= argc)
00053               fp = stdin;
00054        else if ((fp = fopen(argv[i], "r")) == NULL) {
00055               fprintf(stderr, "%s: file not found\n", argv[i]);
00056               return(1);
00057        }
00058        if (reverse) {
00059               if (header) {
00060                      if (checkheader(fp, "ascii", stdout) < 0)
00061                             goto formaterr;
00062               } else {
00063                      newheader("RADIANCE", stdout);
00064                      printargs(argc, argv, stdout);
00065               }
00066               fputformat(AMBFMT, stdout);
00067               putchar('\n');
00068               SET_FILE_BINARY(stdout);
00069               putambmagic(stdout);
00070               writamb(fp);
00071        } else {
00072               SET_FILE_BINARY(fp);
00073               if (checkheader(fp, AMBFMT, header ? stdout : (FILE *)NULL) < 0)
00074                      goto formaterr;
00075               if (!hasambmagic(fp))
00076                      goto formaterr;
00077               if (header) {
00078                      fputformat("ascii", stdout);
00079                      putchar('\n');
00080               }
00081               lookamb(fp);
00082        }
00083        fclose(fp);
00084        return(0);
00085 formaterr:
00086        fprintf(stderr, "%s: format error on input\n", argv[0]);
00087        exit(1);
00088 }
00089 
00090 
00091 extern void
00092 lookamb(                    /* get ambient values from a file */
00093        FILE  *fp
00094 )
00095 {
00096        while (readambval(&av, fp)) {
00097               if (dataonly) {
00098                      printf("%f\t%f\t%f\t", av.pos[0], av.pos[1], av.pos[2]);
00099                      printf("%f\t%f\t%f\t", av.dir[0], av.dir[1], av.dir[2]);
00100                      printf("%d\t%f\t%f\t", av.lvl, av.weight, av.rad);
00101                      printf("%e\t%e\t%e\t", colval(av.val,RED),
00102                                           colval(av.val,GRN),
00103                                           colval(av.val,BLU));
00104                      printf("%f\t%f\t%f\t", av.gpos[0],
00105                                    av.gpos[1], av.gpos[2]);
00106                      printf("%f\t%f\t%f\n", av.gdir[0],
00107                                    av.gdir[1], av.gdir[2]);
00108               } else {
00109                      printf("\nPosition:\t%f\t%f\t%f\n", av.pos[0],
00110                                    av.pos[1], av.pos[2]);
00111                      printf("Direction:\t%f\t%f\t%f\n", av.dir[0],
00112                                    av.dir[1], av.dir[2]);
00113                      printf("Lvl,Wt,Rad:\t%d\t\t%f\t%f\n", av.lvl,
00114                                    av.weight, av.rad);
00115                      printf("Value:\t\t%e\t%e\t%e\n", colval(av.val,RED),
00116                                    colval(av.val,GRN), colval(av.val,BLU));
00117                      printf("Pos.Grad:\t%f\t%f\t%f\n", av.gpos[0],
00118                                    av.gpos[1], av.gpos[2]);
00119                      printf("Dir.Grad:\t%f\t%f\t%f\n", av.gdir[0],
00120                                    av.gdir[1], av.gdir[2]);
00121               }
00122               if (ferror(stdout))
00123                      exit(1);
00124        }
00125 }
00126 
00127 
00128 extern void
00129 writamb(                    /* write binary ambient values */
00130        FILE  *fp
00131 )
00132 {
00133        for ( ; ; ) {
00134               if (!dataonly)
00135                      fscanf(fp, "%*s");
00136               if (fscanf(fp, "%f %f %f",
00137                             &av.pos[0], &av.pos[1], &av.pos[2]) != 3)
00138                      return;
00139               if (!dataonly)
00140                      fscanf(fp, "%*s");
00141               if (fscanf(fp, "%f %f %f",
00142                             &av.dir[0], &av.dir[1], &av.dir[2]) != 3)
00143                      return;
00144               if (!dataonly)
00145                      fscanf(fp, "%*s");
00146               if (fscanf(fp, "%d %f %f",
00147                             &av.lvl, &av.weight, &av.rad) != 3)
00148                      return;
00149               if (!dataonly)
00150                      fscanf(fp, "%*s");
00151               if (fscanf(fp, "%f %f %f",
00152                             &av.val[RED], &av.val[GRN], &av.val[BLU]) != 3)
00153                      return;
00154               if (!dataonly)
00155                      fscanf(fp, "%*s");
00156               if (fscanf(fp, "%f %f %f",
00157                             &av.gpos[0], &av.gpos[1], &av.gpos[2]) != 3)
00158                      return;
00159               if (!dataonly)
00160                      fscanf(fp, "%*s");
00161               if (fscanf(fp, "%f %f %f",
00162                             &av.gdir[0], &av.gdir[1], &av.gdir[2]) != 3)
00163                      return;
00164               av.next = NULL;
00165               writambval(&av, stdout);
00166               if (ferror(stdout))
00167                      exit(1);
00168        }
00169 }