ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pr24.c
Revision: 2.3
Committed: Mon Sep 21 12:15:02 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +13 -2 lines
Log Message:
Changes for PC port

File Contents

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