--- ray/src/px/ra_ps.c 1992/09/21 12:15:08 2.6 +++ ray/src/px/ra_ps.c 1995/10/06 12:38:38 2.16 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -9,26 +9,40 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include +#include #ifdef MSDOS #include #endif #include "color.h" -#include "random.h" +#define GAMVAL 1.47 /* gamma value for pixels */ + +#define GRY -1 /* artificial index for grey */ + #define HMARGIN (.5*72) /* horizontal margin */ #define VMARGIN (.5*72) /* vertical margin */ #define PWIDTH (8.5*72-2*HMARGIN) /* width of device */ #define PHEIGHT (11*72-2*VMARGIN) /* height of device */ +#define RUNCHR '*' /* character to start rle */ +#define MINRUN 4 /* minimum run-length */ +#define RSTRT '!' /* character for MINRUN */ +#define MAXRUN (MINRUN+'~'-RSTRT) /* maximum run-length */ + char code[] = /* 6-bit code lookup table */ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+"; int wrongformat = 0; /* input in wrong format? */ double pixaspect = 1.0; /* pixel aspect ratio */ +int docolor = 0; /* produce color image? */ int bradj = 0; /* brightness adjustment */ int ncopies = 1; /* number of copies */ +extern int Aputprim(), Bputprim(), Cputprim(); + +int (*putprim)() = Aputprim; /* function for writing scanline */ + char *progname; int xmax, ymax; @@ -60,6 +74,21 @@ char *argv[]; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { + case 'b': /* produce b&w PostScript */ + docolor = 0; + break; + case 'c': /* produce color PostScript */ + docolor = 1; + break; + case 'A': /* standard ASCII encoding */ + putprim = Aputprim; + break; + case 'B': /* standard binary encoding */ + putprim = Bputprim; + break; + case 'C': /* compressed ASCII encoding */ + putprim = Cputprim; + break; case 'e': /* exposure adjustment */ if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; @@ -82,7 +111,7 @@ char *argv[]; exit(1); } if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { - fprintf(stderr, "can't open output \"%s\"\n", + fprintf(stderr, "%s: can't open output \"%s\"\n", progname, argv[i+1]); exit(1); } @@ -93,6 +122,9 @@ char *argv[]; getheader(stdin, headline, NULL); if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) quiterr("bad picture format"); + /* gamma compression */ + if (putprim == Cputprim) + setcolrgam(GAMVAL); /* write header */ PSheader(i <= argc-1 ? argv[i] : ""); /* convert file */ @@ -101,7 +133,8 @@ char *argv[]; PStrailer(); exit(0); userr: - fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname); + fprintf(stderr, "Usage: %s [-b|c][-A|B|C][-e +/-stops] [input [output]]\n", + progname); exit(1); } @@ -120,27 +153,20 @@ char *err; PSheader(name) /* print PostScript header */ char *name; { + char *rstr; int landscape = 0; double pwidth, pheight; double iwidth, iheight; - - printf("%%!\n"); + /* EPS comments */ + puts("%!PS-Adobe-2.0 EPSF-2.0"); printf("%%%%Title: %s\n", name); - printf("%%%%Creator: %s\n", progname); + printf("%%%%Creator: %s = %s\n", progname, SCCSid); printf("%%%%Pages: %d\n", ncopies); if (landscape = xmax > pixaspect*ymax) - printf("%%%%Landscape\n"); + puts("%%Orientation: Landscape"); else - printf("%%%%Portrait\n"); - printf("%%%%EndComments\n"); - printf("64 dict begin\n"); - /* define image reader */ - PSprocdef("read6bit"); - /* set up transformation matrix */ - printf("%f %f translate\n", HMARGIN, VMARGIN); + puts("%%Orientation: Portrait"); if (PWIDTH > PHEIGHT ^ landscape) { - printf("0 %f translate\n", PHEIGHT); - printf("-90 rotate\n"); pwidth = PHEIGHT; pheight = PWIDTH; } else { @@ -154,12 +180,60 @@ char *name; iheight = pheight; iwidth = pheight*xmax/(pixaspect*ymax); } + if (pwidth == PHEIGHT) + printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", + HMARGIN+(pheight-iheight)*.5, + VMARGIN+(pwidth-iwidth)*.5, + HMARGIN+(pheight-iheight)*.5+iheight, + VMARGIN+(pwidth-iwidth)*.5+iwidth); + else + printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", + HMARGIN+(pwidth-iwidth)*.5, + VMARGIN+(pheight-iheight)*.5, + HMARGIN+(pwidth-iwidth)*.5+iwidth, + VMARGIN+(pheight-iheight)*.5+iheight); + puts("%%EndComments"); + puts("gsave save"); + puts("17 dict begin"); + /* define image reader */ + if (docolor) { + printf("/redline %d string def\n", xmax); + printf("/grnline %d string def\n", xmax); + printf("/bluline %d string def\n", xmax); + } else + printf("/gryline %d string def\n", xmax); + /* use compressed encoding? */ + if (putprim == Cputprim) + PSprocdef("read6bitRLE"); + /* set up transformation matrix */ + printf("%f %f translate\n", HMARGIN, VMARGIN); + if (pwidth == PHEIGHT) { + printf("%f 0 translate\n", PWIDTH); + puts("90 rotate"); + } printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); printf("%f %f scale\n", iwidth, iheight); - printf("%%%%EndProlog\n"); + puts("%%EndProlog"); /* start image procedure */ - printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, - xmax, -ymax, ymax); + printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax); + if (putprim == Cputprim) { + if (docolor) { + puts("{redline read6bitRLE}"); + puts("{grnline read6bitRLE}"); + puts("{bluline read6bitRLE}"); + puts("true 3 colorimage"); + } else + puts("{gryline read6bitRLE} image"); + } else { + rstr = putprim==Aputprim ? "readhexstring" : "readstring"; + if (docolor) { + printf("{currentfile redline %s pop}\n", rstr); + printf("{currentfile grnline %s pop}\n", rstr); + printf("{currentfile bluline %s pop}\n", rstr); + puts("true 3 colorimage"); + } else + printf("{currentfile gryline %s pop} image\n", rstr); + } } @@ -170,6 +244,7 @@ PStrailer() /* print PostScript trailer */ printf("/#copies %d def\n", ncopies); puts("showpage"); puts("end"); + puts("restore grestore"); puts("%%EOF"); } @@ -183,31 +258,40 @@ char *nam; for (i = 0; i < 128; i++) /* clear */ itab[i] = -1; for (i = 1; i < 63; i++) /* assign greys */ - itab[code[i]] = i<<2 | 2; + itab[code[i]] = 256.0*pow((i+.5)/64.0, GAMVAL); itab[code[0]] = 0; /* black is black */ itab[code[63]] = 255; /* and white is white */ - printf("/decode ["); + printf("/codetab ["); for (i = 0; i < 128; i++) { if (!(i & 0xf)) putchar('\n'); printf(" %3d", itab[i]); } printf("\n] def\n"); - printf("/scanline %d string def\n", xmax); - printf("/%s {\n", nam); + printf("/nrept 0 def\n"); + printf("/readbyte { currentfile read not {stop} if } bind def\n"); + printf("/decode { codetab exch get } bind def\n"); + printf("/%s {\t%% scanbuffer\n", nam); + printf("\t/scanline exch def\n"); printf("\t{ 0 1 %d { scanline exch\n", xmax-1); - printf("\t\t{ decode currentfile read not {stop} if get\n"); - printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n"); - printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n"); + printf("\t\tnrept 0 le\n"); + printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR); + printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1); + printf("\t\t\t\t\t\t/reptv readbyte decode def\n"); + printf("\t\t\t\t\t\treptv exit }\n"); + printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n"); + printf("\t\t\t\tifelse } loop }\n"); + printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n"); + printf("\t\tifelse put\n"); + printf("\t\t} for\n"); + printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n"); + printf("} bind def\n"); } ra2ps() /* convert Radiance scanlines to 6-bit */ { - COLR *scanin; - register int col = 0; - register int c; - register int x; + register COLR *scanin; int y; /* allocate scanline */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); @@ -217,18 +301,116 @@ ra2ps() /* convert Radiance scanlines to 6-bit */ for (y = ymax-1; y >= 0; y--) { if (freadcolrs(scanin, xmax, stdin) < 0) quiterr("error reading Radiance picture"); - normcolrs(scanin, xmax, bradj); /* normalize */ - for (x = 0; x < xmax; x++) { - if (!(col++ & 0x3f)) - putchar('\n'); - c = normbright(scanin[x]) + (random()&3); - if (c > 255) c = 255; - putchar(code[c>>2]); - } + if (putprim == Cputprim) { + if (bradj) /* adjust exposure */ + shiftcolrs(scanin, xmax, bradj); + colrs_gambs(scanin, xmax); /* gamma compression */ + } else + normcolrs(scanin, xmax, bradj); + if (docolor) { + (*putprim)(scanin, RED); + (*putprim)(scanin, GRN); + (*putprim)(scanin, BLU); + } else + (*putprim)(scanin, GRY); if (ferror(stdout)) quiterr("error writing PostScript file"); } putchar('\n'); /* free scanline */ free((char *)scanin); +} + + +int +Aputprim(scn, pri) /* put out hex ASCII primary from scanline */ +COLR *scn; +int pri; +{ + static char hexdigit[] = "0123456789ABCDEF"; + static int col = 0; + register int x, c; + + for (x = 0; x < xmax; x++) { + if (pri == GRY) + c = normbright(scn[x]); + else + c = scn[x][pri]; + if (c > 255) c = 255; + putchar(hexdigit[c>>4]); + putchar(hexdigit[c&0xf]); + if ((col += 2) >= 72) { + putchar('\n'); + col = 0; + } + } +} + + +int +Bputprim(scn, pri) /* put out binary primary from scanline */ +COLR *scn; +int pri; +{ + register int x, c; + + for (x = 0; x < xmax; x++) { + if (pri == GRY) + c = normbright(scn[x]); + else + c = scn[x][pri]; + if (c > 255) c = 255; + putchar(c); + } +} + + +int +Cputprim(scn, pri) /* put out compressed primary from scanline */ +COLR *scn; +int pri; +{ + register int c; + register int x; + int lastc, cnt; + + lastc = -1; cnt = 0; + for (x = 0; x < xmax; x++) { + if (pri == GRY) + c = normbright(scn[x]) + 2; + else + c = scn[x][pri] + 2; + if (c > 255) c = 255; + c = code[c>>2]; + if (c == lastc && cnt < MAXRUN) + cnt++; + else { + putrle(cnt, lastc); + lastc = c; + cnt = 1; + } + } + putrle(cnt, lastc); +} + + +putrle(cnt, cod) /* put out cnt of cod */ +register int cnt, cod; +{ + static int col = 0; + + if (cnt >= MINRUN) { + col += 3; + putchar(RUNCHR); + putchar(RSTRT-MINRUN+cnt); + putchar(cod); + } else { + col += cnt; + while (cnt-- > 0) + putchar(cod); + } + if (col >= 72) { + putchar('\n'); + col = 0; + } }