ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.34
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3
Changes since 2.33: +1 -2 lines
Log Message:
Removed redundant include files

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.34 static const char RCSid[] = "$Id: pfilt.c,v 2.33 2018/08/02 18:33:44 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * pfilt.c - program to post-process picture file.
6     *
7     * 9/26/85
8 greg 2.9 * 6/23/93 Added additional buffers for value spreading
9 greg 1.1 */
10    
11 schorsch 2.25 #include "copyright.h"
12    
13 greg 1.1 #include <signal.h>
14    
15 greg 2.27 #include "platform.h"
16 schorsch 2.23 #include "standard.h"
17 schorsch 2.29 #include "rtio.h"
18 greg 1.1 #include "color.h"
19 greg 2.17 #include "view.h"
20 greg 2.3 #include "paths.h"
21 schorsch 2.29 #include "pfilt.h"
22 greg 2.3
23 greg 1.1
24 greg 2.4 #define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b))
25 greg 1.15
26 greg 2.22 double CHECKRAD = 2.0; /* radius to check for filtering */
27 greg 1.1
28 greg 2.9 #define THRESHRAD 5.0 /* maximum sample spread in output */
29    
30 greg 1.1 COLOR exposure = WHTCOLOR; /* exposure for the frame */
31    
32 greg 2.4 double rad = 0.0; /* output pixel radius for filtering */
33 greg 1.1
34 greg 2.9 double thresh = 0.0; /* maximum contribution for subpixel */
35    
36 greg 1.1 int nrows = 0; /* number of rows for output */
37     int ncols = 0; /* number of columns for output */
38    
39 greg 2.4 double x_c = 1.0; /* ratio of output x size to input */
40     double y_r = 1.0; /* ratio of output y size to input */
41 greg 1.2
42 greg 1.1 int singlepass = 0; /* true means skip first pass */
43    
44     int avghot = 0; /* true means average in bright spots */
45    
46 greg 2.8 double hotlvl = 100.0; /* level considered "hot" */
47 greg 1.1
48     int npts = 0; /* (half) number of points for stars */
49    
50 greg 2.4 double spread = 1e-4; /* spread for star points */
51 greg 1.1
52     char *tfname = NULL;
53    
54 greg 2.11 char template[] = TEMPLATE;
55    
56 greg 1.13 char *lampdat = "lamp.tab"; /* lamp data file */
57    
58 greg 1.19 int order; /* scanline ordering of input */
59 greg 1.1 int xres, yres; /* resolution of input */
60 greg 2.4 double inpaspect = 1.0; /* pixel aspect ratio of input */
61 greg 1.12 int correctaspect = 0; /* aspect ratio correction? */
62 greg 1.1
63 greg 1.16 int wrongformat = 0;
64    
65 greg 2.17 VIEW ourview = STDVIEW;
66     int gotview = 0;
67     int wrapfilt = 0; /* wrap filter horizontally? */
68    
69 greg 2.18 int estatus = 0; /* exit status (for non-fatal errors) */
70    
71 greg 2.9 int xrad; /* x search radius */
72     int yrad; /* y search radius */
73     int xbrad; /* x box size */
74     int ybrad; /* y box size */
75 greg 1.1
76     int barsize; /* size of input scan bar */
77     COLOR **scanin; /* input scan bar */
78     COLOR *scanout; /* output scan line */
79 greg 2.9 COLOR **scoutbar; /* output scan bar (if thresh > 0) */
80     float **greybar; /* grey-averaged input values */
81     int obarsize = 0; /* size of output scan bar */
82     int orad = 0; /* output window radius */
83 greg 1.1
84     char *progname;
85    
86 schorsch 2.28 static gethfunc headline;
87 schorsch 2.30 static brightfunc_t rgb_bright;
88     static brightfunc_t xyz_bright;
89 schorsch 2.29 static void copyfile(FILE *in, FILE *out);
90     static void pass1(FILE *in);
91     static void pass2(FILE *in);
92     static void scan2init(void);
93     static void scan2sync(int r);
94     static void scan2flush(void);
95 schorsch 2.28
96 greg 1.1
97 schorsch 2.29 int
98     main(
99     int argc,
100     char **argv
101     )
102 greg 1.1 {
103     FILE *fin;
104 greg 1.13 float *lampcolor;
105     char *lamptype = NULL;
106 greg 1.1 long fpos;
107 greg 2.4 double outaspect = 0.0;
108     double d;
109 greg 2.13 int i, j;
110 schorsch 2.23 SET_DEFAULT_BINARY();
111     SET_FILE_BINARY(stdin);
112     SET_FILE_BINARY(stdout);
113 greg 1.1 if (signal(SIGINT, quit) == SIG_IGN)
114     signal(SIGINT, SIG_IGN);
115 schorsch 2.24 #ifdef SIGHUP
116 greg 1.1 if (signal(SIGHUP, quit) == SIG_IGN)
117 greg 2.11 signal(SIGHUP, SIG_IGN);
118 schorsch 2.24 #endif
119 greg 1.1 signal(SIGTERM, quit);
120 schorsch 2.24 #ifdef SIGPIPE
121 greg 1.1 signal(SIGPIPE, quit);
122 schorsch 2.24 #endif
123 greg 2.4 #ifdef SIGXCPU
124 greg 1.1 signal(SIGXCPU, quit);
125     signal(SIGXFSZ, quit);
126     #endif
127    
128 greg 2.6 progname = argv[0] = fixargv0(argv[0]);
129 greg 1.1
130     for (i = 1; i < argc; i++)
131     if (argv[i][0] == '-')
132     switch (argv[i][1]) {
133     case 'x':
134 greg 1.3 i++;
135 greg 1.5 if (argv[i][0] == '/') {
136 greg 1.4 x_c = 1.0/atof(argv[i]+1);
137 greg 1.5 ncols = 0;
138     } else
139 greg 1.3 ncols = atoi(argv[i]);
140 greg 1.1 break;
141     case 'y':
142 greg 1.3 i++;
143 greg 1.5 if (argv[i][0] == '/') {
144 greg 1.4 y_r = 1.0/atof(argv[i]+1);
145 greg 1.5 nrows = 0;
146     } else
147 greg 1.3 nrows = atoi(argv[i]);
148 greg 1.1 break;
149 greg 1.12 case 'c':
150     correctaspect = !correctaspect;
151     break;
152 greg 1.9 case 'p':
153     i++;
154     outaspect = atof(argv[i]);
155     break;
156 greg 1.1 case 'e':
157     if (argv[i+1][0] == '+' || argv[i+1][0] == '-')
158     d = pow(2.0, atof(argv[i+1]));
159     else
160     d = atof(argv[i+1]);
161 greg 1.15 if (d < 1e-20 || d > 1e20) {
162     fprintf(stderr,
163     "%s: exposure out of range\n",
164     argv[0]);
165 greg 2.18 quit(1);
166 greg 1.15 }
167 greg 1.1 switch (argv[i][2]) {
168     case '\0':
169     scalecolor(exposure, d);
170     break;
171     case 'r':
172     colval(exposure,RED) *= d;
173     break;
174     case 'g':
175     colval(exposure,GRN) *= d;
176     break;
177     case 'b':
178     colval(exposure,BLU) *= d;
179     break;
180     default:
181     goto badopt;
182     }
183     i++;
184     break;
185 greg 1.13 case 'f':
186     lampdat = argv[++i];
187     break;
188     case 't':
189     lamptype = argv[++i];
190     break;
191 greg 1.1 case '1':
192     singlepass = 1;
193     break;
194 greg 1.6 case '2':
195     singlepass = 0;
196     break;
197 greg 1.9 case 'n':
198 greg 1.1 npts = atoi(argv[++i]) / 2;
199     break;
200     case 's':
201     spread = atof(argv[++i]);
202     break;
203     case 'a':
204     avghot = !avghot;
205     break;
206     case 'h':
207     hotlvl = atof(argv[++i]);
208     break;
209     case 'r':
210     rad = atof(argv[++i]);
211     break;
212 greg 2.9 case 'm':
213     thresh = atof(argv[++i]);
214     if (rad <= FTINY)
215 greg 2.21 rad = 0.6;
216 greg 2.9 break;
217 greg 1.1 case 'b':
218 greg 2.9 rad = thresh = 0.0;
219 greg 1.1 break;
220     default:;
221     badopt:
222     fprintf(stderr, "%s: unknown option: %s\n",
223     progname, argv[i]);
224     quit(1);
225     break;
226     }
227     else
228     break;
229 greg 1.14 /* get lamp data (if necessary) */
230     if (lamptype != NULL) {
231     if (loadlamps(lampdat) < 0)
232     quit(1);
233     if ((lampcolor = matchlamp(lamptype)) == NULL) {
234     fprintf(stderr, "%s: unknown lamp type\n", lamptype);
235     quit(1);
236     }
237 greg 2.13 for (j = 0; j < 3; j++)
238     if (lampcolor[j] > 1e-4)
239     colval(exposure,j) /= lampcolor[j];
240 greg 1.14 freelamps();
241     }
242     /* open input file */
243 greg 1.1 if (i == argc) {
244     if (singlepass)
245     fin = stdin;
246     else {
247 greg 2.11 tfname = mktemp(template);
248 greg 1.1 if ((fin = fopen(tfname, "w+")) == NULL) {
249     fprintf(stderr, "%s: can't create ", progname);
250     fprintf(stderr, "temp file \"%s\"\n", tfname);
251     quit(1);
252     }
253     copyfile(stdin, fin);
254     if (fseek(fin, 0L, 0) == -1) {
255     fprintf(stderr, "%s: seek fail\n", progname);
256     quit(1);
257     }
258     }
259     } else if (i == argc-1) {
260     if ((fin = fopen(argv[i], "r")) == NULL) {
261     fprintf(stderr, "%s: can't open file \"%s\"\n",
262     progname, argv[i]);
263     quit(1);
264     }
265     } else {
266     fprintf(stderr, "%s: bad # file arguments\n", progname);
267     quit(1);
268     }
269 greg 1.9 /* get header */
270 greg 1.16 getheader(fin, headline, NULL);
271     if (wrongformat) {
272     fprintf(stderr, "%s: input must be a Radiance picture\n",
273     progname);
274     quit(1);
275     }
276 greg 1.1 /* add new header info. */
277     printargs(i, argv, stdout);
278     /* get picture size */
279 greg 1.19 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
280 greg 1.1 fprintf(stderr, "%s: bad picture size\n", progname);
281     quit(1);
282     }
283 greg 1.19 if (!(order & YMAJOR))
284     inpaspect = 1.0/inpaspect;
285 greg 2.17 /* wrap around for cylindrical view? */
286     wrapfilt = gotview && ourview.type == VT_CYL &&
287     ourview.horiz >= 360.-FTINY && order & YMAJOR;
288 greg 1.9 /* compute output resolution */
289     if (ncols <= 0)
290 greg 1.4 ncols = x_c*xres + .5;
291 greg 1.9 if (nrows <= 0)
292 greg 1.4 nrows = y_r*yres + .5;
293 greg 1.9 if (outaspect > .01) {
294     d = inpaspect * yres/xres / outaspect;
295     if (d * ncols > nrows)
296     ncols = nrows / d;
297     else
298     nrows = ncols * d;
299     }
300     x_c = (double)ncols/xres;
301     y_r = (double)nrows/yres;
302 greg 1.1
303 greg 1.9 if (singlepass) { /* skip exposure, etc. */
304 greg 1.1 pass1default();
305     pass2(fin);
306     quit(0);
307     }
308    
309     fpos = ftell(fin); /* save input file position */
310    
311     pass1(fin);
312    
313     if (fseek(fin, fpos, 0) == -1) {
314     fprintf(stderr, "%s: seek fail\n", progname);
315     quit(1);
316     }
317     pass2(fin);
318    
319 greg 2.18 quit(estatus);
320 schorsch 2.29 return estatus; /* pro forma return */
321 greg 1.1 }
322    
323    
324 schorsch 2.29 static double
325     rgb_bright(
326     COLOR clr
327     )
328 greg 2.15 {
329     return(bright(clr));
330     }
331    
332    
333 schorsch 2.29 static double
334     xyz_bright(
335     COLOR clr
336     )
337 greg 2.15 {
338     return(clr[CIEY]);
339     }
340    
341    
342 schorsch 2.30 brightfunc_t *ourbright = rgb_bright;
343 greg 2.15
344 schorsch 2.28 static int
345     headline( /* process line from header */
346     char *s,
347     void *p
348     )
349 greg 1.9 {
350 greg 2.33 char fmt[MAXFMTLEN];
351 greg 1.16
352 greg 1.9 fputs(s, stdout); /* copy to output */
353     if (isaspect(s)) /* get aspect ratio */
354     inpaspect *= aspectval(s);
355 greg 2.15 else if (isexpos(s)) /* get exposure */
356 greg 2.8 hotlvl *= exposval(s);
357 greg 2.15 else if (formatval(fmt, s)) { /* get format */
358     wrongformat = 0;
359     if (!strcmp(COLRFMT, fmt))
360     ourbright = rgb_bright;
361     else if (!strcmp(CIEFMT, fmt))
362     ourbright = xyz_bright;
363     else
364     wrongformat = !globmatch(PICFMT, fmt);
365 greg 2.17 } else if (isview(s) && sscanview(&ourview, s) > 0)
366     gotview++;
367 gwlarson 2.20 return(0);
368 greg 1.9 }
369    
370    
371 schorsch 2.29 static void
372     copyfile( /* copy a file */
373 greg 2.32 FILE *in,
374     FILE *out
375 schorsch 2.29 )
376 greg 1.1 {
377 greg 2.32 int c;
378 greg 1.1
379     while ((c = getc(in)) != EOF)
380     putc(c, out);
381    
382     if (ferror(out)) {
383     fprintf(stderr, "%s: write error in copyfile\n", progname);
384     quit(1);
385     }
386     }
387    
388    
389 schorsch 2.29 static void
390     pass1( /* first pass of picture file */
391     FILE *in
392     )
393 greg 1.1 {
394     int i;
395     COLOR *scan;
396    
397     pass1init();
398    
399     scan = (COLOR *)malloc(xres*sizeof(COLOR));
400     if (scan == NULL) {
401     fprintf(stderr, "%s: out of memory\n", progname);
402     quit(1);
403     }
404     for (i = 0; i < yres; i++) {
405     if (freadscan(scan, xres, in) < 0) {
406 greg 2.7 nrows = (long)nrows * i / yres; /* adjust frame */
407 greg 1.1 if (nrows <= 0) {
408     fprintf(stderr, "%s: empty frame\n", progname);
409     quit(1);
410     }
411     fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
412 greg 2.7 progname, (int)(100L*i/yres));
413 greg 1.1 yres = i;
414 greg 1.11 y_r = (double)nrows/yres;
415 greg 2.18 estatus++;
416 greg 1.1 break;
417     }
418     pass1scan(scan, i);
419     }
420 greg 2.21 free((void *)scan);
421 greg 1.1 }
422    
423    
424 schorsch 2.29 static void
425     pass2( /* last pass on file, write to stdout */
426     FILE *in
427     )
428 greg 1.1 {
429     int yread;
430     int ycent, xcent;
431     int r, c;
432 greg 2.4
433 greg 1.1 pass2init();
434     scan2init();
435     yread = 0;
436     for (r = 0; r < nrows; r++) {
437 greg 2.19 ycent = (r+.5)*yres/nrows;
438 greg 1.1 while (yread <= ycent+yrad) {
439     if (yread < yres) {
440     if (freadscan(scanin[yread%barsize],
441     xres, in) < 0) {
442     fprintf(stderr,
443 greg 2.14 "%s: truncated input (y=%d)\n",
444 greg 1.1 progname, yres-1-yread);
445     quit(1);
446     }
447     pass2scan(scanin[yread%barsize], yread);
448     }
449     yread++;
450     }
451 greg 2.9 if (obarsize > 0)
452     scan2sync(r);
453 greg 1.1 for (c = 0; c < ncols; c++) {
454 greg 2.19 xcent = (c+.5)*xres/ncols;
455 greg 2.9 if (thresh > FTINY)
456     dothresh(xcent, ycent, c, r);
457     else if (rad > FTINY)
458     dogauss(scanout[c], xcent, ycent, c, r);
459     else
460 greg 1.1 dobox(scanout[c], xcent, ycent, c, r);
461     }
462 greg 2.9 if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
463 greg 1.1 fprintf(stderr, "%s: write error in pass2\n", progname);
464     quit(1);
465     }
466     }
467 greg 2.9 /* skip leftover input */
468 greg 1.10 while (yread < yres) {
469     if (freadscan(scanin[0], xres, in) < 0)
470     break;
471     yread++;
472     }
473 greg 2.9 scan2flush(); /* flush output */
474 greg 1.1 }
475    
476    
477 schorsch 2.29 static void
478     scan2init(void) /* prepare scanline arrays */
479 greg 1.1 {
480 greg 1.15 COLOR ctmp;
481 greg 2.4 double d;
482 greg 2.32 int i;
483 greg 1.1
484 greg 2.9 xbrad = xres/ncols/2 + 1;
485     ybrad = yres/nrows/2 + 1;
486     if (rad > FTINY) {
487 greg 1.1 if (nrows >= yres && ncols >= xres)
488     rad *= (y_r + x_c)/2.0;
489    
490 greg 2.9 if (thresh > FTINY) {
491     orad = CHECKRAD*THRESHRAD*rad + 1;
492 greg 2.16 xrad = orad/x_c + xbrad;
493     yrad = orad/y_r + ybrad;
494 greg 2.9 obarsize = 2*orad + 1;
495     } else {
496     xrad = CHECKRAD*rad/x_c + 1;
497     yrad = CHECKRAD*rad/y_r + 1;
498     }
499 greg 1.1 initmask(); /* initialize filter table */
500 greg 2.9 } else {
501     xrad = xbrad;
502     yrad = ybrad;
503 greg 1.1 }
504 greg 1.17 barsize = 2*yrad + 1;
505 greg 1.1 scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
506 greg 2.10 if (scanin == NULL)
507     goto memerr;
508 greg 1.1 for (i = 0; i < barsize; i++) {
509     scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
510 greg 2.10 if (scanin[i] == NULL)
511     goto memerr;
512 greg 1.1 }
513 greg 2.9 if (obarsize > 0) {
514     scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
515     greybar = (float **)malloc(obarsize*sizeof(float *));
516 schorsch 2.26 if ((scoutbar == NULL) | (greybar == NULL))
517 greg 2.10 goto memerr;
518 greg 2.9 for (i = 0; i < obarsize; i++) {
519     scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
520     greybar[i] = (float *)malloc(ncols*sizeof(float));
521 schorsch 2.26 if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
522 greg 2.10 goto memerr;
523 greg 2.9 }
524     } else {
525     scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
526 greg 2.10 if (scanout == NULL)
527     goto memerr;
528 greg 1.1 }
529 greg 1.15 /* record pixel aspect ratio */
530 greg 1.12 if (!correctaspect) {
531 greg 1.19 d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
532 greg 1.15 if (!FEQ(d,1.0))
533 greg 1.12 fputaspect(d, stdout);
534     }
535 greg 1.15 /* record exposure */
536 greg 2.15 d = (*ourbright)(exposure);
537 greg 1.15 if (!FEQ(d,1.0))
538 greg 1.9 fputexpos(d, stdout);
539 greg 1.15 /* record color correction */
540     copycolor(ctmp, exposure);
541     scalecolor(ctmp, 1.0/d);
542     if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) ||
543     !FEQ(colval(ctmp,GRN),colval(ctmp,BLU)))
544     fputcolcor(ctmp, stdout);
545 greg 1.1 printf("\n");
546 greg 1.15 /* write out resolution */
547 greg 1.19 fputresolu(order, ncols, nrows, stdout);
548 greg 2.10 return;
549     memerr:
550     fprintf(stderr, "%s: out of memory\n", progname);
551     quit(1);
552 greg 2.9 }
553    
554    
555 schorsch 2.29 static void
556     scan2sync( /* synchronize grey averages and output scan */
557     int r
558     )
559 greg 2.9 {
560     static int nextrow = 0;
561     COLOR ctmp;
562     int ybot;
563 greg 2.32 int c;
564 greg 2.9 /* average input scanlines */
565 greg 2.16 while (nextrow <= r+orad && nextrow < nrows) {
566 greg 2.19 ybot = (nextrow+.5)*yres/nrows;
567 greg 2.9 for (c = 0; c < ncols; c++) {
568 greg 2.19 dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
569 greg 2.15 greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
570 greg 2.9 }
571     /* and zero output scanline */
572 schorsch 2.25 memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR));
573 greg 2.9 nextrow++;
574     }
575     /* point to top scanline for output */
576     if (r-orad >= 0)
577     scanout = scoutbar[(r-orad)%obarsize];
578     else
579     scanout = NULL;
580     }
581    
582    
583 schorsch 2.29 static void
584     scan2flush(void) /* flush output buffer */
585 greg 2.9 {
586 greg 2.32 int r;
587 greg 2.9
588     for (r = nrows-orad; r < nrows; r++)
589     if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0)
590     break;
591     if (fflush(stdout) < 0) {
592     fprintf(stderr, "%s: write error at end of pass2\n", progname);
593 greg 2.18 quit(1);
594 greg 2.9 }
595 greg 1.1 }
596    
597    
598 greg 2.21 void
599 greg 1.1 quit(code) /* remove temporary file and exit */
600     int code;
601     {
602     if (tfname != NULL)
603     unlink(tfname);
604     exit(code);
605     }