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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pfilt.c,v 2.34 2019/12/28 18:05:14 greg Exp $";
3 #endif
4 /*
5 * pfilt.c - program to post-process picture file.
6 *
7 * 9/26/85
8 * 6/23/93 Added additional buffers for value spreading
9 */
10
11 #include "copyright.h"
12
13 #include <signal.h>
14 #include "pfilt.h"
15 #include "platform.h"
16 #include "paths.h"
17 #include "view.h"
18
19 #define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b))
20
21 double CHECKRAD = 2.0; /* radius to check for filtering */
22
23 #define THRESHRAD 5.0 /* maximum sample spread in output */
24
25 COLOR exposure = WHTCOLOR; /* exposure for the frame */
26
27 double rad = 0.0; /* output pixel radius for filtering */
28
29 double thresh = 0.0; /* maximum contribution for subpixel */
30
31 int nrows = 0; /* number of rows for output */
32 int ncols = 0; /* number of columns for output */
33
34 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
37 int singlepass = 0; /* true means skip first pass */
38
39 int avghot = 0; /* true means average in bright spots */
40
41 double hotlvl = 100.0; /* level considered "hot" */
42
43 int npts = 0; /* (half) number of points for stars */
44
45 double spread = 1e-4; /* spread for star points */
46
47 char *tfname = NULL;
48
49 char template[] = TEMPLATE;
50
51 char *lampdat = "lamp.tab"; /* lamp data file */
52
53 int order; /* scanline ordering of input */
54 int xres, yres; /* resolution of input */
55 double inpaspect = 1.0; /* pixel aspect ratio of input */
56 int correctaspect = 0; /* aspect ratio correction? */
57
58 int wrongformat = 0;
59
60 VIEW ourview = STDVIEW;
61 int gotview = 0;
62 int wrapfilt = 0; /* wrap filter horizontally? */
63
64 int estatus = 0; /* exit status (for non-fatal errors) */
65
66 int xrad; /* x search radius */
67 int yrad; /* y search radius */
68 int xbrad; /* x box size */
69 int ybrad; /* y box size */
70
71 int barsize; /* size of input scan bar */
72 COLOR **scanin; /* input scan bar */
73 COLOR *scanout; /* output scan line */
74 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
79 char *progname;
80
81 static gethfunc headline;
82 static brightfunc_t rgb_bright;
83 static brightfunc_t xyz_bright;
84 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
91
92 int
93 main(
94 int argc,
95 char **argv
96 )
97 {
98 FILE *fin;
99 float *lampcolor;
100 char *lamptype = NULL;
101 long fpos;
102 double outaspect = 0.0;
103 double d;
104 int i, j;
105 SET_DEFAULT_BINARY();
106 SET_FILE_BINARY(stdin);
107 SET_FILE_BINARY(stdout);
108 if (signal(SIGINT, quit) == SIG_IGN)
109 signal(SIGINT, SIG_IGN);
110 #ifdef SIGHUP
111 if (signal(SIGHUP, quit) == SIG_IGN)
112 signal(SIGHUP, SIG_IGN);
113 #endif
114 signal(SIGTERM, quit);
115 #ifdef SIGPIPE
116 signal(SIGPIPE, quit);
117 #endif
118 #ifdef SIGXCPU
119 signal(SIGXCPU, quit);
120 signal(SIGXFSZ, quit);
121 #endif
122
123 progname = argv[0] = fixargv0(argv[0]);
124
125 for (i = 1; i < argc; i++)
126 if (argv[i][0] == '-')
127 switch (argv[i][1]) {
128 case 'x':
129 i++;
130 if (argv[i][0] == '/') {
131 x_c = 1.0/atof(argv[i]+1);
132 ncols = 0;
133 } else
134 ncols = atoi(argv[i]);
135 break;
136 case 'y':
137 i++;
138 if (argv[i][0] == '/') {
139 y_r = 1.0/atof(argv[i]+1);
140 nrows = 0;
141 } else
142 nrows = atoi(argv[i]);
143 break;
144 case 'c':
145 correctaspect = !correctaspect;
146 break;
147 case 'p':
148 i++;
149 outaspect = atof(argv[i]);
150 break;
151 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 if (d < 1e-20 || d > 1e20) {
157 fprintf(stderr,
158 "%s: exposure out of range\n",
159 argv[0]);
160 quit(1);
161 }
162 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 case 'f':
181 lampdat = argv[++i];
182 break;
183 case 't':
184 lamptype = argv[++i];
185 break;
186 case '1':
187 singlepass = 1;
188 break;
189 case '2':
190 singlepass = 0;
191 break;
192 case 'n':
193 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 case 'm':
208 thresh = atof(argv[++i]);
209 if (rad <= FTINY)
210 rad = 0.6;
211 break;
212 case 'b':
213 rad = thresh = 0.0;
214 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 /* 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 for (j = 0; j < 3; j++)
233 if (lampcolor[j] > 1e-4)
234 colval(exposure,j) /= lampcolor[j];
235 freelamps();
236 }
237 /* open input file */
238 if (i == argc) {
239 if (singlepass)
240 fin = stdin;
241 else {
242 tfname = mktemp(template);
243 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 /* get header */
265 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 /* add new header info. */
272 printargs(i, argv, stdout);
273 /* get picture size */
274 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
275 fprintf(stderr, "%s: bad picture size\n", progname);
276 quit(1);
277 }
278 if (!(order & YMAJOR))
279 inpaspect = 1.0/inpaspect;
280 /* wrap around for cylindrical view? */
281 wrapfilt = gotview && ourview.type == VT_CYL &&
282 ourview.horiz >= 360.-FTINY && order & YMAJOR;
283 /* compute output resolution */
284 if (ncols <= 0)
285 ncols = x_c*xres + .5;
286 if (nrows <= 0)
287 nrows = y_r*yres + .5;
288 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
298 if (singlepass) { /* skip exposure, etc. */
299 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 quit(estatus);
315 return estatus; /* pro forma return */
316 }
317
318
319 static double
320 rgb_bright(
321 COLOR clr
322 )
323 {
324 return(bright(clr));
325 }
326
327
328 static double
329 xyz_bright(
330 COLOR clr
331 )
332 {
333 return(clr[CIEY]);
334 }
335
336
337 brightfunc_t *ourbright = rgb_bright;
338
339 static int
340 headline( /* process line from header */
341 char *s,
342 void *p
343 )
344 {
345 char fmt[MAXFMTLEN];
346
347 fputs(s, stdout); /* copy to output */
348 if (isaspect(s)) /* get aspect ratio */
349 inpaspect *= aspectval(s);
350 else if (isexpos(s)) /* get exposure */
351 hotlvl *= exposval(s);
352 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 } else if (isview(s) && sscanview(&ourview, s) > 0)
361 gotview++;
362 return(0);
363 }
364
365
366 static void
367 copyfile( /* copy a file */
368 FILE *in,
369 FILE *out
370 )
371 {
372 int c;
373
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 static void
385 pass1( /* first pass of picture file */
386 FILE *in
387 )
388 {
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 nrows = (long)nrows * i / yres; /* adjust frame */
402 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 progname, (int)(100L*i/yres));
408 yres = i;
409 y_r = (double)nrows/yres;
410 estatus++;
411 break;
412 }
413 pass1scan(scan, i);
414 }
415 free((void *)scan);
416 }
417
418
419 static void
420 pass2( /* last pass on file, write to stdout */
421 FILE *in
422 )
423 {
424 int yread;
425 int ycent, xcent;
426 int r, c;
427
428 pass2init();
429 scan2init();
430 yread = 0;
431 for (r = 0; r < nrows; r++) {
432 ycent = (r+.5)*yres/nrows;
433 while (yread <= ycent+yrad) {
434 if (yread < yres) {
435 if (freadscan(scanin[yread%barsize],
436 xres, in) < 0) {
437 fprintf(stderr,
438 "%s: truncated input (y=%d)\n",
439 progname, yres-1-yread);
440 quit(1);
441 }
442 pass2scan(scanin[yread%barsize], yread);
443 }
444 yread++;
445 }
446 if (obarsize > 0)
447 scan2sync(r);
448 for (c = 0; c < ncols; c++) {
449 xcent = (c+.5)*xres/ncols;
450 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 dobox(scanout[c], xcent, ycent, c, r);
456 }
457 if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
458 fprintf(stderr, "%s: write error in pass2\n", progname);
459 quit(1);
460 }
461 }
462 /* skip leftover input */
463 while (yread < yres) {
464 if (freadscan(scanin[0], xres, in) < 0)
465 break;
466 yread++;
467 }
468 scan2flush(); /* flush output */
469 }
470
471
472 static void
473 scan2init(void) /* prepare scanline arrays */
474 {
475 COLOR ctmp;
476 double d;
477 int i;
478
479 xbrad = xres/ncols/2 + 1;
480 ybrad = yres/nrows/2 + 1;
481 if (rad > FTINY) {
482 if (nrows >= yres && ncols >= xres)
483 rad *= (y_r + x_c)/2.0;
484
485 if (thresh > FTINY) {
486 orad = CHECKRAD*THRESHRAD*rad + 1;
487 xrad = orad/x_c + xbrad;
488 yrad = orad/y_r + ybrad;
489 obarsize = 2*orad + 1;
490 } else {
491 xrad = CHECKRAD*rad/x_c + 1;
492 yrad = CHECKRAD*rad/y_r + 1;
493 }
494 initmask(); /* initialize filter table */
495 } else {
496 xrad = xbrad;
497 yrad = ybrad;
498 }
499 barsize = 2*yrad + 1;
500 scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
501 if (scanin == NULL)
502 goto memerr;
503 for (i = 0; i < barsize; i++) {
504 scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
505 if (scanin[i] == NULL)
506 goto memerr;
507 }
508 if (obarsize > 0) {
509 scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
510 greybar = (float **)malloc(obarsize*sizeof(float *));
511 if ((scoutbar == NULL) | (greybar == NULL))
512 goto memerr;
513 for (i = 0; i < obarsize; i++) {
514 scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
515 greybar[i] = (float *)malloc(ncols*sizeof(float));
516 if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
517 goto memerr;
518 }
519 } else {
520 scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
521 if (scanout == NULL)
522 goto memerr;
523 }
524 /* record pixel aspect ratio */
525 if (!correctaspect) {
526 d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
527 if (!FEQ(d,1.0))
528 fputaspect(d, stdout);
529 }
530 /* record exposure */
531 d = (*ourbright)(exposure);
532 if (!FEQ(d,1.0))
533 fputexpos(d, stdout);
534 /* 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 printf("\n");
541 /* write out resolution */
542 fputresolu(order, ncols, nrows, stdout);
543 return;
544 memerr:
545 fprintf(stderr, "%s: out of memory\n", progname);
546 quit(1);
547 }
548
549
550 static void
551 scan2sync( /* synchronize grey averages and output scan */
552 int r
553 )
554 {
555 static int nextrow = 0;
556 COLOR ctmp;
557 int ybot;
558 int c;
559 /* average input scanlines */
560 while (nextrow <= r+orad && nextrow < nrows) {
561 ybot = (nextrow+.5)*yres/nrows;
562 for (c = 0; c < ncols; c++) {
563 dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
564 greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
565 }
566 /* and zero output scanline */
567 memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR));
568 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 static void
579 scan2flush(void) /* flush output buffer */
580 {
581 int r;
582
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 quit(1);
589 }
590 }
591
592
593 void
594 quit(code) /* remove temporary file and exit */
595 int code;
596 {
597 if (tfname != NULL)
598 unlink(tfname);
599 exit(code);
600 }