ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_skel.c
Revision: 2.1
Committed: Tue Nov 12 15:16:41 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2     static char SCCSid[] = "$SunId$ 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     #include "color.h"
21     #include "resolu.h"
22    
23     extern double atof();
24    
25     double gamma = 2.2; /* gamma correction */
26    
27     int bradj = 0; /* brightness adjustment */
28    
29     char *progname;
30    
31     int xmax, ymax;
32    
33    
34     main(argc, argv)
35     int argc;
36     char *argv[];
37     {
38     int reverse = 0;
39     int i;
40    
41     progname = argv[0];
42    
43     for (i = 1; i < argc; i++)
44     if (argv[i][0] == '-')
45     switch (argv[i][1]) {
46     case 'g': /* gamma correction */
47     gamma = atof(argv[++i]);
48     break;
49     case 'e': /* exposure adjustment */
50     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
51     goto userr;
52     bradj = atoi(argv[++i]);
53     break;
54     case 'r': /* reverse conversion */
55     reverse = 1;
56     break;
57     default:
58     goto userr;
59     }
60     else
61     break;
62    
63     if (i < argc-2)
64     goto userr;
65     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
66     fprintf(stderr, "%s: can't open input \"%s\"\n",
67     progname, argv[i]);
68     exit(1);
69     }
70     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
71     fprintf(stderr, "can't open output \"%s\"\n",
72     progname, argv[i+1]);
73     exit(1);
74     }
75     setcolrgam(gamma); /* set up gamma correction */
76     if (reverse) {
77     /* get their image resolution */
78     read_skel_head(&xmax, &ymax);
79     /* put our header */
80     printargs(i, argv, stdout);
81     fputformat(COLRFMT, stdout);
82     putchar('\n');
83     fprtresolu(xmax, ymax, stdout);
84     /* convert file */
85     skel2ra();
86     } else {
87     /* get our header */
88     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
89     fgetresolu(&xmax, &ymax, stdin) < 0)
90     quiterr("bad picture format");
91     /* write their header */
92     write_skel_head(xmax, ymax);
93     /* convert file */
94     ra2skel();
95     }
96     exit(0);
97     userr:
98     fprintf(stderr,
99     "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
100     progname);
101     exit(1);
102     }
103    
104    
105     quiterr(err) /* print message and exit */
106     char *err;
107     {
108     if (err != NULL) {
109     fprintf(stderr, "%s: %s\n", progname, err);
110     exit(1);
111     }
112     exit(0);
113     }
114    
115    
116     skel2ra() /* convert 24-bit scanlines to Radiance picture */
117     {
118     COLR *scanout;
119     register int x;
120     int y;
121     /* allocate scanline */
122     scanout = (COLR *)malloc(xmax*sizeof(COLR));
123     if (scanout == NULL)
124     quiterr("out of memory in skel2ra");
125     /* convert image */
126     for (y = ymax-1; y >= 0; y--) {
127     scanout[x][RED] = getc(stdin);
128     scanout[x][GRN] = getc(stdin);
129     scanout[x][BLU] = getc(stdin);
130     if (feof(stdin) || ferror(stdin))
131     quiterr("error reading skel image");
132     /* undo gamma */
133     gambs_colrs(scanout, xmax);
134     if (bradj) /* adjust exposure */
135     shiftcolrs(scanout, xmax, bradj);
136     if (fwritecolrs(scanout, xmax, stdout) < 0)
137     quiterr("error writing Radiance picture");
138     }
139     /* free scanline */
140     free((char *)scanout);
141     }
142    
143    
144     ra2skel() /* convert Radiance scanlines to 24-bit */
145     {
146     COLR *scanin;
147     register int x;
148     int y;
149     /* allocate scanline */
150     scanin = (COLR *)malloc(xmax*sizeof(COLR));
151     if (scanin == NULL)
152     quiterr("out of memory in ra2skel");
153     /* convert image */
154     for (y = ymax-1; y >= 0; y--) {
155     if (freadcolrs(scanin, xmax, stdin) < 0)
156     quiterr("error reading Radiance picture");
157     if (bradj) /* adjust exposure */
158     shiftcolrs(scanin, xmax, bradj);
159     colrs_gambs(scanin, xmax); /* gamma correction */
160     for (x = 0; x < xmax; x++) {
161     putc(scanin[x][RED], stdout);
162     putc(scanin[x][GRN], stdout);
163     putc(scanin[x][BLU], stdout);
164     }
165     if (ferror(stdout))
166     quiterr("error writing skel file");
167     }
168     /* free scanline */
169     free((char *)scanin);
170     }