ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pr24.c
Revision: 2.8
Committed: Sun Feb 27 10:17:15 1994 UTC (30 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +1 -0 lines
Log Message:
Added new ID to first line of header and changed use of formatval

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 2.5 #include <math.h>
18    
19 greg 1.1 #include "rasterfile.h"
20    
21     #include "color.h"
22    
23 greg 1.11 #include "resolu.h"
24 greg 1.1
25 greg 2.3 extern char *malloc();
26    
27 greg 2.6 double gamcor = 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 greg 2.6 gamcor = atof(argv[++i]);
57 greg 1.1 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 greg 2.7 fprintf(stderr, "%s: can't open output \"%s\"\n",
84 greg 1.1 progname, argv[i+1]);
85     exit(1);
86     }
87 greg 2.6 setcolrgam(gamcor);
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 greg 2.8 newheader("RADIANCE", stdout);
103 greg 1.1 printargs(i, argv, stdout);
104 greg 1.5 fputformat(COLRFMT, stdout);
105 greg 1.1 putchar('\n');
106 greg 1.11 fprtresolu(xmax, ymax, stdout);
107 greg 1.1 /* convert file */
108 greg 2.4 pr2ra(head.ras_type, head.ras_length/ymax - xmax*3);
109 greg 1.1 } else {
110 greg 1.5 /* get header info. */
111     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
112 greg 1.11 fgetresolu(&xmax, &ymax, stdin) < 0)
113 greg 1.5 quiterr("bad picture format");
114 greg 1.1 /* write rasterfile header */
115     head.ras_magic = RAS_MAGIC;
116 greg 2.4 head.ras_width = xmax + (xmax&1);
117 greg 1.1 head.ras_height = ymax;
118     head.ras_depth = 24;
119 greg 2.4 head.ras_length = head.ras_width*head.ras_height*3;
120 greg 1.1 head.ras_maptype = RMT_NONE;
121     head.ras_maplength = 0;
122     fwrite((char *)&head, sizeof(head), 1, stdout);
123     /* convert file */
124 greg 2.4 ra2pr(head.ras_type, head.ras_length/ymax - xmax*3);
125 greg 1.1 }
126     exit(0);
127     userr:
128 greg 1.7 fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
129 greg 1.1 progname);
130     exit(1);
131     }
132    
133    
134     quiterr(err) /* print message and exit */
135     char *err;
136     {
137     if (err != NULL) {
138     fprintf(stderr, "%s: %s\n", progname, err);
139     exit(1);
140     }
141     exit(0);
142     }
143    
144    
145 greg 2.4 pr2ra(rf, pad) /* convert 24-bit scanlines to Radiance picture */
146 greg 1.6 int rf;
147 greg 2.4 int pad;
148 greg 1.1 {
149 greg 1.3 COLR *scanout;
150 greg 1.1 register int x;
151     int y;
152     /* allocate scanline */
153 greg 1.3 scanout = (COLR *)malloc(xmax*sizeof(COLR));
154 greg 1.1 if (scanout == NULL)
155     quiterr("out of memory in pr2ra");
156     /* convert image */
157     for (y = ymax-1; y >= 0; y--) {
158 greg 1.9 if (rf == RT_FORMAT_RGB)
159     for (x = 0; x < xmax; x++) {
160 greg 1.6 scanout[x][RED] = getc(stdin);
161     scanout[x][GRN] = getc(stdin);
162     scanout[x][BLU] = getc(stdin);
163 greg 1.9 }
164     else
165     for (x = 0; x < xmax; x++) {
166 greg 1.6 scanout[x][BLU] = getc(stdin);
167     scanout[x][GRN] = getc(stdin);
168     scanout[x][RED] = getc(stdin);
169     }
170 greg 2.4 for (x = pad; x--; getc(stdin));
171 greg 1.3 if (feof(stdin) || ferror(stdin))
172     quiterr("error reading rasterfile");
173     gambs_colrs(scanout, xmax);
174 greg 1.7 if (bradj)
175     shiftcolrs(scanout, xmax, bradj);
176 greg 1.3 if (fwritecolrs(scanout, xmax, stdout) < 0)
177 greg 1.1 quiterr("error writing Radiance picture");
178     }
179     /* free scanline */
180     free((char *)scanout);
181     }
182    
183    
184 greg 2.4 ra2pr(rf, pad) /* convert Radiance scanlines to 24-bit rasterfile */
185     int rf;
186     int pad;
187 greg 1.1 {
188 greg 2.4 int ord[3];
189 greg 1.3 COLR *scanin;
190 greg 1.1 register int x;
191     int y;
192     /* allocate scanline */
193 greg 1.3 scanin = (COLR *)malloc(xmax*sizeof(COLR));
194 greg 1.1 if (scanin == NULL)
195 greg 1.8 quiterr("out of memory in ra2pr");
196 greg 2.4 if (rf == RT_FORMAT_RGB) {
197     ord[0] = RED; ord[1] = GRN; ord[2] = BLU;
198     } else {
199     ord[0] = BLU; ord[1] = GRN; ord[2] = RED;
200     }
201 greg 1.1 /* convert image */
202     for (y = ymax-1; y >= 0; y--) {
203 greg 1.3 if (freadcolrs(scanin, xmax, stdin) < 0)
204 greg 1.1 quiterr("error reading Radiance picture");
205 greg 1.7 if (bradj)
206     shiftcolrs(scanin, xmax, bradj);
207 greg 1.3 colrs_gambs(scanin, xmax);
208 greg 1.9 if (rf == RT_FORMAT_RGB)
209     for (x = 0; x < xmax; x++) {
210     putc(scanin[x][RED], stdout);
211     putc(scanin[x][GRN], stdout);
212     putc(scanin[x][BLU], stdout);
213     }
214     else
215     for (x = 0; x < xmax; x++) {
216     putc(scanin[x][BLU], stdout);
217     putc(scanin[x][GRN], stdout);
218     putc(scanin[x][RED], stdout);
219     }
220 greg 2.4 for (x = 0; x < pad; x++)
221     putc(scanin[xmax-1][ord[x%3]], stdout);
222 greg 1.1 if (ferror(stdout))
223     quiterr("error writing rasterfile");
224     }
225     /* free scanline */
226     free((char *)scanin);
227     }