ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pict.c
(Generate patch)

Comparing ray/src/px/ra_pict.c (file contents):
Revision 1.1 by greg, Wed Oct 23 09:26:35 1991 UTC vs.
Revision 2.3 by greg, Wed Nov 27 09:14:03 1991 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ AU";
2 > static char SCCSid[] = "$SunId$ Auckuni";
3   #endif
4 +
5   /*
6 <    rad2pict - Convert an Radiance image to APPLE pict format.
7 <        
8 <    School of Architecture, Auckland University, Private Bag
9 <    Auckland, New Zealand
10 < */
6 > *      rad2pict -
7 > *              Convert an Radiance image to APPLE pict format.
8 > *
9 > *                      Orginally Iris to PICT by       Paul Haeberli - 1990
10 > *                      Hacked into Rad to PICT by Russell Street 1990
11 > *
12 > *      History:
13 > *          V 1                 -- Does basic conversion
14 > *          V 1.1 (2/11/91)     -- Added options for Gamma
15 > *                              -- verbose option
16 > *                              -- man page
17 > *                              -- better about allocating buffers
18 > *          V 1.2 (19/11/91)    -- Revised to handle opening "The Radiance Way"
19 > *                              -- Added exposure level adjustment
20 > */
21 >
22   #include <stdio.h>
23 < #include <stdlib.h>
23 >
24   #include "pict.h"
25   #include "color.h"
26 + #include "resolu.h"
27  
28 < char cbuf[8192*5];
29 < char pbuf[8192];
17 < int outbytes;
18 < FILE *outf, *inf;
19 < char **gargv;
28 > extern double   atof();
29 > extern char     *malloc();
30  
31 < putpic(int, int);
31 > int     outbytes;                   /* This had better be 32 bits! */
32 > char    *progname;
33 > int     verbose = 0;
34 > float   gamma = 2.0;
35 > int     bradj = 0;
36  
37 +        /* First some utility routines */
38 +
39   putrect(xorg,yorg,xsize,ysize)
40   int xorg, yorg, xsize, ysize;
41   {
# Line 55 | Line 71 | short s;
71   }
72  
73   putbyte(b)
74 < unsigned char b;
74 > int b;
75   {
76 <    char c[1];
77 <
62 <    c[0] = b;
63 <    if(!fwrite(c,1,1,outf)) {
64 <        fprintf(stderr,"%s: error on write\n", gargv[0]);
76 >    if (putc(b,stdout) == EOF && ferror(stdout)) {
77 >        fprintf(stderr,"%s: error on write\n", progname);
78          exit(1);
79      }
80      outbytes++;
# Line 71 | Line 84 | putbytes(buf,n)
84   unsigned char *buf;
85   int n;
86   {
87 <    if(!fwrite(buf,n,1,outf)) {
88 <        fprintf(stderr,"topict: error on write\n");
87 >    if(!fwrite(buf,n,1,stdout)) {
88 >        fprintf(stderr,"%s: error on write\n", progname);
89          exit(1);
90      }
91      outbytes+=n;
# Line 86 | Line 99 | char **argv;
99      int i, picsize;
100      int ssizepos, lsizepos;
101  
102 <    gargv = argv;
102 >    progname = argv[0];
103  
104 <    if( argc<3 ) {
105 <        fprintf(stderr, "Usage: %s inimage out.pict\n", gargv[0]);
106 <        exit(1);
107 <    }
104 >    for (i = 1; i < argc ; i++)
105 >        if (argv[i][0] ==  '-')
106 >            switch (argv[i][1]) {
107 >                case 'g':       gamma = atof(argv[++i]);
108 >                                break;
109  
110 <    if( (inf=fopen(gargv[1],"rb")) == NULL ) {
111 <        fprintf(stderr,"%s: can't open input file %s\n",gargv[0], gargv[1]);
110 >                case 'e':       if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
111 >                                   usage();
112 >                                else
113 >                                    bradj = atoi(argv[++i]);
114 >                                break;
115 >
116 >                case 'v':       ;
117 >                                verbose = 1;
118 >                                break;
119 >
120 >                case 'r':       fprintf(stderr, "Sorry. Get a Macintosh :-)\n");
121 >                                exit(1);
122 >
123 >                case '-':       i++;
124 >                                goto outofparse;
125 >                                break;              /* NOTREACHED */
126 >
127 >                otherwise:      usage();
128 >                                break;
129 >                 }
130 >        else
131 >            break;
132 >
133 > outofparse:
134 >
135 >    if (i < argc - 2)
136 >        usage();
137 >
138 >    if (i <= argc - 1 && freopen(argv[i], "r", stdin) == NULL) {
139 >        fprintf(stderr, "%s: can not open input \"%s\"\n",
140 >            progname, argv[i]);
141          exit(1);
142      }
143  
144 <    if( (outf=fopen(gargv[2],"wb")) == NULL ) {
145 <        fprintf(stderr,"%s: can't open output file %s\n", gargv[0], gargv[1]);
144 >    if (i <= argc - 2 && freopen(argv[i+1], "w", stdout) == NULL) {
145 >        fprintf(stderr, "%s: can not open input \"%s\"\n",
146 >            progname, argv[i+1]);
147          exit(1);
148      }
149 +        
150 + #ifdef DEBUG
151 +        fprintf(stderr, "Input file: %s\n", i <= argc - 1 ? argv[i] : "stdin");
152 +        fprintf(stderr, "Outut file: %s\n", i <= argc - 2 ? argv[i+1] : "stdout" );
153 +        fprintf(stderr, "Gamma: %f\n", gamma);
154 +        fprintf(stderr, "Brightness adjust: %d\n", bradj);
155 +        fprintf(stderr, "Verbose: %s\n", verbose ? "on" : "off");
156 + #endif
157  
158 <    if (checkheader(inf, COLRFMT, NULL) < 0 ||
159 <            fgetresolu(&xsize, &ysize, inf) != (YMAJOR|YDECR)) {
160 <        fprintf(stderr, "%s: not a radiance picture\n", argv[1]);
158 >
159 >             /* OK. Now we read the size of the Radiance picture */
160 >    if (checkheader(stdin, COLRFMT, NULL) < 0 ||
161 >            fgetresolu(&xsize, &ysize, stdin) < 0 /* != (YMAJOR|YDECR) */ ) {
162 >        fprintf(stderr, "%s: not a radiance picture\n", progname);
163          exit(1);
164 <    }
164 >        }
165  
166 <    setcolrgam(2.0);
166 >            /* Set the gamma correction */
167  
168 +    setcolrgam(gamma);
169 +
170      for(i=0; i<HEADER_SIZE; i++)
171          putbyte(0);
172 +
173      ssizepos = outbytes;
174      putashort(0);               /* low 16 bits of file size less HEADER_SIZE */
175      putrect(0,0,xsize,ysize);   /* bounding box of picture */
# Line 129 | Line 186 | char **argv;
186      putashort(10);
187      putrect(0,0,xsize,ysize);
188  
189 <    putpict(xsize, ysize);
189 >    if (verbose)
190 >        fprintf(stderr, "%s: The picture is %d by %d, with a gamma of %f\n",
191 >            progname, xsize, ysize, gamma);
192  
193 +
194 +    putpict(xsize, ysize);      /* Here is where all the work is done */
195 +
196      putashort(PICT_EndOfPicture); /* end of pict */
197  
198      picsize = outbytes-HEADER_SIZE;
199 <    fseek(outf,ssizepos,0);
199 >    fseek(stdout,ssizepos,0);
200      putashort(picsize&0xffff);
201 <    fseek(outf,lsizepos,0);
201 >    fseek(stdout,lsizepos,0);
202      putalong(picsize);
203  
204 <    fclose(outf);
204 >    fclose(stdout);
205 >    fclose(stdin);
206 >    
207      exit(0);
208 +    return 0;       /* lint fodder */
209   }
210  
211   putpict(xsize, ysize)
212   int xsize;
213   int ysize;
214   {
215 <    int y;
216 <    int nbytes, rowbytes;
215 >    int     y;
216 >    int     nbytes, rowbytes;
217 >    char    *cbuf, *pbuf;
218  
219 +    cbuf = malloc(4 * xsize);
220 +
221 +    if (cbuf == NULL) {
222 +        fprintf(stderr, "%s: not enough memory\n", progname);
223 +        exit(1);
224 +        }
225 +
226 +    pbuf = malloc(4 * xsize);
227 +
228 +    if (pbuf == NULL) {
229 +        fprintf(stderr, "%s: not enough memory\n", progname);
230 +        exit(1);
231 +        }
232 +
233      putashort(PICT_Pack32BitsRect); /* 32 bit rgb */
234      rowbytes = 4*xsize;
235      putalong(0x000000ff);               /* base address */
236  
237 +
238      if(rowbytes&1)
239          rowbytes++;
240      putashort(rowbytes|0x8000); /* rowbytes */
# Line 169 | Line 250 | int ysize;
250      putashort(32);      /* pixelsize */
251      putashort(3);       /* cmpcount */
252  
253 +
254      putashort(8);       /* cmpsize */
255      putalong(0);        /* planebytes */
256      putalong(0);        /* pmtable */
257      putalong(0);        /* pmreserved */
258  
259 +
260      putrect(0,0,xsize,ysize);   /* scr rect */
261      putrect(0,0,xsize,ysize);   /* dest rect */
262  
263 +
264      putashort(0x40);    /* transfer mode */
265 +
266      for(y=0; y<ysize; y++) {
267 <        getrow(inf, cbuf, xsize);
267 >        getrow(stdin, cbuf, xsize);
268  
269          nbytes = packbits(cbuf,pbuf,24*xsize);
270          if(rowbytes>250)
# Line 187 | Line 272 | int ysize;
272          else
273              putbyte(nbytes);
274          putbytes(pbuf,nbytes);
275 <    }
275 >        }
276  
277      if(outbytes&1)
278          putbyte(0);
279 +
280 +    free(cbuf);
281 +    free(pbuf);
282   }
283  
284 < int getrow(FILE *in, char *mybuff, int xsize)
284 > int getrow(in, cbuf, xsize)
285 > FILE *in;
286 > char *cbuf;
287 > int xsize;
288   {
289 <    COLOR    color;
290 <    COLR    *scanin = (COLR*) malloc(xsize * sizeof(COLR));
289 >    extern char *tempbuffer();          /* defined in color.c */
290 >    COLR    *scanin = NULL;
291      int     x;
292  
293 <    if (scanin == NULL) {
294 <        printf("scanin null");
293 >    if ((scanin = (COLR *)tempbuffer(xsize*sizeof(COLR))) == NULL) {
294 >        fprintf(stderr, "%s: not enough memory\n", progname);
295 >        exit(1);
296      }
297  
206
298      if (freadcolrs(scanin, xsize, in) < 0) {
299 <        fprintf(stderr, " read error\n");
299 >        fprintf(stderr, "%s: read error\n", progname);
300          exit(1);
301 <    }
301 >        }
302  
303 <    colrs_gambs(scanin, xsize);
303 >    if (bradj)      /* Adjust exposure level */
304 >        shiftcolrs(scanin, xsize, bradj);
305 >
306 >
307 >    colrs_gambs(scanin, xsize);     /* Gamma correct it */
308      
309      for (x = 0; x < xsize; x++) {
310 <        colr_color(color, scanin[x]);
311 <        cbuf[xsize * 0 + x] = color[RED] * 255;
312 <        cbuf[xsize * 1 + x] = color[GRN] * 255;
313 <        cbuf[xsize * 2 + x] = color[BLU] * 255;
314 <    }
220 <    free(scanin);
310 >        cbuf[x] = scanin[x][RED];
311 >        cbuf[xsize + x] = scanin[x][GRN];
312 >        cbuf[2*xsize + x] = scanin[x][BLU];
313 >        }
314 >
315   }
316  
317 +
318   packbits(ibits,pbits,nbits)
319   unsigned char *ibits, *pbits;
320   int nbits;
321   {
322 <    int bytes;
322 >    int bytes;                      /* UNUSED */
323      unsigned char *sptr;
324      unsigned char *ibitsend;
325      unsigned char *optr = pbits;
# Line 232 | Line 327 | int nbits;
327  
328      nbytes = ((nbits-1)/8)+1;
329      ibitsend = ibits+nbytes;
330 <    while (ibits<ibitsend) {
330 >    while(ibits<ibitsend) {
331          sptr = ibits;
332          ibits += 2;
333          while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0])))
# Line 263 | Line 358 | int nbits;
358          }
359      }
360      return optr-pbits;
361 + }
362 +
363 + usage()
364 + {
365 +    fprintf(stderr, "Usage: %s [-v] [-g gamma] [infile [outfile]]\n",
366 +        progname);
367 +    exit(2);
368   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines