1 |
#ifndef lint |
2 |
static char SCCSid[] = "$SunId$ LBL"; |
3 |
#endif |
4 |
|
5 |
/* Copyright 1989 Regents of the University of California */ |
6 |
|
7 |
#ifndef lint |
8 |
static char SCCSid[] = "@(#)ra_pixar.c 1.2 9/12/89 LBL"; |
9 |
#endif |
10 |
|
11 |
/* ra_pixar.c */ |
12 |
/* |
13 |
* convert from RADIANCE image file to PIXAR image file. (or vice versa) |
14 |
* |
15 |
* David G. Jones July 1989 |
16 |
*/ |
17 |
/* External functions called: |
18 |
* |
19 |
* header.c: |
20 |
* getheader(fp,func); |
21 |
* printargs(argc,argv,fp); |
22 |
* color.c: |
23 |
* freadscan(scanline,len,fp); |
24 |
* fwritescan(scanline,len,fp); |
25 |
*/ |
26 |
|
27 |
#include <stdio.h> |
28 |
#include <math.h> |
29 |
|
30 |
/* PIXAR */ |
31 |
#include <picio.h> |
32 |
#include <pixeldef.h> |
33 |
#include <chad.h> |
34 |
|
35 |
/* RADIANCE (color.h conflicts with PIXAR includes) ugh!! */ |
36 |
#ifdef undef |
37 |
#include "color.h" |
38 |
#else |
39 |
#define XDECR 1 |
40 |
#define YDECR 2 |
41 |
#define YMAJOR 4 |
42 |
|
43 |
#define RED 0 |
44 |
#define GRN 1 |
45 |
#define BLU 2 |
46 |
#define EXP 3 |
47 |
#define colval(col,pri) ((col)[pri]) |
48 |
#define setcolor(col,r,g,b) \ |
49 |
((col)[RED]=(r),(col)[GRN]=(g),(col)[BLU]=(b)) |
50 |
typedef float COLOR[3]; /* red, green, blue */ |
51 |
#endif |
52 |
|
53 |
|
54 |
char *ProgramName; |
55 |
int global_argc; |
56 |
char **global_argv; |
57 |
|
58 |
/* RADIANCE global variables */ |
59 |
FILE *radiance_fp = NULL; |
60 |
double radiance_pixel(); |
61 |
|
62 |
usage() |
63 |
{ |
64 |
fprintf(stderr,"usage: %s radiance.pic pixar.pix\n",ProgramName); |
65 |
fprintf(stderr," or %s -r pixar.pix radiance.pic\n",ProgramName); |
66 |
exit(1); |
67 |
} |
68 |
|
69 |
|
70 |
main(argc,argv) |
71 |
int argc; |
72 |
char *argv[]; |
73 |
{ |
74 |
char *infile; |
75 |
char *outfile; |
76 |
int i; |
77 |
int reverse = 0; |
78 |
|
79 |
ProgramName=argv[0]; |
80 |
if (argc < 3) |
81 |
usage(); |
82 |
infile=argv[argc-2]; |
83 |
outfile=argv[argc-1]; |
84 |
for (i=1; i < argc-2 ; ++i) |
85 |
if (!strcmp(argv[i],"-r")) |
86 |
reverse=1; |
87 |
else |
88 |
usage(); |
89 |
|
90 |
if (reverse) |
91 |
{ |
92 |
global_argc=argc; |
93 |
global_argv=argv; |
94 |
pix_ra(infile,outfile); |
95 |
} |
96 |
else |
97 |
ra_pix(infile,outfile); |
98 |
} |
99 |
|
100 |
ra_pix(infile,outfile) |
101 |
char *infile; |
102 |
char *outfile; |
103 |
{ |
104 |
/* PIXAR variables */ |
105 |
RGBAPixelType *pixar_scanline; |
106 |
PFILE *out; |
107 |
/* RADIANCE variables */ |
108 |
COLOR *radiance_scanline; |
109 |
/* regular variables */ |
110 |
int width; |
111 |
int height; |
112 |
register int x; |
113 |
register int y; |
114 |
|
115 |
/* get width, height from radiance file */ |
116 |
radiance_getsize(infile,&width,&height); |
117 |
radiance_scanline=(COLOR *)malloc(width*sizeof(COLOR)); |
118 |
pixar_scanline=(RGBAPixelType *)malloc(width*sizeof(RGBAPixelType)); |
119 |
if (!radiance_scanline || !pixar_scanline) |
120 |
{ |
121 |
fprintf(stderr,"not enough memory?\n"); |
122 |
perror("malloc"); |
123 |
exit(1); |
124 |
} |
125 |
bzero(pixar_scanline,width*sizeof(RGBAPixelType)); |
126 |
|
127 |
PicSetForce(1); |
128 |
PicSetPsize(width,height); |
129 |
PicSetTsize(width,height); |
130 |
PicSetPformat(PF_RGB); |
131 |
PicSetPstorage(PS_12DUMP); |
132 |
PicSetPmatting(PM_NONE); |
133 |
PicSetOffset(0,0); |
134 |
PicSetLabel("converted from RADIANCE format by ra_pixar"); |
135 |
if (!(out=PicCreat(outfile,0666))) |
136 |
{ |
137 |
fprintf(stderr,"can't open PIXAR image file `%s'\n",outfile); |
138 |
perror("open"); |
139 |
exit(1); |
140 |
} |
141 |
|
142 |
picPreEncodeScanline(out,0); |
143 |
for (y=0; y < height ; ++y) |
144 |
{ |
145 |
radiance_readscanline(radiance_scanline,width); |
146 |
for (x=0; x < width ; ++x) |
147 |
{ |
148 |
SetRGBColor(&pixar_scanline[x], |
149 |
DBL2PXL(radiance_pixel(radiance_scanline[x],RED)), |
150 |
DBL2PXL(radiance_pixel(radiance_scanline[x],GRN)), |
151 |
DBL2PXL(radiance_pixel(radiance_scanline[x],BLU))); |
152 |
} |
153 |
picEncodeScanline(out,pixar_scanline); |
154 |
} |
155 |
picPostEncodeScanline(out); |
156 |
fclose(radiance_fp); |
157 |
PicClose(out); |
158 |
} |
159 |
|
160 |
|
161 |
radiance_getsize(filename,w,h) |
162 |
char *filename; |
163 |
int *w; |
164 |
int *h; |
165 |
{ |
166 |
if (!(radiance_fp=fopen(filename,"r"))) |
167 |
{ |
168 |
fprintf(stderr,"can't open RADIANCE image file `%s'\n",filename); |
169 |
perror("open"); |
170 |
exit(1); |
171 |
} |
172 |
getheader(radiance_fp,NULL); |
173 |
if (fgetresolu(w, h, radiance_fp) != (YMAJOR|YDECR)) |
174 |
{ |
175 |
fprintf(stderr,"bad RADIANCE format\n"); |
176 |
exit(1); |
177 |
} |
178 |
} |
179 |
|
180 |
|
181 |
radiance_readscanline(buf,x) |
182 |
COLOR *buf; |
183 |
int x; |
184 |
{ |
185 |
if (freadscan(buf,x,radiance_fp) < 0) |
186 |
{ |
187 |
fprintf(stderr,"read error?\n"); |
188 |
perror("fread"); |
189 |
exit(1); |
190 |
} |
191 |
} |
192 |
|
193 |
|
194 |
double radiance_pixel(pixel,i) |
195 |
COLOR pixel; |
196 |
int i; |
197 |
{ |
198 |
double value; |
199 |
|
200 |
value=colval(pixel,i); |
201 |
if (value < 0.0) |
202 |
return 0.0; |
203 |
else if (value> 1.0) |
204 |
return 1.0; |
205 |
return value; |
206 |
} |
207 |
|
208 |
pix_ra(infile,outfile) |
209 |
char *infile; |
210 |
char *outfile; |
211 |
{ |
212 |
/* PIXAR variables */ |
213 |
RGBAPixelType *pixar_scanline; |
214 |
PFILE *in; |
215 |
/* RADIANCE variables */ |
216 |
COLOR *radiance_scanline; |
217 |
FILE *out; |
218 |
/* regular variables */ |
219 |
int width; |
220 |
int height; |
221 |
register int x; |
222 |
register int y; |
223 |
|
224 |
if (!(in=PicOpen(infile,"r"))) |
225 |
{ |
226 |
fprintf(stderr,"can't open PIXAR image file `%s'\n",infile); |
227 |
perror("open"); |
228 |
exit(1); |
229 |
} |
230 |
if (!(out=fopen(outfile,"w"))) |
231 |
{ |
232 |
fprintf(stderr,"can't open RADIANCE image file `%s'\n",outfile); |
233 |
perror("open"); |
234 |
exit(1); |
235 |
} |
236 |
|
237 |
/* get width, height from PIXAR file */ |
238 |
width=in->Pwidth; |
239 |
height=in->Pheight; |
240 |
|
241 |
/* allocate scan line space */ |
242 |
radiance_scanline=(COLOR *)malloc(width*sizeof(COLOR)); |
243 |
pixar_scanline=(RGBAPixelType *)malloc(width*sizeof(RGBAPixelType)); |
244 |
if (!radiance_scanline || !pixar_scanline) |
245 |
{ |
246 |
fprintf(stderr,"not enough memory?\n"); |
247 |
perror("malloc"); |
248 |
exit(1); |
249 |
} |
250 |
|
251 |
/* write out the RADIANCE header */ |
252 |
radiance_writeheader(out,width,height); |
253 |
|
254 |
picPreDecodeScanline(in,0); |
255 |
for (y=0; y < height ; ++y) |
256 |
{ |
257 |
picDecodeScanline(in,pixar_scanline); |
258 |
for (x=0; x < width ; ++x) |
259 |
{ |
260 |
setcolor(radiance_scanline[x], |
261 |
PXL2DBL(pixar_scanline[x].Red), |
262 |
PXL2DBL(pixar_scanline[x].Green), |
263 |
PXL2DBL(pixar_scanline[x].Blue)); |
264 |
} |
265 |
radiance_writescanline(out,radiance_scanline,width); |
266 |
} |
267 |
picPostDecodeScanline(in); |
268 |
PicClose(in); |
269 |
fclose(out); |
270 |
} |
271 |
|
272 |
|
273 |
radiance_writeheader(fp,x,y) |
274 |
FILE *fp; |
275 |
int x; |
276 |
int y; |
277 |
{ |
278 |
printargs(global_argc,global_argv,fp); |
279 |
fputc('\n',fp); |
280 |
fputresolu(YMAJOR|YDECR, x, y, fp); |
281 |
fflush(fp); |
282 |
} |
283 |
|
284 |
|
285 |
radiance_writescanline(fp,buf,x) |
286 |
FILE *fp; |
287 |
COLOR *buf; |
288 |
int x; |
289 |
{ |
290 |
if (fwritescan(buf,x,fp) < 0) |
291 |
{ |
292 |
fprintf(stderr,"write error?\n"); |
293 |
perror("fwrite"); |
294 |
exit(1); |
295 |
} |
296 |
} |