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