1 |
#ifndef lint |
2 |
static char SCCSid[] = "$SunId$ LBL"; |
3 |
#endif |
4 |
|
5 |
/* |
6 |
* Interpolate and extrapolate pictures with different view parameters. |
7 |
* |
8 |
* Greg Ward 09Dec89 |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#include "view.h" |
14 |
|
15 |
#include "color.h" |
16 |
|
17 |
#define pscan(y) (ourpict+(y)*ourview.hresolu) |
18 |
#define zscan(y) (ourzbuf+(y)*ourview.hresolu) |
19 |
|
20 |
VIEW ourview = STDVIEW(512); /* desired view */ |
21 |
|
22 |
double zeps = 0.001; /* allowed z epsilon */ |
23 |
|
24 |
COLR *ourpict; /* output picture */ |
25 |
float *ourzbuf; /* corresponding z-buffer */ |
26 |
|
27 |
char *progname; |
28 |
|
29 |
VIEW theirview = STDVIEW(512); /* input view */ |
30 |
int gotview; /* got input view? */ |
31 |
|
32 |
|
33 |
main(argc, argv) /* interpolate pictures */ |
34 |
int argc; |
35 |
char *argv[]; |
36 |
{ |
37 |
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
38 |
int gotvfile = 0; |
39 |
char *err; |
40 |
int i; |
41 |
|
42 |
progname = argv[0]; |
43 |
|
44 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
45 |
switch (argv[i][1]) { |
46 |
case 't': /* threshold */ |
47 |
check(2,1); |
48 |
zeps = atof(argv[++i]); |
49 |
break; |
50 |
case 'v': /* view */ |
51 |
switch (argv[i][2]) { |
52 |
case 't': /* type */ |
53 |
check(4,0); |
54 |
ourview.type = argv[i][3]; |
55 |
break; |
56 |
case 'p': /* point */ |
57 |
check(3,3); |
58 |
ourview.vp[0] = atof(argv[++i]); |
59 |
ourview.vp[1] = atof(argv[++i]); |
60 |
ourview.vp[2] = atof(argv[++i]); |
61 |
break; |
62 |
case 'd': /* direction */ |
63 |
check(3,3); |
64 |
ourview.vdir[0] = atof(argv[++i]); |
65 |
ourview.vdir[1] = atof(argv[++i]); |
66 |
ourview.vdir[2] = atof(argv[++i]); |
67 |
break; |
68 |
case 'u': /* up */ |
69 |
check(3,3); |
70 |
ourview.vup[0] = atof(argv[++i]); |
71 |
ourview.vup[1] = atof(argv[++i]); |
72 |
ourview.vup[2] = atof(argv[++i]); |
73 |
break; |
74 |
case 'h': /* horizontal */ |
75 |
check(3,1); |
76 |
ourview.horiz = atof(argv[++i]); |
77 |
break; |
78 |
case 'v': /* vertical */ |
79 |
check(3,1); |
80 |
ourview.vert = atof(argv[++i]); |
81 |
break; |
82 |
case 'f': /* file */ |
83 |
check(3,1); |
84 |
gotvfile = viewfile(argv[++i], &ourview); |
85 |
if (gotvfile < 0) { |
86 |
perror(argv[i]); |
87 |
exit(1); |
88 |
} else if (gotvfile == 0) { |
89 |
fprintf(stderr, "%s: bad view file\n", |
90 |
argv[i]); |
91 |
exit(1); |
92 |
} |
93 |
break; |
94 |
default: |
95 |
goto badopt; |
96 |
} |
97 |
break; |
98 |
default: |
99 |
badopt: |
100 |
fprintf(stderr, "%s: unknown option '%s'\n", |
101 |
progname, argv[i]); |
102 |
exit(1); |
103 |
} |
104 |
/* check arguments */ |
105 |
if (argc-i < 2 || (argc-i)%2) { |
106 |
fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n", |
107 |
progname); |
108 |
exit(1); |
109 |
} |
110 |
/* set view */ |
111 |
if (err = setview(&ourview)) { |
112 |
fprintf(stderr, "%s: %s\n", progname, err); |
113 |
exit(1); |
114 |
} |
115 |
/* allocate frame */ |
116 |
ourpict = (COLR *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR)); |
117 |
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
118 |
if (ourpict == NULL || ourzbuf == NULL) { |
119 |
perror(progname); |
120 |
exit(1); |
121 |
} |
122 |
/* get input */ |
123 |
for ( ; i < argc; i += 2) |
124 |
addpicture(argv[i], argv[i+1]); |
125 |
/* fill in spaces */ |
126 |
fillpicture(); |
127 |
/* add to header */ |
128 |
printargs(argc, argv, stdout); |
129 |
if (gotvfile) { |
130 |
printf(VIEWSTR); |
131 |
fprintview(&ourview, stdout); |
132 |
printf("\n"); |
133 |
} |
134 |
printf("\n"); |
135 |
/* write output */ |
136 |
writepicture(); |
137 |
|
138 |
exit(0); |
139 |
#undef check |
140 |
} |
141 |
|
142 |
|
143 |
headline(s) /* process header string */ |
144 |
char *s; |
145 |
{ |
146 |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
147 |
register char **an; |
148 |
|
149 |
printf("\t%s", s); |
150 |
|
151 |
for (an = altname; *an != NULL; an++) |
152 |
if (!strncmp(*an, s, strlen(*an))) { |
153 |
if (sscanview(&theirview, s+strlen(*an)) == 0) |
154 |
gotview++; |
155 |
break; |
156 |
} |
157 |
} |
158 |
|
159 |
|
160 |
addpicture(pfile, zfile) /* add picture to output */ |
161 |
char *pfile, *zfile; |
162 |
{ |
163 |
FILE *pfp, *zfp; |
164 |
COLR *scanin; |
165 |
float *zin; |
166 |
char *err; |
167 |
int xres, yres; |
168 |
int y; |
169 |
/* open input files */ |
170 |
if ((pfp = fopen(pfile, "r")) == NULL) { |
171 |
perror(pfile); |
172 |
exit(1); |
173 |
} |
174 |
if ((zfp = fopen(zfile, "r")) == NULL) { |
175 |
perror(zfile); |
176 |
exit(1); |
177 |
} |
178 |
/* get header and view */ |
179 |
printf("%s:\n", pfile); |
180 |
gotview = 0; |
181 |
getheader(pfp, headline); |
182 |
if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) { |
183 |
fprintf(stderr, "%s: picture view error\n", pfile); |
184 |
exit(1); |
185 |
} |
186 |
theirview.hresolu = xres; |
187 |
theirview.vresolu = yres; |
188 |
if (err = setview(&theirview)) { |
189 |
fprintf(stderr, "%s: %s\n", pfile, err); |
190 |
exit(1); |
191 |
} |
192 |
/* allocate scanlines */ |
193 |
scanin = (COLR *)malloc(xres*sizeof(COLR)); |
194 |
zin = (float *)malloc(xres*sizeof(float)); |
195 |
if (scanin == NULL || zin == NULL) { |
196 |
perror(progname); |
197 |
exit(1); |
198 |
} |
199 |
/* load image */ |
200 |
for (y = yres-1; y >= 0; y--) { |
201 |
if (freadcolrs(scanin, xres, pfp) < 0) { |
202 |
fprintf(stderr, "%s: read error\n", pfile); |
203 |
exit(1); |
204 |
} |
205 |
if (fread(zin, sizeof(float), xres, zfp) < xres) { |
206 |
fprintf(stderr, "%s: read error\n", zfile); |
207 |
exit(1); |
208 |
} |
209 |
addscanline(y, scanin, zin); |
210 |
} |
211 |
/* clean up */ |
212 |
free((char *)scanin); |
213 |
free((char *)zin); |
214 |
fclose(pfp); |
215 |
fclose(zfp); |
216 |
} |
217 |
|
218 |
|
219 |
addscanline(y, pline, zline) /* add scanline to output */ |
220 |
int y; |
221 |
COLR *pline; |
222 |
float *zline; |
223 |
{ |
224 |
FVECT p, dir; |
225 |
double xnew, ynew, znew; |
226 |
register int x; |
227 |
register int xpos, ypos; |
228 |
|
229 |
for (x = 0; x < theirview.hresolu; x++) { |
230 |
rayview(p, dir, &theirview, x+.5, y+.5); |
231 |
p[0] += zline[x]*dir[0]; |
232 |
p[1] += zline[x]*dir[1]; |
233 |
p[2] += zline[x]*dir[2]; |
234 |
pixelview(&xnew, &ynew, &znew, &ourview, p); |
235 |
if (znew <= 0.0 || xnew < 0 || xnew > ourview.hresolu |
236 |
|| ynew < 0 || ynew > ourview.vresolu) |
237 |
continue; |
238 |
/* check current value at position */ |
239 |
xpos = xnew; |
240 |
ypos = ynew; |
241 |
if (zscan(ypos)[xpos] <= 0.0 |
242 |
|| zscan(ypos)[xpos] - znew |
243 |
> zeps*zscan(ypos)[xpos]) { |
244 |
zscan(ypos)[xpos] = znew; |
245 |
copycolr(pscan(ypos)[xpos], pline[x]); |
246 |
} |
247 |
} |
248 |
} |
249 |
|
250 |
|
251 |
fillpicture() /* fill in empty spaces */ |
252 |
{ |
253 |
int y; |
254 |
COLR cfill; |
255 |
register int x, xblank; |
256 |
|
257 |
for (y = 0; y < ourview.vresolu; y++) { |
258 |
xblank = -1; |
259 |
for (x = 0; x < ourview.hresolu; x++) |
260 |
if (zscan(y)[x] <= 0.0) { |
261 |
if (xblank < 0) |
262 |
xblank = x; |
263 |
} else if (xblank >= 0) { |
264 |
if (xblank == 0 || zscan(y)[xblank-1] < zscan(y)[x]) |
265 |
copycolr(cfill, pscan(y)[x]); |
266 |
else |
267 |
copycolr(cfill, pscan(y)[xblank-1]); |
268 |
for ( ; xblank < x; xblank++) |
269 |
copycolr(pscan(y)[xblank], cfill); |
270 |
xblank = -1; |
271 |
} |
272 |
if (xblank > 0) { |
273 |
copycolr(cfill, pscan(y)[xblank-1]); |
274 |
for ( ; xblank < ourview.hresolu; xblank++) |
275 |
copycolr(pscan(y)[xblank], cfill); |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
|
281 |
writepicture() /* write out picture */ |
282 |
{ |
283 |
int y; |
284 |
|
285 |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
286 |
for (y = ourview.vresolu-1; y >= 0; y--) |
287 |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { |
288 |
perror(progname); |
289 |
exit(1); |
290 |
} |
291 |
} |