ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_avs.c
Revision: 2.7
Committed: Wed Dec 1 10:02:15 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +2 -0 lines
Log Message:
fixed detection of empty AVS input file

File Contents

# User Rev Content
1 greg 2.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 greg 2.3 #include <math.h>
13 greg 2.1 #ifdef MSDOS
14     #include <fcntl.h>
15     #endif
16     #include "color.h"
17     #include "resolu.h"
18    
19     extern char *malloc();
20    
21 greg 2.4 double gamcor = 2.2; /* gamma correction */
22 greg 2.1
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 greg 2.2 extern long getint();
35 greg 2.1 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 greg 2.4 gamcor = atof(argv[++i]);
45 greg 2.1 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 greg 2.6 fprintf(stderr, "%s: can't open output \"%s\"\n",
69 greg 2.1 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 greg 2.4 setcolrgam(gamcor); /* set up gamma correction */
77 greg 2.1 if (reverse) {
78     /* get their image resolution */
79 greg 2.2 xmax = getint(4, stdin);
80     ymax = getint(4, stdin);
81 greg 2.7 if (feof(stdin))
82     quiterr("empty input file");
83 greg 2.1 /* put our header */
84     printargs(i, argv, stdout);
85     fputformat(COLRFMT, stdout);
86     putchar('\n');
87     fprtresolu(xmax, ymax, stdout);
88     /* convert file */
89     avs2ra();
90     } else {
91     /* get our header */
92     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
93     fgetresolu(&xmax, &ymax, stdin) < 0)
94     quiterr("bad picture format");
95     /* write their header */
96 greg 2.2 putint((long)xmax, 4, stdout);
97     putint((long)ymax, 4, stdout);
98 greg 2.1 /* convert file */
99     ra2avs();
100     }
101     exit(0);
102     userr:
103     fprintf(stderr,
104     "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
105     progname);
106     exit(1);
107     }
108    
109    
110     quiterr(err) /* print message and exit */
111     char *err;
112     {
113     if (err != NULL) {
114     fprintf(stderr, "%s: %s\n", progname, err);
115     exit(1);
116     }
117     exit(0);
118     }
119    
120    
121     avs2ra() /* convert 24-bit scanlines to Radiance picture */
122     {
123     COLR *scanout;
124     register int x;
125     int y;
126     /* allocate scanline */
127     scanout = (COLR *)malloc(xmax*sizeof(COLR));
128     if (scanout == NULL)
129     quiterr("out of memory in avs2ra");
130     /* convert image */
131     for (y = ymax-1; y >= 0; y--) {
132 greg 2.5 for (x = 0; x < xmax; x++) {
133     (void)getc(stdin); /* toss alpha */
134     scanout[x][RED] = getc(stdin);
135     scanout[x][GRN] = getc(stdin);
136     scanout[x][BLU] = getc(stdin);
137     }
138     if (feof(stdin) | ferror(stdin))
139 greg 2.1 quiterr("error reading AVS image");
140     /* undo gamma */
141     gambs_colrs(scanout, xmax);
142     if (bradj) /* adjust exposure */
143     shiftcolrs(scanout, xmax, bradj);
144     if (fwritecolrs(scanout, xmax, stdout) < 0)
145     quiterr("error writing Radiance picture");
146     }
147     /* free scanline */
148     free((char *)scanout);
149     }
150    
151    
152     ra2avs() /* convert Radiance scanlines to 24-bit */
153     {
154     COLR *scanin;
155     register int x;
156     int y;
157     /* allocate scanline */
158     scanin = (COLR *)malloc(xmax*sizeof(COLR));
159     if (scanin == NULL)
160     quiterr("out of memory in ra2avs");
161     /* convert image */
162     for (y = ymax-1; y >= 0; y--) {
163     if (freadcolrs(scanin, xmax, stdin) < 0)
164     quiterr("error reading Radiance picture");
165     if (bradj) /* adjust exposure */
166     shiftcolrs(scanin, xmax, bradj);
167     colrs_gambs(scanin, xmax); /* gamma correction */
168     for (x = 0; x < xmax; x++) {
169     putc(0, stdout); /* no alpha */
170     putc(scanin[x][RED], stdout);
171     putc(scanin[x][GRN], stdout);
172     putc(scanin[x][BLU], stdout);
173     }
174     if (ferror(stdout))
175     quiterr("error writing AVS file");
176     }
177     /* free scanline */
178     free((char *)scanin);
179     }