;+
; NAME:
;	MASK_OFF
;
;
; PURPOSE:
;	To set the off sky regions of the image to a specified colour
;
;
; CATEGORY:
;	Utils
;
;
; CALLING SEQUENCE:
;	mask_off, image, index
;
;
; INPUTS:
;	image	byte	The image to mask
;	index	byte	The value to use in the off-sky area
;	projection int	The projection of the image
;
; RESTRICTIONS:
;	The proper plot transform must have been set.
;
;
; MODIFICATION HISTORY:
;	Original: 18/7/03; SJT
;	Handle projection information: 13/2/04; SJT
;-

pro mask_off, image, index, projection

sz = size(image, /dimensions)

if projection eq 0 then begin
    x = replicate(180., 181)
    y = findgen(181)-90
    y = [y, reverse(y)]
    x = [x, -x]

    aitoff, x, y, sx, sy
    dx = (sx-!x.crange[1])*sz[0]/(!x.crange[0]-!x.crange[1])
    dy = (sy+!y.crange[1])*sz[1]/(!y.crange[1]-!y.crange[0])

    mask = bytarr(sz[0], sz[1])

    in = polyfillv(dx, dy, sz[0], sz[1])
    mask[in] = 1b

    out = where(mask eq 0)
endif else if projection eq 1 then begin
    mask = bytarr(sz[0], sz[1])
    th = findgen(361)*!dtor
    r = 135.0
    sx = -r*sin(th)
    sy = r*cos(th)
    dx = (sx-!x.crange[1])*sz[0]/(!x.crange[0]-!x.crange[1])
    dy = (sy+!y.crange[1])*sz[1]/(!y.crange[1]-!y.crange[0])
    in = polyfillv(dx, dy, sz[0], sz[1])
    mask[in] = 1b

    out = where(mask eq 0)
endif else return

image[out] = index

end
