ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ppm.c
Revision: 2.3
Committed: Mon Sep 21 12:14:55 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +20 -3 lines
Log Message:
Changes for PC port

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 1.1 #include <ctype.h>
18    
19     #include "color.h"
20    
21 greg 1.2 #include "resolu.h"
22    
23 greg 2.2 extern double pow();
24 greg 1.1
25 greg 2.3 extern char *malloc();
26    
27 greg 1.1 int agryscan(), bgryscan(), aclrscan(), bclrscan();
28    
29     int bradj = 0; /* brightness adjustment */
30    
31     int maxval = 255; /* maximum primary value */
32    
33     char *progname;
34    
35     int xmax, ymax;
36    
37    
38     main(argc, argv)
39     int argc;
40     char *argv[];
41     {
42 greg 2.3 char inpbuf[2];
43 greg 1.1 double gamma = 2.2;
44     int binflag = 1;
45     int reverse = 0;
46     int ptype;
47     int i;
48    
49     progname = argv[0];
50    
51     for (i = 1; i < argc; i++)
52     if (argv[i][0] == '-')
53     switch (argv[i][1]) {
54     case 'g':
55     gamma = atof(argv[++i]);
56     break;
57     case 'e':
58     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
59     goto userr;
60     bradj = atoi(argv[++i]);
61     break;
62     case 'a':
63     binflag = 0;
64     break;
65     case 'r':
66     reverse = !reverse;
67     break;
68     default:
69     goto userr;
70     }
71     else
72     break;
73    
74     if (i < argc-2)
75     goto userr;
76     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
77     fprintf(stderr, "%s: can't open input \"%s\"\n",
78     progname, argv[i]);
79     exit(1);
80     }
81     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
82     fprintf(stderr, "can't open output \"%s\"\n",
83     progname, argv[i+1]);
84     exit(1);
85     }
86     setcolrgam(gamma);
87     if (reverse) {
88     /* get header */
89 greg 2.3 if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P')
90 greg 1.1 quiterr("input not a Poskanzer Pixmap");
91 greg 2.3 ptype = inpbuf[1];
92     #ifdef MSDOS
93     if (ptype > 4)
94     setmode(fileno(stdin), O_BINARY);
95     setmode(fileno(stdout), O_BINARY);
96     #endif
97 greg 1.1 xmax = scanint(stdin);
98     ymax = scanint(stdin);
99     maxval = scanint(stdin);
100     /* put header */
101     printargs(i, argv, stdout);
102     fputformat(COLRFMT, stdout);
103     putchar('\n');
104 greg 1.2 fprtresolu(xmax, ymax, stdout);
105 greg 1.1 /* convert file */
106     switch (ptype) {
107     case '2':
108     ppm2ra(agryscan);
109     break;
110     case '5':
111     ppm2ra(bgryscan);
112     break;
113     case '3':
114     ppm2ra(aclrscan);
115     break;
116     case '6':
117     ppm2ra(bclrscan);
118     break;
119     default:
120     quiterr("unsupported Pixmap type");
121     }
122     } else {
123 greg 2.3 #ifdef MSDOS
124     setmode(fileno(stdin), O_BINARY);
125     if (binflag)
126     setmode(fileno(stdout), O_BINARY);
127     #endif
128 greg 1.1 /* get header info. */
129     if (checkheader(stdin, COLRFMT, NULL) < 0 ||
130 greg 1.2 fgetresolu(&xmax, &ymax, stdin) < 0)
131 greg 1.1 quiterr("bad picture format");
132     /* write PPM header */
133     printf("P%c\n%d %d\n%d\n", binflag ? '6' : '3',
134     xmax, ymax, maxval);
135     /* convert file */
136     ra2ppm(binflag);
137     }
138     exit(0);
139     userr:
140     fprintf(stderr,
141     "Usage: %s [-r][-a][-g gamma][-e +/-stops] [input [output]]\n",
142     progname);
143     exit(1);
144     }
145    
146    
147     quiterr(err) /* print message and exit */
148     char *err;
149     {
150     if (err != NULL) {
151     fprintf(stderr, "%s: %s\n", progname, err);
152     exit(1);
153     }
154     exit(0);
155     }
156    
157    
158     ppm2ra(getscan) /* convert color Pixmap to Radiance picture */
159     int (*getscan)();
160     {
161     COLR *scanout;
162     register int x;
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     }