home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / radsrc22 / src / px / psum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-19  |  3.4 KB  |  165 lines

  1. /* Copyright (c) 1986 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)psum.c 2.2 12/19/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  psum.c - program to sum pictures.
  9.  *
  10.  *     10/14/86
  11.  */
  12.  
  13. #include  <stdio.h>
  14.  
  15. #include  "color.h"
  16.  
  17.  
  18. #define  MAXFILE    8
  19.  
  20. int  xsiz, ysiz;
  21.  
  22. char  *progname;
  23.  
  24. char  *fname[MAXFILE];            /* the file names */
  25. FILE  *fptr[MAXFILE];            /* the file pointers */
  26. COLOR  scale[MAXFILE];            /* scaling factors */
  27. int  nfile;                /* number of files */
  28.  
  29.  
  30. tabputs(s)            /* print line preceded by a tab */
  31. char  *s;
  32. {
  33.     putc('\t', stdout);
  34.     fputs(s, stdout);
  35. }
  36.  
  37.  
  38. main(argc, argv)
  39. int  argc;
  40. char  *argv[];
  41. {
  42.     double  d;
  43.     int  xres, yres;
  44.     int  an;
  45.  
  46.     progname = argv[0];
  47.  
  48.     nfile = 0;
  49.     setcolor(scale[0], 1.0, 1.0, 1.0);
  50.  
  51.     for (an = 1; an < argc; an++) {
  52.         if (nfile >= MAXFILE) {
  53.             fprintf(stderr, "%s: too many files\n", progname);
  54.             quit(1);
  55.         }
  56.         if (argv[an][0] == '-')
  57.             switch (argv[an][1]) {
  58.             case 's':
  59.                 d = atof(argv[an+1]);
  60.                 switch (argv[an][2]) {
  61.                 case '\0':
  62.                     scalecolor(scale[nfile], d);
  63.                     break;
  64.                 case 'r':
  65.                     colval(scale[nfile],RED) *= d;
  66.                     break;
  67.                 case 'g':
  68.                     colval(scale[nfile],GRN) *= d;
  69.                     break;
  70.                 case 'b':
  71.                     colval(scale[nfile],BLU) *= d;
  72.                     break;
  73.                 default:
  74.                     goto unkopt;
  75.                 }
  76.                 an++;
  77.                 continue;
  78.             case '\0':
  79.                 fptr[nfile] = stdin;
  80.                 fname[nfile] = "<stdin>";
  81.                 break;
  82.             default:;
  83.             unkopt:
  84.                 fprintf(stderr, "%s: unknown option: %s\n",
  85.                         progname, argv[an]);
  86.                 quit(1);
  87.             }
  88.         else if ((fptr[nfile] = fopen(argv[an], "r")) == NULL) {
  89.             fprintf(stderr, "%s: can't open file: %s\n",
  90.                         progname, argv[an]);
  91.             quit(1);
  92.         } else
  93.             fname[nfile] = argv[an];
  94.                         /* get header */
  95.         fputs(fname[nfile], stdout);
  96.         fputs(":\n", stdout);
  97.         getheader(fptr[nfile], tabputs);
  98.                         /* get picture size */
  99.         if (fgetresolu(&xres, &yres, fptr[nfile]) != (YMAJOR|YDECR)) {
  100.             fprintf(stderr, "%s: bad picture size\n", progname);
  101.             quit(1);
  102.         } else if (nfile == 0) {
  103.             xsiz = xres;
  104.             ysiz = yres;
  105.         } else if (xres != xsiz || yres != ysiz) {
  106.             fprintf(stderr, "%s: pictures are different sizes\n",
  107.                     progname);
  108.             quit(1);
  109.         }
  110.         nfile++;
  111.         setcolor(scale[nfile], 1.0, 1.0, 1.0);
  112.     }
  113.                     /* add new header info. */
  114.     printargs(argc, argv, stdout);
  115.     putchar('\n');
  116.     fputresolu(YMAJOR|YDECR, xsiz, ysiz, stdout);
  117.  
  118.     psum();
  119.     
  120.     quit(0);
  121. }
  122.  
  123.  
  124. psum()                /* sum the files */
  125. {
  126.     COLOR  *scanin, *scanout;
  127.     int  y, i;
  128.     register int  x;
  129.  
  130.     scanin = (COLOR *)malloc(xsiz*sizeof(COLOR));
  131.     scanout = (COLOR *)malloc(xsiz*sizeof(COLOR));
  132.     if (scanin == NULL || scanout == NULL) {
  133.         fprintf(stderr, "%s: out of memory\n", progname);
  134.         quit(1);
  135.     }
  136.     for (y = ysiz-1; y >= 0; y--) {
  137.         for (x = 0; x < xsiz; x++)
  138.             setcolor(scanout[x], 0.0, 0.0, 0.0);
  139.         for (i = 0; i < nfile; i++) {
  140.             if (freadscan(scanin, xsiz, fptr[i]) < 0) {
  141.                 fprintf(stderr, "%s: read error on file: %s\n",
  142.                         progname, fname[i]);
  143.                 quit(1);
  144.             }
  145.             for (x = 0; x < xsiz; x++)
  146.                 multcolor(scanin[x], scale[i]);
  147.             for (x = 0; x < xsiz; x++)
  148.                 addcolor(scanout[x], scanin[x]);
  149.         }
  150.         if (fwritescan(scanout, xsiz, stdout) < 0) {
  151.             fprintf(stderr, "%s: write error\n", progname);
  152.             quit(1);
  153.         }
  154.     }
  155.     free((char *)scanin);
  156.     free((char *)scanout);
  157. }
  158.  
  159.  
  160. quit(code)        /* exit gracefully */
  161. int  code;
  162. {
  163.     exit(code);
  164. }
  165.