Back to index

radiance  4R0+20100331
ra_rgbe.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: ra_rgbe.c,v 2.17 2004/03/28 20:33:14 schorsch Exp $";
00003 #endif
00004 /*
00005  *  program to convert from RADIANCE RLE to flat format
00006  */
00007 
00008 #include  <stdio.h>
00009 #include  <math.h>
00010 #include  <time.h>
00011 #include  <string.h>
00012 
00013 #include  "platform.h"
00014 #include  "rtprocess.h"
00015 #include  "color.h"
00016 #include  "resolu.h"
00017 
00018 #define dumpheader(fp)      fwrite(headlines, 1, headlen, fp)
00019 
00020 int  bradj = 0;                           /* brightness adjustment */
00021 int  doflat = 1;                   /* produce flat file */
00022 int  force = 0;                           /* force file overwrite? */
00023 int  findframe = 0;                /* find a specific frame? */
00024 int  frameno = 0;                  /* current frame number */
00025 int  fmterr = 0;                   /* got input format error */
00026 char  *headlines;                  /* current header info. */
00027 int  headlen;                      /* current header length */
00028 
00029 char  *progname;
00030 
00031 static gethfunc addhline;
00032 static int transfer(char *ospec);
00033 static int loadheader(FILE *fp);
00034 
00035 
00036 int
00037 main(int  argc, char  *argv[])
00038 {
00039        char   *ospec;
00040        int  i;
00041 
00042        progname = argv[0];
00043 
00044        for (i = 1; i < argc; i++)
00045               if (argv[i][0] == '-')
00046                      switch (argv[i][1]) {
00047                      case 'r':
00048                             doflat = !doflat;
00049                             break;
00050                      case 'e':
00051                             if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
00052                                    goto userr;
00053                             bradj = atoi(argv[++i]);
00054                             break;
00055                      case 'n':
00056                             findframe = atoi(argv[++i]);
00057                             break;
00058                      case 'f':
00059                             force++;
00060                             break;
00061                      case '\0':
00062                             goto gotfile;
00063                      default:
00064                             goto userr;
00065                      }
00066               else
00067                      break;
00068 gotfile:
00069        if (i < argc-2)
00070               goto userr;
00071        if (i <= argc-1 && strcmp(argv[i], "-") &&
00072                      freopen(argv[i], "r", stdin) == NULL) {
00073               fprintf(stderr, "%s: can't open input \"%s\"\n",
00074                             progname, argv[i]);
00075               exit(1);
00076        }
00077        SET_FILE_BINARY(stdin);
00078        ospec = i==argc-2 ? argv[i+1] : (char *)NULL;
00079        while (transfer(ospec))
00080               ;
00081        exit(0);
00082 userr:
00083        fprintf(stderr,
00084        "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n",
00085                      progname);
00086        exit(1);
00087 }
00088 
00089 
00090 static int
00091 transfer(                   /* transfer a Radiance picture */
00092        char   *ospec
00093 )
00094 {
00095        char   oname[PATH_MAX];
00096        FILE   *fp;
00097        int    order;
00098        int    xmax, ymax;
00099        COLR   *scanin;
00100        int    y;
00101                                    /* get header info. */
00102        if (!(y = loadheader(stdin)))
00103               return(0);
00104        if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) {
00105               fprintf(stderr, "%s: bad input format\n", progname);
00106               exit(1);
00107        }
00108                                    /* did we pass the target frame? */
00109        if (findframe && findframe < frameno)
00110               return(0);
00111                                    /* allocate scanline */
00112        scanin = (COLR *)tempbuffer(xmax*sizeof(COLR));
00113        if (scanin == NULL) {
00114               perror(progname);
00115               exit(1);
00116        }
00117                                    /* skip frame? */
00118        if (findframe > frameno) {
00119               for (y = ymax; y--; )
00120                      if (freadcolrs(scanin, xmax, stdin) < 0) {
00121                             fprintf(stderr,
00122                                    "%s: error reading input picture\n",
00123                                           progname);
00124                             exit(1);
00125                      }
00126               return(1);
00127        }
00128                                    /* open output file/command */
00129        if (ospec == NULL) {
00130               strcpy(oname, "<stdout>");
00131               fp = stdout;
00132        } else {
00133               sprintf(oname, ospec, frameno);
00134               if (oname[0] == '!') {
00135                      if ((fp = popen(oname+1, "w")) == NULL) {
00136                             fprintf(stderr, "%s: cannot start \"%s\"\n",
00137                                           progname, oname);
00138                             exit(1);
00139                      }
00140               } else {
00141                      if (!force && access(oname, 0) >= 0) {
00142                             fprintf(stderr,
00143                                    "%s: output file \"%s\" exists\n",
00144                                           progname, oname);
00145                             exit(1);
00146                      }
00147                      if ((fp = fopen(oname, "w")) == NULL) {
00148                             fprintf(stderr, "%s: ", progname);
00149                             perror(oname);
00150                             exit(1);
00151                      }
00152               }
00153        }
00154        SET_FILE_BINARY(fp);
00155        dumpheader(fp);                    /* put out header */
00156        fputs(progname, fp);
00157        if (bradj)
00158               fprintf(fp, " -e %+d", bradj);
00159        if (!doflat)
00160               fputs(" -r", fp);
00161        fputc('\n', fp);
00162        if (bradj)
00163               fputexpos(pow(2.0, (double)bradj), fp);
00164        fputc('\n', fp);
00165        fputresolu(order, xmax, ymax, fp);
00166                                    /* transfer picture */
00167        for (y = ymax; y--; ) {
00168               if (freadcolrs(scanin, xmax, stdin) < 0) {
00169                      fprintf(stderr, "%s: error reading input picture\n",
00170                                    progname);
00171                      exit(1);
00172               }
00173               if (bradj)
00174                      shiftcolrs(scanin, xmax, bradj);
00175               if (doflat)
00176                      fwrite((char *)scanin, sizeof(COLR), xmax, fp);
00177               else
00178                      fwritecolrs(scanin, xmax, fp);
00179               if (ferror(fp)) {
00180                      fprintf(stderr, "%s: error writing output to \"%s\"\n",
00181                                    progname, oname);
00182                      exit(1);
00183               }
00184        }
00185                                    /* clean up */
00186        if (oname[0] == '!')
00187               pclose(fp);
00188        else if (ospec != NULL)
00189               fclose(fp);
00190        return(1);
00191 }
00192 
00193 
00194 static int
00195 addhline(                   /* add a line to our info. header */
00196        char   *s,
00197        void   *p
00198 )
00199 {
00200        char   fmt[32];
00201        int    n;
00202 
00203        if (formatval(fmt, s))
00204               fmterr += !globmatch(PICFMT, fmt);
00205        else if (!strncmp(s, "FRAME=", 6))
00206               frameno = atoi(s+6);
00207        n = strlen(s);
00208        if (headlen)
00209               headlines = (char *)realloc((void *)headlines, headlen+n+1);
00210        else
00211               headlines = (char *)malloc(n+1);
00212        if (headlines == NULL) {
00213               perror(progname);
00214               exit(1);
00215        }
00216        strcpy(headlines+headlen, s);
00217        headlen += n;
00218        return(0);
00219 }
00220 
00221 
00222 static int
00223 loadheader(                 /* load an info. header into memory */
00224        FILE   *fp
00225 )
00226 {
00227        fmterr = 0; frameno = 0;
00228        if (headlen) {                     /* free old header */
00229               free(headlines);
00230               headlen = 0;
00231        }
00232        if (getheader(fp, addhline, NULL) < 0)
00233               return(0);
00234        if (fmterr)
00235               return(-1);
00236        return(1);
00237 }