| 1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* lookamb.c - program to examine ambient components. |
| 9 |
– |
* |
| 10 |
– |
* 10/8/86 |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
< |
#include <stdio.h> |
| 8 |
> |
#include "copyright.h" |
| 9 |
|
|
| 10 |
< |
#include "color.h" |
| 10 |
> |
#include "platform.h" |
| 11 |
> |
#include "ray.h" |
| 12 |
> |
#include "ambient.h" |
| 13 |
> |
#include "resolu.h" |
| 14 |
|
|
| 15 |
|
|
| 18 |
– |
typedef struct ambval { |
| 19 |
– |
float pos[3]; /* position in space */ |
| 20 |
– |
float dir[3]; /* normal direction */ |
| 21 |
– |
int lvl; /* recursion level of parent ray */ |
| 22 |
– |
float weight; /* weight of parent ray */ |
| 23 |
– |
COLOR val; /* computed ambient value */ |
| 24 |
– |
float rad; /* validity radius */ |
| 25 |
– |
struct ambval *next; /* next in list */ |
| 26 |
– |
} AMBVAL; /* ambient value */ |
| 27 |
– |
|
| 16 |
|
int dataonly = 0; |
| 17 |
< |
|
| 17 |
> |
int header = 1; |
| 18 |
|
int reverse = 0; |
| 19 |
|
|
| 20 |
|
AMBVAL av; |
| 21 |
|
|
| 22 |
|
|
| 23 |
< |
main(argc, argv) /* load ambient values from a file */ |
| 24 |
< |
char *argv[]; |
| 23 |
> |
static void |
| 24 |
> |
lookamb( /* load & convert ambient values from a file */ |
| 25 |
> |
FILE *fp |
| 26 |
> |
) |
| 27 |
|
{ |
| 28 |
< |
FILE *fp; |
| 39 |
< |
int i; |
| 28 |
> |
FVECT norm, uvec; |
| 29 |
|
|
| 30 |
< |
for (i = 1; i < argc; i++) |
| 31 |
< |
if (argv[i][0] == '-') |
| 32 |
< |
switch (argv[i][1]) { |
| 44 |
< |
case 'd': |
| 45 |
< |
dataonly = 1; |
| 46 |
< |
break; |
| 47 |
< |
case 'r': |
| 48 |
< |
reverse = 1; |
| 49 |
< |
break; |
| 50 |
< |
default: |
| 51 |
< |
fprintf(stderr, "%s: unknown option '%s'\n", |
| 52 |
< |
argv[0], argv[i]); |
| 53 |
< |
return(1); |
| 54 |
< |
} |
| 55 |
< |
else |
| 56 |
< |
break; |
| 57 |
< |
|
| 58 |
< |
if (i >= argc) |
| 59 |
< |
fp = stdin; |
| 60 |
< |
else if ((fp = fopen(argv[i], "r")) == NULL) { |
| 61 |
< |
fprintf(stderr, "%s: file not found\n", argv[i]); |
| 62 |
< |
return(1); |
| 63 |
< |
} |
| 64 |
< |
if (reverse) |
| 65 |
< |
writamb(fp); |
| 66 |
< |
else |
| 67 |
< |
lookamb(fp); |
| 68 |
< |
fclose(fp); |
| 69 |
< |
return(0); |
| 70 |
< |
} |
| 71 |
< |
|
| 72 |
< |
|
| 73 |
< |
lookamb(fp) /* get ambient values from a file */ |
| 74 |
< |
FILE *fp; |
| 75 |
< |
{ |
| 76 |
< |
while (fread((char *)&av, sizeof(AMBVAL), 1, fp) == 1) { |
| 30 |
> |
while (readambval(&av, fp)) { |
| 31 |
> |
decodedir(norm, av.ndir); |
| 32 |
> |
decodedir(uvec, av.udir); |
| 33 |
|
if (dataonly) { |
| 34 |
|
printf("%f\t%f\t%f\t", av.pos[0], av.pos[1], av.pos[2]); |
| 35 |
< |
printf("%f\t%f\t%f\t", av.dir[0], av.dir[1], av.dir[2]); |
| 36 |
< |
printf("%d\t%f\t%f\t", av.lvl, av.weight, av.rad); |
| 37 |
< |
printf("%e\t%e\t%e\n", colval(av.val,RED), |
| 35 |
> |
printf("%f\t%f\t%f\t", norm[0], norm[1], norm[2]); |
| 36 |
> |
printf("%f\t%f\t%f\t", uvec[0], uvec[1], uvec[2]); |
| 37 |
> |
printf("%d\t%f\t%f\t%f\t", av.lvl, av.weight, |
| 38 |
> |
av.rad[0], av.rad[1]); |
| 39 |
> |
printf("%e\t%e\t%e\t", colval(av.val,RED), |
| 40 |
|
colval(av.val,GRN), |
| 41 |
|
colval(av.val,BLU)); |
| 42 |
+ |
printf("%f\t%f\t", av.gpos[0], av.gpos[1]); |
| 43 |
+ |
printf("%f\t%f\t", av.gdir[0], av.gdir[1]); |
| 44 |
+ |
printf("%u\n", av.corral); |
| 45 |
|
} else { |
| 46 |
< |
printf("\nPosition:\t%f\t%f\t%f\n", av.pos[0], |
| 46 |
> |
printf("Position:\t%f\t%f\t%f\n", av.pos[0], |
| 47 |
|
av.pos[1], av.pos[2]); |
| 48 |
< |
printf("Direction:\t%f\t%f\t%f\n", av.dir[0], |
| 49 |
< |
av.dir[1], av.dir[2]); |
| 50 |
< |
printf("Lvl,Wt,Rad:\t%d\t\t%f\t%f\n", av.lvl, |
| 51 |
< |
av.weight, av.rad); |
| 48 |
> |
printf("Normal:\t\t%f\t%f\t%f\n", |
| 49 |
> |
norm[0], norm[1], norm[2]); |
| 50 |
> |
printf("Uvector:\t%f\t%f\t%f\n", |
| 51 |
> |
uvec[0], uvec[1], uvec[2]); |
| 52 |
> |
printf("Lvl,Wt,UVrad:\t%d\t\t%f\t%f\t%f\n", av.lvl, |
| 53 |
> |
av.weight, av.rad[0], av.rad[1]); |
| 54 |
|
printf("Value:\t\t%e\t%e\t%e\n", colval(av.val,RED), |
| 55 |
|
colval(av.val,GRN), colval(av.val,BLU)); |
| 56 |
+ |
printf("Pos.Grad:\t%f\t%f\n", av.gpos[0], av.gpos[1]); |
| 57 |
+ |
printf("Dir.Grad:\t%f\t%f\n", av.gdir[0], av.gdir[1]); |
| 58 |
+ |
printf("Corral:\t\t%8X\n\n", av.corral); |
| 59 |
|
} |
| 60 |
|
if (ferror(stdout)) |
| 61 |
|
exit(1); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
< |
writamb(fp) /* write binary ambient values */ |
| 67 |
< |
FILE *fp; |
| 66 |
> |
static void |
| 67 |
> |
writamb( /* write binary ambient values to stdout */ |
| 68 |
> |
FILE *fp |
| 69 |
> |
) |
| 70 |
|
{ |
| 71 |
+ |
FVECT norm; |
| 72 |
+ |
|
| 73 |
|
for ( ; ; ) { |
| 74 |
|
if (!dataonly) |
| 75 |
|
fscanf(fp, "%*s"); |
| 78 |
|
return; |
| 79 |
|
if (!dataonly) |
| 80 |
|
fscanf(fp, "%*s"); |
| 81 |
< |
if (fscanf(fp, "%f %f %f", |
| 112 |
< |
&av.dir[0], &av.dir[1], &av.dir[2]) != 3) |
| 81 |
> |
if (fscanf(fp, FVFORMAT, &norm[0], &norm[1], &norm[2]) != 3) |
| 82 |
|
return; |
| 83 |
+ |
av.ndir = encodedir(norm); |
| 84 |
|
if (!dataonly) |
| 85 |
|
fscanf(fp, "%*s"); |
| 86 |
< |
if (fscanf(fp, "%d %f %f", |
| 117 |
< |
&av.lvl, &av.weight, &av.rad) != 3) |
| 86 |
> |
if (fscanf(fp, FVFORMAT, &norm[0], &norm[1], &norm[2]) != 3) |
| 87 |
|
return; |
| 88 |
+ |
av.udir = encodedir(norm); |
| 89 |
|
if (!dataonly) |
| 90 |
|
fscanf(fp, "%*s"); |
| 91 |
+ |
if (fscanf(fp, "%hd %f %f %f", &av.lvl, &av.weight, |
| 92 |
+ |
&av.rad[0], &av.rad[1]) != 4) |
| 93 |
+ |
return; |
| 94 |
+ |
if (!dataonly) |
| 95 |
+ |
fscanf(fp, "%*s"); |
| 96 |
|
if (fscanf(fp, "%f %f %f", |
| 97 |
|
&av.val[RED], &av.val[GRN], &av.val[BLU]) != 3) |
| 98 |
|
return; |
| 99 |
< |
fwrite((char *)&av, sizeof(AMBVAL), 1, stdout); |
| 99 |
> |
if (!dataonly) |
| 100 |
> |
fscanf(fp, "%*s"); |
| 101 |
> |
if (fscanf(fp, "%f %f", &av.gpos[0], &av.gpos[1]) != 2) |
| 102 |
> |
return; |
| 103 |
> |
if (!dataonly) |
| 104 |
> |
fscanf(fp, "%*s"); |
| 105 |
> |
if (fscanf(fp, "%f %f", &av.gdir[0], &av.gdir[1]) != 2) |
| 106 |
> |
return; |
| 107 |
> |
if (dataonly) { |
| 108 |
> |
if (fscanf(fp, "%u", &av.corral) != 1) |
| 109 |
> |
return; |
| 110 |
> |
} else if (fscanf(fp, "%*s %X", &av.corral) != 1) |
| 111 |
> |
return; |
| 112 |
> |
av.next = NULL; |
| 113 |
> |
writambval(&av, stdout); |
| 114 |
|
if (ferror(stdout)) |
| 115 |
|
exit(1); |
| 116 |
|
} |
| 117 |
+ |
} |
| 118 |
+ |
|
| 119 |
+ |
|
| 120 |
+ |
int |
| 121 |
+ |
main( /* load ambient values from a file */ |
| 122 |
+ |
int argc, |
| 123 |
+ |
char *argv[] |
| 124 |
+ |
) |
| 125 |
+ |
{ |
| 126 |
+ |
FILE *fp; |
| 127 |
+ |
int i; |
| 128 |
+ |
|
| 129 |
+ |
for (i = 1; i < argc; i++) |
| 130 |
+ |
if (argv[i][0] == '-') |
| 131 |
+ |
switch (argv[i][1]) { |
| 132 |
+ |
case 'd': |
| 133 |
+ |
dataonly = 1; |
| 134 |
+ |
break; |
| 135 |
+ |
case 'r': |
| 136 |
+ |
reverse = 1; |
| 137 |
+ |
break; |
| 138 |
+ |
case 'h': |
| 139 |
+ |
header = 0; |
| 140 |
+ |
break; |
| 141 |
+ |
default: |
| 142 |
+ |
fprintf(stderr, "%s: unknown option '%s'\n", |
| 143 |
+ |
argv[0], argv[i]); |
| 144 |
+ |
return(1); |
| 145 |
+ |
} |
| 146 |
+ |
else |
| 147 |
+ |
break; |
| 148 |
+ |
|
| 149 |
+ |
if (i >= argc) |
| 150 |
+ |
fp = stdin; |
| 151 |
+ |
else if ((fp = fopen(argv[i], "r")) == NULL) { |
| 152 |
+ |
fprintf(stderr, "%s: file not found\n", argv[i]); |
| 153 |
+ |
return(1); |
| 154 |
+ |
} |
| 155 |
+ |
if (reverse) { |
| 156 |
+ |
if (header) { |
| 157 |
+ |
if (checkheader(fp, "ascii", stdout) < 0) |
| 158 |
+ |
goto formaterr; |
| 159 |
+ |
} else { |
| 160 |
+ |
newheader("RADIANCE", stdout); |
| 161 |
+ |
printargs(argc, argv, stdout); |
| 162 |
+ |
} |
| 163 |
+ |
fputformat(AMBFMT, stdout); |
| 164 |
+ |
putchar('\n'); |
| 165 |
+ |
SET_FILE_BINARY(stdout); |
| 166 |
+ |
putambmagic(stdout); |
| 167 |
+ |
writamb(fp); |
| 168 |
+ |
} else { |
| 169 |
+ |
SET_FILE_BINARY(fp); |
| 170 |
+ |
if (checkheader(fp, AMBFMT, header ? stdout : (FILE *)NULL) < 0) |
| 171 |
+ |
goto formaterr; |
| 172 |
+ |
if (!hasambmagic(fp)) |
| 173 |
+ |
goto formaterr; |
| 174 |
+ |
if (header) { |
| 175 |
+ |
fputformat("ascii", stdout); |
| 176 |
+ |
putchar('\n'); |
| 177 |
+ |
} |
| 178 |
+ |
lookamb(fp); |
| 179 |
+ |
} |
| 180 |
+ |
fclose(fp); |
| 181 |
+ |
return(0); |
| 182 |
+ |
formaterr: |
| 183 |
+ |
fprintf(stderr, "%s: format error on input\n", argv[0]); |
| 184 |
+ |
exit(1); |
| 185 |
|
} |