[comp.lang.postscript] PS -> EPS, PS -> EPSF

dgc@cs.purdue.EDU (Doug Crabill) (03/12/91)

There have been many requests for a program to convert from PostScript
to Encapsulate PostScript and Encapsulated PostScript with a bitmap.  I
have written (glued together) a simple solution.

You give my program (pstoepsi) arbitrary PostScript, and it converts
it to Encapsulated PostScript (BoundingBox information) with a bitmap
representation of the image in comments (EPSI/EPSF format).  It
doesn't require unnecessary printing and hand editing of PostScript
source to calculate BoundingBox information (as is needed with bb.ps).

Pipe the rest of this message through /bin/sh to unshar and read the
README for more information.

Send comments if you have them.

Doug Crabill
Department of Computer Science
Purdue University
dgc@cs.purdue.edu


#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  README pbmtoepsi.c pstoepsi pstorast
# Wrapped by dgc@orion.cs.purdue.edu on Tue Mar 12 10:22:04 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(2401 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XDESCRIPTION
X
XThese scripts allow arbitrary PostScript to be converted to
XEncapsulated PostScript with a bitmap (EPS, EPSI, EPSF).  The real
Xwork has been done by the authors of PBMPLUS (Jef Poskanzer),
XGhostScript (L. Peter Deutsch), and OpenWindows (Sun).  All I have
Xdone is supply some glue to put all of these other applications to
Xwork.  It is beyond me why it has not been done before, especially
Xconsidering the number of requests (pleas) for such a conversion
Xprogram.
X
XI am providing these scripts with no guarantee, but will maintain them
Xand fix bugs if you send them.
X
XDoug Crabill
Xdgc@cs.purdue.edu
X
X
XREQUIREMENTS
X
XYou must have the PBMPLUS utilities.
X
XYou must have either OpenWindows, GhostScript, or both.
X
XPBMPLUS is available via anonymous ftp from export.lcs.mit.edu in
Xcontrib/pbmplus.tar.Z, OpenWindows is available from Sun, and
XGhostScript is available via anonymous ftp from prep.ai.mit.edu in
Xpub/gnu/ghostscript-2.1.1.tar.Z (watch for newer versions).
X
X
XINSTALLATION
X
XDrop pbmtoepsi.c into the pbmplus source (in the pbm subdirectory),
Xadd it to the Makefile, and make.  Put the executable with the other
Xpbm binaries.
X
XPut pstorast (formerly pstobits, posted by someone else in the past --
Xdon't remember who) in an appropriate location (/usr/local/bin, or
Xwhatever).
X
XEdit pstoepsi and change all of the variables at the top so they match
Xyour local configuration.
X
X
XHOW TO USE IT
X
XRead the pstoepsi shell script for usage.
X
X
XHOW IT WORKS
X
XThe conversion works by interpreting the source PostScript using
Xeither X/NeWS (provided with OpenWindows) in batch mode or GhostScript
Xin batch mode, converting the result into a portable bitmap, running it
Xthrough pbmtoepsi, which converts it to a PostScript style bitmap
Xcomplete with bounding box information, and concatenating the result
Xwith the original PostScript.
X
XUsing X/NeWS for the conversion generally takes longer, but yields a
Xsharper bitmap than with GhostScript.  It is convenient to have both
Xavailable because occasionally you will want to convert PostScript
Xwhich will cause one or other of the interpreters to fail.  If you
Xwant to encapsulate PostScript which already thinks it is
Xencapsulated, you should probably use the "-strip" option to pstoepsi.
XThis will strip out all of the old encapsulation and add new
Xencapsulation.
X
X
XDoug Crabill
XDepartment of Computer Science
XPurdue University
Xdgc@cs.purdue.edu
END_OF_FILE
if test 2401 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'pbmtoepsi.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pbmtoepsi.c'\"
else
echo shar: Extracting \"'pbmtoepsi.c'\" \(2248 characters\)
sed "s/^X//" >'pbmtoepsi.c' <<'END_OF_FILE'
X/* pbmtoepsi.c
X**
X**    by Doug Crabill, based heavily on pbmtoascii
X**
X**    Converts a pbm file to an encapsulated PostScript style bitmap.
X**    Note that it does NOT covert the pbm file to PostScript, only to
X**    a bitmap to be added to a piece of PostScript generated elsewhere.
X**
X** Copyright (C) 1988 by Jef Poskanzer.
X**
X** Permission to use, copy, modify, and distribute this software and its
X** documentation for any purpose and without fee is hereby granted, provided
X** that the above copyright notice appear in all copies and that both that
X** copyright notice and this permission notice appear in supporting
X** documentation.  This software is provided "as is" without express or
X** implied warranty.
X*/
X
X#include "pbm.h"
X
X#if !defined(MAXINT)
X#define MAXINT (0x7fffffff)
X#endif
X
Xmain( argc, argv )
X	int argc;
X	char *argv[];
X{
X	FILE *ifd;
X	register bit **bits;
X	int rows, cols, row, col, tot, count;
X	int top = MAXINT, bottom = -MAXINT, left = MAXINT, right = -MAXINT;
X	
X	pbm_init( &argc, argv );
X	
X	if ( argc > 2 )
X	    pm_usage( "[pbmfile]" );
X	
X	if ( argc == 2 )
X	    ifd = pm_openr( argv[1] );
X	else
X	    ifd = stdin;
X	
X	bits = pbm_readpbm( ifd, &cols, &rows );
X	
X	pm_close( ifd );
X	
X	for (row = 0; row < rows; row++) {
X		for (col = 0; col < cols; col++) {
X			if (bits[row][col] == PBM_BLACK) {
X				if (row < top) {
X					top = row;
X				}
X				if (row > bottom) {
X					bottom = row;
X				}
X				if (col < left) {
X					left = col;
X				}
X				if (col > right) {
X					right = col;
X				}
X			}
X		}
X	}
X
X	printf("%%!PS-Adobe-2.0 EPSF-1.2\n");
X 	printf("%%%%BoundingBox: %d %d %d %d\n", left, rows - bottom, right, rows - top);
X	printf("%%%%BeginPreview: %d %d 1 %d\n", right - left + 1, bottom - top + 1, bottom - top + 1);
X
X	for (row = top; row <= bottom; row++) {
X		printf("%% ");
X		count = 0;
X		for (col = left; col <= right; col += 4) {
X			tot = 0;
X			if (bits[row][col] == PBM_BLACK) {
X				tot += 8;
X			}
X			if (bits[row][col+1] == PBM_BLACK) {
X				tot += 4;
X			}
X			if (bits[row][col+2] == PBM_BLACK) {
X				tot += 2;
X			}
X			if (bits[row][col+3] == PBM_BLACK) {
X				tot++;
X			}
X			printf("%x", tot);
X			count++;
X		}
X		printf((count % 2) == 0 ? "\n" : "0\n");
X	}
X	printf("%%%%EndImage\n");
X	printf("%%%%EndPreview\n");
X
X	exit( 0 );
X}
END_OF_FILE
if test 2248 -ne `wc -c <'pbmtoepsi.c'`; then
    echo shar: \"'pbmtoepsi.c'\" unpacked with wrong size!
fi
# end of 'pbmtoepsi.c'
fi
if test -f 'pstoepsi' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pstoepsi'\"
else
echo shar: Extracting \"'pstoepsi'\" \(1925 characters\)
sed "s/^X//" >'pstoepsi' <<'END_OF_FILE'
X#!/bin/sh
X# Script to convert an arbitrary PostScript image to an encapsulated
X# PostScript image with a bitmap, suitable for incorporation into
X# FrameMaker, LaTeX, troff, etc.
X#
X# Options:
X#	-gs	Use ghostscript to convert to EPSI
X#	-news	Use Sun's X/NeWS (OpenWindows) to convert to EPSI
X#	-strip	Strip all %% directives from the source file -- useful
X#		when dealing with Adobe Illustrator and others who
X#		use directives incompatible with this coversion.
X#
X# Note in the USAGE line below, the source PostScript file must end
X# in a .ps extention.  This is a GhostScript requirement, not mine...
X#
X# I am providing this without any guarantee.
X#
X# Thu Nov 29 10:57:05 EST 1990
X#
X# Doug Crabill dgc@cs.purdue.edu
X
XUSAGE="Usage: $0 [ -gs | -news ] [ -strip ] <file>.ps <file>.epsi"
X
X########################## Edit these variables #####################
XGS='/usr/local/gnu/gs'
XPSTOPPM='/usr/local/gnu/lib/gs/pstoppm.ps'
XPBMTOEPSI='/usr/local/pbm/pbmtoepsi'
XRASTTOPNM='/usr/local/pbm/rasttopnm'
XINTERP='gs'
XSTRIP='false'
XPSTORAST='./pstorast'
X######################################################################
X
Xfor A do
X	case $A in
X	-gs)	INTERP='gs'
X		shift
X		;;
X	-news)	INTERP='news'
X		shift
X		;;
X	-strip)	STRIP='true'
X		shift
X		;;
X	*)	break
X		;;
X	esac
Xdone
X
XBASE=`basename "$1" .ps`
X
Xif [ $# -ne 2 -o ! -f "$1" -o "$1" = "$BASE" ] ; then
X	echo $USAGE 1>&2
X	exit 1
Xfi
X
XTMP1="/tmp/$USER.1.$$"
XTMP2="/tmp/$USER.2.$$"
XTMP3="/tmp/$USER.3.$$"
Xtrap 'rm -f $TMP1 $TMP2 ${BASE}.ppm; exit' 1 2 3 4 13 15
X
Xif [ "true" = "$STRIP" ] ; then
X	awk '/^%%/ { next } {print}' < $1 > $TMP3
Xelse
X	TMP3="$1"
Xfi
X
Xif [ "gs" = "$INTERP" ] ; then
X	$GS -q -dNODISPLAY $PSTOPPM << DGC
X	($BASE) ppm1run
XDGC
X	$PBMTOEPSI ${BASE}.ppm > $TMP1
X	cat $TMP1 $TMP3 > $2
Xelse
X	$PSTORAST -in $TMP3 -out $TMP1
X	$RASTTOPNM < $TMP1 | $PBMTOEPSI > $TMP2
X	cat $TMP2 $TMP3 > $2
Xfi
X
Xif [ "$1" != "$TMP3" ] ; then
X	rm -f $TMP3
Xfi
Xrm -f $TMP1 $TMP2 ${BASE}.ppm
X
Xexit 0
END_OF_FILE
if test 1925 -ne `wc -c <'pstoepsi'`; then
    echo shar: \"'pstoepsi'\" unpacked with wrong size!
fi
chmod +x 'pstoepsi'
# end of 'pstoepsi'
fi
if test -f 'pstorast' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pstorast'\"
else
echo shar: Extracting \"'pstorast'\" \(1632 characters\)
sed "s/^X//" >'pstorast' <<'END_OF_FILE'
X#! /bin/sh
X# @(#)ps2bits 1.2 89/12/12
X# ps2bits - PostScript to Rasterfile converter.
X#
X
XUSAGE="Usage: `basename ${0}` [-in s] [-out s] [-dpi x y] [-size w h] [-color]"
XOUT="pspage"
XIN="%stdin"
XDPIX=72
XDPIY=72
XXSIZE=8.5
XYSIZE=11
XDEPTH=1
X
Xexport OPENWINHOME XNEWSHOME LD_LIBRARY_PATH
X
XOPENWINHOME="${OPENWINHOME-/usr/local/OpenWindows}"
XXNEWSHOME=${OPENWINHOME}
XLD_LIBRARY_PATH=${OPENWINHOME}/lib:/lib
X
Xif [ ! -f $OPENWINHOME/etc/NeWS/redbook.ps ]; then
X    echo "`basename $0`: xnews is not installed correctly in $OPENWINHOME" 1>&2
X    echo "	(set \$OPENWINHOME to where it is installed...)" 1>&2
X    exit 1
Xfi
X
Xwhile [ ${#} -gt 0 ]; do
X    case "${1}" in
X	-color)	shift;
X		DEPTH=8
X	;;
X	-out)	shift;
X		OUT=${1} shift
X	;;
X	-in)	shift;
X		IN=${1} shift
X	;;
X	-size)	shift;
X		XSIZE=${1} shift;
X		YSIZE=${1} shift
X	;;
X	-dpi)	shift;
X		DPIX=${1} shift;
X		DPIY=${1} shift
X	;;
X	*)	echo ${USAGE};
X		exit 0
X	;;
X    esac
Xdone
X
X$OPENWINHOME/bin/xnews -init "
X    /currentpacking false def
X    /setpacking { pop } def
X    (NeWS/basics.ps) (r) file cvx exec
X    (NeWS/redbook.ps) (r) file cvx exec
X
X    500 dict begin	% start userdict
X	false setautobind
X	/bind {} def
X
X	/showpage { copypage erasepage initgraphics } def
X	/_pageno 0 def
X	/copypage {
X	    /_pageno _pageno 1 add store
X	    (${OUT})
X	    clippath writecanvas
X	} def
X
X	${DPIX} ${XSIZE} mul ${DPIY} ${YSIZE} mul ${DEPTH}
X	[ ${DPIX} 72 div 0 0 ${DPIY} 72 div neg 0 7 index ]
X	null buildimage setcanvas
X	erasepage initgraphics
X
X	% hack for bug in folio initialization code.
X	/Courier findfont 10 scalefont setfont () stringwidth pop pop
X
X	(${IN}) (r) file cvx exec
X    shutdownserver
X"
X
END_OF_FILE
if test 1632 -ne `wc -c <'pstorast'`; then
    echo shar: \"'pstorast'\" unpacked with wrong size!
fi
chmod +x 'pstorast'
# end of 'pstorast'
fi
echo shar: End of shell archive.
exit 0