uc2ps.c source code


/*
 *	tops -
 *		Convert a color or black and white image to PostScript.
 *	This knows how to print images with either 1, 2, 4, or 8 bits per 
 *	pixel, and how to generate different screen densities and screen 
 *	angles.  Postscript data is written to standard out, so use a 
 *	command like:
 *
 *			tops blat.rgb | lp
 *
 *					to actually print a picture.
 *
 *				Paul Haeberli - 1988
 *
 */

#include 
#include 
static unsigned char buf[10240];
int hi[256], low[256];

usage(str)
     char *str;
{
  fprintf(stderr, "%s: usage is:\n%s -w width -h height -p pixelsperinch \n\
[-x page offset (inches) ][-y page offset]\n\
[-d screendensity]\n\
[-a angle]\n\
[-m maxxinches,yinches]\n\
[-b bitsperpixel] < inpic > out.ps\n", str, str);
}



main(argc, argv)
     int argc;
     char **argv;
{
  extern int optind;
  extern char *optarg;
  int c;
  float screendensity, screenangle, pixperinch, maxxsize, maxysize;
  int bitsper, width, height;
  int xoffset = 50, yoffset = 50;

  screendensity = 75.0;
  screenangle = 45.0;
  pixperinch = -1.0;
  maxxsize = 528.0;
  maxysize = 700.0;
  bitsper = 8;
  
  while((c = getopt(argc, argv, "b:d:a:p:m:w:h:x:y:"))!=EOF)
    switch(c){
    case 'x':
      xoffset = atof(optarg) * 72.0;
      break;

    case 'y':
      yoffset = atof(optarg) * 72.0;
      break;

    case 'b':
      bitsper = atoi(optarg);
      break;

    case 'd':
      screendensity = atof(optarg);      
      break;

    case 'a':
      screenangle = atof(optarg);      
      break;

    case 'p':
      pixperinch = atof(optarg);
      break;

    case 'm':
      sscanf(optarg, "%f,%f", &maxxsize, &maxysize);
      break;

    case 'w':
      width = atoi(optarg);
      break;

    case 'h':
      height = atoi(optarg);
      break;

    default:
      usage(argv[0]);
      exit(-1);
    }

  tops(screendensity,screenangle,pixperinch,maxxsize,maxysize,bitsper,
       width, height, xoffset, yoffset);
}

tops(screendensity,screenangle,pixperinch,maxxsize,maxysize,bitsper, 
     width, height, xoffset, yoffset)
     float screendensity, screenangle, pixperinch, maxxsize,maxysize;
     int width, height;
     int bitsper;
     int xoffset, yoffset;
{
  register int x, y, n, i, val;
  int picstrlen;
  float doscale, ppiscale;
  
  maketables();
  
  picstrlen = width*bitsper;
  picstrlen = (picstrlen+7)/8;
  
  if(height/(float)width < maxysize/maxxsize) 
    doscale = maxxsize/width;
  else 
    doscale = maxysize/height;
  if(pixperinch > 0.0) {
    ppiscale = 72.0/pixperinch;
    if(ppiscale> 7;
	  x++;
	}
	psputchar("0123456789abcdef"[val]);
      }
      break;	
    case 2:
      x=0;
      for(n=2*picstrlen; n--; ) {
	val = 0;
	for(i=0; i<2; i++) {
	  val <<= 2;
	  val |= (buf[x]&0xc0) >> 6;
	  x++;
	}
	psputchar("0123456789abcdef"[val]);
      }
      break;	
    case 4:
      x=0;
      for(n=2*picstrlen; n--; ) {
	val = (buf[x]&0xf0) >> 4;
	x++;
	psputchar("0123456789abcdef"[val]);
      }
     int	hi[i] = "0123456789abcdef"[i>>4];
	low[i] = "0123456789abcdef"[i&0xf];
    }
}

static int pos = 0;

psputchar(c)
int c;
{
    putchar(c);
    if(++pos == 50) {
	putchar('\n');
	pos = 0;
    }
}