pro msu_tekprint2,img,file=file,x0=x0,y0=y0,xoffset=xoffset,$
	yoffset=yoffset,landscape=landscape,window=window,$
	noprint=noprint,ct=ct,hwtxt=hwtxt,xtxt=xtxt,ytxt=ytxt,col=col
;+
; NAME
;	msu_tekprint2
; PURPOSE
;	To set up the device parameters for making nice centered
;	  and appropriately sized postscript prints on the MSU
;	  Tektronix Phaser IIsd color printer from an IDL bytescaled image.
;	*** BE SURE THE MEDIA TRAYS YOU WANT ARE IN THE PRINTER!! ***
; HOW IT WORKS
;	Give the program the variable name of the image you want to print.
;	If no image name is passed the programs reads the image from the
;	  active window.
;	The program scales the image to fit the page and centers it.  If
;	  want to save the postscript file pass the program the name via
;	  the keyword file=file.  If you wish a different sized picture, or a
;	  picture placed differently on the page than the default values they
;	  you can set the x0, y0, xoffset, and yoffset parameters.
;	The landscape keyword works by rotating the image.  The device
;	  parameter remains portrait.
; CALLING SEQUENCE
;	msu_tekprint2
;	msu_tekprint2,img
;	msu_tekprint2,img,file='imgname.ps'
;	msu_tekprint2,img,file=file,x0=x0,y0=y0,xoffset=xoffset,$
;          yoffset=yoffset,landscape=landscape,window=window
;	msu_tekprint2,img2,file='sxt_mlso.ps',/noprint,ct=ct,$
;	   hwtxt='SXT and MLSO -- 1992 May 8',xtxt=30,ytxt=25,col=255

; OPTIONAL INPUT
;	img = picture you want to print
; OPTIONAL KEYWORD INPUT
;	file = filename (say, 'mypix.ps") you want to give the picture
;	x0<8.21, y0<10.78 are dimensions of allowed print area in inches.
;	xoffset>0,75 and yoffset>1.125 are LH and bottom margins in inches.
;	/landscape will print the image in landscape format.
;	window = window number of the desired image
;	/noprint may be used to create .ps file without making print
;	ct = number of IDL color table or [[r],[g],[b]] array.
;	hwtxt = strarray with text for xyouts2 to write in hardware font
;	xtxt,ytxt = pixel location to write hwtxt
;	col = color to use for hwtxt (default = 255)
; RESTRICTIONS
;	*** BE SURE THE MEDIA TRAYS YOU WANT ARE IN THE PRINTER!! ***
;	The color table range that works seems to be limted to 0-240 
;	  so the program bytscls the image to that range before printing.
;	The pure, unreversed, color tables come too dark.  Normally use a
;	  Gamma Correction of about 0.5 or a little less.
; HISTORY
;	cp_print.pro Written by LWA 8/04/94 after lots of frustration with
;	  the printing process.  Header fixed and bytscl corrected 8/05/94.
;	prepared from cp_print.pro allowing taking image from screen
;	  and making all variables optional.  LWA 9/01/94
;	LWA 10/14/94 - Added xmax, ymax media dimensions and keyword 
;	  /noprint as well as fixing the header.
;	LWA 8/28/95 - Changed scaling to use entire 255 colors by
;	  bytscaling image inside the program.
;	LWA 9/13/95 - Added hardware text capability by calls to
;	  tv3_msu.pro.
;-

; Get an image if it was not passed in

set_plot,'x'  ; Make certain we are reading from the screen.

IF( KEYWORD_SET(window) NE 0) THEN WSET,window ; Get to the right window.
IF ( N_ELEMENTS(img) LE 0 ) THEN BEGIN
                img = TVRD()
ENDIF

picture=bytscl(img)
if keyword_set(landscape) then picture=rotate(picture,1)

siz=size(picture)
x=float(siz(1))
y=float(siz(2))

set_plot,'ps

; Specify maximum media size -- determined by experiment.
xmax=8.21
ymax=10.78
xmarg=0.75
ymarg=1.188

if NOT keyword_set(x0) then x0=xmax-(xmarg*2.)
;Max width of Tektronix image with margin set by xmarg inches.

if NOT keyword_set(y0) then y0=ymax-(ymarg*2.)	
;Max height of Tektronix image with inch margin set by ymarg inches.

if NOT keyword_set(xoffset) then xoffset=xmarg	;X margin
if NOT keyword_set(yoffset) then yoffset=ymarg	;Y margin
if NOT keyword_set(scale_factor) then scale_factor=1.0

; Test to see if x dimension determines scaling
; and if it does then set yoffset to center the image.

if x/y ge x0/y0 then begin
   yoffset=(ymax-((y/x)*x0))/2.
   ppinch_in=x/x0
endif

; If, however, the y dimension must determine the scaling then
; set the x0 print area to produce the proper y scaling (the x
; dimension seems to control all scaling) and compute the new
; xoffset to center the image in x.

if x/y lt x0/y0 then begin
   x0=(x/y)*y0
   xoffset=(xmax-x0)/2
   ppinch_in=y/y0
endif

;device,/color,bits=8
;device,xsize=x0,ysize=y0,xoffset=xoffset,yoffset=yoffset,/inches
;device,scale_factor=scale_factor

if keyword_set(file) then begin
	device,filename=file
endif else begin
	file='idl.ps'
endelse

tv3_msu,/init,x,y,/color,/hwfont,/revcolor
if keyword_set(ct) then begin
   cts=size(ct)
   if cts(0) eq 0 then loadct,ct else tvlct,ct
endif  
tv3_msu,picture,xoffset,yoffset,ppinch=ppinch_in

if keyword_set(hwtxt) then begin
   if NOT keyword_set(xtxt) or NOT keyword_set(ytxt) then begin
      print, "TO USE HARDWARE FONTS YOU MUST SPECIFY xtxt, ytst."
      return
   endif
   if NOT keyword_set(col) then col=255 else col=col
   xyouts2,xtxt,ytxt,'!18'+hwtxt,siz=1.5,color=col
endif

device,/close
set_plot,'x

if NOT keyword_set(noprint) then begin
   cmd='lpr -Pcp ' + file
   spawn,cmd
endif

end

