ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_avs.c
Revision: 2.8
Committed: Sun Feb 27 10:17:30 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

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