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

  1. /* Copyright (c) 1992 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)pextrem.c 2.1 8/21/92 LBL";
  5. #endif
  6.  
  7. /*
  8.  * Find extrema points in a Radiance picture.
  9.  */
  10.  
  11. #include  <stdio.h>
  12. #ifdef MSDOS
  13. #include  <fcntl.h>
  14. #endif
  15. #include  "color.h"
  16.  
  17.  
  18. int  orig = 0;
  19.  
  20. int  wrongformat = 0;
  21.  
  22. COLOR  expos = WHTCOLOR;
  23.  
  24. extern char  *malloc();
  25.  
  26.  
  27. headline(s)                     /* check header line */
  28. char  *s;
  29. {
  30.     char    fmt[32];
  31.     double  d;
  32.     COLOR   ctmp;
  33.  
  34.     if (isformat(s)) {                      /* format */
  35.         formatval(fmt, s);
  36.         wrongformat = strcmp(fmt, COLRFMT);
  37.     }
  38.     if (!orig)
  39.         return;
  40.     if (isexpos(s)) {                       /* exposure */
  41.         d = exposval(s);
  42.         scalecolor(expos, d);
  43.     } else if (iscolcor(s)) {               /* color correction */
  44.         colcorval(ctmp, s);
  45.         multcolor(expos, ctmp);
  46.     }
  47. }
  48.  
  49.  
  50. main(argc, argv)
  51. int  argc;
  52. char  *argv[];
  53. {
  54.     int  i;
  55.     int  xres, yres;
  56.     int  y;
  57.     register int  x;
  58.     COLR  *scan;
  59.     COLR  cmin, cmax;
  60.     int  xmin, ymin, xmax, ymax;
  61. #ifdef MSDOS
  62.     extern int  _fmode;
  63.     _fmode = O_BINARY;
  64.     setmode(fileno(stdin), O_BINARY);
  65. #endif
  66.     for (i = 1; i < argc; i++)      /* get options */
  67.         if (!strcmp(argv[i], "-o"))
  68.             orig++;
  69.         else
  70.             break;
  71.  
  72.     if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
  73.         fprintf(stderr, "%s: can't open input \"%s\"\n",
  74.                 argv[0], argv[i]);
  75.         exit(1);
  76.     }
  77.                     /* get our header */
  78.     if (getheader(stdin, headline, NULL) < 0 || wrongformat ||
  79.             fgetresolu(&xres, &yres, stdin) < 0) {
  80.         fprintf(stderr, "%s: bad picture format\n", argv[0]);
  81.         exit(1);
  82.     }
  83.     if ((scan = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
  84.         fprintf(stderr, "%s: out of memory\n", argv[0]);
  85.         exit(1);
  86.     }
  87.     setcolr(cmin, 1e10, 1e10, 1e10);
  88.     setcolr(cmax, 0., 0., 0.);
  89.                     /* find extrema */
  90.     for (y = yres-1; y >= 0; y--) {
  91.         if (freadcolrs(scan, xres, stdin) < 0) {
  92.             fprintf(stderr, "%s: read error on input\n", argv[0]);
  93.             exit(1);
  94.         }
  95.         for (x = xres; x-- > 0; ) {
  96.             if (scan[x][EXP] > cmax[EXP] ||
  97.                     (scan[x][EXP] == cmax[EXP] &&
  98.                      normbright(scan[x]) >
  99.                         normbright(cmax))) {
  100.                 copycolr(cmax, scan[x]);
  101.                 xmax = x; ymax = y;
  102.             }
  103.             if (scan[x][EXP] < cmin[EXP] ||
  104.                     (scan[x][EXP] == cmin[EXP] &&
  105.                      normbright(scan[x]) <
  106.                         normbright(cmin))) {
  107.                 copycolr(cmin, scan[x]);
  108.                 xmin = x; ymin = y;
  109.             }
  110.         }
  111.     }
  112.     free((char *)scan);
  113.     printf("%d %d\t%e %e %e\n", xmin, ymin,
  114.             colrval(cmin,RED)/colval(expos,RED),
  115.             colrval(cmin,GRN)/colval(expos,GRN),
  116.             colrval(cmin,BLU)/colval(expos,BLU));
  117.     printf("%d %d\t%e %e %e\n", xmax, ymax,
  118.             colrval(cmax,RED)/colval(expos,RED),
  119.             colrval(cmax,GRN)/colval(expos,GRN),
  120.             colrval(cmax,BLU)/colval(expos,BLU));
  121.     exit(0);
  122. }
  123.