ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pixar.c
Revision: 2.4
Committed: Fri Jan 2 10:25:13 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 2.3: +2 -2 lines
Log Message:
Fixed missing third argument in calls to getheader().

File Contents

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