ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ppm.c
Revision: 2.6
Committed: Thu Nov 18 09:55:12 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +1 -2 lines
Log Message:
minor compiler warning fixes

File Contents

# User Rev Content
1 greg 2.3 /* Copyright (c) 1992 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 Poskanzer Pixmaps
9     */
10    
11     #include <stdio.h>
12    
13 greg 2.3 #ifdef MSDOS
14     #include <fcntl.h>
15     #endif
16    
17 greg 2.4 #include <math.h>
18    
19 greg 1.1 #include <ctype.h>
20    
21     #include "color.h"
22    
23 greg 1.2 #include "resolu.h"
24    
25 greg 1.1
26 greg 2.3 extern char *malloc();
27    
28 greg 1.1 int agryscan(), bgryscan(), aclrscan(), bclrscan();
29    
30     int bradj = 0; /* brightness adjustment */
31    
32     int maxval = 255; /* maximum primary value */
33    
34     char *progname;
35    
36     int xmax, ymax;
37    
38    
39     main(argc, argv)
40     int argc;
41     char *argv[];
42     {
43 greg 2.3 char inpbuf[2];
44 greg 2.5 double gamcor = 2.2;
45 greg 1.1 int binflag = 1;
46     int reverse = 0;
47     int ptype;
48     int i;
49    
50     progname = argv[0];
51    
52     for (i = 1; i < argc; i++)
53     if (argv[i][0] == '-')
54     switch (argv[i][1]) {
55     case 'g':
56 greg 2.5 gamcor = atof(argv[++i]);
57 greg 1.1 break;
58     case 'e':
59     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
60     goto userr;
61     bradj = atoi(argv[++i]);
62     break;
63     case 'a':
64     binflag = 0;
65     break;
66     case 'r':
67     reverse = !reverse;
68     break;
69     default:
70     goto userr;
71     }
72     else
73     break;
74    
75     if (i < argc-2)
76     goto userr;
77     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
78     fprintf(stderr, "%s: can't open input \"%s\"\n",
79     progname, argv[i]);
80     exit(1);
81     }
82     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
83 greg 2.6 fprintf(stderr, "%s: can't open output \"%s\"\n",
84 greg 1.1 progname, argv[i+1]);
85     exit(1);
86     }
87 greg 2.5 setcolrgam(gamcor);
88 greg 1.1 if (reverse) {
89     /* get header */
90 greg 2.3 if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P')
91 greg 1.1 quiterr("input not a Poskanzer Pixmap");
92 greg 2.3 ptype = inpbuf[1];
93     #ifdef MSDOS
94     if (ptype > 4)
95     setmode(fileno(stdin), O_BINARY);
96     setmode(fileno(stdout), O_BINARY);
97     #endif
98 greg 1.1 xmax = scanint(stdin);
99     ymax = scanint(stdin);
100     maxval = scanint(stdin);
101     /* put header */
102     printargs(i, argv, stdout);
103     fputformat(COLRFMT, stdout);
104     putchar('\n');
105 greg 1.2 fprtresolu(xmax, ymax, stdout);
106 greg 1.1 /* convert file */
107     switch (ptype) {
108     case '2':
109     ppm2ra(agryscan);
110     break;
111     case '5':
112     ppm2ra(bgryscan);
113     break;
114     case '3':
115     ppm2ra(aclrscan);
116     break;
117     case '6':
118     ppm2ra(bclrscan);
119     break;
120     default:
121     quiterr("unsupported Pixmap type");
122     }
123     } else {
124 greg 2.3 #ifdef MSDOS
125     setmode(fileno(stdin), O_BINARY);
126     if (binflag)
127     setmode(fileno(stdout), O_BINARY);
128     #endif
129 greg 1.1 /* get header info. */
130     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
131 greg 1.2 fgetresolu(&xmax, &ymax, stdin) < 0)
132 greg 1.1 quiterr("bad picture format");
133     /* write PPM header */
134     printf("P%c\n%d %d\n%d\n", binflag ? '6' : '3',
135     xmax, ymax, maxval);
136     /* convert file */
137     ra2ppm(binflag);
138     }
139     exit(0);
140     userr:
141     fprintf(stderr,
142     "Usage: %s [-r][-a][-g gamma][-e +/-stops] [input [output]]\n",
143     progname);
144     exit(1);
145     }
146    
147    
148     quiterr(err) /* print message and exit */
149     char *err;
150     {
151     if (err != NULL) {
152     fprintf(stderr, "%s: %s\n", progname, err);
153     exit(1);
154     }
155     exit(0);
156     }
157    
158    
159     ppm2ra(getscan) /* convert color Pixmap to Radiance picture */
160     int (*getscan)();
161     {
162     COLR *scanout;
163     int y;
164     /* allocate scanline */
165     scanout = (COLR *)malloc(xmax*sizeof(COLR));
166     if (scanout == NULL)
167     quiterr("out of memory in ppm2ra");
168     /* convert image */
169     for (y = ymax-1; y >= 0; y--) {
170     if ((*getscan)(scanout, xmax, stdin) < 0)
171     quiterr("error reading Pixmap");
172     gambs_colrs(scanout, xmax);
173     if (bradj)
174     shiftcolrs(scanout, xmax, bradj);
175     if (fwritecolrs(scanout, xmax, stdout) < 0)
176     quiterr("error writing Radiance picture");
177     }
178     /* free scanline */
179     free((char *)scanout);
180     }
181    
182    
183     ra2ppm(binary) /* convert Radiance picture to Pixmap */
184     int binary;
185     {
186     COLR *scanin;
187     register int x;
188     int y;
189     /* allocate scanline */
190     scanin = (COLR *)malloc(xmax*sizeof(COLR));
191     if (scanin == NULL)
192     quiterr("out of memory in ra2pr");
193     /* convert image */
194     for (y = ymax-1; y >= 0; y--) {
195     if (freadcolrs(scanin, xmax, stdin) < 0)
196     quiterr("error reading Radiance picture");
197     if (bradj)
198     shiftcolrs(scanin, xmax, bradj);
199     colrs_gambs(scanin, xmax);
200     if (binary)
201     for (x = 0; x < xmax; x++) {
202     putc(scanin[x][RED], stdout);
203     putc(scanin[x][GRN], stdout);
204     putc(scanin[x][BLU], stdout);
205     }
206     else
207     for (x = 0; x < xmax; x++)
208     printf("%d %d %d\n", scanin[x][RED],
209     scanin[x][GRN],
210     scanin[x][BLU]);
211     if (ferror(stdout))
212     quiterr("error writing Pixmap");
213     }
214     /* free scanline */
215     free((char *)scanin);
216     }
217    
218    
219     agryscan(scan, len, fp) /* get an ASCII greyscale scanline */
220     register COLR *scan;
221     register int len;
222     FILE *fp;
223     {
224     while (len-- > 0) {
225     scan[0][RED] =
226     scan[0][GRN] =
227     scan[0][BLU] = normval(scanint(fp));
228     scan++;
229     }
230     return(0);
231     }
232    
233    
234     bgryscan(scan, len, fp) /* get a binary greyscale scanline */
235     register COLR *scan;
236     int len;
237     register FILE *fp;
238     {
239     register int c;
240    
241     while (len-- > 0) {
242     if ((c = getc(fp)) == EOF)
243     return(-1);
244     if (maxval != 255)
245     c = normval(c);
246     scan[0][RED] =
247     scan[0][GRN] =
248     scan[0][BLU] = c;
249     scan++;
250     }
251     return(0);
252     }
253    
254    
255     aclrscan(scan, len, fp) /* get an ASCII color scanline */
256     register COLR *scan;
257     register int len;
258     FILE *fp;
259     {
260     while (len-- > 0) {
261     scan[0][RED] = normval(scanint(fp));
262     scan[0][GRN] = normval(scanint(fp));
263     scan[0][BLU] = normval(scanint(fp));
264     scan++;
265     }
266     return(0);
267     }
268    
269    
270     bclrscan(scan, len, fp) /* get a binary color scanline */
271     register COLR *scan;
272     int len;
273     register FILE *fp;
274     {
275     int r, g, b;
276    
277     while (len-- > 0) {
278     r = getc(fp);
279     g = getc(fp);
280     if ((b = getc(fp)) == EOF)
281     return(-1);
282     if (maxval == 255) {
283     scan[0][RED] = r;
284     scan[0][GRN] = g;
285     scan[0][BLU] = b;
286     } else {
287     scan[0][RED] = normval(r);
288     scan[0][GRN] = normval(g);
289     scan[0][BLU] = normval(b);
290     }
291     scan++;
292     }
293     return(0);
294     }
295    
296    
297     int
298     scanint(fp) /* scan the next positive integer value */
299     register FILE *fp;
300     {
301     register int i, c;
302     tryagain:
303     while (isspace(c = getc(fp)))
304     ;
305     if (c == EOF)
306     quiterr("unexpected end of file");
307     if (c == '#') { /* comment */
308     while ((c = getc(fp)) != EOF && c != '\n')
309     ;
310     goto tryagain;
311     }
312     /* should be integer */
313     i = 0;
314     do {
315     if (!isdigit(c))
316     quiterr("error reading integer");
317     i = 10*i + c - '0';
318     c = getc(fp);
319     } while (c != EOF && !isspace(c));
320     return(i);
321     }
322    
323    
324     int
325     normval(v) /* normalize a value to [0,255] */
326     register int v;
327     {
328     if (v >= maxval)
329     return(255);
330     if (maxval == 255)
331     return(v);
332     return(v*255L/maxval);
333     }