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