/* Grayscale scanned output for the PostScript printer; language PostScript. Done by wtaylor@euclid.colorado.edu 12/17/99 Not very thoroughly tested. */ /* The grayscale has 256 values (8 bits). According to the PostScript manual, this is the maximum possible in this format. Other formats, such as jpeg, are probably required to get a smoother range of colors. */ /* Always check that output ends with `showpage.' */ #define UNIX 1 /* change to 0 if not a unix system */ #define TITLE "PostScript Gray Scale" /* for none, use "". NO UNBALANCED PARENS */ /* Should be compiled with a definition of a function g(x,y). The range of x and y is assumed to be [0,1]. The range of g should be the integers {0,1, ... ,255} (0=black, 255 = white). */ /* There is also an option of calling a once-only startup routine called setup(argv[1]) which reads files, sets some global variables, and so on. This routine will be called only if SETUP is defined to be something other than 0. Definition of setup should be in the file containing g. */ #define SETUP 0 /* make this 0 unless setup routine is desired */ #include #include /* Recommended size is .8, 1.6, 2.4 (small multiples of .8 inches.) Can go up to 6.4 for final copy. Recommended coarseness is 2, 4 or 8. Can go down to 1 for final copy. */ #define MESH 300 /* dots per inch ; don't change */ #define SIZE 4.0 /* width = height of picture, in inches */ #define DOTCOUNT (MESH*SIZE) /* width = height of picture measured in dots */ #define POINTS_PER_INCH 72 /* don't change this */ #define COARSENESS 4 /* should be 1, 2, 4, 8, 16 or 32 */ double g(double x, double y); int f(x,y) double x, y; { double w; int k; w = 256*g(x,y); k = (int) w; if (k<=0) k = 0; if (k>=255) k = 255; return(k); } main(argc,argv) int argc; char *argv[]; { /* The picture will be scaled so that X,Y,j,k,m will all enumerate pixels */ int X,Y; int m, j, k; int CODE; char c; char temp[10000]; int f(), ff, i, ii, total; double x, y; /* Comment: According to what is written in the PostScript manual, on any device, one could simply write ONE bitmap and be done with it. Physical limitations exist, however, on the length of a string. (p. 260: limit = 64K) */ #if SETUP setup(argv[1]); #endif begin_picture(); for (Y=0;Y < DOTCOUNT;Y+=32) for (X=0;X < DOTCOUNT;X+=32) { /* Bitmap begins here; reserved for later delivery, however. */ i=0; total=0; for (m=0;m <32;m+=COARSENESS) { /* Each j-value corresponds to one row of the bitmap */ for (j=0;j<32;j+=(COARSENESS)) { CODE=0; x = (X + j)/DOTCOUNT; y = (Y + m)/DOTCOUNT; ff = f(x,y); /* 0 <= ff < 255 */ CODE = ff; total += ff; /* Here the 8-bit character CODE is delivered as two hexadecimal digits: */ c = CODE/16; if (c<10) temp[i]=c+'0'; else temp[i]=c-10+'A'; i++; c = CODE%16; if (c<10) temp[i]=c+'0'; else temp[i]=c-10+'A'; i++; } } if (total==0) { begin_small_bitmap(X,Y); printf("00"); end_bitmap(); } else if(total==(255*(32/COARSENESS)*(32/COARSENESS))) { begin_small_bitmap(X,Y); printf("FF"); end_bitmap(); } else { begin_bitmap(X,Y); for (ii=0;ii0)) putchar('\n'); putchar(temp[ii]); } end_bitmap(); } } showpage(); } char months[12][20] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; char days[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; begin_picture() { #if(UNIX) char *getlogin(); #include #include char month[20]; char day[20]; struct tm *localtime(), *tp; struct timeval tv; struct timezone tz; #endif double scaling_factor = (((double)POINTS_PER_INCH)/MESH); int size = 32/COARSENESS; printf("%%!PS\n"); /* PS added 11/10/99 wt */ printf("\t(\\t8-bit sampled image in PostScript"); #if(UNIX) printf(" by %s",getlogin()); #endif printf(",\\n) print\n"); printf("\t(\\tprepared with 8-bit sampling software by W. Taylor.\\n) print\n"); printf("\t(\\tFor similar sampling-ware for 1-bit (black and white)\\n) print\n"); printf("\t(\\timages, see Section 2.7 of his \"Geometry of Computer\\n) print\n"); printf("\t(\\tGraphics,\" published by Wadsworth-Brooks-Cole, 1991.\\n) print\n"); printf("\t(\\t * * * * \\n) print\n"); printf("\t(\\tThe 8-bit sampling software was prepared in 1999.\\n) print\n"); printf("\t(\\t * * * * \\n) print\n"); printf("\t(\\tSize: "); printf("%2.1f",SIZE); printf(" inches.\\n) print\n"); printf("\t(\\tPixel resolution: "); printf("%d",COARSENESS); printf("\\n) print\n"); printf("\t(\\tName of picture: "); printf(TITLE); printf("\\n) print\n"); #if(UNIX) gettimeofday(&tv, &tz); tp = localtime(&tv.tv_sec); printf("\t(\\t%s, %s %d, 19%d", days[tp->tm_wday], months[tp->tm_mon], tp->tm_mday, tp->tm_year); if (tp->tm_hour==0) printf(" %d:%d AM", 12 ,tp->tm_min); if (tp->tm_hour<12) printf(" %d:%d AM",tp->tm_hour,tp->tm_min); if (tp->tm_hour==12) printf(" %d:%d PM", 0 ,tp->tm_min); if (tp->tm_hour>12) printf(" %d:%d PM",tp->tm_hour -12,tp->tm_min); printf("\\n) print\n"); #endif printf("save 214.2 88.2 moveto "); printf("%%location of the label\n"); printf("/Times-Roman findfont 14 scalefont setfont %%font for the label\n"); printf("("); printf(TITLE); printf(") show %%the label itself\n"); #if(UNIX) printf("restore save 400.2 48.2 moveto "); printf("%%location of the username\n"); printf("/Times-Roman findfont 8 scalefont"); printf(" setfont %%font for the username\n"); printf("(Login name: %s) show\n",getlogin()); printf("400.2 33.2 moveto "); printf("%%location of the date\n"); printf("(%s, %s %d, 19%d) show restore\n", days[tp->tm_wday], months[tp->tm_mon], tp->tm_mday, tp->tm_year); /* if (tp->tm_hour==0) printf(" %d:%d AM\n", 12 ,tp->tm_min); if (tp->tm_hour<12) printf(" %d:%d AM\n",tp->tm_hour,tp->tm_min); if (tp->tm_hour==12) printf(" %d:%d PM\n", 0 ,tp->tm_min); if (tp->tm_hour>12) printf(" %d:%d PM\n",tp->tm_hour -12,tp->tm_min); */ #endif printf("%%%%\n%%%%\n"); printf("000.0 150.00 translate\n"); printf("%3.2f %3.2f scale ", scaling_factor, scaling_factor); printf("%% This makes one unit = one pixel, at 300 DPI\n"); printf("/x {image restore save} def\n"); printf("/y {translate %d %d 8",size,size); printf(" [%4.5f 0 0 %4.5f 0 0]",(float)size/32, (float)size/32); printf("} def\n"); printf("/z {translate 1 1 8 [0.03125 0 0"); printf(" 0.03125 0 0]} def\n"); printf("save\n"); } begin_small_bitmap(X,Y) int X,Y; { int size = 32/COARSENESS; printf("%d %d ",X+200,Y+200); printf("z\n"); printf("{<\n"); } begin_bitmap(X,Y) int X,Y; { int size = 32/COARSENESS; printf("%d %d ",X+200,Y+200); printf("y\n"); printf("{<\n"); } end_bitmap() { printf(">}\n"); printf("x "); } showpage() { printf("\nshowpage\n"); }