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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_skel.c,v 2.13 2006/03/10 19:40:13 schorsch Exp $";
3 #endif
4 /*
5 * Skeletal 24-bit image conversion program. Replace "skel"
6 * in this file with a more appropriate image type identifier.
7 *
8 * The Rmakefile entry should look something like this:
9 * ra_skel: ra_skel.o
10 * cc $(CFLAGS) -o ra_skel ra_skel.o -lrtrad -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 -lrtrad library.
16 */
17
18 #include <stdio.h>
19 #include <math.h>
20 #include <time.h>
21 #include "paths.h"
22 #include "platform.h"
23 #include "color.h"
24 #include "resolu.h"
25
26 double gamcor = 2.2; /* gamma correction */
27
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 fixargv0(argv[0]);
41
42 for (i = 1; i < argc; i++)
43 if (argv[i][0] == '-')
44 switch (argv[i][1]) {
45 case 'g': /* gamma correction */
46 gamcor = atof(argv[++i]);
47 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 fprintf(stderr, "%s: can't open output \"%s\"\n",
71 progname, argv[i+1]);
72 exit(1);
73 }
74 SET_FILE_BINARY(stdin);
75 SET_FILE_BINARY(stdout);
76 setcolrgam(gamcor); /* set up gamma correction */
77 if (reverse) {
78 /* get their image resolution */
79 read_skel_head(&xmax, &ymax);
80 /* put our header */
81 newheader("RADIANCE", stdout);
82 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 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 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 free((void *)scanout);
145 }
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 free((void *)scanin);
174 }