Back to index

radiance  4R0+20100331
pvalue.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: pvalue.c,v 2.29 2006/12/23 17:27:46 greg Exp $";
00003 #endif
00004 /*
00005  *  pvalue.c - program to print pixel values.
00006  *
00007  *     4/23/86
00008  */
00009 
00010 #include  "platform.h"
00011 #include  "standard.h"
00012 #include  "color.h"
00013 #include  "resolu.h"
00014 #include  "view.h"
00015 
00016 #define        min(a,b)            ((a)<(b)?(a):(b))
00017 
00018                             /* what to put out (also RED, GRN, BLU) */
00019 #define  ALL         3
00020 #define  BRIGHT             4
00021 
00022 RESOLU picres;                     /* resolution of picture */
00023 int  uniq = 0;                     /* print only unique values? */
00024 int  doexposure = 0;        /* exposure change? (>100 to print) */
00025 int  dataonly = 0;          /* data only format? */
00026 int  putprim = ALL;         /* what to put out */
00027 int  reverse = 0;           /* reverse conversion? */
00028 int  format = 'a';          /* input/output format */
00029 char  *fmtid = "ascii";            /* format identifier for header */
00030 int  header = 1;            /* do header? */
00031 long  skipbytes = 0;        /* skip bytes in input? */
00032 int  swapbytes = 0;         /* swap bytes? */
00033 int  interleave = 1;        /* file is interleaved? */
00034 int  resolution = 1;        /* put/get resolution string? */
00035 int  original = 0;          /* convert to original values? */
00036 int  wrongformat = 0;              /* wrong input format? */
00037 double gamcor = 1.0;        /* gamma correction */
00038 
00039 int  ord[3] = {RED, GRN, BLU};     /* RGB ordering */
00040 int  rord[4];               /* reverse ordering */
00041 
00042 COLOR  exposure = WHTCOLOR;
00043 
00044 char  *progname;
00045 
00046 FILE  *fin;
00047 FILE  *fin2 = NULL, *fin3 = NULL;  /* for other color channels */
00048 
00049 
00050 typedef int getfunc_t(COLOR  col);
00051 typedef int putfunc_t(COLOR  col);
00052 typedef double brightfunc_t(COLOR  col);
00053 
00054 getfunc_t *getval;
00055 putfunc_t *putval;
00056 brightfunc_t *mybright;
00057 
00058 static gethfunc checkhead;
00059 static brightfunc_t rgb_bright, xyz_bright;
00060 static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword;
00061 static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword;
00062 static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword;
00063 static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword;
00064 static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword;
00065 
00066 static void set_io(void);
00067 static void pixtoval(void);
00068 static void valtopix(void);
00069 
00070 
00071 static double
00072 rgb_bright(
00073        COLOR  clr
00074 )
00075 {
00076        return(bright(clr));
00077 }
00078 
00079 static double
00080 xyz_bright(
00081        COLOR  clr
00082 )
00083 {
00084        return(clr[CIEY]);
00085 }
00086 
00087 int
00088 main(
00089        int  argc,
00090        char  **argv
00091 )
00092 {
00093        double  d, expval = 1.0;
00094        int  i;
00095 
00096        progname = argv[0];
00097        mybright = &rgb_bright; /* default */
00098 
00099        for (i = 1; i < argc; i++)
00100               if (argv[i][0] == '-' || argv[i][0] == '+')
00101                      switch (argv[i][1]) {
00102                      case 'h':            /* header */
00103                             header = argv[i][0] == '+';
00104                             break;
00105                      case 'H':            /* resolution string */
00106                             resolution = argv[i][0] == '+';
00107                             break;
00108                      case 's':            /* skip bytes in header */
00109                             skipbytes = atol(argv[++i]);
00110                             break;
00111                      case 'u':            /* unique values */
00112                             uniq = argv[i][0] == '-';
00113                             break;
00114                      case 'o':            /* original values */
00115                             original = argv[i][0] == '-';
00116                             break;
00117                      case 'g':            /* gamma correction */
00118                             gamcor = atof(argv[i+1]);
00119                             if (argv[i][0] == '+')
00120                                    gamcor = 1.0/gamcor;
00121                             i++;
00122                             break;
00123                      case 'e':            /* exposure correction */
00124                             d = atof(argv[i+1]);
00125                             if (argv[i+1][0] == '-' || argv[i+1][0] == '+')
00126                                    d = pow(2.0, d);
00127                             if (argv[i][0] == '-')
00128                                    expval *= d;
00129                             scalecolor(exposure, d);
00130                             doexposure++;
00131                             i++;
00132                             break;
00133                      case 'R':            /* reverse byte sequence */
00134                             if (argv[i][0] == '-') {
00135                                    ord[0]=BLU; ord[1]=GRN; ord[2]=RED;
00136                             } else {
00137                                    ord[0]=RED; ord[1]=GRN; ord[2]=BLU;
00138                             }
00139                             break;
00140                      case 'r':            /* reverse conversion */
00141                             reverse = argv[i][0] == '-';
00142                             break;
00143                      case 'n':            /* non-interleaved RGB */
00144                             interleave = argv[i][0] == '+';
00145                             break;
00146                      case 'b':            /* brightness values */
00147                             putprim = argv[i][0] == '-' ? BRIGHT : ALL;
00148                             break;
00149                      case 'p':            /* put primary */
00150                             switch (argv[i][2]) {
00151                             case 'r': case 'R': putprim = RED; break;
00152                             case 'g': case 'G': putprim = GRN; break;
00153                             case 'b': case 'B': putprim = BLU; break;
00154                             default: goto unkopt;
00155                             }
00156                             break;
00157                      case 'd':            /* data only (no indices) */
00158                             dataonly = argv[i][0] == '-';
00159                             switch (argv[i][2]) {
00160                             case '\0':
00161                             case 'a':            /* ascii */
00162                                    format = 'a';
00163                                    fmtid = "ascii";
00164                                    break;
00165                             case 'i':            /* integer */
00166                                    format = 'i';
00167                                    fmtid = "ascii";
00168                                    break;
00169                             case 'b':            /* byte */
00170                                    dataonly = 1;
00171                                    format = 'b';
00172                                    fmtid = "byte";
00173                                    break;
00174                             case 'W':            /* 16-bit swapped */
00175                                    swapbytes = 1;
00176                             case 'w':            /* 16-bit */
00177                                    dataonly = 1;
00178                                    format = 'w';
00179                                    fmtid = "16-bit";
00180                                    break;
00181                             case 'F':            /* swapped floats */
00182                                    swapbytes = 1;
00183                             case 'f':            /* float */
00184                                    dataonly = 1;
00185                                    format = 'f';
00186                                    fmtid = "float";
00187                                    break;
00188                             case 'D':            /* swapped doubles */
00189                                    swapbytes = 1;
00190                             case 'd':            /* double */
00191                                    dataonly = 1;
00192                                    format = 'd';
00193                                    fmtid = "double";
00194                                    break;
00195                             default:
00196                                    goto unkopt;
00197                             }
00198                             break;
00199                      case 'x':            /* x resolution */
00200                      case 'X':            /* x resolution */
00201                             resolution = 0;
00202                             if (argv[i][0] == '-')
00203                                    picres.rt |= XDECR;
00204                             picres.xr = atoi(argv[++i]);
00205                             break;
00206                      case 'y':            /* y resolution */
00207                      case 'Y':            /* y resolution */
00208                             resolution = 0;
00209                             if (argv[i][0] == '-')
00210                                    picres.rt |= YDECR;
00211                             if (picres.xr == 0)
00212                                    picres.rt |= YMAJOR;
00213                             picres.yr = atoi(argv[++i]);
00214                             break;
00215                      default:
00216 unkopt:
00217                             fprintf(stderr, "%s: unknown option: %s\n",
00218                                           progname, argv[i]);
00219                             quit(1);
00220                             break;
00221                      }
00222               else
00223                      break;
00224                                    /* recognize special formats */
00225        if (dataonly && format == 'b') {
00226               if (putprim == ALL)
00227                      fmtid = "24-bit_rgb";
00228               else
00229                      fmtid = "8-bit_grey";
00230        }
00231        if (dataonly && format == 'w') {
00232               if (putprim == ALL)
00233                      fmtid = "48-bit_rgb";
00234               else
00235                      fmtid = "16-bit_grey";
00236        }
00237                                    /* assign reverse ordering */
00238        rord[ord[0]] = 0;
00239        rord[ord[1]] = 1;
00240        rord[ord[2]] = 2;
00241                                    /* get input */
00242        if (i == argc) {
00243               fin = stdin;
00244        } else if (i < argc) {
00245               if ((fin = fopen(argv[i], "r")) == NULL) {
00246                      fprintf(stderr, "%s: can't open file \"%s\"\n",
00247                                           progname, argv[i]);
00248                      quit(1);
00249               }
00250               if (reverse && putprim != BRIGHT && i == argc-3) {
00251                      if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
00252                             fprintf(stderr, "%s: can't open file \"%s\"\n",
00253                                           progname, argv[i+1]);
00254                             quit(1);
00255                      }
00256                      if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
00257                             fprintf(stderr, "%s: can't open file \"%s\"\n",
00258                                           progname, argv[i+2]);
00259                             quit(1);
00260                      }
00261                      interleave = -1;
00262               } else if (i != argc-1)
00263                      fin = NULL;
00264               if (reverse && putprim != BRIGHT && !interleave) {
00265                      fin2 = fopen(argv[i], "r");
00266                      fin3 = fopen(argv[i], "r");
00267               }
00268               if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
00269                             (fseek(fin2, skipbytes, 0) ||
00270                             fseek(fin3, skipbytes, 0))))) {
00271                      fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
00272                                    progname, skipbytes);
00273                      quit(1);
00274               }
00275        }
00276        if (fin == NULL) {
00277               fprintf(stderr, "%s: bad # file arguments\n", progname);
00278               quit(1);
00279        }
00280 
00281        if (reverse) {
00282 #ifdef _WIN32
00283               SET_FILE_BINARY(stdout);
00284               if (format != 'a' && format != 'i')
00285                      SET_FILE_BINARY(fin);
00286 #endif
00287                                    /* get header */
00288               if (header) {
00289                      if (checkheader(fin, fmtid, stdout) < 0) {
00290                             fprintf(stderr, "%s: wrong input format\n",
00291                                           progname);
00292                             quit(1);
00293                      }
00294                      if (fin2 != NULL) {
00295                             getheader(fin2, NULL, NULL);
00296                             getheader(fin3, NULL, NULL);
00297                      }
00298               } else
00299                      newheader("RADIANCE", stdout);
00300                                    /* get resolution */
00301               if ((resolution && !fgetsresolu(&picres, fin)) ||
00302                             picres.xr <= 0 || picres.yr <= 0) {
00303                      fprintf(stderr, "%s: missing resolution\n", progname);
00304                      quit(1);
00305               }
00306               if (resolution && fin2 != NULL) {
00307                      RESOLU  pres2;
00308                      if (!fgetsresolu(&pres2, fin2) ||
00309                                    pres2.rt != picres.rt ||
00310                                    pres2.xr != picres.xr ||
00311                                    pres2.yr != picres.yr ||
00312                                    !fgetsresolu(&pres2, fin3) ||
00313                                    pres2.rt != picres.rt ||
00314                                    pres2.xr != picres.xr ||
00315                                    pres2.yr != picres.yr) {
00316                             fprintf(stderr, "%s: resolution mismatch\n",
00317                                           progname);
00318                             quit(1);
00319                      }
00320               }
00321                                           /* add to header */
00322               printargs(i, argv, stdout);
00323               if (expval < .99 || expval > 1.01)
00324                      fputexpos(expval, stdout);
00325               fputformat(COLRFMT, stdout);
00326               putchar('\n');
00327               fputsresolu(&picres, stdout);      /* always put resolution */
00328               valtopix();
00329        } else {
00330 #ifdef _WIN32
00331               SET_FILE_BINARY(fin);
00332               if (format != 'a' && format != 'i')
00333                      SET_FILE_BINARY(stdout);
00334 #endif
00335                                           /* get header */
00336               getheader(fin, checkhead, NULL);
00337               if (wrongformat) {
00338                      fprintf(stderr,
00339                             "%s: input not a Radiance RGBE picture\n",
00340                                    progname);
00341                      quit(1);
00342               }
00343               if (!fgetsresolu(&picres, fin)) {
00344                      fprintf(stderr, "%s: missing resolution\n", progname);
00345                      quit(1);
00346               }
00347               if (header) {
00348                      printargs(i, argv, stdout);
00349                      if (expval < .99 || expval > 1.01)
00350                             fputexpos(expval, stdout);
00351                      fputformat(fmtid, stdout);
00352                      putchar('\n');
00353               }
00354               if (resolution)                    /* put resolution */
00355                      fputsresolu(&picres, stdout);
00356               pixtoval();
00357        }
00358 
00359        quit(0);
00360        return 0; /* pro forma return */
00361 }
00362 
00363 
00364 static int
00365 checkhead(                         /* deal with line from header */
00366        char   *line,
00367        void   *p
00368 )
00369 {
00370        char   fmt[32];
00371        double d;
00372        COLOR  ctmp;
00373 
00374        if (formatval(fmt, line)) {
00375               if (!strcmp(fmt, CIEFMT))
00376                      mybright = &xyz_bright;
00377               else if (!strcmp(fmt, COLRFMT))
00378                      mybright = &rgb_bright;
00379               else
00380                      wrongformat++;
00381        } else if (original && isexpos(line)) {
00382               d = 1.0/exposval(line);
00383               scalecolor(exposure, d);
00384               doexposure++;
00385        } else if (original && iscolcor(line)) {
00386               colcorval(ctmp, line);
00387               setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
00388                             colval(exposure,GRN)/colval(ctmp,GRN),
00389                             colval(exposure,BLU)/colval(ctmp,BLU));
00390               doexposure++;
00391        } else if (header)
00392               fputs(line, stdout);
00393        return(0);
00394 }
00395 
00396 
00397 static void
00398 pixtoval(void)                            /* convert picture to values */
00399 {
00400        register COLOR       *scanln;
00401        int  dogamma;
00402        COLOR  lastc;
00403        RREAL  hv[2];
00404        int  startprim, endprim;
00405        long  startpos;
00406        int  y;
00407        register int  x;
00408 
00409        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
00410        if (scanln == NULL) {
00411               fprintf(stderr, "%s: out of memory\n", progname);
00412               quit(1);
00413        }
00414        dogamma = gamcor < .95 || gamcor > 1.05;
00415        if (putprim == ALL && !interleave) {
00416               startprim = RED; endprim = BLU;
00417               startpos = ftell(fin);
00418        } else {
00419               startprim = putprim; endprim = putprim;
00420        }
00421        for (putprim = startprim; putprim <= endprim; putprim++) {
00422               if (putprim != startprim && fseek(fin, startpos, 0)) {
00423                      fprintf(stderr, "%s: seek error on input file\n",
00424                                    progname);
00425                      quit(1);
00426               }
00427               set_io();
00428               setcolor(lastc, 0.0, 0.0, 0.0);
00429               for (y = 0; y < numscans(&picres); y++) {
00430                      if (freadscan(scanln, scanlen(&picres), fin) < 0) {
00431                             fprintf(stderr, "%s: read error\n", progname);
00432                             quit(1);
00433                      }
00434                      for (x = 0; x < scanlen(&picres); x++) {
00435                             if (uniq) {
00436                                    if (   colval(scanln[x],RED) ==
00437                                                  colval(lastc,RED) &&
00438                                           colval(scanln[x],GRN) ==
00439                                                  colval(lastc,GRN) &&
00440                                           colval(scanln[x],BLU) ==
00441                                                  colval(lastc,BLU)    )
00442                                           continue;
00443                                    else
00444                                           copycolor(lastc, scanln[x]);
00445                             }
00446                             if (doexposure)
00447                                    multcolor(scanln[x], exposure);
00448                             if (dogamma)
00449                                    setcolor(scanln[x],
00450                                    pow(colval(scanln[x],RED), 1.0/gamcor),
00451                                    pow(colval(scanln[x],GRN), 1.0/gamcor),
00452                                    pow(colval(scanln[x],BLU), 1.0/gamcor));
00453                             if (!dataonly) {
00454                                    pix2loc(hv, &picres, x, y);
00455                                    printf("%7d %7d ",
00456                                                  (int)(hv[0]*picres.xr),
00457                                                  (int)(hv[1]*picres.yr));
00458                             }
00459                             if ((*putval)(scanln[x]) < 0) {
00460                                    fprintf(stderr, "%s: write error\n",
00461                                                  progname);
00462                                    quit(1);
00463                             }
00464                      }
00465               }
00466        }
00467        free((void *)scanln);
00468 }
00469 
00470 
00471 static void
00472 valtopix(void)                     /* convert values to a pixel file */
00473 {
00474        int  dogamma;
00475        register COLOR       *scanln;
00476        int  y;
00477        register int  x;
00478 
00479        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
00480        if (scanln == NULL) {
00481               fprintf(stderr, "%s: out of memory\n", progname);
00482               quit(1);
00483        }
00484        dogamma = gamcor < .95 || gamcor > 1.05;
00485        set_io();
00486        for (y = 0; y < numscans(&picres); y++) {
00487               for (x = 0; x < scanlen(&picres); x++) {
00488                      if (!dataonly) {
00489                             fscanf(fin, "%*d %*d");
00490                             if (fin2 != NULL) {
00491                                    fscanf(fin2, "%*d %*d");
00492                                    fscanf(fin3, "%*d %*d");
00493                             }
00494                      }
00495                      if ((*getval)(scanln[x]) < 0) {
00496                             fprintf(stderr, "%s: read error\n", progname);
00497                             quit(1);
00498                      }
00499                      if (dogamma)
00500                             setcolor(scanln[x],
00501                                    pow(colval(scanln[x],RED), gamcor),
00502                                    pow(colval(scanln[x],GRN), gamcor),
00503                                    pow(colval(scanln[x],BLU), gamcor));
00504                      if (doexposure)
00505                             multcolor(scanln[x], exposure);
00506               }
00507               if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
00508                      fprintf(stderr, "%s: write error\n", progname);
00509                      quit(1);
00510               }
00511        }
00512        free((void *)scanln);
00513 }
00514 
00515 
00516 void
00517 quit(code)
00518 int  code;
00519 {
00520        exit(code);
00521 }
00522 
00523 
00524 static int
00525 getcascii(           /* get an ascii color value from stream(s) */
00526        COLOR  col
00527 )
00528 {
00529        double vd[3];
00530 
00531        if (fin2 == NULL) {
00532               if (fscanf(fin, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
00533                      return(-1);
00534        } else {
00535               if (fscanf(fin, "%lf", &vd[0]) != 1 ||
00536                             fscanf(fin2, "%lf", &vd[1]) != 1 ||
00537                             fscanf(fin3, "%lf", &vd[2]) != 1)
00538                      return(-1);
00539        }
00540        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
00541        return(0);
00542 }
00543 
00544 
00545 static int
00546 getcdouble(          /* get a double color value from stream(s) */
00547        COLOR  col
00548 )
00549 {
00550        double vd[3];
00551 
00552        if (fin2 == NULL) {
00553               if (fread((char *)vd, sizeof(double), 3, fin) != 3)
00554                      return(-1);
00555        } else {
00556               if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
00557                      fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
00558                      fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
00559                      return(-1);
00560        }
00561        if (swapbytes)
00562               swap64((char *)vd, 3);
00563        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
00564        return(0);
00565 }
00566 
00567 
00568 static int
00569 getcfloat(           /* get a float color value from stream(s) */
00570        COLOR  col
00571 )
00572 {
00573        float  vf[3];
00574 
00575        if (fin2 == NULL) {
00576               if (fread((char *)vf, sizeof(float), 3, fin) != 3)
00577                      return(-1);
00578        } else {
00579               if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
00580                      fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
00581                      fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
00582                      return(-1);
00583        }
00584        if (swapbytes)
00585               swap32((char *)vf, 3);
00586        setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
00587        return(0);
00588 }
00589 
00590 
00591 static int
00592 getcint(             /* get an int color value from stream(s) */
00593        COLOR  col
00594 )
00595 {
00596        int  vi[3];
00597 
00598        if (fin2 == NULL) {
00599               if (fscanf(fin, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
00600                      return(-1);
00601        } else {
00602               if (fscanf(fin, "%d", &vi[0]) != 1 ||
00603                             fscanf(fin2, "%d", &vi[1]) != 1 ||
00604                             fscanf(fin3, "%d", &vi[2]) != 1)
00605                      return(-1);
00606        }
00607        setcolor(col, (vi[rord[RED]]+.5)/256.,
00608                      (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
00609        return(0);
00610 }
00611 
00612 
00613 static int
00614 getcbyte(            /* get a byte color value from stream(s) */
00615        COLOR  col
00616 )
00617 {
00618        BYTE  vb[3];
00619 
00620        if (fin2 == NULL) {
00621               if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
00622                      return(-1);
00623        } else {
00624               if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
00625                      fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
00626                      fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
00627                      return(-1);
00628        }
00629        setcolor(col, (vb[rord[RED]]+.5)/256.,
00630                      (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
00631        return(0);
00632 }
00633 
00634 
00635 static int
00636 getcword(            /* get a 16-bit color value from stream(s) */
00637        COLOR  col
00638 )
00639 {
00640        uint16  vw[3];
00641 
00642        if (fin2 == NULL) {
00643               if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
00644                      return(-1);
00645        } else {
00646               if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
00647                      fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
00648                      fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
00649                      return(-1);
00650        }
00651        if (swapbytes)
00652               swap16((char *)vw, 3);
00653        setcolor(col, (vw[rord[RED]]+.5)/65536.,
00654                      (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
00655        return(0);
00656 }
00657 
00658 
00659 static int
00660 getbascii(           /* get an ascii brightness value from fin */
00661        COLOR  col
00662 )
00663 {
00664        double vd;
00665 
00666        if (fscanf(fin, "%lf", &vd) != 1)
00667               return(-1);
00668        setcolor(col, vd, vd, vd);
00669        return(0);
00670 }
00671 
00672 
00673 static int
00674 getbdouble(          /* get a double brightness value from fin */
00675        COLOR  col
00676 )
00677 {
00678        double vd;
00679 
00680        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
00681               return(-1);
00682        if (swapbytes)
00683               swap64((char *)&vd, 1);
00684        setcolor(col, vd, vd, vd);
00685        return(0);
00686 }
00687 
00688 
00689 static int
00690 getbfloat(           /* get a float brightness value from fin */
00691        COLOR  col
00692 )
00693 {
00694        float  vf;
00695 
00696        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
00697               return(-1);
00698        if (swapbytes)
00699               swap32((char *)&vf, 1);
00700        setcolor(col, vf, vf, vf);
00701        return(0);
00702 }
00703 
00704 
00705 static int
00706 getbint(             /* get an int brightness value from fin */
00707        COLOR  col
00708 )
00709 {
00710        int  vi;
00711        double d;
00712 
00713        if (fscanf(fin, "%d", &vi) != 1)
00714               return(-1);
00715        d = (vi+.5)/256.;
00716        setcolor(col, d, d, d);
00717        return(0);
00718 }
00719 
00720 
00721 static int
00722 getbbyte(            /* get a byte brightness value from fin */
00723        COLOR  col
00724 )
00725 {
00726        BYTE  vb;
00727        double d;
00728 
00729        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
00730               return(-1);
00731        d = (vb+.5)/256.;
00732        setcolor(col, d, d, d);
00733        return(0);
00734 }
00735 
00736 
00737 static int
00738 getbword(            /* get a 16-bit brightness value from fin */
00739        COLOR  col
00740 )
00741 {
00742        uint16  vw;
00743        double d;
00744 
00745        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
00746               return(-1);
00747        if (swapbytes)
00748               swap16((char *)&vw, 1);
00749        d = (vw+.5)/65536.;
00750        setcolor(col, d, d, d);
00751        return(0);
00752 }
00753 
00754 
00755 static int
00756 putcascii(                  /* put an ascii color to stdout */
00757        COLOR  col
00758 )
00759 {
00760        fprintf(stdout, "%15.3e %15.3e %15.3e\n",
00761                      colval(col,ord[0]),
00762                      colval(col,ord[1]),
00763                      colval(col,ord[2]));
00764 
00765        return(ferror(stdout) ? -1 : 0);
00766 }
00767 
00768 
00769 static int
00770 putcfloat(                  /* put a float color to stdout */
00771        COLOR  col
00772 )
00773 {
00774        float  vf[3];
00775 
00776        vf[0] = colval(col,ord[0]);
00777        vf[1] = colval(col,ord[1]);
00778        vf[2] = colval(col,ord[2]);
00779        if (swapbytes)
00780               swap32((char *)vf, 3);
00781        fwrite((char *)vf, sizeof(float), 3, stdout);
00782 
00783        return(ferror(stdout) ? -1 : 0);
00784 }
00785 
00786 
00787 static int
00788 putcdouble(                 /* put a double color to stdout */
00789        COLOR  col
00790 )
00791 {
00792        double vd[3];
00793 
00794        vd[0] = colval(col,ord[0]);
00795        vd[1] = colval(col,ord[1]);
00796        vd[2] = colval(col,ord[2]);
00797        if (swapbytes)
00798               swap64((char *)vd, 3);
00799        fwrite((char *)vd, sizeof(double), 3, stdout);
00800 
00801        return(ferror(stdout) ? -1 : 0);
00802 }
00803 
00804 
00805 static int
00806 putcint(                    /* put an int color to stdout */
00807        COLOR  col
00808 )
00809 {
00810        fprintf(stdout, "%d %d %d\n",
00811                      (int)(colval(col,ord[0])*256.),
00812                      (int)(colval(col,ord[1])*256.),
00813                      (int)(colval(col,ord[2])*256.));
00814 
00815        return(ferror(stdout) ? -1 : 0);
00816 }
00817 
00818 
00819 static int
00820 putcbyte(                   /* put a byte color to stdout */
00821        COLOR  col
00822 )
00823 {
00824        long  i;
00825        BYTE  vb[3];
00826 
00827        i = colval(col,ord[0])*256.;
00828        vb[0] = min(i,255);
00829        i = colval(col,ord[1])*256.;
00830        vb[1] = min(i,255);
00831        i = colval(col,ord[2])*256.;
00832        vb[2] = min(i,255);
00833        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
00834 
00835        return(ferror(stdout) ? -1 : 0);
00836 }
00837 
00838 
00839 static int
00840 putcword(                   /* put a 16-bit color to stdout */
00841        COLOR  col
00842 )
00843 {
00844        long  i;
00845        uint16  vw[3];
00846 
00847        i = colval(col,ord[0])*65536.;
00848        vw[0] = min(i,65535);
00849        i = colval(col,ord[1])*65536.;
00850        vw[1] = min(i,65535);
00851        i = colval(col,ord[2])*65536.;
00852        vw[2] = min(i,65535);
00853        if (swapbytes)
00854               swap16((char *)vw, 3);
00855        fwrite((char *)vw, sizeof(uint16), 3, stdout);
00856 
00857        return(ferror(stdout) ? -1 : 0);
00858 }
00859 
00860 
00861 static int
00862 putbascii(                  /* put an ascii brightness to stdout */
00863        COLOR  col
00864 )
00865 {
00866        fprintf(stdout, "%15.3e\n", (*mybright)(col));
00867 
00868        return(ferror(stdout) ? -1 : 0);
00869 }
00870 
00871 
00872 static int
00873 putbfloat(                  /* put a float brightness to stdout */
00874        COLOR  col
00875 )
00876 {
00877        float  vf;
00878 
00879        vf = (*mybright)(col);
00880        if (swapbytes)
00881               swap32((char *)&vf, 1);
00882        fwrite((char *)&vf, sizeof(float), 1, stdout);
00883 
00884        return(ferror(stdout) ? -1 : 0);
00885 }
00886 
00887 
00888 static int
00889 putbdouble(                 /* put a double brightness to stdout */
00890        COLOR  col
00891 )
00892 {
00893        double vd;
00894 
00895        vd = (*mybright)(col);
00896        if (swapbytes)
00897               swap64((char *)&vd, 1);
00898        fwrite((char *)&vd, sizeof(double), 1, stdout);
00899 
00900        return(ferror(stdout) ? -1 : 0);
00901 }
00902 
00903 
00904 static int
00905 putbint(                    /* put an int brightness to stdout */
00906        COLOR  col
00907 )
00908 {
00909        fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
00910 
00911        return(ferror(stdout) ? -1 : 0);
00912 }
00913 
00914 
00915 static int
00916 putbbyte(                   /* put a byte brightness to stdout */
00917        COLOR  col
00918 )
00919 {
00920        register int  i;
00921        BYTE  vb;
00922 
00923        i = (*mybright)(col)*256.;
00924        vb = min(i,255);
00925        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
00926 
00927        return(ferror(stdout) ? -1 : 0);
00928 }
00929 
00930 
00931 static int
00932 putbword(                   /* put a 16-bit brightness to stdout */
00933        COLOR  col
00934 )
00935 {
00936        long  i;
00937        uint16  vw;
00938 
00939        i = (*mybright)(col)*65536.;
00940        vw = min(i,65535);
00941        if (swapbytes)
00942               swap16((char *)&vw, 1);
00943        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
00944 
00945        return(ferror(stdout) ? -1 : 0);
00946 }
00947 
00948 
00949 static int
00950 putpascii(                  /* put an ascii primary to stdout */
00951        COLOR  col
00952 )
00953 {
00954        fprintf(stdout, "%15.3e\n", colval(col,putprim));
00955 
00956        return(ferror(stdout) ? -1 : 0);
00957 }
00958 
00959 
00960 static int
00961 putpfloat(                  /* put a float primary to stdout */
00962        COLOR  col
00963 )
00964 {
00965        float  vf;
00966 
00967        vf = colval(col,putprim);
00968        if (swapbytes)
00969               swap32((char *)&vf, 1);
00970        fwrite((char *)&vf, sizeof(float), 1, stdout);
00971 
00972        return(ferror(stdout) ? -1 : 0);
00973 }
00974 
00975 
00976 static int
00977 putpdouble(                 /* put a double primary to stdout */
00978        COLOR  col
00979 )
00980 {
00981        double vd;
00982 
00983        vd = colval(col,putprim);
00984        if (swapbytes)
00985               swap64((char *)&vd, 1);
00986        fwrite((char *)&vd, sizeof(double), 1, stdout);
00987 
00988        return(ferror(stdout) ? -1 : 0);
00989 }
00990 
00991 
00992 static int
00993 putpint(                    /* put an int primary to stdout */
00994        COLOR  col
00995 )
00996 {
00997        fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
00998 
00999        return(ferror(stdout) ? -1 : 0);
01000 }
01001 
01002 
01003 static int
01004 putpbyte(                   /* put a byte primary to stdout */
01005        COLOR  col
01006 )
01007 {
01008        long  i;
01009        BYTE  vb;
01010 
01011        i = colval(col,putprim)*256.;
01012        vb = min(i,255);
01013        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
01014 
01015        return(ferror(stdout) ? -1 : 0);
01016 }
01017 
01018 
01019 static int
01020 putpword(                   /* put a 16-bit primary to stdout */
01021        COLOR  col
01022 )
01023 {
01024        long  i;
01025        uint16  vw;
01026 
01027        i = colval(col,putprim)*65536.;
01028        vw = min(i,65535);
01029        if (swapbytes)
01030               swap16((char *)&vw, 1);
01031        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
01032 
01033        return(ferror(stdout) ? -1 : 0);
01034 }
01035 
01036 
01037 static void
01038 set_io(void)                /* set put and get functions */
01039 {
01040        switch (format) {
01041        case 'a':                                 /* ascii */
01042               if (putprim == BRIGHT) {
01043                      getval = getbascii;
01044                      putval = putbascii;
01045               } else if (putprim != ALL) {
01046                      getval = getbascii;
01047                      putval = putpascii;
01048               } else {
01049                      getval = getcascii;
01050                      putval = putcascii;
01051                      if (reverse && !interleave) {
01052                             fprintf(stderr,
01053                             "%s: ASCII input files must be interleaved\n",
01054                                           progname);
01055                             quit(1);
01056                      }
01057               }
01058               return;
01059        case 'f':                                 /* binary float */
01060               if (putprim == BRIGHT) {
01061                      getval = getbfloat;
01062                      putval = putbfloat;
01063               } else if (putprim != ALL) {
01064                      getval = getbfloat;
01065                      putval = putpfloat;
01066               } else {
01067                      getval = getcfloat;
01068                      putval = putcfloat;
01069                      if (reverse && !interleave) {
01070                             if (fin2 == NULL)
01071                                    goto namerr;
01072                             if (fseek(fin2,
01073                             (long)sizeof(float)*picres.xr*picres.yr, 1))
01074                                    goto seekerr;
01075                             if (fseek(fin3,
01076                             (long)sizeof(float)*2*picres.xr*picres.yr, 1))
01077                                    goto seekerr;
01078                      }
01079               }
01080               return;
01081        case 'd':                                 /* binary double */
01082               if (putprim == BRIGHT) {
01083                      getval = getbdouble;
01084                      putval = putbdouble;
01085               } else if (putprim != ALL) {
01086                      getval = getbdouble;
01087                      putval = putpdouble;
01088               } else {
01089                      getval = getcdouble;
01090                      putval = putcdouble;
01091                      if (reverse && !interleave) {
01092                             if (fin2 == NULL)
01093                                    goto namerr;
01094                             if (fseek(fin2,
01095                             (long)sizeof(double)*picres.xr*picres.yr, 1))
01096                                    goto seekerr;
01097                             if (fseek(fin3,
01098                             (long)sizeof(double)*2*picres.xr*picres.yr, 1))
01099                                    goto seekerr;
01100                      }
01101               }
01102               return;
01103        case 'i':                                 /* integer */
01104               if (putprim == BRIGHT) {
01105                      getval = getbint;
01106                      putval = putbint;
01107               } else if (putprim != ALL) {
01108                      getval = getbint;
01109                      putval = putpint;
01110               } else {
01111                      getval = getcint;
01112                      putval = putcint;
01113                      if (reverse && !interleave) {
01114                             fprintf(stderr,
01115                             "%s: integer input files must be interleaved\n",
01116                                           progname);
01117                             quit(1);
01118                      }
01119               }
01120               return;
01121        case 'b':                                 /* byte */
01122               if (putprim == BRIGHT) {
01123                      getval = getbbyte;
01124                      putval = putbbyte;
01125               } else if (putprim != ALL) {
01126                      getval = getbbyte;
01127                      putval = putpbyte;
01128               } else {
01129                      getval = getcbyte;
01130                      putval = putcbyte;
01131                      if (reverse && !interleave) {
01132                             if (fin2 == NULL)
01133                                    goto namerr;
01134                             if (fseek(fin2,
01135                             (long)sizeof(BYTE)*picres.xr*picres.yr, 1))
01136                                    goto seekerr;
01137                             if (fseek(fin3,
01138                             (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
01139                                    goto seekerr;
01140                      }
01141               }
01142               return;
01143        case 'w':                                 /* 16-bit */
01144               if (putprim == BRIGHT) {
01145                      getval = getbword;
01146                      putval = putbword;
01147               } else if (putprim != ALL) {
01148                      getval = getbword;
01149                      putval = putpword;
01150               } else {
01151                      getval = getcword;
01152                      putval = putcword;
01153                      if (reverse && !interleave) {
01154                             if (fin2 == NULL)
01155                                    goto namerr;
01156                             if (fseek(fin2,
01157                             (long)sizeof(uint16)*picres.xr*picres.yr, 1))
01158                                    goto seekerr;
01159                             if (fseek(fin3,
01160                             (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
01161                                    goto seekerr;
01162                      }
01163               }
01164               return;
01165        }
01166 /* badopt: */ /* label not used */
01167        fprintf(stderr, "%s: botched file type\n", progname);
01168        quit(1);
01169 namerr:
01170        fprintf(stderr, "%s: non-interleaved file(s) must be named\n",
01171                      progname);
01172        quit(1);
01173 seekerr:
01174        fprintf(stderr, "%s: cannot seek on interleaved input file\n",
01175                      progname);
01176        quit(1);
01177 }