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.8 by greg, Thu Feb 10 15:40:12 1994 UTC vs.
Revision 2.12 by greg, Thu Sep 28 16:34:30 1995 UTC

# Line 13 | Line 13 | static char SCCSid[] = "$SunId$ LBL";
13   #include  <fcntl.h>
14   #endif
15   #include  "color.h"
16 #include  "random.h"
16  
17 + #define GRY             -1                      /* artificial index for grey */
18 +
19   #define HMARGIN         (.5*72)                 /* horizontal margin */
20   #define VMARGIN         (.5*72)                 /* vertical margin */
21   #define PWIDTH          (8.5*72-2*HMARGIN)      /* width of device */
22   #define PHEIGHT         (11*72-2*VMARGIN)       /* height of device */
23  
24 + #define RUNCHR          '*'                     /* character to start rle */
25 + #define MINRUN          4                       /* minimum run-length */
26 + #define RSTRT           '!'                     /* character for MINRUN */
27 + #define MAXRUN          (MINRUN+'~'-RSTRT)      /* maximum run-length */
28 +
29   char  code[] =                  /* 6-bit code lookup table */
30          "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
31  
32   int  wrongformat = 0;                   /* input in wrong format? */
33   double  pixaspect = 1.0;                /* pixel aspect ratio */
34  
35 + int  docolor = 0;                       /* produce color image? */
36   int  bradj = 0;                         /* brightness adjustment */
37   int  ncopies = 1;                       /* number of copies */
38  
# Line 60 | Line 67 | char  *argv[];
67          for (i = 1; i < argc; i++)
68                  if (argv[i][0] == '-')
69                          switch (argv[i][1]) {
70 +                        case 'c':               /* produce color PostScript */
71 +                                docolor++;
72 +                                break;
73                          case 'e':               /* exposure adjustment */
74                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
75                                          goto userr;
# Line 101 | Line 111 | char  *argv[];
111          PStrailer();
112          exit(0);
113   userr:
114 <        fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname);
114 >        fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n",
115 >                        progname);
116          exit(1);
117   }
118  
# Line 123 | Line 134 | char  *name;
134          int  landscape = 0;
135          double  pwidth, pheight;
136          double  iwidth, iheight;
137 <
138 <        printf("%%!\n");
137 >                                        /* EPS comments */
138 >        puts("%!PS-Adobe-2.0 EPSF-2.0");
139          printf("%%%%Title: %s\n", name);
140 <        printf("%%%%Creator: %s\n", progname);
140 >        printf("%%%%Creator: %s = %s\n", progname, SCCSid);
141          printf("%%%%Pages: %d\n", ncopies);
142          if (landscape = xmax > pixaspect*ymax)
143 <                printf("%%%%Landscape\n");
143 >                puts("%%Orientation: Landscape");
144          else
145 <                printf("%%%%Portrait\n");
135 <        printf("%%%%EndComments\n");
136 <        printf("64 dict begin\n");
137 <                                        /* define image reader */
138 <        PSprocdef("read6bit");
139 <                                        /* set up transformation matrix */
140 <        printf("%f %f translate\n", HMARGIN, VMARGIN);
145 >                puts("%%Orientation: Portrait");
146          if (PWIDTH > PHEIGHT ^ landscape) {
142                printf("0 %f translate\n", PHEIGHT);
143                printf("-90 rotate\n");
147                  pwidth = PHEIGHT;
148                  pheight = PWIDTH;
149          } else {
# Line 154 | Line 157 | char  *name;
157                  iheight = pheight;
158                  iwidth = pheight*xmax/(pixaspect*ymax);
159          }
160 +        if (pwidth == PHEIGHT)
161 +                printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
162 +                                HMARGIN+(pheight-iheight)*.5,
163 +                                VMARGIN+(pwidth-iwidth)*.5,
164 +                                HMARGIN+(pheight-iheight)*.5+iheight,
165 +                                VMARGIN+(pwidth-iwidth)*.5+iwidth);
166 +        else
167 +                printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
168 +                                HMARGIN+(pwidth-iwidth)*.5,
169 +                                VMARGIN+(pheight-iheight)*.5,
170 +                                HMARGIN+(pwidth-iwidth)*.5+iwidth,
171 +                                VMARGIN+(pheight-iheight)*.5+iheight);
172 +        puts("%%EndComments");
173 +        puts("save");
174 +        puts("64 dict begin");
175 +                                        /* define image reader */
176 +        if (docolor) {
177 +                printf("/redline %d string def\n", xmax);
178 +                printf("/grnline %d string def\n", xmax);
179 +                printf("/bluline %d string def\n", xmax);
180 +                printf("/rgbline %d string def\n", 3*xmax);
181 +                PSprocdef("read6bitRLE", "interleave");
182 +        } else {
183 +                printf("/greyline %d string def\n", xmax);
184 +                PSprocdef("read6bitRLE", NULL);
185 +        }
186 +                                        /* set up transformation matrix */
187 +        printf("%f %f translate\n", HMARGIN, VMARGIN);
188 +        if (pwidth == PHEIGHT) {
189 +                printf("0 %f translate\n", PHEIGHT);
190 +                puts("-90 rotate");
191 +        }
192          printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
193          printf("%f %f scale\n", iwidth, iheight);
194 <        printf("%%%%EndProlog\n");
194 >        puts("%%EndProlog");
195                                          /* start image procedure */
196 <        printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax,
197 <                        xmax, -ymax, ymax);
196 >        printf("%d %d 8 [%d 0 0 %d 0 %d] ", xmax, ymax, xmax, -ymax, ymax);
197 >        if (docolor) {
198 >                puts("{redline read6bitRLE grnline read6bitRLE");
199 >                puts("\tbluline read6bitRLE rgbline interleave} false 3 colorimage");
200 >        } else
201 >                puts("{greyline read6bitRLE} image");
202   }
203  
204  
# Line 170 | Line 209 | PStrailer()                    /* print PostScript trailer */
209                  printf("/#copies %d def\n", ncopies);
210          puts("showpage");
211          puts("end");
212 +        puts("restore");
213          puts("%%EOF");
214   }
215  
216  
217 < PSprocdef(nam)                  /* define PS procedure to read image */
218 < char  *nam;
217 > PSprocdef(nam, inam)            /* define PS procedure to read image */
218 > char  *nam, *inam;
219   {
220          short  itab[128];
221          register int  i;
# Line 186 | Line 226 | char  *nam;
226                  itab[code[i]] = i<<2 | 2;
227          itab[code[0]] = 0;              /* black is black */
228          itab[code[63]] = 255;           /* and white is white */
229 <        printf("/decode [");
229 >        printf("/codetab [");
230          for (i = 0; i < 128; i++) {
231                  if (!(i & 0xf))
232                          putchar('\n');
233                  printf(" %3d", itab[i]);
234          }
235          printf("\n] def\n");
236 <        printf("/scanline %d string def\n", xmax);
237 <        printf("/%s {\n", nam);
236 >        printf("/nrept 0 def\n");
237 >        printf("/readbyte { currentfile read not {stop} if } bind def\n");
238 >        printf("/decode { codetab exch get } bind def\n");
239 >        printf("/%s {\t%% scanbuffer\n", nam);
240 >        printf("\t/scanline exch def\n");
241          printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
242 <        printf("\t\t{ decode currentfile read not {stop} if get\n");
243 <        printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n");
244 <        printf("\t} stopped {pop pop pop 0 string} {scanline} ifelse\n");
242 >        printf("\t\tnrept 0 le\n");
243 >        printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR);
244 >        printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1);
245 >        printf("\t\t\t\t\t\t/reptv readbyte decode def\n");
246 >        printf("\t\t\t\t\t\treptv exit }\n");
247 >        printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n");
248 >        printf("\t\t\t\tifelse } loop }\n");
249 >        printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n");
250 >        printf("\t\tifelse put\n");
251 >        printf("\t\t} for\n");
252 >        printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n");
253          printf("} bind def\n");
254 +        if (inam == NULL)
255 +                return;
256 +                                        /* define interleaving procedure */
257 +        printf("/%s {\t%% redscn grnscn bluscn rgbscn\n", inam);
258 +        printf("\t/rgbscan exch def /bscan exch def\n");
259 +        printf("\t/gscan exch def /rscan exch def\n");
260 +        printf("\trscan length %d eq gscan length %d eq and ", xmax, xmax);
261 +        printf("bscan length %d eq and \n", xmax);
262 +        printf("\t{ 0 1 %d { /ndx exch def\n", xmax-1);
263 +        printf("\t\trgbscan ndx 3 mul rscan ndx get put\n");
264 +        printf("\t\trgbscan ndx 3 mul 1 add gscan ndx get put\n");
265 +        printf("\t\trgbscan ndx 3 mul 2 add bscan ndx get put\n");
266 +        printf("\t\t} for rgbscan }\n");
267 +        printf("\t{0 string} ifelse\n");
268 +        printf("} bind def\n");
269   }
270  
271  
272   ra2ps()                         /* convert Radiance scanlines to 6-bit */
273   {
274 <        COLR    *scanin;
209 <        register int    col = 0;
210 <        register int    c;
211 <        register int    x;
274 >        register COLR   *scanin;
275          int     y;
276                                                  /* allocate scanline */
277          scanin = (COLR *)malloc(xmax*sizeof(COLR));
# Line 219 | Line 282 | ra2ps()                                /* convert Radiance scanlines to 6-bit */
282                  if (freadcolrs(scanin, xmax, stdin) < 0)
283                          quiterr("error reading Radiance picture");
284                  normcolrs(scanin, xmax, bradj); /* normalize */
285 <                for (x = 0; x < xmax; x++) {
286 <                        if (!(col++ & 0x3f))
287 <                                putchar('\n');
288 <                        c = normbright(scanin[x]) + (random()&3);
289 <                        if (c > 255) c = 255;
290 <                        putchar(code[c>>2]);
228 <                }
285 >                if (docolor) {
286 >                        putprim(scanin, RED);
287 >                        putprim(scanin, GRN);
288 >                        putprim(scanin, BLU);
289 >                } else
290 >                        putprim(scanin, GRY);
291                  if (ferror(stdout))
292                          quiterr("error writing PostScript file");
293          }
294          putchar('\n');
295                                                  /* free scanline */
296          free((char *)scanin);
297 + }
298 +
299 +
300 + putprim(scn, pri)               /* put out one primary from scanline */
301 + COLR    *scn;
302 + int     pri;
303 + {
304 +        register int    c;
305 +        register int    x;
306 +        int     lastc, cnt;
307 +
308 +        lastc = -1; cnt = 0;
309 +        for (x = 0; x < xmax; x++) {
310 +                if (pri == GRY)
311 +                        c = normbright(scn[x]) + 2;
312 +                else
313 +                        c = scn[x][pri] + 2;
314 +                if (c > 255) c = 255;
315 +                c = code[c>>2];
316 +                if (c == lastc && cnt < MAXRUN)
317 +                        cnt++;
318 +                else {
319 +                        putrle(cnt, lastc);
320 +                        lastc = c;
321 +                        cnt = 1;
322 +                }
323 +        }
324 +        putrle(cnt, lastc);
325 + }
326 +
327 +
328 + putrle(cnt, cod)                /* put out cnt of cod */
329 + register int    cnt, cod;
330 + {
331 +        static int      col = 0;
332 +
333 +        if (cnt >= MINRUN) {
334 +                col += 3;
335 +                putchar(RUNCHR);
336 +                putchar(RSTRT-MINRUN+cnt);
337 +                putchar(cod);
338 +        } else {
339 +                col += cnt;
340 +                while (cnt-- > 0)
341 +                        putchar(cod);
342 +        }
343 +        if (col >= 72) {
344 +                putchar('\n');
345 +                col = 0;
346 +        }
347   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines