ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pixar.c
Revision: 1.1
Committed: Mon Jul 24 08:42:56 1989 UTC (34 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 1.1 /* Copyright 1989 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    
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     bzero(pixar_scanline,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);
165     if (fscanf(radiance_fp,"-Y %d +X %d\n",h,w) != 2)
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     fprintf(fp,"-Y %d +X %d\n",y,x);
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     }