ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.40
Committed: Tue Jun 3 21:31:51 2025 UTC (45 hours, 4 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.39: +4 -7 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

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