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

  1. #ifndef lint
  2. static char SCCSid[] = "@(#)ra_skel.c 2.2 12/19/91 LBL";
  3. #endif
  4.  
  5. /*
  6.  *  Skeletal 24-bit image conversion program.  Replace "skel"
  7.  *  in this file with a more appropriate image type identifier.
  8.  *
  9.  *  The Makefile entry should look something like this:
  10.  *      ra_skel:        ra_skel.o
  11.  *              cc $(CFLAGS) -o ra_skel ra_skel.o -lrt -lm
  12.  *      ra_skel.o:      ../common/color.h ../common/resolu.h
  13.  *
  14.  *  If you like to do things the hard way, you can link directly
  15.  *  to the object files "color.o colrops.o resolu.o header.o" in
  16.  *  the common subdirectory instead of using the -lrt library.
  17.  */
  18.  
  19. #include  <stdio.h>
  20. #ifdef MSDOS
  21. #include  <fcntl.h>
  22. #endif
  23. #include  "color.h"
  24. #include  "resolu.h"
  25.  
  26. extern char  *malloc();
  27.  
  28. double  gamma = 2.2;                    /* gamma correction */
  29.  
  30. int  bradj = 0;                         /* brightness adjustment */
  31.  
  32. char  *progname;
  33.  
  34. int  xmax, ymax;
  35.  
  36.  
  37. main(argc, argv)
  38. int  argc;
  39. char  *argv[];
  40. {
  41.     int  reverse = 0;
  42.     int  i;
  43.     
  44.     progname = argv[0];
  45.  
  46.     for (i = 1; i < argc; i++)
  47.         if (argv[i][0] == '-')
  48.             switch (argv[i][1]) {
  49.             case 'g':               /* gamma correction */
  50.                 gamma = atof(argv[++i]);
  51.                 break;
  52.             case 'e':               /* exposure adjustment */
  53.                 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
  54.                     goto userr;
  55.                 bradj = atoi(argv[++i]);
  56.                 break;
  57.             case 'r':               /* reverse conversion */
  58.                 reverse = 1;
  59.                 break;
  60.             default:
  61.                 goto userr;
  62.             }
  63.         else
  64.             break;
  65.  
  66.     if (i < argc-2)
  67.         goto userr;
  68.     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
  69.         fprintf(stderr, "%s: can't open input \"%s\"\n",
  70.                 progname, argv[i]);
  71.         exit(1);
  72.     }
  73.     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
  74.         fprintf(stderr, "can't open output \"%s\"\n",
  75.                 progname, argv[i+1]);
  76.         exit(1);
  77.     }
  78. #ifdef MSDOS
  79.     setmode(fileno(stdin), O_BINARY);
  80.     setmode(fileno(stdout), O_BINARY);
  81. #endif
  82.     setcolrgam(gamma);              /* set up gamma correction */
  83.     if (reverse) {
  84.                     /* get their image resolution */
  85.         read_skel_head(&xmax, &ymax);
  86.                     /* put our header */
  87.         printargs(i, argv, stdout);
  88.         fputformat(COLRFMT, stdout);
  89.         putchar('\n');
  90.         fprtresolu(xmax, ymax, stdout);
  91.                     /* convert file */
  92.         skel2ra();
  93.     } else {
  94.                     /* get our header */
  95.         if (checkheader(stdin, COLRFMT, NULL) < 0 ||
  96.                 fgetresolu(&xmax, &ymax, stdin) < 0)
  97.             quiterr("bad picture format");
  98.                     /* write their header */
  99.         write_skel_head(xmax, ymax);
  100.                     /* convert file */
  101.         ra2skel();
  102.     }
  103.     exit(0);
  104. userr:
  105.     fprintf(stderr,
  106.         "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
  107.             progname);
  108.     exit(1);
  109. }
  110.  
  111.  
  112. quiterr(err)            /* print message and exit */
  113. char  *err;
  114. {
  115.     if (err != NULL) {
  116.         fprintf(stderr, "%s: %s\n", progname, err);
  117.         exit(1);
  118.     }
  119.     exit(0);
  120. }
  121.  
  122.  
  123. skel2ra()               /* convert 24-bit scanlines to Radiance picture */
  124. {
  125.     COLR    *scanout;
  126.     register int    x;
  127.     int     y;
  128.                         /* allocate scanline */
  129.     scanout = (COLR *)malloc(xmax*sizeof(COLR));
  130.     if (scanout == NULL)
  131.         quiterr("out of memory in skel2ra");
  132.                         /* convert image */
  133.     for (y = ymax-1; y >= 0; y--) {
  134.         scanout[x][RED] = getc(stdin);
  135.         scanout[x][GRN] = getc(stdin);
  136.         scanout[x][BLU] = getc(stdin);
  137.         if (feof(stdin) || ferror(stdin))
  138.             quiterr("error reading skel image");
  139.                         /* undo gamma */
  140.         gambs_colrs(scanout, xmax);
  141.         if (bradj)                      /* adjust exposure */
  142.             shiftcolrs(scanout, xmax, bradj);
  143.         if (fwritecolrs(scanout, xmax, stdout) < 0)
  144.             quiterr("error writing Radiance picture");
  145.     }
  146.                         /* free scanline */
  147.     free((char *)scanout);
  148. }
  149.  
  150.  
  151. ra2skel()               /* convert Radiance scanlines to 24-bit */
  152. {
  153.     COLR    *scanin;
  154.     register int    x;
  155.     int     y;
  156.                         /* allocate scanline */
  157.     scanin = (COLR *)malloc(xmax*sizeof(COLR));
  158.     if (scanin == NULL)
  159.         quiterr("out of memory in ra2skel");
  160.                         /* convert image */
  161.     for (y = ymax-1; y >= 0; y--) {
  162.         if (freadcolrs(scanin, xmax, stdin) < 0)
  163.             quiterr("error reading Radiance picture");
  164.         if (bradj)                      /* adjust exposure */
  165.             shiftcolrs(scanin, xmax, bradj);
  166.         colrs_gambs(scanin, xmax);      /* gamma correction */
  167.         for (x = 0; x < xmax; x++) {
  168.             putc(scanin[x][RED], stdout);
  169.             putc(scanin[x][GRN], stdout);
  170.             putc(scanin[x][BLU], stdout);
  171.         }
  172.         if (ferror(stdout))
  173.             quiterr("error writing skel file");
  174.     }
  175.                         /* free scanline */
  176.     free((char *)scanin);
  177. }
  178.