Back to index

radiance  4R0+20100331
ra_skel.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: ra_skel.c,v 2.13 2006/03/10 19:40:13 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Skeletal 24-bit image conversion program.  Replace "skel"
00006  *  in this file with a more appropriate image type identifier.
00007  *
00008  *  The Rmakefile entry should look something like this:
00009  *     ra_skel:      ra_skel.o
00010  *            cc $(CFLAGS) -o ra_skel ra_skel.o -lrtrad -lm
00011  *     ra_skel.o:    ../common/color.h ../common/resolu.h
00012  *
00013  *  If you like to do things the hard way, you can link directly
00014  *  to the object files "color.o colrops.o resolu.o header.o" in
00015  *  the common subdirectory instead of using the -lrtrad library.
00016  */
00017 
00018 #include  <stdio.h>
00019 #include  <math.h>
00020 #include  <time.h>
00021 
00022 #include  "platform.h"
00023 #include  "color.h"
00024 #include  "resolu.h"
00025 
00026 double gamcor = 2.2;               /* gamma correction */
00027 
00028 int  bradj = 0;                           /* brightness adjustment */
00029 
00030 char  *progname;
00031 
00032 int  xmax, ymax;
00033 
00034 
00035 main(argc, argv)
00036 int  argc;
00037 char  *argv[];
00038 {
00039        int  reverse = 0;
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 'g':            /* gamma correction */
00048                             gamcor = atof(argv[++i]);
00049                             break;
00050                      case 'e':            /* exposure adjustment */
00051                             if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
00052                                    goto userr;
00053                             bradj = atoi(argv[++i]);
00054                             break;
00055                      case 'r':            /* reverse conversion */
00056                             reverse = 1;
00057                             break;
00058                      default:
00059                             goto userr;
00060                      }
00061               else
00062                      break;
00063 
00064        if (i < argc-2)
00065               goto userr;
00066        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
00067               fprintf(stderr, "%s: can't open input \"%s\"\n",
00068                             progname, argv[i]);
00069               exit(1);
00070        }
00071        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
00072               fprintf(stderr, "%s: can't open output \"%s\"\n",
00073                             progname, argv[i+1]);
00074               exit(1);
00075        }
00076        SET_FILE_BINARY(stdin);
00077        SET_FILE_BINARY(stdout);
00078        setcolrgam(gamcor);         /* set up gamma correction */
00079        if (reverse) {
00080                                    /* get their image resolution */
00081               read_skel_head(&xmax, &ymax);
00082                                    /* put our header */
00083               newheader("RADIANCE", stdout);
00084               printargs(i, argv, stdout);
00085               fputformat(COLRFMT, stdout);
00086               putchar('\n');
00087               fprtresolu(xmax, ymax, stdout);
00088                                    /* convert file */
00089               skel2ra();
00090        } else {
00091                                    /* get our header */
00092               if (checkheader(stdin, COLRFMT, NULL) < 0 ||
00093                             fgetresolu(&xmax, &ymax, stdin) < 0)
00094                      quiterr("bad picture format");
00095                                    /* write their header */
00096               write_skel_head(xmax, ymax);
00097                                    /* convert file */
00098               ra2skel();
00099        }
00100        exit(0);
00101 userr:
00102        fprintf(stderr,
00103               "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
00104                      progname);
00105        exit(1);
00106 }
00107 
00108 
00109 quiterr(err)         /* print message and exit */
00110 char  *err;
00111 {
00112        if (err != NULL) {
00113               fprintf(stderr, "%s: %s\n", progname, err);
00114               exit(1);
00115        }
00116        exit(0);
00117 }
00118 
00119 
00120 skel2ra()            /* convert 24-bit scanlines to Radiance picture */
00121 {
00122        COLR   *scanout;
00123        register int  x;
00124        int    y;
00125                                           /* allocate scanline */
00126        scanout = (COLR *)malloc(xmax*sizeof(COLR));
00127        if (scanout == NULL)
00128               quiterr("out of memory in skel2ra");
00129                                           /* convert image */
00130        for (y = ymax-1; y >= 0; y--) {
00131               for (x = 0; x < xmax; x++) {
00132                      scanout[x][RED] = getc(stdin);
00133                      scanout[x][GRN] = getc(stdin);
00134                      scanout[x][BLU] = getc(stdin);
00135               }
00136               if (feof(stdin) | ferror(stdin))
00137                      quiterr("error reading skel image");
00138                                           /* undo gamma */
00139               gambs_colrs(scanout, xmax);
00140               if (bradj)                  /* adjust exposure */
00141                      shiftcolrs(scanout, xmax, bradj);
00142               if (fwritecolrs(scanout, xmax, stdout) < 0)
00143                      quiterr("error writing Radiance picture");
00144        }
00145                                           /* free scanline */
00146        free((void *)scanout);
00147 }
00148 
00149 
00150 ra2skel()            /* convert Radiance scanlines to 24-bit */
00151 {
00152        COLR   *scanin;
00153        register int  x;
00154        int    y;
00155                                           /* allocate scanline */
00156        scanin = (COLR *)malloc(xmax*sizeof(COLR));
00157        if (scanin == NULL)
00158               quiterr("out of memory in ra2skel");
00159                                           /* convert image */
00160        for (y = ymax-1; y >= 0; y--) {
00161               if (freadcolrs(scanin, xmax, stdin) < 0)
00162                      quiterr("error reading Radiance picture");
00163               if (bradj)                  /* adjust exposure */
00164                      shiftcolrs(scanin, xmax, bradj);
00165               colrs_gambs(scanin, xmax);  /* gamma correction */
00166               for (x = 0; x < xmax; x++) {
00167                      putc(scanin[x][RED], stdout);
00168                      putc(scanin[x][GRN], stdout);
00169                      putc(scanin[x][BLU], stdout);
00170               }
00171               if (ferror(stdout))
00172                      quiterr("error writing skel file");
00173        }
00174                                           /* free scanline */
00175        free((void *)scanin);
00176 }