ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.37
Committed: Sat Dec 9 02:33:14 2023 UTC (4 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.36: +2 -2 lines
Log Message:
fix(pfilt): Need to set primary channels for pbright() to work properly

File Contents

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