;+
;
; NAME: MK_ROOT_BITMAP
;
; PURPOSE: IDL routine program to process a byte image to a bitmap, save,
;	and load onto root window.

PRO mk_root_bitmap, inimage, floyd=floyd

; INPUT PARAMETERS:
;	inimage = byte array image to process
;
; OPTIONAL INPUT KEYWORDS:
;	floyd - if set, use pretty but slow Floyd-Steinberg halftoning.
;		Default uses faster, noisier DOT_PLOTTER halftoning.
;
; METHOD: Routine converts image from 8-bit to 1-bit, saves and
;	displays on root window.  Works on any Unix platform running X11.
;
; IDL PROCEDURES USED:
;	 dot_plotter, wrt_x11_bitmap
; 
; X11 PROCEDURES USED:
;	xwininfo, xsetroot
;
;
; HISTORY:
;	Written April 29, 1993  Barry LaBonte
;	Eliminate temp file  January 3, 1995  BJL
;
;-

image = BYTSCL(inimage)


; Find root window size
SPAWN, 'xwininfo -root | grep ''Width'' | awk ''{print $3}'' ', width
SPAWN, 'xwininfo -root | grep ''Height'' | awk ''{print $3}'' ', height
width = FIX(width(0))
height = FIX(height(0))
; Center location
wwc = width/2
whc = height/2

; Resize to match root window size
big = BYTARR(width, height)
asize = SIZE(image)
nwid = FIX(asize(1))
nht = FIX(asize(2))
; Magnification factor
magnify = width/nwid > height/nht
IF (magnify GT 1) THEN image = REBIN(image, nwid*magnify, nht*magnify)
asize = SIZE(image)
nwid = FIX(asize(1))
nht = FIX(asize(2))

; Insert into window center
iwc = nwid/2
ihc = nht/2
; Actual (half) size
ahw = iwc < wwc
ahh = ihc < whc
; Load and clear memory
big( wwc-ahw:wwc+ahw-1, whc-ahh:whc+ahh-1) = image(iwc-ahw:iwc+ahw-1, ihc-ahh:ihc+ahh-1)


filename = STRTRIM(STRING(width),1)+'x'+STRTRIM(STRING(height),1)
; Delete old image
SPAWN, 'rm $HOME/root.bitmap.'+filename

; Halftone, write as X11 bitmap, load to root window
IF( KEYWORD_SET(floyd) )  THEN  FLOYD_STEINBERG,big,dots $
	  ELSE DOT_PLOTTER,big,dots
WRT_X11_BITMAP,dots,'$HOME/root.bitmap.'+filename
SPAWN, 'xsetroot -def'
SPAWN, 'xsetroot -bg yellow -fg navy -bitmap $HOME/root.bitmap.'+filename

RETURN
END
