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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines