ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pr24.c
Revision: 1.10
Committed: Mon Oct 14 17:09:39 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +1 -1 lines
Log Message:
changed default gamma correction to 2.2

File Contents

# User Rev Content
1 greg 1.7 /* Copyright (c) 1991 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     #include "rasterfile.h"
14    
15     #include "color.h"
16    
17     extern double atof(), pow();
18    
19 greg 1.10 double gamma = 2.2; /* gamma correction */
20 greg 1.1
21 greg 1.7 int bradj = 0; /* brightness adjustment */
22    
23 greg 1.1 char *progname;
24    
25     int xmax, ymax;
26    
27    
28     main(argc, argv)
29     int argc;
30     char *argv[];
31     {
32     struct rasterfile head;
33     int reverse = 0;
34     int i;
35    
36     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     gamma = atof(argv[++i]);
44     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     fprintf(stderr, "can't open output \"%s\"\n",
71     progname, argv[i+1]);
72     exit(1);
73     }
74 greg 1.3 setcolrgam(gamma);
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     printargs(i, argv, stdout);
90 greg 1.5 fputformat(COLRFMT, stdout);
91 greg 1.1 putchar('\n');
92     fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
93     /* convert file */
94 greg 1.6 pr2ra(head.ras_type);
95 greg 1.1 } else {
96 greg 1.5 /* get header info. */
97     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
98     fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR))
99     quiterr("bad picture format");
100 greg 1.1 /* write rasterfile header */
101     head.ras_magic = RAS_MAGIC;
102     head.ras_width = xmax;
103     head.ras_height = ymax;
104     head.ras_depth = 24;
105     head.ras_length = xmax*ymax*3;
106     head.ras_maptype = RMT_NONE;
107     head.ras_maplength = 0;
108     fwrite((char *)&head, sizeof(head), 1, stdout);
109     /* convert file */
110 greg 1.9 ra2pr(head.ras_type);
111 greg 1.1 }
112     exit(0);
113     userr:
114 greg 1.7 fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
115 greg 1.1 progname);
116     exit(1);
117     }
118    
119    
120     quiterr(err) /* print message and exit */
121     char *err;
122     {
123     if (err != NULL) {
124     fprintf(stderr, "%s: %s\n", progname, err);
125     exit(1);
126     }
127     exit(0);
128     }
129    
130    
131 greg 1.6 pr2ra(rf) /* convert 24-bit scanlines to Radiance picture */
132     int rf;
133 greg 1.1 {
134 greg 1.3 COLR *scanout;
135 greg 1.1 register int x;
136     int y;
137     /* allocate scanline */
138 greg 1.3 scanout = (COLR *)malloc(xmax*sizeof(COLR));
139 greg 1.1 if (scanout == NULL)
140     quiterr("out of memory in pr2ra");
141     /* convert image */
142     for (y = ymax-1; y >= 0; y--) {
143 greg 1.9 if (rf == RT_FORMAT_RGB)
144     for (x = 0; x < xmax; x++) {
145 greg 1.6 scanout[x][RED] = getc(stdin);
146     scanout[x][GRN] = getc(stdin);
147     scanout[x][BLU] = getc(stdin);
148 greg 1.9 }
149     else
150     for (x = 0; x < xmax; x++) {
151 greg 1.6 scanout[x][BLU] = getc(stdin);
152     scanout[x][GRN] = getc(stdin);
153     scanout[x][RED] = getc(stdin);
154     }
155 greg 1.3 if (feof(stdin) || ferror(stdin))
156     quiterr("error reading rasterfile");
157     gambs_colrs(scanout, xmax);
158 greg 1.7 if (bradj)
159     shiftcolrs(scanout, xmax, bradj);
160 greg 1.3 if (fwritecolrs(scanout, xmax, stdout) < 0)
161 greg 1.1 quiterr("error writing Radiance picture");
162     }
163     /* free scanline */
164     free((char *)scanout);
165     }
166    
167    
168 greg 1.9 ra2pr(rf) /* convert Radiance scanlines to 24-bit rasterfile */
169     int rf;
170 greg 1.1 {
171 greg 1.3 COLR *scanin;
172 greg 1.1 register int x;
173     int y;
174     /* allocate scanline */
175 greg 1.3 scanin = (COLR *)malloc(xmax*sizeof(COLR));
176 greg 1.1 if (scanin == NULL)
177 greg 1.8 quiterr("out of memory in ra2pr");
178 greg 1.1 /* convert image */
179     for (y = ymax-1; y >= 0; y--) {
180 greg 1.3 if (freadcolrs(scanin, xmax, stdin) < 0)
181 greg 1.1 quiterr("error reading Radiance picture");
182 greg 1.7 if (bradj)
183     shiftcolrs(scanin, xmax, bradj);
184 greg 1.3 colrs_gambs(scanin, xmax);
185 greg 1.9 if (rf == RT_FORMAT_RGB)
186     for (x = 0; x < xmax; x++) {
187     putc(scanin[x][RED], stdout);
188     putc(scanin[x][GRN], stdout);
189     putc(scanin[x][BLU], stdout);
190     }
191     else
192     for (x = 0; x < xmax; x++) {
193     putc(scanin[x][BLU], stdout);
194     putc(scanin[x][GRN], stdout);
195     putc(scanin[x][RED], stdout);
196     }
197 greg 1.1 if (ferror(stdout))
198     quiterr("error writing rasterfile");
199     }
200     /* free scanline */
201     free((char *)scanin);
202     }