ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pr24.c
Revision: 2.10
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 11 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.9: +5 -15 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.10 static const char RCSid[] = "$Id: ra_pr24.c,v 2.9 2003/02/22 02:07:28 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * program to convert between RADIANCE and 24-bit rasterfiles.
6     */
7    
8     #include <stdio.h>
9 greg 2.9 #include <time.h>
10 greg 2.5 #include <math.h>
11    
12 schorsch 2.10 #include "platform.h"
13 greg 1.1 #include "rasterfile.h"
14     #include "color.h"
15 greg 1.11 #include "resolu.h"
16 greg 1.1
17 greg 2.6 double gamcor = 2.2; /* gamma correction */
18 greg 1.1
19 greg 1.7 int bradj = 0; /* brightness adjustment */
20    
21 greg 1.1 char *progname;
22    
23     int xmax, ymax;
24    
25    
26     main(argc, argv)
27     int argc;
28     char *argv[];
29     {
30     struct rasterfile head;
31     int reverse = 0;
32     int i;
33 schorsch 2.10 SET_DEFAULT_BINARY();
34     SET_FILE_BINARY(stdin);
35     SET_FILE_BINARY(stdout);
36 greg 1.1 progname = argv[0];
37    
38 greg 1.9 head.ras_type = RT_STANDARD;
39 greg 1.1 for (i = 1; i < argc; i++)
40     if (argv[i][0] == '-')
41     switch (argv[i][1]) {
42     case 'g':
43 greg 2.6 gamcor = atof(argv[++i]);
44 greg 1.1 break;
45 greg 1.7 case 'e':
46     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
47     goto userr;
48     bradj = atoi(argv[++i]);
49     break;
50 greg 1.1 case 'r':
51 greg 1.9 if (!strcmp(argv[i], "-rgb"))
52     head.ras_type = RT_FORMAT_RGB;
53     else
54     reverse = 1;
55 greg 1.1 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.7 fprintf(stderr, "%s: can't open output \"%s\"\n",
71 greg 1.1 progname, argv[i+1]);
72     exit(1);
73     }
74 greg 2.6 setcolrgam(gamcor);
75 greg 1.1 if (reverse) {
76     /* get header */
77     if (fread((char *)&head, sizeof(head), 1, stdin) != 1)
78     quiterr("missing header");
79     if (head.ras_magic != RAS_MAGIC)
80     quiterr("bad raster format");
81     xmax = head.ras_width;
82     ymax = head.ras_height;
83 greg 1.6 if ((head.ras_type != RT_STANDARD
84     && head.ras_type != RT_FORMAT_RGB)
85     || head.ras_maptype != RMT_NONE
86     || head.ras_depth != 24)
87 greg 1.1 quiterr("incompatible format");
88     /* put header */
89 greg 2.8 newheader("RADIANCE", stdout);
90 greg 1.1 printargs(i, argv, stdout);
91 greg 1.5 fputformat(COLRFMT, stdout);
92 greg 1.1 putchar('\n');
93 greg 1.11 fprtresolu(xmax, ymax, stdout);
94 greg 1.1 /* convert file */
95 greg 2.4 pr2ra(head.ras_type, head.ras_length/ymax - xmax*3);
96 greg 1.1 } else {
97 greg 1.5 /* get header info. */
98     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
99 greg 1.11 fgetresolu(&xmax, &ymax, stdin) < 0)
100 greg 1.5 quiterr("bad picture format");
101 greg 1.1 /* write rasterfile header */
102     head.ras_magic = RAS_MAGIC;
103 greg 2.4 head.ras_width = xmax + (xmax&1);
104 greg 1.1 head.ras_height = ymax;
105     head.ras_depth = 24;
106 greg 2.4 head.ras_length = head.ras_width*head.ras_height*3;
107 greg 1.1 head.ras_maptype = RMT_NONE;
108     head.ras_maplength = 0;
109     fwrite((char *)&head, sizeof(head), 1, stdout);
110     /* convert file */
111 greg 2.4 ra2pr(head.ras_type, head.ras_length/ymax - xmax*3);
112 greg 1.1 }
113     exit(0);
114     userr:
115 greg 1.7 fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
116 greg 1.1 progname);
117     exit(1);
118     }
119    
120    
121     quiterr(err) /* print message and exit */
122     char *err;
123     {
124     if (err != NULL) {
125     fprintf(stderr, "%s: %s\n", progname, err);
126     exit(1);
127     }
128     exit(0);
129     }
130    
131    
132 greg 2.4 pr2ra(rf, pad) /* convert 24-bit scanlines to Radiance picture */
133 greg 1.6 int rf;
134 greg 2.4 int pad;
135 greg 1.1 {
136 greg 1.3 COLR *scanout;
137 greg 1.1 register int x;
138     int y;
139     /* allocate scanline */
140 greg 1.3 scanout = (COLR *)malloc(xmax*sizeof(COLR));
141 greg 1.1 if (scanout == NULL)
142     quiterr("out of memory in pr2ra");
143     /* convert image */
144     for (y = ymax-1; y >= 0; y--) {
145 greg 1.9 if (rf == RT_FORMAT_RGB)
146     for (x = 0; x < xmax; x++) {
147 greg 1.6 scanout[x][RED] = getc(stdin);
148     scanout[x][GRN] = getc(stdin);
149     scanout[x][BLU] = getc(stdin);
150 greg 1.9 }
151     else
152     for (x = 0; x < xmax; x++) {
153 greg 1.6 scanout[x][BLU] = getc(stdin);
154     scanout[x][GRN] = getc(stdin);
155     scanout[x][RED] = getc(stdin);
156     }
157 greg 2.4 for (x = pad; x--; getc(stdin));
158 greg 1.3 if (feof(stdin) || ferror(stdin))
159     quiterr("error reading rasterfile");
160     gambs_colrs(scanout, xmax);
161 greg 1.7 if (bradj)
162     shiftcolrs(scanout, xmax, bradj);
163 greg 1.3 if (fwritecolrs(scanout, xmax, stdout) < 0)
164 greg 1.1 quiterr("error writing Radiance picture");
165     }
166     /* free scanline */
167 greg 2.9 free((void *)scanout);
168 greg 1.1 }
169    
170    
171 greg 2.4 ra2pr(rf, pad) /* convert Radiance scanlines to 24-bit rasterfile */
172     int rf;
173     int pad;
174 greg 1.1 {
175 greg 2.4 int ord[3];
176 greg 1.3 COLR *scanin;
177 greg 1.1 register int x;
178     int y;
179     /* allocate scanline */
180 greg 1.3 scanin = (COLR *)malloc(xmax*sizeof(COLR));
181 greg 1.1 if (scanin == NULL)
182 greg 1.8 quiterr("out of memory in ra2pr");
183 greg 2.4 if (rf == RT_FORMAT_RGB) {
184     ord[0] = RED; ord[1] = GRN; ord[2] = BLU;
185     } else {
186     ord[0] = BLU; ord[1] = GRN; ord[2] = RED;
187     }
188 greg 1.1 /* convert image */
189     for (y = ymax-1; y >= 0; y--) {
190 greg 1.3 if (freadcolrs(scanin, xmax, stdin) < 0)
191 greg 1.1 quiterr("error reading Radiance picture");
192 greg 1.7 if (bradj)
193     shiftcolrs(scanin, xmax, bradj);
194 greg 1.3 colrs_gambs(scanin, xmax);
195 greg 1.9 if (rf == RT_FORMAT_RGB)
196     for (x = 0; x < xmax; x++) {
197     putc(scanin[x][RED], stdout);
198     putc(scanin[x][GRN], stdout);
199     putc(scanin[x][BLU], stdout);
200     }
201     else
202     for (x = 0; x < xmax; x++) {
203     putc(scanin[x][BLU], stdout);
204     putc(scanin[x][GRN], stdout);
205     putc(scanin[x][RED], stdout);
206     }
207 greg 2.4 for (x = 0; x < pad; x++)
208     putc(scanin[xmax-1][ord[x%3]], stdout);
209 greg 1.1 if (ferror(stdout))
210     quiterr("error writing rasterfile");
211     }
212     /* free scanline */
213 greg 2.9 free((void *)scanin);
214 greg 1.1 }