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

Comparing ray/src/px/ra_ps.c (file contents):
Revision 2.10 by greg, Wed Sep 27 13:29:52 1995 UTC vs.
Revision 2.17 by gregl, Thu Sep 18 15:16:23 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1997 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include  <stdio.h>
12 + #include  <math.h>
13   #ifdef MSDOS
14   #include  <fcntl.h>
15   #endif
16   #include  "color.h"
17  
18 + #define CODE6GAM        1.47                    /* gamma for 6-bit codes */
19 + #define DEFDGAM         1.0                     /* default device gamma */
20 +
21 + #define GRY             -1                      /* artificial index for grey */
22 +
23   #define HMARGIN         (.5*72)                 /* horizontal margin */
24   #define VMARGIN         (.5*72)                 /* vertical margin */
25   #define PWIDTH          (8.5*72-2*HMARGIN)      /* width of device */
# Line 30 | Line 36 | char  code[] =                 /* 6-bit code lookup table */
36   int  wrongformat = 0;                   /* input in wrong format? */
37   double  pixaspect = 1.0;                /* pixel aspect ratio */
38  
39 + double  devgam = DEFDGAM;               /* device gamma response */
40 + int  docolor = 0;                       /* produce color image? */
41   int  bradj = 0;                         /* brightness adjustment */
42   int  ncopies = 1;                       /* number of copies */
43  
44 + extern int      Aputprim(), Bputprim(), Cputprim();
45 +
46 + int  (*putprim)() = Aputprim;           /* function for writing scanline */
47 +
48   char  *progname;
49  
50   int  xmax, ymax;
# Line 64 | Line 76 | char  *argv[];
76          for (i = 1; i < argc; i++)
77                  if (argv[i][0] == '-')
78                          switch (argv[i][1]) {
79 +                        case 'b':               /* produce b&w PostScript */
80 +                                docolor = 0;
81 +                                break;
82 +                        case 'c':               /* produce color PostScript */
83 +                                docolor = 1;
84 +                                break;
85 +                        case 'A':               /* standard ASCII encoding */
86 +                                putprim = Aputprim;
87 +                                break;
88 +                        case 'B':               /* standard binary encoding */
89 +                                putprim = Bputprim;
90 +                                break;
91 +                        case 'C':               /* compressed ASCII encoding */
92 +                                putprim = Cputprim;
93 +                                break;
94 +                        case 'g':
95 +                                devgam = atof(argv[++i]);
96 +                                break;
97                          case 'e':               /* exposure adjustment */
98                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
99                                          goto userr;
# Line 97 | Line 127 | char  *argv[];
127          getheader(stdin, headline, NULL);
128          if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
129                  quiterr("bad picture format");
130 +                                /* gamma compression */
131 +        if (putprim == Cputprim)
132 +                setcolrgam(CODE6GAM/devgam);
133 +        else if (devgam != 1.)
134 +                setcolrgam(1./devgam);
135                                  /* write header */
136          PSheader(i <= argc-1 ? argv[i] : "<stdin>");
137                                  /* convert file */
# Line 105 | Line 140 | char  *argv[];
140          PStrailer();
141          exit(0);
142   userr:
143 <        fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname);
143 >        fprintf(stderr, "Usage: %s [-b|c][-A|B|C][-e +/-stops][-g gamma] [input [output]]\n",
144 >                        progname);
145          exit(1);
146   }
147  
# Line 124 | Line 160 | char  *err;
160   PSheader(name)                  /* print PostScript header */
161   char  *name;
162   {
163 +        char  *rstr;
164          int  landscape = 0;
165          double  pwidth, pheight;
166          double  iwidth, iheight;
167                                          /* EPS comments */
168 <        puts("%%!PS-Adobe-2.0 EPSF-2.0");
168 >        puts("%!PS-Adobe-2.0 EPSF-2.0");
169          printf("%%%%Title: %s\n", name);
170          printf("%%%%Creator: %s = %s\n", progname, SCCSid);
171          printf("%%%%Pages: %d\n", ncopies);
# Line 163 | Line 200 | char  *name;
200                                  HMARGIN+(pwidth-iwidth)*.5+iwidth,
201                                  VMARGIN+(pheight-iheight)*.5+iheight);
202          puts("%%EndComments");
203 <        puts("save");
204 <        puts("64 dict begin");
203 >        puts("gsave save");
204 >        puts("17 dict begin");
205                                          /* define image reader */
206 <        PSprocdef("read6bitRLE");
206 >        if (docolor) {
207 >                printf("/redline %d string def\n", xmax);
208 >                printf("/grnline %d string def\n", xmax);
209 >                printf("/bluline %d string def\n", xmax);
210 >        } else
211 >                printf("/gryline %d string def\n", xmax);
212 >                                        /* use compressed encoding? */
213 >        if (putprim == Cputprim)
214 >                PSprocdef("read6bitRLE");
215                                          /* set up transformation matrix */
216          printf("%f %f translate\n", HMARGIN, VMARGIN);
217          if (pwidth == PHEIGHT) {
218 <                printf("0 %f translate\n", PHEIGHT);
219 <                puts("-90 rotate");
218 >                printf("%f 0 translate\n", PWIDTH);
219 >                puts("90 rotate");
220          }
221          printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
222          printf("%f %f scale\n", iwidth, iheight);
223 <        puts("%%%%EndProlog");
223 >        puts("%%EndProlog");
224                                          /* start image procedure */
225 <        printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bitRLE} image\n", xmax, ymax,
226 <                        xmax, -ymax, ymax);
225 >        printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax);
226 >        if (putprim == Cputprim) {
227 >                if (docolor) {
228 >                        puts("{redline read6bitRLE}");
229 >                        puts("{grnline read6bitRLE}");
230 >                        puts("{bluline read6bitRLE}");
231 >                        puts("true 3 colorimage");
232 >                } else
233 >                        puts("{gryline read6bitRLE} image");
234 >        } else {
235 >                rstr = putprim==Aputprim ? "readhexstring" : "readstring";
236 >                if (docolor) {
237 >                        printf("{currentfile redline %s pop}\n", rstr);
238 >                        printf("{currentfile grnline %s pop}\n", rstr);
239 >                        printf("{currentfile bluline %s pop}\n", rstr);
240 >                        puts("true 3 colorimage");
241 >                } else
242 >                        printf("{currentfile gryline %s pop} image\n", rstr);
243 >        }
244   }
245  
246  
# Line 189 | Line 251 | PStrailer()                    /* print PostScript trailer */
251                  printf("/#copies %d def\n", ncopies);
252          puts("showpage");
253          puts("end");
254 <        puts("restore");
254 >        puts("restore grestore");
255          puts("%%EOF");
256   }
257  
# Line 203 | Line 265 | char  *nam;
265          for (i = 0; i < 128; i++)       /* clear */
266                  itab[i] = -1;
267          for (i = 1; i < 63; i++)        /* assign greys */
268 <                itab[code[i]] = i<<2 | 2;
268 >                itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM);
269          itab[code[0]] = 0;              /* black is black */
270          itab[code[63]] = 255;           /* and white is white */
271          printf("/codetab [");
# Line 213 | Line 275 | char  *nam;
275                  printf(" %3d", itab[i]);
276          }
277          printf("\n] def\n");
216        printf("/scanline %d string def\n", xmax);
278          printf("/nrept 0 def\n");
279 <        printf("/readbyte { currentfile read not {stop} if } def\n");
280 <        printf("/decode { codetab exch get } def\n");
281 <        printf("/%s {\n", nam);
279 >        printf("/readbyte { currentfile read not {stop} if } bind def\n");
280 >        printf("/decode { codetab exch get } bind def\n");
281 >        printf("/%s {\t%% scanbuffer\n", nam);
282 >        printf("\t/scanline exch def\n");
283          printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
284          printf("\t\tnrept 0 le\n");
285          printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR);
# Line 237 | Line 299 | char  *nam;
299   ra2ps()                         /* convert Radiance scanlines to 6-bit */
300   {
301          register COLR   *scanin;
240        register int    c;
241        int     lastc, cnt;
242        register int    x;
302          int     y;
303                                                  /* allocate scanline */
304          scanin = (COLR *)malloc(xmax*sizeof(COLR));
# Line 249 | Line 308 | ra2ps()                                /* convert Radiance scanlines to 6-bit */
308          for (y = ymax-1; y >= 0; y--) {
309                  if (freadcolrs(scanin, xmax, stdin) < 0)
310                          quiterr("error reading Radiance picture");
311 <                normcolrs(scanin, xmax, bradj); /* normalize */
312 <                lastc = -1; cnt = 0;
313 <                for (x = 0; x < xmax; x++) {
314 <                        c = normbright(scanin[x]) + 2;
315 <                        if (c > 255) c = 255;
316 <                        c = code[c>>2];
317 <                        if (c == lastc && cnt < MAXRUN)
318 <                                cnt++;
319 <                        else {
320 <                                putrle(cnt, lastc);
321 <                                lastc = c;
322 <                                cnt = 1;
264 <                        }
265 <                }
266 <                putrle(cnt, lastc);
311 >                if (putprim == Cputprim || devgam != 1.) {
312 >                        if (bradj)                      /* adjust exposure */
313 >                                shiftcolrs(scanin, xmax, bradj);
314 >                        colrs_gambs(scanin, xmax);      /* gamma compression */
315 >                } else
316 >                        normcolrs(scanin, xmax, bradj);
317 >                if (docolor) {
318 >                        (*putprim)(scanin, RED);
319 >                        (*putprim)(scanin, GRN);
320 >                        (*putprim)(scanin, BLU);
321 >                } else
322 >                        (*putprim)(scanin, GRY);
323                  if (ferror(stdout))
324                          quiterr("error writing PostScript file");
325          }
326          putchar('\n');
327                                                  /* free scanline */
328          free((char *)scanin);
329 + }
330 +
331 +
332 + int
333 + Aputprim(scn, pri)              /* put out hex ASCII primary from scanline */
334 + COLR    *scn;
335 + int     pri;
336 + {
337 +        static char     hexdigit[] = "0123456789ABCDEF";
338 +        static int      col = 0;
339 +        register int    x, c;
340 +
341 +        for (x = 0; x < xmax; x++) {
342 +                if (pri == GRY)
343 +                        c = normbright(scn[x]);
344 +                else
345 +                        c = scn[x][pri];
346 +                if (c > 255) c = 255;
347 +                putchar(hexdigit[c>>4]);
348 +                putchar(hexdigit[c&0xf]);
349 +                if ((col += 2) >= 72) {
350 +                        putchar('\n');
351 +                        col = 0;
352 +                }
353 +        }
354 + }
355 +
356 +
357 + int
358 + Bputprim(scn, pri)              /* put out binary primary from scanline */
359 + COLR    *scn;
360 + int     pri;
361 + {
362 +        register int    x, c;
363 +
364 +        for (x = 0; x < xmax; x++) {
365 +                if (pri == GRY)
366 +                        c = normbright(scn[x]);
367 +                else
368 +                        c = scn[x][pri];
369 +                if (c > 255) c = 255;
370 +                putchar(c);
371 +        }
372 + }
373 +
374 +
375 + int
376 + Cputprim(scn, pri)              /* put out compressed primary from scanline */
377 + COLR    *scn;
378 + int     pri;
379 + {
380 +        register int    c;
381 +        register int    x;
382 +        int     lastc, cnt;
383 +
384 +        lastc = -1; cnt = 0;
385 +        for (x = 0; x < xmax; x++) {
386 +                if (pri == GRY)
387 +                        c = normbright(scn[x]) + 2;
388 +                else
389 +                        c = scn[x][pri] + 2;
390 +                if (c > 255) c = 255;
391 +                c = code[c>>2];
392 +                if (c == lastc && cnt < MAXRUN)
393 +                        cnt++;
394 +                else {
395 +                        putrle(cnt, lastc);
396 +                        lastc = c;
397 +                        cnt = 1;
398 +                }
399 +        }
400 +        putrle(cnt, lastc);
401   }
402  
403  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines