[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Screen printing

your name wrote:
> Hi,
>         I hope this works... it's the first time I've tried it.. so here goes.
> I've got an application running IDL 5.2 on various platforms including
> X-windows on an Alpha using OpenVMS and Windows NT.
> The application uses the whole screen to display a number of draw
> widgets, menus, buttons, etc.
> Our customers require a button to provide a "screen dump" of the
> displays to various printers. They are adamant they do not wish to
> provide any further interaction (such as using xwd or something
> similar).
> The only method I've sort of found is to TVRD each area in turn and TV
> them in the correct position in the printer output.
> Any suggestions would be gratefully received.
> Regards,
>         Ian

Ian -

Users can get so lazy sometimes, geez!

My WINDOW_TO_FILE.PRO routine might come in handy, so I've included
it and the accompanying .DOC file below. Only problem is that /PRINT
keyword is only supported under UNIX. But you can probably use it
to at least save windows to graphics files (GIF, PICT, TIFF, etc.)
and then do a UNIX- or NT-specific operation to print the file.

You might also find the following command useful, which of course
is also UNIX specific:

262 <host:/export/home/dev/Idl> alias | grep xwd
print_window    xwd | xpr -device ps | lp -d SunPrinter

All the user has to do is use the mouse to select the window (in
this case the IDL main widget) and it gets printed to the specified

Hope this is useful.


    David S. Foster         Univ. of California, San Diego
     Programmer/Analyst     Brain Image Analysis Laboratory
     foster@bial1.ucsd.edu  Department of Psychiatry
     (619) 622-5892         8950 Via La Jolla Drive, Suite 2240
                            La Jolla, CA  92037
;   WINDOW_TO_FILE.PRO   5-14-97  DSFoster   [Unix]
; Routine to save contents of specified window to graphics file.
; Specify format using keyword: GIF, PICT, TIFF, BMP, JPEG. 
; Defaults to TIFF.
; Also use WINDOW_TO_FILE() to print graphics file containing window
; contents, using /PRINT keyword. If the argument FILENAME is the null
; string '' then a temporary file will be created for printing, and
; will be deleted after printing.
; Creates new pixmap window and reads from that, since reading
; from scrollable draws using TVRD() gives incorrect results (IDL bug!).
; Return values are:
;    0 : No error
;	-1 : Error writing to file
;	-2 : Specified window is unavailable or not open
;	-3 : Unknown graphics file format specified
; Modifications:
;  2-19-96 DSF Bug fix: give appropriate X and Y dimensions in 
;          DEVICE, COPY=[] command.
;  7-19-96 DSF Initialize argument "message".
;  5-14-97 DSF Add JPEG format.

FUNCTION window_to_file, window, filename, message, FORMAT=format, PRINT=print

message = ''

if (keyword_set(FORMAT)) then begin
	format = strupcase(format)
endif else begin
	format = 'TIFF'

; Determine "quality" of JPEG image

if (keyword_set(QUALITY)) then begin
	if (quality lt 0 or quality gt 100) then begin
		message, 'Keyword QUALITY must be between 0 and 100', /continue
		return, -3
	endif else if (format ne 'JPEG' and format ne 'JPG') then begin
		message, 'Keyword QUALITY requires FORMAT=JPEG', /continue
		return, -3
	quality = fix( round(quality) )
endif else begin
	quality = 75                                ; Default to 75 (Very good)

device, window_state=windows
if (windows(window) le 0) then begin
	message, 'Window unavailable', /continue
	return, -2

if (filename eq '') then begin                  ; Generate temporary name
	t = string(bin_date(systime()))                  ; Use date/time
	t = t(0) + t(1) + t(2) + t(3) + t(4) + t(5)
	fname = '/tmp/IDL_' + strcompress(t, /remove_all)
endif else begin
	fname = filename
	openw, unit, fname, /get_lun, error=err     ; Else is file writable?
	if (err ne 0) then begin
		message, ' Error writing to file: ' + fname, /continue
		return, -1
	free_lun, unit

old_window = !d.window
wset, window                                    ; Get size of specified window
xsize = !d.x_size
ysize = !d.y_size

window, xsize=xsize, ysize=ysize, /free, /pixmap
DEVICE, copy=[0,0, xsize-1,ysize-1, 0,0, window]
array = tvrd()                                  ; Window contents to array

; Reverse array if necessary
if (!order ne 0 and format ne 'TIFF' and format ne 'TIF') then $
	array = reverse(temporary(array),2)

TVLCT, r,g,b, /GET                              ; Get color table

case (format) of
	'TIFF': tiff_write, fname, array, red=r, green=g, blue=b
	'TIF': tiff_write, fname, array, red=r, green=g, blue=b
	'BMP': write_bmp, fname, array, r,g,b
	'PICT': write_pict, fname, array, r,g,b
	'PIC': write_pict, fname, array, r,g,b
	'GIF': write_gif, fname, array, r,g,b
	'JPEG': write_jpeg, fname, array, quality=quality, order=0
	'JPG': write_jpeg, fname, array, quality=quality, order=0
	else: begin
		message, 'Unknown file format specified with FORMAT: ' + format, $
		if (old_window ne -1) then wset, old_window
		return, -3

if (keyword_set(PRINT)) then begin
	if (filename eq '') then begin       ; Make copy of file for print (-c)
		command = 'lp -c -w ' + fname + ' 2>&1'
	endif else begin
		command = 'lp -w ' + fname + ' 2>&1'

	SPAWN, ["/bin/sh", "-c", command], results, /NOSHELL   ; PRINT IT!

	if (n_params() ge 3) then message = results
	if (filename eq '') then begin       ; Delete temporary file
		ret = delete_files(fname)
		if (ret ne 0) then $
			message, 'Error deleting file: ' + fname, /continue
if (old_window ne -1) then wset, old_window
return, 0

This routine saves the contents of a designated IDL window to
a graphics file. The possible file formats are: GIF, PICT, TIFF,
JPEG and BMP. Also use WINDOW_TO_FILE() to print the contents of
the window to the default printer. You can specify that the window
contents is printed, but no file is saved.

When window contents are printed, uses the UNIX command "lp -w" to

Calling Sequence

  Rtnval = WINDOW_TO_FILE(Window, Filename [, Message])



    This is the IDL window ID of the window whose contents you
    wish to save to file and/or print. A check is made to ensure
    that this is a currently valid window.


    This is the full pathname of the file you wish to save the
    window contents to.

    If Filename is the null string '' and you have set the keyword
    PRINT to print the window contents, no file will be created;
    a temporary file is created, printed, and then deleted.


    If this optional argument is included along with the keyword
    PRINT then the message returned by the UNIX "lp" command is
    returned in this argument. This message will indicate the
    print-job ID if successful, or any error message(s) if not.



    This keyword specifies the graphics file format for the saved
    file. Valid values are:

         TIFF (or TIF)     [Tagged Image File Format]
         PICT (or PIC)     [Macintosh]
         BMP               [Windows Bitmap]
         GIF               [Graphics Interchange Format]
         JPEG              [Joint Photographic Experts Group]

    The default format is TIFF if this keyword is not specified.

    Saving images as JPEG files currently only supports grayscale
    images. You can use the QUALITY keyword to control the quality
    of the lossy compression.

    Note that often certain formats will be required when printing,
    depending upon the available printer(s).


    Set this keyword if you want the window contents printed to the
    default printer. If the argument Filename is the null string ''
    then no file will be created (a temporary file will be created,
    printed, and then deleted).


    Use this keyword to control the quality of the lossy compression
    used when saving JPEG files. Values range from 0 (worst) to 
    100 (best). The default is 75, which is "very good".


    Saves the window contents to a graphics file of specified format,
    unless you are only printing the image, in which case a temporary
    file is created, printed and then deleted.

    Return values are:
       0 : No error
      -1 : Error writing to file
      -2 : Specified window is not open or is unavailable
      -3 : Unknown graphics file format specified with FORMAT


  To save the window contents to a file in GIF format:

    Ret = WINDOW_TO_FILE(Window_id, '/dir/im/picture.tif', $

  To save the window contents to a file in JPEG format and
  specify a "quality" of extremely good:

    Ret = WINDOW_TO_FILE(Window_id, '/dir/im/picture.tif', $
                  FORMAT='GIF', QUALITY=100)

  To print the window contents as a TIFF format, without saving to
  a file:

    Ret = WINDOW_TO_FILE(Window_id, '', FORMAT='TIFF', /PRINT)