ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.34
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3
Changes since 2.33: +1 -2 lines
Log Message:
Removed redundant include files

File Contents

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