/* Simple program to read in an 8-bit deep picture file from a Targa M8
 * board and display it via PostScript.
 *
 * Copyright 1988,1989 Metron Computerware, Ltd. Oakland, CA 94602
 * Unpublished work of Metron Computerware, Ltd.  All rights reserved.
 *
 * Small pictures have already been rotated from the original raster by the
 * capture program to print in a proper orientation on the paper.
 *
 * Last revised 21 March 1989
 *
 * This program first outputs a postscript program to stdout.
 * It then reads stdin or a file to get the hexified image.
 * It sends this postscript stuff to stdout.
 *
 * Usage: printuface [fn] [< hexpicturefile] > postscriptdevice
 *        if any argument is supplied at all, the program expects to read
 *        that as a uunet formatted file.  Otherwise it reads that format
 *        from stdin.
 *
 * cc -o printface printface.c
 *
 * The pixels on a Targa board are not quite square.  In fact, they are
 * off by the ratio 96:108, very nearly
 */

#include <stdio.h>
#define SERIAL

char scanline[256];                 /* Input for one scanline           */
main(argc,argv)
    int argc;
    char **argv;
{
    int d,i,j,k,l,m;
    char textout[256];
    char fname[128];                /* The first name behind the face   */
    char lname[128];                /* The last name behind the face    */
    char netaddr[128];              /* Net address, if any              */
    char pixinfo[128];              /* width, height, depth of data     */
    char imageinfo[128];            /* width, height, depth of image    */
    unsigned char ch,*cp;
    FILE *fp;
    int pt = 18;                    /* pointsize for message            */

    k = 128;                        /* Normal image                     */
    l = 108;

    if (argc > 1) {
	fp = fopen(argv[1],"r");
	if (fp == NULL) {
	    printf("Can't open %s for input\n",argv[1]);
	    exit (1);
	}
    }
    else
	fp = stdin;


    /* Get the fixed input */
    fgets(fname,     127,fp);
    fgets(lname,     127,fp);
    fgets(netaddr,   127,fp);
    fgets(pixinfo,   127,fp);
    while (sscanf(pixinfo,"PicData: %d %d %d",&l,&k,&d) != 3)
	    fgets(pixinfo,   127,fp);
    fgets(imageinfo, 127,fp);   /* Throw away aspect info, as we assume it  */
    if (l == 108 || l == 96)
	k = 128;
    else {
	printf("Bad picture width %d\n", l);
	exit (1);
    }
    fgets(scanline,127,fp);     /* Throw away blank line */

    d = 0;

    /* Simpleminded stupid construction and output of a postscript      */
    /* program.  This IS NOT proper encapsulated PostScript format      */

    sprintf(textout,"%%!\n/piktur %d %d mul string def\n",k,l);
    write(1,textout,strlen(textout));

    sprintf(textout,"/main {\n/Helvetica findfont %d scalefont setfont\n",pt);
    write(1,textout,strlen(textout));

    sprintf(textout,"currentscreen /p exch def\n");
    write(1,textout,strlen(textout));
    sprintf(textout,"pop pop 50 45 /p load setscreen\n");
    write(1,textout,strlen(textout));

    /* This transfer function prevents pure white on 300 dpi laser printers */
    sprintf(textout,
	" { 1.05 mul 0.05 add dup .99 gt { pop .99 } if } settransfer\n");
    write(1,textout,strlen(textout));

    sprintf(textout,"gsave\n72 72 translate 108 %d moveto\n",d+pt+2);
    write(1,textout,strlen(textout));
    sprintf(textout,"(%s) show\n",fname);
    write(1,textout,strlen(textout));
    sprintf(textout,"108 %d moveto\n",d);
    write(1,textout,strlen(textout));
    sprintf(textout,"(%s) show\n",lname);
    write(1,textout,strlen(textout));

    sprintf(textout,"108 %d moveto\n",d-(pt+2));
    write(1,textout,strlen(textout));
    sprintf(textout,"(%s) show grestore\n",netaddr);
    write(1,textout,strlen(textout));

    /* This line causes the hex image to be read into the string 'piktur'*/
    sprintf(textout,"currentfile piktur readhexstring pop pop \n");
    write(1,textout,strlen(textout));

    sprintf(textout,"72 -1100 translate\n0 0 moveto\n2.5 2.5 scale\n");
    write(1,textout,strlen(textout));

    /*The factor 1.125 compensates for rectangular pixels in the Targa Board*/
    if (l == 108)
	sprintf(textout, "360 1.125 div 1.375 mul  360 1.375 mul scale\n");
    else
	sprintf(textout, "360           1.375 mul  360 1.375 mul scale\n");
    write(1,textout,strlen(textout));


    /* These lines setup the matrix and display a large copy of the picture */
    sprintf(textout,"%d %d 8 ",l,k);
    write(1,textout,strlen(textout));

    sprintf(textout,"[ 256 0 0 256 0 -256 ] \n");
    write(1,textout,strlen(textout));
    sprintf(textout,"{ piktur } image \n");
    write(1,textout,strlen(textout));


    sprintf(textout,"0.0 0.8 translate\n0.25 0.25 scale\n");
    write(1,textout,strlen(textout));

    /* These lines setup the matrix and display a small copy of the picture */

    sprintf(textout,"%d %d 8 ",l,k);
    write(1,textout,strlen(textout));
    sprintf(textout,"[ 512 0 0 512 0 -256 ] \n");
    write(1,textout,strlen(textout));

    sprintf(textout," { piktur } ");
    write(1,textout,strlen(textout));

    sprintf(textout,"image showpage} def\n");
    write(1,textout,strlen(textout));
    sprintf(textout,"main\n");
    write(1,textout,strlen(textout));

    /* The Hexified file has the ouput scanlines broken into 4 pieces,
     * and each piece is twice as wide as the number of pixels, since
     * each byte is converted to two hex digits.
     * Thus each piece is not too wide, for screen readability.
     */

    m = l + 1;         /* Hexified info plus newline */
    for (i=0;i<k*4;i++) {
	    fgets(scanline,m,fp);
	    printf("%s",scanline);
    }

    /* Terminate the PostScript with an EOF */
    printf("\n\004");
}
