ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.41
Committed: Sat Jun 7 05:09:46 2025 UTC (14 hours, 16 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.40: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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