--- ray/src/px/ra_ps.c 1995/09/28 11:23:42 2.11 +++ ray/src/px/ra_ps.c 1997/09/23 16:52:23 2.19 @@ -1,7 +1,7 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1997 Silicon Graphics, Inc. */ #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static char SCCSid[] = "$SunId$ SGI"; #endif /* @@ -9,17 +9,27 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include +#include +#include #ifdef MSDOS #include #endif #include "color.h" +#define UPPER(c) ((c)&~0x20) /* ASCII trick */ + +#define CODE6GAM 1.47 /* gamma for 6-bit codes */ +#define DEFDGAM 1.0 /* default device gamma */ + #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 DEFMARG 0.5 /* default margin (inches) */ +#define DEFWIDTH 8.5 /* default page width */ +#define DEFHEIGHT 11 /* default page height */ +#define HMARGIN (hmarg*72) /* horizontal margin */ +#define VMARGIN (vmarg*72) /* vertical margin */ +#define PWIDTH (width*72-2*HMARGIN) /* width of device */ +#define PHEIGHT (height*72-2*VMARGIN) /* height of device */ #define RUNCHR '*' /* character to start rle */ #define MINRUN 4 /* minimum run-length */ @@ -32,15 +42,25 @@ char code[] = /* 6-bit code lookup table */ int wrongformat = 0; /* input in wrong format? */ double pixaspect = 1.0; /* pixel aspect ratio */ +double devgam = DEFDGAM; /* device gamma response */ +double hmarg = DEFMARG, + vmarg = DEFMARG; /* horizontal and vertical margins */ +double width = DEFWIDTH, + height = DEFHEIGHT; /* default paper width and height */ 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; extern char *malloc(); +extern double unit2inch(); headline(s) /* check header line */ @@ -61,15 +81,52 @@ int argc; char *argv[]; { int i; + double d; progname = argv[0]; 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++; + 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 'g': /* device gamma adjustment */ + devgam = atof(argv[++i]); + break; + case 'p': /* paper size */ + parsepaper(argv[++i]); + break; + case 'm': /* margin */ + d = atof(argv[i+1]); + d *= unit2inch(argv[i+1]); + switch (argv[i][2]) { + case '\0': + hmarg = vmarg = d; + break; + case 'h': + hmarg = d; + break; + case 'v': + vmarg = d; + break; + default: + goto userr; + } + i++; + break; case 'e': /* exposure adjustment */ if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; @@ -103,6 +160,11 @@ char *argv[]; getheader(stdin, headline, NULL); if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) quiterr("bad picture format"); + /* gamma compression */ + if (putprim == Cputprim) + setcolrgam(CODE6GAM); + else if (devgam != 1.) + setcolrgam(devgam); /* write header */ PSheader(i <= argc-1 ? argv[i] : ""); /* convert file */ @@ -111,12 +173,107 @@ char *argv[]; PStrailer(); exit(0); userr: - fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n", + fprintf(stderr, +"Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-g gamma] [input [output]]\n", progname); exit(1); } +double +unit2inch(s) /* determine unit */ +register char *s; +{ + static struct unit {char n; float f} u[] = { + 'i', 1., + 'm', 1./25.4, + 'c', 1./2.54, + '\0' }; + register struct unit *up; + + while (*s && !isalpha(*s)) + s++; + for (up = u; up->n; up++) + if (up->n == *s) + return(up->f); + return(1.); +} + + +int +matchid(name, id) /* see if name matches id (case insensitive) */ +char *name; +register char *id; +{ + register char *s = name; + + while (*s) { + if (isalpha(*s)) { + if (!isalpha(*id) || UPPER(*s) != UPPER(*id)) + return(0); + } else if (*s != *id) + return(0); + s++; id++; + } + return(!*id || s-name >= 3); /* substrings >= 3 chars OK */ +} + + +parsepaper(ps) /* determine paper size from name */ +char *ps; +{ + static struct psize {char n[12]; float w,h;} p[] = { + "envelope", 4.12, 9.5, + "executive", 7.25, 10.5, + "letter", 8.5, 11., + "lettersmall", 7.68, 10.16, + "legal", 8.5, 14., + "monarch", 3.87, 7.5, + "statement", 5.5, 8.5, + "tabloid", 11., 17., + "A3", 11.69, 16.54, + "A4", 8.27, 11.69, + "A4small", 7.47, 10.85, + "A5", 6.00, 8.27, + "A6", 4.13, 6.00, + "B4", 10.12, 14.33, + "B5", 7.17, 10.12, + "C5", 6.38, 9.01, + "C6", 4.49, 6.38, + "DL", 4.33, 8.66, + "hagaki", 3.94, 5.83, + "" }; + register struct psize *pp; + register char *s = ps; + double d; + + if (isdigit(*s)) { /* check for WWxHH specification */ + width = atof(s); + while (*s && !isalpha(*s)) + s++; + d = unit2inch(s); + height = atof(++s); + width *= d; + height *= d; + if (width >= 1. & height >= 1.) + return; + } else /* check for match to standard size */ + for (pp = p; pp->n[0]; pp++) + if (matchid(s, pp->n)) { + width = pp->w; + height = pp->h; + return; + } + fprintf(stderr, "%s: unknown paper size \"%s\" -- known sizes:\n", + progname, ps); + fprintf(stderr, "_Name________Width_Height_(inches)\n"); + for (pp = p; pp->n[0]; pp++) + fprintf(stderr, "%-11s %5.2f %5.2f\n", pp->n, pp->w, pp->h); + fprintf(stderr, "Or use WWxHH size specification\n"); + exit(1); +} + + quiterr(err) /* print message and exit */ char *err; { @@ -131,6 +288,7 @@ char *err; PSheader(name) /* print PostScript header */ char *name; { + char *rstr; int landscape = 0; double pwidth, pheight; double iwidth, iheight; @@ -170,35 +328,47 @@ char *name; HMARGIN+(pwidth-iwidth)*.5+iwidth, VMARGIN+(pheight-iheight)*.5+iheight); puts("%%EndComments"); - puts("save"); - puts("64 dict begin"); + 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); - printf("/rgbline %d string def\n", 3*xmax); - PSprocdef("read6bitRLE", "interleave"); - } else { - printf("/greyline %d string def\n", xmax); - PSprocdef("read6bitRLE", NULL); - } + } 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("0 %f translate\n", PHEIGHT); - puts("-90 rotate"); + 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); - puts("%%%%EndProlog"); + puts("%%EndProlog"); /* start image procedure */ - printf("%d %d 8 [%d 0 0 %d 0 %d] ", xmax, ymax, xmax, -ymax, ymax); - if (docolor) { - puts("{redline read6bitRLE grnline read6bitRLE"); - puts("\tbluline read6bitRLE rgbline interleave} false 3 colorimage"); - } else - puts("{greyline read6bitRLE} image"); + 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); + } } @@ -209,13 +379,13 @@ PStrailer() /* print PostScript trailer */ printf("/#copies %d def\n", ncopies); puts("showpage"); puts("end"); - puts("restore"); + puts("restore grestore"); puts("%%EOF"); } -PSprocdef(nam, inam) /* define PS procedure to read image */ -char *nam, *inam; +PSprocdef(nam) /* define PS procedure to read image */ +char *nam; { short itab[128]; register int i; @@ -223,7 +393,7 @@ char *nam, *inam; 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, CODE6GAM/devgam); itab[code[0]] = 0; /* black is black */ itab[code[63]] = 255; /* and white is white */ printf("/codetab ["); @@ -251,21 +421,6 @@ char *nam, *inam; printf("\t\t} for\n"); printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n"); printf("} bind def\n"); - if (inam == NULL) - return; - /* define interleaving procedure */ - printf("/%s {\t%% redscn grnscn bluscn rgbscn\n", inam); - printf("\t/rgbscan exch def /bscan exch def\n"); - printf("\t/gscan exch def /rscan exch def\n"); - printf("\trscan length %d eq gscan length %d eq and ", xmax, xmax); - printf("bscan length %d eq and \n", xmax); - printf("\t{ 0 1 %d { /ndx exch def\n", xmax-1); - printf("\t\trgbscan ndx 3 mul rscan ndx get put\n"); - printf("\t\trgbscan ndx 3 mul 1 add gscan ndx get put\n"); - printf("\t\trgbscan ndx 3 mul 2 add bscan ndx get put\n"); - printf("\t\t} for rgbscan }\n"); - printf("\t{0 string} ifelse\n"); - printf("} bind def\n"); } @@ -281,13 +436,18 @@ 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 */ + if (putprim == Cputprim || devgam != 1.) { + 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); + (*putprim)(scanin, RED); + (*putprim)(scanin, GRN); + (*putprim)(scanin, BLU); } else - putprim(scanin, GRY); + (*putprim)(scanin, GRY); if (ferror(stdout)) quiterr("error writing PostScript file"); } @@ -297,7 +457,51 @@ ra2ps() /* convert Radiance scanlines to 6-bit */ } -putprim(scn, pri) /* put out one primary from scanline */ +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; {