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 (4 months, 2 weeks 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pfilt.c,v 2.38 2023/12/14 19:03:19 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 COLORV **scanin; /* input scan bar */
73 COLORV *scanout; /* output scan line */
74 COLORV **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 void copyfile(FILE *infp, FILE *out);
82 static void pass1(FILE *infp);
83 static void pass2(FILE *infp);
84 static void scan2init(void);
85 static void scan2sync(int r);
86 static void scan2flush(void);
87
88
89 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 int
153 main(
154 int argc,
155 char **argv
156 )
157 {
158 FILE *fin;
159 float *lampcolor;
160 char *lamptype = NULL;
161 long fpos;
162 double outaspect = 0.0;
163 double d;
164 int i, j;
165 SET_DEFAULT_BINARY();
166 SET_FILE_BINARY(stdin);
167 SET_FILE_BINARY(stdout);
168 if (signal(SIGINT, quit) == SIG_IGN)
169 signal(SIGINT, SIG_IGN);
170 #ifdef SIGHUP
171 if (signal(SIGHUP, quit) == SIG_IGN)
172 signal(SIGHUP, SIG_IGN);
173 #endif
174 signal(SIGTERM, quit);
175 #ifdef SIGPIPE
176 signal(SIGPIPE, quit);
177 #endif
178 #ifdef SIGXCPU
179 signal(SIGXCPU, quit);
180 signal(SIGXFSZ, quit);
181 #endif
182
183 progname = argv[0] = fixargv0(argv[0]);
184
185 for (i = 1; i < argc; i++)
186 if (argv[i][0] == '-')
187 switch (argv[i][1]) {
188 case 'x':
189 i++;
190 if (argv[i][0] == '/') {
191 x_c = 1.0/atof(argv[i]+1);
192 ncols = 0;
193 } else
194 ncols = atoi(argv[i]);
195 break;
196 case 'y':
197 i++;
198 if (argv[i][0] == '/') {
199 y_r = 1.0/atof(argv[i]+1);
200 nrows = 0;
201 } else
202 nrows = atoi(argv[i]);
203 break;
204 case 'c':
205 correctaspect = !correctaspect;
206 break;
207 case 'p':
208 i++;
209 outaspect = atof(argv[i]);
210 break;
211 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 if (d < 1e-20 || d > 1e20) {
217 fprintf(stderr,
218 "%s: exposure out of range\n",
219 argv[0]);
220 quit(1);
221 }
222 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 case 'f':
241 lampdat = argv[++i];
242 break;
243 case 't':
244 lamptype = argv[++i];
245 break;
246 case '1':
247 singlepass = 1;
248 break;
249 case '2':
250 singlepass = 0;
251 break;
252 case 'n':
253 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 case 'm':
268 thresh = atof(argv[++i]);
269 if (rad <= FTINY)
270 rad = 0.6;
271 break;
272 case 'b':
273 rad = thresh = 0.0;
274 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 /* 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 for (j = 0; j < 3; j++)
293 if (lampcolor[j] > 1e-4)
294 colval(exposure,j) /= lampcolor[j];
295 freelamps();
296 }
297 /* open input file */
298 if (i == argc) {
299 if (singlepass)
300 fin = stdin;
301 else {
302 tfname = mktemp(template);
303 if ((fin = fopen(tfname, "w+b")) == NULL) {
304 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 if ((fin = fopen(argv[i], "rb")) == NULL) {
316 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 /* get header */
325 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 if ((ourbright == spec_bright) ^ (NCSAMP > 3) ||
332 setspectrsamp(CNDX, WLPART) < 0) {
333 fprintf(stderr, "%s: bad number of components\n", progname);
334 quit(1);
335 }
336 /* add new header info. */
337 printargs(i, argv, stdout);
338 /* get picture size */
339 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
340 fprintf(stderr, "%s: bad picture size\n", progname);
341 quit(1);
342 }
343 if (!(order & YMAJOR))
344 inpaspect = 1.0/inpaspect;
345 /* wrap around for cylindrical view? */
346 wrapfilt = gotview && ourview.type == VT_CYL &&
347 ourview.horiz >= 360.-FTINY && order & YMAJOR;
348 /* compute output resolution */
349 if (ncols <= 0)
350 ncols = x_c*xres + .5;
351 if (nrows <= 0)
352 nrows = y_r*yres + .5;
353 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
363 if (singlepass) { /* skip exposure, etc. */
364 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 quit(estatus);
380 return estatus; /* pro forma return */
381 }
382
383
384 static void
385 copyfile( /* copy a file */
386 FILE *infp,
387 FILE *out
388 )
389 {
390 int c;
391
392 while ((c = getc(infp)) != EOF)
393 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 static void
403 pass1( /* first pass of picture file */
404 FILE *infp
405 )
406 {
407 int i;
408 COLORV *scan;
409
410 pass1init();
411
412 scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
413 if (scan == NULL) {
414 fprintf(stderr, "%s: out of memory\n", progname);
415 quit(1);
416 }
417 for (i = 0; i < yres; i++) {
418 if (freadsscan(scan, NCSAMP, xres, infp) < 0) {
419 nrows = (long)nrows * i / yres; /* adjust frame */
420 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 progname, (int)(100L*i/yres));
426 yres = i;
427 y_r = (double)nrows/yres;
428 estatus++;
429 break;
430 }
431 pass1scan(scan, i);
432 }
433 free(scan);
434 }
435
436
437 static void
438 pass2( /* last pass on file, write to stdout */
439 FILE *infp
440 )
441 {
442 int yread;
443 int ycent, xcent;
444 int r, c;
445
446 pass2init();
447 scan2init();
448 yread = 0;
449 for (r = 0; r < nrows; r++) {
450 ycent = (r+.5)*yres/nrows;
451 while (yread <= ycent+yrad) {
452 if (yread < yres) {
453 if (freadsscan(scanin[yread%barsize],
454 NCSAMP, xres, infp) < 0) {
455 fprintf(stderr,
456 "%s: truncated input (y=%d)\n",
457 progname, yres-1-yread);
458 quit(1);
459 }
460 pass2scan(scanin[yread%barsize], yread);
461 }
462 yread++;
463 }
464 if (obarsize > 0) /* => thresh > FTINY */
465 scan2sync(r);
466 for (c = 0; c < ncols; c++) {
467 xcent = (c+.5)*xres/ncols;
468 if (thresh > FTINY)
469 dothresh(xcent, ycent, c, r);
470 else if (rad > FTINY)
471 dogauss(scanout+c*NCSAMP, xcent, ycent, c, r);
472 else
473 dobox(scanout+c*NCSAMP, xcent, ycent, c, r);
474 }
475 if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) {
476 fprintf(stderr, "%s: write error in pass2\n", progname);
477 quit(1);
478 }
479 }
480 /* skip leftover input */
481 while (yread < yres) {
482 if (freadscolrs((uby8 *)tempbuffer(xres*(NCSAMP+1)),
483 NCSAMP, xres, infp) < 0)
484 break;
485 yread++;
486 }
487 scan2flush(); /* flush output */
488 }
489
490
491 static void
492 scan2init(void) /* prepare scanline arrays */
493 {
494 COLOR ctmp;
495 double d;
496 int i;
497
498 xbrad = xres/ncols/2 + 1;
499 ybrad = yres/nrows/2 + 1;
500 if (rad > FTINY) {
501 if (nrows >= yres && ncols >= xres)
502 rad *= (y_r + x_c)/2.0;
503
504 if (thresh > FTINY) {
505 orad = CHECKRAD*THRESHRAD*rad + 1;
506 xrad = orad/x_c + xbrad;
507 yrad = orad/y_r + ybrad;
508 obarsize = 2*orad + 1;
509 } else {
510 xrad = CHECKRAD*rad/x_c + 1;
511 yrad = CHECKRAD*rad/y_r + 1;
512 }
513 initmask(); /* initialize filter table */
514 } else {
515 xrad = xbrad;
516 yrad = ybrad;
517 }
518 barsize = 2*yrad + 1;
519 scanin = (COLORV **)malloc(barsize*sizeof(COLORV *));
520 if (scanin == NULL)
521 goto memerr;
522 for (i = 0; i < barsize; i++) {
523 scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
524 if (scanin[i] == NULL)
525 goto memerr;
526 }
527 if (obarsize > 0) {
528 scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *));
529 greybar = (float **)malloc(obarsize*sizeof(float *));
530 if ((scoutbar == NULL) | (greybar == NULL))
531 goto memerr;
532 for (i = 0; i < obarsize; i++) {
533 scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
534 greybar[i] = (float *)malloc(ncols*sizeof(float));
535 if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
536 goto memerr;
537 }
538 } else {
539 scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
540 if (scanout == NULL)
541 goto memerr;
542 }
543 /* record pixel aspect ratio */
544 if (!correctaspect) {
545 d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
546 if (!FEQ(d,1.0))
547 fputaspect(d, stdout);
548 }
549 /* record exposure */
550 d = bright(exposure);
551 if (!FEQ(d,1.0))
552 fputexpos(d, stdout);
553 /* 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 printf("\n");
560 /* write out resolution */
561 fputresolu(order, ncols, nrows, stdout);
562 return;
563 memerr:
564 fprintf(stderr, "%s: out of memory\n", progname);
565 quit(1);
566 }
567
568
569 static void
570 scan2sync( /* synchronize grey averages and output scan */
571 int r
572 )
573 {
574 static int nextrow = 0;
575 SCOLOR ctmp;
576 int ybot;
577 int c;
578 /* average input scanlines */
579 while (nextrow <= r+orad && nextrow < nrows) {
580 ybot = (nextrow+.5)*yres/nrows;
581 for (c = 0; c < ncols; c++) {
582 dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
583 greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
584 }
585 /* and zero output scanline */
586 memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV));
587 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 static void
598 scan2flush(void) /* flush output buffer */
599 {
600 int r;
601
602 for (r = nrows-orad; r < nrows; r++)
603 if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0)
604 break;
605 if (fflush(stdout) < 0) {
606 fprintf(stderr, "%s: write error at end of pass2\n", progname);
607 quit(1);
608 }
609 }
610
611
612 void
613 quit(code) /* remove temporary file and exit */
614 int code;
615 {
616 if (tfname != NULL)
617 unlink(tfname);
618 exit(code);
619 }