ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.5
Committed: Fri Aug 14 14:04:09 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +5 -4 lines
Log Message:
set black to black and white to white

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1992 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Radiance picture to PostScript file translator -- one way!
9     */
10    
11     #include <stdio.h>
12     #include "color.h"
13     #include "random.h"
14    
15     #define HMARGIN (.5*72) /* horizontal margin */
16     #define VMARGIN (.5*72) /* vertical margin */
17     #define PWIDTH (8.5*72-2*HMARGIN) /* width of device */
18     #define PHEIGHT (11*72-2*VMARGIN) /* height of device */
19    
20     char code[] = /* 6-bit code lookup table */
21     "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
22    
23     int wrongformat = 0; /* input in wrong format? */
24     double pixaspect = 1.0; /* pixel aspect ratio */
25    
26     int bradj = 0; /* brightness adjustment */
27 greg 2.4 int ncopies = 1; /* number of copies */
28 greg 2.1
29     char *progname;
30    
31     int xmax, ymax;
32    
33    
34     headline(s) /* check header line */
35     char *s;
36     {
37     char fmt[32];
38    
39     if (isformat(s)) {
40     formatval(fmt, s);
41     wrongformat = strcmp(fmt, COLRFMT);
42     } else if (isaspect(s))
43     pixaspect *= aspectval(s);
44     }
45    
46    
47     main(argc, argv)
48     int argc;
49     char *argv[];
50     {
51     int i;
52    
53     progname = argv[0];
54    
55     for (i = 1; i < argc; i++)
56     if (argv[i][0] == '-')
57     switch (argv[i][1]) {
58     case 'e': /* exposure adjustment */
59     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
60     goto userr;
61     bradj = atoi(argv[++i]);
62     break;
63 greg 2.4 case 'n': /* number of copies */
64     ncopies = atoi(argv[++i]);
65     break;
66 greg 2.1 default:
67     goto userr;
68     }
69     else
70     break;
71    
72     if (i < argc-2)
73     goto userr;
74     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
75     fprintf(stderr, "%s: can't open input \"%s\"\n",
76     progname, argv[i]);
77     exit(1);
78     }
79     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
80     fprintf(stderr, "can't open output \"%s\"\n",
81     progname, argv[i+1]);
82     exit(1);
83     }
84     /* get our header */
85     getheader(stdin, headline, NULL);
86     if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
87     quiterr("bad picture format");
88     /* write header */
89     PSheader(i <= argc-1 ? argv[i] : "<stdin>");
90     /* convert file */
91     ra2ps();
92     /* write trailer */
93     PStrailer();
94     exit(0);
95     userr:
96     fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname);
97     exit(1);
98     }
99    
100    
101     quiterr(err) /* print message and exit */
102     char *err;
103     {
104     if (err != NULL) {
105     fprintf(stderr, "%s: %s\n", progname, err);
106     exit(1);
107     }
108     exit(0);
109     }
110    
111    
112     PSheader(name) /* print PostScript header */
113     char *name;
114     {
115     int landscape = 0;
116     double pwidth, pheight;
117 greg 2.2 double iwidth, iheight;
118 greg 2.1
119     printf("%%!\n");
120     printf("%%%%Title: %s\n", name);
121     printf("%%%%Creator: %s\n", progname);
122 greg 2.4 printf("%%%%Pages: %d\n", ncopies);
123 greg 2.1 if (landscape = xmax > pixaspect*ymax)
124     printf("%%%%Landscape\n");
125     else
126     printf("%%%%Portrait\n");
127     printf("%%%%EndComments\n");
128     printf("64 dict begin\n");
129 greg 2.3 /* define image reader */
130     PSprocdef("read6bit");
131 greg 2.1 /* set up transformation matrix */
132 greg 2.2 printf("%f %f translate\n", HMARGIN, VMARGIN);
133 greg 2.1 if (PWIDTH > PHEIGHT ^ landscape) {
134     printf("0 %f translate\n", PHEIGHT);
135 greg 2.2 printf("-90 rotate\n");
136 greg 2.1 pwidth = PHEIGHT;
137     pheight = PWIDTH;
138     } else {
139     pwidth = PWIDTH;
140     pheight = PHEIGHT;
141     }
142     if (pheight/pwidth > pixaspect*ymax/xmax) {
143 greg 2.2 iwidth = pwidth;
144     iheight = pwidth*pixaspect*ymax/xmax;
145 greg 2.1 } else {
146 greg 2.2 iheight = pheight;
147     iwidth = pheight*xmax/(pixaspect*ymax);
148 greg 2.1 }
149 greg 2.2 printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
150     printf("%f %f scale\n", iwidth, iheight);
151 greg 2.1 printf("%%%%EndProlog\n");
152 greg 2.3 /* start image procedure */
153 greg 2.2 printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax,
154     xmax, -ymax, ymax);
155 greg 2.1 }
156    
157    
158     PStrailer() /* print PostScript trailer */
159     {
160     puts("%%Trailer");
161 greg 2.4 if (ncopies > 1)
162     printf("/#copies %d def\n", ncopies);
163     puts("showpage");
164 greg 2.1 puts("end");
165     puts("%%EOF");
166     }
167    
168    
169     PSprocdef(nam) /* define PS procedure to read image */
170     char *nam;
171     {
172     short itab[128];
173     register int i;
174 greg 2.5 /* assign code values */
175     for (i = 0; i < 128; i++) /* clear */
176 greg 2.1 itab[i] = -1;
177 greg 2.5 for (i = 1; i < 63; i++) /* assign greys */
178 greg 2.1 itab[code[i]] = i<<2 | 2;
179 greg 2.5 itab[code[0]] = 0; /* black is black */
180     itab[code[63]] = 255; /* and white is white */
181 greg 2.1 printf("/decode [");
182     for (i = 0; i < 128; i++) {
183     if (!(i & 0xf))
184     putchar('\n');
185     printf(" %3d", itab[i]);
186     }
187     printf("\n] def\n");
188     printf("/scanline %d string def\n", xmax);
189     printf("/%s {\n", nam);
190     printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
191     printf("\t\t{ decode currentfile read not {stop} if get\n");
192     printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n");
193     printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n");
194     }
195    
196    
197     ra2ps() /* convert Radiance scanlines to 6-bit */
198     {
199     COLR *scanin;
200     register int col = 0;
201     register int c;
202     register int x;
203     int y;
204     /* allocate scanline */
205     scanin = (COLR *)malloc(xmax*sizeof(COLR));
206     if (scanin == NULL)
207     quiterr("out of memory in ra2ps");
208     /* convert image */
209     for (y = ymax-1; y >= 0; y--) {
210     if (freadcolrs(scanin, xmax, stdin) < 0)
211     quiterr("error reading Radiance picture");
212     normcolrs(scanin, xmax, bradj); /* normalize */
213     for (x = 0; x < xmax; x++) {
214     if (!(col++ & 0x3f))
215     putchar('\n');
216     c = normbright(scanin[x]) + (random()&3);
217     if (c > 255) c = 255;
218     putchar(code[c>>2]);
219     }
220     if (ferror(stdout))
221     quiterr("error writing PostScript file");
222     }
223     putchar('\n');
224     /* free scanline */
225     free((char *)scanin);
226     }