ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_avs.c
Revision: 2.5
Committed: Wed Oct 27 09:37:37 1993 UTC (30 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +7 -5 lines
Log Message:
fixed missing loop

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, "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 /* put our header */
82 printargs(i, argv, stdout);
83 fputformat(COLRFMT, stdout);
84 putchar('\n');
85 fprtresolu(xmax, ymax, stdout);
86 /* convert file */
87 avs2ra();
88 } else {
89 /* get our header */
90 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
91 fgetresolu(&xmax, &ymax, stdin) < 0)
92 quiterr("bad picture format");
93 /* write their header */
94 putint((long)xmax, 4, stdout);
95 putint((long)ymax, 4, stdout);
96 /* convert file */
97 ra2avs();
98 }
99 exit(0);
100 userr:
101 fprintf(stderr,
102 "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
103 progname);
104 exit(1);
105 }
106
107
108 quiterr(err) /* print message and exit */
109 char *err;
110 {
111 if (err != NULL) {
112 fprintf(stderr, "%s: %s\n", progname, err);
113 exit(1);
114 }
115 exit(0);
116 }
117
118
119 avs2ra() /* convert 24-bit scanlines to Radiance picture */
120 {
121 COLR *scanout;
122 register int x;
123 int y;
124 /* allocate scanline */
125 scanout = (COLR *)malloc(xmax*sizeof(COLR));
126 if (scanout == NULL)
127 quiterr("out of memory in avs2ra");
128 /* convert image */
129 for (y = ymax-1; y >= 0; y--) {
130 for (x = 0; x < xmax; x++) {
131 (void)getc(stdin); /* toss alpha */
132 scanout[x][RED] = getc(stdin);
133 scanout[x][GRN] = getc(stdin);
134 scanout[x][BLU] = getc(stdin);
135 }
136 if (feof(stdin) | ferror(stdin))
137 quiterr("error reading AVS image");
138 /* undo gamma */
139 gambs_colrs(scanout, xmax);
140 if (bradj) /* adjust exposure */
141 shiftcolrs(scanout, xmax, bradj);
142 if (fwritecolrs(scanout, xmax, stdout) < 0)
143 quiterr("error writing Radiance picture");
144 }
145 /* free scanline */
146 free((char *)scanout);
147 }
148
149
150 ra2avs() /* convert Radiance scanlines to 24-bit */
151 {
152 COLR *scanin;
153 register int x;
154 int y;
155 /* allocate scanline */
156 scanin = (COLR *)malloc(xmax*sizeof(COLR));
157 if (scanin == NULL)
158 quiterr("out of memory in ra2avs");
159 /* convert image */
160 for (y = ymax-1; y >= 0; y--) {
161 if (freadcolrs(scanin, xmax, stdin) < 0)
162 quiterr("error reading Radiance picture");
163 if (bradj) /* adjust exposure */
164 shiftcolrs(scanin, xmax, bradj);
165 colrs_gambs(scanin, xmax); /* gamma correction */
166 for (x = 0; x < xmax; x++) {
167 putc(0, stdout); /* no alpha */
168 putc(scanin[x][RED], stdout);
169 putc(scanin[x][GRN], stdout);
170 putc(scanin[x][BLU], stdout);
171 }
172 if (ferror(stdout))
173 quiterr("error writing AVS file");
174 }
175 /* free scanline */
176 free((char *)scanin);
177 }