ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_skel.c
Revision: 2.11
Committed: Sat Feb 22 02:07:28 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.10: +4 -8 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id$";
3 greg 2.1 #endif
4 greg 2.4 /*
5     * Skeletal 24-bit image conversion program. Replace "skel"
6 greg 2.1 * in this file with a more appropriate image type identifier.
7     *
8 greg 2.10 * The Rmakefile entry should look something like this:
9 greg 2.1 * ra_skel: ra_skel.o
10     * cc $(CFLAGS) -o ra_skel ra_skel.o -lrt -lm
11     * ra_skel.o: ../common/color.h ../common/resolu.h
12     *
13     * If you like to do things the hard way, you can link directly
14     * to the object files "color.o colrops.o resolu.o header.o" in
15     * the common subdirectory instead of using the -lrt library.
16     */
17    
18     #include <stdio.h>
19 greg 2.5 #include <math.h>
20 greg 2.3 #ifdef MSDOS
21     #include <fcntl.h>
22     #endif
23 greg 2.11 #include <time.h>
24 greg 2.1 #include "color.h"
25     #include "resolu.h"
26    
27 greg 2.6 double gamcor = 2.2; /* gamma correction */
28 greg 2.1
29     int bradj = 0; /* brightness adjustment */
30    
31     char *progname;
32    
33     int xmax, ymax;
34    
35    
36     main(argc, argv)
37     int argc;
38     char *argv[];
39     {
40     int reverse = 0;
41     int i;
42    
43     progname = argv[0];
44    
45     for (i = 1; i < argc; i++)
46     if (argv[i][0] == '-')
47     switch (argv[i][1]) {
48     case 'g': /* gamma correction */
49 greg 2.6 gamcor = atof(argv[++i]);
50 greg 2.1 break;
51     case 'e': /* exposure adjustment */
52     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
53     goto userr;
54     bradj = atoi(argv[++i]);
55     break;
56     case 'r': /* reverse conversion */
57     reverse = 1;
58     break;
59     default:
60     goto userr;
61     }
62     else
63     break;
64    
65     if (i < argc-2)
66     goto userr;
67     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
68     fprintf(stderr, "%s: can't open input \"%s\"\n",
69     progname, argv[i]);
70     exit(1);
71     }
72     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
73 greg 2.8 fprintf(stderr, "%s: can't open output \"%s\"\n",
74 greg 2.1 progname, argv[i+1]);
75     exit(1);
76     }
77 greg 2.3 #ifdef MSDOS
78     setmode(fileno(stdin), O_BINARY);
79     setmode(fileno(stdout), O_BINARY);
80     #endif
81 greg 2.6 setcolrgam(gamcor); /* set up gamma correction */
82 greg 2.1 if (reverse) {
83     /* get their image resolution */
84     read_skel_head(&xmax, &ymax);
85     /* put our header */
86 greg 2.9 newheader("RADIANCE", stdout);
87 greg 2.1 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 greg 2.7 for (x = 0; x < xmax; x++) {
135     scanout[x][RED] = getc(stdin);
136     scanout[x][GRN] = getc(stdin);
137     scanout[x][BLU] = getc(stdin);
138     }
139     if (feof(stdin) | ferror(stdin))
140 greg 2.1 quiterr("error reading skel image");
141     /* undo gamma */
142     gambs_colrs(scanout, xmax);
143     if (bradj) /* adjust exposure */
144     shiftcolrs(scanout, xmax, bradj);
145     if (fwritecolrs(scanout, xmax, stdout) < 0)
146     quiterr("error writing Radiance picture");
147     }
148     /* free scanline */
149 greg 2.11 free((void *)scanout);
150 greg 2.1 }
151    
152    
153     ra2skel() /* convert Radiance scanlines to 24-bit */
154     {
155     COLR *scanin;
156     register int x;
157     int y;
158     /* allocate scanline */
159     scanin = (COLR *)malloc(xmax*sizeof(COLR));
160     if (scanin == NULL)
161     quiterr("out of memory in ra2skel");
162     /* convert image */
163     for (y = ymax-1; y >= 0; y--) {
164     if (freadcolrs(scanin, xmax, stdin) < 0)
165     quiterr("error reading Radiance picture");
166     if (bradj) /* adjust exposure */
167     shiftcolrs(scanin, xmax, bradj);
168     colrs_gambs(scanin, xmax); /* gamma correction */
169     for (x = 0; x < xmax; x++) {
170     putc(scanin[x][RED], stdout);
171     putc(scanin[x][GRN], stdout);
172     putc(scanin[x][BLU], stdout);
173     }
174     if (ferror(stdout))
175     quiterr("error writing skel file");
176     }
177     /* free scanline */
178 greg 2.11 free((void *)scanin);
179 greg 2.1 }