[net.sources] Images to PostScript

majka@ubc-vision.UUCP (Marc Majka) (05/13/86)

Here is a little filter for turning a raster file containing an arbirary
sized image (8 bits per pixel) into a PostScript file.   Included are the
source, man page, and Makefile.

---
Marc Majka


- - - CUT - - - CUT - - - CUT - - - CUT - - - CUT - - - CUT - - - CUT - - -
#!/bin/sh
#
# shell archive - extract with /bin/sh
#
echo extracting file README
sed 's/^X//' > README <<'!FUNKY!STUFF!'
XThis package contains a filter for turning raster "image" files into 
XPostScript, using the PostScript image operator.  The package contains 
X5 files:
X
XREADME   - this file
XMakefile - for make
Xrps.c    - source
Xrps.1    - man page in nroff -man form
Xrps.man  - formatted manual page
X
XThe program reads an image, 1 byte per pixel, from its input, converts the 
Xpixel value into 2 hex digits, and sticks them behind an "image" operator.
XAn output window of arbitrary size and location may be specified for output.
X
XOur LaserWriters here at UBC seem to be a bit fussy about printing the output.
XThere seem to be some combinations of input size and requested output size that
Xmake the LaserWriter give up on the job.  If anybody can figure out why,
XPLEASE let me know!  
X
X---
XMarc Majka
!FUNKY!STUFF!
echo extracting file Makefile
sed 's/^X//' > Makefile <<'!FUNKY!STUFF!'
X# Makefile for rps
Xrps: rps.c
X	cc -o rps rps.c
!FUNKY!STUFF!
echo extracting file rps.c
sed 's/^X//' > rps.c <<'!FUNKY!STUFF!'
X#include <stdio.h>
Xstatic char tohex[17];
Xstatic int npixo, copy;
X
X/************************************************************************/
X/*                                                                      */
X/* This little program lets you print an "image" file on a Laser Writer */
X/* using PostScript's "image" operator.  This program is based on the   */
X/* example given in the PostScript Cookbook.  It is set up for a 300dpi */
X/* machine.  The image dimensions default to 256*256, with 1 byte per   */
X/* image pixel.  See the manual page for more details.                  */
X/*                                                                      */
X/************************************************************************/
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X    FILE *ifp, *fopen();
X    int r, c, nr, nc, row, col, sc, argn, win, im;
X    int r1, c1, r2, c2, x1, y1, x2, y2; 
X    unsigned char pix;
X    char head[1024];
X    double bscale, boff, temp, min, max, range;
X
X    strcpy(tohex,"0123456789ABCDEF");
X    npixo = 0;
X    x1 = 0;
X    y1 = 3300;
X    x2 = 2550;
X    y2 = 0;
X    copy = 1;
X    im = 0;
X    nr = 256;
X    nc = 256;
X
X    for (argn = 1; argn < argc; argn++) {
X        if (argv[argn][0] == '-') {
X            switch (argv[argn][1]) {
X                case 'w':
X                    win = argn;
X                    r1 = atoi(argv[++win]);
X                    c1 = atoi(argv[++win]);
X                    r2 = atoi(argv[++win]);
X                    c2 = atoi(argv[++win]);
X                    x1 = c1;
X                    y1 = 3300 - r1;
X                    x2 = c2;
X                    y2 = 3300 - r2;
X                    win = 1;
X                    argn += 4;
X                    break;
X                case 'i':
X                    nr = atoi(argv[++argn]);
X                    nc = atoi(argv[++argn]);
X                    break;
X                case 'c': copy = atoi(argv[++argn]); break;
X                default:
X                    fprintf(stderr,
X                    "iffps [file] [-w r1 c1 r2 c2] [-c n] [-i nr nc]\n");
X                    fprintf(stderr,"  file  input image   [stdin]\n");
X                    fprintf(stderr,"  -w    output window [0 0 3300 2550]\n");
X                    fprintf(stderr,"  -c    copies        [1]\n");
X                    fprintf(stderr,"  -i    input size    [256 256]\n");
X                    exit(0);
X            }
X        }
X        else {
X             if (im) {
X                fprintf(stderr,
X                    "iffps [file] [-w r1 c1 r2 c2] [-c n] [-i nr nc]\n");
X                exit(0);
X            }
X            im = 1;
X            ifp = fopen(argv[argn],"r");
X            if (ifp == NULL) {
X                fprintf(stderr,"iffps: can't open image file %s\n",argv[argn]);
X                exit(1);
X            }
X        }
X    }
X
X    if (!im) ifp = stdin;
X    
X    prologue(nr,nc,x1,y1,x2,y2);
X    
X    for (r = 0; r < nr; r++) 
X        for (c = 0; c < nc; c++) {
X             pix = getc(ifp);
X             puthexpix(pix);
X        }
X
X    epilogue();
X    
X    fclose(ifp);
X    exit(0);
X}
X
Xprologue(nr,nc,x1,y1,x2,y2)
Xint nr,nc,x1,y1,x2,y2;
X{
X    printf("gsave\n");
X    printf("initgraphics\n");
X    printf("0.24 0.24 scale\n");
X    printf("/imline %d string def\n",nc*2);
X    printf("/drawimage {\n");
X    printf("    %d %d 8\n",nc,nr);
X    printf("    [%d 0 0 %d 0 %d]\n",nc,-1*nr,nr);
X    printf("    { currentfile imline readhexstring pop } image\n");
X    printf("} def\n");
X    printf("%d %d translate\n",x1,y2);
X    printf("%d %d scale\n",x2-x1,y1-y2);
X    printf("drawimage\n");
X}
X
Xepilogue()
X{
X    if (npixo) printf("\n");
X    if (copy > 1) printf("%d {copypage} repeat\n",copy-1);
X    printf("showpage\n");
X    printf("grestore\n");
X}
X
Xputhexpix(p)
Xunsigned char p;
X{
X    char hi, lo;
X
X    hi = (p >> 4) & 15;
X    lo = p & 15;
X    
X    printf("%c%c",tohex[hi],tohex[lo]);
X    npixo += 1;
X    if (npixo >= 32) {
X        printf("\n");
X        npixo = 0;
X    }
X}
!FUNKY!STUFF!
echo extracting file rps.1
sed 's/^X//' > rps.1 <<'!FUNKY!STUFF!'
X.TH RPS 1
X.SH NAME
Xrps - convert image files to PostScript
X.SH SYNOPSIS
X.B rps
X[file] [-w r1 c1 r2 c2] [-c n] [-i nr nc]
X.SH DESCRIPTION
X.I rps
Xconverts an image file into postcript language, which may then be printed
Xon a Laser Writer with
X.I lpr
X.PP
XWhen given a file name as an argument,
X.I rps
Xreads the named file as an input image.  The image is read as a 1 byte per
Xpixel image.  The image size is assumed to be 256 rows by 256 columns.  The 
X.B \-i
Xoption may be used to specify the size of the input if it is not 256 * 256.
XThe two arguments following the 
X.B \-i
Xoption are taken as the row and column dimensions.
X.sp
XThe
X.B \-w
Xarguments specify an output window on the Laser Writer.  The page in specified
Xin a row-column coordinate system, with 3300 rows and 2550 columns.  There are
X300 pixels per inch.
X.sp
XThe 
X.B \-c
Xargument specifies the number of copies of the image to be printed.
X.SH AUTHOR
XMarc Majka
!FUNKY!STUFF!
echo extracting file rps.man
sed 's/^X//' > rps.man <<'!FUNKY!STUFF!'
XRPS(1)              UNIX Programmer's Manual               RPS(1)
X
XNAME
X     rps - convert image files to PostScript
X
XSYNOPSIS
X     rps [file] [-w r1 c1 r2 c2] [-c n] [-i nr nc]
X
XDESCRIPTION
X     rps converts an image file into postcript language, which
X     may then be printed on a Laser Writer with lpr.
X
X     When given a file name as an argument, rps reads the named
X     file as an input image.  The image is read as a 1 byte per
X     pixel image.  The image size is assumed to be 256 rows by
X     256 columns.  The -i option may be used to specify the size
X     of the input if it is not 256 * 256.  The two arguments fol-
X     lowing the -i option are taken as the row and column dimen-
X     sions.
X
X     The -w arguments specify an output window on the Laser
X     Writer.  The page in specified in a row-column coordinate
X     system, with 3300 rows and 2550 columns.  There are 300 pix-
X     els per inch.
X
X     The -c argument specifies the number of copies of the image
X     to be printed.
X
XAUTHOR
X     Marc Majka
!FUNKY!STUFF!
echo Done\!
exit 0