ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.52
Committed: Fri Feb 12 00:47:08 2021 UTC (3 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.51: +10 -15 lines
Log Message:
refactor: added macros for RREAL comparisons

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.52 static const char RCSid[] = "$Id: image.c,v 2.51 2020/05/14 20:49:57 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * image.c - routines for image generation.
6 greg 2.16 *
7     * External symbols declared in view.h
8     */
9    
10 greg 2.17 #include "copyright.h"
11 greg 1.1
12 greg 2.32 #include <ctype.h>
13 schorsch 2.22 #include "rtio.h"
14 greg 2.28 #include "rtmath.h"
15 greg 2.27 #include "paths.h"
16 greg 1.1 #include "view.h"
17    
18 greg 1.5 VIEW stdview = STDVIEW; /* default view parameters */
19 greg 1.1
20 schorsch 2.26 static gethfunc gethview;
21    
22 greg 1.1
23     char *
24 greg 2.35 setview( /* set hvec and vvec, return message on error */
25     VIEW *v
26     )
27 greg 1.3 {
28 greg 1.12 static char ill_horiz[] = "illegal horizontal view size";
29     static char ill_vert[] = "illegal vertical view size";
30    
31 greg 2.39 if ((v->vfore < -FTINY) | (v->vaft < -FTINY) ||
32     (v->vaft > FTINY) & (v->vaft <= v->vfore))
33 greg 2.7 return("illegal fore/aft clipping plane");
34    
35 greg 2.31 if (v->vdist <= FTINY)
36     return("illegal view distance");
37 greg 2.29 v->vdist *= normalize(v->vdir); /* normalize direction */
38     if (v->vdist == 0.0)
39 greg 1.1 return("zero view direction");
40    
41 greg 1.14 if (normalize(v->vup) == 0.0) /* normalize view up */
42     return("zero view up vector");
43    
44 greg 1.5 fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */
45    
46     if (normalize(v->hvec) == 0.0)
47 greg 1.14 return("view up parallel to view direction");
48 greg 1.1
49 greg 1.5 fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */
50    
51 greg 1.12 if (v->horiz <= FTINY)
52     return(ill_horiz);
53     if (v->vert <= FTINY)
54     return(ill_vert);
55    
56     switch (v->type) {
57     case VT_PAR: /* parallel view */
58 greg 1.5 v->hn2 = v->horiz;
59 greg 1.12 v->vn2 = v->vert;
60     break;
61     case VT_PER: /* perspective view */
62     if (v->horiz >= 180.0-FTINY)
63     return(ill_horiz);
64     if (v->vert >= 180.0-FTINY)
65     return(ill_vert);
66 greg 1.5 v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
67 greg 1.12 v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
68     break;
69 greg 2.9 case VT_CYL: /* cylindrical panorama */
70     if (v->horiz > 360.0+FTINY)
71     return(ill_horiz);
72     if (v->vert >= 180.0-FTINY)
73     return(ill_vert);
74     v->hn2 = v->horiz * (PI/180.0);
75     v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
76     break;
77 greg 1.12 case VT_ANG: /* angular fisheye */
78     if (v->horiz > 360.0+FTINY)
79     return(ill_horiz);
80     if (v->vert > 360.0+FTINY)
81     return(ill_vert);
82 greg 2.9 v->hn2 = v->horiz * (PI/180.0);
83     v->vn2 = v->vert * (PI/180.0);
84 greg 1.12 break;
85     case VT_HEM: /* hemispherical fisheye */
86     if (v->horiz > 180.0+FTINY)
87     return(ill_horiz);
88     if (v->vert > 180.0+FTINY)
89     return(ill_vert);
90     v->hn2 = 2.0 * sin(v->horiz*(PI/180.0/2.0));
91     v->vn2 = 2.0 * sin(v->vert*(PI/180.0/2.0));
92     break;
93 greg 2.34 case VT_PLS: /* planispheric fisheye */
94     if (v->horiz >= 360.0-FTINY)
95     return(ill_horiz);
96     if (v->vert >= 360.0-FTINY)
97     return(ill_vert);
98     v->hn2 = 2.*sin(v->horiz*(PI/180.0/2.0)) /
99     (1.0 + cos(v->horiz*(PI/180.0/2.0)));
100     v->vn2 = 2.*sin(v->vert*(PI/180.0/2.0)) /
101     (1.0 + cos(v->vert*(PI/180.0/2.0)));
102     break;
103 greg 1.12 default:
104 greg 1.1 return("unknown view type");
105 greg 1.12 }
106 greg 2.34 if (v->type != VT_ANG && v->type != VT_PLS) {
107 greg 2.9 if (v->type != VT_CYL) {
108     v->hvec[0] *= v->hn2;
109     v->hvec[1] *= v->hn2;
110     v->hvec[2] *= v->hn2;
111     }
112 greg 1.12 v->vvec[0] *= v->vn2;
113     v->vvec[1] *= v->vn2;
114     v->vvec[2] *= v->vn2;
115     }
116 greg 1.5 v->hn2 *= v->hn2;
117     v->vn2 *= v->vn2;
118 greg 1.1
119     return(NULL);
120     }
121    
122    
123 greg 2.16 void
124 greg 2.35 normaspect( /* fix pixel aspect or resolution */
125     double va, /* view aspect ratio */
126     double *ap, /* pixel aspect in (or out if 0) */
127     int *xp,
128     int *yp /* x and y resolution in (or out if *ap!=0) */
129     )
130 greg 1.6 {
131     if (*ap <= FTINY)
132 greg 1.7 *ap = va * *xp / *yp; /* compute pixel aspect */
133 greg 1.6 else if (va * *xp > *ap * *yp)
134 greg 1.10 *xp = *yp / va * *ap + .5; /* reduce x resolution */
135 greg 1.6 else
136 greg 1.10 *yp = *xp * va / *ap + .5; /* reduce y resolution */
137 greg 1.6 }
138    
139    
140 greg 2.7 double
141 greg 2.35 viewray( /* compute ray origin and direction */
142     FVECT orig,
143     FVECT direc,
144     VIEW *v,
145     double x,
146     double y
147     )
148 greg 1.1 {
149 greg 1.12 double d, z;
150    
151 greg 1.5 x += v->hoff - 0.5;
152     y += v->voff - 0.5;
153 greg 1.1
154 greg 1.12 switch(v->type) {
155     case VT_PAR: /* parallel view */
156 greg 2.7 orig[0] = v->vp[0] + v->vfore*v->vdir[0]
157     + x*v->hvec[0] + y*v->vvec[0];
158     orig[1] = v->vp[1] + v->vfore*v->vdir[1]
159     + x*v->hvec[1] + y*v->vvec[1];
160     orig[2] = v->vp[2] + v->vfore*v->vdir[2]
161     + x*v->hvec[2] + y*v->vvec[2];
162 greg 1.1 VCOPY(direc, v->vdir);
163 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
164 greg 1.12 case VT_PER: /* perspective view */
165 greg 1.5 direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
166     direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
167     direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
168 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
169 greg 2.7 d = normalize(direc);
170 greg 2.8 return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
171 greg 1.12 case VT_HEM: /* hemispherical fisheye */
172 greg 1.13 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
173 greg 1.12 if (z < 0.0)
174 greg 2.7 return(-1.0);
175 greg 1.12 z = sqrt(z);
176     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
177     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
178     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
179 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
180 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
181 greg 2.9 case VT_CYL: /* cylindrical panorama */
182     d = x * v->horiz * (PI/180.0);
183     z = cos(d);
184     x = sin(d);
185     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
186     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
187     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
188 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
189 greg 2.9 d = normalize(direc);
190     return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
191 greg 1.12 case VT_ANG: /* angular fisheye */
192 greg 2.34 x *= (1.0/180.0)*v->horiz;
193     y *= (1.0/180.0)*v->vert;
194 greg 1.12 d = x*x + y*y;
195     if (d > 1.0)
196 greg 2.7 return(-1.0);
197 greg 1.12 d = sqrt(d);
198     z = cos(PI*d);
199 greg 2.34 d = d <= FTINY ? PI : sqrt(1.0 - z*z)/d;
200     x *= d;
201     y *= d;
202     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
203     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
204     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
205 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
206 greg 2.34 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
207     case VT_PLS: /* planispheric fisheye */
208     x *= sqrt(v->hn2);
209     y *= sqrt(v->vn2);
210     d = x*x + y*y;
211     z = (1. - d)/(1. + d);
212 greg 2.38 x *= (1. + z);
213     y *= (1. + z);
214 greg 1.12 direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
215     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
216     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
217 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
218 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
219 greg 1.1 }
220 greg 2.7 return(-1.0);
221 greg 1.1 }
222    
223    
224 greg 2.41 int
225 greg 2.35 viewloc( /* find image location for point */
226     FVECT ip,
227     VIEW *v,
228     FVECT p
229 greg 2.46 ) /* Use VL_* flags to interpret return value */
230 greg 1.3 {
231 greg 2.46 int rflags = VL_GOOD;
232 gregl 2.13 double d, d2;
233 greg 1.3 FVECT disp;
234 greg 1.5
235 greg 2.34 VSUB(disp, p, v->vp);
236 greg 1.3
237 greg 1.12 switch (v->type) {
238     case VT_PAR: /* parallel view */
239 greg 2.7 ip[2] = DOT(disp,v->vdir) - v->vfore;
240 greg 1.13 break;
241 greg 1.12 case VT_PER: /* perspective view */
242 greg 1.11 d = DOT(disp,v->vdir);
243 greg 2.47 if ((v->vaft > FTINY) & (d >= v->vaft))
244 greg 2.46 rflags |= VL_BEYOND;
245 gregl 2.13 ip[2] = VLEN(disp);
246 greg 2.42 if (d < -FTINY) { /* fold pyramid */
247 greg 1.17 ip[2] = -ip[2];
248 greg 1.11 d = -d;
249 greg 2.42 } else if (d <= FTINY)
250 greg 2.46 return(VL_BAD); /* at infinite edge */
251 greg 2.41 d = 1.0/d;
252     disp[0] *= d;
253     disp[1] *= d;
254     disp[2] *= d;
255 greg 2.42 if (ip[2] < 0.0) d = -d;
256 greg 2.7 ip[2] *= (1.0 - v->vfore*d);
257 greg 1.13 break;
258 greg 1.12 case VT_HEM: /* hemispherical fisheye */
259     d = normalize(disp);
260 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
261     ip[2] = -d;
262     else
263     ip[2] = d;
264 greg 2.7 ip[2] -= v->vfore;
265 greg 1.13 break;
266 greg 2.9 case VT_CYL: /* cylindrical panorama */
267     d = DOT(disp,v->hvec);
268 gregl 2.13 d2 = DOT(disp,v->vdir);
269     ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
270 greg 2.51 d2 = d*d + d2*d2;
271     if (d2 <= FTINY*FTINY)
272 greg 2.46 return(VL_BAD); /* at pole */
273 greg 2.51 if ((v->vaft > FTINY) & (d2 >= v->vaft*v->vaft))
274 greg 2.46 rflags |= VL_BEYOND;
275 greg 2.51 d = 1.0/sqrt(d2);
276 gregl 2.13 ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
277     ip[2] = VLEN(disp);
278 gwlarson 2.15 ip[2] *= (1.0 - v->vfore*d);
279 greg 2.42 goto gotall;
280 greg 1.12 case VT_ANG: /* angular fisheye */
281 greg 1.17 ip[0] = 0.5 - v->hoff;
282     ip[1] = 0.5 - v->voff;
283 greg 2.7 ip[2] = normalize(disp) - v->vfore;
284 greg 1.12 d = DOT(disp,v->vdir);
285     if (d >= 1.0-FTINY)
286 greg 2.42 goto gotall;
287 greg 1.12 if (d <= -(1.0-FTINY)) {
288 greg 2.7 ip[0] += 180.0/v->horiz;
289 greg 2.42 goto gotall;
290 greg 1.12 }
291 greg 2.34 d = (180.0/PI)*acos(d) / sqrt(1.0 - d*d);
292     ip[0] += DOT(disp,v->hvec)*d/v->horiz;
293     ip[1] += DOT(disp,v->vvec)*d/v->vert;
294 greg 2.42 goto gotall;
295 greg 2.34 case VT_PLS: /* planispheric fisheye */
296     ip[0] = 0.5 - v->hoff;
297     ip[1] = 0.5 - v->voff;
298     ip[2] = normalize(disp) - v->vfore;
299     d = DOT(disp,v->vdir);
300     if (d >= 1.0-FTINY)
301 greg 2.42 goto gotall;
302 greg 2.34 if (d <= -(1.0-FTINY))
303 greg 2.46 return(VL_BAD);
304 greg 2.38 ip[0] += DOT(disp,v->hvec)/((1. + d)*sqrt(v->hn2));
305     ip[1] += DOT(disp,v->vvec)/((1. + d)*sqrt(v->vn2));
306 greg 2.42 goto gotall;
307 greg 2.45 default:
308 greg 2.46 return(VL_BAD);
309 greg 1.3 }
310 greg 1.17 ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
311     ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
312 greg 2.46 gotall: /* add appropriate return flags */
313 greg 2.48 if (ip[2] <= 0.0)
314     rflags |= VL_BEHIND;
315     else if ((v->type != VT_PER) & (v->type != VT_CYL))
316     rflags |= VL_BEYOND*((v->vaft > FTINY) &
317     (ip[2] >= v->vaft - v->vfore));
318 greg 2.46 rflags |= VL_OUTSIDE*((0.0 >= ip[0]) | (ip[0] >= 1.0) |
319     (0.0 >= ip[1]) | (ip[1] >= 1.0));
320     return(rflags);
321 greg 1.3 }
322    
323    
324 greg 2.16 void
325 greg 2.35 pix2loc( /* compute image location from pixel pos. */
326     RREAL loc[2],
327     RESOLU *rp,
328     int px,
329     int py
330     )
331 greg 1.17 {
332 greg 2.36 int x, y;
333 greg 1.17
334 greg 2.16 if (rp->rt & YMAJOR) {
335 greg 1.17 x = px;
336     y = py;
337     } else {
338     x = py;
339     y = px;
340     }
341 greg 2.16 if (rp->rt & XDECR)
342 greg 1.17 x = rp->xr-1 - x;
343 greg 2.16 if (rp->rt & YDECR)
344 greg 1.17 y = rp->yr-1 - y;
345     loc[0] = (x+.5)/rp->xr;
346     loc[1] = (y+.5)/rp->yr;
347     }
348    
349    
350 greg 2.16 void
351 greg 2.35 loc2pix( /* compute pixel pos. from image location */
352     int pp[2],
353     RESOLU *rp,
354     double lx,
355     double ly
356     )
357 greg 1.17 {
358 greg 2.36 int x, y;
359 greg 1.17
360 greg 2.37 x = (int)(lx*rp->xr + .5 - (lx < 0.0));
361     y = (int)(ly*rp->yr + .5 - (ly < 0.0));
362    
363 greg 2.16 if (rp->rt & XDECR)
364 greg 1.17 x = rp->xr-1 - x;
365 greg 2.16 if (rp->rt & YDECR)
366 greg 1.17 y = rp->yr-1 - y;
367 greg 2.16 if (rp->rt & YMAJOR) {
368 greg 1.17 pp[0] = x;
369     pp[1] = y;
370     } else {
371     pp[0] = y;
372     pp[1] = x;
373     }
374     }
375    
376    
377 greg 1.5 int
378 greg 2.35 getviewopt( /* process view argument */
379     VIEW *v,
380     int ac,
381     char *av[]
382     )
383 greg 1.5 {
384 greg 2.49 #define check(c,l) if ((av[0][c]&&!isspace(av[0][c])) || \
385 greg 1.16 badarg(ac-1,av+1,l)) return(-1)
386 greg 1.5
387 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
388 greg 1.5 return(-1);
389     switch (av[0][2]) {
390     case 't': /* type */
391 greg 2.49 if (!av[0][3] || isspace(av[0][3]))
392 greg 1.5 return(-1);
393 greg 1.16 check(4,"");
394 greg 1.5 v->type = av[0][3];
395     return(0);
396     case 'p': /* point */
397 greg 1.16 check(3,"fff");
398 greg 1.5 v->vp[0] = atof(av[1]);
399     v->vp[1] = atof(av[2]);
400     v->vp[2] = atof(av[3]);
401     return(3);
402     case 'd': /* direction */
403 greg 1.16 check(3,"fff");
404 greg 1.5 v->vdir[0] = atof(av[1]);
405     v->vdir[1] = atof(av[2]);
406     v->vdir[2] = atof(av[3]);
407 greg 2.30 v->vdist = 1.;
408 greg 1.5 return(3);
409     case 'u': /* up */
410 greg 1.16 check(3,"fff");
411 greg 1.5 v->vup[0] = atof(av[1]);
412     v->vup[1] = atof(av[2]);
413     v->vup[2] = atof(av[3]);
414     return(3);
415     case 'h': /* horizontal size */
416 greg 1.16 check(3,"f");
417 greg 1.5 v->horiz = atof(av[1]);
418     return(1);
419     case 'v': /* vertical size */
420 greg 1.16 check(3,"f");
421 greg 1.5 v->vert = atof(av[1]);
422     return(1);
423 greg 2.7 case 'o': /* fore clipping plane */
424     check(3,"f");
425     v->vfore = atof(av[1]);
426     return(1);
427     case 'a': /* aft clipping plane */
428     check(3,"f");
429     v->vaft = atof(av[1]);
430     return(1);
431 greg 1.5 case 's': /* shift */
432 greg 1.16 check(3,"f");
433 greg 1.5 v->hoff = atof(av[1]);
434     return(1);
435     case 'l': /* lift */
436 greg 1.16 check(3,"f");
437 greg 1.5 v->voff = atof(av[1]);
438     return(1);
439     default:
440     return(-1);
441     }
442     #undef check
443     }
444    
445    
446     int
447 greg 2.35 sscanview( /* get view parameters from string */
448     VIEW *vp,
449     char *s
450     )
451 greg 1.1 {
452 greg 1.5 int ac;
453     char *av[4];
454     int na;
455     int nvopts = 0;
456    
457 greg 2.32 while (isspace(*s))
458     if (!*s++)
459     return(0);
460 greg 1.9 while (*s) {
461 greg 1.5 ac = 0;
462     do {
463 greg 2.21 if (ac || *s == '-')
464     av[ac++] = s;
465 greg 2.32 while (*s && !isspace(*s))
466 greg 1.5 s++;
467 greg 2.32 while (isspace(*s))
468 greg 1.5 s++;
469     } while (*s && ac < 4);
470     if ((na = getviewopt(vp, ac, av)) >= 0) {
471     if (na+1 < ac)
472     s = av[na+1];
473     nvopts++;
474 greg 1.9 } else if (ac > 1)
475     s = av[1];
476     }
477 greg 1.5 return(nvopts);
478 greg 1.1 }
479    
480    
481 greg 2.16 void
482 greg 2.35 fprintview( /* write out view parameters */
483     VIEW *vp,
484     FILE *fp
485     )
486 greg 1.1 {
487     fprintf(fp, " -vt%c", vp->type);
488     fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
489 greg 2.29 fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0]*vp->vdist,
490     vp->vdir[1]*vp->vdist,
491     vp->vdir[2]*vp->vdist);
492 greg 1.1 fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
493     fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
494 greg 2.7 fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
495 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
496 greg 2.11 }
497    
498    
499     char *
500 greg 2.35 viewopt( /* translate to minimal view string */
501     VIEW *vp
502     )
503 greg 2.11 {
504     static char vwstr[128];
505 greg 2.36 char *cp = vwstr;
506 greg 2.11
507 greg 2.33 *cp = '\0';
508 greg 2.11 if (vp->type != stdview.type) {
509     sprintf(cp, " -vt%c", vp->type);
510     cp += strlen(cp);
511     }
512 greg 2.52 if (!VABSEQ(vp->vp,stdview.vp)) {
513 greg 2.11 sprintf(cp, " -vp %.6g %.6g %.6g",
514     vp->vp[0], vp->vp[1], vp->vp[2]);
515     cp += strlen(cp);
516     }
517 greg 2.52 if (!FABSEQ(vp->vdist,stdview.vdist) || !VABSEQ(vp->vdir,stdview.vdir)) {
518 greg 2.11 sprintf(cp, " -vd %.6g %.6g %.6g",
519 greg 2.29 vp->vdir[0]*vp->vdist,
520     vp->vdir[1]*vp->vdist,
521     vp->vdir[2]*vp->vdist);
522 greg 2.11 cp += strlen(cp);
523     }
524 greg 2.52 if (!VABSEQ(vp->vup,stdview.vup)) {
525 greg 2.11 sprintf(cp, " -vu %.6g %.6g %.6g",
526     vp->vup[0], vp->vup[1], vp->vup[2]);
527     cp += strlen(cp);
528     }
529 greg 2.52 if (!FABSEQ(vp->horiz,stdview.horiz)) {
530 greg 2.11 sprintf(cp, " -vh %.6g", vp->horiz);
531     cp += strlen(cp);
532     }
533 greg 2.52 if (!FABSEQ(vp->vert,stdview.vert)) {
534 greg 2.11 sprintf(cp, " -vv %.6g", vp->vert);
535     cp += strlen(cp);
536     }
537 greg 2.52 if (!FABSEQ(vp->vfore,stdview.vfore)) {
538 greg 2.11 sprintf(cp, " -vo %.6g", vp->vfore);
539     cp += strlen(cp);
540     }
541 greg 2.52 if (!FABSEQ(vp->vaft,stdview.vaft)) {
542 greg 2.11 sprintf(cp, " -va %.6g", vp->vaft);
543     cp += strlen(cp);
544     }
545 greg 2.52 if (!FABSEQ(vp->hoff,stdview.hoff)) {
546 greg 2.11 sprintf(cp, " -vs %.6g", vp->hoff);
547     cp += strlen(cp);
548     }
549 greg 2.52 if (!FABSEQ(vp->voff,stdview.voff)) {
550 greg 2.11 sprintf(cp, " -vl %.6g", vp->voff);
551     cp += strlen(cp);
552     }
553     return(vwstr);
554 greg 1.1 }
555    
556    
557 greg 2.3 int
558 greg 2.35 isview( /* is this a view string? */
559     char *s
560     )
561 greg 2.3 {
562 greg 2.25 static char *altname[]={NULL,VIEWSTR,"rpict","rview","rvu","rpiece","pinterp",NULL};
563 greg 2.5 extern char *progname;
564 greg 2.36 char *cp;
565     char **an;
566 greg 2.3 /* add program name to list */
567 greg 2.5 if (altname[0] == NULL) {
568     for (cp = progname; *cp; cp++)
569     ;
570     while (cp > progname && !ISDIRSEP(cp[-1]))
571     cp--;
572     altname[0] = cp;
573     }
574 greg 2.3 /* skip leading path */
575     cp = s;
576 greg 2.49 while (*cp && !isspace(*cp))
577 greg 2.3 cp++;
578 greg 2.5 while (cp > s && !ISDIRSEP(cp[-1]))
579 greg 2.3 cp--;
580     for (an = altname; *an != NULL; an++)
581     if (!strncmp(*an, cp, strlen(*an)))
582     return(1);
583     return(0);
584     }
585 greg 1.1
586 greg 2.3
587 greg 1.15 struct myview {
588     VIEW *hv;
589     int ok;
590     };
591 greg 1.1
592 greg 1.15
593 gwlarson 2.14 static int
594 schorsch 2.26 gethview( /* get view from header */
595     char *s,
596     void *v
597     )
598 greg 1.1 {
599 schorsch 2.26 if (isview(s) && sscanview(((struct myview*)v)->hv, s) > 0)
600     ((struct myview*)v)->ok++;
601 gwlarson 2.14 return(0);
602 greg 1.1 }
603    
604    
605     int
606 greg 2.35 viewfile( /* get view from file */
607     char *fname,
608     VIEW *vp,
609     RESOLU *rp
610     )
611 greg 1.1 {
612 greg 1.15 struct myview mvs;
613 greg 1.1 FILE *fp;
614    
615 greg 2.16 if (fname == NULL || !strcmp(fname, "-"))
616     fp = stdin;
617     else if ((fp = fopen(fname, "r")) == NULL)
618 greg 1.1 return(-1);
619    
620 greg 1.15 mvs.hv = vp;
621     mvs.ok = 0;
622 greg 1.1
623 schorsch 2.26 getheader(fp, gethview, &mvs);
624 greg 1.8
625 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
626 greg 1.15 mvs.ok = 0;
627 greg 1.1
628 greg 2.50 if (fp != stdin)
629     fclose(fp);
630 greg 1.1
631 greg 1.15 return(mvs.ok);
632 greg 1.1 }