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