ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_skel.c
Revision: 2.14
Committed: Fri Jun 6 19:11:21 2025 UTC (3 weeks, 3 days ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.13: +3 -5 lines
Log Message:
refactor: Making use of printargs() more consistent with fixargv0()

File Contents

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