ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pr24.c
Revision: 2.12
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 2.11: +21 -15 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

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