ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.2
Committed: Mon Jul 13 09:53:14 1992 UTC (31 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +11 -18 lines
Log Message:
changed and fixed image transformation

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