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

Comparing ray/src/px/pcomb.c (file contents):
Revision 2.41 by greg, Mon Nov 5 23:40:26 2007 UTC vs.
Revision 2.57 by greg, Sat Dec 9 23:46:51 2023 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9  
10   #include "platform.h"
11   #include "standard.h"
12 < #include "rtprocess.h"
12 > #include "paths.h"
13   #include "color.h"
14   #include "calcomp.h"
15   #include "view.h"
16  
17 < #define MAXINP          512             /* maximum number of input files */
18 < #define WINSIZ          64              /* scanline window size */
17 > #define MAXINP          1024            /* maximum number of input files */
18 > #define WINSIZ          127             /* scanline window size */
19   #define MIDSCN          ((WINSIZ-1)/2+1)
20  
21   struct {
# Line 23 | Line 23 | struct {
23          FILE    *fp;            /* stream pointer */
24          VIEW    vw;             /* view for picture */
25          RESOLU  rs;             /* image resolution and orientation */
26 +        int     infloat;        /* input is floating point (#comp)? */
27          float   pa;             /* pixel aspect ratio */
28          COLOR   *scan[WINSIZ];  /* input scanline window */
29          COLOR   coef;           /* coefficient */
# Line 31 | Line 32 | struct {
32  
33   int     nfiles;                         /* number of input files */
34  
35 < char    ourfmt[LPICFMT+1] = PICFMT;     /* input picture format */
35 > VIEW    *commvp = NULL;                 /* common view parameters */
36  
37 + char    ourfmt[MAXFMTLEN] = PICFMT;     /* input picture format */
38 + int     outfloat = 0;                   /* #component float output? */
39 +
40   char    StandardInput[] = "<stdin>";
41   char    Command[] = "<Command>";
42   char    vcolin[3][4] = {"ri", "gi", "bi"};
# Line 74 | Line 78 | int    gotview;
78  
79  
80   static gethfunc headline;
81 < static void checkfile(void);
81 > static void checkfile(int orig);
82   static double rgb_bright(COLOR  clr);
83   static double xyz_bright(COLOR  clr);
84   static void init(void);
# Line 96 | Line 100 | main(
100          int     original;
101          double  f;
102          int     a;
103 +
104          SET_DEFAULT_BINARY();
105          SET_FILE_BINARY(stdin);
106          SET_FILE_BINARY(stdout);
107          progname = argv[0];
108 +        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
109 +        esupport &= ~(E_OUTCHAN|E_INCHAN);
110                                                  /* scan options */
111          for (a = 1; a < argc; a++) {
112                  if (argv[a][0] == '-')
# Line 115 | Line 122 | main(
122                                  echoheader = !echoheader;
123                                  continue;
124                          case 'f':
125 +                                if (argv[a][2]) {
126 +                                        outfloat = (argv[a][2] == 'f');
127 +                                        continue;
128 +                                }
129 +                                /* fall through */
130                          case 'e':
131                                  a++;
132                                  continue;
# Line 172 | Line 184 | main(
184                                  quit(1);
185                          }
186                  }
187 <                checkfile();
176 <                if (original) {
177 <                        colval(input[nfiles].coef,RED) /=
178 <                                        colval(input[nfiles].expos,RED);
179 <                        colval(input[nfiles].coef,GRN) /=
180 <                                        colval(input[nfiles].expos,GRN);
181 <                        colval(input[nfiles].coef,BLU) /=
182 <                                        colval(input[nfiles].expos,BLU);
183 <                        setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
184 <                }
187 >                checkfile(original);
188                  nfiles++;
189                  original = 0;
190          }
191          init();                         /* set constants */
192                                          /* go back and get expressions */
193          for (a = 1; a < argc; a++) {
194 +                char    *fpath;
195                  if (argv[a][0] == '-')
196                          switch (argv[a][1]) {
197                          case 'x':
# Line 201 | Line 205 | main(
205                          case 'h':
206                                  continue;
207                          case 'f':
208 <                                fcompile(argv[++a]);
208 >                                if (argv[a][2])
209 >                                        continue;
210 >                                fpath = getpath(argv[++a], getrlibpath(), 0);
211 >                                if (fpath == NULL) {
212 >                                        eputs(argv[0]);
213 >                                        eputs(": cannot find file '");
214 >                                        eputs(argv[a]);
215 >                                        eputs("'\n");
216 >                                        quit(1);
217 >                                }
218 >                                fcompile(fpath);
219                                  continue;
220                          case 'e':
221                                  scompile(argv[++a], NULL, 0);
# Line 209 | Line 223 | main(
223                          }
224                  break;
225          }
226 <                                                /* set/get output resolution */
213 <        if (!vardefined(vxres))
214 <                varset(vxres, ':', (double)xmax);
215 <        if (!vardefined(vyres))
216 <                varset(vyres, ':', (double)ymax);
226 >                                                /* get output resolution */
227          xres = varvalue(vxres) + .5;
228          yres = varvalue(vyres) + .5;
229          if (xres <= 0 || yres <= 0) {
# Line 221 | Line 231 | main(
231                  eputs(": illegal output resolution\n");
232                  quit(1);
233          }
234 <                                                /* complete header */
235 <        printargs(argc, argv, stdout);
236 <        if (strcmp(ourfmt, PICFMT))
237 <                fputformat(ourfmt, stdout);     /* print format if known */
238 <        putchar('\n');
239 <        fprtresolu(xres, yres, stdout);
240 <                                                /* combine pictures */
241 <        combine();
234 >        if (!vardefined(vbrtout))               /* single or 3-channel? */
235 >                outfloat *= 3;
236 >
237 >        printargs(argc, argv, stdout);          /* complete header */
238 >        if (commvp != NULL) {
239 >                fputs(VIEWSTR, stdout);
240 >                fprintview(commvp, stdout);
241 >                fputc('\n', stdout);
242 >        }
243 >        if (outfloat) {                         /* print format if known */
244 >                printf("NROWS=%d\nNCOLS=%d\n", yres, xres);
245 >                fputncomp(outfloat, stdout);
246 >                fputendian(stdout);
247 >                fputformat("float", stdout);
248 >        } else if (strcmp(ourfmt, PICFMT))
249 >                fputformat(ourfmt, stdout);
250 >        fputc('\n', stdout);                    /* end header */
251 >        if (!outfloat)
252 >                fprtresolu(xres, yres, stdout);
253 >
254 >        combine();                              /* combine pictures */
255          quit(0);
256   usage:
257          eputs("Usage: ");
258          eputs(argv[0]);
259          eputs(
260 < " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] pic ..]\n");
260 > " [-w][-h][-ff][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] hdr ..]\n");
261          quit(1);
262          return 1; /* pro forma return */
263   }
# Line 246 | Line 269 | headline(                      /* check header line & echo if requested *
269          void    *p
270   )
271   {
272 <        char    fmt[32];
272 >        int     orig = *(int *)p;
273 >        char    fmt[MAXFMTLEN];
274          double  d;
275 +        int     bigend;
276          COLOR   ctmp;
277  
278          if (isheadid(s))                        /* header id */
# Line 256 | Line 281 | headline(                      /* check header line & echo if requested *
281                  if (globmatch(ourfmt, fmt)) {
282                          wrongformat = 0;
283                          strcpy(ourfmt, fmt);
284 <                } else
284 >                } else if (!strcmp("float", fmt))
285 >                        input[nfiles].infloat *= -1;
286 >                else
287                          wrongformat = globmatch(PICFMT, fmt) ? 1 : -1;
288                  return(0);      /* don't echo */
289          }
290 <        if (isexpos(s)) {                       /* exposure */
290 >        if ((bigend = isbigendian(s)) >= 0) {
291 >                if (bigend != nativebigendian()) {
292 >                        eputs(input[nfiles].name);
293 >                        eputs(": unsupported input byte ordering\n");
294 >                        quit(1);
295 >                }
296 >                return(0);      /* don't echo */
297 >        }
298 >        if (!strncmp("NROWS=", s, 6)) {         /* X-resolution */
299 >                input[nfiles].rs.yr = atoi(s+6);
300 >                return(0);      /* don't echo */
301 >        }
302 >        if (!strncmp("NCOLS=", s, 6)) {         /* Y-resolution */
303 >                input[nfiles].rs.xr = atoi(s+6);
304 >                return(0);      /* don't echo */
305 >        }
306 >        if (isncomp(s))                         /* # components */
307 >                input[nfiles].infloat *= ncompval(s);
308 >
309 >        if (orig) {                             /* undo exposure? */
310 >                if (isexpos(s)) {
311 >                        d = 1./exposval(s);
312 >                        scalecolor(input[nfiles].coef, d);
313 >                        return(0);      /* don't echo */
314 >                }
315 >                if (iscolcor(s)) {
316 >                        int     i;
317 >                        colcorval(ctmp, s);
318 >                        for (i = 3; i--; )
319 >                                colval(input[nfiles].coef,i) /= colval(ctmp,i);
320 >                        return(0);      /* don't echo */
321 >                }
322 >        } else if (isexpos(s)) {                /* record exposure? */
323                  d = exposval(s);
324                  scalecolor(input[nfiles].expos, d);
325 <        } else if (iscolcor(s)) {               /* color correction */
325 >        } else if (iscolcor(s)) {
326                  colcorval(ctmp, s);
327                  multcolor(input[nfiles].expos, ctmp);
328 <        } else if (isaspect(s))
328 >        }
329 >        if (isaspect(s))
330                  input[nfiles].pa *= aspectval(s);
331          else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0)
332                  gotview++;
# Line 280 | Line 340 | headline(                      /* check header line & echo if requested *
340  
341  
342   static void
343 < checkfile(void)                 /* ready a file */
343 > checkfile(int orig)                     /* ready a file */
344   {
345 <        register int    i;
345 >        int     i;
346                                          /* process header */
347          gotview = 0;
348 +        input[nfiles].infloat = -1;
349 +        input[nfiles].rs.rt = PIXSTANDARD;
350 +        input[nfiles].rs.xr = input[nfiles].rs.yr = 0;
351          if (echoheader) {
352                  fputs(input[nfiles].name, stdout);
353                  fputs(":\n", stdout);
354          }
355 <        getheader(input[nfiles].fp, headline, NULL);
355 >        getheader(input[nfiles].fp, headline, &orig);
356 >
357 >        if (input[nfiles].infloat <= 0)
358 >                input[nfiles].infloat = 0;
359 >        else if ((input[nfiles].infloat != 3) &
360 >                                (input[nfiles].infloat != 1)) {
361 >                eputs(input[nfiles].name);
362 >                eputs(": unsupported number of components\n");
363 >                quit(1);
364 >        }
365          if (wrongformat < 0) {
366                  eputs(input[nfiles].name);
367 <                eputs(": not a Radiance picture\n");
367 >                eputs(": not a Radiance picture or float matrix\n");
368                  quit(1);
369          }
370          if (wrongformat > 0) {
# Line 301 | Line 373 | checkfile(void)                        /* ready a file */
373          }
374          if (!gotview || setview(&input[nfiles].vw) != NULL)
375                  input[nfiles].vw.type = 0;
376 <        if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) {
376 >        else if (commvp == NULL)
377 >                commvp = &input[nfiles].vw;
378 >        if ((input[nfiles].rs.xr <= 0) | (input[nfiles].rs.yr <= 0) &&
379 >                        !fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) {
380                  eputs(input[nfiles].name);
381                  eputs(": bad picture size\n");
382                  quit(1);
# Line 345 | Line 420 | double (*ourbright)() = rgb_bright;
420   static void
421   init(void)                                      /* perform final setup */
422   {
423 <        register int    i;
423 >        int     i;
424                                                  /* define constants */
425          varset("PI", ':', PI);
426          varset(vnfiles, ':', (double)nfiles);
# Line 368 | Line 443 | init(void)                                     /* perform final setup */
443                  ourbright = xyz_bright;
444          } else
445                  varset(vwhteff, ':', WHTEFFICACY);
446 +                                                /* these may be overridden */
447 +        varset(vxres, ':', (double)xmax);
448 +        varset(vyres, ':', (double)ymax);
449   }
450  
451  
# Line 377 | Line 455 | combine(void)                  /* combine pictures */
455          EPNODE  *coldef[3], *brtdef;
456          COLOR   *scanout;
457          double  d;
458 <        register int    i, j;
458 >        int     i, j;
459                                                  /* check defined variables */
460          for (j = 0; j < 3; j++) {
461                  if (vardefined(vcolout[j]))
# Line 398 | Line 476 | combine(void)                  /* combine pictures */
476              advance();
477              varset(vypos, '=', (double)ypos);
478              for (xpos = 0; xpos < xres; xpos++) {
479 <                xscan = (xpos+.5)*xmax/xres - .4999;
479 >                xscan = (xpos+.5)*xmax/xres;
480                  varset(vxpos, '=', (double)xpos);
481                  eclock++;
482                  if (brtdef != NULL) {
483                      d = evalue(brtdef);
484 <                    if (d < 0.0)
407 <                        d = 0.0;
484 >                    d *= (outfloat > 0) | (d >= 0);
485                      setcolor(scanout[xpos], d, d, d);
486                  } else {
487                      for (j = 0; j < 3; j++) {
# Line 415 | Line 492 | combine(void)                  /* combine pictures */
492                              for (i = 0; i < nfiles; i++)
493                                  d += colval(input[i].scan[MIDSCN][xscan],j);
494                          }
495 <                        if (d < 0.0)
419 <                            d = 0.0;
495 >                        d *= (outfloat > 0) | (d >= 0);
496                          colval(scanout[xpos],j) = d;
497                      }
498                  }
499              }
500 <            if (fwritescan(scanout, xres, stdout) < 0) {
501 <                    perror("write error");
502 <                    quit(1);
500 >            switch (outfloat) {
501 >            case 3:                     /* writing out float triplets */
502 >                if (fwrite(scanout, sizeof(float)*3, xres, stdout) != xres)
503 >                    goto writerr;
504 >                break;
505 >            case 1:                     /* writing out gray float values */
506 >                for (xpos = 0; xpos < xres; xpos++)
507 >                    if (putbinary(&scanout[xpos][CIEY],
508 >                                sizeof(float), 1, stdout) != 1)
509 >                        goto writerr;
510 >                break;
511 >            default:                    /* writing out Radiance picture */
512 >                if (fwritescan(scanout, xres, stdout) < 0)
513 >                    goto writerr;
514 >                break;
515              }
516          }
517          efree((char *)scanout);
518 +        return;
519 + writerr:
520 +    perror("write error");
521 +    quit(1);
522   }
523  
524  
# Line 434 | Line 526 | static void
526   advance(void)                   /* read in data for next scanline */
527   {
528          int     ytarget;
529 <        register COLOR  *st;
530 <        register int    i, j;
529 >        COLOR   *st;
530 >        int     i, j;
531  
532 <        for (ytarget = (ypos+.5)*ymax/yres - .4999; yscan > ytarget; yscan--)
532 >        for (ytarget = (ypos+.5)*ymax/yres; yscan > ytarget; yscan--)
533                  for (i = 0; i < nfiles; i++) {
534                          st = input[i].scan[WINSIZ-1];
535                          for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
# Line 445 | Line 537 | advance(void)                  /* read in data for next scanline */
537                          input[i].scan[0] = st;
538                          if (yscan <= MIDSCN)            /* hit bottom? */
539                                  continue;
540 <                        if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */
541 <                                eputs(input[i].name);
542 <                                eputs(": read error\n");
543 <                                quit(1);
544 <                        }
540 >                                                         /* read scanline */
541 >                        if (input[i].infloat) {
542 >                                if (fread(st, sizeof(float)*input[i].infloat,
543 >                                                xmax, input[i].fp) != xmax)
544 >                                        goto readerr;
545 >                                if (input[i].infloat == 1) {
546 >                                        const COLORV    *f = st[0] + xmax;
547 >                                        int             x = xmax;
548 >                                        while (x-- > 0)
549 >                                                st[x][BLU] = st[x][GRN] =
550 >                                                        st[x][RED] = *--f;
551 >                                }
552 >                        } else if (freadscan(st, xmax, input[i].fp) < 0)
553 >                                goto readerr;
554                          for (j = 0; j < xmax; j++)      /* adjust color */
555                                  multcolor(st[j], input[i].coef);
556                  }
557 +        return;
558 + readerr:
559 +        eputs(input[i].name);
560 +        eputs(": read error\n");
561 +        quit(1);
562   }
563  
564  
565   static double
566   l_expos(                        /* return picture exposure */
567 <        register char   *nam
567 >        char    *nam
568   )
569   {
570 <        register int    fn, n;
570 >        int     fn, n;
571 >        double  d;
572  
573 <        fn = argument(1) - .5;
574 <        if (fn < 0 || fn >= nfiles)
575 <                return(1.0);
573 >        d = argument(1);
574 >        if (d <= -0.5 || d >= nfiles+0.5) {
575 >                errno = EDOM;
576 >                return(0.0);
577 >        }
578 >        if (d < 0.5)
579 >                return((double)nfiles);
580 >        fn = d - 0.5;
581          if (nam == vbrtexp)
582                  return((*ourbright)(input[fn].expos));
583          n = 3;
# Line 481 | Line 593 | l_expos(                       /* return picture exposure */
593   static double
594   l_pixaspect(char *nm)           /* return pixel aspect ratio */
595   {
596 <        register int    fn;
596 >        int     fn;
597 >        double  d;
598  
599 <        fn = argument(1) - .5;
600 <        if (fn < 0 || fn >= nfiles)
601 <                return(1.0);
599 >        d = argument(1);
600 >        if (d <= -0.5 || d >= nfiles+0.5) {
601 >                errno = EDOM;
602 >                return(0.0);
603 >        }
604 >        if (d < 0.5)
605 >                return((double)nfiles);
606 >        fn = d - 0.5;
607          return(input[fn].pa);
608   }
609  
610  
611   static double
612   l_colin(                        /* return color value for picture */
613 <        register char   *nam
613 >        char    *nam
614   )
615   {
616          int     fn;
617 <        register int    n, xoff, yoff;
617 >        int     n, xoff, yoff;
618          double  d;
619  
620 <        fn = argument(1) - .5;
621 <        if (fn < 0 || fn >= nfiles) {
620 >        d = argument(1);
621 >        if (d <= -0.5 || d >= nfiles+0.5) {
622                  errno = EDOM;
623                  return(0.0);
624          }
625 +        if (d < 0.5)
626 +                return((double)nfiles);
627 +        fn = d - 0.5;
628          xoff = yoff = 0;
629          n = nargum();
630          if (n >= 2) {
# Line 548 | Line 669 | l_colin(                       /* return color value for picture */
669  
670   static double
671   l_ray(          /* return ray origin or direction */
672 <        register char   *nam
672 >        char    *nam
673   )
674   {
675          static unsigned long    ltick[MAXINP];
676          static FVECT    lorg[MAXINP], ldir[MAXINP];
677          static double   ldist[MAXINP];
678          RREAL   loc[2];
679 +        double  d;
680          int     fn;
681 <        register int    i;
681 >        int     i;
682  
683 <        fn = argument(1) - .5;
684 <        if (fn < 0 || fn >= nfiles) {
683 >        d = argument(1);
684 >        if (d <= -0.5 || d >= nfiles+0.5) {
685                  errno = EDOM;
686                  return(0.0);
687          }
688 +        if (d < 0.5)
689 +                return((double)nfiles);
690 +        fn = d - 0.5;
691          if (ltick[fn] != eclock) {              /* need to compute? */
692 <                lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0;
693 <                ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0;
694 <                ldist[fn] = -1.0;
695 <                if (input[fn].vw.type == 0)
692 >                if (input[fn].vw.type == 0) {
693 >                        lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0;
694 >                        ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0;
695 >                        ldist[fn] = -1.0;
696                          errno = EDOM;
697 <                else {
697 >                } else {
698                          pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan);
699                          ldist[fn] = viewray(lorg[fn], ldir[fn],
700                                          &input[fn].vw, loc[0], loc[1]);
701 +                        if (ldist[fn] < -FTINY)
702 +                                errno = EDOM;
703                  }
704                  ltick[fn] = eclock;
705          }
# Line 596 | Line 723 | l_psize(char *nm)              /* compute pixel size in steradians
723          RREAL   locx[2], locy[2];
724          double  d;
725          int     fn;
726 <        register int    i;
726 >        int     i;
727  
728          d = argument(1);
729 <        if (d < .5 || d >= nfiles+.5) {
729 >        if (d <= -0.5 || d >= nfiles+0.5) {
730                  errno = EDOM;
731                  return(0.0);
732          }
733 <        fn = d - .5;
733 >        if (d < 0.5)
734 >                return((double)nfiles);
735 >        fn = d - 0.5;
736          if (ltick[fn] != eclock) {              /* need to compute? */
737                  psize[fn] = 0.0;
738                  if (input[fn].vw.type == 0)
# Line 634 | Line 763 | l_psize(char *nm)              /* compute pixel size in steradians
763  
764  
765   extern void
766 < wputs(char      *msg)
766 > wputs(const char *msg)
767   {
768          if (!nowarn)
769                  eputs(msg);
# Line 642 | Line 771 | wputs(char     *msg)
771  
772  
773   extern void
774 < eputs(char *msg)
774 > eputs(const char *msg)
775   {
776          fputs(msg, stderr);
777   }
# Line 651 | Line 780 | eputs(char *msg)
780   extern void
781   quit(int code)          /* exit gracefully */
782   {
783 <        register int  i;
783 >        int  i;
784                                  /* close input files */
785          for (i = 0; i < nfiles; i++)
786                  if (input[i].name == Command)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines