Back to index

radiance  4R0+20100331
pextrem.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: pextrem.c,v 2.11 2005/06/02 18:13:47 greg Exp $";
00003 #endif
00004 /*
00005  * Find extrema points in a Radiance picture.
00006  */
00007 
00008 #include  <stdio.h>
00009 #include  <math.h>
00010 #include  <string.h>
00011 
00012 #include  "platform.h"
00013 #include  "color.h"
00014 #include  "resolu.h"
00015 
00016 
00017 int  orig = 0;
00018 
00019 int  wrongformat = 0;
00020 
00021 COLOR  expos = WHTCOLOR;
00022 
00023 static gethfunc headline;
00024 
00025 
00026 static int
00027 headline(                   /* check header line */
00028        char  *s,
00029        void   *p
00030 )
00031 {
00032        char   fmt[32];
00033        double d;
00034        COLOR  ctmp;
00035 
00036        if (isformat(s)) {                 /* format */
00037               formatval(fmt, s);
00038               wrongformat = !globmatch(PICFMT, fmt);
00039        }
00040        if (!orig)
00041               return(0);
00042        if (isexpos(s)) {                  /* exposure */
00043               d = exposval(s);
00044               scalecolor(expos, d);
00045        } else if (iscolcor(s)) {          /* color correction */
00046               colcorval(ctmp, s);
00047               multcolor(expos, ctmp);
00048        }
00049        return(0);
00050 }
00051 
00052 
00053 int
00054 main(
00055        int  argc,
00056        char  *argv[]
00057 )
00058 {
00059        int  i;
00060        int  xres, yres;
00061        int  y;
00062        register int  x;
00063        COLR  *scan;
00064        COLR  cmin, cmax;
00065        int  xmin, ymin, xmax, ymax;
00066        SET_DEFAULT_BINARY();
00067        SET_FILE_BINARY(stdin);
00068        for (i = 1; i < argc; i++)  /* get options */
00069               if (!strcmp(argv[i], "-o"))
00070                      orig++;
00071               else
00072                      break;
00073 
00074        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
00075               fprintf(stderr, "%s: can't open input \"%s\"\n",
00076                             argv[0], argv[i]);
00077               exit(1);
00078        }
00079                                    /* get our header */
00080        if (getheader(stdin, headline, NULL) < 0 || wrongformat ||
00081                      fgetresolu(&xres, &yres, stdin) < 0) {
00082               fprintf(stderr, "%s: bad picture format\n", argv[0]);
00083               exit(1);
00084        }
00085        if ((scan = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
00086               fprintf(stderr, "%s: out of memory\n", argv[0]);
00087               exit(1);
00088        }
00089        setcolr(cmin, 1e30, 1e30, 1e30);
00090        setcolr(cmax, 0., 0., 0.); xmax=ymax=0;
00091                                    /* find extrema */
00092        for (y = yres-1; y >= 0; y--) {
00093               if (freadcolrs(scan, xres, stdin) < 0) {
00094                      fprintf(stderr, "%s: read error on input\n", argv[0]);
00095                      exit(1);
00096               }
00097               for (x = xres; x-- > 0; ) {
00098                      if (scan[x][EXP] > cmax[EXP] ||
00099                                    (scan[x][EXP] == cmax[EXP] &&
00100                                     normbright(scan[x]) >
00101                                           normbright(cmax))) {
00102                             copycolr(cmax, scan[x]);
00103                             xmax = x; ymax = y;
00104                      }
00105                      if (scan[x][EXP] < cmin[EXP] ||
00106                                    (scan[x][EXP] == cmin[EXP] &&
00107                                     normbright(scan[x]) <
00108                                           normbright(cmin))) {
00109                             copycolr(cmin, scan[x]);
00110                             xmin = x; ymin = y;
00111                      }
00112               }
00113        }
00114        free((void *)scan);
00115        printf("%d %d\t%e %e %e\n", xmin, ymin,
00116                      colrval(cmin,RED)/colval(expos,RED),
00117                      colrval(cmin,GRN)/colval(expos,GRN),
00118                      colrval(cmin,BLU)/colval(expos,BLU));
00119        printf("%d %d\t%e %e %e\n", xmax, ymax,
00120                      colrval(cmax,RED)/colval(expos,RED),
00121                      colrval(cmax,GRN)/colval(expos,GRN),
00122                      colrval(cmax,BLU)/colval(expos,BLU));
00123        exit(0);
00124 }