ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pr.c
Revision: 1.10
Committed: Thu Apr 18 14:35:38 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +5 -4 lines
Log Message:
added format information to headers

File Contents

# Content
1 /* Copyright (c) 1986 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * ra_pr.c - program to convert between RADIANCE and pixrect picture format.
9 *
10 * 11/10/87
11 * 3/1/88 Added Paul Heckbert's color map allocation
12 */
13
14 #include <stdio.h>
15
16 #include "rasterfile.h"
17
18 #include "color.h"
19
20 #include "pic.h"
21
22 /* descriptor for a picture file or frame buffer */
23 typedef struct {
24 char *name; /* file name */
25 FILE *fp; /* file pointer */
26 int nexty; /* file positioning */
27 int bytes_line; /* 0 == variable length lines */
28 union {
29 long b; /* initial scanline */
30 long *y; /* individual scanline */
31 } pos; /* position(s) */
32 } pic;
33
34 extern pic *openinput(), *openoutput();
35
36 extern char *ecalloc(), *emalloc();
37
38 extern long ftell();
39
40 extern double atof(), pow();
41
42 double gamma = 2.0; /* gamma correction */
43
44 pic *inpic, *outpic;
45
46 char *progname;
47
48 char errmsg[128];
49
50 COLR *inl;
51
52 int xmax, ymax;
53
54
55 main(argc, argv)
56 int argc;
57 char *argv[];
58 {
59 colormap rasmap;
60 struct rasterfile head;
61 int dither = 1;
62 int reverse = 0;
63 int ncolors = 256;
64 int greyscale = 0;
65 int i;
66
67 progname = argv[0];
68
69 for (i = 1; i < argc; i++)
70 if (argv[i][0] == '-')
71 switch (argv[i][1]) {
72 case 'd':
73 dither = !dither;
74 break;
75 case 'g':
76 gamma = atof(argv[++i]);
77 break;
78 case 'b':
79 greyscale = !greyscale;
80 break;
81 case 'r':
82 reverse = !reverse;
83 break;
84 case 'c':
85 ncolors = atoi(argv[++i]);
86 break;
87 default:
88 goto userr;
89 }
90 else
91 break;
92
93 if (reverse) {
94 if (i < argc-2)
95 goto userr;
96 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
97 sprintf(errmsg, "can't open input \"%s\"", argv[i]);
98 quiterr(errmsg);
99 }
100 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
101 sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
102 quiterr(errmsg);
103 }
104 /* get header */
105 if (fread((char *)&head, sizeof(head), 1, stdin) != 1)
106 quiterr("missing header");
107 if (head.ras_magic != RAS_MAGIC)
108 quiterr("bad raster format");
109 xmax = head.ras_width;
110 ymax = head.ras_height;
111 if (head.ras_type != RT_STANDARD ||
112 head.ras_maptype != RMT_EQUAL_RGB ||
113 head.ras_depth != 8)
114 quiterr("incompatible format");
115 /* put header */
116 printargs(i, argv, stdout);
117 fputformat(COLRFMT, stdout);
118 putchar('\n');
119 fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
120 /* convert file */
121 pr2ra(&head);
122 } else {
123 if (i > argc-1 || i < argc-2)
124 goto userr;
125 if ((inpic = openinput(argv[i], &head)) == NULL) {
126 sprintf(errmsg, "can't open input \"%s\"", argv[i]);
127 quiterr(errmsg);
128 }
129 if ((outpic = openoutput(i==argc-2 ? argv[i+1] : (char *)NULL,
130 &head)) == NULL) {
131 sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
132 quiterr(errmsg);
133 }
134 /* convert file */
135 if (greyscale)
136 biq(dither,ncolors,1,rasmap);
137 else
138 ciq(dither,ncolors,1,rasmap);
139 }
140 quiterr(NULL);
141 userr:
142 fprintf(stderr,
143 "Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n",
144 progname);
145 fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n",
146 progname);
147 exit(1);
148 }
149
150
151 quiterr(err) /* print message and exit */
152 char *err;
153 {
154 if (err != NULL) {
155 fprintf(stderr, "%s: %s\n", progname, err);
156 exit(1);
157 }
158 exit(0);
159 }
160
161
162 eputs(s)
163 char *s;
164 {
165 fputs(s, stderr);
166 }
167
168
169 quit(code)
170 int code;
171 {
172 exit(code);
173 }
174
175
176 pic *
177 openinput(fname, h) /* open RADIANCE input file */
178 char *fname;
179 register struct rasterfile *h;
180 {
181 register pic *p;
182
183 p = (pic *)emalloc(sizeof(pic));
184 p->name = fname;
185 if (fname == NULL)
186 p->fp = stdin;
187 else if ((p->fp = fopen(fname, "r")) == NULL)
188 return(NULL);
189 /* check header */
190 if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
191 fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR))
192 quiterr("bad picture format");
193 p->nexty = 0;
194 p->bytes_line = 0; /* variable length lines */
195 p->pos.y = (long *)ecalloc(ymax, sizeof(long));
196 p->pos.y[0] = ftell(p->fp);
197 /* assign header */
198 h->ras_magic = RAS_MAGIC;
199 h->ras_width = xmax + (xmax&1); /* round to 16 bits */
200 h->ras_height = ymax;
201 h->ras_depth = 8;
202 h->ras_type = RT_STANDARD;
203 h->ras_length = h->ras_width*h->ras_height;
204 h->ras_maptype = RMT_EQUAL_RGB;
205 h->ras_maplength = 256*3;
206 /* allocate scanline */
207 inl = (COLR *)emalloc(xmax*sizeof(COLR));
208
209 return(p);
210 }
211
212
213 pic *
214 openoutput(fname, h) /* open output rasterfile */
215 char *fname;
216 register struct rasterfile *h;
217 {
218 register pic *p;
219
220 p = (pic *)emalloc(sizeof(pic));
221 p->name = fname;
222 if (fname == NULL)
223 p->fp = stdout;
224 else if ((p->fp = fopen(fname, "w")) == NULL)
225 return(NULL);
226 /* write header */
227 fwrite((char *)h, sizeof(*h), 1, p->fp);
228 p->nexty = -1; /* needs color map */
229 p->bytes_line = h->ras_width;
230 p->pos.b = 0;
231
232 return(p);
233 }
234
235
236 pr2ra(h) /* pixrect file to RADIANCE file */
237 struct rasterfile *h;
238 {
239 BYTE cmap[3][256];
240 COLR ctab[256];
241 COLR *scanline;
242 register int i, j, c;
243
244 scanline = (COLR *)emalloc(xmax*sizeof(COLR));
245 /* get color table */
246 for (i = 0; i < 3; i ++)
247 if (fread((char *)cmap[i], h->ras_maplength/3, 1, stdin) != 1)
248 quiterr("error reading color table");
249 /* convert table */
250 for (i = 0; i < h->ras_maplength/3; i++)
251 setcolr(ctab[i],
252 pow((cmap[0][i]+.5)/256.,gamma),
253 pow((cmap[1][i]+.5)/256.,gamma),
254 pow((cmap[2][i]+.5)/256.,gamma));
255 /* convert file */
256 for (i = 0; i < ymax; i++) {
257 for (j = 0; j < xmax; j++) {
258 if ((c = getc(stdin)) == EOF)
259 quiterr("error reading rasterfile");
260 copycolr(scanline[j], ctab[c]);
261 }
262 if (xmax & 1) /* extra byte */
263 getc(stdin);
264 if (fwritecolrs(scanline, xmax, stdout) < 0)
265 quiterr("error writing RADIANCE file");
266 }
267 free((char *)scanline);
268 }
269
270
271 picreadline3(y, l3) /* read in 3-byte scanline */
272 int y;
273 register rgbpixel *l3;
274 {
275 register int i;
276
277 if (inpic->nexty != y) { /* find scanline */
278 if (inpic->bytes_line == 0) {
279 if (inpic->pos.y[y] == 0) {
280 while (inpic->nexty < y) {
281 if (freadcolrs(inl, xmax, inpic->fp) < 0)
282 quiterr("read error in picreadline3");
283 inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
284 }
285 } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
286 quiterr("seek error in picreadline3");
287 } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
288 quiterr("seek error in picreadline3");
289 } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
290 inpic->pos.y[inpic->nexty] = ftell(inpic->fp);
291 if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */
292 quiterr("read error in picreadline3");
293 inpic->nexty = y+1;
294 /* convert scanline */
295 normcolrs(inl, xmax, 0);
296 for (i = 0; i < xmax; i++) {
297 l3[i].r = inl[i][RED];
298 l3[i].g = inl[i][GRN];
299 l3[i].b = inl[i][BLU];
300 }
301 }
302
303
304 picwriteline(y, l) /* write out scanline */
305 int y;
306 register pixel *l;
307 {
308 if (outpic->nexty != y) { /* seek to scanline */
309 if (outpic->bytes_line == 0) {
310 if (outpic->pos.y[y] == 0)
311 quiterr("cannot seek in picwriteline");
312 else if (fseek(outpic->fp, outpic->pos.y[y], 0) == EOF)
313 quiterr("seek error in picwriteline");
314 } else if (fseek(outpic->fp, y*outpic->bytes_line+outpic->pos.b, 0) == EOF)
315 quiterr("seek error in picwriteline");
316 }
317 /* write scanline */
318 if (fwrite((char *)l, sizeof(pixel), xmax, outpic->fp) != xmax)
319 quiterr("write error in picwriteline");
320 if (xmax&1) /* on 16-bit boundary */
321 putc(l[xmax-1], outpic->fp);
322 outpic->nexty = y+1;
323 if (outpic->bytes_line == 0 && outpic->pos.y[outpic->nexty] == 0)
324 outpic->pos.y[outpic->nexty] = ftell(outpic->fp);
325 }
326
327
328 picwritecm(cm) /* write out color map */
329 colormap cm;
330 {
331 register int i, j;
332
333 if (outpic->nexty != -1 &&
334 fseek(outpic->fp, (long)sizeof(struct rasterfile), 0) == EOF)
335 quiterr("seek error in picwritecm");
336 for (i = 0; i < 3; i++)
337 for (j = 0; j < 256; j++)
338 putc(cm[i][j], outpic->fp);
339 outpic->nexty = 0;
340 if (outpic->bytes_line == 0)
341 outpic->pos.y[0] = ftell(outpic->fp);
342 else
343 outpic->pos.b = ftell(outpic->fp);
344 }
345
346
347 picreadcm(map) /* do gamma correction if requested */
348 colormap map;
349 {
350 register int i, val;
351
352 for (i = 0; i < 256; i++) {
353 val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0;
354 map[0][i] = map[1][i] = map[2][i] = val;
355 }
356 }