ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_avs.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 -8 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 schorsch 2.10 static const char RCSid[] = "$Id: ra_avs.c,v 2.9 2003/02/22 02:07:27 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Convert Radiance file to/from AVS file.
6     */
7    
8     #include <stdio.h>
9 greg 2.3 #include <math.h>
10 greg 2.9 #include <time.h>
11 schorsch 2.10
12     #include "platform.h"
13 greg 2.1 #include "color.h"
14     #include "resolu.h"
15    
16 greg 2.4 double gamcor = 2.2; /* gamma correction */
17 greg 2.1
18     int bradj = 0; /* brightness adjustment */
19    
20     char *progname;
21    
22     int xmax, ymax;
23    
24    
25     main(argc, argv)
26     int argc;
27     char *argv[];
28     {
29 greg 2.2 extern long getint();
30 greg 2.1 int reverse = 0;
31     int i;
32    
33     progname = argv[0];
34    
35     for (i = 1; i < argc; i++)
36     if (argv[i][0] == '-')
37     switch (argv[i][1]) {
38     case 'g': /* gamma correction */
39 greg 2.4 gamcor = atof(argv[++i]);
40 greg 2.1 break;
41     case 'e': /* exposure adjustment */
42     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
43     goto userr;
44     bradj = atoi(argv[++i]);
45     break;
46     case 'r': /* reverse conversion */
47     reverse = 1;
48     break;
49     default:
50     goto userr;
51     }
52     else
53     break;
54    
55     if (i < argc-2)
56     goto userr;
57     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
58     fprintf(stderr, "%s: can't open input \"%s\"\n",
59     progname, argv[i]);
60     exit(1);
61     }
62     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
63 greg 2.6 fprintf(stderr, "%s: can't open output \"%s\"\n",
64 greg 2.1 progname, argv[i+1]);
65     exit(1);
66     }
67 schorsch 2.10 SET_FILE_BINARY(stdin);
68     SET_FILE_BINARY(stdout);
69 greg 2.4 setcolrgam(gamcor); /* set up gamma correction */
70 greg 2.1 if (reverse) {
71     /* get their image resolution */
72 greg 2.2 xmax = getint(4, stdin);
73     ymax = getint(4, stdin);
74 greg 2.7 if (feof(stdin))
75     quiterr("empty input file");
76 greg 2.1 /* put our header */
77 greg 2.8 newheader("RADIANCE", stdout);
78 greg 2.1 printargs(i, argv, stdout);
79     fputformat(COLRFMT, stdout);
80     putchar('\n');
81     fprtresolu(xmax, ymax, stdout);
82     /* convert file */
83     avs2ra();
84     } else {
85     /* get our header */
86     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
87     fgetresolu(&xmax, &ymax, stdin) < 0)
88     quiterr("bad picture format");
89     /* write their header */
90 greg 2.2 putint((long)xmax, 4, stdout);
91     putint((long)ymax, 4, stdout);
92 greg 2.1 /* convert file */
93     ra2avs();
94     }
95     exit(0);
96     userr:
97     fprintf(stderr,
98     "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
99     progname);
100     exit(1);
101     }
102    
103    
104     quiterr(err) /* print message and exit */
105     char *err;
106     {
107     if (err != NULL) {
108     fprintf(stderr, "%s: %s\n", progname, err);
109     exit(1);
110     }
111     exit(0);
112     }
113    
114    
115     avs2ra() /* convert 24-bit scanlines to Radiance picture */
116     {
117     COLR *scanout;
118     register int x;
119     int y;
120     /* allocate scanline */
121     scanout = (COLR *)malloc(xmax*sizeof(COLR));
122     if (scanout == NULL)
123     quiterr("out of memory in avs2ra");
124     /* convert image */
125     for (y = ymax-1; y >= 0; y--) {
126 greg 2.5 for (x = 0; x < xmax; x++) {
127     (void)getc(stdin); /* toss alpha */
128     scanout[x][RED] = getc(stdin);
129     scanout[x][GRN] = getc(stdin);
130     scanout[x][BLU] = getc(stdin);
131     }
132     if (feof(stdin) | ferror(stdin))
133 greg 2.1 quiterr("error reading AVS image");
134     /* undo gamma */
135     gambs_colrs(scanout, xmax);
136     if (bradj) /* adjust exposure */
137     shiftcolrs(scanout, xmax, bradj);
138     if (fwritecolrs(scanout, xmax, stdout) < 0)
139     quiterr("error writing Radiance picture");
140     }
141     /* free scanline */
142 greg 2.9 free((void *)scanout);
143 greg 2.1 }
144    
145    
146     ra2avs() /* convert Radiance scanlines to 24-bit */
147     {
148     COLR *scanin;
149     register int x;
150     int y;
151     /* allocate scanline */
152     scanin = (COLR *)malloc(xmax*sizeof(COLR));
153     if (scanin == NULL)
154     quiterr("out of memory in ra2avs");
155     /* convert image */
156     for (y = ymax-1; y >= 0; y--) {
157     if (freadcolrs(scanin, xmax, stdin) < 0)
158     quiterr("error reading Radiance picture");
159     if (bradj) /* adjust exposure */
160     shiftcolrs(scanin, xmax, bradj);
161     colrs_gambs(scanin, xmax); /* gamma correction */
162     for (x = 0; x < xmax; x++) {
163     putc(0, stdout); /* no alpha */
164     putc(scanin[x][RED], stdout);
165     putc(scanin[x][GRN], stdout);
166     putc(scanin[x][BLU], stdout);
167     }
168     if (ferror(stdout))
169     quiterr("error writing AVS file");
170     }
171     /* free scanline */
172 greg 2.9 free((void *)scanin);
173 greg 2.1 }