From karn@thumper.bellcore.com Wed Jun 28 08:02:29 1989 Received: from thumper.bellcore.com by uunet.uu.net (5.61/1.14) with SMTP id AA01268; Wed, 28 Jun 89 08:01:41 -0400 Received: by thumper.bellcore.com (5.54/4.7) id AA14767; Tue, 27 Jun 89 18:16:03 EDT Received: by jupiter.bellcore.com (4.12/4.7) id AA15628; Tue, 27 Jun 89 18:14:03 edt Date: Tue, 27 Jun 89 18:14:03 edt From: karn@thumper.bellcore.com (Phil R. Karn) Message-Id: <8906272214.AA15628@jupiter.bellcore.com> To: rick@uunet.uu.net Subject: updated sunfaced.c Status: RO /* * Display a face from the uunet FaceSaver picture library * * Uses SunView - Dithered version for visually impaired single bit-plane * Sun Workstations. * * Date: Sun Apr 9 14:50:02 1989 * * Author: D. Murali Raju * Computing Facility, School of Engineering and Applied Science, * The George Washington University, Washington, DC 20052. * Internet: raju@gwusun.gwu.edu UUCP: ...!uunet!gwusun!raju * * Hacked from Jeff Michaud's xface program on uunet. * Rewrote header processing to handle additional header fields - P Karn * 27 June 1989 */ #include typedef unsigned char byte; #define True 1 #define False 0 #define BUFSIZE 1024 #define PIXSIZE 2 #define MAXBPL 15 /* Max bytes per output line in resultant XBM */ char line[BUFSIZE]; char firstname[BUFSIZE]; char lastname [BUFSIZE]; char email [BUFSIZE]; int depth; int iwidth, iheight, idepth; int totalpixels; byte *Image; /* The final image array */ int XC = 0, YC = 0, /* Output X and Y coords of current pixel */ Width, Height, /* image dimensions */ BytesPerScanline; /* bytes per scanline in output raster */ FILE *fp; char *cmd; Syntax() { printf("Usage: %s [-d <1|2|4|8|16>] [-e expansion] [-x] filename\n", cmd); exit(1); } FatalError (identifier) char *identifier; { fprintf(stderr, "%s: %s\n",cmd, identifier); exit(-1); } ReadFace(fname) char *fname; { if (strcmp(fname,"-")==0) { fp = stdin; fname = ""; } else fp = fopen(fname,"r"); if (!fp) FatalError("file not found"); InitImage(); /* Allocate the Image */ Image = (byte *) malloc(Width*Height); if (!Image) FatalError("not enough memory for Image"); BytesPerScanline = Width; YC = Height - 1; XC = 0; ReadItIn(); } InitImage() { char tmp[BUFSIZE]; gethdr(fp,"FirstName",firstname,BUFSIZE); gethdr(fp,"LastName",lastname,BUFSIZE); gethdr(fp,"E-mail",email,BUFSIZE); gethdr(fp,"PicData",tmp,BUFSIZE); sscanf(tmp, "%d %d %d", &Width, &Height, &depth); gethdr(fp,"Image",tmp,BUFSIZE); sscanf(tmp, "%d %d %d", &iwidth, &iheight, &idepth); totalpixels = Width * Height; /* Position the file at the start of the image data */ rewind(fp); while(!feof(fp)){ fgets(tmp,BUFSIZE,fp); if(tmp[0] == '\n') break; } } /* Scan line Enum junk */ char *enum_ptr; /* local to enum routines */ void init_enum(buf) char *buf; { enum_ptr = buf; } int next_enum(out) char *out; { if( enum_ptr[0] == '\n' || enum_ptr[0] == '\0' ) return False; strncpy(out, enum_ptr, PIXSIZE); out[PIXSIZE] = '\0'; enum_ptr += PIXSIZE; return True; } ReadItIn() { while( fgets(line, BUFSIZE, fp) != NULL ) { char pixel[PIXSIZE+1]; init_enum(line); while( next_enum(pixel) ) { unsigned int value; sscanf(pixel, "%x", &value); AddToPixel((byte)value); } } } AddToPixel(Index) byte Index; { if (YC >= 0) *(Image + YC * BytesPerScanline + XC) = Index; /* Update the X-coordinate, and if it overflows, update the Y-coordinate */ if (++XC == Width) { XC = 0; YC--; } } int d_mat[16][16]; /* Dither Matrix (Max size = 16) */ int d2_mat[2][2] = { 0, 2, 3, 1 }; /* The base dither matrix */ int dmSize; /* Current Dither Matrix size */ GetDitherMatrix (mat_size) int mat_size; { register int i, j, n; int SizeOk, m, i1, i2, j1, j2; float scale; /* * Check for legal Matrix Size: 2, 4, 8, 16 are the only allowed values */ SizeOk = False; for (i=1; i<5; i++) { if (mat_size == 1< 2) { for (n=2; n < dmSize; n = 2*n) { for (i=0; i 127) ? 0 : 1); else return((val > d_mat[i%dmSize][j%dmSize]) ? 0 : 1); } PrintDitherMatrix() { register int i, j; for (i=0; i #include Frame frame; /* Type defined by sunview */ Canvas canvas; /* Type defined by sunview */ Rect CanvRect; Rect *crect; Pixwin *pw; int MulFac, CanH, CanW; int ResX, ResY; /* Image size */ int debugFlag = 0; LoadDitheredImage() { register int i, j, ival, ip, jp; int PxVal(); int js, je, is, ie; if (MulFac==1) { pw_lock (pw, crect); for (j=ResY-1;j>=0;j--) { for (i=0;i=0;j--) { js = j*MulFac; je = js + MulFac; pw_lock (pw, crect); for (i=0;i 1) { if (debugFlag) printf ("Creating %dx%d Dither Matrix ...\n", dmSize, dmSize); GetDitherMatrix (dmSize); if (debugFlag) PrintDitherMatrix(); } else printf ("No dithering! Thresholding at value=127 ...\n"); if (debugFlag) printf ("Reading image data from file \"%s\" ...\n", fname); ReadFace(fname); if (debugFlag) printf ("Image expansion factor is %d.\n", MulFac); if (debugFlag) printf ("Creating Frame and Canvas ...\n"); ResY = Height; ResX = Width; /* Create frame & canvas */ if (dmSize > 1) sprintf (Wname, "%s %s <%s> (Size: %dx%dx%d) Dithered %dx%d", firstname, lastname, email, Width, Height, depth, dmSize, dmSize); else sprintf (Wname, "%s %s <%s> (Size: %dx%dx%d) Undithered.", firstname, lastname, email, Width, Height, depth); frame = window_create (0, FRAME, FRAME_LABEL, Wname, 0); CanH = MulFac * ResY; CanW = MulFac * ResX; canvas = window_create (frame, CANVAS, WIN_HEIGHT, CanH, WIN_WIDTH, CanW, 0); CanvRect.r_left = 0; CanvRect.r_top = 0; CanvRect.r_width = CanW; CanvRect.r_height = CanH; crect = (&CanvRect); window_fit(frame); pw = canvas_pixwin(canvas); if (debugFlag) printf ("Painting Image ...\n"); LoadDitheredImage(); if (debugFlag) printf ("Done. Waiting for a ^C or Quit from the Image Window.\n"); else fprintf (stderr, "Done. Waiting for a ^C or Quit from the Image Window.\n"); window_main_loop(frame); exit(0); } /* Search the file open on fp for a line of the form: * Label: value * and return the value string through "val". Returns 0 on success, -1 * if label could not be found. * 27 June 1989 Phil Karn */ int gethdr(fp,label,val,size) FILE *fp; char *label; char *val; int size; { char buf[BUFSIZE]; char *cp,*index(); rewind(fp); for(;;){ fgets(buf, BUFSIZE, fp); if(feof(fp) || buf[0] == '\n') /* Blank line or EOF? */ return -1; /* Read no further */ /* Look for a colon terminating the label */ if((cp = index(buf,':')) != NULL) *cp = '\0'; else continue; /* Ignore lines without colons */ if(strcmp(buf,label) != 0) continue; /* No match */ /* cp -> first char in value field */ cp++; while(*cp == ' ' && *cp != '\0') cp++; /* Give it to the user */ strncpy(val,cp,size); return 0; } }