/* Raster output for the PostScript printer; language PostScript. Done by wtaylor@euclid.colorado.edu 11/99 This is an example with random dithering. In other words, it implements section 2.7 including the probabilistic filter of section 2.7.2. */ /* Always check that output ends with `showpage.' */ #define UNIX 1 /* change to 0 if not a unix system */ #define TITLE "Gray wedge" /* for none, use "". NO UNBALANCED PARENS */ /* 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 f. */ #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 */ /* Best to keep the picture coarse (4 or above) until you get the hang of it. Also small, 4 inches or less. */ 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[300]; 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+=(8*COARSENESS)) { /* And each k-value corresponds to an 8-bit character describing 8 bits of the row. If the row length is less than eight, then padding is added below. */ CODE=0; for (k=0;((k<(8*COARSENESS))&&(k<32));k+=COARSENESS) { x = (X + j + k)/DOTCOUNT; y = (Y + m)/DOTCOUNT; ff = f(x,y); CODE = (2*CODE) + ff; total += ff; } /* Padding for each row of the bitmap, as specified in footnote 6 on page 74 of the PostScript manual: */ if(COARSENESS== 8) CODE*= 16; if(COARSENESS==16) CODE*= 64; if(COARSENESS==32) CODE*=128; /* 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++; } } /* printf("\nTotal = %d\n",total); */ if (total==0) { begin_small_bitmap(X,Y); printf("00"); end_bitmap(); } else if(total==((32/COARSENESS)*(32/COARSENESS))) { begin_small_bitmap(X,Y); printf("80"); 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(\\tRaster picture"); #if(UNIX) printf(" by %s",getlogin()); #endif printf(",\\n) print\n"); printf("\t(\\tprepared with software by W. Taylor. See Section\\n) print\n"); printf("\t(\\t2.7 of his \"Geometry of Computer Graphics,\"\\n) print\n"); printf("\t(\\tpublished by Wadsworth-Brooks-Cole, 1991.\\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"); 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()); gettimeofday(&tv, &tz); tp = localtime(&tv.tv_sec); printf("400.2 33.2 moveto "); printf("%%location of the date\n"); printf("(%s, %s %d, %d) show restore\n", days[tp->tm_wday], months[tp->tm_mon], tp->tm_mday, 1900+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 1",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 1 [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"); } f(r,s) double r, s; { double y, z, w; double g(); y = rand()%1000; w = y/1000; if (g(r,s) >= w) return (1); return (0); } double g(r,s) double r, s; { double z; z = 0.5 * (r+s); return(z); }