ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.35
Committed: Thu Dec 7 21:15:54 2023 UTC (4 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +3 -8 lines
Log Message:
refactor: Cleaned up and simplified #include's

File Contents

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