;+
;  IDL routine to write out a byte array as an X11 bitmap

PRO wrt_x11_bitmap, array, filename

; Input:
;	array = byte array, assumed to be 0 or 1
;	filename = name of file to write to
; Use X11 command  xsetroot to load bitmap onto the root window.
;
;   Written April, 1993   Barry LaBonte
;-


; How big?
asize = SIZE(array)
dim1 = asize(1)
dim2 = asize(2)

; Make the target array
; Must be multiple of 8 bits per line
; Do it in nibble to get leading zeroes (avoid blanks) on output
dimout = (dim1/8)*2
bitmap = BYTARR(dimout,dim2)
b15 = BYTE(15)
index=INDGEN(dimout) + 1
index = REFORM(index,2,dimout/2)
index(1,*) = index(1,*) - 2
index = REFORM(index,dimout)

; Leftmost bit of image is least significant bit of first byte
bitvect = BYTARR(dimout*4)
FOR j=0,dim2-1 DO BEGIN
; Flip image top for bottom
	bitvect(0:dim1-1) = array(*,dim2-j-1)
	bitvect = REFORM(bitvect,4,dimout)
; Load bits by shifting
	FOR i=1,3 DO BEGIN
		bitvect(i,*) = ISHFT(bitvect(i,*),i)
	ENDFOR
; Sum shifted bits
	temp = bitvect(0,*)+bitvect(1,*)+bitvect(2,*)+bitvect(3,*)
	temp = REFORM(temp)
; Store so the even nibbles come first for printout
	bitmap(*,j) = b15 - temp(index)
	bitvect = REFORM(bitvect,dimout*4)
ENDFOR

; Print out header
GET_LUN,unit
OPENW,unit,filename
PRINTF,unit,FORMAT='("#define ",a,"_width ",i)',filename,dimout*4
PRINTF,unit,FORMAT='("#define ",a,"_height ",i)',filename,dim2
PRINTF,unit,FORMAT='("static char ",a,"_bits[] = {")',filename

; Now the array
PRINTF,unit,FORMAT='(2x,12("0x",2z1,",")/)', bitmap

; The tail
PRINTF,unit,'  };'
CLOSE,unit

RETURN
END
