ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.39
Committed: Fri Dec 15 01:57:45 2023 UTC (5 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.38: +78 -76 lines
Log Message:
refactor(pfilt): minor code fixes and reorg

File Contents

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