ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
(Generate patch)

Comparing ray/src/px/pfilt.c (file contents):
Revision 1.10 by greg, Thu Sep 13 09:46:00 1990 UTC vs.
Revision 2.28 by schorsch, Fri Jan 2 12:47:01 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
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  <stdio.h>
11 > #include  "copyright.h"
12  
13   #include  <signal.h>
14 + #include  <string.h>
15  
16 + #include  "platform.h"
17 + #include  "standard.h"
18   #include  "color.h"
19 + #include  "view.h"
20 + #include  "paths.h"
21  
22 < extern char  *malloc();
22 > extern float  *matchlamp();
23  
24 < #define  CHECKRAD       1.5     /* radius to check for filtering */
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 */
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 */
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 = 1000.0;        /* level considered "hot" */
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 */
50 > double  spread = 1e-4;          /* spread for star points */
51  
43 #define  TEMPLATE       "/usr/tmp/pfXXXXXX"
44
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 */
60 > double  inpaspect = 1.0;        /* pixel aspect ratio of input */
61 > int  correctaspect = 0;         /* aspect ratio correction? */
62  
63 < int  xrad;                      /* x window size */
51 < int  yrad;                      /* y window size */
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  
88 +
89   main(argc, argv)
90   int  argc;
91   char  **argv;
92   {
64        extern char  *mktemp();
65        extern double  atof(), pow();
66        extern long  ftell();
67        extern int  quit(), headline();
93          FILE  *fin;
94 +        float  *lampcolor;
95 +        char  *lamptype = NULL;
96          long  fpos;
97 <        double  outaspect = 0.0;
98 <        double  d;
99 <        int  i;
100 <
97 >        double  outaspect = 0.0;
98 >        double  d;
99 >        int  i, j;
100 >        SET_DEFAULT_BINARY();
101 >        SET_FILE_BINARY(stdin);
102 >        SET_FILE_BINARY(stdout);
103          if (signal(SIGINT, quit) == SIG_IGN)
104                  signal(SIGINT, SIG_IGN);
105 + #ifdef SIGHUP
106          if (signal(SIGHUP, quit) == SIG_IGN)
107 <                signal(SIGINT, SIG_IGN);
107 >                signal(SIGHUP, SIG_IGN);
108 > #endif
109          signal(SIGTERM, quit);
110 + #ifdef SIGPIPE
111          signal(SIGPIPE, quit);
112 < #ifdef  SIGXCPU
112 > #endif
113 > #ifdef  SIGXCPU
114          signal(SIGXCPU, quit);
115          signal(SIGXFSZ, quit);
116   #endif
117  
118 <        progname = argv[0];
118 >        progname = argv[0] = fixargv0(argv[0]);
119  
120          for (i = 1; i < argc; i++)
121                  if (argv[i][0] == '-')
# Line 103 | Line 136 | char  **argv;
136                                  } else
137                                          nrows = atoi(argv[i]);
138                                  break;
139 +                        case 'c':
140 +                                correctaspect = !correctaspect;
141 +                                break;
142                          case 'p':
143                                  i++;
144                                  outaspect = atof(argv[i]);
# Line 112 | Line 148 | char  **argv;
148                                          d = pow(2.0, atof(argv[i+1]));
149                                  else
150                                          d = atof(argv[i+1]);
151 +                                if (d < 1e-20 || d > 1e20) {
152 +                                        fprintf(stderr,
153 +                                                "%s: exposure out of range\n",
154 +                                                        argv[0]);
155 +                                        quit(1);
156 +                                }
157                                  switch (argv[i][2]) {
158                                  case '\0':
159                                          scalecolor(exposure, d);
# Line 130 | Line 172 | char  **argv;
172                                  }
173                                  i++;
174                                  break;
175 +                        case 'f':
176 +                                lampdat = argv[++i];
177 +                                break;
178 +                        case 't':
179 +                                lamptype = argv[++i];
180 +                                break;
181                          case '1':
182                                  singlepass = 1;
183                                  break;
# Line 151 | Line 199 | char  **argv;
199                          case 'r':
200                                  rad = atof(argv[++i]);
201                                  break;
202 +                        case 'm':
203 +                                thresh = atof(argv[++i]);
204 +                                if (rad <= FTINY)
205 +                                        rad = 0.6;
206 +                                break;
207                          case 'b':
208 <                                rad = 0.0;
208 >                                rad = thresh = 0.0;
209                                  break;
210                          default:;
211                          badopt:
# Line 163 | Line 216 | char  **argv;
216                          }
217                  else
218                          break;
219 <                        
219 >                                        /* get lamp data (if necessary) */
220 >        if (lamptype != NULL) {
221 >                if (loadlamps(lampdat) < 0)
222 >                        quit(1);
223 >                if ((lampcolor = matchlamp(lamptype)) == NULL) {
224 >                        fprintf(stderr, "%s: unknown lamp type\n", lamptype);
225 >                        quit(1);
226 >                }
227 >                for (j = 0; j < 3; j++)
228 >                        if (lampcolor[j] > 1e-4)
229 >                                colval(exposure,j) /= lampcolor[j];
230 >                freelamps();
231 >        }
232 >                                        /* open input file */
233          if (i == argc) {
234                  if (singlepass)
235                          fin = stdin;
236                  else {
237 <                        tfname = mktemp(TEMPLATE);
237 >                        tfname = mktemp(template);
238                          if ((fin = fopen(tfname, "w+")) == NULL) {
239                                  fprintf(stderr, "%s: can't create ", progname);
240                                  fprintf(stderr, "temp file \"%s\"\n", tfname);
# Line 191 | Line 257 | char  **argv;
257                  quit(1);
258          }
259                                          /* get header */
260 <        getheader(fin, headline);
260 >        getheader(fin, headline, NULL);
261 >        if (wrongformat) {
262 >                fprintf(stderr, "%s: input must be a Radiance picture\n",
263 >                                progname);
264 >                quit(1);
265 >        }
266                                          /* add new header info. */
267          printargs(i, argv, stdout);
268                                          /* get picture size */
269 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
269 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
270                  fprintf(stderr, "%s: bad picture size\n", progname);
271                  quit(1);
272          }
273 +        if (!(order & YMAJOR))
274 +                inpaspect = 1.0/inpaspect;
275 +                                        /* wrap around for cylindrical view? */
276 +        wrapfilt = gotview && ourview.type == VT_CYL &&
277 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
278                                          /* compute output resolution */
279          if (ncols <= 0)
280                  ncols = x_c*xres + .5;
# Line 230 | Line 306 | char  **argv;
306          }
307          pass2(fin);
308  
309 <        quit(0);
309 >        quit(estatus);
310   }
311  
312  
313 < headline(s)                             /* process line from header */
314 < char  *s;
313 > double
314 > rgb_bright(clr)
315 > COLOR  clr;
316   {
317 +        return(bright(clr));
318 + }
319 +
320 +
321 + double
322 + xyz_bright(clr)
323 + COLOR  clr;
324 + {
325 +        return(clr[CIEY]);
326 + }
327 +
328 +
329 + double  (*ourbright)() = rgb_bright;
330 +
331 +
332 + static int
333 + headline(                               /* process line from header */
334 +        char    *s,
335 +        void    *p
336 + )
337 + {
338 +        char  fmt[32];
339 +
340          fputs(s, stdout);               /* copy to output */
341          if (isaspect(s))                /* get aspect ratio */
342                  inpaspect *= aspectval(s);
343 +        else if (isexpos(s))            /* get exposure */
344 +                hotlvl *= exposval(s);
345 +        else if (formatval(fmt, s)) {   /* get format */
346 +                wrongformat = 0;
347 +                if (!strcmp(COLRFMT, fmt))
348 +                        ourbright = rgb_bright;
349 +                else if (!strcmp(CIEFMT, fmt))
350 +                        ourbright = xyz_bright;
351 +                else
352 +                        wrongformat = !globmatch(PICFMT, fmt);
353 +        } else if (isview(s) && sscanview(&ourview, s) > 0)
354 +                gotview++;
355 +        return(0);
356   }
357  
358  
# Line 273 | Line 386 | FILE  *in;
386          }
387          for (i = 0; i < yres; i++) {
388                  if (freadscan(scan, xres, in) < 0) {
389 <                        nrows = nrows * i / yres;       /* adjust frame */
389 >                        nrows = (long)nrows * i / yres; /* adjust frame */
390                          if (nrows <= 0) {
391                                  fprintf(stderr, "%s: empty frame\n", progname);
392                                  quit(1);
393                          }
394                          fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
395 <                                        progname, 100*i/yres);
395 >                                        progname, (int)(100L*i/yres));
396                          yres = i;
397 +                        y_r = (double)nrows/yres;
398 +                        estatus++;
399                          break;
400                  }
401                  pass1scan(scan, i);
402          }
403 <        free((char *)scan);
403 >        free((void *)scan);
404   }
405  
406  
# Line 295 | Line 410 | FILE  *in;
410          int  yread;
411          int  ycent, xcent;
412          int  r, c;
413 <        
413 >        
414          pass2init();
415          scan2init();
416          yread = 0;
417          for (r = 0; r < nrows; r++) {
418 <                ycent = (long)r*yres/nrows;
418 >                ycent = (r+.5)*yres/nrows;
419                  while (yread <= ycent+yrad) {
420                          if (yread < yres) {
421                                  if (freadscan(scanin[yread%barsize],
422                                                  xres, in) < 0) {
423                                          fprintf(stderr,
424 <                                                "%s: bad read (y=%d)\n",
424 >                                                "%s: truncated input (y=%d)\n",
425                                                  progname, yres-1-yread);
426                                          quit(1);
427                                  }
# Line 314 | Line 429 | FILE  *in;
429                          }
430                          yread++;
431                  }
432 +                if (obarsize > 0)
433 +                        scan2sync(r);
434                  for (c = 0; c < ncols; c++) {
435 <                        xcent = (long)c*xres/ncols;
436 <                        if (rad <= 0.0)
437 <                                dobox(scanout[c], xcent, ycent, c, r);
438 <                        else
435 >                        xcent = (c+.5)*xres/ncols;
436 >                        if (thresh > FTINY)
437 >                                dothresh(xcent, ycent, c, r);
438 >                        else if (rad > FTINY)
439                                  dogauss(scanout[c], xcent, ycent, c, r);
440 +                        else
441 +                                dobox(scanout[c], xcent, ycent, c, r);
442                  }
443 <                if (fwritescan(scanout, ncols, stdout) < 0) {
443 >                if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
444                          fprintf(stderr, "%s: write error in pass2\n", progname);
445                          quit(1);
446                  }
447          }
448 <                                        /* skip leftovers */
448 >                                        /* skip leftover input */
449          while (yread < yres) {
450                  if (freadscan(scanin[0], xres, in) < 0)
451                          break;
452                  yread++;
453          }
454 +        scan2flush();                   /* flush output */
455   }
456  
457  
458   scan2init()                     /* prepare scanline arrays */
459   {
460 <        double  d;
460 >        COLOR   ctmp;
461 >        double  d;
462          register int  i;
463  
464 <        if (rad <= 0.0) {
465 <                xrad = xres/ncols/2 + 1;
466 <                yrad = yres/nrows/2 + 1;
346 <        } else {
464 >        xbrad = xres/ncols/2 + 1;
465 >        ybrad = yres/nrows/2 + 1;
466 >        if (rad > FTINY) {
467                  if (nrows >= yres && ncols >= xres)
468                          rad *= (y_r + x_c)/2.0;
469  
470 <                xrad = CHECKRAD*rad/x_c + 1;
471 <                yrad = CHECKRAD*rad/y_r + 1;
472 <
470 >                if (thresh > FTINY) {
471 >                        orad = CHECKRAD*THRESHRAD*rad + 1;
472 >                        xrad = orad/x_c + xbrad;
473 >                        yrad = orad/y_r + ybrad;
474 >                        obarsize = 2*orad + 1;
475 >                } else {
476 >                        xrad = CHECKRAD*rad/x_c + 1;
477 >                        yrad = CHECKRAD*rad/y_r + 1;
478 >                }
479                  initmask();             /* initialize filter table */
480 +        } else {
481 +                xrad = xbrad;
482 +                yrad = ybrad;
483          }
484 <        barsize = 2 * yrad;
484 >        barsize = 2*yrad + 1;
485          scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
486 +        if (scanin == NULL)
487 +                goto memerr;
488          for (i = 0; i < barsize; i++) {
489                  scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
490 <                if (scanin[i] == NULL) {
491 <                        fprintf(stderr, "%s: out of memory\n", progname);
492 <                        quit(1);
490 >                if (scanin[i] == NULL)
491 >                        goto memerr;
492 >        }
493 >        if (obarsize > 0) {
494 >                scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
495 >                greybar = (float **)malloc(obarsize*sizeof(float *));
496 >                if ((scoutbar == NULL) | (greybar == NULL))
497 >                        goto memerr;
498 >                for (i = 0; i < obarsize; i++) {
499 >                        scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
500 >                        greybar[i] = (float *)malloc(ncols*sizeof(float));
501 >                        if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
502 >                                goto memerr;
503                  }
504 +        } else {
505 +                scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
506 +                if (scanout == NULL)
507 +                        goto memerr;
508          }
509 <        scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
510 <        if (scanout == NULL) {
511 <                fprintf(stderr, "%s: out of memory\n", progname);
512 <                quit(1);
509 >                                        /* record pixel aspect ratio */
510 >        if (!correctaspect) {
511 >                d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
512 >                if (!FEQ(d,1.0))
513 >                        fputaspect(d, stdout);
514          }
515 <                                        /* record pixel aspect and exposure */
516 <        d = x_c / y_r;
517 <        if (d < .99 || d > 1.01)
372 <                fputaspect(d, stdout);
373 <        d = bright(exposure);
374 <        if (d < .995 || d > 1.005)
515 >                                        /* record exposure */
516 >        d = (*ourbright)(exposure);
517 >        if (!FEQ(d,1.0))
518                  fputexpos(d, stdout);
519 +                                        /* record color correction */
520 +        copycolor(ctmp, exposure);
521 +        scalecolor(ctmp, 1.0/d);
522 +        if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) ||
523 +                        !FEQ(colval(ctmp,GRN),colval(ctmp,BLU)))
524 +                fputcolcor(ctmp, stdout);
525          printf("\n");
526 <        fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */
526 >                                        /* write out resolution */
527 >        fputresolu(order, ncols, nrows, stdout);
528 >        return;
529 > memerr:
530 >        fprintf(stderr, "%s: out of memory\n", progname);
531 >        quit(1);
532   }
533  
534  
535 + scan2sync(r)                    /* synchronize grey averages and output scan */
536 + int  r;
537 + {
538 +        static int  nextrow = 0;
539 +        COLOR  ctmp;
540 +        int  ybot;
541 +        register int  c;
542 +                                        /* average input scanlines */
543 +        while (nextrow <= r+orad && nextrow < nrows) {
544 +                ybot = (nextrow+.5)*yres/nrows;
545 +                for (c = 0; c < ncols; c++) {
546 +                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
547 +                        greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
548 +                }
549 +                                        /* and zero output scanline */
550 +                memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR));
551 +                nextrow++;
552 +        }
553 +                                        /* point to top scanline for output */
554 +        if (r-orad >= 0)
555 +                scanout = scoutbar[(r-orad)%obarsize];
556 +        else
557 +                scanout = NULL;
558 + }
559 +
560 +
561 + scan2flush()                    /* flush output buffer */
562 + {
563 +        register int  r;
564 +
565 +        for (r = nrows-orad; r < nrows; r++)
566 +                if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0)
567 +                        break;
568 +        if (fflush(stdout) < 0) {
569 +                fprintf(stderr, "%s: write error at end of pass2\n", progname);
570 +                quit(1);
571 +        }
572 + }
573 +
574 +
575 + void
576   quit(code)              /* remove temporary file and exit */
577   int  code;
578   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines